FileOperationsHelper.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author masensio
  5. * @author David A. Velasco
  6. * Copyright (C) 2015 ownCloud Inc.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. package com.owncloud.android.files;
  22. import android.accounts.Account;
  23. import android.content.ActivityNotFoundException;
  24. import android.content.Context;
  25. import android.content.Intent;
  26. import android.content.pm.PackageManager;
  27. import android.content.pm.ResolveInfo;
  28. import android.net.Uri;
  29. import android.os.Parcelable;
  30. import android.support.v4.app.DialogFragment;
  31. import android.webkit.MimeTypeMap;
  32. import android.widget.Toast;
  33. import com.owncloud.android.R;
  34. import com.owncloud.android.authentication.AccountUtils;
  35. import com.owncloud.android.datamodel.OCFile;
  36. import com.owncloud.android.db.OCUpload;
  37. import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
  38. import com.owncloud.android.files.services.FileUploadService.FileUploaderBinder;
  39. import com.owncloud.android.lib.common.network.WebdavUtils;
  40. import com.owncloud.android.lib.common.utils.Log_OC;
  41. import com.owncloud.android.lib.resources.shares.ShareType;
  42. import com.owncloud.android.lib.resources.status.OwnCloudVersion;
  43. import com.owncloud.android.services.OperationsService;
  44. import com.owncloud.android.services.observer.FileObserverService;
  45. import com.owncloud.android.ui.activity.FileActivity;
  46. import com.owncloud.android.ui.activity.ShareActivity;
  47. import com.owncloud.android.ui.dialog.ShareLinkToDialog;
  48. import com.owncloud.android.ui.dialog.SharePasswordDialogFragment;
  49. import java.util.List;
  50. /**
  51. *
  52. */
  53. public class FileOperationsHelper {
  54. private static final String TAG = FileOperationsHelper.class.getSimpleName();
  55. private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
  56. protected FileActivity mFileActivity = null;
  57. /// Identifier of operation in progress which result shouldn't be lost
  58. private long mWaitingForOpId = Long.MAX_VALUE;
  59. public FileOperationsHelper(FileActivity fileActivity) {
  60. mFileActivity = fileActivity;
  61. }
  62. public void openFile(OCFile file) {
  63. if (file != null) {
  64. String storagePath = file.getStoragePath();
  65. String encodedStoragePath = WebdavUtils.encodePath(storagePath);
  66. Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
  67. intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath),
  68. file.getMimetype());
  69. intentForSavedMimeType.setFlags(
  70. Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
  71. );
  72. Intent intentForGuessedMimeType = null;
  73. if (storagePath.lastIndexOf('.') >= 0) {
  74. String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
  75. storagePath.substring(storagePath.lastIndexOf('.') + 1)
  76. );
  77. if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
  78. intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
  79. intentForGuessedMimeType.setDataAndType(Uri.parse("file://" +
  80. encodedStoragePath), guessedMimeType);
  81. intentForGuessedMimeType.setFlags(
  82. Intent.FLAG_GRANT_READ_URI_PERMISSION |
  83. Intent.FLAG_GRANT_WRITE_URI_PERMISSION
  84. );
  85. }
  86. }
  87. Intent openFileWithIntent;
  88. if (intentForGuessedMimeType != null) {
  89. openFileWithIntent = intentForGuessedMimeType;
  90. } else {
  91. openFileWithIntent = intentForSavedMimeType;
  92. }
  93. List<ResolveInfo> launchables = mFileActivity.getPackageManager().
  94. queryIntentActivities(openFileWithIntent, PackageManager.GET_INTENT_FILTERS);
  95. if(launchables != null && launchables.size() > 0) {
  96. try {
  97. mFileActivity.startActivity(
  98. Intent.createChooser(
  99. openFileWithIntent, mFileActivity.getString(R.string.actionbar_open_with)
  100. )
  101. );
  102. } catch (ActivityNotFoundException anfe) {
  103. showNoAppForFileTypeToast(mFileActivity.getApplicationContext());
  104. }
  105. } else {
  106. showNoAppForFileTypeToast(mFileActivity.getApplicationContext());
  107. }
  108. } else {
  109. Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
  110. }
  111. }
  112. /**
  113. * Displays a toast stating that no application could be found to open the file.
  114. *
  115. * @param context the context to be able to show a toast.
  116. */
  117. private void showNoAppForFileTypeToast(Context context) {
  118. Toast.makeText(context,
  119. R.string.file_list_no_app_for_file_type, Toast.LENGTH_SHORT)
  120. .show();
  121. }
  122. /**
  123. * Helper method to share a file via a public link. Starts a request to do it in {@link OperationsService}
  124. *
  125. * @param file The file to share.
  126. * @param password Optional password to protect the public share.
  127. */
  128. public void shareFileViaLink(OCFile file, String password) {
  129. if (isSharedSupported()) {
  130. if (file != null) {
  131. mFileActivity.showLoadingDialog(
  132. mFileActivity.getApplicationContext().
  133. getString(R.string.wait_a_moment)
  134. );
  135. Intent service = new Intent(mFileActivity, OperationsService.class);
  136. service.setAction(OperationsService.ACTION_CREATE_SHARE_VIA_LINK);
  137. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  138. if (password != null && password.length() > 0) {
  139. service.putExtra(OperationsService.EXTRA_SHARE_PASSWORD, password);
  140. }
  141. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  142. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  143. } else {
  144. Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
  145. // TODO user-level error?
  146. }
  147. } else {
  148. // Show a Message
  149. Toast t = Toast.makeText(
  150. mFileActivity, mFileActivity.getString(R.string.share_link_no_support_share_api),
  151. Toast.LENGTH_LONG
  152. );
  153. t.show();
  154. }
  155. }
  156. public void getFileWithLink(OCFile file){
  157. if (isSharedSupported()) {
  158. if (file != null) {
  159. mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
  160. getString(R.string.wait_a_moment));
  161. Intent service = new Intent(mFileActivity, OperationsService.class);
  162. service.setAction(OperationsService.ACTION_CREATE_SHARE_VIA_LINK);
  163. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  164. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  165. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  166. } else {
  167. Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
  168. }
  169. } else {
  170. // Show a Message
  171. Toast t = Toast.makeText(
  172. mFileActivity, mFileActivity.getString(R.string.share_link_no_support_share_api),
  173. Toast.LENGTH_LONG
  174. );
  175. t.show();
  176. }
  177. }
  178. public void shareFileWithLinkToApp(OCFile file, String password, Intent sendIntent) {
  179. if (file != null) {
  180. mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
  181. getString(R.string.wait_a_moment));
  182. Intent service = new Intent(mFileActivity, OperationsService.class);
  183. service.setAction(OperationsService.ACTION_CREATE_SHARE_VIA_LINK);
  184. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  185. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  186. service.putExtra(OperationsService.EXTRA_SHARE_PASSWORD, password);
  187. service.putExtra(OperationsService.EXTRA_SEND_INTENT, sendIntent);
  188. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  189. } else {
  190. Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
  191. }
  192. }
  193. /**
  194. * Helper method to share a file with a known sharee. Starts a request to do it in {@link OperationsService}
  195. *
  196. * @param file The file to share.
  197. * @param shareeName Name (user name or group name) of the target sharee.
  198. * @param shareType The share type determines the sharee type.
  199. */
  200. public void shareFileWithSharee(OCFile file, String shareeName, ShareType shareType) {
  201. if (file != null) {
  202. // TODO check capability?
  203. mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
  204. getString(R.string.wait_a_moment));
  205. Intent service = new Intent(mFileActivity, OperationsService.class);
  206. service.setAction(OperationsService.ACTION_CREATE_SHARE_WITH_SHAREE);
  207. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  208. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  209. service.putExtra(OperationsService.EXTRA_SHARE_WITH, shareeName);
  210. service.putExtra(OperationsService.EXTRA_SHARE_TYPE, shareType);
  211. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  212. } else {
  213. Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
  214. }
  215. }
  216. /**
  217. * @return 'True' if the server supports the Share API
  218. */
  219. public boolean isSharedSupported() {
  220. if (mFileActivity.getAccount() != null) {
  221. OwnCloudVersion serverVersion = AccountUtils.getServerVersion(mFileActivity.getAccount());
  222. return (serverVersion != null && serverVersion.isSharedSupported());
  223. }
  224. return false;
  225. }
  226. /**
  227. * Helper method to unshare a file publicly shared via link.
  228. * Starts a request to do it in {@link OperationsService}
  229. *
  230. * @param file The file to unshare.
  231. */
  232. public void unshareFileViaLink(OCFile file) {
  233. // Unshare the file: Create the intent
  234. Intent unshareService = new Intent(mFileActivity, OperationsService.class);
  235. unshareService.setAction(OperationsService.ACTION_UNSHARE);
  236. unshareService.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  237. unshareService.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  238. unshareService.putExtra(OperationsService.EXTRA_SHARE_TYPE, ShareType.PUBLIC_LINK);
  239. unshareService.putExtra(OperationsService.EXTRA_SHARE_WITH, "");
  240. queueShareIntent(unshareService);
  241. }
  242. public void unshareFileWithUserOrGroup(OCFile file, ShareType shareType, String userOrGroup){
  243. // Unshare the file: Create the intent
  244. Intent unshareService = new Intent(mFileActivity, OperationsService.class);
  245. unshareService.setAction(OperationsService.ACTION_UNSHARE);
  246. unshareService.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  247. unshareService.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  248. unshareService.putExtra(OperationsService.EXTRA_SHARE_TYPE, shareType);
  249. unshareService.putExtra(OperationsService.EXTRA_SHARE_WITH, userOrGroup);
  250. queueShareIntent(unshareService);
  251. }
  252. private void queueShareIntent(Intent shareIntent){
  253. if (isSharedSupported()) {
  254. // Unshare the file
  255. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().
  256. queueNewOperation(shareIntent);
  257. mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
  258. getString(R.string.wait_a_moment));
  259. } else {
  260. // Show a Message
  261. Toast t = Toast.makeText(mFileActivity,
  262. mFileActivity.getString(R.string.share_link_no_support_share_api),
  263. Toast.LENGTH_LONG);
  264. t.show();
  265. }
  266. }
  267. /**
  268. * Show an instance of {@link ShareType} for sharing or unsharing the {@OCFile} received as parameter.
  269. *
  270. * @param file File to share or unshare.
  271. */
  272. public void showShareFile(OCFile file){
  273. Intent intent = new Intent(mFileActivity, ShareActivity.class);
  274. intent.putExtra(mFileActivity.EXTRA_FILE, (Parcelable) file);
  275. intent.putExtra(mFileActivity.EXTRA_ACCOUNT, mFileActivity.getAccount());
  276. mFileActivity.startActivity(intent);
  277. }
  278. /**
  279. * Starts a dialog that requests a password to the user to protect a share link.
  280. *
  281. * @param file File which public share will be protected by the requested password
  282. * @param createShare When 'true', the request for password will be followed by the creation of a new
  283. * public link; when 'false', a public share is assumed to exist, and the password
  284. * is bound to it.
  285. */
  286. public void requestPasswordForShareViaLink(OCFile file, boolean createShare) {
  287. SharePasswordDialogFragment dialog =
  288. SharePasswordDialogFragment.newInstance(file, createShare);
  289. dialog.show(
  290. mFileActivity.getSupportFragmentManager(),
  291. SharePasswordDialogFragment.PASSWORD_FRAGMENT
  292. );
  293. }
  294. /**
  295. * Updates a public share on a file to set its password.
  296. * Starts a request to do it in {@link OperationsService}
  297. *
  298. * @param file File which public share will be protected with a password.
  299. * @param password Password to set for the public link; null or empty string to clear
  300. * the current password
  301. */
  302. public void setPasswordToShareViaLink(OCFile file, String password) {
  303. // Set password updating share
  304. Intent updateShareIntent = new Intent(mFileActivity, OperationsService.class);
  305. updateShareIntent.setAction(OperationsService.ACTION_UPDATE_SHARE);
  306. updateShareIntent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  307. updateShareIntent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  308. updateShareIntent.putExtra(
  309. OperationsService.EXTRA_SHARE_PASSWORD,
  310. (password == null) ? "" : password
  311. );
  312. queueShareIntent(updateShareIntent);
  313. }
  314. /**
  315. * Updates a public share on a file to set its expiration date.
  316. * Starts a request to do it in {@link OperationsService}
  317. *
  318. * @param file File which public share will be constrained with an expiration date.
  319. * @param expirationTimeInMillis Expiration date to set. A negative value clears the current expiration
  320. * date, leaving the link unrestricted. Zero makes no change.
  321. */
  322. public void setExpirationDateToShareViaLink(OCFile file, long expirationTimeInMillis) {
  323. Intent updateShareIntent = new Intent(mFileActivity, OperationsService.class);
  324. updateShareIntent.setAction(OperationsService.ACTION_UPDATE_SHARE);
  325. updateShareIntent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  326. updateShareIntent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  327. updateShareIntent.putExtra(
  328. OperationsService.EXTRA_SHARE_EXPIRATION_DATE_IN_MILLIS,
  329. expirationTimeInMillis
  330. );
  331. queueShareIntent(updateShareIntent);
  332. }
  333. /**
  334. * @return 'True' if the server supports the Search Users API
  335. */
  336. public boolean isSearchUsersSupportedSupported() {
  337. if (mFileActivity.getAccount() != null) {
  338. OwnCloudVersion serverVersion = AccountUtils.getServerVersion(mFileActivity.getAccount());
  339. return (serverVersion != null && serverVersion.isSearchUsersSupported());
  340. }
  341. return false;
  342. }
  343. public void sendDownloadedFile(OCFile file) {
  344. if (file != null) {
  345. String storagePath = file.getStoragePath();
  346. String encodedStoragePath = WebdavUtils.encodePath(storagePath);
  347. Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
  348. // set MimeType
  349. sendIntent.setType(file.getMimetype());
  350. sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + encodedStoragePath));
  351. sendIntent.putExtra(Intent.ACTION_SEND, true); // Send Action
  352. // Show dialog, without the own app
  353. String[] packagesToExclude = new String[]{mFileActivity.getPackageName()};
  354. DialogFragment chooserDialog = ShareLinkToDialog.newInstance(sendIntent, packagesToExclude);
  355. chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
  356. } else {
  357. Log_OC.wtf(TAG, "Trying to send a NULL OCFile");
  358. }
  359. }
  360. /**
  361. * Request the synchronization of a file or folder with the OC server, including its contents.
  362. *
  363. * @param file The file or folder to synchronize
  364. */
  365. public void syncFile(OCFile file) {
  366. if (!file.isFolder()){
  367. Intent intent = new Intent(mFileActivity, OperationsService.class);
  368. intent.setAction(OperationsService.ACTION_SYNC_FILE);
  369. intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  370. intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  371. intent.putExtra(OperationsService.EXTRA_SYNC_FILE_CONTENTS, true);
  372. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(intent);
  373. mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
  374. getString(R.string.wait_a_moment));
  375. } else {
  376. Intent intent = new Intent(mFileActivity, OperationsService.class);
  377. intent.setAction(OperationsService.ACTION_SYNC_FOLDER);
  378. intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  379. intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  380. mFileActivity.startService(intent);
  381. }
  382. }
  383. public void toggleFavorite(OCFile file, boolean isFavorite) {
  384. file.setFavorite(isFavorite);
  385. mFileActivity.getStorageManager().saveFile(file);
  386. /// register the OCFile instance in the observer service to monitor local updates
  387. Intent observedFileIntent = FileObserverService.makeObservedFileIntent(
  388. mFileActivity,
  389. file,
  390. mFileActivity.getAccount(),
  391. isFavorite);
  392. mFileActivity.startService(observedFileIntent);
  393. /// immediate content synchronization
  394. if (file.isFavorite()) {
  395. syncFile(file);
  396. }
  397. }
  398. public void renameFile(OCFile file, String newFilename) {
  399. // RenameFile
  400. Intent service = new Intent(mFileActivity, OperationsService.class);
  401. service.setAction(OperationsService.ACTION_RENAME);
  402. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  403. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  404. service.putExtra(OperationsService.EXTRA_NEWNAME, newFilename);
  405. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  406. mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
  407. getString(R.string.wait_a_moment));
  408. }
  409. public void removeFile(OCFile file, boolean onlyLocalCopy) {
  410. // RemoveFile
  411. Intent service = new Intent(mFileActivity, OperationsService.class);
  412. service.setAction(OperationsService.ACTION_REMOVE);
  413. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  414. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  415. service.putExtra(OperationsService.EXTRA_REMOVE_ONLY_LOCAL, onlyLocalCopy);
  416. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  417. mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
  418. getString(R.string.wait_a_moment));
  419. }
  420. public void createFolder(String remotePath, boolean createFullPath) {
  421. // Create Folder
  422. Intent service = new Intent(mFileActivity, OperationsService.class);
  423. service.setAction(OperationsService.ACTION_CREATE_FOLDER);
  424. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  425. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, remotePath);
  426. service.putExtra(OperationsService.EXTRA_CREATE_FULL_PATH, createFullPath);
  427. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  428. mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
  429. getString(R.string.wait_a_moment));
  430. }
  431. /**
  432. * Retry uploading a failed or cancelled upload with force.
  433. */
  434. public void retryUpload(OCUpload upload) {
  435. Account account = mFileActivity.getAccount();
  436. FileUploaderBinder uploaderBinder = mFileActivity.getFileUploaderBinder();
  437. if (uploaderBinder != null) {
  438. upload.removeAllUploadRestrictions(); //only this object, upload DB stays untouched.
  439. uploaderBinder.retry(account, upload);
  440. } else {
  441. Log_OC.w(TAG, "uploaderBinder not set. Cannot remove " + upload.getOCFile());
  442. }
  443. }
  444. /**
  445. * Remove upload from upload list.
  446. */
  447. public void removeUploadFromList(OCUpload upload) {
  448. FileUploaderBinder uploaderBinder = mFileActivity.getFileUploaderBinder();
  449. if (uploaderBinder != null) {
  450. uploaderBinder.remove(upload);
  451. } else {
  452. Log_OC.w(TAG, "uploaderBinder not set. Cannot remove " + upload.getOCFile());
  453. }
  454. }
  455. /**
  456. * Cancel the transference in downloads (files/folders) and file uploads
  457. * @param file OCFile
  458. */
  459. public void cancelTransference(OCFile file) {
  460. Account account = mFileActivity.getAccount();
  461. if (file.isFolder()) {
  462. OperationsService.OperationsServiceBinder opsBinder =
  463. mFileActivity.getOperationsServiceBinder();
  464. if (opsBinder != null) {
  465. opsBinder.cancel(account, file);
  466. }
  467. }
  468. // for both files and folders
  469. FileDownloaderBinder downloaderBinder = mFileActivity.getFileDownloaderBinder();
  470. // On reliable_uploads
  471. // FileUploaderBinder uploaderBinder = mFileActivity.getFileUploaderBinder();
  472. // if (downloaderBinder != null) {
  473. // if (downloaderBinder.isDownloading(account, file)) {
  474. // // Remove etag for parent, if file is a keep_in_sync
  475. // if (file.isFavorite()) {
  476. // OCFile parent = mFileActivity.getStorageManager().getFileById(file.getParentId());
  477. // parent.setEtag("");
  478. // mFileActivity.getStorageManager().saveFile(parent);
  479. // }
  480. //
  481. // downloaderBinder.cancel(account, file);
  482. // } else {
  483. // Log_OC.d(TAG, "Download for " + file + " not in progress. Cannot cancel " + file);
  484. // }
  485. // }
  486. // if (uploaderBinder != null) {
  487. // if (uploaderBinder.isUploading(account, file)) {
  488. // uploaderBinder.cancel(account, file);
  489. // } else {
  490. // Log_OC.d(TAG, "Upload for " + file + " not in progress. Cannot cancel.");
  491. // }
  492. // }
  493. // if(downloaderBinder == null && uploaderBinder == null) {
  494. // Log_OC.w(TAG, "Neither downloaderBinder nor uploaderBinder set. Cannot cancel.");
  495. if (downloaderBinder != null && downloaderBinder.isDownloading(account, file)) {
  496. downloaderBinder.cancel(account, file);
  497. }
  498. FileUploaderBinder uploaderBinder = mFileActivity.getFileUploaderBinder();
  499. if (uploaderBinder != null && uploaderBinder.isUploading(account, file)) {
  500. uploaderBinder.cancel(account, file);
  501. }
  502. }
  503. /**
  504. * Start move file operation
  505. *
  506. * @param newfile File where it is going to be moved
  507. * @param currentFile File with the previous info
  508. */
  509. public void moveFile(OCFile newfile, OCFile currentFile) {
  510. // Move files
  511. Intent service = new Intent(mFileActivity, OperationsService.class);
  512. service.setAction(OperationsService.ACTION_MOVE_FILE);
  513. service.putExtra(OperationsService.EXTRA_NEW_PARENT_PATH, newfile.getRemotePath());
  514. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, currentFile.getRemotePath());
  515. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  516. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  517. mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
  518. getString(R.string.wait_a_moment));
  519. }
  520. /**
  521. * Start copy file operation
  522. *
  523. * @param newfile File where it is going to be moved
  524. * @param currentFile File with the previous info
  525. */
  526. public void copyFile(OCFile newfile, OCFile currentFile) {
  527. // Copy files
  528. Intent service = new Intent(mFileActivity, OperationsService.class);
  529. service.setAction(OperationsService.ACTION_COPY_FILE);
  530. service.putExtra(OperationsService.EXTRA_NEW_PARENT_PATH, newfile.getRemotePath());
  531. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, currentFile.getRemotePath());
  532. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  533. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  534. mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
  535. getString(R.string.wait_a_moment));
  536. }
  537. public long getOpIdWaitingFor() {
  538. return mWaitingForOpId;
  539. }
  540. public void setOpIdWaitingFor(long waitingForOpId) {
  541. mWaitingForOpId = waitingForOpId;
  542. }
  543. /**
  544. * @return 'True' if the server doesn't need to check forbidden characters
  545. */
  546. public boolean isVersionWithForbiddenCharacters() {
  547. if (mFileActivity.getAccount() != null) {
  548. OwnCloudVersion serverVersion =
  549. AccountUtils.getServerVersion(mFileActivity.getAccount());
  550. return (serverVersion != null && serverVersion.isVersionWithForbiddenCharacters());
  551. }
  552. return false;
  553. }
  554. }