314 lines
11 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;
2016-09-27 22:57:20 +08:00
import android.content.SharedPreferences;
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-25 10:11:57 -05:00
import android.preference.PreferenceManager;
2016-09-28 00:33:01 +08:00
import android.provider.OpenableColumns;
2016-09-21 01:08:05 +08:00
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.widget.Toast;
2016-12-25 22:36:51 +08:00
import com.topjohnwu.magisk.InstallFragment;
2016-12-25 15:11:59 +08:00
import com.topjohnwu.magisk.ModulesFragment;
2016-09-21 01:08:05 +08:00
import com.topjohnwu.magisk.R;
2016-12-25 15:11:59 +08:00
import com.topjohnwu.magisk.ReposFragment;
2016-12-25 03:05:22 +08:00
import com.topjohnwu.magisk.StatusFragment;
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;
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) {
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) {
2016-09-29 01:42:25 +08:00
String jsonStr = WebRequest.makeWebServiceCall(UPDATE_JSON, WebRequest.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");
2016-12-25 03:05:22 +08:00
StatusFragment.remoteMagiskVersion = magisk.getDouble("versionCode");
StatusFragment.magiskLink = magisk.getString("link");
StatusFragment.magiskChangelog = magisk.getString("changelog");
2016-09-21 01:08:05 +08:00
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) {
2016-12-25 15:29:24 +08:00
StatusFragment.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) {
StatusFragment.SNCheckResult = i;
2016-12-25 15:29:24 +08:00
StatusFragment.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) {
2016-12-25 15:29:24 +08:00
ModulesFragment.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) {
2016-12-25 15:29:24 +08:00
ReposFragment.repoLoadDone.trigger();
2016-09-27 22:57:20 +08:00
}
2016-09-21 01:08:05 +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 {}
protected void copyToCache() throws Throwable {
2016-09-28 14:50:26 +08:00
try {
InputStream in = mContext.getContentResolver().openInputStream(mUri);
mCachedFile = new File(mContext.getCacheDir().getAbsolutePath() + "/install.zip");
2016-11-23 22:38:15 +08:00
if (mCachedFile.exists() && !mCachedFile.delete()) {
throw new IOException();
}
OutputStream outputStream = new FileOutputStream(mCachedFile);
byte buffer[] = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
outputStream.close();
Logger.dev("FlashZip: File created successfully - " + mCachedFile.getPath());
2016-09-28 14:50:26 +08:00
in.close();
} catch (FileNotFoundException e) {
2016-12-23 23:05:41 +08:00
Log.e(Logger.TAG, "FlashZip: Invalid Uri");
2016-09-28 14:50:26 +08:00
throw e;
} catch (IOException e) {
2016-12-23 23:05:41 +08:00
Log.e(Logger.TAG, "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");
return Utils.readFile(mCachedFile.getParent() + "/META-INF/com/google/android/updater-script")
.get(0).contains("#MAGISK");
}
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;
2016-09-28 14:50:26 +08:00
if (Shell.rootAccess()) {
2016-11-29 13:38:32 +08:00
publishProgress(mContext.getString(R.string.zip_install_progress_msg, mFilename));
2016-09-21 01:08:05 +08:00
ret = Shell.su(
2016-11-29 13:24:48 +08:00
"BOOTMODE=true sh " + mCachedFile.getParent() +
"/META-INF/com/google/android/update-binary dummy 1 " + mCachedFile.getPath(),
2016-10-03 00:05:53 +08:00
"if [ $? -eq 0 ]; then echo true; else echo false; fi"
2016-09-21 01:08:05 +08:00
);
2016-09-28 00:33:01 +08:00
Logger.dev("FlashZip: Console log:");
for (String line : ret) {
Logger.dev(line);
}
2016-11-29 13:24:48 +08:00
Shell.su(
"rm -rf " + mCachedFile.getParent() + "/*",
"rm -rf " + TMP_FOLDER_PATH
);
2016-11-29 13:38:32 +08:00
} else {
if (mCachedFile != null && mCachedFile.exists() && !mCachedFile.delete()) {
Utils.removeItem(mCachedFile.getPath());
}
return -1;
2016-09-27 15:51:38 +08:00
}
2016-09-28 14:50:26 +08:00
if (ret != null && Boolean.parseBoolean(ret.get(ret.size() - 1))) {
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:
done();
break;
2016-09-21 01:08:05 +08:00
}
}
protected void done() {
2016-09-29 23:24:31 +08:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
2016-10-01 05:21:24 +08:00
prefs.edit().putBoolean("module_done", false).putBoolean("update_check_done", true).apply();
2016-12-25 15:11:59 +08:00
new LoadModules().exec();
2016-09-29 23:24:31 +08:00
2016-09-25 10:11:57 -05:00
AlertDialog.Builder builder;
2016-10-03 00:05:53 +08:00
String theme = prefs.getString("theme", "");
2016-09-25 10:11:57 -05:00
if (theme.equals("Dark")) {
2016-09-27 15:51:38 +08:00
builder = new AlertDialog.Builder(mContext, R.style.AlertDialog_dh);
2016-09-25 10:11:57 -05:00
} else {
builder = new AlertDialog.Builder(mContext);
}
builder
2016-09-21 01:08:05 +08:00
.setTitle(R.string.reboot_title)
.setMessage(R.string.reboot_msg)
2016-09-29 23:24:31 +08:00
.setPositiveButton(R.string.reboot, (dialogInterface1, 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) {
InstallFragment.blockList = Shell.su("ls /dev/block | grep mmc");
if (InstallFragment.bootBlock == null) {
InstallFragment.bootBlock = Utils.detectBootImage();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
InstallFragment.blockDetectionDone.trigger();
}
}
2016-09-21 01:08:05 +08:00
}