FileOperationsHelper.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012-2015 ownCloud Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. package com.owncloud.android.files;
  18. import org.apache.http.protocol.HTTP;
  19. import android.accounts.Account;
  20. import android.accounts.AccountManager;
  21. import android.content.Intent;
  22. import android.net.Uri;
  23. import android.support.v4.app.DialogFragment;
  24. import android.webkit.MimeTypeMap;
  25. import android.widget.Toast;
  26. import com.owncloud.android.R;
  27. import com.owncloud.android.datamodel.OCFile;
  28. import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
  29. import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
  30. import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
  31. import com.owncloud.android.lib.common.network.WebdavUtils;
  32. import com.owncloud.android.lib.common.utils.Log_OC;
  33. import com.owncloud.android.lib.resources.status.OwnCloudVersion;
  34. import com.owncloud.android.services.OperationsService;
  35. import com.owncloud.android.ui.activity.FileActivity;
  36. import com.owncloud.android.ui.dialog.ShareLinkToDialog;
  37. /**
  38. *
  39. * @author masensio
  40. * @author David A. Velasco
  41. */
  42. public class FileOperationsHelper {
  43. private static final String TAG = FileOperationsHelper.class.getName();
  44. private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
  45. protected FileActivity mFileActivity = null;
  46. /// Identifier of operation in progress which result shouldn't be lost
  47. private long mWaitingForOpId = Long.MAX_VALUE;
  48. public FileOperationsHelper(FileActivity fileActivity) {
  49. mFileActivity = fileActivity;
  50. }
  51. public void openFile(OCFile file) {
  52. if (file != null) {
  53. String storagePath = file.getStoragePath();
  54. String encodedStoragePath = WebdavUtils.encodePath(storagePath);
  55. Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
  56. intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), file.getMimetype());
  57. intentForSavedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  58. Intent intentForGuessedMimeType = null;
  59. if (storagePath.lastIndexOf('.') >= 0) {
  60. String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
  61. if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
  62. intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
  63. intentForGuessedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), guessedMimeType);
  64. intentForGuessedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  65. }
  66. }
  67. Intent chooserIntent = null;
  68. if (intentForGuessedMimeType != null) {
  69. chooserIntent = Intent.createChooser(intentForGuessedMimeType, mFileActivity.getString(R.string.actionbar_open_with));
  70. } else {
  71. chooserIntent = Intent.createChooser(intentForSavedMimeType, mFileActivity.getString(R.string.actionbar_open_with));
  72. }
  73. mFileActivity.startActivity(chooserIntent);
  74. } else {
  75. Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
  76. }
  77. }
  78. public void shareFileWithLink(OCFile file) {
  79. if (isSharedSupported()) {
  80. if (file != null) {
  81. String link = "https://fake.url";
  82. Intent intent = createShareWithLinkIntent(link);
  83. String[] packagesToExclude = new String[] { mFileActivity.getPackageName() };
  84. DialogFragment chooserDialog = ShareLinkToDialog.newInstance(intent, packagesToExclude, file);
  85. chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
  86. } else {
  87. Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
  88. }
  89. } else {
  90. // Show a Message
  91. Toast t = Toast.makeText(mFileActivity, mFileActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
  92. t.show();
  93. }
  94. }
  95. public void shareFileWithLinkToApp(OCFile file, Intent sendIntent) {
  96. if (file != null) {
  97. mFileActivity.showLoadingDialog();
  98. Intent service = new Intent(mFileActivity, OperationsService.class);
  99. service.setAction(OperationsService.ACTION_CREATE_SHARE);
  100. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  101. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  102. service.putExtra(OperationsService.EXTRA_SEND_INTENT, sendIntent);
  103. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  104. } else {
  105. Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
  106. }
  107. }
  108. private Intent createShareWithLinkIntent(String link) {
  109. Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
  110. intentToShareLink.putExtra(Intent.EXTRA_TEXT, link);
  111. intentToShareLink.setType(HTTP.PLAIN_TEXT_TYPE);
  112. return intentToShareLink;
  113. }
  114. /**
  115. * @return 'True' if the server supports the Share API
  116. */
  117. public boolean isSharedSupported() {
  118. if (mFileActivity.getAccount() != null) {
  119. AccountManager accountManager = AccountManager.get(mFileActivity);
  120. String version = accountManager.getUserData(mFileActivity.getAccount(), Constants.KEY_OC_VERSION);
  121. return (new OwnCloudVersion(version)).isSharedSupported();
  122. }
  123. return false;
  124. }
  125. public void unshareFileWithLink(OCFile file) {
  126. if (isSharedSupported()) {
  127. // Unshare the file
  128. Intent service = new Intent(mFileActivity, OperationsService.class);
  129. service.setAction(OperationsService.ACTION_UNSHARE);
  130. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  131. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  132. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  133. mFileActivity.showLoadingDialog();
  134. } else {
  135. // Show a Message
  136. Toast t = Toast.makeText(mFileActivity, mFileActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
  137. t.show();
  138. }
  139. }
  140. public void sendDownloadedFile(OCFile file) {
  141. if (file != null) {
  142. Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
  143. // set MimeType
  144. sendIntent.setType(file.getMimetype());
  145. sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file.getStoragePath()));
  146. sendIntent.putExtra(Intent.ACTION_SEND, true); // Send Action
  147. // Show dialog, without the own app
  148. String[] packagesToExclude = new String[] { mFileActivity.getPackageName() };
  149. DialogFragment chooserDialog = ShareLinkToDialog.newInstance(sendIntent, packagesToExclude, file);
  150. chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
  151. } else {
  152. Log_OC.wtf(TAG, "Trying to send a NULL OCFile");
  153. }
  154. }
  155. public void syncFile(OCFile file) {
  156. if (!file.isFolder()){
  157. Intent intent = new Intent(mFileActivity, OperationsService.class);
  158. intent.setAction(OperationsService.ACTION_SYNC_FILE);
  159. intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  160. intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  161. intent.putExtra(OperationsService.EXTRA_SYNC_FILE_CONTENTS, true);
  162. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(intent);
  163. mFileActivity.showLoadingDialog();
  164. } else {
  165. Intent intent = new Intent(mFileActivity, OperationsService.class);
  166. intent.setAction(OperationsService.ACTION_SYNC_FOLDER);
  167. intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  168. intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  169. mFileActivity.startService(intent);
  170. }
  171. }
  172. public void renameFile(OCFile file, String newFilename) {
  173. // RenameFile
  174. Intent service = new Intent(mFileActivity, OperationsService.class);
  175. service.setAction(OperationsService.ACTION_RENAME);
  176. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  177. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  178. service.putExtra(OperationsService.EXTRA_NEWNAME, newFilename);
  179. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  180. mFileActivity.showLoadingDialog();
  181. }
  182. public void removeFile(OCFile file, boolean onlyLocalCopy) {
  183. // RemoveFile
  184. Intent service = new Intent(mFileActivity, OperationsService.class);
  185. service.setAction(OperationsService.ACTION_REMOVE);
  186. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  187. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  188. service.putExtra(OperationsService.EXTRA_REMOVE_ONLY_LOCAL, onlyLocalCopy);
  189. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  190. mFileActivity.showLoadingDialog();
  191. }
  192. public void createFolder(String remotePath, boolean createFullPath) {
  193. // Create Folder
  194. Intent service = new Intent(mFileActivity, OperationsService.class);
  195. service.setAction(OperationsService.ACTION_CREATE_FOLDER);
  196. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  197. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, remotePath);
  198. service.putExtra(OperationsService.EXTRA_CREATE_FULL_PATH, createFullPath);
  199. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  200. mFileActivity.showLoadingDialog();
  201. }
  202. /**
  203. * Cancel the transference in downloads (files/folders) and file uploads
  204. * @param file OCFile
  205. */
  206. public void cancelTransference(OCFile file) {
  207. Account account = mFileActivity.getAccount();
  208. if (file.isFolder()) {
  209. OperationsService.OperationsServiceBinder opsBinder = mFileActivity.getOperationsServiceBinder();
  210. if (opsBinder != null) {
  211. opsBinder.cancel(account, file);
  212. }
  213. }
  214. // for both files and folders
  215. FileDownloaderBinder downloaderBinder = mFileActivity.getFileDownloaderBinder();
  216. FileUploaderBinder uploaderBinder = mFileActivity.getFileUploaderBinder();
  217. if (downloaderBinder != null && downloaderBinder.isDownloading(account, file)) {
  218. downloaderBinder.cancel(account, file);
  219. // TODO - review why is this here, and solve in a better way
  220. // Remove etag for parent, if file is a keep_in_sync
  221. if (file.keepInSync()) {
  222. OCFile parent = mFileActivity.getStorageManager().getFileById(file.getParentId());
  223. parent.setEtag("");
  224. mFileActivity.getStorageManager().saveFile(parent);
  225. }
  226. } else if (uploaderBinder != null && uploaderBinder.isUploading(account, file)) {
  227. uploaderBinder.cancel(account, file);
  228. }
  229. }
  230. /**
  231. * Start move file operation
  232. * @param newfile File where it is going to be moved
  233. * @param currentFile File with the previous info
  234. */
  235. public void moveFile(OCFile newfile, OCFile currentFile) {
  236. // Move files
  237. Intent service = new Intent(mFileActivity, OperationsService.class);
  238. service.setAction(OperationsService.ACTION_MOVE_FILE);
  239. service.putExtra(OperationsService.EXTRA_NEW_PARENT_PATH, newfile.getRemotePath());
  240. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, currentFile.getRemotePath());
  241. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  242. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().queueNewOperation(service);
  243. mFileActivity.showLoadingDialog();
  244. }
  245. public long getOpIdWaitingFor() {
  246. return mWaitingForOpId;
  247. }
  248. public void setOpIdWaitingFor(long waitingForOpId) {
  249. mWaitingForOpId = waitingForOpId;
  250. }
  251. }