FileOperationsHelper.java 37 KB

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