FileOperationsHelper.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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.os.storage.StorageManager;
  24. import android.support.v4.app.DialogFragment;
  25. import android.webkit.MimeTypeMap;
  26. import android.widget.Toast;
  27. import com.owncloud.android.R;
  28. import com.owncloud.android.datamodel.FileDataStorageManager;
  29. import com.owncloud.android.datamodel.OCFile;
  30. import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
  31. import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
  32. import com.owncloud.android.lib.common.accounts.AccountUtils.Constants;
  33. import com.owncloud.android.lib.common.network.WebdavUtils;
  34. import com.owncloud.android.lib.common.utils.Log_OC;
  35. import com.owncloud.android.lib.resources.status.OwnCloudVersion;
  36. import com.owncloud.android.services.OperationsService;
  37. import com.owncloud.android.ui.activity.FileActivity;
  38. import com.owncloud.android.ui.dialog.ShareLinkToDialog;
  39. import java.util.ArrayList;
  40. import java.util.List;
  41. import java.util.Vector;
  42. /**
  43. *
  44. * @author masensio
  45. * @author David A. Velasco
  46. */
  47. public class FileOperationsHelper {
  48. private static final String TAG = FileOperationsHelper.class.getName();
  49. private static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
  50. protected FileActivity mFileActivity = null;
  51. /// Identifier of operation in progress which result shouldn't be lost
  52. private long mWaitingForOpId = Long.MAX_VALUE;
  53. public FileOperationsHelper(FileActivity fileActivity) {
  54. mFileActivity = fileActivity;
  55. }
  56. public void openFile(OCFile file) {
  57. if (file != null) {
  58. String storagePath = file.getStoragePath();
  59. String encodedStoragePath = WebdavUtils.encodePath(storagePath);
  60. Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
  61. intentForSavedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), file.getMimetype());
  62. intentForSavedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  63. Intent intentForGuessedMimeType = null;
  64. if (storagePath.lastIndexOf('.') >= 0) {
  65. String guessedMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
  66. if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
  67. intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
  68. intentForGuessedMimeType.setDataAndType(Uri.parse("file://"+ encodedStoragePath), guessedMimeType);
  69. intentForGuessedMimeType.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  70. }
  71. }
  72. Intent chooserIntent = null;
  73. if (intentForGuessedMimeType != null) {
  74. chooserIntent = Intent.createChooser(intentForGuessedMimeType, mFileActivity.getString(R.string.actionbar_open_with));
  75. } else {
  76. chooserIntent = Intent.createChooser(intentForSavedMimeType, mFileActivity.getString(R.string.actionbar_open_with));
  77. }
  78. mFileActivity.startActivity(chooserIntent);
  79. } else {
  80. Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
  81. }
  82. }
  83. public void shareFileWithLink(OCFile file) {
  84. if (isSharedSupported()) {
  85. if (file != null) {
  86. String link = "https://fake.url";
  87. Intent intent = createShareWithLinkIntent(link);
  88. String[] packagesToExclude = new String[] { mFileActivity.getPackageName() };
  89. DialogFragment chooserDialog = ShareLinkToDialog.newInstance(intent, packagesToExclude, file);
  90. chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
  91. } else {
  92. Log_OC.wtf(TAG, "Trying to share a NULL OCFile");
  93. }
  94. } else {
  95. // Show a Message
  96. Toast t = Toast.makeText(mFileActivity, mFileActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
  97. t.show();
  98. }
  99. }
  100. public void shareFileWithLinkToApp(OCFile file, Intent sendIntent) {
  101. if (file != null) {
  102. mFileActivity.showLoadingDialog();
  103. Intent service = new Intent(mFileActivity, OperationsService.class);
  104. service.setAction(OperationsService.ACTION_CREATE_SHARE);
  105. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  106. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  107. service.putExtra(OperationsService.EXTRA_SEND_INTENT, sendIntent);
  108. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().newOperation(service);
  109. } else {
  110. Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
  111. }
  112. }
  113. private Intent createShareWithLinkIntent(String link) {
  114. Intent intentToShareLink = new Intent(Intent.ACTION_SEND);
  115. intentToShareLink.putExtra(Intent.EXTRA_TEXT, link);
  116. intentToShareLink.setType(HTTP.PLAIN_TEXT_TYPE);
  117. return intentToShareLink;
  118. }
  119. /**
  120. * @return 'True' if the server supports the Share API
  121. */
  122. public boolean isSharedSupported() {
  123. if (mFileActivity.getAccount() != null) {
  124. AccountManager accountManager = AccountManager.get(mFileActivity);
  125. String version = accountManager.getUserData(mFileActivity.getAccount(), Constants.KEY_OC_VERSION);
  126. return (new OwnCloudVersion(version)).isSharedSupported();
  127. }
  128. return false;
  129. }
  130. public void unshareFileWithLink(OCFile file) {
  131. if (isSharedSupported()) {
  132. // Unshare the file
  133. Intent service = new Intent(mFileActivity, OperationsService.class);
  134. service.setAction(OperationsService.ACTION_UNSHARE);
  135. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  136. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  137. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().newOperation(service);
  138. mFileActivity.showLoadingDialog();
  139. } else {
  140. // Show a Message
  141. Toast t = Toast.makeText(mFileActivity, mFileActivity.getString(R.string.share_link_no_support_share_api), Toast.LENGTH_LONG);
  142. t.show();
  143. }
  144. }
  145. public void sendDownloadedFile(OCFile file) {
  146. if (file != null) {
  147. Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
  148. // set MimeType
  149. sendIntent.setType(file.getMimetype());
  150. sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file.getStoragePath()));
  151. sendIntent.putExtra(Intent.ACTION_SEND, true); // Send Action
  152. // Show dialog, without the own app
  153. String[] packagesToExclude = new String[] { mFileActivity.getPackageName() };
  154. DialogFragment chooserDialog = ShareLinkToDialog.newInstance(sendIntent, packagesToExclude, file);
  155. chooserDialog.show(mFileActivity.getSupportFragmentManager(), FTAG_CHOOSER_DIALOG);
  156. } else {
  157. Log_OC.wtf(TAG, "Trying to send a NULL OCFile");
  158. }
  159. }
  160. public void syncFile(OCFile file) {
  161. Vector<OCFile> filesList = new Vector<OCFile>();
  162. if (!file.isFolder()){
  163. filesList.add(file);
  164. }else {
  165. // Add files recursivly
  166. FileDataStorageManager storageManager = mFileActivity.getStorageManager();
  167. filesList.addAll(storageManager.getFolderContent(file));
  168. boolean newfiles;
  169. do {
  170. Vector<OCFile> tmpFolders = new Vector<OCFile>();
  171. for (OCFile tmpfile : filesList) {
  172. if (tmpfile.isFolder()) {
  173. tmpFolders.add(tmpfile);
  174. }
  175. }
  176. if (tmpFolders.isEmpty()){
  177. newfiles = false;
  178. }else {
  179. for(OCFile tmpFolder : tmpFolders){
  180. filesList.remove(tmpFolder);
  181. filesList.addAll(storageManager.getFolderContent(tmpFolder));
  182. }
  183. newfiles = true;
  184. }
  185. }while(newfiles);
  186. }
  187. // Sync file(s)
  188. for (OCFile childFile : filesList) {
  189. Intent service = new Intent(mFileActivity, OperationsService.class);
  190. service.setAction(OperationsService.ACTION_SYNC_FILE);
  191. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  192. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, childFile.getRemotePath());
  193. service.putExtra(OperationsService.EXTRA_SYNC_FILE_CONTENTS, true);
  194. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().newOperation(service);
  195. }
  196. if (!file.isFolder()) {
  197. mFileActivity.showLoadingDialog();
  198. }
  199. }
  200. public void renameFile(OCFile file, String newFilename) {
  201. // RenameFile
  202. Intent service = new Intent(mFileActivity, OperationsService.class);
  203. service.setAction(OperationsService.ACTION_RENAME);
  204. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  205. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  206. service.putExtra(OperationsService.EXTRA_NEWNAME, newFilename);
  207. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().newOperation(service);
  208. mFileActivity.showLoadingDialog();
  209. }
  210. public void removeFile(OCFile file, boolean onlyLocalCopy) {
  211. // RemoveFile
  212. Intent service = new Intent(mFileActivity, OperationsService.class);
  213. service.setAction(OperationsService.ACTION_REMOVE);
  214. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  215. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath());
  216. service.putExtra(OperationsService.EXTRA_REMOVE_ONLY_LOCAL, onlyLocalCopy);
  217. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().newOperation(service);
  218. mFileActivity.showLoadingDialog();
  219. }
  220. public void createFolder(String remotePath, boolean createFullPath) {
  221. // Create Folder
  222. Intent service = new Intent(mFileActivity, OperationsService.class);
  223. service.setAction(OperationsService.ACTION_CREATE_FOLDER);
  224. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  225. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, remotePath);
  226. service.putExtra(OperationsService.EXTRA_CREATE_FULL_PATH, createFullPath);
  227. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().newOperation(service);
  228. mFileActivity.showLoadingDialog();
  229. }
  230. public void cancelTransference(OCFile file) {
  231. Account account = mFileActivity.getAccount();
  232. FileDownloaderBinder downloaderBinder = mFileActivity.getFileDownloaderBinder();
  233. FileUploaderBinder uploaderBinder = mFileActivity.getFileUploaderBinder();
  234. if (downloaderBinder != null && downloaderBinder.isDownloading(account, file)) {
  235. // Remove etag for parent, if file is a keep_in_sync
  236. if (file.keepInSync()) {
  237. OCFile parent = mFileActivity.getStorageManager().getFileById(file.getParentId());
  238. parent.setEtag("");
  239. mFileActivity.getStorageManager().saveFile(parent);
  240. }
  241. downloaderBinder.cancel(account, file);
  242. } else if (uploaderBinder != null && uploaderBinder.isUploading(account, file)) {
  243. uploaderBinder.cancel(account, file);
  244. }
  245. }
  246. /**
  247. * Start move file operation
  248. * @param newfile File where it is going to be moved
  249. * @param currentFile File with the previous info
  250. */
  251. public void moveFile(OCFile newfile, OCFile currentFile) {
  252. // Move files
  253. Intent service = new Intent(mFileActivity, OperationsService.class);
  254. service.setAction(OperationsService.ACTION_MOVE_FILE);
  255. service.putExtra(OperationsService.EXTRA_NEW_PARENT_PATH, newfile.getRemotePath());
  256. service.putExtra(OperationsService.EXTRA_REMOTE_PATH, currentFile.getRemotePath());
  257. service.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount());
  258. mWaitingForOpId = mFileActivity.getOperationsServiceBinder().newOperation(service);
  259. mFileActivity.showLoadingDialog();
  260. }
  261. public long getOpIdWaitingFor() {
  262. return mWaitingForOpId;
  263. }
  264. public void setOpIdWaitingFor(long waitingForOpId) {
  265. mWaitingForOpId = waitingForOpId;
  266. }
  267. }