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

View File

@ -1,6 +1,6 @@
package com.topjohnwu.magisk.asyncs; package com.topjohnwu.magisk.asyncs;
import android.app.Activity; import android.content.Context;
import android.net.Uri; import android.net.Uri;
import android.text.TextUtils; import android.text.TextUtils;
@ -26,44 +26,22 @@ public class FlashZip extends ParallelTask<Void, String, Integer> {
private String mFilename; private String mFilename;
private AdaptiveList<String> mList; private AdaptiveList<String> mList;
public FlashZip(Activity context, Uri uri, AdaptiveList<String> list) { public FlashZip(Context context, Uri uri, AdaptiveList<String> list) {
super(context); super(context);
mUri = uri; mUri = uri;
mList = list; mList = list;
mCachedFile = new File(magiskManager.getCacheDir(), "install.zip"); mCachedFile = new File(context.getCacheDir(), "install.zip");
mScriptFile = new File(magiskManager.getCacheDir(), "/META-INF/com/google/android/update-binary"); mScriptFile = new File(context.getCacheDir(), "/META-INF/com/google/android/update-binary");
mCheckFile = new File(mScriptFile.getParent(), "updater-script"); mCheckFile = new File(mScriptFile.getParent(), "updater-script");
// Try to get the filename ourselves // Try to get the filename ourselves
mFilename = Utils.getNameFromUri(magiskManager, mUri); mFilename = Utils.getNameFromUri(context, 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;
}
} }
private boolean unzipAndCheck() throws Exception { private boolean unzipAndCheck() throws Exception {
ZipUtils.unzip(mCachedFile, mCachedFile.getParentFile(), "META-INF/com/google/android"); 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"); return Utils.isValidShellResponse(ret) && ret.get(0).contains("#MAGISK");
} }
@ -80,8 +58,28 @@ public class FlashZip extends ParallelTask<Void, String, Integer> {
@Override @Override
protected Integer doInBackground(Void... voids) { protected Integer doInBackground(Void... voids) {
MagiskManager magiskManager = getMagiskManager();
if (magiskManager == null) return -1;
try { 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; if (!unzipAndCheck()) return 0;
mList.add(magiskManager.getString(R.string.zip_install_progress_msg, mFilename)); mList.add(magiskManager.getString(R.string.zip_install_progress_msg, mFilename));
magiskManager.shell.su(mList, 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 // -1 = error, manual install; 0 = invalid zip; 1 = success
@Override @Override
protected void onPostExecute(Integer result) { protected void onPostExecute(Integer result) {
MagiskManager magiskManager = getMagiskManager();
if (magiskManager == null) return;
magiskManager.shell.su_raw( magiskManager.shell.su_raw(
"rm -rf " + mCachedFile.getParent() + "/*", "rm -rf " + mCachedFile.getParent() + "/*",
"rm -rf " + MagiskManager.TMP_FOLDER_PATH "rm -rf " + MagiskManager.TMP_FOLDER_PATH
@ -106,19 +106,16 @@ public class FlashZip extends ParallelTask<Void, String, Integer> {
switch (result) { switch (result) {
case -1: case -1:
mList.add(magiskManager.getString(R.string.install_error)); mList.add(magiskManager.getString(R.string.install_error));
Utils.showUriSnack(activity, mUri); Utils.showUriSnack(getActivity(), mUri);
break; return;
case 0: case 0:
mList.add(magiskManager.getString(R.string.invalid_zip)); mList.add(magiskManager.getString(R.string.invalid_zip));
break; return;
case 1: case 1:
onSuccess(); // Success
break; break;
} }
new LoadModules(magiskManager).exec();
super.onPostExecute(result); super.onPostExecute(result);
} }
protected void onSuccess() {
new LoadModules(activity).exec();
}
} }

View File

@ -1,9 +1,10 @@
package com.topjohnwu.magisk.asyncs; package com.topjohnwu.magisk.asyncs;
import android.app.Activity; import android.content.Context;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.adapters.ApplicationAdapter; import com.topjohnwu.magisk.adapters.ApplicationAdapter;
import java.util.Collections; import java.util.Collections;
@ -12,12 +13,14 @@ import java.util.List;
public class LoadApps extends ParallelTask<Void, Void, Void> { public class LoadApps extends ParallelTask<Void, Void, Void> {
public LoadApps(Activity context) { public LoadApps(Context context) {
super(context); super(context);
} }
@Override @Override
protected Void doInBackground(Void... voids) { protected Void doInBackground(Void... voids) {
MagiskManager magiskManager = getMagiskManager();
if (magiskManager == null) return null;
PackageManager pm = magiskManager.getPackageManager(); PackageManager pm = magiskManager.getPackageManager();
List<ApplicationInfo> list = pm.getInstalledApplications(0); List<ApplicationInfo> list = pm.getInstalledApplications(0);
for (Iterator<ApplicationInfo> i = list.iterator(); i.hasNext(); ) { for (Iterator<ApplicationInfo> i = list.iterator(); i.hasNext(); ) {
@ -34,6 +37,8 @@ public class LoadApps extends ParallelTask<Void, Void, Void> {
@Override @Override
protected void onPostExecute(Void v) { 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; package com.topjohnwu.magisk.asyncs;
import android.app.Activity; import android.content.Context;
import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.module.BaseModule; 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 class LoadModules extends ParallelTask<Void, Void, Void> {
public LoadModules(Activity context) { public LoadModules(Context context) {
super(context); super(context);
} }
@Override @Override
protected Void doInBackground(Void... voids) { protected Void doInBackground(Void... voids) {
MagiskManager magiskManager = getMagiskManager();
if (magiskManager == null) return null;
Logger.dev("LoadModules: Loading modules"); Logger.dev("LoadModules: Loading modules");
magiskManager.moduleMap = new ValueSortedMap<>(); magiskManager.moduleMap = new ValueSortedMap<>();
@ -35,6 +37,8 @@ public class LoadModules extends ParallelTask<Void, Void, Void> {
@Override @Override
protected void onPostExecute(Void v) { protected void onPostExecute(Void v) {
MagiskManager magiskManager = getMagiskManager();
if (magiskManager == null) return;
magiskManager.moduleLoadDone.trigger(); magiskManager.moduleLoadDone.trigger();
super.onPostExecute(v); super.onPostExecute(v);
} }

View File

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

View File

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

View File

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

View File

@ -32,6 +32,8 @@ public class ProcessRepoZip extends ParallelTask<Void, Void, Boolean> {
@Override @Override
protected void onPreExecute() { protected void onPreExecute() {
Activity activity = getActivity();
if (activity == null) return;
progressDialog = ProgressDialog.show(activity, progressDialog = ProgressDialog.show(activity,
activity.getString(R.string.zip_process_title), activity.getString(R.string.zip_process_title),
activity.getString(R.string.zip_process_msg)); activity.getString(R.string.zip_process_msg));
@ -39,6 +41,8 @@ public class ProcessRepoZip extends ParallelTask<Void, Void, Boolean> {
@Override @Override
protected Boolean doInBackground(Void... params) { protected Boolean doInBackground(Void... params) {
Activity activity = getActivity();
if (activity == null) return null;
try { try {
// Create temp file // Create temp file
@ -84,6 +88,8 @@ public class ProcessRepoZip extends ParallelTask<Void, Void, Boolean> {
@Override @Override
protected void onPostExecute(Boolean result) { protected void onPostExecute(Boolean result) {
Activity activity = getActivity();
if (activity == null) return;
progressDialog.dismiss(); progressDialog.dismiss();
if (result) { if (result) {
if (Shell.rootAccess() && mInstall) { if (Shell.rootAccess() && mInstall) {
@ -92,7 +98,7 @@ public class ProcessRepoZip extends ParallelTask<Void, Void, Boolean> {
Utils.showUriSnack(activity, mUri); Utils.showUriSnack(activity, mUri);
} }
} else { } 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.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; 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; if (!isValid) return;
try { try {
shellProcess.exitValue(); shellProcess.exitValue();
@ -144,8 +145,24 @@ public class Shell {
sh_raw(commands); sh_raw(commands);
} }
public void su(List<String> output, String... commands) { public void su(Collection<String> output, String... commands) {
if (!rootAccess()) return; if (!rootAccess()) return;
sh(output, commands); 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.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.List; import java.util.Collection;
/** /**
* Modified by topjohnwu, based on Chainfire's libsuperuser * Modified by topjohnwu, based on Chainfire's libsuperuser
@ -15,8 +15,7 @@ import java.util.List;
public class StreamGobbler extends Thread { public class StreamGobbler extends Thread {
private BufferedReader reader = null; private BufferedReader reader = null;
private List<String> writer = null; private Collection<String> writer = null;
private boolean isRoot = false;
/** /**
* <p>StreamGobbler constructor</p> * <p>StreamGobbler constructor</p>
@ -28,7 +27,7 @@ public class StreamGobbler extends Thread {
* @param inputStream InputStream to read from * @param inputStream InputStream to read from
* @param outputList {@literal List<String>} to write to, or null * @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 { try {
while (inputStream.available() != 0) { while (inputStream.available() != 0) {
inputStream.skip(inputStream.available()); inputStream.skip(inputStream.available());

View File

@ -108,23 +108,6 @@ public class Utils {
.replace("#", "").replace("@", "").replace("*", ""); .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) { public static boolean lowercaseContains(CharSequence string, CharSequence nonNullLowercaseSearch) {
return !TextUtils.isEmpty(string) && string.toString().toLowerCase().contains(nonNullLowercaseSearch); return !TextUtils.isEmpty(string) && string.toString().toLowerCase().contains(nonNullLowercaseSearch);
} }