FileOperationsHelper.java 33 KB

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