358 lines
14 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-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;
2016-09-28 14:50:26 +08:00
import android.text.TextUtils;
2016-09-21 01:08:05 +08:00
import android.util.Log;
import android.widget.Toast;
import com.topjohnwu.magisk.ModulesFragment;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.ReposFragment;
import com.topjohnwu.magisk.module.Module;
2016-09-26 10:45:34 +08:00
import com.topjohnwu.magisk.receivers.DownloadReceiver;
2016-09-21 01:08:05 +08:00
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;
2016-09-21 11:29:43 +08:00
import java.util.Collections;
2016-09-21 01:08:05 +08:00
import java.util.List;
public class Async {
2016-09-28 14:50:26 +08:00
public static class constructEnv extends AsyncTask<Void, Void, Void> {
Context mContext;
2016-09-28 14:50:26 +08:00
public constructEnv(Context context) {
mContext = context;
}
@Override
protected Void doInBackground(Void... voids) {
String toolPath = mContext.getApplicationInfo().dataDir + "/busybox";
String busybox = mContext.getApplicationInfo().dataDir + "/lib/libbusybox.so";
2016-09-28 14:50:26 +08:00
String zip = mContext.getApplicationInfo().dataDir + "/lib/libzip.so";
if (Shell.rootAccess()) {
if (!Utils.commandExists("unzip") || !Utils.commandExists("zip") || !Utils.itemExist(toolPath)) {
Shell.sh(
"rm -rf " + toolPath,
"mkdir " + toolPath,
"chmod 755 " + toolPath,
"ln -s " + busybox + " " + toolPath + "/busybox",
"for tool in $(" + toolPath + "/busybox --list); do",
"ln -s " + busybox + " " + toolPath + "/$tool",
"done",
!Utils.commandExists("zip") ? "ln -s " + zip + " " + toolPath + "/zip" : ""
);
}
}
2016-09-28 14:50:26 +08:00
return null;
}
}
2016-09-21 01:08:05 +08:00
public static class CheckUpdates extends AsyncTask<Void, Void, Void> {
private Context mContext;
public CheckUpdates(Context context) {
mContext = context;
}
@Override
protected Void doInBackground(Void... voids) {
2016-09-27 15:51:38 +08:00
String jsonStr = WebRequest.makeWebServiceCall(Utils.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");
Utils.remoteMagiskVersion = magisk.getInt("versionCode");
Utils.magiskLink = magisk.getString("link");
Utils.magiskChangelog = magisk.getString("changelog");
2016-09-28 14:50:26 +08:00
Utils.remoteAppVersion = app.getString("version");
Utils.remoteAppVersionCode = app.getInt("versionCode");
2016-09-21 01:08:05 +08:00
Utils.appLink = app.getString("link");
Utils.appChangelog = app.getString("changelog");
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-09-21 01:08:05 +08:00
if (Shell.rootAccess() && Utils.magiskVersion == -1) {
new AlertDialog.Builder(mContext)
.setTitle(R.string.no_magisk_title)
.setMessage(R.string.no_magisk_msg)
.setCancelable(true)
2016-09-27 15:51:38 +08:00
.setPositiveButton(R.string.download_install, (dialogInterface, i) -> Utils.downloadAndReceive(
mContext,
2016-09-28 14:50:26 +08:00
new DownloadReceiver() {
2016-09-27 15:51:38 +08:00
@Override
public void task(Uri uri) {
2016-09-28 14:50:26 +08:00
new Async.FlashZIP(mContext, uri).executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
}
2016-09-27 15:51:38 +08:00
},
Utils.magiskLink,
2016-09-28 14:50:26 +08:00
"Magisk-v" + String.valueOf(Utils.remoteMagiskVersion) + ".zip"))
2016-09-21 01:08:05 +08:00
.setNegativeButton(R.string.no_thanks, null)
.show();
}
}
}
public static class LoadModules extends AsyncTask<Void, Void, Void> {
private Context mContext;
public LoadModules(Context context) {
mContext = context;
}
@Override
protected Void doInBackground(Void... voids) {
ModulesFragment.listModules.clear();
2016-09-28 00:33:01 +08:00
Logger.dev("Loading modules");
2016-09-21 01:08:05 +08:00
List<String> magisk = Utils.getModList(Utils.MAGISK_PATH);
List<String> magiskCache = Utils.getModList(Utils.MAGISK_CACHE_PATH);
for (String mod : magisk) {
2016-09-28 00:33:01 +08:00
Logger.dev("Adding modules from " + mod);
2016-09-27 15:51:38 +08:00
ModulesFragment.listModules.add(new Module(mod));
2016-09-21 01:08:05 +08:00
}
for (String mod : magiskCache) {
2016-09-28 00:33:01 +08:00
Logger.dev("Adding cache modules from " + mod);
2016-09-27 15:51:38 +08:00
Module cacheMod = new Module(mod);
2016-09-21 01:08:05 +08:00
// Prevent people forgot to change module.prop
cacheMod.setCache();
ModulesFragment.listModules.add(cacheMod);
}
2016-09-21 11:29:43 +08:00
Collections.sort(ModulesFragment.listModules, new Utils.ModuleComparator());
2016-09-28 00:33:01 +08:00
Logger.dev("Module load done");
2016-09-21 01:08:05 +08:00
return null;
}
2016-09-27 22:57:20 +08:00
@Override
protected void onPostExecute(Void v) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
prefs.edit().putBoolean("module_done", true).apply();
}
2016-09-21 01:08:05 +08:00
}
public static class LoadRepos extends AsyncTask<Void, Void, Void> {
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) {
ReposFragment.mListRepos.clear();
2016-09-21 11:29:43 +08:00
RepoHelper.createRepoMap(mContext);
2016-09-23 17:12:29 +08:00
RepoHelper.checkUpdate();
2016-09-21 11:29:43 +08:00
ReposFragment.mListRepos = RepoHelper.getSortedList();
2016-09-21 01:08:05 +08:00
return null;
}
2016-09-27 22:57:20 +08:00
@Override
protected void onPostExecute(Void v) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
prefs.edit().putBoolean("repo_done", true).apply();
}
2016-09-21 01:08:05 +08:00
}
2016-09-28 14:50:26 +08:00
public static class FlashZIP extends AsyncTask<Void, Void, Integer> {
2016-09-21 01:08:05 +08:00
2016-09-27 22:57:20 +08:00
private String mName;
2016-09-28 14:50:26 +08:00
protected Uri mUri;
2016-09-21 01:08:05 +08:00
private ProgressDialog progress;
2016-09-28 14:50:26 +08:00
protected File mFile, sdFile;
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
2016-09-27 15:51:38 +08:00
public FlashZIP(Context context, Uri uri, String name) {
2016-09-21 01:08:05 +08:00
mContext = context;
2016-09-27 15:51:38 +08:00
mUri = uri;
2016-09-21 01:08:05 +08:00
mName = name;
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);
int nameIndex = c.getColumnIndex(OpenableColumns.DISPLAY_NAME);
c.moveToFirst();
2016-09-28 14:50:26 +08:00
if (nameIndex != -1) {
mName = c.getString(nameIndex);
} else {
int idx = uri.getPath().lastIndexOf('/');
mName = uri.getPath().substring(idx + 1);
}
2016-09-28 00:33:01 +08:00
c.close();
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 {
try {
InputStream in = mContext.getContentResolver().openInputStream(mUri);
mFile = new File(mContext.getCacheDir().getAbsolutePath() + "/install.zip");
createFileFromInputStream(in, mFile);
in.close();
} catch (FileNotFoundException e) {
Log.e("Magisk", "FlashZip: Invalid Uri");
throw e;
} catch (IOException e) {
Log.e("Magisk", "FlashZip: Error in creating file");
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, mName));
}
@Override
2016-09-28 14:50:26 +08:00
protected Integer doInBackground(Void... voids) {
2016-09-28 00:33:01 +08:00
Logger.dev("FlashZip Running... " + mName);
2016-09-28 14:50:26 +08:00
List<String> ret = null;
if (Shell.rootAccess()) {
2016-09-27 15:51:38 +08:00
try {
2016-09-28 14:50:26 +08:00
preProcessing();
} catch (Throwable e) {
this.cancel(true);
progress.cancel();
e.printStackTrace();
return -1;
2016-09-27 15:51:38 +08:00
}
2016-09-21 01:08:05 +08:00
ret = Shell.su(
2016-09-27 15:51:38 +08:00
"unzip -o " + mFile.getPath() + " META-INF/com/google/android/* -d " + mFile.getParent(),
2016-09-28 14:50:26 +08:00
"if [ \"$(cat " + mFile.getParent() + "/META-INF/com/google/android/updater-script)\" = \"#MAGISK\" ]; then echo true; else echo false; fi"
);
if (! Boolean.parseBoolean(ret.get(ret.size() - 1))) {
return 0;
}
ret = Shell.su(
2016-09-27 15:51:38 +08:00
"BOOTMODE=true sh " + mFile.getParent() + "/META-INF/com/google/android/update-binary dummy 1 "+ mFile.getPath(),
2016-09-28 14:50:26 +08:00
"if [ $? -eq 0 ]; then echo true; else echo false; fi",
"rm -rf " + mFile.getParent() + "/META-INF"
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-09-21 01:08:05 +08:00
}
2016-09-27 15:51:38 +08:00
// Copy the file to sdcard
2016-09-28 14:50:26 +08:00
if (copyToSD && mFile != null) {
sdFile = new File(Environment.getExternalStorageDirectory() + "/MagiskManager/" + (mName.contains(".zip") ? mName : mName + ".zip").replace(" ", "_"));
if ((!sdFile.getParentFile().exists() && !sdFile.getParentFile().mkdirs()) || (sdFile.exists() && !sdFile.delete())) {
sdFile = null;
} else {
try {
FileInputStream in = new FileInputStream(mFile);
createFileFromInputStream(in, sdFile);
in.close();
mFile.delete();
} catch (IOException e) {
// Use the badass way :)
Shell.su("cp -af " + mFile.getPath() + " " + sdFile.getPath());
if (!sdFile.exists()) {
sdFile = null;
}
2016-09-27 15:51:38 +08:00
}
2016-09-28 14:50:26 +08:00
}
if (mFile.exists() && !mFile.delete()) {
Shell.su("rm -f " + mFile.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, mFile.getAbsolutePath()), Toast.LENGTH_LONG).show();
}
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-25 10:11:57 -05:00
AlertDialog.Builder builder;
String theme = PreferenceManager.getDefaultSharedPreferences(mContext).getString("theme", "");
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-27 15:51:38 +08:00
.setPositiveButton(R.string.reboot, (dialogInterface1, i) -> Shell.su("sh -c reboot"))
2016-09-21 01:08:05 +08:00
.setNegativeButton(R.string.no_thanks, null)
.show();
}
}
}