329 lines
12 KiB
Java
Raw Normal View History

2016-09-21 01:08:05 +08:00
package com.topjohnwu.magisk.utils;
import android.app.ProgressDialog;
import android.content.Context;
2017-01-07 00:29:53 +08:00
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
2016-09-28 00:33:01 +08:00
import android.database.Cursor;
2016-09-21 01:08:05 +08:00
import android.net.Uri;
import android.os.AsyncTask;
2016-09-28 00:33:01 +08:00
import android.provider.OpenableColumns;
2016-09-21 01:08:05 +08:00
import android.widget.Toast;
2017-01-25 04:27:05 +08:00
import com.topjohnwu.magisk.Global;
2016-09-21 01:08:05 +08:00
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.adapters.ApplicationAdapter;
2017-01-25 04:27:05 +08:00
import com.topjohnwu.magisk.module.ModuleHelper;
2016-09-21 01:08:05 +08:00
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
2017-01-07 00:29:53 +08:00
import java.util.Collections;
import java.util.Iterator;
2016-09-21 01:08:05 +08:00
import java.util.List;
public class Async {
2016-11-07 21:06:18 +08:00
public abstract static class RootTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {
@SafeVarargs
public final void exec(Params... params) {
2017-01-10 22:47:58 +08:00
if (!Shell.rootAccess()) return;
2016-11-07 21:06:18 +08:00
executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, params);
}
}
public abstract static class NormalTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {
@SafeVarargs
public final void exec(Params... params) {
executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
}
}
2016-09-29 01:42:25 +08:00
public static final String UPDATE_JSON = "https://raw.githubusercontent.com/topjohnwu/MagiskManager/updates/magisk_update.json";
2016-11-07 21:06:18 +08:00
public static final String MAGISK_HIDE_PATH = "/magisk/.core/magiskhide/";
2016-11-29 13:24:48 +08:00
public static final String TMP_FOLDER_PATH = "/dev/tmp";
2016-11-07 21:06:18 +08:00
public static class CheckUpdates extends NormalTask<Void, Void, Void> {
2016-09-21 01:08:05 +08:00
@Override
protected Void doInBackground(Void... voids) {
String jsonStr = WebService.request(UPDATE_JSON, WebService.GET);
2016-09-21 01:08:05 +08:00
try {
2016-09-27 15:51:38 +08:00
JSONObject json = new JSONObject(jsonStr);
2016-09-21 01:08:05 +08:00
JSONObject magisk = json.getJSONObject("magisk");
2017-01-25 04:27:05 +08:00
Global.Info.remoteMagiskVersion = magisk.getDouble("versionCode");
Global.Info.magiskLink = magisk.getString("link");
Global.Info.releaseNoteLink = magisk.getString("note");
2016-12-25 15:11:59 +08:00
} catch (JSONException ignored) {}
2016-09-21 01:08:05 +08:00
return null;
}
@Override
2016-09-28 14:50:26 +08:00
protected void onPostExecute(Void v) {
2017-01-25 04:27:05 +08:00
Global.Events.updateCheckDone.trigger();
2016-09-21 01:08:05 +08:00
}
}
2016-12-25 15:11:59 +08:00
public static void checkSafetyNet(Context context) {
new SafetyNetHelper(context) {
@Override
public void handleResults(int i) {
2017-01-25 04:27:05 +08:00
Global.Info.SNCheckResult = i;
Global.Events.safetyNetDone.trigger();
2016-12-25 15:11:59 +08:00
}
}.requestTest();
}
2016-09-21 01:08:05 +08:00
2016-12-25 15:11:59 +08:00
public static class LoadModules extends RootTask<Void, Void, Void> {
2016-09-21 01:08:05 +08:00
@Override
protected Void doInBackground(Void... voids) {
2016-09-29 01:42:25 +08:00
ModuleHelper.createModuleMap();
2016-09-21 01:08:05 +08:00
return null;
}
2016-09-27 22:57:20 +08:00
@Override
protected void onPostExecute(Void v) {
2017-01-25 04:27:05 +08:00
Global.Events.moduleLoadDone.trigger();
2016-09-27 22:57:20 +08:00
}
2016-09-21 01:08:05 +08:00
}
2016-11-07 21:06:18 +08:00
public static class LoadRepos extends NormalTask<Void, Void, Void> {
2016-09-21 01:08:05 +08:00
private Context mContext;
2016-09-21 11:29:43 +08:00
public LoadRepos(Context context) {
2016-09-21 01:08:05 +08:00
mContext = context;
}
@Override
protected Void doInBackground(Void... voids) {
2016-09-29 01:42:25 +08:00
ModuleHelper.createRepoMap(mContext);
2016-09-21 01:08:05 +08:00
return null;
}
2016-09-27 22:57:20 +08:00
@Override
protected void onPostExecute(Void v) {
2017-01-25 04:27:05 +08:00
Global.Events.repoLoadDone.trigger();
2016-09-27 22:57:20 +08:00
}
2016-09-21 01:08:05 +08:00
}
2017-01-30 20:04:49 +08:00
public static class LoadApps extends RootTask<Void, Void, Void> {
2017-01-07 00:29:53 +08:00
private PackageManager pm;
public LoadApps(PackageManager packageManager) {
pm = packageManager;
}
@Override
2017-01-30 20:04:49 +08:00
protected Void doInBackground(Void... voids) {
Global.Data.appList = pm.getInstalledApplications(0);
for (Iterator<ApplicationInfo> i = Global.Data.appList.iterator(); i.hasNext(); ) {
2017-01-07 00:29:53 +08:00
ApplicationInfo info = i.next();
if (ApplicationAdapter.BLACKLIST.contains(info.packageName) || !info.enabled)
2017-01-07 00:29:53 +08:00
i.remove();
}
2017-01-30 20:04:49 +08:00
Collections.sort(Global.Data.appList, (a, b) -> a.loadLabel(pm).toString().toLowerCase()
2017-01-07 00:29:53 +08:00
.compareTo(b.loadLabel(pm).toString().toLowerCase()));
2017-01-30 20:04:49 +08:00
Global.Data.magiskHideList = Shell.su(Async.MAGISK_HIDE_PATH + "list");
return null;
2017-01-07 00:29:53 +08:00
}
@Override
2017-01-30 20:04:49 +08:00
protected void onPostExecute(Void v) {
Global.Events.packageLoadDone.trigger();
2017-01-07 00:29:53 +08:00
}
}
2016-11-23 18:33:16 +08:00
public static class FlashZIP extends RootTask<Void, String, Integer> {
2016-09-21 01:08:05 +08:00
2016-09-28 14:50:26 +08:00
protected Uri mUri;
2016-11-23 18:33:16 +08:00
protected File mCachedFile;
private String mFilename;
2016-11-23 18:33:16 +08:00
protected ProgressDialog progress;
2016-09-21 01:08:05 +08:00
private Context mContext;
public FlashZIP(Context context, Uri uri, String filename) {
2016-09-21 01:08:05 +08:00
mContext = context;
2016-09-27 15:51:38 +08:00
mUri = uri;
mFilename = filename;
2016-09-21 01:08:05 +08:00
}
2016-09-27 15:51:38 +08:00
public FlashZIP(Context context, Uri uri) {
2016-09-21 01:08:05 +08:00
mContext = context;
2016-09-27 15:51:38 +08:00
mUri = uri;
2016-11-23 22:38:15 +08:00
// Try to get the filename ourselves
2016-09-28 00:33:01 +08:00
Cursor c = mContext.getContentResolver().query(uri, null, null, null, null);
2016-09-29 23:24:31 +08:00
if (c != null) {
int nameIndex = c.getColumnIndex(OpenableColumns.DISPLAY_NAME);
c.moveToFirst();
if (nameIndex != -1) {
mFilename = c.getString(nameIndex);
2016-09-29 23:24:31 +08:00
}
c.close();
}
if (mFilename == null) {
2016-09-28 14:50:26 +08:00
int idx = uri.getPath().lastIndexOf('/');
mFilename = uri.getPath().substring(idx + 1);
2016-09-28 14:50:26 +08:00
}
2016-09-21 01:08:05 +08:00
}
protected void preProcessing() throws Throwable {}
2016-12-27 04:38:16 +08:00
protected void copyToCache() throws Throwable {
publishProgress(mContext.getString(R.string.copying_msg));
2017-01-12 02:02:52 +01:00
mCachedFile = new File(mContext.getCacheDir().getAbsolutePath() + "/install.zip");
if (mCachedFile.exists() && !mCachedFile.delete()) {
2017-01-25 16:45:55 +08:00
Logger.error("FlashZip: Error while deleting already existing file");
2017-01-12 02:02:52 +01:00
throw new IOException();
}
try (
InputStream in = mContext.getContentResolver().openInputStream(mUri);
OutputStream outputStream = new FileOutputStream(mCachedFile)
) {
2016-11-23 22:38:15 +08:00
byte buffer[] = new byte[1024];
int length;
2017-01-25 16:45:55 +08:00
if (in == null) throw new FileNotFoundException();
2016-11-23 22:38:15 +08:00
while ((length = in.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
Logger.dev("FlashZip: File created successfully - " + mCachedFile.getPath());
2016-09-28 14:50:26 +08:00
} catch (FileNotFoundException e) {
2017-01-25 16:45:55 +08:00
Logger.error("FlashZip: Invalid Uri");
2016-09-28 14:50:26 +08:00
throw e;
} catch (IOException e) {
2017-01-25 16:45:55 +08:00
Logger.error("FlashZip: Error in creating file");
2016-09-28 14:50:26 +08:00
throw e;
}
2016-09-21 01:08:05 +08:00
}
2016-11-29 13:24:48 +08:00
protected boolean unzipAndCheck() {
ZipUtils.unzip(mCachedFile, mCachedFile.getParentFile(), "META-INF/com/google/android");
List<String> ret;
ret = Utils.readFile(mCachedFile.getParent() + "/META-INF/com/google/android/updater-script");
return Utils.isValidShellResponse(ret) && ret.get(0).contains("#MAGISK");
2016-11-29 13:24:48 +08:00
}
2016-09-21 01:08:05 +08:00
@Override
protected void onPreExecute() {
2016-11-23 22:38:15 +08:00
progress = new ProgressDialog(mContext);
2016-11-23 18:33:16 +08:00
progress.setTitle(R.string.zip_install_progress_title);
progress.show();
}
@Override
protected void onProgressUpdate(String... values) {
progress.setMessage(values[0]);
2016-09-21 01:08:05 +08:00
}
@Override
2016-09-28 14:50:26 +08:00
protected Integer doInBackground(Void... voids) {
Logger.dev("FlashZip Running... " + mFilename);
2016-11-29 13:38:32 +08:00
List<String> ret;
try {
preProcessing();
copyToCache();
} catch (Throwable e) {
e.printStackTrace();
return -1;
}
2016-11-29 13:24:48 +08:00
if (!unzipAndCheck()) return 0;
2017-01-10 22:47:58 +08:00
publishProgress(mContext.getString(R.string.zip_install_progress_msg, mFilename));
ret = Shell.su(
"BOOTMODE=true sh " + mCachedFile.getParent() +
"/META-INF/com/google/android/update-binary dummy 1 " + mCachedFile.getPath(),
"if [ $? -eq 0 ]; then echo true; else echo false; fi"
);
2017-01-26 13:46:54 +08:00
if (!Utils.isValidShellResponse(ret)) return -1;
2017-01-10 22:47:58 +08:00
Logger.dev("FlashZip: Console log:");
for (String line : ret) {
Logger.dev(line);
2016-09-27 15:51:38 +08:00
}
2017-01-10 22:47:58 +08:00
Shell.su(
"rm -rf " + mCachedFile.getParent() + "/*",
"rm -rf " + TMP_FOLDER_PATH
);
2016-12-27 04:38:16 +08:00
if (Boolean.parseBoolean(ret.get(ret.size() - 1))) {
2016-09-28 14:50:26 +08:00
return 1;
}
return -1;
2016-09-21 01:08:05 +08:00
}
2016-11-29 13:38:32 +08:00
// -1 = error, manual install; 0 = invalid zip; 1 = success
2016-09-21 01:08:05 +08:00
@Override
2016-09-28 14:50:26 +08:00
protected void onPostExecute(Integer result) {
2016-09-21 01:08:05 +08:00
super.onPostExecute(result);
progress.dismiss();
2016-09-28 14:50:26 +08:00
switch (result) {
case -1:
2016-11-29 13:24:48 +08:00
Toast.makeText(mContext, mContext.getString(R.string.install_error), Toast.LENGTH_LONG).show();
Toast.makeText(mContext, mContext.getString(R.string.manual_install_1, mUri.getPath()), Toast.LENGTH_LONG).show();
Toast.makeText(mContext, mContext.getString(R.string.manual_install_2), Toast.LENGTH_LONG).show();
2016-09-28 14:50:26 +08:00
break;
case 0:
Toast.makeText(mContext, mContext.getString(R.string.invalid_zip), Toast.LENGTH_LONG).show();
break;
case 1:
2016-12-26 17:15:46 +08:00
onSuccess();
2016-09-28 14:50:26 +08:00
break;
2016-09-21 01:08:05 +08:00
}
}
2016-12-26 17:15:46 +08:00
protected void onSuccess() {
2017-01-25 04:27:05 +08:00
Global.Events.updateCheckDone.trigger();
2016-12-25 15:11:59 +08:00
new LoadModules().exec();
2016-09-29 23:24:31 +08:00
2017-01-10 22:30:05 +08:00
Utils.getAlertDialogBuilder(mContext)
2016-09-21 01:08:05 +08:00
.setTitle(R.string.reboot_title)
.setMessage(R.string.reboot_msg)
2017-01-26 13:46:54 +08:00
.setPositiveButton(R.string.reboot, (dialogInterface, i) -> Shell.sh("su -c reboot"))
2016-09-21 01:08:05 +08:00
.setNegativeButton(R.string.no_thanks, null)
.show();
}
}
2016-09-29 23:24:31 +08:00
2016-11-07 21:06:18 +08:00
public static class MagiskHide extends RootTask<Object, Void, Void> {
@Override
protected Void doInBackground(Object... params) {
boolean add = (boolean) params[0];
String packageName = (String) params[1];
Shell.su(MAGISK_HIDE_PATH + (add ? "add " : "rm ") + packageName);
return null;
}
public void add(CharSequence packageName) {
exec(true, packageName);
}
public void rm(CharSequence packageName) {
exec(false, packageName);
}
}
2016-12-25 22:36:51 +08:00
public static class GetBootBlocks extends RootTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
2016-12-27 04:38:16 +08:00
if (Shell.rootAccess()) {
2017-01-25 04:27:05 +08:00
Global.Data.blockList = Shell.su("ls /dev/block | grep mmc");
2017-01-26 13:46:54 +08:00
if (Global.Info.bootBlock == null)
2017-01-25 04:27:05 +08:00
Global.Info.bootBlock = Utils.detectBootImage();
2016-12-25 22:36:51 +08:00
}
return null;
}
@Override
2017-01-25 16:45:55 +08:00
protected void onPostExecute(Void v) {
2017-01-25 04:27:05 +08:00
Global.Events.blockDetectionDone.trigger();
2016-12-25 22:36:51 +08:00
}
}
2016-09-21 01:08:05 +08:00
}