FileOperationsHelper.java 33 KB

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