FileOperationsHelper.java 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author masensio
  5. * @author David A. Velasco
  6. * @author Juan Carlos González Cabrero
  7. * Copyright (C) 2015 ownCloud Inc.
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. package com.owncloud.android.ui.helpers;
  23. import android.accounts.Account;
  24. import android.content.ActivityNotFoundException;
  25. import android.content.Context;
  26. import android.content.Intent;
  27. import android.content.pm.PackageManager;
  28. import android.content.pm.ResolveInfo;
  29. import android.net.Uri;
  30. import android.support.annotation.Nullable;
  31. import android.support.v4.app.DialogFragment;
  32. import android.webkit.MimeTypeMap;
  33. import android.widget.Toast;
  34. import com.owncloud.android.R;
  35. import com.owncloud.android.authentication.AccountUtils;
  36. import com.owncloud.android.datamodel.OCFile;
  37. import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
  38. import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
  39. import com.owncloud.android.lib.common.utils.Log_OC;
  40. import com.owncloud.android.lib.resources.shares.OCShare;
  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.events.FavoriteEvent;
  49. import org.greenrobot.eventbus.EventBus;
  50. import java.io.BufferedReader;
  51. import java.io.FileInputStream;
  52. import java.io.IOException;
  53. import java.io.InputStreamReader;
  54. import java.util.ArrayList;
  55. import java.util.Collection;
  56. import java.util.List;
  57. import java.util.regex.Matcher;
  58. import java.util.regex.Pattern;
  59. /**
  60. *
  61. */
  62. public class FileOperationsHelper {
  63. private static final String TAG = FileOperationsHelper.class.getSimpleName();
  64. private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
  65. protected FileActivity mFileActivity = null;
  66. /// Identifier of operation in progress which result shouldn't be lost
  67. private long mWaitingForOpId = Long.MAX_VALUE;
  68. private static final Pattern mPatternUrl = Pattern.compile("^URL=(.+)$");
  69. private static final Pattern mPatternString = Pattern.compile("<string>(.+)</string>");
  70. public FileOperationsHelper(FileActivity fileActivity) {
  71. mFileActivity = fileActivity;
  72. }
  73. @Nullable
  74. private String getUrlFromFile(String storagePath, Pattern pattern) {
  75. String url = null;
  76. InputStreamReader fr = null;
  77. BufferedReader br = null;
  78. try {
  79. fr = new InputStreamReader(new FileInputStream(storagePath), "UTF8");
  80. br = new BufferedReader(fr);
  81. String line;
  82. while ((line = br.readLine()) != null) {
  83. Matcher m = pattern.matcher(line);
  84. if (m.find()) {
  85. url = m.group(1);
  86. break;
  87. }
  88. }
  89. } catch (IOException e) {
  90. Log_OC.d(TAG, e.getMessage());
  91. } finally {
  92. if (br != null) {
  93. try {
  94. br.close();
  95. } catch (IOException e) {
  96. Log_OC.d(TAG, "Error closing buffered reader for URL file", e);
  97. }
  98. }
  99. if (fr != null) {
  100. try {
  101. fr.close();
  102. } catch (IOException e) {
  103. Log_OC.d(TAG, "Error closing file reader for URL file", e);
  104. }
  105. }
  106. }
  107. return url;
  108. }
  109. @Nullable
  110. private Intent createIntentFromFile(String storagePath) {
  111. String url = null;
  112. int lastIndexOfDot = storagePath.lastIndexOf('.');
  113. if (lastIndexOfDot >= 0) {
  114. String fileExt = storagePath.substring(lastIndexOfDot + 1);
  115. if (fileExt.equalsIgnoreCase("url") ||fileExt.equalsIgnoreCase("desktop")) {
  116. // Windows internet shortcut file .url
  117. // Ubuntu internet shortcut file .desktop
  118. url = getUrlFromFile(storagePath, mPatternUrl);
  119. } else if (fileExt.equalsIgnoreCase("webloc")) {
  120. // mac internet shortcut file .webloc
  121. url = getUrlFromFile(storagePath, mPatternString);
  122. }
  123. }
  124. if (url == null) {
  125. return null;
  126. }
  127. return new Intent(Intent.ACTION_VIEW, Uri.parse(url));
  128. }
  129. public void openFile(OCFile file) {
  130. if (file != null) {
  131. String storagePath = file.getStoragePath();
  132. Intent openFileWithIntent = null;
  133. int lastIndexOfDot = storagePath.lastIndexOf('.');
  134. if (lastIndexOfDot >= 0) {
  135. String fileExt = storagePath.substring(lastIndexOfDot + 1);
  136. String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExt);
  137. if (guessedMimeType != null) {
  138. openFileWithIntent = new Intent(Intent.ACTION_VIEW);
  139. openFileWithIntent.setDataAndType(
  140. file.getExposedFileUri(mFileActivity),
  141. guessedMimeType
  142. );
  143. }
  144. }
  145. if(openFileWithIntent == null) {
  146. openFileWithIntent = createIntentFromFile(storagePath);
  147. }
  148. if (openFileWithIntent == null) {
  149. openFileWithIntent = new Intent(Intent.ACTION_VIEW);
  150. openFileWithIntent.setDataAndType(
  151. file.getExposedFileUri(mFileActivity),
  152. file.getMimetype()
  153. );
  154. }
  155. openFileWithIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  156. List<ResolveInfo> launchables = mFileActivity.getPackageManager().
  157. queryIntentActivities(openFileWithIntent, PackageManager.GET_INTENT_FILTERS);
  158. if(launchables != null && launchables.size() > 0) {
  159. try {
  160. mFileActivity.startActivity(
  161. Intent.createChooser(
  162. openFileWithIntent, mFileActivity.getString(R.string.actionbar_open_with)
  163. )
  164. );
  165. } catch (ActivityNotFoundException anfe) {
  166. showNoAppForFileTypeToast(mFileActivity.getApplicationContext());
  167. }
  168. } else {
  169. showNoAppForFileTypeToast(mFileActivity.getApplicationContext());
  170. }
  171. } else {
  172. Log_OC.e(TAG, "Trying to open a NULL OCFile");
  173. }
  174. }
  175. /**
  176. * Displays a toast stating that no application could be found to open the file.
  177. *
  178. * @param context the context to be able to show a toast.
  179. */
  180. private void showNoAppForFileTypeToast(Context context) {
  181. Toast.makeText(context,
  182. R.string.file_list_no_app_for_file_type, Toast.LENGTH_SHORT)
  183. .show();
  184. }
  185. /**
  186. * Helper method to share a file via a public link. Starts a request to do it in {@link OperationsService}
  187. *
  188. * @param file The file to share.
  189. * @param password Optional password to protect the public share.
  190. */
  191. public void shareFileViaLink(OCFile file, String password) {
  192. if (isSharedSupported()) {
  193. if (file != null) {
  194. mFileActivity.showLoadingDialog(
  195. mFileActivity.getApplicationContext().
  196. getString(R.string.wait_a_moment)
  197. );
  198. Intent service = new Intent(mFileActivity, OperationsService.class);
  199. service.setAction(OperationsService.ACTION_CREATE_SHARE_VIA_LINK);
  200. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  201. if (password != null && password.length() > 0) {
  202. service.putExtra(OperationsService.EXTRA_SHARE_PASSWORD, password);
  203. }
  204. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  205. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  206. } else {
  207. Log_OC.e(TAG, "Trying to share a NULL OCFile");
  208. // TODO user-level error?
  209. }
  210. } else {
  211. // Show a Message
  212. Toast t = Toast.makeText(
  213. mFileActivity, mFileActivity.getString(R.string.share_link_no_support_share_api),
  214. Toast.LENGTH_LONG
  215. );
  216. t.show();
  217. }
  218. }
  219. public void getFileWithLink(OCFile file){
  220. if (isSharedSupported()) {
  221. if (file != null) {
  222. mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
  223. getString(R.string.wait_a_moment));
  224. Intent service = new Intent(mFileActivity, OperationsService.class);
  225. service.setAction(OperationsService.ACTION_CREATE_SHARE_VIA_LINK);
  226. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  227. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  228. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  229. } else {
  230. Log_OC.e(TAG, "Trying to share a NULL OCFile");
  231. }
  232. } else {
  233. // Show a Message
  234. Toast t = Toast.makeText(
  235. mFileActivity, mFileActivity.getString(R.string.share_link_no_support_share_api),
  236. Toast.LENGTH_LONG
  237. );
  238. t.show();
  239. }
  240. }
  241. /**
  242. * Helper method to share a file with a known sharee. Starts a request to do it in {@link OperationsService}
  243. *
  244. * @param file The file to share.
  245. * @param shareeName Name (user name or group name) of the target sharee.
  246. * @param shareType The share type determines the sharee type.
  247. * @param permissions Permissions to grant to sharee on the shared file.
  248. */
  249. public void shareFileWithSharee(OCFile file, String shareeName, ShareType shareType, int permissions) {
  250. if (file != null) {
  251. // TODO check capability?
  252. mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
  253. getString(R.string.wait_a_moment));
  254. Intent service = new Intent(mFileActivity, OperationsService.class);
  255. service.setAction(OperationsService.ACTION_CREATE_SHARE_WITH_SHAREE);
  256. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  257. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  258. service.putExtra(OperationsService.EXTRA_SHARE_WITH, shareeName);
  259. service.putExtra(OperationsService.EXTRA_SHARE_TYPE, shareType);
  260. service.putExtra(OperationsService.EXTRA_SHARE_PERMISSIONS, permissions);
  261. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  262. } else {
  263. Log_OC.e(TAG, "Trying to share a NULL OCFile");
  264. }
  265. }
  266. /**
  267. * @return 'True' if the server supports the Share API
  268. */
  269. public boolean isSharedSupported() {
  270. if (mFileActivity.getAccount() != null) {
  271. OwnCloudVersion serverVersion = AccountUtils.getServerVersion(mFileActivity.getAccount());
  272. return (serverVersion != null && serverVersion.isSharedSupported());
  273. }
  274. return false;
  275. }
  276. /**
  277. * Helper method to unshare a file publicly shared via link.
  278. * Starts a request to do it in {@link OperationsService}
  279. *
  280. * @param file The file to unshare.
  281. */
  282. public void unshareFileViaLink(OCFile file) {
  283. // Unshare the file: Create the intent
  284. Intent unshareService = new Intent(mFileActivity, OperationsService.class);
  285. unshareService.setAction(OperationsService.ACTION_UNSHARE);
  286. unshareService.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  287. unshareService.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  288. unshareService.putExtra(OperationsService.EXTRA_SHARE_TYPE, ShareType.PUBLIC_LINK);
  289. unshareService.putExtra(OperationsService.EXTRA_SHARE_WITH, "");
  290. queueShareIntent(unshareService);
  291. }
  292. public void unshareFileWithUserOrGroup(OCFile file, ShareType shareType, String userOrGroup){
  293. // Unshare the file: Create the intent
  294. Intent unshareService = new Intent(mFileActivity, OperationsService.class);
  295. unshareService.setAction(OperationsService.ACTION_UNSHARE);
  296. unshareService.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  297. unshareService.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  298. unshareService.putExtra(OperationsService.EXTRA_SHARE_TYPE, shareType);
  299. unshareService.putExtra(OperationsService.EXTRA_SHARE_WITH, userOrGroup);
  300. queueShareIntent(unshareService);
  301. }
  302. private void queueShareIntent(Intent shareIntent){
  303. if (isSharedSupported()) {
  304. // Unshare the file
  305. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().
  306. queueNewOperation(shareIntent);
  307. mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
  308. getString(R.string.wait_a_moment));
  309. } else {
  310. // Show a Message
  311. Toast t = Toast.makeText(mFileActivity,
  312. mFileActivity.getString(R.string.share_link_no_support_share_api),
  313. Toast.LENGTH_LONG);
  314. t.show();
  315. }
  316. }
  317. /**
  318. * Show an instance of {@link ShareType} for sharing or unsharing the {@OCFile} received as parameter.
  319. *
  320. * @param file File to share or unshare.
  321. */
  322. public void showShareFile(OCFile file){
  323. Intent intent = new Intent(mFileActivity, ShareActivity.class);
  324. intent.putExtra(FileActivity.EXTRA_FILE, file);
  325. intent.putExtra(FileActivity.EXTRA_ACCOUNT, mFileActivity.getAccount());
  326. mFileActivity.startActivity(intent);
  327. }
  328. /**
  329. * Updates a public share on a file to set its password.
  330. * Starts a request to do it in {@link OperationsService}
  331. *
  332. * @param file File which public share will be protected with a password.
  333. * @param password Password to set for the public link; null or empty string to clear
  334. * the current password
  335. */
  336. public void setPasswordToShareViaLink(OCFile file, String password) {
  337. // Set password updating share
  338. Intent updateShareIntent = new Intent(mFileActivity, OperationsService.class);
  339. updateShareIntent.setAction(OperationsService.ACTION_UPDATE_SHARE);
  340. updateShareIntent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  341. updateShareIntent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  342. updateShareIntent.putExtra(
  343. OperationsService.EXTRA_SHARE_PASSWORD,
  344. (password == null) ? "" : password
  345. );
  346. queueShareIntent(updateShareIntent);
  347. }
  348. /**
  349. * Updates a public share on a file to set its expiration date.
  350. * Starts a request to do it in {@link OperationsService}
  351. *
  352. * @param file File which public share will be constrained with an expiration date.
  353. * @param expirationTimeInMillis Expiration date to set. A negative value clears the current expiration
  354. * date, leaving the link unrestricted. Zero makes no change.
  355. */
  356. public void setExpirationDateToShareViaLink(OCFile file, long expirationTimeInMillis) {
  357. Intent updateShareIntent = new Intent(mFileActivity, OperationsService.class);
  358. updateShareIntent.setAction(OperationsService.ACTION_UPDATE_SHARE);
  359. updateShareIntent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  360. updateShareIntent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  361. updateShareIntent.putExtra(
  362. OperationsService.EXTRA_SHARE_EXPIRATION_DATE_IN_MILLIS,
  363. expirationTimeInMillis
  364. );
  365. queueShareIntent(updateShareIntent);
  366. }
  367. /**
  368. * Updates a share on a file to set its access permissions.
  369. * Starts a request to do it in {@link OperationsService}
  370. *
  371. * @param share {@link OCShare} instance which permissions will be updated.
  372. * @param permissions New permissions to set. A value <= 0 makes no update.
  373. */
  374. public void setPermissionsToShare(OCShare share, int permissions) {
  375. Intent updateShareIntent = new Intent(mFileActivity, OperationsService.class);
  376. updateShareIntent.setAction(OperationsService.ACTION_UPDATE_SHARE);
  377. updateShareIntent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  378. updateShareIntent.putExtra(OperationsService.EXTRA_SHARE_ID, share.getId());
  379. updateShareIntent.putExtra(
  380. OperationsService.EXTRA_SHARE_PERMISSIONS,
  381. permissions
  382. );
  383. queueShareIntent(updateShareIntent);
  384. }
  385. /**
  386. * Updates a public share on a folder to set its editing permission.
  387. * Starts a request to do it in {@link OperationsService}
  388. *
  389. * @param folder Folder which editing permission of his public share will be modified.
  390. * @param uploadPermission New state of the permission for editing the folder shared via link.
  391. */
  392. public void setUploadPermissionsToShare(OCFile folder, boolean uploadPermission) {
  393. Intent updateShareIntent = new Intent(mFileActivity, OperationsService.class);
  394. updateShareIntent.setAction(OperationsService.ACTION_UPDATE_SHARE);
  395. updateShareIntent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  396. updateShareIntent.putExtra(OperationsService.EXTRA_REMOTE_PATH, folder.getRemotePath());
  397. updateShareIntent.putExtra(
  398. OperationsService.EXTRA_SHARE_PUBLIC_UPLOAD,
  399. uploadPermission
  400. );
  401. queueShareIntent(updateShareIntent);
  402. }
  403. /**
  404. * Updates a public share on a folder to set its hide file listing permission.
  405. * Starts a request to do it in {@link OperationsService}
  406. *
  407. * @param share {@link OCShare} instance which permissions will be updated.
  408. * @param hideFileListing New state of the permission for editing the folder shared via link.
  409. */
  410. public void setHideFileListingPermissionsToShare(OCShare share, boolean hideFileListing) {
  411. Intent updateShareIntent = new Intent(mFileActivity, OperationsService.class);
  412. updateShareIntent.setAction(OperationsService.ACTION_UPDATE_SHARE);
  413. updateShareIntent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  414. updateShareIntent.putExtra(OperationsService.EXTRA_SHARE_ID, share.getId());
  415. if (hideFileListing) {
  416. updateShareIntent.putExtra(OperationsService.EXTRA_SHARE_PERMISSIONS, OCShare.CREATE_PERMISSION_FLAG);
  417. } else {
  418. OwnCloudVersion serverVersion = AccountUtils.getServerVersion(mFileActivity.getAccount());
  419. if (serverVersion != null && serverVersion.isNotReshareableFederatedSupported()) {
  420. updateShareIntent.putExtra(OperationsService.EXTRA_SHARE_PERMISSIONS,
  421. OCShare.FEDERATED_PERMISSIONS_FOR_FOLDER_AFTER_OC9);
  422. } else {
  423. updateShareIntent.putExtra(OperationsService.EXTRA_SHARE_PERMISSIONS,
  424. OCShare.FEDERATED_PERMISSIONS_FOR_FOLDER_UP_TO_OC9);
  425. }
  426. }
  427. queueShareIntent(updateShareIntent);
  428. }
  429. /**
  430. * @return 'True' if the server supports the Search Users API
  431. */
  432. public boolean isSearchUserSupportedSupported() {
  433. if (mFileActivity.getAccount() != null) {
  434. OwnCloudVersion serverVersion = AccountUtils.getServerVersion(mFileActivity.getAccount());
  435. return (serverVersion != null && serverVersion.isSearchUsersSupported());
  436. }
  437. return false;
  438. }
  439. public void sendDownloadedFile(OCFile file) {
  440. if (file != null) {
  441. Intent sendIntent = new Intent(Intent.ACTION_SEND);
  442. // set MimeType
  443. sendIntent.setType(file.getMimetype());
  444. sendIntent.putExtra(
  445. Intent.EXTRA_STREAM,
  446. file.getExposedFileUri(mFileActivity)
  447. );
  448. sendIntent.putExtra(Intent.ACTION_SEND, true); // Send Action
  449. // Show dialog, without the own app
  450. String[] packagesToExclude = new String[]{mFileActivity.getPackageName()};
  451. DialogFragment chooserDialog = ShareLinkToDialog.newInstance(sendIntent, packagesToExclude);
  452. chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
  453. } else {
  454. Log_OC.e(TAG, "Trying to send a NULL OCFile");
  455. }
  456. }
  457. public void syncFiles(Collection<OCFile> files) {
  458. for (OCFile file: files) {
  459. syncFile(file);
  460. }
  461. }
  462. /**
  463. * Request the synchronization of a file or folder with the OC server, including its contents.
  464. *
  465. * @param file The file or folder to synchronize
  466. */
  467. public void syncFile(OCFile file) {
  468. if (!file.isFolder()){
  469. Intent intent = new Intent(mFileActivity, OperationsService.class);
  470. intent.setAction(OperationsService.ACTION_SYNC_FILE);
  471. intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  472. intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  473. intent.putExtra(OperationsService.EXTRA_SYNC_FILE_CONTENTS, true);
  474. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(intent);
  475. mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
  476. getString(R.string.wait_a_moment));
  477. } else {
  478. Intent intent = new Intent(mFileActivity, OperationsService.class);
  479. intent.setAction(OperationsService.ACTION_SYNC_FOLDER);
  480. intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  481. intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  482. mFileActivity.startService(intent);
  483. }
  484. }
  485. public void toggleFavoriteFiles(Collection<OCFile> files, boolean shouldBeFavorite) {
  486. List<OCFile> alreadyRightStateList = new ArrayList<>();
  487. for(OCFile file : files) {
  488. if(file.getIsFavorite() == shouldBeFavorite) {
  489. alreadyRightStateList.add(file);
  490. }
  491. }
  492. files.removeAll(alreadyRightStateList);
  493. for (OCFile file: files) {
  494. toggleFavoriteFile(file, shouldBeFavorite);
  495. }
  496. }
  497. public void toggleFavoriteFile(OCFile file, boolean shouldBeFavorite) {
  498. if(file.getIsFavorite() != shouldBeFavorite) {
  499. EventBus.getDefault().post(new FavoriteEvent(file.getRemotePath(), shouldBeFavorite, file.getRemoteId()));
  500. }
  501. }
  502. public void toogleOfflineFiles(Collection<OCFile> files, boolean isAvailableOffline){
  503. List<OCFile> alreadyRightStateList = new ArrayList<>();
  504. for(OCFile file : files) {
  505. if(file.isAvailableOffline() == isAvailableOffline) {
  506. alreadyRightStateList.add(file);
  507. }
  508. }
  509. files.removeAll(alreadyRightStateList);
  510. for (OCFile file: files) {
  511. toggleOfflineFile(file, isAvailableOffline);
  512. }
  513. }
  514. public void toggleOfflineFile(OCFile file, boolean isAvailableOffline) {
  515. if (file.isAvailableOffline() != isAvailableOffline) {
  516. file.setAvailableOffline(isAvailableOffline);
  517. mFileActivity.getStorageManager().saveFile(file);
  518. /// register the OCFile instance in the observer service to monitor local updates
  519. Intent observedFileIntent = FileObserverService.makeObservedFileIntent(
  520. mFileActivity,
  521. file,
  522. mFileActivity.getAccount(),
  523. isAvailableOffline);
  524. mFileActivity.startService(observedFileIntent);
  525. /// immediate content synchronization
  526. if (file.isAvailableOffline()) {
  527. syncFile(file);
  528. }
  529. }
  530. }
  531. public void renameFile(OCFile file, String newFilename) {
  532. // RenameFile
  533. Intent service = new Intent(mFileActivity, OperationsService.class);
  534. service.setAction(OperationsService.ACTION_RENAME);
  535. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  536. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  537. service.putExtra(OperationsService.EXTRA_NEWNAME, newFilename);
  538. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  539. mFileActivity.showLoadingDialog(mFileActivity.getString(R.string.wait_a_moment));
  540. }
  541. /**
  542. * Start operations to delete one or several files
  543. *
  544. * @param files Files to delete
  545. * @param onlyLocalCopy When 'true' only local copy of the files is removed; otherwise files are also deleted
  546. * in the server.
  547. */
  548. public void removeFiles(Collection<OCFile> files, boolean onlyLocalCopy) {
  549. for (OCFile file : files) {
  550. // RemoveFile
  551. Intent service = new Intent(mFileActivity, OperationsService.class);
  552. service.setAction(OperationsService.ACTION_REMOVE);
  553. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  554. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  555. service.putExtra(OperationsService.EXTRA_REMOVE_ONLY_LOCAL, onlyLocalCopy);
  556. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  557. }
  558. mFileActivity.showLoadingDialog(mFileActivity.getString(R.string.wait_a_moment));
  559. }
  560. public void createFolder(String remotePath, boolean createFullPath) {
  561. // Create Folder
  562. Intent service = new Intent(mFileActivity, OperationsService.class);
  563. service.setAction(OperationsService.ACTION_CREATE_FOLDER);
  564. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  565. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, remotePath);
  566. service.putExtra(OperationsService.EXTRA_CREATE_FULL_PATH, createFullPath);
  567. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  568. mFileActivity.showLoadingDialog(mFileActivity.getString(R.string.wait_a_moment));
  569. }
  570. /**
  571. * Cancel the transference in downloads (files/folders) and file uploads
  572. * @param file OCFile
  573. */
  574. public void cancelTransference(OCFile file) {
  575. Account account = mFileActivity.getAccount();
  576. if (file.isFolder()) {
  577. OperationsService.OperationsServiceBinder opsBinder =
  578. mFileActivity.getOperationsServiceBinder();
  579. if (opsBinder != null) {
  580. opsBinder.cancel(account, file);
  581. }
  582. }
  583. // for both files and folders
  584. FileDownloaderBinder downloaderBinder = mFileActivity.getFileDownloaderBinder();
  585. if (downloaderBinder != null && downloaderBinder.isDownloading(account, file)) {
  586. downloaderBinder.cancel(account, file);
  587. }
  588. FileUploaderBinder uploaderBinder = mFileActivity.getFileUploaderBinder();
  589. if (uploaderBinder != null && uploaderBinder.isUploading(account, file)) {
  590. uploaderBinder.cancel(account, file);
  591. }
  592. }
  593. /**
  594. * Start operations to move one or several files
  595. *
  596. * @param files Files to move
  597. * @param targetFolder Folder where the files while be moved into
  598. */
  599. public void moveFiles(Collection<OCFile> files, OCFile targetFolder) {
  600. for (OCFile file : files) {
  601. Intent service = new Intent(mFileActivity, OperationsService.class);
  602. service.setAction(OperationsService.ACTION_MOVE_FILE);
  603. service.putExtra(OperationsService.EXTRA_NEW_PARENT_PATH, targetFolder.getRemotePath());
  604. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  605. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  606. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  607. }
  608. mFileActivity.showLoadingDialog(mFileActivity.getString(R.string.wait_a_moment));
  609. }
  610. /**
  611. * Start operations to copy one or several files
  612. *
  613. * @param files Files to copy
  614. * @param targetFolder Folder where the files while be copied into
  615. */
  616. public void copyFiles(Collection<OCFile> files, OCFile targetFolder) {
  617. for (OCFile file : files) {
  618. Intent service = new Intent(mFileActivity, OperationsService.class);
  619. service.setAction(OperationsService.ACTION_COPY_FILE);
  620. service.putExtra(OperationsService.EXTRA_NEW_PARENT_PATH, targetFolder.getRemotePath());
  621. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  622. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  623. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  624. }
  625. mFileActivity.showLoadingDialog(mFileActivity.getString(R.string.wait_a_moment));
  626. }
  627. public long getOpIdWaitingFor() {
  628. return mWaitingForOpId;
  629. }
  630. public void setOpIdWaitingFor(long waitingForOpId) {
  631. mWaitingForOpId = waitingForOpId;
  632. }
  633. /**
  634. * @return 'True' if the server doesn't need to check forbidden characters
  635. */
  636. public boolean isVersionWithForbiddenCharacters() {
  637. if (mFileActivity.getAccount() != null) {
  638. OwnCloudVersion serverVersion =
  639. AccountUtils.getServerVersion(mFileActivity.getAccount());
  640. return (serverVersion != null && serverVersion.isVersionWithForbiddenCharacters());
  641. }
  642. return false;
  643. }
  644. /**
  645. * Starts a check of the currenlty stored credentials for the given account.
  646. *
  647. * @param account OC account which credentials will be checked.
  648. */
  649. public void checkCurrentCredentials(Account account) {
  650. Intent service = new Intent(mFileActivity, OperationsService.class);
  651. service.setAction(OperationsService.ACTION_CHECK_CURRENT_CREDENTIALS);
  652. service.putExtra(OperationsService.EXTRA_ACCOUNT, account);
  653. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  654. mFileActivity.showLoadingDialog(
  655. mFileActivity.getString(R.string.wait_checking_credentials)
  656. );
  657. }
  658. }