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();
mSwipeRefreshLayout.setRefreshing(true);
mSwipeRefreshLayout.setOnRefreshListener(() -> new Async.LoadApps(getApplication()).exec());
mSwipeRefreshLayout.setOnRefreshListener(() -> new Async.LoadApps(getActivity()).exec());
appAdapter = new ApplicationAdapter(packageManager);
recyclerView.setAdapter(appAdapter);

View File

@ -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<Void> blockDetectionDone = new CallbackEvent<>();

View File

@ -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() {

View File

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

View File

@ -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;
});

View File

@ -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);

View File

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

View File

@ -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());
}
}

View File

@ -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();
}
}

View File

@ -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<String> ret = Shell.sh("getprop persist.magisk.hide");

View File

@ -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)) {

View File

@ -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();

View File

@ -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);
}
}

View File

@ -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<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
public final void exec(Params... params) {
if (!Shell.rootAccess()) return;
@ -39,6 +48,15 @@ public class Async {
}
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
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<Void, Void, Void> {
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<Void, Void, Void> {
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<Void, Void, Void> {
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<Void, Void, Void> {
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<Void, Void, Void> {
MagiskManager magiskManager;
public GetBootBlocks(MagiskManager context) {
magiskManager = context;
public GetBootBlocks(Activity context) {
super(context);
}
@Override

View File

@ -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<String> 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();
}
}

View File

@ -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