diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java b/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java index b40192aa1..71147e1f6 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java @@ -176,8 +176,8 @@ public class MagiskFragment extends Fragment shownDialog = false; // Trigger state check - if (Utils.checkNetworkStatus(mm)) { - new CheckUpdates(getActivity()).exec(); + if (Utils.checkNetworkStatus()) { + new CheckUpdates().exec(); } else { mSwipeRefreshLayout.setRefreshing(false); } @@ -230,7 +230,7 @@ public class MagiskFragment extends Fragment private void updateUI() { ((MainActivity) getActivity()).checkHideSection(); - boolean hasNetwork = Utils.checkNetworkStatus(getActivity()); + boolean hasNetwork = Utils.checkNetworkStatus(); boolean hasRoot = Shell.rootAccess(); boolean isUpToDate = mm.magiskVersionCode > 1300; diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskLogFragment.java b/app/src/main/java/com/topjohnwu/magisk/MagiskLogFragment.java index 6612cc34a..05e1008e5 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskLogFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskLogFragment.java @@ -170,9 +170,9 @@ public class MagiskLogFragment extends Fragment { case 2: boolean bool = (boolean) o; if (bool) { - getMagiskManager().toast(targetFile.toString(), Toast.LENGTH_LONG); + MagiskManager.toast(targetFile.toString(), Toast.LENGTH_LONG); } else { - getMagiskManager().toast(R.string.logs_save_failed, Toast.LENGTH_LONG); + MagiskManager.toast(R.string.logs_save_failed, Toast.LENGTH_LONG); } break; } diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java b/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java index 670cada36..cc813c58c 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java @@ -32,6 +32,7 @@ import com.topjohnwu.magisk.utils.Utils; import java.io.IOException; import java.io.InputStream; +import java.lang.ref.WeakReference; import java.util.List; import java.util.Locale; import java.util.Map; @@ -39,6 +40,9 @@ import java.util.concurrent.ExecutionException; public class MagiskManager extends Application { + // Global weak reference to self + private static WeakReference weakSelf; + public static final String MAGISK_DISABLE_FILE = "/cache/.disable_magisk"; public static final String MAGISK_HOST_FILE = "/magisk/.core/hosts"; public static final String TMP_FOLDER_PATH = "/dev/tmp"; @@ -110,21 +114,17 @@ public class MagiskManager extends Application { private static Handler mHandler = new Handler(); private boolean started = false; - private static class LoadLocale extends ParallelTask { - - LoadLocale(Context context) { - super(context); - } + private class LoadLocale extends ParallelTask { @Override protected Void doInBackground(Void... voids) { - getMagiskManager().locales = Utils.getAvailableLocale(getMagiskManager()); + locales = Utils.getAvailableLocale(); return null; } @Override protected void onPostExecute(Void aVoid) { - getMagiskManager().localeDone.publish(); + localeDone.publish(); } } @@ -137,15 +137,20 @@ public class MagiskManager extends Application { // Don't migrate yet, wait and check Magisk version suDB = new SuDatabaseHelper(this); } else { - suDB = new SuDatabaseHelper(Utils.getEncContext(this)); + suDB = new SuDatabaseHelper(Utils.getEncContext()); } + weakSelf = new WeakReference<>(this); repoDB = new RepoDatabaseHelper(this); defaultLocale = Locale.getDefault(); setLocale(); loadConfig(); } + public static MagiskManager get() { + return weakSelf.get(); + } + public void setLocale() { localeConfig = prefs.getString("locale", ""); if (localeConfig.isEmpty()) { @@ -177,12 +182,12 @@ public class MagiskManager extends Application { snet_version = prefs.getInt("snet_version", -1); } - public void toast(String msg, int duration) { - mHandler.post(() -> Toast.makeText(this, msg, duration).show()); + public static void toast(String msg, int duration) { + mHandler.post(() -> Toast.makeText(weakSelf.get(), msg, duration).show()); } - public void toast(int resId, int duration) { - mHandler.post(() -> Toast.makeText(this, resId, duration).show()); + public static void toast(int resId, int duration) { + mHandler.post(() -> Toast.makeText(weakSelf.get(), resId, duration).show()); } public void startup() { @@ -190,7 +195,7 @@ public class MagiskManager extends Application { return; started = true; - boolean hasNetwork = Utils.checkNetworkStatus(this); + boolean hasNetwork = Utils.checkNetworkStatus(); getMagiskInfo(); @@ -204,14 +209,14 @@ public class MagiskManager extends Application { } } - new LoadLocale(this).exec(); + new LoadLocale().exec(); // Root actions if (Shell.rootAccess()) { if (hasNetwork && !Utils.itemExist(BUSYBOXPATH + "/busybox")) { try { // Force synchronous, make sure we have busybox to use - new DownloadBusybox(this).exec().get(); + new DownloadBusybox().exec().get(); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } @@ -265,7 +270,7 @@ public class MagiskManager extends Application { getSystemService(NotificationManager.class).createNotificationChannel(channel); } - LoadModules loadModuleTask = new LoadModules(this); + LoadModules loadModuleTask = new LoadModules(); // Start update check job if (hasNetwork) { ComponentName service = new ComponentName(this, UpdateCheckService.class); @@ -275,7 +280,7 @@ public class MagiskManager extends Application { .setPeriodic(8 * 60 * 60 * 1000) .build(); ((JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE)).schedule(jobInfo); - loadModuleTask.setCallBack(() -> new UpdateRepos(this, false).exec()); + loadModuleTask.setCallBack(() -> new UpdateRepos(false).exec()); } // Fire asynctasks loadModuleTask.exec(); @@ -284,7 +289,6 @@ public class MagiskManager extends Application { public void getMagiskInfo() { List ret; - Shell.registerShell(this); ret = Shell.sh("su -v"); if (Utils.isValidShellResponse(ret)) { suVersion = ret.get(0); diff --git a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java index 5190601ee..f655e0016 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java @@ -118,7 +118,7 @@ public class MainActivity extends Activity && prefs.getBoolean("magiskhide", false)); menu.findItem(R.id.modules).setVisible( Shell.rootAccess() && getMagiskManager().magiskVersionCode >= 0); - menu.findItem(R.id.downloads).setVisible(Utils.checkNetworkStatus(this) && + menu.findItem(R.id.downloads).setVisible(Utils.checkNetworkStatus() && Shell.rootAccess() && getMagiskManager().magiskVersionCode >= 0); menu.findItem(R.id.log).setVisible(Shell.rootAccess()); menu.findItem(R.id.superuser).setVisible( diff --git a/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java b/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java index b314e1a29..238d279fd 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java @@ -54,7 +54,7 @@ public class ModulesFragment extends Fragment implements Topic.Subscriber { mSwipeRefreshLayout.setOnRefreshListener(() -> { recyclerView.setVisibility(View.GONE); - new LoadModules(getActivity()).exec(); + new LoadModules().exec(); }); recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { diff --git a/app/src/main/java/com/topjohnwu/magisk/ReposFragment.java b/app/src/main/java/com/topjohnwu/magisk/ReposFragment.java index 4d144439d..b73fe4b15 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ReposFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/ReposFragment.java @@ -47,7 +47,7 @@ public class ReposFragment extends Fragment implements Topic.Subscriber { mSwipeRefreshLayout.setOnRefreshListener(() -> { recyclerView.setVisibility(View.VISIBLE); emptyRv.setVisibility(View.GONE); - new UpdateRepos(getActivity(), true).exec(); + new UpdateRepos(true).exec(); }); getActivity().setTitle(R.string.downloads); diff --git a/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java b/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java index 08dd4ad97..935c30952 100644 --- a/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java @@ -121,14 +121,14 @@ public class SettingsActivity extends Activity implements Topic.Subscriber { findPreference("clear").setOnPreferenceClickListener((pref) -> { mm.prefs.edit().remove(UpdateRepos.ETAG_KEY).apply(); mm.repoDB.clearRepo(); - mm.toast(R.string.repo_cache_cleared, Toast.LENGTH_SHORT); + MagiskManager.toast(R.string.repo_cache_cleared, Toast.LENGTH_SHORT); return true; }); hideManager.setOnPreferenceClickListener((pref) -> { Utils.runWithPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE, - () -> new HideManager(getActivity()).exec()); + () -> new HideManager().exec()); return true; }); @@ -239,7 +239,7 @@ public class SettingsActivity extends Activity implements Topic.Subscriber { break; case "update_channel": mm.updateChannel = Utils.getPrefsInt(prefs, "update_channel"); - new CheckUpdates(mm, true).exec(); + new CheckUpdates(true).exec(); break; } mm.loadConfig(); diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/CheckSafetyNet.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/CheckSafetyNet.java index 20836efc6..543f399e9 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/CheckSafetyNet.java +++ b/app/src/main/java/com/topjohnwu/magisk/asyncs/CheckSafetyNet.java @@ -33,7 +33,7 @@ public class CheckSafetyNet extends ParallelTask { @Override protected void onPreExecute() { - MagiskManager mm = getMagiskManager(); + MagiskManager mm = MagiskManager.get(); if (mm.snet_version != CheckSafetyNet.SNET_VER) { Shell.sh("rm -rf " + dexPath.getParent()); } @@ -72,13 +72,13 @@ public class CheckSafetyNet extends ParallelTask { Object helper = helperClazz.getConstructors()[0].newInstance( getActivity(), Proxy.newProxyInstance( loader, new Class[] { callbackClazz }, (proxy, method, args) -> { - getMagiskManager().safetyNetDone.publish(false, args[0]); + MagiskManager.get().safetyNetDone.publish(false, args[0]); return null; })); helperClazz.getMethod("attest").invoke(helper); } catch (Exception e) { e.printStackTrace(); - getMagiskManager().safetyNetDone.publish(false, -1); + MagiskManager.get().safetyNetDone.publish(false, -1); } super.onPostExecute(err); } diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/CheckUpdates.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/CheckUpdates.java index 0e94b4d45..6d50f0ee1 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/CheckUpdates.java +++ b/app/src/main/java/com/topjohnwu/magisk/asyncs/CheckUpdates.java @@ -1,7 +1,5 @@ package com.topjohnwu.magisk.asyncs; -import android.content.Context; - import com.topjohnwu.magisk.BuildConfig; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.utils.ShowUI; @@ -18,21 +16,19 @@ public class CheckUpdates extends ParallelTask { private static final String STABLE_URL = "https://raw.githubusercontent.com/topjohnwu/MagiskManager/update/stable.json"; private static final String BETA_URL = "https://raw.githubusercontent.com/topjohnwu/MagiskManager/update/beta.json"; - private boolean showNotification = false; + private boolean showNotification; - public CheckUpdates(Context context) { - super(context); + public CheckUpdates() { + this(false); } - public CheckUpdates(Context context, boolean b) { - super(context); + public CheckUpdates(boolean b) { showNotification = b; } @Override protected Void doInBackground(Void... voids) { - MagiskManager mm = getMagiskManager(); - if (mm == null) return null; + MagiskManager mm = MagiskManager.get(); String jsonStr; switch (mm.updateChannel) { case STABLE_CHANNEL: @@ -61,13 +57,12 @@ public class CheckUpdates extends ParallelTask { @Override protected void onPostExecute(Void v) { - MagiskManager mm = getMagiskManager(); - if (mm == null) return; + MagiskManager mm = MagiskManager.get(); if (showNotification && mm.updateNotification) { if (BuildConfig.VERSION_CODE < mm.remoteManagerVersionCode) { - ShowUI.showManagerUpdateNotification(mm); + ShowUI.showManagerUpdateNotification(); } else if (mm.magiskVersionCode < mm.remoteMagiskVersionCode) { - ShowUI.showMagiskUpdateNotification(mm); + ShowUI.showMagiskUpdateNotification(); } } mm.updateCheckDone.publish(); diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/DownloadBusybox.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/DownloadBusybox.java index a8e041b22..01292beab 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/DownloadBusybox.java +++ b/app/src/main/java/com/topjohnwu/magisk/asyncs/DownloadBusybox.java @@ -21,14 +21,10 @@ public class DownloadBusybox extends ParallelTask { private File busybox; - public DownloadBusybox(Context context) { - super(context); - busybox = new File(context.getCacheDir(), "busybox"); - } - @Override protected Void doInBackground(Void... voids) { - Context context = getMagiskManager(); + Context context = MagiskManager.get(); + busybox = new File(context.getCacheDir(), "busybox"); Utils.removeItem(context.getApplicationInfo().dataDir + "/busybox"); try { FileOutputStream out = new FileOutputStream(busybox); diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/FlashZip.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/FlashZip.java index 31ce9570a..2dce04092 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/FlashZip.java +++ b/app/src/main/java/com/topjohnwu/magisk/asyncs/FlashZip.java @@ -53,8 +53,7 @@ public class FlashZip extends ParallelTask { @Override protected Integer doInBackground(Void... voids) { - MagiskManager mm = getMagiskManager(); - if (mm == null) return -1; + MagiskManager mm = MagiskManager.get(); try { mList.add("- Copying zip to temp directory"); @@ -94,8 +93,7 @@ public class FlashZip extends ParallelTask { // -1 = error, manual install; 0 = invalid zip; 1 = success @Override protected void onPostExecute(Integer result) { - MagiskManager mm = getMagiskManager(); - if (mm == null) return; + MagiskManager mm = MagiskManager.get(); Shell.su_raw( "rm -rf " + mCachedFile.getParent(), "rm -rf " + MagiskManager.TMP_FOLDER_PATH @@ -110,7 +108,7 @@ public class FlashZip extends ParallelTask { break; case 1: // Success - new LoadModules(mm).exec(); + new LoadModules().exec(); break; } super.onPostExecute(result); diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/HideManager.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/HideManager.java index 5069ddd4a..a83ada077 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/HideManager.java +++ b/app/src/main/java/com/topjohnwu/magisk/asyncs/HideManager.java @@ -1,6 +1,5 @@ package com.topjohnwu.magisk.asyncs; -import android.content.Context; import android.content.pm.PackageManager; import android.os.Environment; import android.widget.Toast; @@ -24,10 +23,6 @@ public class HideManager extends ParallelTask { private static final String ANDROID_MANIFEST = "AndroidManifest.xml"; private static final byte[] UNHIDE_PKG_NAME = "com.topjohnwu.unhide\0".getBytes(); - public HideManager(Context context) { - super(context); - } - private String genPackageName(String prefix, int length) { StringBuilder builder = new StringBuilder(length); builder.append(prefix); @@ -51,14 +46,12 @@ public class HideManager extends ParallelTask { @Override protected void onPreExecute() { - getMagiskManager().toast(R.string.hide_manager_toast, Toast.LENGTH_SHORT); + MagiskManager.toast(R.string.hide_manager_toast, Toast.LENGTH_SHORT); } @Override protected Boolean doInBackground(Void... voids) { - MagiskManager mm = getMagiskManager(); - if (mm == null) - return false; + MagiskManager mm = MagiskManager.get(); // Generate a new unhide app with random package name File unhideAPK = new File(Environment.getExternalStorageDirectory() + "/MagiskManager", "unhide.apk"); @@ -127,11 +120,8 @@ public class HideManager extends ParallelTask { @Override protected void onPostExecute(Boolean b) { - MagiskManager mm = getMagiskManager(); - if (mm == null) - return; if (!b) { - mm.toast(R.string.hide_manager_fail_toast, Toast.LENGTH_LONG); + MagiskManager.toast(R.string.hide_manager_fail_toast, Toast.LENGTH_LONG); } super.onPostExecute(b); } diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java index 94e1604ef..75a95a896 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java +++ b/app/src/main/java/com/topjohnwu/magisk/asyncs/InstallMagisk.java @@ -72,10 +72,9 @@ public class InstallMagisk extends ParallelTask { @Override protected Boolean doInBackground(Void... voids) { - MagiskManager mm = getMagiskManager(); - if (mm == null) return false; + MagiskManager mm = MagiskManager.get(); - File install = new File(Utils.getEncContext(mm).getFilesDir().getParent(), "install"); + File install = new File(Utils.getEncContext().getFilesDir().getParent(), "install"); Shell.sh_raw("rm -rf " + install); List abis = Arrays.asList(Build.SUPPORTED_ABIS); diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/LoadModules.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/LoadModules.java index e1042d60a..858266a8e 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/LoadModules.java +++ b/app/src/main/java/com/topjohnwu/magisk/asyncs/LoadModules.java @@ -1,7 +1,5 @@ package com.topjohnwu.magisk.asyncs; -import android.content.Context; - import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.container.Module; import com.topjohnwu.magisk.container.ValueSortedMap; @@ -11,10 +9,6 @@ import java.util.List; public class LoadModules extends ParallelTask { - public LoadModules(Context context) { - super(context); - } - private List getModList() { String command = "ls -d " + MagiskManager.MAGISK_PATH + "/* | grep -v lost+found"; return Shell.su(command); @@ -22,8 +16,7 @@ public class LoadModules extends ParallelTask { @Override protected Void doInBackground(Void... voids) { - MagiskManager mm = getMagiskManager(); - if (mm == null) return null; + MagiskManager mm = MagiskManager.get(); mm.moduleMap = new ValueSortedMap<>(); for (String path : getModList()) { @@ -36,9 +29,7 @@ public class LoadModules extends ParallelTask { @Override protected void onPostExecute(Void v) { - MagiskManager mm = getMagiskManager(); - if (mm == null) return; - mm.moduleLoadDone.publish(); + MagiskManager.get().moduleLoadDone.publish(); super.onPostExecute(v); } } diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/MarkDownWindow.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/MarkDownWindow.java index 7a9b8cdf1..e5f74951e 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/MarkDownWindow.java +++ b/app/src/main/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.MagiskManager; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.utils.WebService; @@ -29,7 +30,7 @@ public class MarkDownWindow extends ParallelTask { Node doc = parser.parse(md); return String.format( " %s", - getMagiskManager().isDarkTheme ? "dark" : "light", renderer.render(doc)); + MagiskManager.get().isDarkTheme ? "dark" : "light", renderer.render(doc)); } @Override diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/ParallelTask.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/ParallelTask.java index 723d89b1d..5f087967d 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/ParallelTask.java +++ b/app/src/main/java/com/topjohnwu/magisk/asyncs/ParallelTask.java @@ -1,7 +1,6 @@ package com.topjohnwu.magisk.asyncs; import android.app.Activity; -import android.content.Context; import android.os.AsyncTask; import com.topjohnwu.magisk.MagiskManager; @@ -12,18 +11,12 @@ import java.lang.ref.WeakReference; public abstract class ParallelTask extends AsyncTask { private WeakReference weakActivity; - private WeakReference weakMagiskManager; private Runnable callback = null; public ParallelTask() {} - public ParallelTask(Context context) { - weakMagiskManager = new WeakReference<>(Utils.getMagiskManager(context)); - } - public ParallelTask(Activity context) { - this((Context) context); weakActivity = new WeakReference<>(context); } @@ -31,10 +24,6 @@ public abstract class ParallelTask extends AsyncTask

exec(Params... params) { executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params); diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java index 485379261..2eba9eb10 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java +++ b/app/src/main/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java @@ -10,6 +10,7 @@ import android.support.annotation.NonNull; import android.widget.Toast; import com.topjohnwu.magisk.FlashActivity; +import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.container.InputStreamWrapper; import com.topjohnwu.magisk.utils.Shell; @@ -175,7 +176,7 @@ public class ProcessRepoZip extends ParallelTask { Utils.showUriSnack(activity, uri); } } else { - Utils.getMagiskManager(activity).toast(R.string.process_error, Toast.LENGTH_LONG); + MagiskManager.toast(R.string.process_error, Toast.LENGTH_LONG); } super.onPostExecute(result); } diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/RestoreStockBoot.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/RestoreStockBoot.java index c67f4c75e..fd6717c0d 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/RestoreStockBoot.java +++ b/app/src/main/java/com/topjohnwu/magisk/asyncs/RestoreStockBoot.java @@ -1,8 +1,8 @@ package com.topjohnwu.magisk.asyncs; -import android.content.Context; import android.widget.Toast; +import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.utils.Shell; import com.topjohnwu.magisk.utils.Utils; @@ -13,8 +13,7 @@ public class RestoreStockBoot extends ParallelTask { private String mBoot; - public RestoreStockBoot(Context context, String boot) { - super(context); + public RestoreStockBoot(String boot) { mBoot = boot; } @@ -33,9 +32,9 @@ public class RestoreStockBoot extends ParallelTask { @Override protected void onPostExecute(Boolean result) { if (result) { - getMagiskManager().toast(R.string.restore_done, Toast.LENGTH_SHORT); + MagiskManager.toast(R.string.restore_done, Toast.LENGTH_SHORT); } else { - getMagiskManager().toast(R.string.restore_fail, Toast.LENGTH_LONG); + MagiskManager.toast(R.string.restore_fail, Toast.LENGTH_LONG); } } } diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/UpdateRepos.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/UpdateRepos.java index 17f3f8ab7..f301a97b1 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/UpdateRepos.java +++ b/app/src/main/java/com/topjohnwu/magisk/asyncs/UpdateRepos.java @@ -1,6 +1,5 @@ package com.topjohnwu.magisk.asyncs; -import android.content.Context; import android.content.SharedPreferences; import android.os.AsyncTask; @@ -44,13 +43,13 @@ public class UpdateRepos extends ParallelTask { private int tasks = 0; - public UpdateRepos(Context context, boolean force) { - super(context); - prefs = getMagiskManager().prefs; - repoDB = getMagiskManager().repoDB; - getMagiskManager().repoLoadDone.hasPublished = false; + public UpdateRepos(boolean force) { + MagiskManager mm = MagiskManager.get(); + prefs = mm.prefs; + repoDB = mm.repoDB; + mm.repoLoadDone.hasPublished = false; // Legacy data cleanup - File old = new File(context.getApplicationInfo().dataDir + "/shared_prefs", "RepoMap.xml"); + File old = new File(mm.getApplicationInfo().dataDir + "/shared_prefs", "RepoMap.xml"); if (old.exists() || prefs.getString("repomap", null) != null) { old.delete(); prefs.edit().remove("version").remove("repomap").remove(ETAG_KEY).apply(); @@ -211,9 +210,7 @@ public class UpdateRepos extends ParallelTask { @Override protected void onPostExecute(Void v) { - MagiskManager mm = getMagiskManager(); - if (mm == null) return; - mm.repoLoadDone.publish(); + MagiskManager.get().repoLoadDone.publish(); super.onPostExecute(v); } } diff --git a/app/src/main/java/com/topjohnwu/magisk/receivers/DownloadReceiver.java b/app/src/main/java/com/topjohnwu/magisk/receivers/DownloadReceiver.java index 979ab7a25..3500e02b4 100644 --- a/app/src/main/java/com/topjohnwu/magisk/receivers/DownloadReceiver.java +++ b/app/src/main/java/com/topjohnwu/magisk/receivers/DownloadReceiver.java @@ -8,6 +8,7 @@ import android.database.Cursor; import android.net.Uri; import android.widget.Toast; +import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.utils.Utils; @@ -34,7 +35,7 @@ public abstract class DownloadReceiver extends BroadcastReceiver { onDownloadDone(uri); break; default: - Utils.getMagiskManager(context).toast(R.string.download_file_error, Toast.LENGTH_LONG); + MagiskManager.toast(R.string.download_file_error, Toast.LENGTH_LONG); break; } context.unregisterReceiver(this); 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 b67aae8ca..be132f0f1 100644 --- a/app/src/main/java/com/topjohnwu/magisk/services/UpdateCheckService.java +++ b/app/src/main/java/com/topjohnwu/magisk/services/UpdateCheckService.java @@ -11,7 +11,7 @@ public class UpdateCheckService extends JobService { @Override public boolean onStartJob(JobParameters params) { Utils.getMagiskManager(this).getMagiskInfo(); - new CheckUpdates(this, true) + new CheckUpdates(true) .setCallBack(() -> jobFinished(params, false)).exec(); return true; } 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 167d90305..c576c8665 100644 --- a/app/src/main/java/com/topjohnwu/magisk/superuser/SuReceiver.java +++ b/app/src/main/java/com/topjohnwu/magisk/superuser/SuReceiver.java @@ -37,7 +37,7 @@ public class SuReceiver extends BroadcastReceiver { if (mode < 0) return; if (mode == NOTIFY_USER_TO_OWNER) { - magiskManager.toast(R.string.multiuser_hint_owner_request, Toast.LENGTH_LONG); + MagiskManager.toast(R.string.multiuser_hint_owner_request, Toast.LENGTH_LONG); 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 366a8620c..f98ab61dc 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Shell.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Shell.java @@ -1,6 +1,5 @@ package com.topjohnwu.magisk.utils; -import android.content.Context; import android.text.TextUtils; import com.topjohnwu.magisk.MagiskManager; @@ -10,7 +9,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; -import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -21,8 +19,6 @@ import java.util.List; public class Shell { - private static WeakReference weakMm; - // -1 = no shell; 0 = non root shell; 1 = root shell public static int status; @@ -47,12 +43,8 @@ public class Shell { STDOUT = process.getInputStream(); } - public static void registerShell(Context context) { - weakMm = new WeakReference<>(Utils.getMagiskManager(context)); - } - public static Shell getShell() { - MagiskManager mm = weakMm.get(); + MagiskManager mm = MagiskManager.get(); boolean needNewShell = mm.shell == null; if (!needNewShell) { diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/ShowUI.java b/app/src/main/java/com/topjohnwu/magisk/utils/ShowUI.java index a84b72b24..89f9b1576 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/ShowUI.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/ShowUI.java @@ -34,7 +34,8 @@ public class ShowUI { private static final int MAGISK_UPDATE_NOTIFICATION_ID = 1; private static final int APK_UPDATE_NOTIFICATION_ID = 2; - public static void showMagiskUpdateNotification(MagiskManager mm) { + public static void showMagiskUpdateNotification() { + MagiskManager mm = MagiskManager.get(); NotificationCompat.Builder builder = new NotificationCompat.Builder(mm, MagiskManager.NOTIFICATION_CHANNEL); builder.setSmallIcon(R.drawable.ic_magisk) .setContentTitle(mm.getString(R.string.magisk_update_title)) @@ -54,7 +55,8 @@ public class ShowUI { notificationManager.notify(MAGISK_UPDATE_NOTIFICATION_ID, builder.build()); } - public static void showManagerUpdateNotification(MagiskManager mm) { + public static void showManagerUpdateNotification() { + MagiskManager mm = MagiskManager.get(); NotificationCompat.Builder builder = new NotificationCompat.Builder(mm, MagiskManager.NOTIFICATION_CHANNEL); builder.setSmallIcon(R.drawable.ic_magisk) .setContentTitle(mm.getString(R.string.manager_update_title)) @@ -73,7 +75,7 @@ public class ShowUI { } public static void showMagiskInstallDialog(MagiskFragment fragment, boolean enc, boolean verity) { - MagiskManager mm = Utils.getMagiskManager(fragment.getActivity()); + MagiskManager mm = Utils.getMagiskManager(fragment.getContext()); String filename = Utils.getLegalFilename("Magisk-v" + mm.remoteMagiskVersionString + ".zip"); new AlertDialogBuilder(fragment.getActivity()) .setTitle(mm.getString(R.string.repo_install_title, mm.getString(R.string.magisk))) @@ -101,10 +103,10 @@ public class ShowUI { switch (idx) { case 1: if (mm.remoteMagiskVersionCode < 1400) { - mm.toast(R.string.no_boot_file_patch_support, Toast.LENGTH_LONG); + MagiskManager.toast(R.string.no_boot_file_patch_support, Toast.LENGTH_LONG); return; } - mm.toast(R.string.boot_file_patch_msg, Toast.LENGTH_LONG); + MagiskManager.toast(R.string.boot_file_patch_msg, Toast.LENGTH_LONG); Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("*/*"); fragment.startActivityForResult(intent, SELECT_BOOT_IMG, @@ -256,7 +258,7 @@ public class ShowUI { "cat " + uninstaller + " > /cache/" + UNINSTALLER, "cat " + utils + " > /data/magisk/" + Utils.UTIL_FUNCTIONS ); - mm.toast(R.string.uninstall_toast, Toast.LENGTH_LONG); + MagiskManager.toast(R.string.uninstall_toast, Toast.LENGTH_LONG); Shell.su_raw( "sleep 5", "pm uninstall " + mm.getApplicationInfo().packageName @@ -268,7 +270,7 @@ public class ShowUI { .setNeutralButton(R.string.restore_stock_boot, (d, i) -> { String boot = fragment.getSelectedBootImage(); if (boot == null) return; - new RestoreStockBoot(mm, boot).exec(); + new RestoreStockBoot(boot).exec(); }) .setNegativeButton(R.string.no_thanks, null) .show(); diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java index 48f37390a..52c6b641b 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java @@ -143,41 +143,43 @@ public class Utils { .setAction(R.string.ok, (v)->{}).show(); } - public static boolean checkNetworkStatus(Context context) { - ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + public static boolean checkNetworkStatus() { + ConnectivityManager manager = (ConnectivityManager) + MagiskManager.get().getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = manager.getActiveNetworkInfo(); return networkInfo != null && networkInfo.isConnected(); } - public static String getLocaleString(Context context, Locale locale, @StringRes int id) { + public static String getLocaleString(Locale locale, @StringRes int id) { + Context context = MagiskManager.get(); Configuration config = context.getResources().getConfiguration(); config.setLocale(locale); Context localizedContext = context.createConfigurationContext(config); return localizedContext.getString(id); } - public static List getAvailableLocale(Context context) { + public static List getAvailableLocale() { List locales = new ArrayList<>(); HashSet set = new HashSet<>(); Locale locale; - int compareId = R.string.download_file_error; + @StringRes int compareId = R.string.download_file_error; // Add default locale locales.add(Locale.ENGLISH); - set.add(getLocaleString(context, Locale.ENGLISH, compareId)); + set.add(getLocaleString(Locale.ENGLISH, compareId)); // Add some special locales locales.add(Locale.TAIWAN); - set.add(getLocaleString(context, Locale.TAIWAN, compareId)); + set.add(getLocaleString(Locale.TAIWAN, compareId)); locale = new Locale("pt", "BR"); locales.add(locale); - set.add(getLocaleString(context, locale, compareId)); + set.add(getLocaleString(locale, compareId)); // Other locales - for (String s : context.getAssets().getLocales()) { + for (String s : MagiskManager.get().getAssets().getLocales()) { locale = Locale.forLanguageTag(s); - if (set.add(getLocaleString(context, locale, compareId))) { + if (set.add(getLocaleString(locale, compareId))) { locales.add(locale); } } @@ -206,7 +208,8 @@ public class Utils { == DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE_PER_USER; } - public static Context getEncContext(Context context) { + public static Context getEncContext() { + Context context = MagiskManager.get(); if (useFDE(context)) return context.createDeviceProtectedStorageContext(); else