FileOperationsHelper.java 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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 java.io.BufferedReader;
  50. import java.io.FileInputStream;
  51. import java.io.FileReader;
  52. import java.io.IOException;
  53. import java.io.InputStreamReader;
  54. import java.nio.charset.StandardCharsets;
  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(mFileActivity.EXTRA_FILE, (Parcelable) file);
  325. intent.putExtra(mFileActivity.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. updateShareIntent.
  419. putExtra(OperationsService.EXTRA_SHARE_PERMISSIONS, OCShare.FEDERATED_PERMISSIONS_FOR_FOLDER);
  420. }
  421. queueShareIntent(updateShareIntent);
  422. }
  423. /**
  424. * @return 'True' if the server supports the Search Users API
  425. */
  426. public boolean isSearchUserSupportedSupported() {
  427. if (mFileActivity.getAccount() != null) {
  428. OwnCloudVersion serverVersion = AccountUtils.getServerVersion(mFileActivity.getAccount());
  429. return (serverVersion != null && serverVersion.isSearchUsersSupported());
  430. }
  431. return false;
  432. }
  433. public void sendDownloadedFile(OCFile file) {
  434. if (file != null) {
  435. String storagePath = file.getStoragePath();
  436. Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
  437. // set MimeType
  438. sendIntent.setType(file.getMimetype());
  439. sendIntent.putExtra(
  440. Intent.EXTRA_STREAM,
  441. file.getExposedFileUri(mFileActivity)
  442. );
  443. sendIntent.putExtra(Intent.ACTION_SEND, true); // Send Action
  444. // Show dialog, without the own app
  445. String[] packagesToExclude = new String[]{mFileActivity.getPackageName()};
  446. DialogFragment chooserDialog = ShareLinkToDialog.newInstance(sendIntent, packagesToExclude);
  447. chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
  448. } else {
  449. Log_OC.e(TAG, "Trying to send a NULL OCFile");
  450. }
  451. }
  452. public void syncFiles(Collection<OCFile> files) {
  453. for (OCFile file: files) {
  454. syncFile(file);
  455. }
  456. }
  457. /**
  458. * Request the synchronization of a file or folder with the OC server, including its contents.
  459. *
  460. * @param file The file or folder to synchronize
  461. */
  462. public void syncFile(OCFile file) {
  463. if (!file.isFolder()){
  464. Intent intent = new Intent(mFileActivity, OperationsService.class);
  465. intent.setAction(OperationsService.ACTION_SYNC_FILE);
  466. intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  467. intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  468. intent.putExtra(OperationsService.EXTRA_SYNC_FILE_CONTENTS, true);
  469. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(intent);
  470. mFileActivity.showLoadingDialog(mFileActivity.getApplicationContext().
  471. getString(R.string.wait_a_moment));
  472. } else {
  473. Intent intent = new Intent(mFileActivity, OperationsService.class);
  474. intent.setAction(OperationsService.ACTION_SYNC_FOLDER);
  475. intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  476. intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  477. mFileActivity.startService(intent);
  478. }
  479. }
  480. public void toggleFavorites(Collection<OCFile> files, boolean isFavorite){
  481. for (OCFile file: files) {
  482. toggleFavorite(file, isFavorite);
  483. }
  484. }
  485. public void toggleFavorite(OCFile file, boolean isFavorite) {
  486. file.setFavorite(isFavorite);
  487. mFileActivity.getStorageManager().saveFile(file);
  488. /// register the OCFile instance in the observer service to monitor local updates
  489. Intent observedFileIntent = FileObserverService.makeObservedFileIntent(
  490. mFileActivity,
  491. file,
  492. mFileActivity.getAccount(),
  493. isFavorite);
  494. mFileActivity.startService(observedFileIntent);
  495. /// immediate content synchronization
  496. if (file.isFavorite()) {
  497. syncFile(file);
  498. }
  499. }
  500. public void renameFile(OCFile file, String newFilename) {
  501. // RenameFile
  502. Intent service = new Intent(mFileActivity, OperationsService.class);
  503. service.setAction(OperationsService.ACTION_RENAME);
  504. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  505. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  506. service.putExtra(OperationsService.EXTRA_NEWNAME, newFilename);
  507. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  508. mFileActivity.showLoadingDialog(mFileActivity.getString(R.string.wait_a_moment));
  509. }
  510. /**
  511. * Start operations to delete one or several files
  512. *
  513. * @param files Files to delete
  514. * @param onlyLocalCopy When 'true' only local copy of the files is removed; otherwise files are also deleted
  515. * in the server.
  516. */
  517. public void removeFiles(Collection<OCFile> files, boolean onlyLocalCopy) {
  518. for (OCFile file : files) {
  519. // RemoveFile
  520. Intent service = new Intent(mFileActivity, OperationsService.class);
  521. service.setAction(OperationsService.ACTION_REMOVE);
  522. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  523. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  524. service.putExtra(OperationsService.EXTRA_REMOVE_ONLY_LOCAL, onlyLocalCopy);
  525. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  526. }
  527. mFileActivity.showLoadingDialog(mFileActivity.getString(R.string.wait_a_moment));
  528. }
  529. public void createFolder(String remotePath, boolean createFullPath) {
  530. // Create Folder
  531. Intent service = new Intent(mFileActivity, OperationsService.class);
  532. service.setAction(OperationsService.ACTION_CREATE_FOLDER);
  533. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  534. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, remotePath);
  535. service.putExtra(OperationsService.EXTRA_CREATE_FULL_PATH, createFullPath);
  536. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  537. mFileActivity.showLoadingDialog(mFileActivity.getString(R.string.wait_a_moment));
  538. }
  539. /**
  540. * Cancel the transference in downloads (files/folders) and file uploads
  541. * @param file OCFile
  542. */
  543. public void cancelTransference(OCFile file) {
  544. Account account = mFileActivity.getAccount();
  545. if (file.isFolder()) {
  546. OperationsService.OperationsServiceBinder opsBinder =
  547. mFileActivity.getOperationsServiceBinder();
  548. if (opsBinder != null) {
  549. opsBinder.cancel(account, file);
  550. }
  551. }
  552. // for both files and folders
  553. FileDownloaderBinder downloaderBinder = mFileActivity.getFileDownloaderBinder();
  554. if (downloaderBinder != null && downloaderBinder.isDownloading(account, file)) {
  555. downloaderBinder.cancel(account, file);
  556. }
  557. FileUploaderBinder uploaderBinder = mFileActivity.getFileUploaderBinder();
  558. if (uploaderBinder != null && uploaderBinder.isUploading(account, file)) {
  559. uploaderBinder.cancel(account, file);
  560. }
  561. }
  562. /**
  563. * Start operations to move one or several files
  564. *
  565. * @param files Files to move
  566. * @param targetFolder Folder where the files while be moved into
  567. */
  568. public void moveFiles(Collection<OCFile> files, OCFile targetFolder) {
  569. for (OCFile file : files) {
  570. Intent service = new Intent(mFileActivity, OperationsService.class);
  571. service.setAction(OperationsService.ACTION_MOVE_FILE);
  572. service.putExtra(OperationsService.EXTRA_NEW_PARENT_PATH, targetFolder.getRemotePath());
  573. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  574. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  575. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  576. }
  577. mFileActivity.showLoadingDialog(mFileActivity.getString(R.string.wait_a_moment));
  578. }
  579. /**
  580. * Start operations to copy one or several files
  581. *
  582. * @param files Files to copy
  583. * @param targetFolder Folder where the files while be copied into
  584. */
  585. public void copyFiles(Collection<OCFile> files, OCFile targetFolder) {
  586. for (OCFile file : files) {
  587. Intent service = new Intent(mFileActivity, OperationsService.class);
  588. service.setAction(OperationsService.ACTION_COPY_FILE);
  589. service.putExtra(OperationsService.EXTRA_NEW_PARENT_PATH, targetFolder.getRemotePath());
  590. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  591. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  592. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  593. }
  594. mFileActivity.showLoadingDialog(mFileActivity.getString(R.string.wait_a_moment));
  595. }
  596. public long getOpIdWaitingFor() {
  597. return mWaitingForOpId;
  598. }
  599. public void setOpIdWaitingFor(long waitingForOpId) {
  600. mWaitingForOpId = waitingForOpId;
  601. }
  602. /**
  603. * @return 'True' if the server doesn't need to check forbidden characters
  604. */
  605. public boolean isVersionWithForbiddenCharacters() {
  606. if (mFileActivity.getAccount() != null) {
  607. OwnCloudVersion serverVersion =
  608. AccountUtils.getServerVersion(mFileActivity.getAccount());
  609. return (serverVersion != null && serverVersion.isVersionWithForbiddenCharacters());
  610. }
  611. return false;
  612. }
  613. /**
  614. * Starts a check of the currenlty stored credentials for the given account.
  615. *
  616. * @param account OC account which credentials will be checked.
  617. */
  618. public void checkCurrentCredentials(Account account) {
  619. Intent service = new Intent(mFileActivity, OperationsService.class);
  620. service.setAction(OperationsService.ACTION_CHECK_CURRENT_CREDENTIALS);
  621. service.putExtra(OperationsService.EXTRA_ACCOUNT, account);
  622. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  623. mFileActivity.showLoadingDialog(
  624. mFileActivity.getString(R.string.wait_checking_credentials)
  625. );
  626. }
  627. }