1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-26 14:21:16 +00:00

Fix static leak in ShareActivity#CopyFilesTask (#1140)

This commit is contained in:
Catfriend1 2018-06-17 18:06:07 +02:00 committed by Audrius Butkevicius
parent 691edabaf6
commit 585191bd66

View file

@ -31,6 +31,7 @@ import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.text.DateFormat; import java.text.DateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
@ -150,7 +151,8 @@ public class ShareActivity extends StateDialogActivity
files.entrySet().iterator().next().setValue(mShareName.getText().toString()); files.entrySet().iterator().next().setValue(mShareName.getText().toString());
Folder folder = (Folder) mFoldersSpinner.getSelectedItem(); Folder folder = (Folder) mFoldersSpinner.getSelectedItem();
File directory = new File(folder.path, getSavedSubDirectory()); File directory = new File(folder.path, getSavedSubDirectory());
new CopyFilesTask(files, folder, directory).execute(); CopyFilesTask mCopyFilesTask = new CopyFilesTask(this, files, folder, directory);
mCopyFilesTask.execute();
}); });
mFoldersSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { mFoldersSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@ -272,25 +274,37 @@ public class ShareActivity extends StateDialogActivity
return savedSubDirectory; return savedSubDirectory;
} }
private class CopyFilesTask extends AsyncTask<Void, Void, Boolean> { private static class CopyFilesTask extends AsyncTask<Void, Void, Boolean> {
private WeakReference<ShareActivity> refShareActivity;
private ProgressDialog mProgress; private ProgressDialog mProgress;
private final Map<Uri, String> mFiles; private final Map<Uri, String> mFiles;
private final Folder mFolder; private final Folder mFolder;
private final File mDirectory; private final File mDirectory;
private int mCopied = 0, mIgnored = 0; private int mCopied = 0, mIgnored = 0;
CopyFilesTask(Map<Uri, String> files, Folder folder, File directory) { CopyFilesTask(ShareActivity context, Map<Uri, String> files, Folder folder, File directory) {
refShareActivity = new WeakReference<>(context);
this.mFiles = files; this.mFiles = files;
this.mFolder = folder; this.mFolder = folder;
this.mDirectory = directory; this.mDirectory = directory;
} }
protected void onPreExecute() { protected void onPreExecute() {
mProgress = ProgressDialog.show(ShareActivity.this, null, // Get a reference to the activity if it is still there.
getString(R.string.copy_progress), true); ShareActivity shareActivity = refShareActivity.get();
// shareActivity cannot be null before the task executes.
mProgress = ProgressDialog.show(shareActivity, null,
shareActivity.getString(R.string.copy_progress), true);
} }
protected Boolean doInBackground(Void... params) { protected Boolean doInBackground(Void... params) {
// Get a reference to the activity if it is still there.
ShareActivity shareActivity = refShareActivity.get();
if (shareActivity == null || shareActivity.isFinishing()) {
cancel(true);
return true;
}
boolean isError = false; boolean isError = false;
for (Map.Entry<Uri, String> entry : mFiles.entrySet()) { for (Map.Entry<Uri, String> entry : mFiles.entrySet()) {
InputStream inputStream = null; InputStream inputStream = null;
@ -300,7 +314,7 @@ public class ShareActivity extends StateDialogActivity
mIgnored++; mIgnored++;
continue; continue;
} }
inputStream = getContentResolver().openInputStream(entry.getKey()); inputStream = shareActivity.getContentResolver().openInputStream(entry.getKey());
Files.asByteSink(outFile).writeFrom(inputStream); Files.asByteSink(outFile).writeFrom(inputStream);
mCopied++; mCopied++;
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
@ -324,18 +338,23 @@ public class ShareActivity extends StateDialogActivity
} }
protected void onPostExecute(Boolean isError) { protected void onPostExecute(Boolean isError) {
Util.dismissDialogSafe(mProgress, ShareActivity.this); // Get a reference to the activity if it is still there.
Toast.makeText(ShareActivity.this, mIgnored > 0 ? ShareActivity shareActivity = refShareActivity.get();
getResources().getQuantityString(R.plurals.copy_success_partially, mCopied, if (shareActivity == null || shareActivity.isFinishing()) {
return;
}
Util.dismissDialogSafe(mProgress, shareActivity);
Toast.makeText(shareActivity, mIgnored > 0 ?
shareActivity.getResources().getQuantityString(R.plurals.copy_success_partially, mCopied,
mCopied, mFolder.label, mIgnored) : mCopied, mFolder.label, mIgnored) :
getResources().getQuantityString(R.plurals.copy_success, mCopied, mCopied, shareActivity.getResources().getQuantityString(R.plurals.copy_success, mCopied, mCopied,
mFolder.label), mFolder.label),
Toast.LENGTH_LONG).show(); Toast.LENGTH_LONG).show();
if (isError) { if (isError) {
Toast.makeText(ShareActivity.this, getString(R.string.copy_exception), Toast.makeText(shareActivity, shareActivity.getString(R.string.copy_exception),
Toast.LENGTH_SHORT).show(); Toast.LENGTH_SHORT).show();
} }
finish(); shareActivity.finish();
} }
} }