mirror of
https://github.com/syncthing/syncthing-android.git
synced 2025-01-09 19:53:33 +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.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
/**
|
||||
* Shows the log information from Syncthing.
|
||||
|
@ -27,7 +28,7 @@ public class LogActivity extends SyncthingActivity {
|
|||
|
||||
private TextView mLog;
|
||||
private boolean mSyncthingLog = true;
|
||||
private AsyncTask mFetchLogTask;
|
||||
private AsyncTask mFetchLogTask = null;
|
||||
private ScrollView mScrollView;
|
||||
private Intent mShareIntent;
|
||||
|
||||
|
@ -97,27 +98,43 @@ public class LogActivity extends SyncthingActivity {
|
|||
}
|
||||
}
|
||||
|
||||
private void scrollToBottom() {
|
||||
mScrollView.post(() -> mScrollView.scrollTo(0, mLog.getBottom()));
|
||||
private void updateLog() {
|
||||
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) {
|
||||
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) {
|
||||
mLog.setText(log);
|
||||
if (mShareIntent != null)
|
||||
mShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, log);
|
||||
scrollToBottom();
|
||||
// Get a reference to the activity if it is still there.
|
||||
LogActivity logActivity = refLogActivity.get();
|
||||
if (logActivity == null || logActivity.isFinishing()) {
|
||||
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 "";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue