127 lines
5.4 KiB
Java
Raw Normal View History

2017-01-25 04:27:05 +08:00
package com.topjohnwu.magisk;
2017-01-25 13:16:50 +08:00
import android.content.Context;
import android.content.SharedPreferences;
2017-01-30 20:04:49 +08:00
import android.content.pm.ApplicationInfo;
2017-01-25 13:16:50 +08:00
import android.preference.PreferenceManager;
2017-01-30 19:27:00 +08:00
import android.util.SparseArray;
2017-01-25 13:16:50 +08:00
import com.topjohnwu.magisk.module.Module;
2017-01-25 04:27:05 +08:00
import com.topjohnwu.magisk.module.Repo;
import com.topjohnwu.magisk.utils.CallbackHandler;
2017-01-26 13:46:54 +08:00
import com.topjohnwu.magisk.utils.Shell;
import com.topjohnwu.magisk.utils.Utils;
2017-01-25 04:27:05 +08:00
import com.topjohnwu.magisk.utils.ValueSortedMap;
import java.util.List;
public class Global {
2017-01-26 13:46:54 +08:00
2017-01-25 04:27:05 +08:00
public static class Info {
public static double magiskVersion;
public static String magiskVersionString = "(none)";
2017-01-26 13:46:54 +08:00
public static double remoteMagiskVersion = -1;
2017-01-25 04:27:05 +08:00
public static String magiskLink;
public static String releaseNoteLink;
public static int SNCheckResult = -1;
public static String bootBlock = null;
2017-01-27 03:38:53 +08:00
public static boolean isSuClient = false;
public static String suVersion = null;
2017-01-25 04:27:05 +08:00
}
public static class Data {
2017-01-30 19:27:00 +08:00
public static ValueSortedMap<String, Repo> repoMap;
public static ValueSortedMap<String, Module> moduleMap;
2017-01-25 04:27:05 +08:00
public static List<String> blockList;
2017-01-30 20:04:49 +08:00
public static List<ApplicationInfo> appList;
public static List<String> magiskHideList;
public static void clear() {
repoMap = null;
moduleMap = null;
blockList = null;
appList = null;
magiskHideList = null;
}
2017-01-25 04:27:05 +08:00
}
public static class Events {
public static final CallbackHandler.Event blockDetectionDone = new CallbackHandler.Event();
public static final CallbackHandler.Event packageLoadDone = new CallbackHandler.Event();
public static final CallbackHandler.Event reloadMainActivity = new CallbackHandler.Event();
public static final CallbackHandler.Event moduleLoadDone = new CallbackHandler.Event();
public static final CallbackHandler.Event repoLoadDone = new CallbackHandler.Event();
public static final CallbackHandler.Event updateCheckDone = new CallbackHandler.Event();
public static final CallbackHandler.Event safetyNetDone = new CallbackHandler.Event();
2017-01-30 19:27:00 +08:00
public static SparseArray<CallbackHandler.Event> uidMap = new SparseArray<>();
2017-01-25 04:27:05 +08:00
}
2017-01-25 13:16:50 +08:00
public static class Configs {
public static boolean isDarkTheme;
public static boolean shellLogging;
public static boolean devLogging;
2017-01-28 06:13:07 +08:00
public static int suRequestTimeout;
2017-01-27 01:02:40 +08:00
public static int suLogTimeout = 14;
2017-01-28 06:13:07 +08:00
public static int suAccessState;
public static int suResponseType;
public static int suNotificationType;
2017-01-26 13:46:54 +08:00
}
public static void init(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
Configs.isDarkTheme = prefs.getBoolean("dark_theme", false);
Configs.devLogging = prefs.getBoolean("developer_logging", false);
Configs.shellLogging = prefs.getBoolean("shell_logging", false);
updateMagiskInfo();
2017-01-29 16:52:43 +08:00
initSuAccess();
2017-01-28 06:13:07 +08:00
initSuConfigs(context);
// Initialize prefs
prefs.edit()
2017-01-28 22:02:33 +08:00
.putBoolean("dark_theme", Configs.isDarkTheme)
2017-01-28 06:13:07 +08:00
.putBoolean("magiskhide", Utils.itemExist(false, "/magisk/.core/magiskhide/enable"))
.putBoolean("busybox", Utils.commandExists("busybox"))
.putBoolean("hosts", Utils.itemExist(false, "/magisk/.core/hosts"))
.putString("su_request_timeout", String.valueOf(Configs.suRequestTimeout))
.putString("su_auto_response", String.valueOf(Configs.suResponseType))
.putString("su_notification", String.valueOf(Configs.suNotificationType))
.putString("su_access", String.valueOf(Configs.suAccessState))
.apply();
}
public static void initSuConfigs(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
Configs.suRequestTimeout = Utils.getPrefsInt(prefs, "su_request_timeout", 10);
Configs.suResponseType = Utils.getPrefsInt(prefs, "su_auto_response", 0);
Configs.suNotificationType = Utils.getPrefsInt(prefs, "su_notification", 1);
2017-01-29 16:52:43 +08:00
}
public static void initSuAccess() {
List<String> ret = Shell.sh("su -v");
if (Utils.isValidShellResponse(ret)) {
Info.suVersion = ret.get(0);
Info.isSuClient = Info.suVersion.toUpperCase().contains("MAGISK");
}
if (Info.isSuClient) {
ret = Shell.sh("getprop persist.sys.root_access");
if (Utils.isValidShellResponse(ret))
Configs.suAccessState = Integer.parseInt(ret.get(0));
else {
Shell.su("setprop persist.sys.root_access 3");
Configs.suAccessState = 3;
}
}
2017-01-26 13:46:54 +08:00
}
static void updateMagiskInfo() {
List<String> ret = Shell.sh("getprop magisk.version");
2017-01-27 01:02:40 +08:00
if (!Utils.isValidShellResponse(ret)) {
2017-01-26 13:46:54 +08:00
Info.magiskVersion = -1;
} else {
try {
Info.magiskVersionString = ret.get(0);
Info.magiskVersion = Double.parseDouble(ret.get(0));
} catch (NumberFormatException e) {
// Custom version don't need to receive updates
Info.magiskVersion = Double.POSITIVE_INFINITY;
}
2017-01-25 13:16:50 +08:00
}
}
2017-01-25 04:27:05 +08:00
}