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")
public void uploadFiles() {
List<Uri> contentUris = new ArrayList<>();
List<String> 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;
}
/**

View file

@ -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<Object, Void, String> {
public class CopyTmpFileAsyncTask extends AsyncTask<Object, Void, Integer> {
private final String TAG = CopyTmpFileAsyncTask.class.getSimpleName();
@ -52,16 +52,14 @@ public class CopyTmpFileAsyncTask extends AsyncTask<Object, Void, String> {
*/
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<Object, Void, String> {
* 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<OnCopyTmpFileTaskListener>(listener);
mAppContext = context.getApplicationContext();
mContext = context;
}
/**
@ -97,48 +93,59 @@ public class CopyTmpFileAsyncTask extends AsyncTask<Object, Void, String> {
* - 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<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 {
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<Object, Void, String> {
}
@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<Object, Void, String> {
*/
public interface OnCopyTmpFileTaskListener{
void onTmpFileCopied(String result, int index);
void onTmpFileCopied(int numFiles);
}
}