FileOperationsHelper.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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.support.v4.app.DialogFragment;
  30. import android.webkit.MimeTypeMap;
  31. import android.widget.Toast;
  32. import com.owncloud.android.R;
  33. import com.owncloud.android.authentication.AccountUtils;
  34. import com.owncloud.android.datamodel.OCFile;
  35. import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
  36. import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
  37. import com.owncloud.android.lib.common.network.WebdavUtils;
  38. import com.owncloud.android.lib.common.utils.Log_OC;
  39. import com.owncloud.android.lib.resources.shares.ShareType;
  40. import com.owncloud.android.lib.resources.status.OwnCloudVersion;
  41. import com.owncloud.android.services.OperationsService;
  42. import com.owncloud.android.services.observer.FileObserverService;
  43. import com.owncloud.android.ui.activity.FileActivity;
  44. import com.owncloud.android.ui.activity.ShareActivity;
  45. import com.owncloud.android.ui.dialog.ShareLinkToDialog;
  46. import com.owncloud.android.ui.dialog.SharePasswordDialogFragment;
  47. import org.apache.http.protocol.HTTP;
  48. import java.util.List;
  49. /**
  50. *
  51. */
  52. public class FileOperationsHelper {
  53. private static final String TAG = FileOperationsHelper.class.getName();
  54. private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
  55. protected FileActivity mFileActivity = null;
  56. /// Identifier of operation in progress which result shouldn't be lost
  57. private long mWaitingForOpId = Long.MAX_VALUE;
  58. public FileOperationsHelper(FileActivity fileActivity) {
  59. mFileActivity = fileActivity;
  60. }
  61. public void openFile(OCFile file) {
  62. if (file != null) {
  63. String storagePath = file.getStoragePath();
  64. String encodedStoragePath = WebdavUtils.encodePath(storagePath);
  65. Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
  66. intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath),
  67. file.getMimetype());
  68. intentForSavedMimeType.setFlags(
  69. Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION
  70. );
  71. Intent intentForGuessedMimeType = null;
  72. if (storagePath.lastIndexOf('.') >= 0) {
  73. String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
  74. storagePath.substring(storagePath.lastIndexOf('.') + 1)
  75. );
  76. if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
  77. intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
  78. intentForGuessedMimeType.
  79. setDataAndType(Uri.parse("file://"+ encodedStoragePath),
  80. 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. */
  127. public void shareFileViaLink(OCFile file) {
  128. if (isSharedSupported()) {
  129. if (file != null) {
  130. mFileActivity.showLoadingDialog(
  131. mFileActivity.getApplicationContext().
  132. getString(R.string.wait_a_moment)
  133. );
  134. Intent service = new Intent(mFileActivity, OperationsService.class);
  135. service.setAction(OperationsService.ACTION_CREATE_SHARE_VIA_LINK);
  136. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  137. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  138. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  139. } else {
  140. Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
  141. // TODO user-level error?
  142. }
  143. } else {
  144. // Show a Message
  145. Toast t = Toast.makeText(
  146. mFileActivity, mFileActivity.getString(R.string.share_link_no_support_share_api),
  147. Toast.LENGTH_LONG
  148. );
  149. t.show();
  150. }
  151. }
  152. public void getFileWithLink(OCFile file){
  153. if (isSharedSupported()) {
  154. if (file != null) {
  155. mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
  156. getString(R.string.wait_a_moment));
  157. Intent service = new Intent(mFileActivity, OperationsService.class);
  158. service.setAction(OperationsService.ACTION_CREATE_SHARE_VIA_LINK);
  159. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  160. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  161. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  162. } else {
  163. Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
  164. }
  165. } else {
  166. // Show a Message
  167. Toast t = Toast.makeText(
  168. mFileActivity, mFileActivity.getString(R.string.share_link_no_support_share_api),
  169. Toast.LENGTH_LONG
  170. );
  171. t.show();
  172. }
  173. }
  174. public void shareFileWithLinkOLD(OCFile file) {
  175. if (isSharedSupported()) {
  176. if (file != null) {
  177. String link = "https://fake.url";
  178. Intent intent = createShareWithLinkIntent(link);
  179. String[] packagesToExclude = new String[]{mFileActivity.getPackageName()};
  180. DialogFragment chooserDialog = ShareLinkToDialog.newInstance(intent,
  181. packagesToExclude, file);
  182. chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
  183. } else {
  184. Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
  185. }
  186. } else {
  187. // Show a Message
  188. Toast t = Toast.makeText(
  189. mFileActivity, mFileActivity.getString(R.string.share_link_no_support_share_api),
  190. Toast.LENGTH_LONG
  191. );
  192. t.show();
  193. }
  194. }
  195. public void shareFileWithLinkToApp(OCFile file, String password, Intent sendIntent) {
  196. if (file != null) {
  197. mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
  198. getString(R.string.wait_a_moment));
  199. Intent service = new Intent(mFileActivity, OperationsService.class);
  200. service.setAction(OperationsService.ACTION_CREATE_SHARE_VIA_LINK);
  201. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  202. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  203. service.putExtra(OperationsService.EXTRA_SHARE_PASSWORD, password);
  204. service.putExtra(OperationsService.EXTRA_SEND_INTENT, sendIntent);
  205. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  206. } else {
  207. Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
  208. }
  209. }
  210. private Intent createShareWithLinkIntent(String link) {
  211. Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
  212. intentToShareLink.putExtra(Intent.EXTRA_TEXT, link);
  213. intentToShareLink.setType(HTTP.PLAIN_TEXT_TYPE);
  214. return intentToShareLink;
  215. }
  216. /**
  217. * Helper method to share a file with a known sharee. Starts a request to do it in {@link OperationsService}
  218. *
  219. * @param file The file to share.
  220. * @param shareeName Name (user name or group name) of the target sharee.
  221. * @param shareType The share type determines the sharee type.
  222. */
  223. public void shareFileWithSharee(OCFile file, String shareeName, ShareType shareType) {
  224. if (file != null) {
  225. // TODO check capability?
  226. mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
  227. getString(R.string.wait_a_moment));
  228. Intent service = new Intent(mFileActivity, OperationsService.class);
  229. service.setAction(OperationsService.ACTION_CREATE_SHARE_WITH_SHAREE);
  230. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  231. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  232. service.putExtra(OperationsService.EXTRA_SHARE_WITH, shareeName);
  233. service.putExtra(OperationsService.EXTRA_SHARE_TYPE, shareType);
  234. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  235. } else {
  236. Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
  237. }
  238. }
  239. /**
  240. * @return 'True' if the server supports the Share API
  241. */
  242. public boolean isSharedSupported() {
  243. if (mFileActivity.getAccount() != null) {
  244. OwnCloudVersion serverVersion = AccountUtils.getServerVersion(mFileActivity.getAccount());
  245. return (serverVersion != null && serverVersion.isSharedSupported());
  246. }
  247. return false;
  248. }
  249. /**
  250. * Helper method to unshare a file publicly shared via link.
  251. * Starts a request to do it in {@link OperationsService}
  252. *
  253. * @param file The file to unshare.
  254. */
  255. public void unshareFileViaLink(OCFile file) {
  256. // Unshare the file: Create the intent
  257. Intent unshareService = new Intent(mFileActivity, OperationsService.class);
  258. unshareService.setAction(OperationsService.ACTION_UNSHARE);
  259. unshareService.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  260. unshareService.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  261. unshareService.putExtra(OperationsService.EXTRA_SHARE_TYPE, ShareType.PUBLIC_LINK);
  262. unshareService.putExtra(OperationsService.EXTRA_SHARE_WITH, "");
  263. queueShareIntent(unshareService);
  264. }
  265. public void unshareFileWithUserOrGroup(OCFile file, ShareType shareType, String userOrGroup){
  266. // Unshare the file: Create the intent
  267. Intent unshareService = new Intent(mFileActivity, OperationsService.class);
  268. unshareService.setAction(OperationsService.ACTION_UNSHARE);
  269. unshareService.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  270. unshareService.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  271. unshareService.putExtra(OperationsService.EXTRA_SHARE_TYPE, shareType);
  272. unshareService.putExtra(OperationsService.EXTRA_SHARE_WITH, userOrGroup);
  273. queueShareIntent(unshareService);
  274. }
  275. private void queueShareIntent(Intent shareIntent){
  276. if (isSharedSupported()) {
  277. // Unshare the file
  278. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().
  279. queueNewOperation(shareIntent);
  280. mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
  281. getString(R.string.wait_a_moment));
  282. } else {
  283. // Show a Message
  284. Toast t = Toast.makeText(mFileActivity,
  285. mFileActivity.getString(R.string.share_link_no_support_share_api),
  286. Toast.LENGTH_LONG);
  287. t.show();
  288. }
  289. }
  290. /**
  291. * Show an instance of {@link ShareType} for sharing or unsharing the {@OCFile} received as parameter.
  292. *
  293. * @param file File to share or unshare.
  294. */
  295. public void showShareFile(OCFile file){
  296. Intent intent = new Intent(mFileActivity, ShareActivity.class);
  297. intent.putExtra(mFileActivity.EXTRA_FILE, file);
  298. intent.putExtra(mFileActivity.EXTRA_ACCOUNT, mFileActivity.getAccount());
  299. mFileActivity.startActivity(intent);
  300. }
  301. /**
  302. * Starts a dialog that requests a password to the user to protect a share link.
  303. *
  304. * @param file File which public share will be protected by the requested password
  305. */
  306. public void requestPasswordForShareViaLink(OCFile file) {
  307. SharePasswordDialogFragment dialog =
  308. SharePasswordDialogFragment.newInstance(
  309. file,
  310. null
  311. );
  312. dialog.show(
  313. mFileActivity.getSupportFragmentManager(),
  314. SharePasswordDialogFragment.PASSWORD_FRAGMENT
  315. );
  316. }
  317. /**
  318. * Updates a public share on a file to set its password.
  319. * Starts a request to do it in {@link OperationsService}
  320. *
  321. * @param file File which public share will be protected with a password.
  322. * @param password Password to set for the public link; null or empty string to clear
  323. * the current password
  324. */
  325. public void setPasswordToShareViaLink(OCFile file, String password) {
  326. // Set password updating share
  327. Intent updateShareIntent = new Intent(mFileActivity, OperationsService.class);
  328. updateShareIntent.setAction(OperationsService.ACTION_UPDATE_SHARE);
  329. updateShareIntent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  330. updateShareIntent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  331. updateShareIntent.putExtra(
  332. OperationsService.EXTRA_SHARE_PASSWORD,
  333. (password == null) ? "" : password
  334. );
  335. queueShareIntent(updateShareIntent);
  336. }
  337. /**
  338. * Updates a public share on a file to set its expiration date.
  339. * Starts a request to do it in {@link OperationsService}
  340. *
  341. * @param file File which public share will be constrained with an expiration date.
  342. * @param expirationTimeInMillis Expiration date to set. A negative value clears the current expiration
  343. * date, leaving the link unrestricted. Zero makes no change.
  344. */
  345. public void setExpirationDateToShareViaLink(OCFile file, long expirationTimeInMillis) {
  346. Intent updateShareIntent = new Intent(mFileActivity, OperationsService.class);
  347. updateShareIntent.setAction(OperationsService.ACTION_UPDATE_SHARE);
  348. updateShareIntent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  349. updateShareIntent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  350. updateShareIntent.putExtra(
  351. OperationsService.EXTRA_SHARE_EXPIRATION_DATE_IN_MILLIS,
  352. expirationTimeInMillis
  353. );
  354. queueShareIntent(updateShareIntent);
  355. }
  356. /**
  357. * @return 'True' if the server supports the Search Users API
  358. */
  359. public boolean isSearchUsersSupportedSupported() {
  360. if (mFileActivity.getAccount() != null) {
  361. OwnCloudVersion serverVersion = AccountUtils.getServerVersion(mFileActivity.getAccount());
  362. return (serverVersion != null && serverVersion.isSearchUsersSupported());
  363. }
  364. return false;
  365. }
  366. public void sendDownloadedFile(OCFile file) {
  367. if (file != null) {
  368. String storagePath = file.getStoragePath();
  369. String encodedStoragePath = WebdavUtils.encodePath(storagePath);
  370. Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
  371. // set MimeType
  372. sendIntent.setType(file.getMimetype());
  373. sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + encodedStoragePath));
  374. sendIntent.putExtra(Intent.ACTION_SEND, true); // Send Action
  375. // Show dialog, without the own app
  376. String[] packagesToExclude = new String[]{mFileActivity.getPackageName()};
  377. DialogFragment chooserDialog = ShareLinkToDialog.newInstance(sendIntent,
  378. packagesToExclude, file);
  379. chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
  380. } else {
  381. Log_OC.wtf(TAG, "Trying to send a NULL OCFile");
  382. }
  383. }
  384. /**
  385. * Request the synchronization of a file or folder with the OC server, including its contents.
  386. *
  387. * @param file The file or folder to synchronize
  388. */
  389. public void syncFile(OCFile file) {
  390. if (!file.isFolder()){
  391. Intent intent = new Intent(mFileActivity, OperationsService.class);
  392. intent.setAction(OperationsService.ACTION_SYNC_FILE);
  393. intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  394. intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  395. intent.putExtra(OperationsService.EXTRA_SYNC_FILE_CONTENTS, true);
  396. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(intent);
  397. mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
  398. getString(R.string.wait_a_moment));
  399. } else {
  400. Intent intent = new Intent(mFileActivity, OperationsService.class);
  401. intent.setAction(OperationsService.ACTION_SYNC_FOLDER);
  402. intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  403. intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  404. mFileActivity.startService(intent);
  405. }
  406. }
  407. public void toggleFavorite(OCFile file, boolean isFavorite) {
  408. file.setFavorite(isFavorite);
  409. mFileActivity.getStorageManager().saveFile(file);
  410. /// register the OCFile instance in the observer service to monitor local updates
  411. Intent observedFileIntent = FileObserverService.makeObservedFileIntent(
  412. mFileActivity,
  413. file,
  414. mFileActivity.getAccount(),
  415. isFavorite);
  416. mFileActivity.startService(observedFileIntent);
  417. /// immediate content synchronization
  418. if (file.isFavorite()) {
  419. syncFile(file);
  420. }
  421. }
  422. public void renameFile(OCFile file, String newFilename) {
  423. // RenameFile
  424. Intent service = new Intent(mFileActivity, OperationsService.class);
  425. service.setAction(OperationsService.ACTION_RENAME);
  426. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  427. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  428. service.putExtra(OperationsService.EXTRA_NEWNAME, newFilename);
  429. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  430. mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
  431. getString(R.string.wait_a_moment));
  432. }
  433. public void removeFile(OCFile file, boolean onlyLocalCopy) {
  434. // RemoveFile
  435. Intent service = new Intent(mFileActivity, OperationsService.class);
  436. service.setAction(OperationsService.ACTION_REMOVE);
  437. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  438. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  439. service.putExtra(OperationsService.EXTRA_REMOVE_ONLY_LOCAL, onlyLocalCopy);
  440. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  441. mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
  442. getString(R.string.wait_a_moment));
  443. }
  444. public void createFolder(String remotePath, boolean createFullPath) {
  445. // Create Folder
  446. Intent service = new Intent(mFileActivity, OperationsService.class);
  447. service.setAction(OperationsService.ACTION_CREATE_FOLDER);
  448. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  449. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, remotePath);
  450. service.putExtra(OperationsService.EXTRA_CREATE_FULL_PATH, createFullPath);
  451. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  452. mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
  453. getString(R.string.wait_a_moment));
  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. if (downloaderBinder != null && downloaderBinder.isDownloading(account, file)) {
  471. downloaderBinder.cancel(account, file);
  472. }
  473. FileUploaderBinder uploaderBinder = mFileActivity.getFileUploaderBinder();
  474. if (uploaderBinder != null && uploaderBinder.isUploading(account, file)) {
  475. uploaderBinder.cancel(account, file);
  476. }
  477. }
  478. /**
  479. * Start move file operation
  480. *
  481. * @param newfile File where it is going to be moved
  482. * @param currentFile File with the previous info
  483. */
  484. public void moveFile(OCFile newfile, OCFile currentFile) {
  485. // Move files
  486. Intent service = new Intent(mFileActivity, OperationsService.class);
  487. service.setAction(OperationsService.ACTION_MOVE_FILE);
  488. service.putExtra(OperationsService.EXTRA_NEW_PARENT_PATH, newfile.getRemotePath());
  489. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, currentFile.getRemotePath());
  490. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  491. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  492. mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
  493. getString(R.string.wait_a_moment));
  494. }
  495. /**
  496. * Start copy file operation
  497. *
  498. * @param newfile File where it is going to be moved
  499. * @param currentFile File with the previous info
  500. */
  501. public void copyFile(OCFile newfile, OCFile currentFile) {
  502. // Copy files
  503. Intent service = new Intent(mFileActivity, OperationsService.class);
  504. service.setAction(OperationsService.ACTION_COPY_FILE);
  505. service.putExtra(OperationsService.EXTRA_NEW_PARENT_PATH, newfile.getRemotePath());
  506. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, currentFile.getRemotePath());
  507. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  508. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  509. mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
  510. getString(R.string.wait_a_moment));
  511. }
  512. public long getOpIdWaitingFor() {
  513. return mWaitingForOpId;
  514. }
  515. public void setOpIdWaitingFor(long waitingForOpId) {
  516. mWaitingForOpId = waitingForOpId;
  517. }
  518. /**
  519. * @return 'True' if the server doesn't need to check forbidden characters
  520. */
  521. public boolean isVersionWithForbiddenCharacters() {
  522. if (mFileActivity.getAccount() != null) {
  523. OwnCloudVersion serverVersion =
  524. AccountUtils.getServerVersion(mFileActivity.getAccount());
  525. return (serverVersion != null && serverVersion.isVersionWithForbiddenCharacters());
  526. }
  527. return false;
  528. }
  529. }