FileOperationsHelper.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012-2014 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.resources.status.OwnCloudVersion;
  33. import com.owncloud.android.services.OperationsService;
  34. import com.owncloud.android.ui.activity.FileActivity;
  35. import com.owncloud.android.ui.dialog.ShareLinkToDialog;
  36. import com.owncloud.android.utils.Log_OC;
  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().newOperation(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().newOperation(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. // Sync file
  157. Intent service = new Intent(mFileActivity, OperationsService.class);
  158. service.setAction(OperationsService.ACTION_SYNC_FILE);
  159. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  160. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  161. service.putExtra(OperationsService.EXTRA_SYNC_FILE_CONTENTS, true);
  162. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().newOperation(service);
  163. mFileActivity.showLoadingDialog();
  164. }
  165. public void renameFile(OCFile file, String newFilename) {
  166. // RenameFile
  167. Intent service = new Intent(mFileActivity, OperationsService.class);
  168. service.setAction(OperationsService.ACTION_RENAME);
  169. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  170. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  171. service.putExtra(OperationsService.EXTRA_NEWNAME, newFilename);
  172. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().newOperation(service);
  173. mFileActivity.showLoadingDialog();
  174. }
  175. public void removeFile(OCFile file, boolean onlyLocalCopy) {
  176. // RemoveFile
  177. Intent service = new Intent(mFileActivity, OperationsService.class);
  178. service.setAction(OperationsService.ACTION_REMOVE);
  179. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  180. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  181. service.putExtra(OperationsService.EXTRA_REMOVE_ONLY_LOCAL, onlyLocalCopy);
  182. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().newOperation(service);
  183. mFileActivity.showLoadingDialog();
  184. }
  185. public void createFolder(String remotePath, boolean createFullPath) {
  186. // Create Folder
  187. Intent service = new Intent(mFileActivity, OperationsService.class);
  188. service.setAction(OperationsService.ACTION_CREATE_FOLDER);
  189. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  190. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, remotePath);
  191. service.putExtra(OperationsService.EXTRA_CREATE_FULL_PATH, createFullPath);
  192. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().newOperation(service);
  193. mFileActivity.showLoadingDialog();
  194. }
  195. public void cancelTransference(OCFile file) {
  196. Account account = mFileActivity.getAccount();
  197. FileDownloaderBinder downloaderBinder = mFileActivity.getFileDownloaderBinder();
  198. FileUploaderBinder uploaderBinder = mFileActivity.getFileUploaderBinder();
  199. if (downloaderBinder != null && downloaderBinder.isDownloading(account, file)) {
  200. // Remove etag for parent, if file is a keep_in_sync
  201. if (file.keepInSync()) {
  202. OCFile parent = mFileActivity.getStorageManager().getFileById(file.getParentId());
  203. parent.setEtag("");
  204. mFileActivity.getStorageManager().saveFile(parent);
  205. }
  206. downloaderBinder.cancel(account, file);
  207. } else if (uploaderBinder != null && uploaderBinder.isUploading(account, file)) {
  208. uploaderBinder.cancel(account, file);
  209. }
  210. }
  211. public long getOpIdWaitingFor() {
  212. return mWaitingForOpId;
  213. }
  214. public void setOpIdWaitingFor(long waitingForOpId) {
  215. mWaitingForOpId = waitingForOpId;
  216. }
  217. }