FileOperationsHelper.java 37 KB

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