From 314f1fbd1fd9fb8c2722c64f80971a8c5a08b016 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Carlos=20Gonz=C3=A1lez=20Cabrero?= Date: Tue, 12 Apr 2016 10:51:21 +0200 Subject: [PATCH] Call to the async task with an uri array to iterate over it --- .../android/ui/activity/Uploader.java | 44 ++++--- .../android/utils/CopyTmpFileAsyncTask.java | 113 ++++++++---------- 2 files changed, 81 insertions(+), 76 deletions(-) diff --git a/src/com/owncloud/android/ui/activity/Uploader.java b/src/com/owncloud/android/ui/activity/Uploader.java index 7c5ffc0fb3..8c68788464 100644 --- a/src/com/owncloud/android/ui/activity/Uploader.java +++ b/src/com/owncloud/android/ui/activity/Uploader.java @@ -542,6 +542,10 @@ public class Uploader extends FileActivity @SuppressLint("NewApi") public void uploadFiles() { + + List contentUris = new ArrayList<>(); + List contentRemotePaths = new ArrayList<>(); + for (Parcelable sourceStream : mStreamsToUpload) { Uri sourceUri = (Uri) sourceStream; if (sourceUri == null) { @@ -556,8 +560,8 @@ public class Uploader extends FileActivity String remotePath = mUploadPath + displayName; if (ContentResolver.SCHEME_CONTENT.equals(sourceUri.getScheme())) { - /// content: uris will be copied to temporary files before calling {@link FileUploader} - copyThenUpload(sourceUri, remotePath); + contentUris.add(sourceUri); + contentRemotePaths.add(remotePath); } else if (ContentResolver.SCHEME_FILE.equals(sourceUri.getScheme())) { /// file: uris should point to a local file, should be safe let FileUploader handle them @@ -570,6 +574,10 @@ public class Uploader extends FileActivity } } + /// content: uris will be copied to temporary files before calling {@link FileUploader} + copyThenUpload(contentUris.toArray(new Uri[contentUris.size()]), + contentRemotePaths.toArray(new String[contentRemotePaths.size()])); + // Save the path to shared preferences; even if upload is not possible, user chose the folder SharedPreferences.Editor appPrefs = PreferenceManager .getDefaultSharedPreferences(getApplicationContext()).edit(); @@ -581,11 +589,11 @@ public class Uploader extends FileActivity /** * - * @param sourceUri - * @param remotePath + * @param sourceUris + * @param remotePaths */ - private void copyThenUpload(Uri sourceUri, String remotePath) { - mNumCacheFile++; + private void copyThenUpload(Uri[] sourceUris, String[] remotePaths) { + mNumCacheFile+= sourceUris.length; showWaitingCopyDialog(); @@ -593,9 +601,8 @@ public class Uploader extends FileActivity copyTask.execute( CopyTmpFileAsyncTask.makeParamsToExecute( getAccount(), - sourceUri, - remotePath, - mNumCacheFile + sourceUris, + remotePaths ) ); } @@ -828,14 +835,23 @@ public class Uploader extends FileActivity /** * Process the result of CopyTmpFileAsyncTask * - * @param result - * @param index + * @param numFiles */ @Override - public void onTmpFileCopied(String result, int index) { - if (mNumCacheFile-- == 0) { - dismissWaitingCopyDialog(); + public void onTmpFileCopied(int numFiles) { + + dismissWaitingCopyDialog(); + + if(mNumCacheFile != numFiles) { + String message = String.format( + getString(R.string.uploader_error_forbidden_content), + getString(R.string.app_name) + ); + Toast.makeText(this, message, Toast.LENGTH_LONG).show(); + Log_OC.d(TAG, message); } + + mNumCacheFile -= numFiles; } /** diff --git a/src/com/owncloud/android/utils/CopyTmpFileAsyncTask.java b/src/com/owncloud/android/utils/CopyTmpFileAsyncTask.java index 7ae94c6502..92612b6492 100644 --- a/src/com/owncloud/android/utils/CopyTmpFileAsyncTask.java +++ b/src/com/owncloud/android/utils/CopyTmpFileAsyncTask.java @@ -39,7 +39,7 @@ import java.lang.ref.WeakReference; /** * AsyncTask to copy a file from a uri in a temporal file */ -public class CopyTmpFileAsyncTask extends AsyncTask { +public class CopyTmpFileAsyncTask extends AsyncTask { private final String TAG = CopyTmpFileAsyncTask.class.getSimpleName(); @@ -52,16 +52,14 @@ public class CopyTmpFileAsyncTask extends AsyncTask { */ public final static Object[] makeParamsToExecute( Account account, - Uri sourceUri, - String remotePath, - Integer numCacheFile + Uri[] sourceUris, + String[] remotePaths ) { return new Object[] { account, - sourceUri, - remotePath, - numCacheFile + sourceUris, + remotePaths }; } @@ -78,14 +76,12 @@ public class CopyTmpFileAsyncTask extends AsyncTask { * since it needs to exist until the end of the AsyncTask although the caller Activity were finished * before. */ - private final Context mAppContext; + private final Context mContext; - private int mIndex; - public CopyTmpFileAsyncTask(OnCopyTmpFileTaskListener listener, Context context) { mListener = new WeakReference(listener); - mAppContext = context.getApplicationContext(); + mContext = context; } /** @@ -97,48 +93,59 @@ public class CopyTmpFileAsyncTask extends AsyncTask { * - ContentResolver: content resolver */ @Override - protected String doInBackground(Object[] params) { - String pathToCopiedFile = null; + protected Integer doInBackground(Object[] params) { - if (params != null && params.length == 4) { + int numFiles = 0; + + if (params != null && params.length == 3) { Account account = (Account) params[0]; - Uri uri = (Uri) params[1]; - String remotePath = (String) params[2]; - mIndex = ((Integer) params[3]); // TODO really? + Uri[] uris = (Uri[]) params[1]; + String[] remotePaths = (String[]) params[2]; InputStream inputStream = null; FileOutputStream outputStream = null; String fullTempPath = null; - ContentResolver contentResolver = mAppContext.getContentResolver(); + Uri actualUri = null; + String actualRemotePath = null; + + ContentResolver contentResolver = mContext.getContentResolver(); // TODO: test that it's safe for URLs with temporary access; // alternative: receive InputStream in another parameter try { - fullTempPath = FileStorageUtils.getTemporalPath(account.name) + remotePath; - inputStream = contentResolver.openInputStream(uri); - File cacheFile = new File(fullTempPath); - File tempDir = cacheFile.getParentFile(); - if (!tempDir.exists()) { - tempDir.mkdirs(); + for(int i=0; i < uris.length; i++) { + actualUri = uris[i]; + actualRemotePath = remotePaths[i]; + + fullTempPath = FileStorageUtils.getTemporalPath(account.name) + actualRemotePath; + inputStream = contentResolver.openInputStream(actualUri); + File cacheFile = new File(fullTempPath); + File tempDir = cacheFile.getParentFile(); + if (!tempDir.exists()) { + tempDir.mkdirs(); + } + cacheFile.createNewFile(); + outputStream = new FileOutputStream(fullTempPath); + byte[] buffer = new byte[4096]; + + int count = 0; + + while ((count = inputStream.read(buffer)) > 0) { + outputStream.write(buffer, 0, count); + } + + requestUpload( + account, + fullTempPath, + actualRemotePath, + contentResolver.getType(actualUri) + ); + numFiles++; } - cacheFile.createNewFile(); - outputStream = new FileOutputStream(fullTempPath); - byte[] buffer = new byte[4096]; - - int count = 0; - - while ((count = inputStream.read(buffer)) > 0) { - outputStream.write(buffer, 0, count); - } - - outputStream.close(); - inputStream.close(); - - pathToCopiedFile = fullTempPath; } catch (Exception e) { - Log_OC.e(TAG, "Exception while copying " + uri.toString() + " to temporary file", e); + Log_OC.e(TAG, "Exception while copying " + actualUri.toString() + " to temporary file", e); // clean if (fullTempPath != null) { @@ -168,35 +175,17 @@ public class CopyTmpFileAsyncTask extends AsyncTask { } } - if (pathToCopiedFile != null) { - requestUpload( - account, - pathToCopiedFile, - remotePath, - contentResolver.getType(uri) - ); - // mRemoteCacheData.get(index), - - } else { - String message = String.format( - mAppContext.getString(R.string.uploader_error_forbidden_content), - mAppContext.getString(R.string.app_name) - ); - Toast.makeText(mAppContext, message, Toast.LENGTH_LONG).show(); - Log_OC.d(TAG, message); - } - } else { throw new IllegalArgumentException("Error in parameters number"); } - return pathToCopiedFile; + return numFiles; } private void requestUpload(Account account, String localPath, String remotePath, String mimeType) { FileUploader.UploadRequester requester = new FileUploader.UploadRequester(); requester.uploadNewFile( - mAppContext, + mContext, account, localPath, remotePath, @@ -209,10 +198,10 @@ public class CopyTmpFileAsyncTask extends AsyncTask { } @Override - protected void onPostExecute(String result) { + protected void onPostExecute(Integer numFiles) { OnCopyTmpFileTaskListener listener = mListener.get(); if (listener!= null) { - listener.onTmpFileCopied(result, mIndex); + listener.onTmpFileCopied(numFiles.intValue()); } else { Log_OC.i(TAG, "User left Uploader activity before the temporal copies were finished "); } @@ -223,6 +212,6 @@ public class CopyTmpFileAsyncTask extends AsyncTask { */ public interface OnCopyTmpFileTaskListener{ - void onTmpFileCopied(String result, int index); + void onTmpFileCopied(int numFiles); } }