UriUploader.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * Copyright (C) 2016 ownCloud Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2,
  8. * as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package com.owncloud.android.ui.helpers;
  20. import android.accounts.Account;
  21. import android.content.ContentResolver;
  22. import android.net.Uri;
  23. import android.os.Parcelable;
  24. import com.owncloud.android.R;
  25. import com.owncloud.android.files.services.FileUploader;
  26. import com.owncloud.android.lib.common.utils.Log_OC;
  27. import com.owncloud.android.operations.UploadFileOperation;
  28. import com.owncloud.android.ui.activity.FileActivity;
  29. import com.owncloud.android.ui.asynctasks.CopyAndUploadContentUrisTask;
  30. import com.owncloud.android.utils.DisplayUtils;
  31. import com.owncloud.android.utils.UriUtils;
  32. import java.util.ArrayList;
  33. import java.util.List;
  34. /**
  35. * This class handle both file:// and content:// URIs pointing to files to upload.
  36. */
  37. public class UriUploader {
  38. private final String TAG = UriUploader.class.getSimpleName();
  39. private FileActivity mActivity;
  40. private ArrayList<Parcelable> mUrisToUpload;
  41. private CopyAndUploadContentUrisTask.OnCopyTmpFilesTaskListener mCopyTmpTaskListener;
  42. private int mBehaviour;
  43. private String mUploadPath;
  44. private Account mAccount;
  45. private boolean mShowWaitingDialog;
  46. private UriUploaderResultCode mCode = UriUploaderResultCode.OK;
  47. public enum UriUploaderResultCode {
  48. OK,
  49. ERROR_UNKNOWN,
  50. ERROR_NO_FILE_TO_UPLOAD,
  51. ERROR_READ_PERMISSION_NOT_GRANTED
  52. }
  53. public UriUploader(
  54. FileActivity activity,
  55. ArrayList<Parcelable> uris,
  56. String uploadPath,
  57. Account account,
  58. int behaviour,
  59. boolean showWaitingDialog,
  60. CopyAndUploadContentUrisTask.OnCopyTmpFilesTaskListener copyTmpTaskListener
  61. ) {
  62. mActivity = activity;
  63. mUrisToUpload = uris;
  64. mUploadPath = uploadPath;
  65. mAccount = account;
  66. mBehaviour = behaviour;
  67. mShowWaitingDialog = showWaitingDialog;
  68. mCopyTmpTaskListener = copyTmpTaskListener;
  69. }
  70. public UriUploaderResultCode uploadUris() {
  71. try {
  72. List<Uri> contentUris = new ArrayList<>();
  73. List<String> contentRemotePaths = new ArrayList<>();
  74. int schemeFileCounter = 0;
  75. for (Parcelable sourceStream : mUrisToUpload) {
  76. Uri sourceUri = (Uri) sourceStream;
  77. if (sourceUri != null) {
  78. String displayName = UriUtils.getDisplayNameForUri(sourceUri, mActivity);
  79. if (displayName == null) {
  80. displayName = generateDiplayName();
  81. }
  82. String remotePath = mUploadPath + displayName;
  83. if (ContentResolver.SCHEME_CONTENT.equals(sourceUri.getScheme())) {
  84. contentUris.add(sourceUri);
  85. contentRemotePaths.add(remotePath);
  86. } else if (ContentResolver.SCHEME_FILE.equals(sourceUri.getScheme())) {
  87. /// file: uris should point to a local file, should be safe let FileUploader handle them
  88. requestUpload(sourceUri.getPath(), remotePath);
  89. schemeFileCounter++;
  90. }
  91. }
  92. }
  93. if (!contentUris.isEmpty()) {
  94. /// content: uris will be copied to temporary files before calling {@link FileUploader}
  95. copyThenUpload(contentUris.toArray(new Uri[contentUris.size()]),
  96. contentRemotePaths.toArray(new String[contentRemotePaths.size()]));
  97. } else if (schemeFileCounter == 0) {
  98. mCode = UriUploaderResultCode.ERROR_NO_FILE_TO_UPLOAD;
  99. }
  100. } catch (SecurityException e) {
  101. mCode = UriUploaderResultCode.ERROR_READ_PERMISSION_NOT_GRANTED;
  102. Log_OC.e(TAG, "Permissions fail", e);
  103. } catch (Exception e) {
  104. mCode = UriUploaderResultCode.ERROR_UNKNOWN;
  105. Log_OC.e(TAG, "Unexpected error", e);
  106. }
  107. return mCode;
  108. }
  109. private String generateDiplayName() {
  110. return mActivity.getString(R.string.common_unknown) +
  111. "-" + DisplayUtils.unixTimeToHumanReadable(System.currentTimeMillis());
  112. }
  113. /**
  114. * Requests the upload of a file in the local file system to {@link FileUploader} service.
  115. *
  116. * The original file will be left in its original location, and will not be duplicated.
  117. * As a side effect, the user will see the file as not uploaded when accesses to the OC app.
  118. * This is considered as acceptable, since when a file is shared from another app to OC,
  119. * the usual workflow will go back to the original app.
  120. *
  121. * @param localPath Absolute path in the local file system to the file to upload.
  122. * @param remotePath Absolute path in the current OC account to set to the uploaded file.
  123. */
  124. private void requestUpload(String localPath, String remotePath) {
  125. FileUploader.UploadRequester requester = new FileUploader.UploadRequester();
  126. requester.uploadNewFile(
  127. mActivity,
  128. mAccount,
  129. localPath,
  130. remotePath,
  131. mBehaviour,
  132. null, // MIME type will be detected from file name
  133. false, // do not create parent folder if not existent
  134. UploadFileOperation.CREATED_BY_USER
  135. );
  136. }
  137. /**
  138. *
  139. * @param sourceUris Array of content:// URIs to the files to upload
  140. * @param remotePaths Array of absolute paths to set to the uploaded files
  141. */
  142. private void copyThenUpload(Uri[] sourceUris, String[] remotePaths) {
  143. if (mShowWaitingDialog) {
  144. mActivity.showLoadingDialog(mActivity.getResources().
  145. getString(R.string.wait_for_tmp_copy_from_private_storage));
  146. }
  147. CopyAndUploadContentUrisTask copyTask = new CopyAndUploadContentUrisTask
  148. (mCopyTmpTaskListener, mActivity);
  149. copyTask.execute(
  150. CopyAndUploadContentUrisTask.makeParamsToExecute(
  151. mAccount,
  152. sourceUris,
  153. remotePaths,
  154. mActivity.getContentResolver()
  155. )
  156. );
  157. }
  158. }