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

239 lines
9.6 KiB
Java
Raw Normal View History

2017-02-06 19:01:32 +01:00
package com.topjohnwu.magisk;
import android.app.Application;
2017-07-13 20:27:02 +02:00
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
2017-02-06 19:01:32 +01:00
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
2017-07-13 20:27:02 +02:00
import android.os.Build;
2017-02-06 23:02:06 +01:00
import android.os.Handler;
2017-02-06 19:01:32 +01:00
import android.preference.PreferenceManager;
import android.text.TextUtils;
2017-02-06 23:02:06 +01:00
import android.widget.Toast;
2017-02-06 19:01:32 +01:00
2017-07-20 18:46:13 +02:00
import com.topjohnwu.magisk.database.RepoDatabaseHelper;
2017-05-31 18:19:52 +02:00
import com.topjohnwu.magisk.database.SuDatabaseHelper;
2017-02-06 19:01:32 +01:00
import com.topjohnwu.magisk.module.Module;
import com.topjohnwu.magisk.module.Repo;
2017-02-06 21:09:49 +01:00
import com.topjohnwu.magisk.utils.CallbackEvent;
2017-05-19 21:04:14 +02:00
import com.topjohnwu.magisk.utils.SafetyNetHelper;
2017-02-06 19:01:32 +01:00
import com.topjohnwu.magisk.utils.Shell;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.magisk.utils.ValueSortedMap;
import java.io.File;
import java.util.List;
public class MagiskManager extends Application {
public static final String MAGISK_DISABLE_FILE = "/cache/.disable_magisk";
2017-02-12 12:49:46 +01:00
public static final String TMP_FOLDER_PATH = "/dev/tmp";
2017-02-12 13:53:41 +01:00
public static final String MAGISK_PATH = "/magisk";
2017-05-21 06:16:38 +02:00
public static final String UNINSTALLER = "magisk_uninstaller.sh";
2017-07-10 18:50:27 +02:00
public static final String UTIL_FUNCTIONS= "util_functions.sh";
public static final String INTENT_SECTION = "section";
2017-07-19 19:44:32 +02:00
public static final String INTENT_VERSION = "version";
public static final String INTENT_LINK = "link";
public static final String BUSYBOX_VERSION = "1.26.2";
2017-05-26 20:41:24 +02:00
public static final String MAGISKHIDE_PROP = "persist.magisk.hide";
public static final String DISABLE_INDICATION_PROP = "ro.magisk.disable";
2017-07-13 20:27:02 +02:00
public static final String NOTIFICATION_CHANNEL = "magisk_update_notice";
2017-02-06 19:01:32 +01:00
// Events
public final CallbackEvent<Void> magiskHideDone = new CallbackEvent<>();
2017-02-06 21:09:49 +01:00
public final CallbackEvent<Void> reloadMainActivity = new CallbackEvent<>();
public final CallbackEvent<Void> moduleLoadDone = new CallbackEvent<>();
public final CallbackEvent<Void> repoLoadDone = new CallbackEvent<>();
public final CallbackEvent<Void> updateCheckDone = new CallbackEvent<>();
public final CallbackEvent<Void> safetyNetDone = new CallbackEvent<>();
2017-02-06 19:01:32 +01:00
// Info
public String magiskVersionString;
2017-05-11 20:25:07 +02:00
public int magiskVersionCode = -1;
public String remoteMagiskVersionString;
public int remoteMagiskVersionCode = -1;
2017-02-06 19:01:32 +01:00
public String magiskLink;
public String releaseNoteLink;
2017-06-06 20:19:23 +02:00
public String remoteManagerVersionString;
public int remoteManagerVersionCode = -1;
public String managerLink;
2017-05-19 21:04:14 +02:00
public SafetyNetHelper.Result SNCheckResult;
2017-02-06 19:01:32 +01:00
public String bootBlock = null;
public boolean isSuClient = false;
public String suVersion = null;
public boolean disabled;
2017-02-06 19:01:32 +01:00
// Data
public ValueSortedMap<String, Repo> repoMap;
public ValueSortedMap<String, Module> moduleMap;
public List<String> blockList;
public List<ApplicationInfo> appList;
public List<String> magiskHideList;
// Configurations
public static boolean shellLogging;
public static boolean devLogging;
2017-02-07 00:32:40 +01:00
public boolean magiskHide;
2017-02-06 19:01:32 +01:00
public boolean isDarkTheme;
2017-02-21 21:58:03 +01:00
public boolean updateNotification;
2017-05-31 10:31:33 +02:00
public boolean suReauth;
2017-02-06 19:01:32 +01:00
public int suRequestTimeout;
public int suLogTimeout = 14;
public int suAccessState;
2017-05-26 20:41:24 +02:00
public int multiuserMode;
2017-02-06 19:01:32 +01:00
public int suResponseType;
public int suNotificationType;
2017-06-08 16:27:24 +02:00
public int suNamespaceMode;
2017-02-06 19:01:32 +01:00
2017-05-31 18:19:52 +02:00
// Global resources
2017-02-06 19:01:32 +01:00
public SharedPreferences prefs;
2017-05-31 18:19:52 +02:00
public SuDatabaseHelper suDB;
2017-07-20 18:46:13 +02:00
public RepoDatabaseHelper repoDB;
2017-07-17 21:34:06 +02:00
public Shell shell;
2017-02-06 19:01:32 +01:00
2017-02-06 23:02:06 +01:00
private static Handler mHandler = new Handler();
2017-02-06 19:01:32 +01:00
@Override
public void onCreate() {
super.onCreate();
2017-07-15 19:20:39 +02:00
new File(getApplicationInfo().dataDir).mkdirs(); /* Create the app data directory */
2017-02-06 19:01:32 +01:00
prefs = PreferenceManager.getDefaultSharedPreferences(this);
2017-07-17 21:34:06 +02:00
shell = Shell.getShell();
2017-07-20 18:46:13 +02:00
suDB = new SuDatabaseHelper(this);
repoDB = new RepoDatabaseHelper(this);
2017-02-06 19:01:32 +01:00
}
2017-02-06 23:02:06 +01: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-06 19:01:32 +01:00
public void init() {
isDarkTheme = prefs.getBoolean("dark_theme", false);
2017-05-11 20:25:07 +02:00
if (BuildConfig.DEBUG) {
devLogging = prefs.getBoolean("developer_logging", false);
shellLogging = prefs.getBoolean("shell_logging", false);
} else {
devLogging = false;
shellLogging = false;
}
2017-07-01 11:38:33 +02:00
magiskHide = prefs.getBoolean("magiskhide", true);
initSU();
2017-02-06 19:01:32 +01:00
updateMagiskInfo();
2017-07-17 21:34:06 +02:00
updateBlockInfo();
// Initialize busybox
File busybox = new File(getApplicationInfo().dataDir + "/busybox/busybox");
if (!busybox.exists() || !TextUtils.equals(prefs.getString("busybox_version", ""), BUSYBOX_VERSION)) {
busybox.getParentFile().mkdirs();
2017-07-17 21:34:06 +02:00
shell.su_raw(
"cp -f " + new File(getApplicationInfo().nativeLibraryDir, "libbusybox.so") + " " + busybox,
"chmod -R 755 " + busybox.getParent(),
busybox + " --install -s " + busybox.getParent()
);
}
2017-02-06 19:01:32 +01:00
// Initialize prefs
prefs.edit()
.putBoolean("dark_theme", isDarkTheme)
.putBoolean("magiskhide", magiskHide)
2017-02-21 21:58:03 +01:00
.putBoolean("notification", updateNotification)
2017-02-06 19:01:32 +01:00
.putBoolean("hosts", new File("/magisk/.core/hosts").exists())
2017-07-17 21:34:06 +02:00
.putBoolean("disable", Utils.itemExist(shell, MAGISK_DISABLE_FILE))
2017-05-31 10:31:33 +02:00
.putBoolean("su_reauth", suReauth)
2017-02-06 19:01:32 +01: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-26 20:41:24 +02:00
.putString("multiuser_mode", String.valueOf(multiuserMode))
2017-06-08 16:27:24 +02:00
.putString("mnt_ns", String.valueOf(suNamespaceMode))
.putString("busybox_version", BUSYBOX_VERSION)
2017-02-06 19:01:32 +01:00
.apply();
// Add busybox to PATH
2017-07-17 21:34:06 +02:00
shell.su_raw("PATH=$PATH:" + busybox.getParent());
2017-07-13 20:27:02 +02: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-06 19:01:32 +01:00
}
2017-05-31 10:31:33 +02: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 10:31:33 +02:00
initSUConfig();
2017-02-06 19:01:32 +01:00
2017-07-17 21:34:06 +02:00
List<String> ret = shell.sh("su -v");
2017-02-06 19:01:32 +01:00
if (Utils.isValidShellResponse(ret)) {
suVersion = ret.get(0);
isSuClient = suVersion.toUpperCase().contains("MAGISK");
}
if (isSuClient) {
2017-05-31 21:18:41 +02:00
suAccessState = suDB.getSettings(SuDatabaseHelper.ROOT_ACCESS, 3);
multiuserMode = suDB.getSettings(SuDatabaseHelper.MULTIUSER_MODE, 0);
2017-06-08 16:27:24 +02:00
suNamespaceMode = suDB.getSettings(SuDatabaseHelper.MNT_NS, 1);
2017-02-06 19:01:32 +01:00
}
}
public void updateMagiskInfo() {
2017-07-19 10:10:17 +02:00
updateNotification = prefs.getBoolean("notification", true);
2017-05-11 20:25:07 +02:00
List<String> ret;
2017-07-17 21:34:06 +02:00
ret = shell.sh("magisk -v");
2017-02-06 19:01:32 +01:00
if (!Utils.isValidShellResponse(ret)) {
2017-07-17 21:34:06 +02:00
ret = shell.sh("getprop magisk.version");
2017-05-11 20:25:07 +02:00
if (Utils.isValidShellResponse(ret)) {
try {
magiskVersionString = ret.get(0);
magiskVersionCode = (int) Double.parseDouble(ret.get(0)) * 10;
} catch (NumberFormatException ignored) {}
}
2017-02-06 19:01:32 +01:00
} else {
2017-05-11 20:25:07 +02:00
magiskVersionString = ret.get(0).split(":")[0];
2017-07-17 21:34:06 +02:00
ret = shell.sh("magisk -V");
2017-02-06 19:01:32 +01:00
try {
2017-05-11 20:25:07 +02:00
magiskVersionCode = Integer.parseInt(ret.get(0));
} catch (NumberFormatException ignored) {}
2017-02-06 19:01:32 +01:00
}
2017-07-17 21:34:06 +02:00
ret = shell.sh("getprop " + DISABLE_INDICATION_PROP);
2017-02-06 19:01:32 +01:00
try {
disabled = Utils.isValidShellResponse(ret) && Integer.parseInt(ret.get(0)) != 0;
} catch (NumberFormatException e) {
disabled = false;
}
2017-07-17 21:34:06 +02:00
ret = shell.sh("getprop " + MAGISKHIDE_PROP);
try {
2017-07-01 11:38:33 +02:00
magiskHide = !Utils.isValidShellResponse(ret) || Integer.parseInt(ret.get(0)) != 0;
} catch (NumberFormatException e) {
2017-07-01 11:38:33 +02:00
magiskHide = true;
}
2017-07-17 21:34:06 +02: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-06 19:01:32 +01:00
}
}