mirror of
https://github.com/syncthing/syncthing-android.git
synced 2025-01-10 03:55:53 +00:00
Fix static leak in LogActivity#AsyncTask (#1142)
This commit is contained in:
parent
6122c8befa
commit
b93da522e1
1 changed files with 70 additions and 52 deletions
|
@ -17,6 +17,7 @@ import com.nutomic.syncthingandroid.R;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.lang.ref.WeakReference;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shows the log information from Syncthing.
|
* Shows the log information from Syncthing.
|
||||||
|
@ -27,7 +28,7 @@ public class LogActivity extends SyncthingActivity {
|
||||||
|
|
||||||
private TextView mLog;
|
private TextView mLog;
|
||||||
private boolean mSyncthingLog = true;
|
private boolean mSyncthingLog = true;
|
||||||
private AsyncTask mFetchLogTask;
|
private AsyncTask mFetchLogTask = null;
|
||||||
private ScrollView mScrollView;
|
private ScrollView mScrollView;
|
||||||
private Intent mShareIntent;
|
private Intent mShareIntent;
|
||||||
|
|
||||||
|
@ -97,27 +98,43 @@ public class LogActivity extends SyncthingActivity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void scrollToBottom() {
|
private void updateLog() {
|
||||||
mScrollView.post(() -> mScrollView.scrollTo(0, mLog.getBottom()));
|
if (mFetchLogTask != null) {
|
||||||
|
mFetchLogTask.cancel(true);
|
||||||
|
}
|
||||||
|
mLog.setText(R.string.retrieving_logs);
|
||||||
|
mFetchLogTask = new UpdateLogTask(this).execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class UpdateLogTask extends AsyncTask<Void, Void, String> {
|
||||||
|
private WeakReference<LogActivity> refLogActivity;
|
||||||
|
|
||||||
|
UpdateLogTask(LogActivity context) {
|
||||||
|
refLogActivity = new WeakReference<>(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateLog() {
|
|
||||||
if (mFetchLogTask != null)
|
|
||||||
mFetchLogTask.cancel(true);
|
|
||||||
mLog.setText(R.string.retrieving_logs);
|
|
||||||
mFetchLogTask = new AsyncTask<Void, Void, String>() {
|
|
||||||
@Override
|
|
||||||
protected String doInBackground(Void... params) {
|
protected String doInBackground(Void... params) {
|
||||||
return getLog(mSyncthingLog);
|
// Get a reference to the activity if it is still there.
|
||||||
|
LogActivity logActivity = refLogActivity.get();
|
||||||
|
if (logActivity == null || logActivity.isFinishing()) {
|
||||||
|
cancel(true);
|
||||||
|
return "";
|
||||||
}
|
}
|
||||||
@Override
|
return getLog(logActivity.mSyncthingLog);
|
||||||
|
}
|
||||||
|
|
||||||
protected void onPostExecute(String log) {
|
protected void onPostExecute(String log) {
|
||||||
mLog.setText(log);
|
// Get a reference to the activity if it is still there.
|
||||||
if (mShareIntent != null)
|
LogActivity logActivity = refLogActivity.get();
|
||||||
mShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, log);
|
if (logActivity == null || logActivity.isFinishing()) {
|
||||||
scrollToBottom();
|
return;
|
||||||
}
|
}
|
||||||
}.execute();
|
logActivity.mLog.setText(log);
|
||||||
|
if (logActivity.mShareIntent != null) {
|
||||||
|
logActivity.mShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, log);
|
||||||
|
}
|
||||||
|
// Scroll to bottom
|
||||||
|
logActivity.mScrollView.post(() -> logActivity.mScrollView.scrollTo(0, logActivity.mLog.getBottom()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -155,5 +172,6 @@ public class LogActivity extends SyncthingActivity {
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue