1
0
Fork 0
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:
Catfriend1 2018-06-17 20:35:05 +02:00 committed by Audrius Butkevicius
parent 6122c8befa
commit b93da522e1

View file

@ -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,63 +98,80 @@ public class LogActivity extends SyncthingActivity {
} }
} }
private void scrollToBottom() {
mScrollView.post(() -> mScrollView.scrollTo(0, mLog.getBottom()));
}
private void updateLog() { private void updateLog() {
if (mFetchLogTask != null) if (mFetchLogTask != null) {
mFetchLogTask.cancel(true); mFetchLogTask.cancel(true);
}
mLog.setText(R.string.retrieving_logs); mLog.setText(R.string.retrieving_logs);
mFetchLogTask = new AsyncTask<Void, Void, String>() { mFetchLogTask = new UpdateLogTask(this).execute();
@Override
protected String doInBackground(Void... params) {
return getLog(mSyncthingLog);
}
@Override
protected void onPostExecute(String log) {
mLog.setText(log);
if (mShareIntent != null)
mShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, log);
scrollToBottom();
}
}.execute();
} }
/** private static class UpdateLogTask extends AsyncTask<Void, Void, String> {
* Queries logcat to obtain a log. private WeakReference<LogActivity> refLogActivity;
*
* @param syncthingLog Filter on Syncthing's native messages. UpdateLogTask(LogActivity context) {
*/ refLogActivity = new WeakReference<>(context);
private String getLog(final boolean syncthingLog) { }
Process process = null;
try { protected String doInBackground(Void... params) {
ProcessBuilder pb; // Get a reference to the activity if it is still there.
if (syncthingLog) { LogActivity logActivity = refLogActivity.get();
pb = new ProcessBuilder("/system/bin/logcat", "-t", "300", "-v", "time", "-s", "SyncthingNativeCode"); if (logActivity == null || logActivity.isFinishing()) {
} else { cancel(true);
pb = new ProcessBuilder("/system/bin/logcat", "-t", "300", "-v", "time", "*:i ps:s art:s"); return "";
} }
pb.redirectErrorStream(true); return getLog(logActivity.mSyncthingLog);
process = pb.start(); }
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream(), "UTF-8"), 8192); protected void onPostExecute(String log) {
StringBuilder log = new StringBuilder(); // Get a reference to the activity if it is still there.
String line; LogActivity logActivity = refLogActivity.get();
String sep = System.getProperty("line.separator"); if (logActivity == null || logActivity.isFinishing()) {
while ((line = bufferedReader.readLine()) != null) { return;
log.append(line); }
log.append(sep); logActivity.mLog.setText(log);
} if (logActivity.mShareIntent != null) {
return log.toString(); logActivity.mShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, log);
} catch (IOException e) { }
Log.w(TAG, "Error reading Android log", e); // Scroll to bottom
} finally { logActivity.mScrollView.post(() -> logActivity.mScrollView.scrollTo(0, logActivity.mLog.getBottom()));
if (process != null) { }
process.destroy();
} /**
* Queries logcat to obtain a log.
*
* @param syncthingLog Filter on Syncthing's native messages.
*/
private String getLog(final boolean syncthingLog) {
Process process = null;
try {
ProcessBuilder pb;
if (syncthingLog) {
pb = new ProcessBuilder("/system/bin/logcat", "-t", "300", "-v", "time", "-s", "SyncthingNativeCode");
} else {
pb = new ProcessBuilder("/system/bin/logcat", "-t", "300", "-v", "time", "*:i ps:s art:s");
}
pb.redirectErrorStream(true);
process = pb.start();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream(), "UTF-8"), 8192);
StringBuilder log = new StringBuilder();
String line;
String sep = System.getProperty("line.separator");
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
log.append(sep);
}
return log.toString();
} catch (IOException e) {
Log.w(TAG, "Error reading Android log", e);
} finally {
if (process != null) {
process.destroy();
}
}
return "";
} }
return "";
} }
} }