From ea884e7fa1c141db4e6679c443ca4386f3496ccb Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 12 Aug 2017 01:31:34 +0800 Subject: [PATCH] Re-organize application startup --- .../com/topjohnwu/magisk/MagiskFragment.java | 2 +- .../com/topjohnwu/magisk/MagiskManager.java | 87 +++++++++---------- .../magisk/receivers/BootReceiver.java | 2 - .../magisk/receivers/PackageReceiver.java | 1 - .../magisk/services/UpdateCheckService.java | 2 +- .../magisk/superuser/RequestActivity.java | 1 - .../magisk/superuser/SuReceiver.java | 1 - .../com/topjohnwu/magisk/utils/Shell.java | 2 +- build.gradle | 2 +- 9 files changed, 47 insertions(+), 53 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java b/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java index c94e1fd3b..b2a64f443 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java @@ -260,6 +260,7 @@ public class MagiskFragment extends Fragment @Override public void onRefresh() { + magiskManager.getMagiskInfo(); updateUI(); magiskUpdateText.setText(R.string.checking_for_updates); @@ -304,7 +305,6 @@ public class MagiskFragment extends Fragment private void updateUI() { ((MainActivity) getActivity()).checkHideSection(); - magiskManager.updateMagiskInfo(); final int ROOT = 0x1, NETWORK = 0x2, UPTODATE = 0x4; int status = 0; diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java b/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java index c56f2364a..1ca581d56 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java @@ -117,6 +117,16 @@ public class MagiskManager extends Application { } } + @Override + public void onCreate() { + super.onCreate(); + new File(getApplicationInfo().dataDir).mkdirs(); /* Create the app data directory */ + prefs = PreferenceManager.getDefaultSharedPreferences(this); + suDB = new SuDatabaseHelper(this); + repoDB = new RepoDatabaseHelper(this); + loadConfig(); + } + public void setLocale() { String localeTag = prefs.getString("locale", ""); if (localeTag.isEmpty()) { @@ -130,17 +140,31 @@ public class MagiskManager extends Application { res.updateConfiguration(config, res.getDisplayMetrics()); } - @Override - public void onCreate() { - super.onCreate(); - new File(getApplicationInfo().dataDir).mkdirs(); /* Create the app data directory */ - prefs = PreferenceManager.getDefaultSharedPreferences(this); - shell = Shell.getShell(); - suDB = new SuDatabaseHelper(this); - repoDB = new RepoDatabaseHelper(this); + private void loadConfig() { + // Locale defaultLocale = Locale.getDefault(); setLocale(); new LoadLocale(this).exec(); + + isDarkTheme = prefs.getBoolean("dark_theme", false); + if (BuildConfig.DEBUG) { + devLogging = prefs.getBoolean("developer_logging", false); + shellLogging = prefs.getBoolean("shell_logging", false); + } else { + devLogging = false; + shellLogging = false; + } + + // su + 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); + suAccessState = suDB.getSettings(SuDatabaseHelper.ROOT_ACCESS, 3); + multiuserMode = suDB.getSettings(SuDatabaseHelper.MULTIUSER_MODE, 0); + suNamespaceMode = suDB.getSettings(SuDatabaseHelper.MNT_NS, 1); + + updateNotification = prefs.getBoolean("notification", true); } public void toast(String msg, int duration) { @@ -152,18 +176,10 @@ public class MagiskManager extends Application { } public void init() { - isDarkTheme = prefs.getBoolean("dark_theme", false); - if (BuildConfig.DEBUG) { - devLogging = prefs.getBoolean("developer_logging", false); - shellLogging = prefs.getBoolean("shell_logging", false); - } else { - devLogging = false; - shellLogging = false; - } - initSU(); - updateMagiskInfo(); + getMagiskInfo(); updateBlockInfo(); - // Initialize prefs + + // Write back default values prefs.edit() .putBoolean("dark_theme", isDarkTheme) .putBoolean("magiskhide", magiskHide) @@ -192,31 +208,14 @@ public class MagiskManager extends Application { } - 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() { - initSUConfig(); - - List ret = shell.sh("su -v"); + public void getMagiskInfo() { + Shell.getShell(this); + List ret; + ret = shell.sh("su -v"); if (Utils.isValidShellResponse(ret)) { suVersion = ret.get(0); isSuClient = suVersion.toUpperCase().contains("MAGISK"); } - if (isSuClient) { - suAccessState = suDB.getSettings(SuDatabaseHelper.ROOT_ACCESS, 3); - multiuserMode = suDB.getSettings(SuDatabaseHelper.MULTIUSER_MODE, 0); - suNamespaceMode = suDB.getSettings(SuDatabaseHelper.MNT_NS, 1); - } - } - - public void updateMagiskInfo() { - updateNotification = prefs.getBoolean("notification", true); - List ret; ret = shell.sh("magisk -v"); if (!Utils.isValidShellResponse(ret)) { ret = shell.sh("getprop magisk.version"); @@ -247,11 +246,11 @@ public class MagiskManager extends Application { } } - public void updateBlockInfo() { + private void updateBlockInfo() { List 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", + "for BLOCK in boot_a kern-a android_boot kernel boot lnx; do", + " BOOTIMAGE=`find /dev/block -iname $BLOCK | head -n 1` 2>/dev/null", + " [ ! -z $BOOTIMAGE ] && break", "done", "[ ! -z \"$BOOTIMAGE\" -a -L \"$BOOTIMAGE\" ] && BOOTIMAGE=`readlink $BOOTIMAGE`", "echo \"$BOOTIMAGE\"" diff --git a/app/src/main/java/com/topjohnwu/magisk/receivers/BootReceiver.java b/app/src/main/java/com/topjohnwu/magisk/receivers/BootReceiver.java index 81a754af6..183f2f34c 100644 --- a/app/src/main/java/com/topjohnwu/magisk/receivers/BootReceiver.java +++ b/app/src/main/java/com/topjohnwu/magisk/receivers/BootReceiver.java @@ -6,7 +6,6 @@ import android.content.Intent; import android.os.Build; import com.topjohnwu.magisk.services.OnBootIntentService; -import com.topjohnwu.magisk.utils.Utils; public class BootReceiver extends BroadcastReceiver { @@ -21,7 +20,6 @@ public class BootReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { - Utils.getMagiskManager(context).initSU(); // There is currently no need to start an IntentService onBoot // startIntentService(context); } diff --git a/app/src/main/java/com/topjohnwu/magisk/receivers/PackageReceiver.java b/app/src/main/java/com/topjohnwu/magisk/receivers/PackageReceiver.java index b9501db43..4b5e829d2 100644 --- a/app/src/main/java/com/topjohnwu/magisk/receivers/PackageReceiver.java +++ b/app/src/main/java/com/topjohnwu/magisk/receivers/PackageReceiver.java @@ -12,7 +12,6 @@ public class PackageReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { MagiskManager magiskManager = Utils.getMagiskManager(context); - magiskManager.initSUConfig(); String pkg = intent.getData().getEncodedSchemeSpecificPart(); Policy policy = magiskManager.suDB.getPolicy(pkg); diff --git a/app/src/main/java/com/topjohnwu/magisk/services/UpdateCheckService.java b/app/src/main/java/com/topjohnwu/magisk/services/UpdateCheckService.java index 07bd80a9d..b67aae8ca 100644 --- a/app/src/main/java/com/topjohnwu/magisk/services/UpdateCheckService.java +++ b/app/src/main/java/com/topjohnwu/magisk/services/UpdateCheckService.java @@ -10,7 +10,7 @@ public class UpdateCheckService extends JobService { @Override public boolean onStartJob(JobParameters params) { - Utils.getMagiskManager(this).updateMagiskInfo(); + Utils.getMagiskManager(this).getMagiskInfo(); new CheckUpdates(this, true) .setCallBack(() -> jobFinished(params, false)).exec(); return true; diff --git a/app/src/main/java/com/topjohnwu/magisk/superuser/RequestActivity.java b/app/src/main/java/com/topjohnwu/magisk/superuser/RequestActivity.java index c8bc6a578..2da12825c 100644 --- a/app/src/main/java/com/topjohnwu/magisk/superuser/RequestActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/superuser/RequestActivity.java @@ -17,7 +17,6 @@ public class RequestActivity extends Activity { return; } - getApplicationContext().initSUConfig(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).setClass(this, SuRequestActivity.class); startActivity(intent); finish(); diff --git a/app/src/main/java/com/topjohnwu/magisk/superuser/SuReceiver.java b/app/src/main/java/com/topjohnwu/magisk/superuser/SuReceiver.java index cbbf8470d..150f00a2e 100644 --- a/app/src/main/java/com/topjohnwu/magisk/superuser/SuReceiver.java +++ b/app/src/main/java/com/topjohnwu/magisk/superuser/SuReceiver.java @@ -27,7 +27,6 @@ public class SuReceiver extends BroadcastReceiver { Policy policy; MagiskManager magiskManager = (MagiskManager) context.getApplicationContext(); - magiskManager.initSUConfig(); if (intent == null) return; diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Shell.java b/app/src/main/java/com/topjohnwu/magisk/utils/Shell.java index 87cbb2031..24ed952d7 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Shell.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Shell.java @@ -91,7 +91,7 @@ public class Shell { public static Shell getShell(Context context) { MagiskManager magiskManager = Utils.getMagiskManager(context); - if (!magiskManager.shell.isValid) { + if (magiskManager.shell == null || !magiskManager.shell.isValid) { // Get new shell if needed magiskManager.shell = getShell(); } diff --git a/build.gradle b/build.gradle index 43fe7ffe2..82c7e4ce7 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ buildscript { maven { url "https://maven.google.com" } } dependencies { - classpath 'com.android.tools.build:gradle:3.0.0-alpha9' + classpath 'com.android.tools.build:gradle:3.0.0-beta1' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files