Call to the async task with an uri array to iterate over it

This commit is contained in:
Juan Carlos González Cabrero 2016-04-12 10:51:21 +02:00
parent 6430ece72c
commit 314f1fbd1f
2 changed files with 81 additions and 76 deletions

View file

@ -542,6 +542,10 @@ public class Uploader extends FileActivity
@SuppressLint("NewApi") @SuppressLint("NewApi")
public void uploadFiles() { public void uploadFiles() {
List<Uri> contentUris = new ArrayList<>();
List<String> contentRemotePaths = new ArrayList<>();
for (Parcelable sourceStream : mStreamsToUpload) { for (Parcelable sourceStream : mStreamsToUpload) {
Uri sourceUri = (Uri) sourceStream; Uri sourceUri = (Uri) sourceStream;
if (sourceUri == null) { if (sourceUri == null) {
@ -556,8 +560,8 @@ public class Uploader extends FileActivity
String remotePath = mUploadPath + displayName; String remotePath = mUploadPath + displayName;
if (ContentResolver.SCHEME_CONTENT.equals(sourceUri.getScheme())) { if (ContentResolver.SCHEME_CONTENT.equals(sourceUri.getScheme())) {
/// content: uris will be copied to temporary files before calling {@link FileUploader} contentUris.add(sourceUri);
copyThenUpload(sourceUri, remotePath); contentRemotePaths.add(remotePath);
} else if (ContentResolver.SCHEME_FILE.equals(sourceUri.getScheme())) { } else if (ContentResolver.SCHEME_FILE.equals(sourceUri.getScheme())) {
/// file: uris should point to a local file, should be safe let FileUploader handle them /// 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 // Save the path to shared preferences; even if upload is not possible, user chose the folder
SharedPreferences.Editor appPrefs = PreferenceManager SharedPreferences.Editor appPrefs = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext()).edit(); .getDefaultSharedPreferences(getApplicationContext()).edit();
@ -581,11 +589,11 @@ public class Uploader extends FileActivity
/** /**
* *
* @param sourceUri * @param sourceUris
* @param remotePath * @param remotePaths
*/ */
private void copyThenUpload(Uri sourceUri, String remotePath) { private void copyThenUpload(Uri[] sourceUris, String[] remotePaths) {
mNumCacheFile++; mNumCacheFile+= sourceUris.length;
showWaitingCopyDialog(); showWaitingCopyDialog();
@ -593,9 +601,8 @@ public class Uploader extends FileActivity
copyTask.execute( copyTask.execute(
CopyTmpFileAsyncTask.makeParamsToExecute( CopyTmpFileAsyncTask.makeParamsToExecute(
getAccount(), getAccount(),
sourceUri, sourceUris,
remotePath, remotePaths
mNumCacheFile
) )
); );
} }
@ -828,14 +835,23 @@ public class Uploader extends FileActivity
/** /**
* Process the result of CopyTmpFileAsyncTask * Process the result of CopyTmpFileAsyncTask
* *
* @param result * @param numFiles
* @param index
*/ */
@Override @Override
public void onTmpFileCopied(String result, int index) { public void onTmpFileCopied(int numFiles) {
if (mNumCacheFile-- == 0) {
dismissWaitingCopyDialog(); 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;
} }
/** /**

View file

@ -39,7 +39,7 @@ import java.lang.ref.WeakReference;
/** /**
* AsyncTask to copy a file from a uri in a temporal file * AsyncTask to copy a file from a uri in a temporal file
*/ */
public class CopyTmpFileAsyncTask extends AsyncTask<Object, Void, String> { public class CopyTmpFileAsyncTask extends AsyncTask<Object, Void, Integer> {
private final String TAG = CopyTmpFileAsyncTask.class.getSimpleName(); private final String TAG = CopyTmpFileAsyncTask.class.getSimpleName();
@ -52,16 +52,14 @@ public class CopyTmpFileAsyncTask extends AsyncTask<Object, Void, String> {
*/ */
public final static Object[] makeParamsToExecute( public final static Object[] makeParamsToExecute(
Account account, Account account,
Uri sourceUri, Uri[] sourceUris,
String remotePath, String[] remotePaths
Integer numCacheFile
) { ) {
return new Object[] { return new Object[] {
account, account,
sourceUri, sourceUris,
remotePath, remotePaths
numCacheFile
}; };
} }
@ -78,14 +76,12 @@ public class CopyTmpFileAsyncTask extends AsyncTask<Object, Void, String> {
* since it needs to exist until the end of the AsyncTask although the caller Activity were finished * since it needs to exist until the end of the AsyncTask although the caller Activity were finished
* before. * before.
*/ */
private final Context mAppContext; private final Context mContext;
private int mIndex;
public CopyTmpFileAsyncTask(OnCopyTmpFileTaskListener listener, Context context) { public CopyTmpFileAsyncTask(OnCopyTmpFileTaskListener listener, Context context) {
mListener = new WeakReference<OnCopyTmpFileTaskListener>(listener); mListener = new WeakReference<OnCopyTmpFileTaskListener>(listener);
mAppContext = context.getApplicationContext(); mContext = context;
} }
/** /**
@ -97,48 +93,59 @@ public class CopyTmpFileAsyncTask extends AsyncTask<Object, Void, String> {
* - ContentResolver: content resolver * - ContentResolver: content resolver
*/ */
@Override @Override
protected String doInBackground(Object[] params) { protected Integer doInBackground(Object[] params) {
String pathToCopiedFile = null;
if (params != null && params.length == 4) { int numFiles = 0;
if (params != null && params.length == 3) {
Account account = (Account) params[0]; Account account = (Account) params[0];
Uri uri = (Uri) params[1]; Uri[] uris = (Uri[]) params[1];
String remotePath = (String) params[2]; String[] remotePaths = (String[]) params[2];
mIndex = ((Integer) params[3]); // TODO really?
InputStream inputStream = null; InputStream inputStream = null;
FileOutputStream outputStream = null; FileOutputStream outputStream = null;
String fullTempPath = 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; // TODO: test that it's safe for URLs with temporary access;
// alternative: receive InputStream in another parameter // alternative: receive InputStream in another parameter
try { try {
fullTempPath = FileStorageUtils.getTemporalPath(account.name) + remotePath; for(int i=0; i < uris.length; i++) {
inputStream = contentResolver.openInputStream(uri); actualUri = uris[i];
File cacheFile = new File(fullTempPath); actualRemotePath = remotePaths[i];
File tempDir = cacheFile.getParentFile();
if (!tempDir.exists()) { fullTempPath = FileStorageUtils.getTemporalPath(account.name) + actualRemotePath;
tempDir.mkdirs(); 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) { } 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 // clean
if (fullTempPath != null) { if (fullTempPath != null) {
@ -168,35 +175,17 @@ public class CopyTmpFileAsyncTask extends AsyncTask<Object, Void, String> {
} }
} }
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 { } else {
throw new IllegalArgumentException("Error in parameters number"); throw new IllegalArgumentException("Error in parameters number");
} }
return pathToCopiedFile; return numFiles;
} }
private void requestUpload(Account account, String localPath, String remotePath, String mimeType) { private void requestUpload(Account account, String localPath, String remotePath, String mimeType) {
FileUploader.UploadRequester requester = new FileUploader.UploadRequester(); FileUploader.UploadRequester requester = new FileUploader.UploadRequester();
requester.uploadNewFile( requester.uploadNewFile(
mAppContext, mContext,
account, account,
localPath, localPath,
remotePath, remotePath,
@ -209,10 +198,10 @@ public class CopyTmpFileAsyncTask extends AsyncTask<Object, Void, String> {
} }
@Override @Override
protected void onPostExecute(String result) { protected void onPostExecute(Integer numFiles) {
OnCopyTmpFileTaskListener listener = mListener.get(); OnCopyTmpFileTaskListener listener = mListener.get();
if (listener!= null) { if (listener!= null) {
listener.onTmpFileCopied(result, mIndex); listener.onTmpFileCopied(numFiles.intValue());
} else { } else {
Log_OC.i(TAG, "User left Uploader activity before the temporal copies were finished "); Log_OC.i(TAG, "User left Uploader activity before the temporal copies were finished ");
} }
@ -223,6 +212,6 @@ public class CopyTmpFileAsyncTask extends AsyncTask<Object, Void, String> {
*/ */
public interface OnCopyTmpFileTaskListener{ public interface OnCopyTmpFileTaskListener{
void onTmpFileCopied(String result, int index); void onTmpFileCopied(int numFiles);
} }
} }