GitAsyncTask: Acquire WeakReference of activity to prevent context leaks

Signed-off-by: Harsh Shandilya <msfjarvis@gmail.com>
This commit is contained in:
Harsh Shandilya 2019-05-31 13:26:52 +05:30
parent 68d20c5f2f
commit 56e53d36fd
No known key found for this signature in database
GPG key ID: C2E74282C2133D62

View file

@ -16,25 +16,31 @@ import org.eclipse.jgit.api.StatusCommand;
import org.eclipse.jgit.transport.PushResult; import org.eclipse.jgit.transport.PushResult;
import org.eclipse.jgit.transport.RemoteRefUpdate; import org.eclipse.jgit.transport.RemoteRefUpdate;
import java.lang.ref.WeakReference;
public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> { public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> {
private Activity activity; private WeakReference<Activity> activityWeakReference;
private boolean finishOnEnd; private boolean finishOnEnd;
private boolean refreshListOnEnd; private boolean refreshListOnEnd;
private ProgressDialog dialog; private ProgressDialog dialog;
private GitOperation operation; private GitOperation operation;
private Activity getActivity() {
return activityWeakReference.get();
}
public GitAsyncTask(Activity activity, boolean finishOnEnd, boolean refreshListOnEnd, GitOperation operation) { public GitAsyncTask(Activity activity, boolean finishOnEnd, boolean refreshListOnEnd, GitOperation operation) {
this.activity = activity; this.activityWeakReference = new WeakReference<>(activity);
this.finishOnEnd = finishOnEnd; this.finishOnEnd = finishOnEnd;
this.refreshListOnEnd = refreshListOnEnd; this.refreshListOnEnd = refreshListOnEnd;
this.operation = operation; this.operation = operation;
dialog = new ProgressDialog(this.activity); dialog = new ProgressDialog(getActivity());
} }
protected void onPreExecute() { protected void onPreExecute() {
this.dialog.setMessage(activity.getResources().getString(R.string.running_dialog_text)); this.dialog.setMessage(getActivity().getResources().getString(R.string.running_dialog_text));
this.dialog.setCancelable(false); this.dialog.setCancelable(false);
this.dialog.show(); this.dialog.show();
} }
@ -42,6 +48,7 @@ public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> {
@Override @Override
protected String doInBackground(GitCommand... commands) { protected String doInBackground(GitCommand... commands) {
Integer nbChanges = null; Integer nbChanges = null;
final Activity activity = getActivity();
for (GitCommand command : commands) { for (GitCommand command : commands) {
Log.d("doInBackground", "Executing the command <" + command.toString() + ">"); Log.d("doInBackground", "Executing the command <" + command.toString() + ">");
try { try {
@ -113,13 +120,13 @@ public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> {
this.operation.onSuccess(); this.operation.onSuccess();
if (finishOnEnd) { if (finishOnEnd) {
this.activity.setResult(Activity.RESULT_OK); this.getActivity().setResult(Activity.RESULT_OK);
this.activity.finish(); this.getActivity().finish();
} }
if (refreshListOnEnd) { if (refreshListOnEnd) {
try { try {
((PasswordStore) this.activity).updateListAdapter(); ((PasswordStore) this.getActivity()).updateListAdapter();
} catch (ClassCastException e) { } catch (ClassCastException e) {
// oups, mistake // oups, mistake
} }