1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-29 15:51:17 +00:00

Fix static leak in SettingsActivity#TestRootTask (#1141)

This commit is contained in:
Catfriend1 2018-06-17 18:27:55 +02:00 committed by Audrius Butkevicius
parent 585191bd66
commit f2064ab3b1

View file

@ -36,6 +36,7 @@ import com.nutomic.syncthingandroid.util.Languages;
import com.nutomic.syncthingandroid.util.Util; import com.nutomic.syncthingandroid.util.Util;
import com.nutomic.syncthingandroid.views.WifiSsidPreference; import com.nutomic.syncthingandroid.views.WifiSsidPreference;
import java.lang.ref.WeakReference;
import java.security.InvalidParameterException; import java.security.InvalidParameterException;
import javax.inject.Inject; import javax.inject.Inject;
@ -460,7 +461,7 @@ public class SettingsActivity extends SyncthingActivity {
if (mUseRoot.isChecked()) { if (mUseRoot.isChecked()) {
// Only check preference after root was granted. // Only check preference after root was granted.
mUseRoot.setChecked(false); mUseRoot.setChecked(false);
new TestRootTask().execute(); new TestRootTask(this).execute();
} else { } else {
new Thread(() -> Util.fixAppDataPermissions(getActivity())).start(); new Thread(() -> Util.fixAppDataPermissions(getActivity())).start();
mRequireRestart = true; mRequireRestart = true;
@ -550,7 +551,13 @@ public class SettingsActivity extends SyncthingActivity {
/** /**
* Enables or disables {@link #mUseRoot} preference depending whether root is available. * Enables or disables {@link #mUseRoot} preference depending whether root is available.
*/ */
private class TestRootTask extends AsyncTask<Void, Void, Boolean> { private static class TestRootTask extends AsyncTask<Void, Void, Boolean> {
private WeakReference<SettingsFragment> refSettingsFragment;
TestRootTask(SettingsFragment context) {
refSettingsFragment = new WeakReference<>(context);
}
@Override @Override
protected Boolean doInBackground(Void... params) { protected Boolean doInBackground(Void... params) {
return Shell.SU.available(); return Shell.SU.available();
@ -558,11 +565,16 @@ public class SettingsActivity extends SyncthingActivity {
@Override @Override
protected void onPostExecute(Boolean haveRoot) { protected void onPostExecute(Boolean haveRoot) {
// Get a reference to the fragment if it is still there.
SettingsFragment settingsFragment = refSettingsFragment.get();
if (settingsFragment == null) {
return;
}
if (haveRoot) { if (haveRoot) {
mRequireRestart = true; settingsFragment.mRequireRestart = true;
mUseRoot.setChecked(true); settingsFragment.mUseRoot.setChecked(true);
} else { } else {
Toast.makeText(getActivity(), R.string.toast_root_denied, Toast.LENGTH_SHORT) Toast.makeText(settingsFragment.getActivity(), R.string.toast_root_denied, Toast.LENGTH_SHORT)
.show(); .show();
} }
} }