Contexts are different: Make context clearer

This commit is contained in:
topjohnwu 2017-02-12 05:02:18 +08:00
parent d1b5ebad7d
commit b07361580a
16 changed files with 72 additions and 69 deletions

View File

@ -50,7 +50,7 @@ public class MagiskHideFragment extends Fragment implements CallbackEvent.Listen
PackageManager packageManager = getActivity().getPackageManager(); PackageManager packageManager = getActivity().getPackageManager();
mSwipeRefreshLayout.setRefreshing(true); mSwipeRefreshLayout.setRefreshing(true);
mSwipeRefreshLayout.setOnRefreshListener(() -> new Async.LoadApps(getApplication()).exec()); mSwipeRefreshLayout.setOnRefreshListener(() -> new Async.LoadApps(getActivity()).exec());
appAdapter = new ApplicationAdapter(packageManager); appAdapter = new ApplicationAdapter(packageManager);
recyclerView.setAdapter(appAdapter); recyclerView.setAdapter(appAdapter);

View File

@ -22,7 +22,6 @@ import java.util.List;
public class MagiskManager extends Application { public class MagiskManager extends Application {
public static final String MAGISK_DISABLE_FILE = "/cache/.disable_magisk"; public static final String MAGISK_DISABLE_FILE = "/cache/.disable_magisk";
public static final String MAGISK_MANAGER_BOOT = "/dev/.magisk_manager_boot";
// Events // Events
public final CallbackEvent<Void> blockDetectionDone = new CallbackEvent<>(); public final CallbackEvent<Void> blockDetectionDone = new CallbackEvent<>();

View File

@ -53,7 +53,7 @@ public class ModulesFragment extends Fragment implements CallbackEvent.Listener<
mSwipeRefreshLayout.setOnRefreshListener(() -> { mSwipeRefreshLayout.setOnRefreshListener(() -> {
recyclerView.setVisibility(View.GONE); recyclerView.setVisibility(View.GONE);
new Async.LoadModules(getApplication()).exec(); new Async.LoadModules(getActivity()).exec();
}); });
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

View File

@ -68,7 +68,7 @@ public class ReposFragment extends Fragment implements CallbackEvent.Listener<Vo
mSwipeRefreshLayout.setOnRefreshListener(() -> { mSwipeRefreshLayout.setOnRefreshListener(() -> {
recyclerView.setVisibility(View.GONE); recyclerView.setVisibility(View.GONE);
new Async.LoadRepos(getApplication()).exec(); new Async.LoadRepos(getActivity()).exec();
}); });
if (getApplication().repoLoadDone.isTriggered) { if (getApplication().repoLoadDone.isTriggered) {

View File

@ -78,7 +78,7 @@ public class SettingsActivity extends Activity {
private ListPreference suAccess, autoRes, suNotification, requestTimeout; private ListPreference suAccess, autoRes, suNotification, requestTimeout;
private MagiskManager getApplication() { private MagiskManager getApplication() {
return (MagiskManager) getActivity().getApplication(); return Utils.getMagiskManager(getActivity());
} }
@Override @Override
@ -103,7 +103,7 @@ public class SettingsActivity extends Activity {
setSummary(); setSummary();
findPreference("clear").setOnPreferenceClickListener((pref) -> { findPreference("clear").setOnPreferenceClickListener((pref) -> {
ModuleHelper.clearRepoCache(getApplication()); ModuleHelper.clearRepoCache(getActivity());
return true; return true;
}); });

View File

@ -27,20 +27,20 @@ public class SplashActivity extends Activity {
boolean started = Utils.isValidShellResponse(ret) && Integer.parseInt(ret.get(0)) != 0; boolean started = Utils.isValidShellResponse(ret) && Integer.parseInt(ret.get(0)) != 0;
// Now fire all async tasks // Now fire all async tasks
new Async.CheckUpdates(magiskManager).exec(); new Async.CheckUpdates(this).exec();
new Async.GetBootBlocks(magiskManager).exec(); new Async.GetBootBlocks(this).exec();
if (magiskManager.magiskHide && !magiskManager.disabled && if (magiskManager.magiskHide && !magiskManager.disabled &&
magiskManager.magiskVersion > 11 && !started) { magiskManager.magiskVersion > 11 && !started) {
new Async.MagiskHide().enable(); new Async.MagiskHide().enable();
} }
new Async.LoadModules(magiskManager) { new Async.LoadModules(this) {
@Override @Override
protected void onPostExecute(Void v) { protected void onPostExecute(Void v) {
super.onPostExecute(v); super.onPostExecute(v);
new Async.LoadRepos(magiskManager).exec(); new Async.LoadRepos(activity).exec();
} }
}.exec(); }.exec();
new Async.LoadApps(magiskManager).exec(); new Async.LoadApps(this).exec();
// Preparation done, now start main activity // Preparation done, now start main activity
Intent intent = new Intent(getApplicationContext(), MainActivity.class); Intent intent = new Intent(getApplicationContext(), MainActivity.class);

View File

@ -84,7 +84,7 @@ public class StatusFragment extends Fragment implements CallbackEvent.Listener<V
noDialog = false; noDialog = false;
updateUI(); updateUI();
new Async.CheckUpdates(getApplication()).exec(); new Async.CheckUpdates(getActivity()).exec();
}); });
safetyNetContainer.setOnClickListener(view -> { safetyNetContainer.setOnClickListener(view -> {

View File

@ -1,11 +1,12 @@
package com.topjohnwu.magisk.components; package com.topjohnwu.magisk.components;
import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.utils.Utils;
public class Fragment extends android.support.v4.app.Fragment { public class Fragment extends android.support.v4.app.Fragment {
public MagiskManager getApplication() { public MagiskManager getApplication() {
return (MagiskManager) getActivity().getApplicationContext(); return Utils.getMagiskManager(getActivity());
} }
} }

View File

@ -1,5 +1,6 @@
package com.topjohnwu.magisk.module; package com.topjohnwu.magisk.module;
import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.widget.Toast; import android.widget.Toast;
@ -142,15 +143,16 @@ public class ModuleHelper {
Logger.dev("ModuleHelper: Repo load done"); Logger.dev("ModuleHelper: Repo load done");
} }
public static void clearRepoCache(MagiskManager magiskManager) { public static void clearRepoCache(Activity activity) {
SharedPreferences repoMap = magiskManager.getSharedPreferences(FILE_KEY, Context.MODE_PRIVATE); MagiskManager magiskManager = Utils.getMagiskManager(activity);
SharedPreferences repoMap = activity.getSharedPreferences(FILE_KEY, Context.MODE_PRIVATE);
repoMap.edit() repoMap.edit()
.remove(ETAG_KEY) .remove(ETAG_KEY)
.remove(VERSION_KEY) .remove(VERSION_KEY)
.apply(); .apply();
magiskManager.repoLoadDone.isTriggered = false; magiskManager.repoLoadDone.isTriggered = false;
new Async.LoadRepos(magiskManager).exec(); new Async.LoadRepos(activity).exec();
Toast.makeText(magiskManager, R.string.repo_cache_cleared, Toast.LENGTH_SHORT).show(); Toast.makeText(activity, R.string.repo_cache_cleared, Toast.LENGTH_SHORT).show();
} }
} }

View File

@ -29,7 +29,7 @@ public class BootReceiver extends BroadcastReceiver {
@Override @Override
protected void onHandleIntent(Intent intent) { protected void onHandleIntent(Intent intent) {
MagiskManager magiskManager = (MagiskManager) getApplicationContext(); MagiskManager magiskManager = Utils.getMagiskManager(this);
magiskManager.initSuAccess(); magiskManager.initSuAccess();
magiskManager.updateMagiskInfo(); magiskManager.updateMagiskInfo();
List<String> ret = Shell.sh("getprop persist.magisk.hide"); List<String> ret = Shell.sh("getprop persist.magisk.hide");

View File

@ -1,5 +1,6 @@
package com.topjohnwu.magisk.receivers; package com.topjohnwu.magisk.receivers;
import android.app.Activity;
import android.app.DownloadManager; import android.app.DownloadManager;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
@ -12,7 +13,7 @@ import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.Utils;
public abstract class DownloadReceiver extends BroadcastReceiver { public abstract class DownloadReceiver extends BroadcastReceiver {
public Context mContext; public Activity activity;
public String mFilename; public String mFilename;
long downloadID; long downloadID;
@ -20,7 +21,7 @@ public abstract class DownloadReceiver extends BroadcastReceiver {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
mContext = context; activity = (Activity) context;
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
String action = intent.getAction(); String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) { if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {

View File

@ -23,7 +23,7 @@ public class MagiskDlReceiver extends DownloadReceiver {
@Override @Override
public void onDownloadDone(Uri uri) { public void onDownloadDone(Uri uri) {
new Async.FlashZIP(mContext, uri, mFilename) { new Async.FlashZIP(activity, uri, mFilename) {
@Override @Override
protected void preProcessing() throws Throwable { protected void preProcessing() throws Throwable {
@ -36,7 +36,7 @@ public class MagiskDlReceiver extends DownloadReceiver {
@Override @Override
protected boolean unzipAndCheck() { 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()) { if (Shell.rootAccess()) {
// We might not have busybox yet, unzip with Java // We might not have busybox yet, unzip with Java
// We will have complete busybox after Magisk installation // We will have complete busybox after Magisk installation
@ -56,7 +56,7 @@ public class MagiskDlReceiver extends DownloadReceiver {
@Override @Override
protected Void doInBackground(Void... params) { protected Void doInBackground(Void... params) {
Shell.su("setprop magisk.version " Shell.su("setprop magisk.version "
+ String.valueOf(((MagiskManager) mContext.getApplicationContext()).remoteMagiskVersion)); + String.valueOf(((MagiskManager) activity.getApplicationContext()).remoteMagiskVersion));
return null; return null;
} }
}.exec(); }.exec();

View File

@ -13,27 +13,27 @@ public class RepoDlReceiver extends DownloadReceiver {
@Override @Override
public void onDownloadDone(Uri uri) { public void onDownloadDone(Uri uri) {
// Flash the zip // Flash the zip
new Async.FlashZIP(mContext, uri, mFilename){ new Async.FlashZIP(activity, uri, mFilename){
@Override @Override
protected void preProcessing() throws Throwable { protected void preProcessing() throws Throwable {
// Process and sign the zip // 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(); ByteArrayInOutStream buffer = new ByteArrayInOutStream();
// First remove top folder (the folder with the repo name) in Github source zip // 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 // 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 // Adjust the zip to prevent unzip issues
ZipUtils.adjustZip(buffer); ZipUtils.adjustZip(buffer);
// Finally, sign the whole zip file again // 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 // Write it back to the downloaded zip
try (OutputStream out = mContext.getContentResolver().openOutputStream(mUri)) { try (OutputStream out = activity.getContentResolver().openOutputStream(mUri)) {
buffer.writeTo(out); buffer.writeTo(out);
} }
} }

View File

@ -1,7 +1,7 @@
package com.topjohnwu.magisk.utils; package com.topjohnwu.magisk.utils;
import android.app.Activity;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.database.Cursor; import android.database.Cursor;
@ -31,6 +31,15 @@ import java.util.List;
public class Async { public class Async {
public abstract static class RootTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> { public abstract static class RootTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {
protected Activity activity;
protected MagiskManager magiskManager;
public RootTask() {}
public RootTask(Activity context) {
activity = context;
magiskManager = Utils.getMagiskManager(context);
}
@SafeVarargs @SafeVarargs
public final void exec(Params... params) { public final void exec(Params... params) {
if (!Shell.rootAccess()) return; if (!Shell.rootAccess()) return;
@ -39,6 +48,15 @@ public class Async {
} }
public abstract static class NormalTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> { public abstract static class NormalTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {
protected Activity activity;
protected MagiskManager magiskManager;
public NormalTask() {}
public NormalTask(Activity context) {
activity = context;
magiskManager = Utils.getMagiskManager(context);
}
@SafeVarargs @SafeVarargs
public final void exec(Params... params) { public final void exec(Params... params) {
executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params); executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
@ -51,10 +69,8 @@ public class Async {
public static class CheckUpdates extends NormalTask<Void, Void, Void> { public static class CheckUpdates extends NormalTask<Void, Void, Void> {
MagiskManager magiskManager; public CheckUpdates(Activity context) {
super(context);
public CheckUpdates(MagiskManager context) {
magiskManager = context;
} }
@Override @Override
@ -88,11 +104,10 @@ public class Async {
public static class LoadModules extends RootTask<Void, Void, Void> { public static class LoadModules extends RootTask<Void, Void, Void> {
protected MagiskManager magiskManager; public LoadModules(Activity context) {
super(context);
public LoadModules(MagiskManager context) {
magiskManager = context;
} }
@Override @Override
protected Void doInBackground(Void... voids) { protected Void doInBackground(Void... voids) {
ModuleHelper.createModuleMap(magiskManager); ModuleHelper.createModuleMap(magiskManager);
@ -107,10 +122,8 @@ public class Async {
public static class LoadRepos extends NormalTask<Void, Void, Void> { public static class LoadRepos extends NormalTask<Void, Void, Void> {
private MagiskManager magiskManager; public LoadRepos(Activity context) {
super(context);
public LoadRepos(MagiskManager context) {
magiskManager = context;
} }
@Override @Override
@ -127,10 +140,8 @@ public class Async {
public static class LoadApps extends RootTask<Void, Void, Void> { public static class LoadApps extends RootTask<Void, Void, Void> {
private MagiskManager magiskManager; public LoadApps(Activity context) {
super(context);
public LoadApps(MagiskManager context) {
magiskManager = context;
} }
@Override @Override
@ -160,19 +171,15 @@ public class Async {
protected File mCachedFile; protected File mCachedFile;
private String mFilename; private String mFilename;
protected ProgressDialog progress; protected ProgressDialog progress;
private Context mContext;
private MagiskManager magiskManager;
public FlashZIP(Context context, Uri uri, String filename) { public FlashZIP(Activity context, Uri uri, String filename) {
mContext = context; super(context);
magiskManager = (MagiskManager) context.getApplicationContext();
mUri = uri; mUri = uri;
mFilename = filename; mFilename = filename;
} }
public FlashZIP(Context context, Uri uri) { public FlashZIP(Activity context, Uri uri) {
mContext = context; super(context);
magiskManager = (MagiskManager) context.getApplicationContext();
mUri = uri; mUri = uri;
// Try to get the filename ourselves // Try to get the filename ourselves
@ -229,7 +236,7 @@ public class Async {
@Override @Override
protected void onPreExecute() { protected void onPreExecute() {
progress = new ProgressDialog(mContext); progress = new ProgressDialog(activity);
progress.setTitle(R.string.zip_install_progress_title); progress.setTitle(R.string.zip_install_progress_title);
progress.show(); progress.show();
} }
@ -294,9 +301,9 @@ public class Async {
protected void onSuccess() { protected void onSuccess() {
magiskManager.updateCheckDone.trigger(); magiskManager.updateCheckDone.trigger();
new LoadModules(magiskManager).exec(); new LoadModules(activity).exec();
Utils.getAlertDialogBuilder(mContext) Utils.getAlertDialogBuilder(activity)
.setTitle(R.string.reboot_title) .setTitle(R.string.reboot_title)
.setMessage(R.string.reboot_msg) .setMessage(R.string.reboot_msg)
.setPositiveButton(R.string.reboot, (dialogInterface, i) -> Shell.su(true, "reboot")) .setPositiveButton(R.string.reboot, (dialogInterface, i) -> Shell.su(true, "reboot"))
@ -334,10 +341,8 @@ public class Async {
public static class GetBootBlocks extends RootTask<Void, Void, Void> { public static class GetBootBlocks extends RootTask<Void, Void, Void> {
MagiskManager magiskManager; public GetBootBlocks(Activity context) {
super(context);
public GetBootBlocks(MagiskManager context) {
magiskManager = context;
} }
@Override @Override

View File

@ -140,13 +140,8 @@ public class Utils {
return Integer.parseInt(prefs.getString(key, String.valueOf(def))); return Integer.parseInt(prefs.getString(key, String.valueOf(def)));
} }
public static void checkAndStartMagiskHide() { public static MagiskManager getMagiskManager(Context context) {
String command = "ps | grep magiskhide >/dev/null; echo $?"; return (MagiskManager) context.getApplicationContext();
List<String> ret = Shell.su(command);
if (!isValidShellResponse(ret))
return;
if (Integer.parseInt(ret.get(0)) != 0)
new Async.MagiskHide().enable();
} }
} }

View File

@ -6,7 +6,7 @@ buildscript {
mavenCentral() mavenCentral()
} }
dependencies { 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 // NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files // in the individual module build.gradle files