FileOperationsHelper.java 42 KB

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