370 lines
13 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-10-01 05:21:24 +08:00
import android.content.pm.ApplicationInfo;
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-27 15:51:38 +08:00
import android.os.Environment;
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-09-29 01:42:25 +08:00
import com.topjohnwu.magisk.MagiskFragment;
2016-09-21 01:08:05 +08:00
import com.topjohnwu.magisk.R;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
2016-09-28 14:50:26 +08:00
import java.io.FileInputStream;
2016-09-21 01:08:05 +08:00
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-09-29 01:42:25 +08:00
2016-11-07 23:59:10 +08:00
public static class ConstructEnv extends NormalTask<Void, Void, Void> {
2016-10-01 05:21:24 +08:00
ApplicationInfo mInfo;
2016-11-07 23:59:10 +08:00
public ConstructEnv(ApplicationInfo info) {
2016-10-01 05:21:24 +08:00
mInfo = info;
}
@Override
protected Void doInBackground(Void... voids) {
2016-10-01 05:21:24 +08:00
String toolPath = mInfo.dataDir + "/tools";
String busybox = mInfo.dataDir + "/lib/libbusybox.so";
if (!Utils.itemExist(false, toolPath)) {
Shell.sh(
"mkdir " + toolPath,
"chmod 755 " + toolPath,
"cd " + toolPath,
"ln -s " + busybox + " busybox",
"for tool in $(./busybox --list); do",
"ln -s " + busybox + " $tool",
"done",
2016-11-21 01:33:12 +08:00
"rm -f su sh"
);
}
return null;
}
2016-11-07 21:06:18 +08:00
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
new RootTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
Shell.su("PATH=" + mInfo.dataDir + "/tools:$PATH");
return null;
}
}.exec();
}
}
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
2016-10-01 05:21:24 +08:00
private SharedPreferences mPrefs;
2016-09-21 01:08:05 +08:00
2016-10-01 05:21:24 +08:00
public CheckUpdates(SharedPreferences prefs) {
mPrefs = prefs;
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");
JSONObject app = json.getJSONObject("app");
MagiskFragment.remoteMagiskVersion = magisk.getDouble("versionCode");
2016-09-29 01:42:25 +08:00
MagiskFragment.magiskLink = magisk.getString("link");
MagiskFragment.magiskChangelog = magisk.getString("changelog");
2016-09-21 01:08:05 +08:00
2016-09-29 01:42:25 +08:00
MagiskFragment.remoteAppVersion = app.getString("version");
MagiskFragment.remoteAppVersionCode = app.getInt("versionCode");
MagiskFragment.appLink = app.getString("link");
MagiskFragment.appChangelog = app.getString("changelog");
2016-09-21 01:08:05 +08:00
2016-09-28 14:50:26 +08:00
} catch (JSONException ignored) {
Logger.dev("JSON error!");
}
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-10-01 05:21:24 +08:00
mPrefs.edit().putBoolean("update_check_done", true).apply();
2016-09-21 01:08:05 +08:00
}
}
2016-11-07 21:06:18 +08:00
public static class LoadModules extends RootTask<Void, Void, Void> {
2016-09-21 01:08:05 +08:00
2016-10-01 05:21:24 +08:00
private SharedPreferences mPrefs;
2016-09-21 01:08:05 +08:00
2016-10-01 05:21:24 +08:00
public LoadModules(SharedPreferences prefs) {
mPrefs = prefs;
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-10-01 05:21:24 +08:00
mPrefs.edit().putBoolean("module_done", true).apply();
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-10-01 05:21:24 +08:00
PreferenceManager.getDefaultSharedPreferences(mContext).edit()
.putBoolean("repo_done", true).apply();
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 FlashZIP extends RootTask<Void, Void, Integer> {
2016-09-21 01:08:05 +08:00
2016-09-28 14:50:26 +08:00
protected Uri mUri;
protected File mCachedFile, sdFile;
private String mFilename;
private ProgressDialog progress;
2016-09-21 01:08:05 +08:00
private Context mContext;
2016-09-27 15:51:38 +08:00
private boolean copyToSD;
2016-09-21 01:08:05 +08:00
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-27 15:51:38 +08:00
copyToSD = true;
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-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-27 15:51:38 +08:00
copyToSD = false;
2016-09-21 01:08:05 +08:00
}
2016-09-28 14:50:26 +08:00
private void createFileFromInputStream(InputStream inputStream, File file) throws IOException {
if (file.exists() && !file.delete()) {
2016-09-27 15:51:38 +08:00
throw new IOException();
}
2016-09-28 14:50:26 +08:00
file.setWritable(true, false);
OutputStream outputStream = new FileOutputStream(file);
2016-09-27 15:51:38 +08:00
byte buffer[] = new byte[1024];
int length;
2016-09-27 15:51:38 +08:00
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
2016-09-21 01:08:05 +08:00
}
2016-09-27 15:51:38 +08:00
outputStream.close();
2016-09-28 14:50:26 +08:00
Logger.dev("FlashZip: File created successfully - " + file.getPath());
}
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");
mCachedFile.delete();
Utils.removeItem(mCachedFile.getPath());
createFileFromInputStream(in, mCachedFile);
2016-09-28 14:50:26 +08:00
in.close();
} catch (FileNotFoundException e) {
Log.e(Logger.LOG_TAG, "FlashZip: Invalid Uri");
2016-09-28 14:50:26 +08:00
throw e;
} catch (IOException e) {
Log.e(Logger.LOG_TAG, "FlashZip: Error in creating file");
2016-09-28 14:50:26 +08:00
throw e;
}
2016-09-21 01:08:05 +08:00
}
@Override
protected void onPreExecute() {
progress = ProgressDialog.show(mContext, mContext.getString(R.string.zip_install_progress_title), mContext.getString(R.string.zip_install_progress_msg, mFilename));
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-09-28 14:50:26 +08:00
List<String> ret = null;
try {
preProcessing();
copyToCache();
} catch (Throwable e) {
this.cancel(true);
progress.cancel();
e.printStackTrace();
return -1;
}
2016-09-28 14:50:26 +08:00
if (Shell.rootAccess()) {
2016-09-21 01:08:05 +08:00
ret = Shell.su(
"unzip -o " + mCachedFile.getPath() + " META-INF/com/google/android/* -d " + mCachedFile.getParent(),
"if [ \"$(cat " + mCachedFile.getParent() + "/META-INF/com/google/android/updater-script)\" = \"#MAGISK\" ]; then echo true; else echo false; fi"
2016-09-28 14:50:26 +08:00
);
if (! Boolean.parseBoolean(ret.get(ret.size() - 1))) {
return 0;
}
ret = Shell.su(
"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
);
Shell.su("rm -rf " + mCachedFile.getParent() + "/META-INF");
2016-09-28 00:33:01 +08:00
Logger.dev("FlashZip: Console log:");
for (String line : ret) {
Logger.dev(line);
}
2016-09-21 01:08:05 +08:00
}
2016-09-27 15:51:38 +08:00
// Copy the file to sdcard
if (copyToSD && mCachedFile != null) {
String filename = Utils.getLegalFilename(mFilename.contains(".zip") ? mFilename : mFilename + ".zip");
2016-09-30 03:18:08 +08:00
sdFile = new File(Environment.getExternalStorageDirectory() + "/MagiskManager/" + filename);
Logger.dev("FlashZip: Copy zip back to " + sdFile.getPath());
2016-09-28 14:50:26 +08:00
if ((!sdFile.getParentFile().exists() && !sdFile.getParentFile().mkdirs()) || (sdFile.exists() && !sdFile.delete())) {
sdFile = null;
} else {
try {
FileInputStream in = new FileInputStream(mCachedFile);
2016-09-28 14:50:26 +08:00
createFileFromInputStream(in, sdFile);
in.close();
} catch (IOException e) {
// Use the badass way :)
e.printStackTrace();
Shell.su("cp -af " + mCachedFile.getPath() + " " + sdFile.getPath());
2016-09-28 14:50:26 +08:00
if (!sdFile.exists()) {
sdFile = null;
}
2016-09-27 15:51:38 +08:00
}
2016-09-28 14:50:26 +08:00
}
2016-10-03 00:05:53 +08:00
}
if (mCachedFile != null && mCachedFile.exists() && !mCachedFile.delete()) {
Utils.removeItem(mCachedFile.getPath());
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-09-28 14:50:26 +08:00
// -1 = error; 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:
if (sdFile == null) {
Toast.makeText(mContext, mContext.getString(R.string.install_error), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(mContext, mContext.getString(R.string.manual_install, sdFile.getAbsolutePath()), 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-11-07 21:06:18 +08:00
new LoadModules(prefs).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-09-21 01:08:05 +08:00
}