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

201 lines
7.2 KiB
Java
Raw Normal View History

2017-02-07 02:01:32 +08:00
package com.topjohnwu.magisk;
import android.app.Application;
import android.content.SharedPreferences;
2017-10-21 22:54:47 +08:00
import android.content.pm.PackageManager;
2017-07-22 22:14:02 +08:00
import android.content.res.Configuration;
import android.content.res.Resources;
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-10-07 17:12:36 +08:00
import com.topjohnwu.magisk.container.Module;
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-11-06 04:41:23 +08:00
import com.topjohnwu.magisk.utils.Const;
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;
2017-10-16 00:54:48 +08:00
import java.lang.ref.WeakReference;
2017-02-07 02:01:32 +08:00
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 {
2017-10-16 00:54:48 +08:00
// Global weak reference to self
private static WeakReference<MagiskManager> weakSelf;
// 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
2017-11-06 04:47:24 +08:00
public boolean hasInit = false;
2017-10-21 22:54:47 +08:00
public int userId;
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-02-07 02:01:32 +08:00
public String bootBlock = null;
2017-10-20 00:41:44 +08:00
public int snet_version;
public int updateServiceVersion;
2017-02-07 02:01:32 +08:00
// Data
public Map<String, Module> moduleMap;
2017-07-22 22:14:02 +08:00
public List<Locale> locales;
2017-02-07 02:01:32 +08:00
// Configurations
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-11-17 12:36:19 +08:00
public boolean coreOnly;
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-08-27 01:38:05 +08:00
public String localeConfig;
2017-08-29 01:34:42 +08:00
public int updateChannel;
2017-09-03 17:24:05 +08:00
public String bootFormat;
2017-11-20 03:09:08 +08:00
public String customChannelUrl;
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-11-06 05:36:20 +08:00
public Runnable permissionGrantCallback = null;
2017-02-07 02:01:32 +08:00
2017-02-07 06:02:06 +08:00
private static Handler mHandler = new Handler();
2017-10-16 11:51:34 +08:00
public MagiskManager() {
weakSelf = new WeakReference<>(this);
}
2017-08-12 01:31:34 +08:00
@Override
public void onCreate() {
super.onCreate();
prefs = PreferenceManager.getDefaultSharedPreferences(this);
2017-10-21 22:54:47 +08:00
userId = getApplicationInfo().uid / 100000;
2017-09-15 18:03:25 +08:00
2017-10-20 00:41:44 +08:00
if (Utils.getDatabasePath(this, SuDatabaseHelper.DB_NAME).exists()) {
2017-09-15 18:03:25 +08:00
// Don't migrate yet, wait and check Magisk version
suDB = new SuDatabaseHelper(this);
} else {
2017-10-20 00:41:44 +08:00
suDB = new SuDatabaseHelper();
2017-09-15 18:03:25 +08:00
}
2017-10-21 22:54:47 +08:00
// If detect original package, self destruct!
2017-11-06 04:41:23 +08:00
if (!getPackageName().equals(Const.ORIG_PKG_NAME)) {
2017-10-21 22:54:47 +08:00
try {
2017-11-06 04:41:23 +08:00
getPackageManager().getApplicationInfo(Const.ORIG_PKG_NAME, 0);
2017-10-21 22:54:47 +08:00
Shell.su(String.format(Locale.US, "pm uninstall --user %d %s", userId, getPackageName()));
return;
} catch (PackageManager.NameNotFoundException ignored) { /* Expected*/ }
}
2017-08-12 01:31:34 +08:00
repoDB = new RepoDatabaseHelper(this);
2017-09-03 21:12:09 +08:00
defaultLocale = Locale.getDefault();
setLocale();
2017-08-12 01:31:34 +08:00
loadConfig();
}
2017-10-16 00:54:48 +08:00
public static MagiskManager get() {
return weakSelf.get();
}
2017-07-22 22:14:02 +08:00
public void setLocale() {
2017-11-06 04:41:23 +08:00
localeConfig = prefs.getString(Const.Key.LOCALE, "");
2017-08-27 01:38:05 +08:00
if (localeConfig.isEmpty()) {
2017-07-22 22:14:02 +08:00
locale = defaultLocale;
} else {
2017-08-27 01:38:05 +08:00
locale = Locale.forLanguageTag(localeConfig);
2017-07-22 22:14:02 +08:00
}
Resources res = getBaseContext().getResources();
Configuration config = new Configuration(res.getConfiguration());
config.setLocale(locale);
res.updateConfiguration(config, res.getDisplayMetrics());
}
2017-09-03 15:35:14 +08:00
public void loadConfig() {
2017-11-06 04:41:23 +08:00
isDarkTheme = prefs.getBoolean(Const.Key.DARK_THEME, false);
2017-08-12 01:31:34 +08:00
// su
2017-11-06 04:41:23 +08:00
suRequestTimeout = Utils.getPrefsInt(prefs, Const.Key.SU_REQUEST_TIMEOUT, Const.Value.timeoutList[2]);
suResponseType = Utils.getPrefsInt(prefs, Const.Key.SU_AUTO_RESPONSE, Const.Value.SU_PROMPT);
suNotificationType = Utils.getPrefsInt(prefs, Const.Key.SU_NOTIFICATION, Const.Value.NOTIFICATION_TOAST);
suReauth = prefs.getBoolean(Const.Key.SU_REAUTH, false);
suAccessState = suDB.getSettings(Const.Key.ROOT_ACCESS, Const.Value.ROOT_ACCESS_APPS_AND_ADB);
multiuserMode = suDB.getSettings(Const.Key.SU_MULTIUSER_MODE, Const.Value.MULTIUSER_MODE_OWNER_ONLY);
suNamespaceMode = suDB.getSettings(Const.Key.SU_MNT_NS, Const.Value.NAMESPACE_MODE_REQUESTER);
2017-11-17 12:36:19 +08:00
coreOnly = prefs.getBoolean(Const.Key.DISABLE, false);
2017-11-06 04:41:23 +08:00
updateNotification = prefs.getBoolean(Const.Key.UPDATE_NOTIFICATION, true);
updateChannel = Utils.getPrefsInt(prefs, Const.Key.UPDATE_CHANNEL, Const.Value.STABLE_CHANNEL);
bootFormat = prefs.getString(Const.Key.BOOT_FORMAT, ".img");
snet_version = prefs.getInt(Const.Key.SNET_VER, -1);
updateServiceVersion = prefs.getInt(Const.Key.UPDATE_SERVICE_VER, -1);
2017-11-20 03:09:08 +08:00
customChannelUrl = prefs.getString(Const.Key.CUSTOM_CHANNEL, "");
2017-02-07 02:01:32 +08:00
}
2017-10-16 00:54:48 +08:00
public static void toast(String msg, int duration) {
mHandler.post(() -> Toast.makeText(weakSelf.get(), msg, duration).show());
2017-02-07 06:02:06 +08:00
}
2017-10-16 00:54:48 +08:00
public static void toast(int resId, int duration) {
mHandler.post(() -> Toast.makeText(weakSelf.get(), resId, duration).show());
2017-02-07 06:02:06 +08:00
}
2017-11-06 04:47:24 +08:00
public void loadMagiskInfo() {
2017-08-12 01:31:34 +08:00
List<String> ret;
2017-10-15 23:02:44 +08:00
ret = Shell.sh("magisk -v");
2017-02-07 02:01:32 +08:00
if (!Utils.isValidShellResponse(ret)) {
2017-10-15 23:02:44 +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-10-15 23:02:44 +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-10-15 03:12:13 +08:00
if (magiskVersionCode > 1435) {
2017-11-06 04:41:23 +08:00
ret = Shell.su("resetprop -p " + Const.MAGISKHIDE_PROP);
2017-10-15 03:12:13 +08:00
} else {
2017-11-06 04:41:23 +08:00
ret = Shell.sh("getprop " + Const.MAGISKHIDE_PROP);
2017-10-15 03:12:13 +08:00
}
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
}
2017-11-06 05:36:20 +08:00
public void setPermissionGrantCallback(Runnable callback) {
permissionGrantCallback = callback;
}
2017-02-07 02:01:32 +08:00
}