Update AsyncTasks to prevent memory leak

This commit is contained in:
topjohnwu 2017-07-19 18:01:22 +08:00
parent 240d14779a
commit 56f57c20a2
12 changed files with 193 additions and 141 deletions

View File

@ -1,17 +1,13 @@
package com.topjohnwu.magisk;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
@ -28,13 +24,12 @@ import android.widget.Toast;
import com.topjohnwu.magisk.asyncs.ParallelTask;
import com.topjohnwu.magisk.components.Fragment;
import com.topjohnwu.magisk.components.SnackbarMaker;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.magisk.utils.Shell;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
@ -67,7 +62,7 @@ public class MagiskLogFragment extends Fragment {
txtLog.setTextIsSelectable(true);
new LogManager(getActivity()).read();
new LogManager().read();
return view;
}
@ -81,7 +76,7 @@ public class MagiskLogFragment extends Fragment {
@Override
public void onResume() {
super.onResume();
new LogManager(getActivity()).read();
new LogManager().read();
}
@Override
@ -100,13 +95,13 @@ public class MagiskLogFragment extends Fragment {
mClickedMenuItem = item;
switch (item.getItemId()) {
case R.id.menu_refresh:
new LogManager(getActivity()).read();
new LogManager().read();
return true;
case R.id.menu_save:
new LogManager(getActivity()).save();
new LogManager().save();
return true;
case R.id.menu_clear:
new LogManager(getActivity()).clear();
new LogManager().clear();
return true;
default:
return true;
@ -129,29 +124,19 @@ public class MagiskLogFragment extends Fragment {
private class LogManager extends ParallelTask<Object, Void, Object> {
int mode;
File targetFile;
LogManager(Activity activity) {
super(activity);
}
private int mode;
private File targetFile;
@SuppressLint("DefaultLocale")
@Override
protected Object doInBackground(Object... params) {
MagiskManager magiskManager = MagiskLogFragment.this.getApplication();
mode = (int) params[0];
switch (mode) {
case 0:
List<String> logList = Utils.readFile(magiskManager.shell, MAGISK_LOG);
if (Utils.isValidShellResponse(logList)) {
StringBuilder llog = new StringBuilder(15 * 10 * 1024);
for (String s : logList) {
llog.append(s).append("\n");
}
return llog.toString();
}
return "";
StringBuildingList logList = new StringBuildingList();
magiskManager.shell.su(logList, "cat " + MAGISK_LOG);
return logList.toString();
case 1:
magiskManager.shell.su_raw("echo > " + MAGISK_LOG);
@ -159,17 +144,6 @@ public class MagiskLogFragment extends Fragment {
return "";
case 2:
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
}
return false;
}
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return false;
}
Calendar now = Calendar.getInstance();
String filename = String.format(
"magisk_%s_%04d%02d%02d_%02d%02d%02d.log", "error",
@ -184,19 +158,14 @@ public class MagiskLogFragment extends Fragment {
return false;
}
List<String> in = Utils.readFile(magiskManager.shell, MAGISK_LOG);
if (Utils.isValidShellResponse(in)) {
try (FileWriter out = new FileWriter(targetFile)) {
for (String line : in)
out.write(line + "\n");
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
try (FileWriter out = new FileWriter(targetFile)) {
FileWritingList fileWritingList = new FileWritingList(out);
magiskManager.shell.su(fileWritingList, "cat " + MAGISK_LOG);
} catch (IOException e) {
e.printStackTrace();
return false;
}
return false;
return true;
}
return null;
}
@ -204,12 +173,10 @@ public class MagiskLogFragment extends Fragment {
@Override
protected void onPostExecute(Object o) {
if (o == null) return;
boolean bool;
String llog;
switch (mode) {
case 0:
case 1:
llog = (String) o;
String llog = (String) o;
progressBar.setVisibility(View.GONE);
if (TextUtils.isEmpty(llog))
txtLog.setText(R.string.log_is_empty);
@ -219,27 +186,64 @@ public class MagiskLogFragment extends Fragment {
hsvLog.post(() -> hsvLog.scrollTo(0, 0));
break;
case 2:
bool = (boolean) o;
boolean bool = (boolean) o;
if (bool) {
Toast.makeText(getActivity(), targetFile.toString(), Toast.LENGTH_LONG).show();
MagiskLogFragment.this.getApplication().toast(targetFile.toString(), Toast.LENGTH_LONG);
} else {
Toast.makeText(getActivity(), getString(R.string.logs_save_failed), Toast.LENGTH_LONG).show();
MagiskLogFragment.this.getApplication().toast(R.string.logs_save_failed, Toast.LENGTH_LONG);
}
break;
}
}
public void read() {
void read() {
exec(0);
}
public void clear() {
void clear() {
exec(1);
}
public void save() {
void save() {
exec(2);
}
}
private static class StringBuildingList extends Shell.AbstractList<String> {
StringBuilder builder;
StringBuildingList() {
builder = new StringBuilder();
}
@Override
public boolean add(String s) {
builder.append(s).append("\n");
return true;
}
@Override
public String toString() {
return builder.toString();
}
}
private static class FileWritingList extends Shell.AbstractList<String> {
private FileWriter writer;
FileWritingList(FileWriter out) {
writer = out;
}
@Override
public boolean add(String s) {
try {
writer.write(s + "\n");
} catch (IOException ignored) {}
return true;
}
}
}

View File

@ -3,6 +3,7 @@ package com.topjohnwu.magisk.asyncs;
import android.content.Context;
import com.topjohnwu.magisk.BuildConfig;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.magisk.utils.WebService;
@ -15,17 +16,19 @@ public class CheckUpdates extends ParallelTask<Void, Void, Void> {
private boolean showNotification = false;
public CheckUpdates(Context context, boolean b) {
this(context);
showNotification = b;
public CheckUpdates(Context context) {
super(context);
}
public CheckUpdates(Context context) {
magiskManager = Utils.getMagiskManager(context);
public CheckUpdates(Context context, boolean b) {
super(context);
showNotification = b;
}
@Override
protected Void doInBackground(Void... voids) {
MagiskManager magiskManager = getMagiskManager();
if (magiskManager == null) return null;
String jsonStr = WebService.request(UPDATE_JSON, WebService.GET);
try {
JSONObject json = new JSONObject(jsonStr);
@ -44,6 +47,8 @@ public class CheckUpdates extends ParallelTask<Void, Void, Void> {
@Override
protected void onPostExecute(Void v) {
MagiskManager magiskManager = getMagiskManager();
if (magiskManager == null) return;
if (showNotification && magiskManager.updateNotification) {
if (BuildConfig.VERSION_CODE < magiskManager.remoteManagerVersionCode) {
Utils.showManagerUpdate(magiskManager);

View File

@ -1,6 +1,6 @@
package com.topjohnwu.magisk.asyncs;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
@ -26,44 +26,22 @@ public class FlashZip extends ParallelTask<Void, String, Integer> {
private String mFilename;
private AdaptiveList<String> mList;
public FlashZip(Activity context, Uri uri, AdaptiveList<String> list) {
public FlashZip(Context context, Uri uri, AdaptiveList<String> list) {
super(context);
mUri = uri;
mList = list;
mCachedFile = new File(magiskManager.getCacheDir(), "install.zip");
mScriptFile = new File(magiskManager.getCacheDir(), "/META-INF/com/google/android/update-binary");
mCachedFile = new File(context.getCacheDir(), "install.zip");
mScriptFile = new File(context.getCacheDir(), "/META-INF/com/google/android/update-binary");
mCheckFile = new File(mScriptFile.getParent(), "updater-script");
// Try to get the filename ourselves
mFilename = Utils.getNameFromUri(magiskManager, mUri);
}
private void copyToCache() throws Exception {
mList.add(magiskManager.getString(R.string.copying_msg));
mCachedFile.delete();
try (
InputStream in = magiskManager.getContentResolver().openInputStream(mUri);
OutputStream outputStream = new FileOutputStream(mCachedFile)
) {
byte buffer[] = new byte[1024];
int length;
if (in == null) throw new FileNotFoundException();
while ((length = in.read(buffer)) > 0)
outputStream.write(buffer, 0, length);
} catch (FileNotFoundException e) {
mList.add("! Invalid Uri");
throw e;
} catch (IOException e) {
mList.add("! Cannot copy to cache");
throw e;
}
mFilename = Utils.getNameFromUri(context, mUri);
}
private boolean unzipAndCheck() throws Exception {
ZipUtils.unzip(mCachedFile, mCachedFile.getParentFile(), "META-INF/com/google/android");
List<String> ret = Utils.readFile(magiskManager.shell, mCheckFile.getPath());
List<String> ret = Utils.readFile(getShell(), mCheckFile.getPath());
return Utils.isValidShellResponse(ret) && ret.get(0).contains("#MAGISK");
}
@ -80,8 +58,28 @@ public class FlashZip extends ParallelTask<Void, String, Integer> {
@Override
protected Integer doInBackground(Void... voids) {
MagiskManager magiskManager = getMagiskManager();
if (magiskManager == null) return -1;
try {
copyToCache();
mList.add(magiskManager.getString(R.string.copying_msg));
mCachedFile.delete();
try (
InputStream in = magiskManager.getContentResolver().openInputStream(mUri);
OutputStream outputStream = new FileOutputStream(mCachedFile)
) {
if (in == null) throw new FileNotFoundException();
byte buffer[] = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0)
outputStream.write(buffer, 0, length);
} catch (FileNotFoundException e) {
mList.add("! Invalid Uri");
throw e;
} catch (IOException e) {
mList.add("! Cannot copy to cache");
throw e;
}
if (!unzipAndCheck()) return 0;
mList.add(magiskManager.getString(R.string.zip_install_progress_msg, mFilename));
magiskManager.shell.su(mList,
@ -99,6 +97,8 @@ public class FlashZip extends ParallelTask<Void, String, Integer> {
// -1 = error, manual install; 0 = invalid zip; 1 = success
@Override
protected void onPostExecute(Integer result) {
MagiskManager magiskManager = getMagiskManager();
if (magiskManager == null) return;
magiskManager.shell.su_raw(
"rm -rf " + mCachedFile.getParent() + "/*",
"rm -rf " + MagiskManager.TMP_FOLDER_PATH
@ -106,19 +106,16 @@ public class FlashZip extends ParallelTask<Void, String, Integer> {
switch (result) {
case -1:
mList.add(magiskManager.getString(R.string.install_error));
Utils.showUriSnack(activity, mUri);
break;
Utils.showUriSnack(getActivity(), mUri);
return;
case 0:
mList.add(magiskManager.getString(R.string.invalid_zip));
break;
return;
case 1:
onSuccess();
// Success
break;
}
new LoadModules(magiskManager).exec();
super.onPostExecute(result);
}
protected void onSuccess() {
new LoadModules(activity).exec();
}
}

View File

@ -1,9 +1,10 @@
package com.topjohnwu.magisk.asyncs;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.adapters.ApplicationAdapter;
import java.util.Collections;
@ -12,12 +13,14 @@ import java.util.List;
public class LoadApps extends ParallelTask<Void, Void, Void> {
public LoadApps(Activity context) {
public LoadApps(Context context) {
super(context);
}
@Override
protected Void doInBackground(Void... voids) {
MagiskManager magiskManager = getMagiskManager();
if (magiskManager == null) return null;
PackageManager pm = magiskManager.getPackageManager();
List<ApplicationInfo> list = pm.getInstalledApplications(0);
for (Iterator<ApplicationInfo> i = list.iterator(); i.hasNext(); ) {
@ -34,6 +37,8 @@ public class LoadApps extends ParallelTask<Void, Void, Void> {
@Override
protected void onPostExecute(Void v) {
new MagiskHide(activity).list();
MagiskManager magiskManager = getMagiskManager();
if (magiskManager == null) return;
new MagiskHide(magiskManager).list();
}
}

View File

@ -1,6 +1,6 @@
package com.topjohnwu.magisk.asyncs;
import android.app.Activity;
import android.content.Context;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.module.BaseModule;
@ -11,12 +11,14 @@ import com.topjohnwu.magisk.utils.ValueSortedMap;
public class LoadModules extends ParallelTask<Void, Void, Void> {
public LoadModules(Activity context) {
public LoadModules(Context context) {
super(context);
}
@Override
protected Void doInBackground(Void... voids) {
MagiskManager magiskManager = getMagiskManager();
if (magiskManager == null) return null;
Logger.dev("LoadModules: Loading modules");
magiskManager.moduleMap = new ValueSortedMap<>();
@ -35,6 +37,8 @@ public class LoadModules extends ParallelTask<Void, Void, Void> {
@Override
protected void onPostExecute(Void v) {
MagiskManager magiskManager = getMagiskManager();
if (magiskManager == null) return;
magiskManager.moduleLoadDone.trigger();
super.onPostExecute(v);
}

View File

@ -1,9 +1,10 @@
package com.topjohnwu.magisk.asyncs;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.database.RepoDatabaseHelper;
import com.topjohnwu.magisk.module.BaseModule;
import com.topjohnwu.magisk.module.Repo;
@ -41,11 +42,11 @@ public class LoadRepos extends ParallelTask<Void, Void, Void> {
private RepoDatabaseHelper repoDB;
private SharedPreferences prefs;
public LoadRepos(Activity context) {
public LoadRepos(Context context) {
super(context);
prefs = magiskManager.prefs;
prefs = getMagiskManager().prefs;
String prefsPath = context.getApplicationInfo().dataDir + "/shared_prefs";
repoDB = new RepoDatabaseHelper(magiskManager);
repoDB = new RepoDatabaseHelper(context);
// Legacy data cleanup
File old = new File(prefsPath, "RepoMap.xml");
if (old.exists() || !prefs.getString("repomap", "empty").equals("empty")) {
@ -154,6 +155,8 @@ public class LoadRepos extends ParallelTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
MagiskManager magiskManager = getMagiskManager();
if (magiskManager == null) return null;
Logger.dev("LoadRepos: Loading repos");
cached = repoDB.getRepoMap(false);
@ -183,6 +186,8 @@ public class LoadRepos extends ParallelTask<Void, Void, Void> {
@Override
protected void onPostExecute(Void v) {
MagiskManager magiskManager = getMagiskManager();
if (magiskManager == null) return;
magiskManager.repoLoadDone.trigger();
super.onPostExecute(v);
}

View File

@ -1,6 +1,8 @@
package com.topjohnwu.magisk.asyncs;
import android.app.Activity;
import android.content.Context;
import com.topjohnwu.magisk.MagiskManager;
import java.util.List;
@ -8,12 +10,14 @@ public class MagiskHide extends ParallelTask<Object, Void, Void> {
private boolean isList = false;
public MagiskHide(Activity context) {
public MagiskHide(Context context) {
super(context);
}
@Override
protected Void doInBackground(Object... params) {
MagiskManager magiskManager = getMagiskManager();
if (magiskManager == null) return null;
String command = (String) params[0];
List<String> ret = magiskManager.shell.su("magiskhide --" + command);
if (isList) {
@ -24,6 +28,8 @@ public class MagiskHide extends ParallelTask<Object, Void, Void> {
@Override
protected void onPostExecute(Void v) {
MagiskManager magiskManager = getMagiskManager();
if (magiskManager == null) return;
if (isList) {
magiskManager.magiskHideDone.trigger();
}
@ -48,7 +54,7 @@ public class MagiskHide extends ParallelTask<Object, Void, Void> {
public void list() {
isList = true;
if (magiskManager == null) return;
if (getMagiskManager() == null) return;
exec("ls");
}

View File

@ -1,23 +1,44 @@
package com.topjohnwu.magisk.asyncs;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.utils.Shell;
import com.topjohnwu.magisk.utils.Utils;
import java.lang.ref.WeakReference;
public abstract class ParallelTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {
protected Activity activity;
protected MagiskManager magiskManager;
private WeakReference<Activity> weakActivity;
private WeakReference<MagiskManager> weakMagiskManager;
private Runnable callback = null;
public ParallelTask() {}
public ParallelTask(Context context) {
weakMagiskManager = new WeakReference<>(Utils.getMagiskManager(context));
}
public ParallelTask(Activity context) {
activity = context;
magiskManager = Utils.getMagiskManager(context);
this((Context) context);
weakActivity = new WeakReference<>(context);
}
protected Activity getActivity() {
return weakActivity.get();
}
protected MagiskManager getMagiskManager() {
return weakMagiskManager.get();
}
protected Shell getShell() {
MagiskManager magiskManager = getMagiskManager();
return magiskManager == null ? null : getMagiskManager().shell;
}
@SuppressWarnings("unchecked")

View File

@ -32,6 +32,8 @@ public class ProcessRepoZip extends ParallelTask<Void, Void, Boolean> {
@Override
protected void onPreExecute() {
Activity activity = getActivity();
if (activity == null) return;
progressDialog = ProgressDialog.show(activity,
activity.getString(R.string.zip_process_title),
activity.getString(R.string.zip_process_msg));
@ -39,6 +41,8 @@ public class ProcessRepoZip extends ParallelTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
Activity activity = getActivity();
if (activity == null) return null;
try {
// Create temp file
@ -84,6 +88,8 @@ public class ProcessRepoZip extends ParallelTask<Void, Void, Boolean> {
@Override
protected void onPostExecute(Boolean result) {
Activity activity = getActivity();
if (activity == null) return;
progressDialog.dismiss();
if (result) {
if (Shell.rootAccess() && mInstall) {
@ -92,7 +98,7 @@ public class ProcessRepoZip extends ParallelTask<Void, Void, Boolean> {
Utils.showUriSnack(activity, mUri);
}
} else {
magiskManager.toast(R.string.process_error, Toast.LENGTH_LONG);
Utils.getMagiskManager(activity).toast(R.string.process_error, Toast.LENGTH_LONG);
}
}
}

View File

@ -8,6 +8,7 @@ import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
@ -116,7 +117,7 @@ public class Shell {
}
}
public void sh(List<String> output, String... commands) {
public void sh(Collection<String> output, String... commands) {
if (!isValid) return;
try {
shellProcess.exitValue();
@ -144,8 +145,24 @@ public class Shell {
sh_raw(commands);
}
public void su(List<String> output, String... commands) {
public void su(Collection<String> output, String... commands) {
if (!rootAccess()) return;
sh(output, commands);
}
public static abstract class AbstractList<E> extends java.util.AbstractList<E> {
@Override
public abstract boolean add(E e);
@Override
public E get(int i) {
return null;
}
@Override
public int size() {
return 0;
}
}
}

View File

@ -6,7 +6,7 @@ import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Collection;
/**
* Modified by topjohnwu, based on Chainfire's libsuperuser
@ -15,8 +15,7 @@ import java.util.List;
public class StreamGobbler extends Thread {
private BufferedReader reader = null;
private List<String> writer = null;
private boolean isRoot = false;
private Collection<String> writer = null;
/**
* <p>StreamGobbler constructor</p>
@ -28,7 +27,7 @@ public class StreamGobbler extends Thread {
* @param inputStream InputStream to read from
* @param outputList {@literal List<String>} to write to, or null
*/
public StreamGobbler(InputStream inputStream, List<String> outputList) {
public StreamGobbler(InputStream inputStream, Collection<String> outputList) {
try {
while (inputStream.available() != 0) {
inputStream.skip(inputStream.available());

View File

@ -108,23 +108,6 @@ public class Utils {
.replace("#", "").replace("@", "").replace("*", "");
}
public static String detectBootImage(Shell shell) {
String[] commands = {
"for PARTITION in kern-a KERN-A android_boot ANDROID_BOOT kernel KERNEL boot BOOT lnx LNX; do",
"BOOTIMAGE=`readlink /dev/block/by-name/$PARTITION || " +
"readlink /dev/block/platform/*/by-name/$PARTITION || " +
"readlink /dev/block/platform/*/*/by-name/$PARTITION`",
"if [ ! -z \"$BOOTIMAGE\" ]; then break; fi",
"done",
"echo \"$BOOTIMAGE\""
};
List<String> ret = shell.su(commands);
if (isValidShellResponse(ret)) {
return ret.get(0);
}
return null;
}
public static boolean lowercaseContains(CharSequence string, CharSequence nonNullLowercaseSearch) {
return !TextUtils.isEmpty(string) && string.toString().toLowerCase().contains(nonNullLowercaseSearch);
}