FileOperationsHelper.java 31 KB

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