Magisk/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java

267 lines
10 KiB
Java
Raw Normal View History

2017-02-07 02:01:32 +08:00
package com.topjohnwu.magisk;
import android.app.Application;
2017-07-14 02:27:02 +08:00
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
2017-02-07 02:01:32 +08:00
import android.content.SharedPreferences;
2017-07-22 22:14:02 +08:00
import android.content.res.Configuration;
import android.content.res.Resources;
2017-07-14 02:27:02 +08:00
import android.os.Build;
2017-02-07 06:02:06 +08:00
import android.os.Handler;
2017-02-07 02:01:32 +08:00
import android.preference.PreferenceManager;
2017-02-07 06:02:06 +08:00
import android.widget.Toast;
2017-02-07 02:01:32 +08:00
2017-07-22 22:14:02 +08:00
import com.topjohnwu.magisk.asyncs.ParallelTask;
2017-07-21 00:46:13 +08:00
import com.topjohnwu.magisk.database.RepoDatabaseHelper;
2017-06-01 00:19:52 +08:00
import com.topjohnwu.magisk.database.SuDatabaseHelper;
2017-02-07 02:01:32 +08:00
import com.topjohnwu.magisk.module.Module;
2017-05-20 03:04:14 +08:00
import com.topjohnwu.magisk.utils.SafetyNetHelper;
2017-02-07 02:01:32 +08:00
import com.topjohnwu.magisk.utils.Shell;
import com.topjohnwu.magisk.utils.Topic;
2017-02-07 02:01:32 +08:00
import com.topjohnwu.magisk.utils.Utils;
import java.io.File;
import java.util.List;
2017-07-22 22:14:02 +08:00
import java.util.Locale;
import java.util.Map;
2017-02-07 02:01:32 +08:00
public class MagiskManager extends Application {
public static final String MAGISK_DISABLE_FILE = "/cache/.disable_magisk";
2017-02-12 19:49:46 +08:00
public static final String TMP_FOLDER_PATH = "/dev/tmp";
2017-02-12 20:53:41 +08:00
public static final String MAGISK_PATH = "/magisk";
2017-05-21 12:16:38 +08:00
public static final String UNINSTALLER = "magisk_uninstaller.sh";
2017-07-11 00:50:27 +08:00
public static final String UTIL_FUNCTIONS= "util_functions.sh";
public static final String INTENT_SECTION = "section";
2017-07-20 01:44:32 +08:00
public static final String INTENT_VERSION = "version";
public static final String INTENT_LINK = "link";
2017-05-27 02:41:24 +08:00
public static final String MAGISKHIDE_PROP = "persist.magisk.hide";
public static final String DISABLE_INDICATION_PROP = "ro.magisk.disable";
2017-07-14 02:27:02 +08:00
public static final String NOTIFICATION_CHANNEL = "magisk_update_notice";
public static final String BUSYBOX_VERSION = "1.27.1";
public static final String BUSYBOX_ARM = "https://github.com/topjohnwu/ndk-busybox/releases/download/1.27.1/busybox-arm";
public static final String BUSYBOX_X86 = "https://github.com/topjohnwu/ndk-busybox/releases/download/1.27.1/busybox-x86";
2017-02-07 02:01:32 +08:00
// Topics
public final Topic magiskHideDone = new Topic();
public final Topic reloadActivity = new Topic();
public final Topic moduleLoadDone = new Topic();
public final Topic repoLoadDone = new Topic();
public final Topic updateCheckDone = new Topic();
public final Topic safetyNetDone = new Topic();
public final Topic localeDone = new Topic();
2017-02-07 02:01:32 +08:00
// Info
public String magiskVersionString;
2017-05-12 02:25:07 +08:00
public int magiskVersionCode = -1;
public String remoteMagiskVersionString;
public int remoteMagiskVersionCode = -1;
2017-02-07 02:01:32 +08:00
public String magiskLink;
public String releaseNoteLink;
2017-06-07 02:19:23 +08:00
public String remoteManagerVersionString;
public int remoteManagerVersionCode = -1;
public String managerLink;
2017-05-20 03:04:14 +08:00
public SafetyNetHelper.Result SNCheckResult;
2017-02-07 02:01:32 +08:00
public String bootBlock = null;
public boolean isSuClient = false;
public String suVersion = null;
public boolean disabled;
2017-02-07 02:01:32 +08:00
// Data
public Map<String, Module> moduleMap;
2017-02-07 02:01:32 +08:00
public List<String> blockList;
2017-07-22 22:14:02 +08:00
public List<Locale> locales;
2017-02-07 02:01:32 +08:00
// Configurations
public static boolean shellLogging;
public static boolean devLogging;
2017-07-22 22:14:02 +08:00
public static Locale locale;
public static Locale defaultLocale;
2017-02-07 02:01:32 +08:00
2017-02-07 07:32:40 +08:00
public boolean magiskHide;
2017-02-07 02:01:32 +08:00
public boolean isDarkTheme;
2017-02-22 04:58:03 +08:00
public boolean updateNotification;
2017-05-31 16:31:33 +08:00
public boolean suReauth;
2017-02-07 02:01:32 +08:00
public int suRequestTimeout;
public int suLogTimeout = 14;
public int suAccessState;
2017-05-27 02:41:24 +08:00
public int multiuserMode;
2017-02-07 02:01:32 +08:00
public int suResponseType;
public int suNotificationType;
2017-06-08 22:27:24 +08:00
public int suNamespaceMode;
2017-02-07 02:01:32 +08:00
2017-06-01 00:19:52 +08:00
// Global resources
2017-02-07 02:01:32 +08:00
public SharedPreferences prefs;
2017-06-01 00:19:52 +08:00
public SuDatabaseHelper suDB;
2017-07-21 00:46:13 +08:00
public RepoDatabaseHelper repoDB;
2017-07-18 03:34:06 +08:00
public Shell shell;
2017-02-07 02:01:32 +08:00
2017-02-07 06:02:06 +08:00
private static Handler mHandler = new Handler();
2017-07-22 22:14:02 +08:00
private static class LoadLocale extends ParallelTask<Void, Void, Void> {
LoadLocale(Context context) {
super(context);
}
@Override
protected Void doInBackground(Void... voids) {
getMagiskManager().locales = Utils.getAvailableLocale(getMagiskManager());
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
getMagiskManager().localeDone.publish();
2017-07-22 22:14:02 +08:00
}
}
public void setLocale() {
String localeTag = prefs.getString("locale", "");
if (localeTag.isEmpty()) {
locale = defaultLocale;
} else {
locale = Locale.forLanguageTag(localeTag);
}
Resources res = getBaseContext().getResources();
Configuration config = new Configuration(res.getConfiguration());
config.setLocale(locale);
res.updateConfiguration(config, res.getDisplayMetrics());
}
2017-02-07 02:01:32 +08:00
@Override
public void onCreate() {
super.onCreate();
2017-07-16 01:20:39 +08:00
new File(getApplicationInfo().dataDir).mkdirs(); /* Create the app data directory */
2017-02-07 02:01:32 +08:00
prefs = PreferenceManager.getDefaultSharedPreferences(this);
2017-07-18 03:34:06 +08:00
shell = Shell.getShell();
2017-07-21 00:46:13 +08:00
suDB = new SuDatabaseHelper(this);
repoDB = new RepoDatabaseHelper(this);
2017-07-22 22:14:02 +08:00
defaultLocale = Locale.getDefault();
setLocale();
new LoadLocale(this).exec();
2017-02-07 02:01:32 +08:00
}
2017-02-07 06:02:06 +08:00
public void toast(String msg, int duration) {
mHandler.post(() -> Toast.makeText(this, msg, duration).show());
}
public void toast(int resId, int duration) {
mHandler.post(() -> Toast.makeText(this, resId, duration).show());
}
2017-02-07 02:01:32 +08:00
public void init() {
isDarkTheme = prefs.getBoolean("dark_theme", false);
2017-05-12 02:25:07 +08:00
if (BuildConfig.DEBUG) {
devLogging = prefs.getBoolean("developer_logging", false);
shellLogging = prefs.getBoolean("shell_logging", false);
} else {
devLogging = false;
shellLogging = false;
}
initSU();
2017-02-07 02:01:32 +08:00
updateMagiskInfo();
2017-07-18 03:34:06 +08:00
updateBlockInfo();
2017-02-07 02:01:32 +08:00
// Initialize prefs
prefs.edit()
.putBoolean("dark_theme", isDarkTheme)
.putBoolean("magiskhide", magiskHide)
2017-02-22 04:58:03 +08:00
.putBoolean("notification", updateNotification)
2017-02-07 02:01:32 +08:00
.putBoolean("hosts", new File("/magisk/.core/hosts").exists())
2017-07-18 03:34:06 +08:00
.putBoolean("disable", Utils.itemExist(shell, MAGISK_DISABLE_FILE))
2017-05-31 16:31:33 +08:00
.putBoolean("su_reauth", suReauth)
2017-02-07 02:01:32 +08:00
.putString("su_request_timeout", String.valueOf(suRequestTimeout))
.putString("su_auto_response", String.valueOf(suResponseType))
.putString("su_notification", String.valueOf(suNotificationType))
.putString("su_access", String.valueOf(suAccessState))
2017-05-27 02:41:24 +08:00
.putString("multiuser_mode", String.valueOf(multiuserMode))
2017-06-08 22:27:24 +08:00
.putString("mnt_ns", String.valueOf(suNamespaceMode))
.putString("busybox_version", BUSYBOX_VERSION)
2017-02-07 02:01:32 +08:00
.apply();
// Add busybox to PATH
shell.su_raw("PATH=" + getApplicationInfo().dataDir + "/busybox:$PATH");
2017-07-14 02:27:02 +08:00
// Create notification channel on Android O
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL,
getString(R.string.magisk_updates), NotificationManager.IMPORTANCE_DEFAULT);
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
}
2017-02-07 02:01:32 +08:00
}
2017-05-31 16:31:33 +08:00
public void initSUConfig() {
suRequestTimeout = Utils.getPrefsInt(prefs, "su_request_timeout", 10);
suResponseType = Utils.getPrefsInt(prefs, "su_auto_response", 0);
suNotificationType = Utils.getPrefsInt(prefs, "su_notification", 1);
suReauth = prefs.getBoolean("su_reauth", false);
}
public void initSU() {
2017-05-31 16:31:33 +08:00
initSUConfig();
2017-02-07 02:01:32 +08:00
2017-07-18 03:34:06 +08:00
List<String> ret = shell.sh("su -v");
2017-02-07 02:01:32 +08:00
if (Utils.isValidShellResponse(ret)) {
suVersion = ret.get(0);
isSuClient = suVersion.toUpperCase().contains("MAGISK");
}
if (isSuClient) {
2017-06-01 03:18:41 +08:00
suAccessState = suDB.getSettings(SuDatabaseHelper.ROOT_ACCESS, 3);
multiuserMode = suDB.getSettings(SuDatabaseHelper.MULTIUSER_MODE, 0);
2017-06-08 22:27:24 +08:00
suNamespaceMode = suDB.getSettings(SuDatabaseHelper.MNT_NS, 1);
2017-02-07 02:01:32 +08:00
}
}
public void updateMagiskInfo() {
2017-07-19 16:10:17 +08:00
updateNotification = prefs.getBoolean("notification", true);
2017-05-12 02:25:07 +08:00
List<String> ret;
2017-07-18 03:34:06 +08:00
ret = shell.sh("magisk -v");
2017-02-07 02:01:32 +08:00
if (!Utils.isValidShellResponse(ret)) {
2017-07-18 03:34:06 +08:00
ret = shell.sh("getprop magisk.version");
2017-05-12 02:25:07 +08:00
if (Utils.isValidShellResponse(ret)) {
try {
magiskVersionString = ret.get(0);
magiskVersionCode = (int) Double.parseDouble(ret.get(0)) * 10;
} catch (NumberFormatException ignored) {}
}
2017-02-07 02:01:32 +08:00
} else {
2017-05-12 02:25:07 +08:00
magiskVersionString = ret.get(0).split(":")[0];
2017-07-18 03:34:06 +08:00
ret = shell.sh("magisk -V");
2017-02-07 02:01:32 +08:00
try {
2017-05-12 02:25:07 +08:00
magiskVersionCode = Integer.parseInt(ret.get(0));
} catch (NumberFormatException ignored) {}
2017-02-07 02:01:32 +08:00
}
2017-07-18 03:34:06 +08:00
ret = shell.sh("getprop " + DISABLE_INDICATION_PROP);
2017-02-07 02:01:32 +08:00
try {
disabled = Utils.isValidShellResponse(ret) && Integer.parseInt(ret.get(0)) != 0;
} catch (NumberFormatException e) {
disabled = false;
}
2017-07-18 03:34:06 +08:00
ret = shell.sh("getprop " + MAGISKHIDE_PROP);
try {
2017-07-01 17:38:33 +08:00
magiskHide = !Utils.isValidShellResponse(ret) || Integer.parseInt(ret.get(0)) != 0;
} catch (NumberFormatException e) {
2017-07-01 17:38:33 +08:00
magiskHide = true;
}
2017-07-18 03:34:06 +08:00
}
public void updateBlockInfo() {
List<String> res = shell.su(
"for BLOCK in boot_a BOOT_A kern-a KERN-A android_boot ANDROID_BOOT kernel KERNEL boot BOOT lnx LNX; do",
"BOOTIMAGE=`ls /dev/block/by-name/$BLOCK || ls /dev/block/platform/*/by-name/$BLOCK || ls /dev/block/platform/*/*/by-name/$BLOCK` 2>/dev/null",
"[ ! -z \"$BOOTIMAGE\" ] && break",
"done",
"[ ! -z \"$BOOTIMAGE\" -a -L \"$BOOTIMAGE\" ] && BOOTIMAGE=`readlink $BOOTIMAGE`",
"echo \"$BOOTIMAGE\""
);
if (Utils.isValidShellResponse(res)) {
bootBlock = res.get(0);
} else {
blockList = shell.su("ls -d /dev/block/mmc* /dev/block/sd* 2>/dev/null");
}
2017-02-07 02:01:32 +08:00
}
}