From b07361580a9c213b45dbaf09cd0b0deb7d65977f Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 12 Feb 2017 05:02:18 +0800 Subject: [PATCH] Contexts are different: Make context clearer --- .../topjohnwu/magisk/MagiskHideFragment.java | 2 +- .../com/topjohnwu/magisk/MagiskManager.java | 1 - .../com/topjohnwu/magisk/ModulesFragment.java | 2 +- .../com/topjohnwu/magisk/ReposFragment.java | 2 +- .../topjohnwu/magisk/SettingsActivity.java | 4 +- .../com/topjohnwu/magisk/SplashActivity.java | 10 +-- .../com/topjohnwu/magisk/StatusFragment.java | 2 +- .../topjohnwu/magisk/components/Fragment.java | 3 +- .../topjohnwu/magisk/module/ModuleHelper.java | 10 +-- .../magisk/receivers/BootReceiver.java | 2 +- .../magisk/receivers/DownloadReceiver.java | 5 +- .../magisk/receivers/MagiskDlReceiver.java | 6 +- .../magisk/receivers/RepoDlReceiver.java | 12 ++-- .../com/topjohnwu/magisk/utils/Async.java | 69 ++++++++++--------- .../com/topjohnwu/magisk/utils/Utils.java | 9 +-- build.gradle | 2 +- 16 files changed, 72 insertions(+), 69 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java b/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java index 430937003..bf7e917e5 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskHideFragment.java @@ -50,7 +50,7 @@ public class MagiskHideFragment extends Fragment implements CallbackEvent.Listen PackageManager packageManager = getActivity().getPackageManager(); mSwipeRefreshLayout.setRefreshing(true); - mSwipeRefreshLayout.setOnRefreshListener(() -> new Async.LoadApps(getApplication()).exec()); + mSwipeRefreshLayout.setOnRefreshListener(() -> new Async.LoadApps(getActivity()).exec()); appAdapter = new ApplicationAdapter(packageManager); recyclerView.setAdapter(appAdapter); diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java b/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java index 6896ddf5d..67b99bd93 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java @@ -22,7 +22,6 @@ import java.util.List; public class MagiskManager extends Application { public static final String MAGISK_DISABLE_FILE = "/cache/.disable_magisk"; - public static final String MAGISK_MANAGER_BOOT = "/dev/.magisk_manager_boot"; // Events public final CallbackEvent blockDetectionDone = new CallbackEvent<>(); diff --git a/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java b/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java index 09fa73404..5bf296c23 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/ModulesFragment.java @@ -53,7 +53,7 @@ public class ModulesFragment extends Fragment implements CallbackEvent.Listener< mSwipeRefreshLayout.setOnRefreshListener(() -> { recyclerView.setVisibility(View.GONE); - new Async.LoadModules(getApplication()).exec(); + new Async.LoadModules(getActivity()).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 ea6c2aba8..b84ab1caf 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ReposFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/ReposFragment.java @@ -68,7 +68,7 @@ public class ReposFragment extends Fragment implements CallbackEvent.Listener { recyclerView.setVisibility(View.GONE); - new Async.LoadRepos(getApplication()).exec(); + new Async.LoadRepos(getActivity()).exec(); }); if (getApplication().repoLoadDone.isTriggered) { diff --git a/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java b/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java index 9c47fea90..9cbd87f1f 100644 --- a/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java @@ -78,7 +78,7 @@ public class SettingsActivity extends Activity { private ListPreference suAccess, autoRes, suNotification, requestTimeout; private MagiskManager getApplication() { - return (MagiskManager) getActivity().getApplication(); + return Utils.getMagiskManager(getActivity()); } @Override @@ -103,7 +103,7 @@ public class SettingsActivity extends Activity { setSummary(); findPreference("clear").setOnPreferenceClickListener((pref) -> { - ModuleHelper.clearRepoCache(getApplication()); + ModuleHelper.clearRepoCache(getActivity()); return true; }); diff --git a/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java b/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java index 6f756bff9..adb4f3ad4 100644 --- a/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java @@ -27,20 +27,20 @@ public class SplashActivity extends Activity { boolean started = Utils.isValidShellResponse(ret) && Integer.parseInt(ret.get(0)) != 0; // Now fire all async tasks - new Async.CheckUpdates(magiskManager).exec(); - new Async.GetBootBlocks(magiskManager).exec(); + new Async.CheckUpdates(this).exec(); + new Async.GetBootBlocks(this).exec(); if (magiskManager.magiskHide && !magiskManager.disabled && magiskManager.magiskVersion > 11 && !started) { new Async.MagiskHide().enable(); } - new Async.LoadModules(magiskManager) { + new Async.LoadModules(this) { @Override protected void onPostExecute(Void v) { super.onPostExecute(v); - new Async.LoadRepos(magiskManager).exec(); + new Async.LoadRepos(activity).exec(); } }.exec(); - new Async.LoadApps(magiskManager).exec(); + new Async.LoadApps(this).exec(); // Preparation done, now start main activity Intent intent = new Intent(getApplicationContext(), MainActivity.class); diff --git a/app/src/main/java/com/topjohnwu/magisk/StatusFragment.java b/app/src/main/java/com/topjohnwu/magisk/StatusFragment.java index 094a5c10c..2abc44b7d 100644 --- a/app/src/main/java/com/topjohnwu/magisk/StatusFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/StatusFragment.java @@ -84,7 +84,7 @@ public class StatusFragment extends Fragment implements CallbackEvent.Listener { diff --git a/app/src/main/java/com/topjohnwu/magisk/components/Fragment.java b/app/src/main/java/com/topjohnwu/magisk/components/Fragment.java index 4052088e0..1ce06b84b 100644 --- a/app/src/main/java/com/topjohnwu/magisk/components/Fragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/components/Fragment.java @@ -1,11 +1,12 @@ package com.topjohnwu.magisk.components; import com.topjohnwu.magisk.MagiskManager; +import com.topjohnwu.magisk.utils.Utils; public class Fragment extends android.support.v4.app.Fragment { public MagiskManager getApplication() { - return (MagiskManager) getActivity().getApplicationContext(); + return Utils.getMagiskManager(getActivity()); } } diff --git a/app/src/main/java/com/topjohnwu/magisk/module/ModuleHelper.java b/app/src/main/java/com/topjohnwu/magisk/module/ModuleHelper.java index a68d1770d..ad942684c 100644 --- a/app/src/main/java/com/topjohnwu/magisk/module/ModuleHelper.java +++ b/app/src/main/java/com/topjohnwu/magisk/module/ModuleHelper.java @@ -1,5 +1,6 @@ package com.topjohnwu.magisk.module; +import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.widget.Toast; @@ -142,15 +143,16 @@ public class ModuleHelper { Logger.dev("ModuleHelper: Repo load done"); } - public static void clearRepoCache(MagiskManager magiskManager) { - SharedPreferences repoMap = magiskManager.getSharedPreferences(FILE_KEY, Context.MODE_PRIVATE); + public static void clearRepoCache(Activity activity) { + MagiskManager magiskManager = Utils.getMagiskManager(activity); + SharedPreferences repoMap = activity.getSharedPreferences(FILE_KEY, Context.MODE_PRIVATE); repoMap.edit() .remove(ETAG_KEY) .remove(VERSION_KEY) .apply(); magiskManager.repoLoadDone.isTriggered = false; - new Async.LoadRepos(magiskManager).exec(); - Toast.makeText(magiskManager, R.string.repo_cache_cleared, Toast.LENGTH_SHORT).show(); + new Async.LoadRepos(activity).exec(); + Toast.makeText(activity, R.string.repo_cache_cleared, Toast.LENGTH_SHORT).show(); } } diff --git a/app/src/main/java/com/topjohnwu/magisk/receivers/BootReceiver.java b/app/src/main/java/com/topjohnwu/magisk/receivers/BootReceiver.java index 712c5e237..c360e1ad5 100644 --- a/app/src/main/java/com/topjohnwu/magisk/receivers/BootReceiver.java +++ b/app/src/main/java/com/topjohnwu/magisk/receivers/BootReceiver.java @@ -29,7 +29,7 @@ public class BootReceiver extends BroadcastReceiver { @Override protected void onHandleIntent(Intent intent) { - MagiskManager magiskManager = (MagiskManager) getApplicationContext(); + MagiskManager magiskManager = Utils.getMagiskManager(this); magiskManager.initSuAccess(); magiskManager.updateMagiskInfo(); List ret = Shell.sh("getprop persist.magisk.hide"); 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 726ce47fb..82253336f 100644 --- a/app/src/main/java/com/topjohnwu/magisk/receivers/DownloadReceiver.java +++ b/app/src/main/java/com/topjohnwu/magisk/receivers/DownloadReceiver.java @@ -1,5 +1,6 @@ package com.topjohnwu.magisk.receivers; +import android.app.Activity; import android.app.DownloadManager; import android.content.BroadcastReceiver; import android.content.Context; @@ -12,7 +13,7 @@ import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.utils.Utils; public abstract class DownloadReceiver extends BroadcastReceiver { - public Context mContext; + public Activity activity; public String mFilename; long downloadID; @@ -20,7 +21,7 @@ public abstract class DownloadReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { - mContext = context; + activity = (Activity) context; DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); String action = intent.getAction(); if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) { diff --git a/app/src/main/java/com/topjohnwu/magisk/receivers/MagiskDlReceiver.java b/app/src/main/java/com/topjohnwu/magisk/receivers/MagiskDlReceiver.java index eed7ecf33..f6be0eb2e 100644 --- a/app/src/main/java/com/topjohnwu/magisk/receivers/MagiskDlReceiver.java +++ b/app/src/main/java/com/topjohnwu/magisk/receivers/MagiskDlReceiver.java @@ -23,7 +23,7 @@ public class MagiskDlReceiver extends DownloadReceiver { @Override public void onDownloadDone(Uri uri) { - new Async.FlashZIP(mContext, uri, mFilename) { + new Async.FlashZIP(activity, uri, mFilename) { @Override protected void preProcessing() throws Throwable { @@ -36,7 +36,7 @@ public class MagiskDlReceiver extends DownloadReceiver { @Override protected boolean unzipAndCheck() { - publishProgress(mContext.getString(R.string.zip_install_unzip_zip_msg)); + publishProgress(activity.getString(R.string.zip_install_unzip_zip_msg)); if (Shell.rootAccess()) { // We might not have busybox yet, unzip with Java // We will have complete busybox after Magisk installation @@ -56,7 +56,7 @@ public class MagiskDlReceiver extends DownloadReceiver { @Override protected Void doInBackground(Void... params) { Shell.su("setprop magisk.version " - + String.valueOf(((MagiskManager) mContext.getApplicationContext()).remoteMagiskVersion)); + + String.valueOf(((MagiskManager) activity.getApplicationContext()).remoteMagiskVersion)); return null; } }.exec(); diff --git a/app/src/main/java/com/topjohnwu/magisk/receivers/RepoDlReceiver.java b/app/src/main/java/com/topjohnwu/magisk/receivers/RepoDlReceiver.java index 212f12f1b..c402c3681 100644 --- a/app/src/main/java/com/topjohnwu/magisk/receivers/RepoDlReceiver.java +++ b/app/src/main/java/com/topjohnwu/magisk/receivers/RepoDlReceiver.java @@ -13,27 +13,27 @@ public class RepoDlReceiver extends DownloadReceiver { @Override public void onDownloadDone(Uri uri) { // Flash the zip - new Async.FlashZIP(mContext, uri, mFilename){ + new Async.FlashZIP(activity, uri, mFilename){ @Override protected void preProcessing() throws Throwable { // Process and sign the zip - publishProgress(mContext.getString(R.string.zip_install_process_zip_msg)); + publishProgress(activity.getString(R.string.zip_install_process_zip_msg)); ByteArrayInOutStream buffer = new ByteArrayInOutStream(); // First remove top folder (the folder with the repo name) in Github source zip - ZipUtils.removeTopFolder(mContext.getContentResolver().openInputStream(mUri), buffer); + ZipUtils.removeTopFolder(activity.getContentResolver().openInputStream(mUri), buffer); // Then sign the zip for the first time - ZipUtils.signZip(mContext, buffer.getInputStream(), buffer, false); + ZipUtils.signZip(activity, buffer.getInputStream(), buffer, false); // Adjust the zip to prevent unzip issues ZipUtils.adjustZip(buffer); // Finally, sign the whole zip file again - ZipUtils.signZip(mContext, buffer.getInputStream(), buffer, true); + ZipUtils.signZip(activity, buffer.getInputStream(), buffer, true); // Write it back to the downloaded zip - try (OutputStream out = mContext.getContentResolver().openOutputStream(mUri)) { + try (OutputStream out = activity.getContentResolver().openOutputStream(mUri)) { buffer.writeTo(out); } } diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Async.java b/app/src/main/java/com/topjohnwu/magisk/utils/Async.java index 4d80a6edd..499ff741b 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Async.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Async.java @@ -1,7 +1,7 @@ package com.topjohnwu.magisk.utils; +import android.app.Activity; import android.app.ProgressDialog; -import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.database.Cursor; @@ -31,6 +31,15 @@ import java.util.List; public class Async { public abstract static class RootTask extends AsyncTask { + protected Activity activity; + protected MagiskManager magiskManager; + + public RootTask() {} + + public RootTask(Activity context) { + activity = context; + magiskManager = Utils.getMagiskManager(context); + } @SafeVarargs public final void exec(Params... params) { if (!Shell.rootAccess()) return; @@ -39,6 +48,15 @@ public class Async { } public abstract static class NormalTask extends AsyncTask { + protected Activity activity; + protected MagiskManager magiskManager; + + public NormalTask() {} + + public NormalTask(Activity context) { + activity = context; + magiskManager = Utils.getMagiskManager(context); + } @SafeVarargs public final void exec(Params... params) { executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params); @@ -51,10 +69,8 @@ public class Async { public static class CheckUpdates extends NormalTask { - MagiskManager magiskManager; - - public CheckUpdates(MagiskManager context) { - magiskManager = context; + public CheckUpdates(Activity context) { + super(context); } @Override @@ -88,11 +104,10 @@ public class Async { public static class LoadModules extends RootTask { - protected MagiskManager magiskManager; - - public LoadModules(MagiskManager context) { - magiskManager = context; + public LoadModules(Activity context) { + super(context); } + @Override protected Void doInBackground(Void... voids) { ModuleHelper.createModuleMap(magiskManager); @@ -107,10 +122,8 @@ public class Async { public static class LoadRepos extends NormalTask { - private MagiskManager magiskManager; - - public LoadRepos(MagiskManager context) { - magiskManager = context; + public LoadRepos(Activity context) { + super(context); } @Override @@ -127,10 +140,8 @@ public class Async { public static class LoadApps extends RootTask { - private MagiskManager magiskManager; - - public LoadApps(MagiskManager context) { - magiskManager = context; + public LoadApps(Activity context) { + super(context); } @Override @@ -160,19 +171,15 @@ public class Async { protected File mCachedFile; private String mFilename; protected ProgressDialog progress; - private Context mContext; - private MagiskManager magiskManager; - public FlashZIP(Context context, Uri uri, String filename) { - mContext = context; - magiskManager = (MagiskManager) context.getApplicationContext(); + public FlashZIP(Activity context, Uri uri, String filename) { + super(context); mUri = uri; mFilename = filename; } - public FlashZIP(Context context, Uri uri) { - mContext = context; - magiskManager = (MagiskManager) context.getApplicationContext(); + public FlashZIP(Activity context, Uri uri) { + super(context); mUri = uri; // Try to get the filename ourselves @@ -229,7 +236,7 @@ public class Async { @Override protected void onPreExecute() { - progress = new ProgressDialog(mContext); + progress = new ProgressDialog(activity); progress.setTitle(R.string.zip_install_progress_title); progress.show(); } @@ -294,9 +301,9 @@ public class Async { protected void onSuccess() { magiskManager.updateCheckDone.trigger(); - new LoadModules(magiskManager).exec(); + new LoadModules(activity).exec(); - Utils.getAlertDialogBuilder(mContext) + Utils.getAlertDialogBuilder(activity) .setTitle(R.string.reboot_title) .setMessage(R.string.reboot_msg) .setPositiveButton(R.string.reboot, (dialogInterface, i) -> Shell.su(true, "reboot")) @@ -334,10 +341,8 @@ public class Async { public static class GetBootBlocks extends RootTask { - MagiskManager magiskManager; - - public GetBootBlocks(MagiskManager context) { - magiskManager = context; + public GetBootBlocks(Activity context) { + super(context); } @Override 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 6d4557adc..628d9a486 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java @@ -140,13 +140,8 @@ public class Utils { return Integer.parseInt(prefs.getString(key, String.valueOf(def))); } - public static void checkAndStartMagiskHide() { - String command = "ps | grep magiskhide >/dev/null; echo $?"; - List ret = Shell.su(command); - if (!isValidShellResponse(ret)) - return; - if (Integer.parseInt(ret.get(0)) != 0) - new Async.MagiskHide().enable(); + public static MagiskManager getMagiskManager(Context context) { + return (MagiskManager) context.getApplicationContext(); } } \ No newline at end of file diff --git a/build.gradle b/build.gradle index 2360186a4..bfddb4b1d 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ buildscript { mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:2.3.0-beta3' + classpath 'com.android.tools.build:gradle:2.3.0-beta4' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files