From 914b7ee056d6e0ca11f08f2f457498b3e902f8a9 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Tue, 31 Jul 2018 03:51:11 +0800 Subject: [PATCH] Start moving things outside of top Application class --- .../com/topjohnwu/magisk/FlashActivity.java | 2 +- .../java/com/topjohnwu/magisk/Global.java | 58 +++++++++++++++ .../com/topjohnwu/magisk/MagiskFragment.java | 36 ++++----- .../com/topjohnwu/magisk/MagiskManager.java | 73 +------------------ .../com/topjohnwu/magisk/MainActivity.java | 6 +- .../topjohnwu/magisk/SettingsActivity.java | 12 +-- .../com/topjohnwu/magisk/SplashActivity.java | 12 +-- .../magisk/adapters/ApplicationAdapter.java | 6 +- .../magisk/asyncs/CheckSafetyNet.java | 8 +- .../topjohnwu/magisk/asyncs/CheckUpdates.java | 27 +++---- .../com/topjohnwu/magisk/asyncs/FlashZip.java | 3 +- .../topjohnwu/magisk/asyncs/HideManager.java | 5 +- .../magisk/asyncs/InstallMagisk.java | 15 ++-- .../topjohnwu/magisk/asyncs/LoadModules.java | 5 +- .../magisk/asyncs/MarkDownWindow.java | 3 +- .../magisk/asyncs/ProcessRepoZip.java | 4 +- .../magisk/asyncs/RestoreImages.java | 6 +- .../topjohnwu/magisk/asyncs/UpdateRepos.java | 3 +- .../components/InstallMethodDialog.java | 7 +- .../components/MagiskInstallDialog.java | 11 +-- .../components/ManagerInstallDialog.java | 9 ++- .../magisk/components/UninstallDialog.java | 5 +- .../magisk/database/MagiskDatabaseHelper.java | 14 ++-- .../magisk/database/RepoDatabaseHelper.java | 3 +- .../magisk/receivers/ShortcutReceiver.java | 15 ++-- .../magisk/services/UpdateCheckService.java | 7 +- .../magisk/superuser/SuReceiver.java | 5 +- .../com/topjohnwu/magisk/utils/Const.java | 6 +- .../magisk/utils/FingerprintHelper.java | 5 +- .../magisk/utils/NotificationMgr.java | 13 ++-- .../com/topjohnwu/magisk/utils/RootUtils.java | 36 ++++----- .../com/topjohnwu/magisk/utils/Utils.java | 31 +++++++- 32 files changed, 237 insertions(+), 214 deletions(-) create mode 100644 app/src/full/java/com/topjohnwu/magisk/Global.java diff --git a/app/src/full/java/com/topjohnwu/magisk/FlashActivity.java b/app/src/full/java/com/topjohnwu/magisk/FlashActivity.java index b967e54d1..a40d54524 100644 --- a/app/src/full/java/com/topjohnwu/magisk/FlashActivity.java +++ b/app/src/full/java/com/topjohnwu/magisk/FlashActivity.java @@ -75,7 +75,7 @@ public class FlashActivity extends Activity { e.printStackTrace(); return; } - MagiskManager.toast(logFile.getPath(), Toast.LENGTH_LONG); + Global.toast(logFile.getPath(), Toast.LENGTH_LONG); } @Override diff --git a/app/src/full/java/com/topjohnwu/magisk/Global.java b/app/src/full/java/com/topjohnwu/magisk/Global.java new file mode 100644 index 000000000..4e6c943fa --- /dev/null +++ b/app/src/full/java/com/topjohnwu/magisk/Global.java @@ -0,0 +1,58 @@ +package com.topjohnwu.magisk; + +import android.os.Handler; +import android.os.Looper; +import android.widget.Toast; + +import com.topjohnwu.magisk.utils.Const; +import com.topjohnwu.superuser.ShellUtils; + +import java.lang.ref.WeakReference; + +public class Global { + // Global app instance + public static WeakReference weakApp; + public static Handler mainHandler = new Handler(Looper.getMainLooper()); + + // Current status + public static String magiskVersionString; + public static int magiskVersionCode = -1; + public static boolean magiskHide; + + // Update Info + public static String remoteMagiskVersionString; + public static int remoteMagiskVersionCode = -1; + public static String magiskLink; + public static String magiskNoteLink; + public static String remoteManagerVersionString; + public static int remoteManagerVersionCode = -1; + public static String managerLink; + public static String managerNoteLink; + public static String uninstallerLink; + + // Install flags + public static boolean keepVerity = false; + public static boolean keepEnc = false; + + public static void loadMagiskInfo() { + try { + magiskVersionString = ShellUtils.fastCmd("magisk -v").split(":")[0]; + magiskVersionCode = Integer.parseInt(ShellUtils.fastCmd("magisk -V")); + String s = ShellUtils.fastCmd((magiskVersionCode >= Const.MAGISK_VER.RESETPROP_PERSIST ? + "resetprop -p " : "getprop ") + Const.MAGISKHIDE_PROP); + magiskHide = s.isEmpty() || Integer.parseInt(s) != 0; + } catch (NumberFormatException ignored) {} + } + + public static MagiskManager MM() { + return weakApp.get(); + } + + public static void toast(CharSequence msg, int duration) { + mainHandler.post(() -> Toast.makeText(MM(), msg, duration).show()); + } + + public static void toast(int resId, int duration) { + mainHandler.post(() -> Toast.makeText(MM(), resId, duration).show()); + } +} diff --git a/app/src/full/java/com/topjohnwu/magisk/MagiskFragment.java b/app/src/full/java/com/topjohnwu/magisk/MagiskFragment.java index ec416291e..b58e8f3ac 100644 --- a/app/src/full/java/com/topjohnwu/magisk/MagiskFragment.java +++ b/app/src/full/java/com/topjohnwu/magisk/MagiskFragment.java @@ -119,7 +119,7 @@ public class MagiskFragment extends Fragment shownDialog = true; // Show Manager update first - if (mm.remoteManagerVersionCode > BuildConfig.VERSION_CODE) { + if (Global.remoteManagerVersionCode > BuildConfig.VERSION_CODE) { new ManagerInstallDialog((Activity) requireActivity()).show(); return; } @@ -146,10 +146,10 @@ public class MagiskFragment extends Fragment expandableContainer.expandLayout = expandLayout; setupExpandable(); - keepVerityChkbox.setChecked(mm.keepVerity); - keepVerityChkbox.setOnCheckedChangeListener((view, checked) -> mm.keepVerity = checked); - keepEncChkbox.setChecked(mm.keepEnc); - keepEncChkbox.setOnCheckedChangeListener((view, checked) -> mm.keepEnc = checked); + keepVerityChkbox.setChecked(Global.keepVerity); + keepVerityChkbox.setOnCheckedChangeListener((view, checked) -> Global.keepVerity = checked); + keepEncChkbox.setChecked(Global.keepEnc); + keepEncChkbox.setOnCheckedChangeListener((view, checked) -> Global.keepEnc = checked); mSwipeRefreshLayout.setOnRefreshListener(this); updateUI(); @@ -159,7 +159,7 @@ public class MagiskFragment extends Fragment @Override public void onRefresh() { - mm.loadMagiskInfo(); + Global.loadMagiskInfo(); updateUI(); magiskUpdateText.setText(R.string.checking_for_updates); @@ -170,8 +170,8 @@ public class MagiskFragment extends Fragment mm.safetyNetDone.reset(); mm.updateCheckDone.reset(); - mm.remoteMagiskVersionString = null; - mm.remoteMagiskVersionCode = -1; + Global.remoteMagiskVersionString = null; + Global.remoteMagiskVersionCode = -1; collapse(); shownDialog = false; @@ -214,7 +214,7 @@ public class MagiskFragment extends Fragment boolean hasNetwork = Download.checkNetworkStatus(mm); boolean hasRoot = Shell.rootAccess(); - boolean isUpToDate = mm.magiskVersionCode > Const.MAGISK_VER.UNIFIED; + boolean isUpToDate = Global.magiskVersionCode > Const.MAGISK_VER.UNIFIED; magiskUpdate.setVisibility(hasNetwork ? View.VISIBLE : View.GONE); safetyNetCard.setVisibility(hasNetwork ? View.VISIBLE : View.GONE); @@ -223,14 +223,14 @@ public class MagiskFragment extends Fragment int image, color; - if (mm.magiskVersionCode < 0) { + if (Global.magiskVersionCode < 0) { color = colorBad; image = R.drawable.ic_cancel; magiskVersionText.setText(R.string.magisk_version_error); } else { color = colorOK; image = R.drawable.ic_check_circle; - magiskVersionText.setText(getString(R.string.current_magisk_title, "v" + mm.magiskVersionString)); + magiskVersionText.setText(getString(R.string.current_magisk_title, "v" + Global.magiskVersionString)); } magiskStatusIcon.setImageResource(image); @@ -240,7 +240,7 @@ public class MagiskFragment extends Fragment private void updateCheckUI() { int image, color; - if (mm.remoteMagiskVersionCode < 0) { + if (Global.remoteMagiskVersionCode < 0) { color = colorNeutral; image = R.drawable.ic_help; magiskUpdateText.setText(R.string.invalid_update_channel); @@ -248,11 +248,11 @@ public class MagiskFragment extends Fragment } else { color = colorOK; image = R.drawable.ic_check_circle; - magiskUpdateText.setText(getString(R.string.install_magisk_title, "v" + mm.remoteMagiskVersionString)); + magiskUpdateText.setText(getString(R.string.install_magisk_title, "v" + Global.remoteMagiskVersionString)); installButton.setVisibility(View.VISIBLE); - if (mm.remoteManagerVersionCode > BuildConfig.VERSION_CODE) { + if (Global.remoteManagerVersionCode > BuildConfig.VERSION_CODE) { installText.setText(getString(R.string.update, getString(R.string.app_name))); - } else if (mm.magiskVersionCode > 0 && mm.remoteMagiskVersionCode > mm.magiskVersionCode) { + } else if (Global.magiskVersionCode > 0 && Global.remoteMagiskVersionCode > Global.magiskVersionCode) { installText.setText(getString(R.string.update, getString(R.string.magisk))); } else { installText.setText(R.string.install); @@ -267,10 +267,10 @@ public class MagiskFragment extends Fragment mSwipeRefreshLayout.setRefreshing(false); if (!shownDialog) { - if (mm.remoteMagiskVersionCode > mm.magiskVersionCode - || mm.remoteManagerVersionCode > BuildConfig.VERSION_CODE) { + if (Global.remoteMagiskVersionCode > Global.magiskVersionCode + || Global.remoteManagerVersionCode > BuildConfig.VERSION_CODE) { install(); - } else if (mm.remoteMagiskVersionCode >= Const.MAGISK_VER.FIX_ENV && + } else if (Global.remoteMagiskVersionCode >= Const.MAGISK_VER.FIX_ENV && !ShellUtils.fastCmdResult("env_check")) { new EnvFixDialog(requireActivity()).show(); } diff --git a/app/src/full/java/com/topjohnwu/magisk/MagiskManager.java b/app/src/full/java/com/topjohnwu/magisk/MagiskManager.java index 0807a5b18..18a74463b 100644 --- a/app/src/full/java/com/topjohnwu/magisk/MagiskManager.java +++ b/app/src/full/java/com/topjohnwu/magisk/MagiskManager.java @@ -1,8 +1,5 @@ package com.topjohnwu.magisk; -import android.app.job.JobInfo; -import android.app.job.JobScheduler; -import android.content.ComponentName; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.content.res.Configuration; @@ -16,14 +13,12 @@ import android.widget.Toast; import com.topjohnwu.magisk.container.Module; import com.topjohnwu.magisk.database.MagiskDatabaseHelper; import com.topjohnwu.magisk.database.RepoDatabaseHelper; -import com.topjohnwu.magisk.services.UpdateCheckService; import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.RootUtils; import com.topjohnwu.magisk.utils.Topic; import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.superuser.ContainerApp; import com.topjohnwu.superuser.Shell; -import com.topjohnwu.superuser.ShellUtils; import com.topjohnwu.superuser.io.SuFile; import com.topjohnwu.superuser.io.SuFileInputStream; @@ -39,8 +34,6 @@ import java.util.Map; public class MagiskManager extends ContainerApp { - public static WeakReference weakSelf; - public static Handler mHandler = new Handler(); public static Locale locale; public static Locale defaultLocale; @@ -55,27 +48,11 @@ public class MagiskManager extends ContainerApp { // Info public boolean hasInit = false; - public String magiskVersionString; - public int magiskVersionCode = -1; - public String remoteMagiskVersionString; - public int remoteMagiskVersionCode = -1; - public String remoteManagerVersionString; - public int remoteManagerVersionCode = -1; - - public String magiskLink; - public String magiskNoteLink; - public String managerLink; - public String managerNoteLink; - public String uninstallerLink; - - public boolean keepVerity = false; - public boolean keepEnc = false; // Data public Map moduleMap; public List locales; - public boolean magiskHide; public boolean isDarkTheme; public int suRequestTimeout; public int suLogTimeout = 14; @@ -95,15 +72,7 @@ public class MagiskManager extends ContainerApp { public RepoDatabaseHelper repoDB; public MagiskManager() { - weakSelf = new WeakReference<>(this); - } - - public static void toast(CharSequence msg, int duration) { - mHandler.post(() -> Toast.makeText(weakSelf.get(), msg, duration).show()); - } - - public static void toast(int resId, int duration) { - mHandler.post(() -> Toast.makeText(weakSelf.get(), resId, duration).show()); + Global.weakApp = new WeakReference<>(this); } @Override @@ -135,10 +104,6 @@ public class MagiskManager extends ContainerApp { loadConfig(); } - public static MagiskManager get() { - return weakSelf.get(); - } - public void setLocale() { localeConfig = prefs.getString(Const.Key.LOCALE, ""); if (localeConfig.isEmpty()) { @@ -171,7 +136,7 @@ public class MagiskManager extends ContainerApp { public void writeConfig() { prefs.edit() .putBoolean(Const.Key.DARK_THEME, isDarkTheme) - .putBoolean(Const.Key.MAGISKHIDE, magiskHide) + .putBoolean(Const.Key.MAGISKHIDE, Global.magiskHide) .putBoolean(Const.Key.HOSTS, Const.MAGISK_HOST_FILE.exists()) .putBoolean(Const.Key.COREONLY, Const.MAGISK_DISABLE_FILE.exists()) .putString(Const.Key.SU_REQUEST_TIMEOUT, String.valueOf(suRequestTimeout)) @@ -188,40 +153,6 @@ public class MagiskManager extends ContainerApp { .apply(); } - public void loadMagiskInfo() { - try { - magiskVersionString = ShellUtils.fastCmd("magisk -v").split(":")[0]; - magiskVersionCode = Integer.parseInt(ShellUtils.fastCmd("magisk -V")); - String s = ShellUtils.fastCmd((magiskVersionCode >= Const.MAGISK_VER.RESETPROP_PERSIST ? - "resetprop -p " : "getprop ") + Const.MAGISKHIDE_PROP); - magiskHide = s.isEmpty() || Integer.parseInt(s) != 0; - } catch (Exception ignored) {} - } - - public void getDefaultInstallFlags() { - keepVerity = Boolean.parseBoolean(ShellUtils.fastCmd("echo $KEEPVERITY")); - keepEnc = Boolean.parseBoolean(ShellUtils.fastCmd("echo $KEEPFORCEENCRYPT")); - } - - public void setupUpdateCheck() { - JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE); - - if (prefs.getBoolean(Const.Key.CHECK_UPDATES, true)) { - if (scheduler.getAllPendingJobs().isEmpty() || - Const.UPDATE_SERVICE_VER > prefs.getInt(Const.Key.UPDATE_SERVICE_VER, -1)) { - ComponentName service = new ComponentName(this, UpdateCheckService.class); - JobInfo info = new JobInfo.Builder(Const.ID.UPDATE_SERVICE_ID, service) - .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) - .setPersisted(true) - .setPeriodic(8 * 60 * 60 * 1000) - .build(); - scheduler.schedule(info); - } - } else { - scheduler.cancel(Const.UPDATE_SERVICE_VER); - } - } - public void dumpPrefs() { // Flush prefs to disk prefs.edit().commit(); diff --git a/app/src/full/java/com/topjohnwu/magisk/MainActivity.java b/app/src/full/java/com/topjohnwu/magisk/MainActivity.java index 3a3e5024e..222400197 100644 --- a/app/src/full/java/com/topjohnwu/magisk/MainActivity.java +++ b/app/src/full/java/com/topjohnwu/magisk/MainActivity.java @@ -125,12 +125,12 @@ public class MainActivity extends Activity MagiskManager mm = getMagiskManager(); Menu menu = navigationView.getMenu(); menu.findItem(R.id.magiskhide).setVisible( - Shell.rootAccess() && mm.magiskVersionCode >= Const.MAGISK_VER.UNIFIED + Shell.rootAccess() && Global.magiskVersionCode >= Const.MAGISK_VER.UNIFIED && mm.prefs.getBoolean(Const.Key.MAGISKHIDE, false)); menu.findItem(R.id.modules).setVisible(!mm.prefs.getBoolean(Const.Key.COREONLY, false) && - Shell.rootAccess() && mm.magiskVersionCode >= 0); + Shell.rootAccess() && Global.magiskVersionCode >= 0); menu.findItem(R.id.downloads).setVisible(!mm.prefs.getBoolean(Const.Key.COREONLY, false) - && Download.checkNetworkStatus(this) && Shell.rootAccess() && mm.magiskVersionCode >= 0); + && Download.checkNetworkStatus(this) && Shell.rootAccess() && Global.magiskVersionCode >= 0); menu.findItem(R.id.log).setVisible(Shell.rootAccess()); menu.findItem(R.id.superuser).setVisible(Shell.rootAccess() && !(Const.USER_ID > 0 && mm.multiuserMode == Const.Value.MULTIUSER_MODE_OWNER_MANAGED)); diff --git a/app/src/full/java/com/topjohnwu/magisk/SettingsActivity.java b/app/src/full/java/com/topjohnwu/magisk/SettingsActivity.java index 8dd7deb5d..ea65238e9 100644 --- a/app/src/full/java/com/topjohnwu/magisk/SettingsActivity.java +++ b/app/src/full/java/com/topjohnwu/magisk/SettingsActivity.java @@ -108,7 +108,7 @@ public class SettingsActivity extends Activity implements Topic.Subscriber { findPreference("clear").setOnPreferenceClickListener((pref) -> { prefs.edit().remove(Const.Key.ETAG_KEY).apply(); mm.repoDB.clearRepo(); - MagiskManager.toast(R.string.repo_cache_cleared, Toast.LENGTH_SHORT); + Global.toast(R.string.repo_cache_cleared, Toast.LENGTH_SHORT); return true; }); @@ -159,7 +159,7 @@ public class SettingsActivity extends Activity implements Topic.Subscriber { fingerprint.setSummary(R.string.disable_fingerprint); } - if (mm.magiskVersionCode >= Const.MAGISK_VER.MANAGER_HIDE) { + if (Global.magiskVersionCode >= Const.MAGISK_VER.MANAGER_HIDE) { if (mm.getPackageName().equals(Const.ORIG_PKG_NAME)) { hideManager.setOnPreferenceClickListener((pref) -> { new HideManager(getActivity()).exec(); @@ -183,8 +183,8 @@ public class SettingsActivity extends Activity implements Topic.Subscriber { Shell.su("rm -f /data/local/tmp/manager.apk").exec(); } }, - mm.managerLink, - Utils.fmt("MagiskManager-v%s.apk", mm.remoteManagerVersionString) + Global.managerLink, + Utils.fmt("MagiskManager-v%s.apk", Global.remoteManagerVersionString) ); return true; }); @@ -206,7 +206,7 @@ public class SettingsActivity extends Activity implements Topic.Subscriber { if (!Shell.rootAccess()) { prefScreen.removePreference(magiskCategory); generalCatagory.removePreference(hideManager); - } else if (mm.magiskVersionCode < Const.MAGISK_VER.UNIFIED) { + } else if (Global.magiskVersionCode < Const.MAGISK_VER.UNIFIED) { prefScreen.removePreference(magiskCategory); } } @@ -289,7 +289,7 @@ public class SettingsActivity extends Activity implements Topic.Subscriber { new CheckUpdates().exec(); break; case Const.Key.CHECK_UPDATES: - mm.setupUpdateCheck(); + Utils.setupUpdateCheck(); break; } mm.loadConfig(); diff --git a/app/src/full/java/com/topjohnwu/magisk/SplashActivity.java b/app/src/full/java/com/topjohnwu/magisk/SplashActivity.java index 9d4423507..15a57aaec 100644 --- a/app/src/full/java/com/topjohnwu/magisk/SplashActivity.java +++ b/app/src/full/java/com/topjohnwu/magisk/SplashActivity.java @@ -25,10 +25,10 @@ public class SplashActivity extends Activity { super.onCreate(savedInstanceState); MagiskManager mm = getMagiskManager(); + // Force create a shell if not created yet + boolean root = Shell.rootAccess(); mm.repoDB = new RepoDatabaseHelper(this); - mm.loadMagiskInfo(); - mm.getDefaultInstallFlags(); mm.loadPrefs(); // Dynamic detect all locales @@ -54,9 +54,9 @@ public class SplashActivity extends Activity { } // Magisk working as expected - if (Shell.rootAccess() && mm.magiskVersionCode > 0) { + if (root && Global.magiskVersionCode > 0) { // Update check service - mm.setupUpdateCheck(); + Utils.setupUpdateCheck(); // Fire asynctasks loadModuleTask.exec(); } @@ -76,12 +76,12 @@ public class SplashActivity extends Activity { static class LoadLocale extends ParallelTask { @Override protected Void doInBackground(Void... voids) { - MagiskManager.get().locales = Utils.getAvailableLocale(); + Global.MM().locales = Utils.getAvailableLocale(); return null; } @Override protected void onPostExecute(Void aVoid) { - MagiskManager.get().localeDone.publish(); + Global.MM().localeDone.publish(); } } } diff --git a/app/src/full/java/com/topjohnwu/magisk/adapters/ApplicationAdapter.java b/app/src/full/java/com/topjohnwu/magisk/adapters/ApplicationAdapter.java index 1e730d0a9..d77b3922d 100644 --- a/app/src/full/java/com/topjohnwu/magisk/adapters/ApplicationAdapter.java +++ b/app/src/full/java/com/topjohnwu/magisk/adapters/ApplicationAdapter.java @@ -12,7 +12,7 @@ import android.widget.Filter; import android.widget.ImageView; import android.widget.TextView; -import com.topjohnwu.magisk.MagiskManager; +import com.topjohnwu.magisk.Global; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.asyncs.ParallelTask; import com.topjohnwu.magisk.utils.Const; @@ -37,7 +37,7 @@ public class ApplicationAdapter extends RecyclerView.Adapter { public static final File dexPath = - new File(MagiskManager.get().getFilesDir().getParent() + "/snet", "snet.apk"); + new File(Global.MM().getFilesDir().getParent() + "/snet", "snet.apk"); private ISafetyNetHelper helper; public CheckSafetyNet(Activity activity) { @@ -50,7 +50,7 @@ public class CheckSafetyNet extends ParallelTask { Class.class, String.class, Activity.class, Object.class) .invoke(null, ISafetyNetHelper.class, dexPath.getPath(), getActivity(), (ISafetyNetHelper.Callback) code -> - MagiskManager.get().safetyNetDone.publish(false, code)); + Global.MM().safetyNetDone.publish(false, code)); if (helper.getVersion() != Const.SNET_VER) { throw new Exception(); } @@ -79,7 +79,7 @@ public class CheckSafetyNet extends ParallelTask { helper.attest(); } else { e.printStackTrace(); - MagiskManager.get().safetyNetDone.publish(false, -1); + Global.MM().safetyNetDone.publish(false, -1); } super.onPostExecute(e); } diff --git a/app/src/full/java/com/topjohnwu/magisk/asyncs/CheckUpdates.java b/app/src/full/java/com/topjohnwu/magisk/asyncs/CheckUpdates.java index 17de3269c..621fa917f 100644 --- a/app/src/full/java/com/topjohnwu/magisk/asyncs/CheckUpdates.java +++ b/app/src/full/java/com/topjohnwu/magisk/asyncs/CheckUpdates.java @@ -1,6 +1,7 @@ package com.topjohnwu.magisk.asyncs; import com.topjohnwu.magisk.BuildConfig; +import com.topjohnwu.magisk.Global; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.NotificationMgr; @@ -51,7 +52,7 @@ public class CheckUpdates extends ParallelTask { @Override protected Void doInBackground(Void... voids) { - MagiskManager mm = MagiskManager.get(); + MagiskManager mm = Global.MM(); String jsonStr = ""; switch (mm.updateChannel) { case Const.Value.STABLE_CHANNEL: @@ -73,30 +74,30 @@ public class CheckUpdates extends ParallelTask { } JSONObject magisk = getJson(json, "magisk"); - mm.remoteMagiskVersionString = getString(magisk, "version", null); - mm.remoteMagiskVersionCode = getInt(magisk, "versionCode", -1); - mm.magiskLink = getString(magisk, "link", null); - mm.magiskNoteLink = getString(magisk, "note", null); + Global.remoteMagiskVersionString = getString(magisk, "version", null); + Global.remoteMagiskVersionCode = getInt(magisk, "versionCode", -1); + Global.magiskLink = getString(magisk, "link", null); + Global.magiskNoteLink = getString(magisk, "note", null); JSONObject manager = getJson(json, "app"); - mm.remoteManagerVersionString = getString(manager, "version", null); - mm.remoteManagerVersionCode = getInt(manager, "versionCode", -1); - mm.managerLink = getString(manager, "link", null); - mm.managerNoteLink = getString(manager, "note", null); + Global.remoteManagerVersionString = getString(manager, "version", null); + Global.remoteManagerVersionCode = getInt(manager, "versionCode", -1); + Global.managerLink = getString(manager, "link", null); + Global.managerNoteLink = getString(manager, "note", null); JSONObject uninstaller = getJson(json, "uninstaller"); - mm.uninstallerLink = getString(uninstaller, "link", null); + Global.uninstallerLink = getString(uninstaller, "link", null); return null; } @Override protected void onPostExecute(Void v) { - MagiskManager mm = MagiskManager.get(); + MagiskManager mm = Global.MM(); if (showNotification) { - if (BuildConfig.VERSION_CODE < mm.remoteManagerVersionCode) { + if (BuildConfig.VERSION_CODE < Global.remoteManagerVersionCode) { NotificationMgr.managerUpdate(); - } else if (mm.magiskVersionCode < mm.remoteMagiskVersionCode) { + } else if (Global.magiskVersionCode < Global.remoteMagiskVersionCode) { NotificationMgr.magiskUpdate(); } } diff --git a/app/src/full/java/com/topjohnwu/magisk/asyncs/FlashZip.java b/app/src/full/java/com/topjohnwu/magisk/asyncs/FlashZip.java index 58e4b4d05..56a9ff909 100644 --- a/app/src/full/java/com/topjohnwu/magisk/asyncs/FlashZip.java +++ b/app/src/full/java/com/topjohnwu/magisk/asyncs/FlashZip.java @@ -5,6 +5,7 @@ import android.net.Uri; import android.view.View; import com.topjohnwu.magisk.FlashActivity; +import com.topjohnwu.magisk.Global; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.components.SnackbarMaker; import com.topjohnwu.magisk.utils.Const; @@ -44,7 +45,7 @@ public class FlashZip extends ParallelTask { @Override protected Integer doInBackground(Void... voids) { - MagiskManager mm = MagiskManager.get(); + MagiskManager mm = Global.MM(); try { console.add("- Copying zip to temp directory"); diff --git a/app/src/full/java/com/topjohnwu/magisk/asyncs/HideManager.java b/app/src/full/java/com/topjohnwu/magisk/asyncs/HideManager.java index 92c7bf8a6..6f202a078 100644 --- a/app/src/full/java/com/topjohnwu/magisk/asyncs/HideManager.java +++ b/app/src/full/java/com/topjohnwu/magisk/asyncs/HideManager.java @@ -4,6 +4,7 @@ import android.app.Activity; import android.app.ProgressDialog; import android.widget.Toast; +import com.topjohnwu.magisk.Global; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.utils.Const; @@ -55,7 +56,7 @@ public class HideManager extends ParallelTask { @Override protected Boolean doInBackground(Void... voids) { - MagiskManager mm = MagiskManager.get(); + MagiskManager mm = Global.MM(); // Generate a new app with random package name SuFile repack = new SuFile("/data/local/tmp/repack.apk"); @@ -88,7 +89,7 @@ public class HideManager extends ParallelTask { protected void onPostExecute(Boolean b) { dialog.dismiss(); if (!b) { - MagiskManager.toast(R.string.hide_manager_fail_toast, Toast.LENGTH_LONG); + Global.toast(R.string.hide_manager_fail_toast, Toast.LENGTH_LONG); } super.onPostExecute(b); } diff --git a/app/src/full/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java b/app/src/full/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java index 06b743b25..d53d99cb5 100644 --- a/app/src/full/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java +++ b/app/src/full/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java @@ -9,6 +9,7 @@ import android.text.TextUtils; import android.view.View; import android.widget.Toast; +import com.topjohnwu.magisk.Global; import com.topjohnwu.magisk.FlashActivity; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; @@ -59,7 +60,7 @@ public class InstallMagisk extends ParallelTask { public InstallMagisk(Activity context) { super(context); - mm = MagiskManager.get(); + mm = Global.MM(); mode = FIX_ENV_MODE; } @@ -129,8 +130,8 @@ public class InstallMagisk extends ParallelTask { private void extractFiles(String arch) throws IOException { console.add("- Downloading zip"); String filename = Utils.fmt("Magisk-v%s(%d).zip", - mm.remoteMagiskVersionString, mm.remoteMagiskVersionCode); - HttpURLConnection conn = WebService.mustRequest(mm.magiskLink, null); + Global.remoteMagiskVersionString, Global.remoteMagiskVersionCode); + HttpURLConnection conn = WebService.mustRequest(Global.magiskLink, null); BufferedInputStream buf = new BufferedInputStream(new ProgressStream(conn)); buf.mark(Integer.MAX_VALUE); try (OutputStream out = new FileOutputStream(new File(Download.EXTERNAL_PATH, filename))) { @@ -204,7 +205,7 @@ public class InstallMagisk extends ParallelTask { // Patch boot image if (!Shell.sh("cd " + installDir, Utils.fmt( "KEEPFORCEENCRYPT=%b KEEPVERITY=%b sh update-binary indep boot_patch.sh %s", - mm.keepEnc, mm.keepVerity, mBoot)) + Global.keepEnc, Global.keepVerity, mBoot)) .to(console, logs).exec().isSuccess()) return null; @@ -258,7 +259,7 @@ public class InstallMagisk extends ParallelTask { case DIRECT_MODE: Shell.Job job = Shell.su(Utils.fmt("direct_install %s %s %s", patched, mBoot, installDir)) .to(console, logs); - if (!mm.keepVerity) + if (!Global.keepVerity) job.add("find_dtbo_image", "patch_dtbo_image"); job.exec(); break; @@ -331,7 +332,7 @@ public class InstallMagisk extends ParallelTask { List abis = Arrays.asList(Build.SUPPORTED_ABIS); String arch; - if (mm.remoteMagiskVersionCode >= Const.MAGISK_VER.SEPOL_REFACTOR) { + if (Global.remoteMagiskVersionCode >= Const.MAGISK_VER.SEPOL_REFACTOR) { // 32-bit only if (abis.contains("x86")) arch = "x86"; else arch = "arm"; @@ -368,7 +369,7 @@ public class InstallMagisk extends ParallelTask { protected void onPostExecute(Boolean result) { if (mode == FIX_ENV_MODE) { dialog.dismiss(); - MagiskManager.toast(result ? R.string.setup_done : R.string.setup_fail, Toast.LENGTH_LONG); + Global.toast(result ? R.string.setup_done : R.string.setup_fail, Toast.LENGTH_LONG); } else { // Running in FlashActivity FlashActivity activity = (FlashActivity) getActivity(); diff --git a/app/src/full/java/com/topjohnwu/magisk/asyncs/LoadModules.java b/app/src/full/java/com/topjohnwu/magisk/asyncs/LoadModules.java index 52ae9ecab..e15f701c3 100644 --- a/app/src/full/java/com/topjohnwu/magisk/asyncs/LoadModules.java +++ b/app/src/full/java/com/topjohnwu/magisk/asyncs/LoadModules.java @@ -1,5 +1,6 @@ package com.topjohnwu.magisk.asyncs; +import com.topjohnwu.magisk.Global; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.container.Module; import com.topjohnwu.magisk.container.ValueSortedMap; @@ -15,7 +16,7 @@ public class LoadModules extends ParallelTask { @Override protected Void doInBackground(Void... voids) { - MagiskManager mm = MagiskManager.get(); + MagiskManager mm = Global.MM(); mm.moduleMap = new ValueSortedMap<>(); for (String name : getModList()) { @@ -28,7 +29,7 @@ public class LoadModules extends ParallelTask { @Override protected void onPostExecute(Void v) { - MagiskManager.get().moduleLoadDone.publish(); + Global.MM().moduleLoadDone.publish(); super.onPostExecute(v); } } diff --git a/app/src/full/java/com/topjohnwu/magisk/asyncs/MarkDownWindow.java b/app/src/full/java/com/topjohnwu/magisk/asyncs/MarkDownWindow.java index 06a31ef92..2383835c3 100644 --- a/app/src/full/java/com/topjohnwu/magisk/asyncs/MarkDownWindow.java +++ b/app/src/full/java/com/topjohnwu/magisk/asyncs/MarkDownWindow.java @@ -4,6 +4,7 @@ import android.app.Activity; import android.support.v7.app.AlertDialog; import android.webkit.WebView; +import com.topjohnwu.magisk.Global; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.utils.WebService; @@ -38,7 +39,7 @@ public class MarkDownWindow extends ParallelTask { @Override protected String doInBackground(Void... voids) { - MagiskManager mm = MagiskManager.get(); + MagiskManager mm = Global.MM(); String md; if (mUrl != null) { md = WebService.getString(mUrl); diff --git a/app/src/full/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java b/app/src/full/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java index c76c948a9..3887427c9 100644 --- a/app/src/full/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java +++ b/app/src/full/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java @@ -9,7 +9,7 @@ import android.support.annotation.NonNull; import android.widget.Toast; import com.topjohnwu.magisk.FlashActivity; -import com.topjohnwu.magisk.MagiskManager; +import com.topjohnwu.magisk.Global; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.components.Activity; import com.topjohnwu.magisk.components.SnackbarMaker; @@ -148,7 +148,7 @@ public class ProcessRepoZip extends ParallelTask { SnackbarMaker.showUri(activity, uri); } } else { - MagiskManager.toast(R.string.process_error, Toast.LENGTH_LONG); + Global.toast(R.string.process_error, Toast.LENGTH_LONG); } super.onPostExecute(result); } diff --git a/app/src/full/java/com/topjohnwu/magisk/asyncs/RestoreImages.java b/app/src/full/java/com/topjohnwu/magisk/asyncs/RestoreImages.java index fb28a2dd5..b562aee3d 100644 --- a/app/src/full/java/com/topjohnwu/magisk/asyncs/RestoreImages.java +++ b/app/src/full/java/com/topjohnwu/magisk/asyncs/RestoreImages.java @@ -4,7 +4,7 @@ import android.app.Activity; import android.app.ProgressDialog; import android.widget.Toast; -import com.topjohnwu.magisk.MagiskManager; +import com.topjohnwu.magisk.Global; import com.topjohnwu.magisk.R; import com.topjohnwu.superuser.ShellUtils; @@ -31,9 +31,9 @@ public class RestoreImages extends ParallelTask { protected void onPostExecute(Boolean result) { dialog.cancel(); if (result) { - MagiskManager.toast(R.string.restore_done, Toast.LENGTH_SHORT); + Global.toast(R.string.restore_done, Toast.LENGTH_SHORT); } else { - MagiskManager.toast(R.string.restore_fail, Toast.LENGTH_LONG); + Global.toast(R.string.restore_fail, Toast.LENGTH_LONG); } } } diff --git a/app/src/full/java/com/topjohnwu/magisk/asyncs/UpdateRepos.java b/app/src/full/java/com/topjohnwu/magisk/asyncs/UpdateRepos.java index 961ad0186..857cfe01a 100644 --- a/app/src/full/java/com/topjohnwu/magisk/asyncs/UpdateRepos.java +++ b/app/src/full/java/com/topjohnwu/magisk/asyncs/UpdateRepos.java @@ -3,6 +3,7 @@ package com.topjohnwu.magisk.asyncs; import android.database.Cursor; import android.text.TextUtils; +import com.topjohnwu.magisk.Global; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.ReposFragment; import com.topjohnwu.magisk.container.Repo; @@ -48,7 +49,7 @@ public class UpdateRepos extends ParallelTask { private ExecutorService threadPool; public UpdateRepos(boolean force) { - mm = MagiskManager.get(); + mm = Global.MM(); mm.repoLoadDone.reset(); forceUpdate = force; threadPool = Executors.newFixedThreadPool(CORE_POOL_SIZE); diff --git a/app/src/full/java/com/topjohnwu/magisk/components/InstallMethodDialog.java b/app/src/full/java/com/topjohnwu/magisk/components/InstallMethodDialog.java index 53408da89..5527cc002 100644 --- a/app/src/full/java/com/topjohnwu/magisk/components/InstallMethodDialog.java +++ b/app/src/full/java/com/topjohnwu/magisk/components/InstallMethodDialog.java @@ -7,6 +7,7 @@ import android.support.design.widget.Snackbar; import android.support.v7.app.AlertDialog; import android.widget.Toast; +import com.topjohnwu.magisk.Global; import com.topjohnwu.magisk.FlashActivity; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; @@ -27,12 +28,12 @@ class InstallMethodDialog extends AlertDialog.Builder { Intent intent; switch (idx) { case 1: - if (mm.remoteMagiskVersionCode < 1400) { + if (Global.remoteMagiskVersionCode < 1400) { SnackbarMaker.make(activity, R.string.no_boot_file_patch_support, Snackbar.LENGTH_LONG).show(); return; } - MagiskManager.toast(R.string.boot_file_patch_msg, Toast.LENGTH_LONG); + Global.toast(R.string.boot_file_patch_msg, Toast.LENGTH_LONG); intent = new Intent(Intent.ACTION_GET_CONTENT).setType("*/*"); activity.startActivityForResult(intent, Const.ID.SELECT_BOOT, (requestCode, resultCode, data) -> { @@ -51,7 +52,7 @@ class InstallMethodDialog extends AlertDialog.Builder { public void onDownloadDone(Context context, Uri uri) { SnackbarMaker.showUri(activity, uri); } - }, mm.magiskLink, filename); + }, Global.magiskLink, filename); break; case 2: intent = new Intent(activity, FlashActivity.class) diff --git a/app/src/full/java/com/topjohnwu/magisk/components/MagiskInstallDialog.java b/app/src/full/java/com/topjohnwu/magisk/components/MagiskInstallDialog.java index d224f484f..be194c048 100644 --- a/app/src/full/java/com/topjohnwu/magisk/components/MagiskInstallDialog.java +++ b/app/src/full/java/com/topjohnwu/magisk/components/MagiskInstallDialog.java @@ -4,6 +4,7 @@ import android.content.Intent; import android.net.Uri; import android.text.TextUtils; +import com.topjohnwu.magisk.Global; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.asyncs.MarkDownWindow; @@ -19,7 +20,7 @@ public class MagiskInstallDialog extends CustomAlertDialog { super(activity); MagiskManager mm = Utils.getMagiskManager(activity); String filename = Utils.fmt("Magisk-v%s(%d).zip", - mm.remoteMagiskVersionString, mm.remoteMagiskVersionCode); + Global.remoteMagiskVersionString, Global.remoteMagiskVersionCode); setTitle(mm.getString(R.string.repo_install_title, mm.getString(R.string.magisk))); setMessage(mm.getString(R.string.repo_install_msg, filename)); setCancelable(true); @@ -37,15 +38,15 @@ public class MagiskInstallDialog extends CustomAlertDialog { new InstallMethodDialog(activity, options, filename).show(); }); setNegativeButton(R.string.no_thanks, null); - if (!TextUtils.isEmpty(mm.magiskNoteLink)) { + if (!TextUtils.isEmpty(Global.magiskNoteLink)) { setNeutralButton(R.string.release_notes, (d, i) -> { - if (mm.magiskNoteLink.contains("forum.xda-developers")) { + if (Global.magiskNoteLink.contains("forum.xda-developers")) { // Open forum links in browser - Intent openLink = new Intent(Intent.ACTION_VIEW, Uri.parse(mm.magiskNoteLink)); + Intent openLink = new Intent(Intent.ACTION_VIEW, Uri.parse(Global.magiskNoteLink)); openLink.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); mm.startActivity(openLink); } else { - new MarkDownWindow(activity, null, mm.magiskNoteLink).exec(); + new MarkDownWindow(activity, null, Global.magiskNoteLink).exec(); } }); } diff --git a/app/src/full/java/com/topjohnwu/magisk/components/ManagerInstallDialog.java b/app/src/full/java/com/topjohnwu/magisk/components/ManagerInstallDialog.java index 31486b4bc..f4f72ea5f 100644 --- a/app/src/full/java/com/topjohnwu/magisk/components/ManagerInstallDialog.java +++ b/app/src/full/java/com/topjohnwu/magisk/components/ManagerInstallDialog.java @@ -5,6 +5,7 @@ import android.content.Intent; import android.support.annotation.NonNull; import android.text.TextUtils; +import com.topjohnwu.magisk.Global; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.asyncs.MarkDownWindow; @@ -18,21 +19,21 @@ public class ManagerInstallDialog extends CustomAlertDialog { super(activity); MagiskManager mm = Utils.getMagiskManager(activity); String filename = Utils.fmt("MagiskManager-v%s(%d).apk", - mm.remoteManagerVersionString, mm.remoteManagerVersionCode); + Global.remoteManagerVersionString, Global.remoteManagerVersionCode); setTitle(mm.getString(R.string.repo_install_title, mm.getString(R.string.app_name))); setMessage(mm.getString(R.string.repo_install_msg, filename)); setCancelable(true); setPositiveButton(R.string.install, (d, i) -> activity.runWithPermission( new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, () -> { Intent intent = new Intent(mm, ManagerUpdate.class); - intent.putExtra(Const.Key.INTENT_SET_LINK, mm.managerLink); + intent.putExtra(Const.Key.INTENT_SET_LINK, Global.managerLink); intent.putExtra(Const.Key.INTENT_SET_FILENAME, filename); mm.sendBroadcast(intent); })) .setNegativeButton(R.string.no_thanks, null); - if (!TextUtils.isEmpty(mm.managerNoteLink)) { + if (!TextUtils.isEmpty(Global.managerNoteLink)) { setNeutralButton(R.string.app_changelog, (d, i) -> - new MarkDownWindow(activity, null, mm.managerNoteLink).exec()); + new MarkDownWindow(activity, null, Global.managerNoteLink).exec()); } } } diff --git a/app/src/full/java/com/topjohnwu/magisk/components/UninstallDialog.java b/app/src/full/java/com/topjohnwu/magisk/components/UninstallDialog.java index 7e0206a44..3ed2941dd 100644 --- a/app/src/full/java/com/topjohnwu/magisk/components/UninstallDialog.java +++ b/app/src/full/java/com/topjohnwu/magisk/components/UninstallDialog.java @@ -7,6 +7,7 @@ import android.net.Uri; import android.support.annotation.NonNull; import android.text.TextUtils; +import com.topjohnwu.magisk.Global; import com.topjohnwu.magisk.FlashActivity; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; @@ -24,7 +25,7 @@ public class UninstallDialog extends CustomAlertDialog { setTitle(R.string.uninstall_magisk_title); setMessage(R.string.uninstall_magisk_msg); setNeutralButton(R.string.restore_img, (d, i) -> new RestoreImages(activity).exec()); - if (!TextUtils.isEmpty(mm.uninstallerLink)) { + if (!TextUtils.isEmpty(Global.uninstallerLink)) { setPositiveButton(R.string.complete_uninstall, (d, i) -> Download.receive(activity, new DownloadReceiver() { @Override @@ -35,7 +36,7 @@ public class UninstallDialog extends CustomAlertDialog { .putExtra(Const.Key.FLASH_ACTION, Const.Value.UNINSTALL); context.startActivity(intent); } - }, mm.uninstallerLink, "magisk-uninstaller.zip")); + }, Global.uninstallerLink, "magisk-uninstaller.zip")); } } } diff --git a/app/src/full/java/com/topjohnwu/magisk/database/MagiskDatabaseHelper.java b/app/src/full/java/com/topjohnwu/magisk/database/MagiskDatabaseHelper.java index 746d4e468..529b93c13 100644 --- a/app/src/full/java/com/topjohnwu/magisk/database/MagiskDatabaseHelper.java +++ b/app/src/full/java/com/topjohnwu/magisk/database/MagiskDatabaseHelper.java @@ -11,6 +11,7 @@ import android.support.annotation.NonNull; import android.text.TextUtils; import android.widget.Toast; +import com.topjohnwu.magisk.Global; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.container.Policy; @@ -72,13 +73,12 @@ public class MagiskDatabaseHelper { // We don't want the app to crash, create a db and return return mm.openOrCreateDatabase("su.db", Context.MODE_PRIVATE, null); } - mm.loadMagiskInfo(); // Cleanup Shell.su("db_clean " + Const.USER_ID).exec(); - if (mm.magiskVersionCode < Const.MAGISK_VER.FBE_AWARE) { + if (Global.magiskVersionCode < Const.MAGISK_VER.FBE_AWARE) { // Super old legacy mode return mm.openOrCreateDatabase("su.db", Context.MODE_PRIVATE, null); - } else if (mm.magiskVersionCode < Const.MAGISK_VER.HIDDEN_PATH) { + } else if (Global.magiskVersionCode < Const.MAGISK_VER.HIDDEN_PATH) { // Legacy mode with FBE aware if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { de.moveDatabaseFrom(mm, "su.db"); @@ -89,7 +89,7 @@ public class MagiskDatabaseHelper { final SuFile GLOBAL_DB = new SuFile("/data/adb/magisk.db"); mm.deleteDatabase("su.db"); de.deleteDatabase("su.db"); - if (mm.magiskVersionCode < Const.MAGISK_VER.SEPOL_REFACTOR) { + if (Global.magiskVersionCode < Const.MAGISK_VER.SEPOL_REFACTOR) { // We need some additional policies on old versions Shell.su("db_sepatch").exec(); } @@ -123,7 +123,7 @@ public class MagiskDatabaseHelper { POLICY_TABLE, POLICY_TABLE)); db.execSQL(Utils.fmt("DROP TABLE %s_old", POLICY_TABLE)); - MagiskManager.get().deleteDatabase("sulog.db"); + Global.MM().deleteDatabase("sulog.db"); ++oldVersion; } if (oldVersion == 2) { @@ -142,7 +142,7 @@ public class MagiskDatabaseHelper { // Remove everything, we do not support downgrade public void onDowngrade(SQLiteDatabase db) { - MagiskManager.toast(R.string.su_db_corrupt, Toast.LENGTH_LONG); + Global.toast(R.string.su_db_corrupt, Toast.LENGTH_LONG); db.execSQL("DROP TABLE IF EXISTS " + POLICY_TABLE); db.execSQL("DROP TABLE IF EXISTS " + LOG_TABLE); db.execSQL("DROP TABLE IF EXISTS " + SETTINGS_TABLE); @@ -174,7 +174,7 @@ public class MagiskDatabaseHelper { // Clear outdated policies db.delete(POLICY_TABLE, Utils.fmt("until > 0 AND until < %d", System.currentTimeMillis() / 1000), null); // Clear outdated logs - db.delete(LOG_TABLE, Utils.fmt("time < %d", System.currentTimeMillis() - MagiskManager.get().suLogTimeout * 86400000), null); + db.delete(LOG_TABLE, Utils.fmt("time < %d", System.currentTimeMillis() - Global.MM().suLogTimeout * 86400000), null); } public void deletePolicy(Policy policy) { diff --git a/app/src/full/java/com/topjohnwu/magisk/database/RepoDatabaseHelper.java b/app/src/full/java/com/topjohnwu/magisk/database/RepoDatabaseHelper.java index acc4fecb0..7995fee28 100644 --- a/app/src/full/java/com/topjohnwu/magisk/database/RepoDatabaseHelper.java +++ b/app/src/full/java/com/topjohnwu/magisk/database/RepoDatabaseHelper.java @@ -5,6 +5,7 @@ import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; +import com.topjohnwu.magisk.Global; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.container.Repo; import com.topjohnwu.magisk.utils.Const; @@ -108,7 +109,7 @@ public class RepoDatabaseHelper extends SQLiteOpenHelper { orderBy = "last_update DESC"; } return mDb.query(TABLE_NAME, null, "minMagisk<=? AND minMagisk>=?", - new String[] { String.valueOf(mm.magiskVersionCode), String.valueOf(Const.MIN_MODULE_VER()) }, + new String[] { String.valueOf(Global.magiskVersionCode), String.valueOf(Const.MIN_MODULE_VER()) }, null, null, orderBy); } diff --git a/app/src/full/java/com/topjohnwu/magisk/receivers/ShortcutReceiver.java b/app/src/full/java/com/topjohnwu/magisk/receivers/ShortcutReceiver.java index 5ae2b2357..80ee13ae4 100644 --- a/app/src/full/java/com/topjohnwu/magisk/receivers/ShortcutReceiver.java +++ b/app/src/full/java/com/topjohnwu/magisk/receivers/ShortcutReceiver.java @@ -8,8 +8,8 @@ import android.content.pm.ShortcutManager; import android.graphics.drawable.Icon; import android.os.Build; import android.support.annotation.RequiresApi; -import android.text.TextUtils; +import com.topjohnwu.magisk.Global; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.SplashActivity; @@ -25,10 +25,6 @@ public class ShortcutReceiver extends BroadcastReceiver { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { MagiskManager mm = Utils.getMagiskManager(context); ShortcutManager manager = context.getSystemService(ShortcutManager.class); - if (TextUtils.equals(intent.getAction(), Intent.ACTION_LOCALE_CHANGED)) { - // It is triggered with locale change, manual load Magisk info - mm.loadMagiskInfo(); - } manager.setDynamicShortcuts(getShortCuts(mm)); } } @@ -36,8 +32,8 @@ public class ShortcutReceiver extends BroadcastReceiver { @RequiresApi(api = Build.VERSION_CODES.N_MR1) private ArrayList getShortCuts(MagiskManager mm) { ArrayList shortCuts = new ArrayList<>(); - if (Shell.rootAccess() && - !(Const.USER_ID > 0 && + boolean root = Shell.rootAccess(); + if (root && !(Const.USER_ID > 0 && mm.multiuserMode == Const.Value.MULTIUSER_MODE_OWNER_MANAGED)) { shortCuts.add(new ShortcutInfo.Builder(mm, "superuser") .setShortLabel(mm.getString(R.string.superuser)) @@ -49,7 +45,7 @@ public class ShortcutReceiver extends BroadcastReceiver { .setRank(0) .build()); } - if (Shell.rootAccess() && mm.magiskVersionCode >= Const.MAGISK_VER.UNIFIED + if (root && Global.magiskVersionCode >= Const.MAGISK_VER.UNIFIED && mm.prefs.getBoolean(Const.Key.MAGISKHIDE, false)) { shortCuts.add(new ShortcutInfo.Builder(mm, "magiskhide") .setShortLabel(mm.getString(R.string.magiskhide)) @@ -61,8 +57,7 @@ public class ShortcutReceiver extends BroadcastReceiver { .setRank(1) .build()); } - if (!mm.prefs.getBoolean(Const.Key.COREONLY, false) && - Shell.rootAccess() && mm.magiskVersionCode >= 0) { + if (!mm.prefs.getBoolean(Const.Key.COREONLY, false) && root && Global.magiskVersionCode >= 0) { shortCuts.add(new ShortcutInfo.Builder(mm, "modules") .setShortLabel(mm.getString(R.string.modules)) .setIntent(new Intent(mm, SplashActivity.class) diff --git a/app/src/full/java/com/topjohnwu/magisk/services/UpdateCheckService.java b/app/src/full/java/com/topjohnwu/magisk/services/UpdateCheckService.java index 89a958fc2..41bda1e7c 100644 --- a/app/src/full/java/com/topjohnwu/magisk/services/UpdateCheckService.java +++ b/app/src/full/java/com/topjohnwu/magisk/services/UpdateCheckService.java @@ -4,15 +4,14 @@ import android.app.job.JobParameters; import android.app.job.JobService; import com.topjohnwu.magisk.asyncs.CheckUpdates; -import com.topjohnwu.magisk.utils.Utils; +import com.topjohnwu.superuser.Shell; public class UpdateCheckService extends JobService { @Override public boolean onStartJob(JobParameters params) { - Utils.getMagiskManager(this).loadMagiskInfo(); - new CheckUpdates(true) - .setCallBack(() -> jobFinished(params, false)).exec(); + Shell.getShell(); + new CheckUpdates(true).setCallBack(() -> jobFinished(params, false)).exec(); return true; } diff --git a/app/src/full/java/com/topjohnwu/magisk/superuser/SuReceiver.java b/app/src/full/java/com/topjohnwu/magisk/superuser/SuReceiver.java index d39d1f4be..b561e6dde 100644 --- a/app/src/full/java/com/topjohnwu/magisk/superuser/SuReceiver.java +++ b/app/src/full/java/com/topjohnwu/magisk/superuser/SuReceiver.java @@ -7,6 +7,7 @@ import android.content.pm.PackageManager; import android.os.Process; import android.widget.Toast; +import com.topjohnwu.magisk.Global; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.container.Policy; @@ -32,7 +33,7 @@ public class SuReceiver extends BroadcastReceiver { if (mode < 0) return; if (mode == Const.Value.NOTIFY_USER_TO_OWNER) { - MagiskManager.toast(R.string.multiuser_hint_owner_request, Toast.LENGTH_LONG); + Global.toast(R.string.multiuser_hint_owner_request, Toast.LENGTH_LONG); return; } @@ -70,7 +71,7 @@ public class SuReceiver extends BroadcastReceiver { } if (policy.notification && mm.suNotificationType == Const.Value.NOTIFICATION_TOAST) { - MagiskManager.toast(message, Toast.LENGTH_SHORT); + Global.toast(message, Toast.LENGTH_SHORT); } if (mode == Const.Value.NOTIFY_NORMAL_LOG && policy.logging) { diff --git a/app/src/full/java/com/topjohnwu/magisk/utils/Const.java b/app/src/full/java/com/topjohnwu/magisk/utils/Const.java index 609232e94..4eff9d71f 100644 --- a/app/src/full/java/com/topjohnwu/magisk/utils/Const.java +++ b/app/src/full/java/com/topjohnwu/magisk/utils/Const.java @@ -3,7 +3,7 @@ package com.topjohnwu.magisk.utils; import android.os.Process; import com.topjohnwu.magisk.BuildConfig; -import com.topjohnwu.magisk.MagiskManager; +import com.topjohnwu.magisk.Global; import java.io.File; import java.util.Arrays; @@ -40,13 +40,13 @@ public class Const { public static final int SNET_VER = 10; public static int MIN_MODULE_VER() { - return MagiskManager.get().magiskVersionCode >= MAGISK_VER.REMOVE_LEGACY_LINK ? 1500 : 1400; + return Global.magiskVersionCode >= MAGISK_VER.REMOVE_LEGACY_LINK ? 1500 : 1400; } /* A list of apps that should not be shown as hide-able */ public static final List HIDE_BLACKLIST = Arrays.asList( "android", - MagiskManager.get().getPackageName(), + Global.MM().getPackageName(), "com.google.android.gms" ); diff --git a/app/src/full/java/com/topjohnwu/magisk/utils/FingerprintHelper.java b/app/src/full/java/com/topjohnwu/magisk/utils/FingerprintHelper.java index 1808f06e5..8816e8c83 100644 --- a/app/src/full/java/com/topjohnwu/magisk/utils/FingerprintHelper.java +++ b/app/src/full/java/com/topjohnwu/magisk/utils/FingerprintHelper.java @@ -9,6 +9,7 @@ import android.security.keystore.KeyGenParameterSpec; import android.security.keystore.KeyPermanentlyInvalidatedException; import android.security.keystore.KeyProperties; +import com.topjohnwu.magisk.Global; import com.topjohnwu.magisk.MagiskManager; import java.security.KeyStore; @@ -27,14 +28,14 @@ public abstract class FingerprintHelper { public static boolean canUseFingerprint() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return false; - MagiskManager mm = MagiskManager.get(); + MagiskManager mm = Global.MM(); KeyguardManager km = mm.getSystemService(KeyguardManager.class); FingerprintManager fm = mm.getSystemService(FingerprintManager.class); return km.isKeyguardSecure() && fm != null && fm.isHardwareDetected() && fm.hasEnrolledFingerprints(); } protected FingerprintHelper() throws Exception { - MagiskManager mm = MagiskManager.get(); + MagiskManager mm = Global.MM(); KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore"); manager = mm.getSystemService(FingerprintManager.class); cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" diff --git a/app/src/full/java/com/topjohnwu/magisk/utils/NotificationMgr.java b/app/src/full/java/com/topjohnwu/magisk/utils/NotificationMgr.java index 1f26539bf..0e5d7f77b 100644 --- a/app/src/full/java/com/topjohnwu/magisk/utils/NotificationMgr.java +++ b/app/src/full/java/com/topjohnwu/magisk/utils/NotificationMgr.java @@ -7,6 +7,7 @@ import android.content.Intent; import android.support.v4.app.NotificationCompat; import android.support.v4.app.TaskStackBuilder; +import com.topjohnwu.magisk.Global; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.SplashActivity; @@ -16,7 +17,7 @@ import com.topjohnwu.magisk.receivers.RebootReceiver; public class NotificationMgr { public static void magiskUpdate() { - MagiskManager mm = MagiskManager.get(); + MagiskManager mm = Global.MM(); Intent intent = new Intent(mm, SplashActivity.class); intent.putExtra(Const.Key.OPEN_SECTION, "magisk"); @@ -29,7 +30,7 @@ public class NotificationMgr { NotificationCompat.Builder builder = new NotificationCompat.Builder(mm, Const.ID.NOTIFICATION_CHANNEL); builder.setSmallIcon(R.drawable.ic_magisk_outline) .setContentTitle(mm.getString(R.string.magisk_update_title)) - .setContentText(mm.getString(R.string.magisk_update_available, mm.remoteMagiskVersionString)) + .setContentText(mm.getString(R.string.magisk_update_available, Global.remoteMagiskVersionString)) .setVibrate(new long[]{0, 100, 100, 100}) .setAutoCancel(true) .setContentIntent(pendingIntent); @@ -40,12 +41,12 @@ public class NotificationMgr { } public static void managerUpdate() { - MagiskManager mm = MagiskManager.get(); + MagiskManager mm = Global.MM(); String filename = Utils.fmt("MagiskManager-v%s(%d).apk", - mm.remoteManagerVersionString, mm.remoteManagerVersionCode); + Global.remoteManagerVersionString, Global.remoteManagerVersionCode); Intent intent = new Intent(mm, ManagerUpdate.class); - intent.putExtra(Const.Key.INTENT_SET_LINK, mm.managerLink); + intent.putExtra(Const.Key.INTENT_SET_LINK, Global.managerLink); intent.putExtra(Const.Key.INTENT_SET_FILENAME, filename); PendingIntent pendingIntent = PendingIntent.getBroadcast(mm, Const.ID.APK_UPDATE_NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT); @@ -64,7 +65,7 @@ public class NotificationMgr { } public static void dtboPatched() { - MagiskManager mm = MagiskManager.get(); + MagiskManager mm = Global.MM(); Intent intent = new Intent(mm, RebootReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(mm, diff --git a/app/src/full/java/com/topjohnwu/magisk/utils/RootUtils.java b/app/src/full/java/com/topjohnwu/magisk/utils/RootUtils.java index 39bd49f99..62931fd32 100644 --- a/app/src/full/java/com/topjohnwu/magisk/utils/RootUtils.java +++ b/app/src/full/java/com/topjohnwu/magisk/utils/RootUtils.java @@ -3,9 +3,11 @@ package com.topjohnwu.magisk.utils; import android.content.Context; import android.support.annotation.NonNull; +import com.topjohnwu.magisk.Global; import com.topjohnwu.magisk.R; import com.topjohnwu.superuser.BusyBox; import com.topjohnwu.superuser.Shell; +import com.topjohnwu.superuser.ShellUtils; import com.topjohnwu.superuser.io.SuFile; import java.io.File; @@ -18,26 +20,10 @@ public class RootUtils extends Shell.Initializer { BusyBox.BB_PATH = new File(Const.BUSYBOX_PATH); } - private static boolean fileInit = false; - public static void uninstallPkg(String pkg) { Shell.su("db_clean " + Const.USER_ID, "pm uninstall " + pkg).exec(); } - private void initConsts() { - Const.MAGISK_DISABLE_FILE = new SuFile("/cache/.disable_magisk"); - SuFile file = new SuFile("/sbin/.core/img"); - if (file.exists()) { - Const.MAGISK_PATH = file; - } else if ((file = new SuFile("/dev/magisk/img")).exists()) { - Const.MAGISK_PATH = file; - } else { - Const.MAGISK_PATH = new SuFile("/magisk"); - } - Const.MAGISK_HOST_FILE = new SuFile(Const.MAGISK_PATH + "/.core/hosts"); - fileInit = true; - } - @Override public boolean onInit(Context context, @NonNull Shell shell) { if (shell.isRoot()) { @@ -51,8 +37,22 @@ public class RootUtils extends Shell.Initializer { } catch (IOException e) { return false; } - if (!fileInit) - initConsts(); + + Const.MAGISK_DISABLE_FILE = new SuFile("/cache/.disable_magisk"); + SuFile file = new SuFile("/sbin/.core/img"); + if (file.exists()) { + Const.MAGISK_PATH = file; + } else if ((file = new SuFile("/dev/magisk/img")).exists()) { + Const.MAGISK_PATH = file; + } else { + Const.MAGISK_PATH = new SuFile("/magisk"); + } + Const.MAGISK_HOST_FILE = new SuFile(Const.MAGISK_PATH + "/.core/hosts"); + + Global.loadMagiskInfo(); + + Global.keepVerity = Boolean.parseBoolean(ShellUtils.fastCmd("echo $KEEPVERITY")); + Global.keepEnc = Boolean.parseBoolean(ShellUtils.fastCmd("echo $KEEPFORCEENCRYPT")); } return true; } diff --git a/app/src/full/java/com/topjohnwu/magisk/utils/Utils.java b/app/src/full/java/com/topjohnwu/magisk/utils/Utils.java index 6dbe0e421..5b568ba5c 100644 --- a/app/src/full/java/com/topjohnwu/magisk/utils/Utils.java +++ b/app/src/full/java/com/topjohnwu/magisk/utils/Utils.java @@ -1,5 +1,8 @@ package com.topjohnwu.magisk.utils; +import android.app.job.JobInfo; +import android.app.job.JobScheduler; +import android.content.ComponentName; import android.content.Context; import android.content.SharedPreferences; import android.content.res.Configuration; @@ -8,8 +11,10 @@ import android.net.Uri; import android.provider.OpenableColumns; import android.support.annotation.StringRes; +import com.topjohnwu.magisk.Global; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; +import com.topjohnwu.magisk.services.UpdateCheckService; import java.util.ArrayList; import java.util.Collections; @@ -50,7 +55,7 @@ public class Utils { } public static String getLocaleString(Locale locale, @StringRes int id) { - Context context = MagiskManager.get(); + Context context = Global.MM(); Configuration config = context.getResources().getConfiguration(); config.setLocale(locale); Context localizedContext = context.createConfigurationContext(config); @@ -76,7 +81,7 @@ public class Utils { set.add(getLocaleString(locale, compareId)); // Other locales - for (String s : MagiskManager.get().getAssets().getLocales()) { + for (String s : Global.MM().getAssets().getLocales()) { locale = Locale.forLanguageTag(s); if (set.add(getLocaleString(locale, compareId))) { locales.add(locale); @@ -89,7 +94,7 @@ public class Utils { } public static int dpInPx(int dp) { - Context context = MagiskManager.get(); + Context context = Global.MM(); float scale = context.getResources().getDisplayMetrics().density; return (int) (dp * scale + 0.5); } @@ -106,4 +111,24 @@ public class Utils { return newString; } } + + public static void setupUpdateCheck() { + MagiskManager mm = Global.MM(); + JobScheduler scheduler = (JobScheduler) mm.getSystemService(Context.JOB_SCHEDULER_SERVICE); + + if (mm.prefs.getBoolean(Const.Key.CHECK_UPDATES, true)) { + if (scheduler.getAllPendingJobs().isEmpty() || + Const.UPDATE_SERVICE_VER > mm.prefs.getInt(Const.Key.UPDATE_SERVICE_VER, -1)) { + ComponentName service = new ComponentName(mm, UpdateCheckService.class); + JobInfo info = new JobInfo.Builder(Const.ID.UPDATE_SERVICE_ID, service) + .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) + .setPersisted(true) + .setPeriodic(8 * 60 * 60 * 1000) + .build(); + scheduler.schedule(info); + } + } else { + scheduler.cancel(Const.UPDATE_SERVICE_VER); + } + } } \ No newline at end of file