From c5a7ab24151526224fc1fa5ced32b0c2b9a0fd73 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 9 Jun 2018 17:14:24 +0800 Subject: [PATCH] Move runWithPermission method --- .../topjohnwu/magisk/MagiskLogFragment.java | 4 +- .../com/topjohnwu/magisk/ModulesFragment.java | 4 +- .../topjohnwu/magisk/SettingsActivity.java | 2 +- .../magisk/asyncs/ProcessRepoZip.java | 4 +- .../topjohnwu/magisk/components/Fragment.java | 4 ++ .../com/topjohnwu/magisk/utils/ShowUI.java | 2 +- .../topjohnwu/magisk/components/Activity.java | 41 +++++++++++++++++-- .../magisk/components/Application.java | 6 --- .../com/topjohnwu/magisk/utils/Utils.java | 38 +++-------------- .../com/topjohnwu/magisk/SplashActivity.java | 2 +- 10 files changed, 54 insertions(+), 53 deletions(-) diff --git a/src/full/java/com/topjohnwu/magisk/MagiskLogFragment.java b/src/full/java/com/topjohnwu/magisk/MagiskLogFragment.java index 73f08c1bf..ea29e5f8e 100644 --- a/src/full/java/com/topjohnwu/magisk/MagiskLogFragment.java +++ b/src/full/java/com/topjohnwu/magisk/MagiskLogFragment.java @@ -21,7 +21,6 @@ import com.topjohnwu.magisk.asyncs.ParallelTask; import com.topjohnwu.magisk.components.Fragment; import com.topjohnwu.magisk.components.SnackbarMaker; import com.topjohnwu.magisk.utils.Const; -import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.superuser.CallbackList; import com.topjohnwu.superuser.Shell; @@ -88,8 +87,7 @@ public class MagiskLogFragment extends Fragment { new LogManager().read(); return true; case R.id.menu_save: - Utils.runWithPermission(getActivity(), - new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, + runWithPermission(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, () -> new LogManager().save()); return true; case R.id.menu_clear: diff --git a/src/full/java/com/topjohnwu/magisk/ModulesFragment.java b/src/full/java/com/topjohnwu/magisk/ModulesFragment.java index 679923ef3..db3a79ebd 100644 --- a/src/full/java/com/topjohnwu/magisk/ModulesFragment.java +++ b/src/full/java/com/topjohnwu/magisk/ModulesFragment.java @@ -21,7 +21,6 @@ import com.topjohnwu.magisk.components.Fragment; import com.topjohnwu.magisk.container.Module; import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.Topic; -import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.superuser.Shell; import java.util.ArrayList; @@ -40,8 +39,7 @@ public class ModulesFragment extends Fragment implements Topic.Subscriber { @BindView(R.id.empty_rv) TextView emptyRv; @OnClick(R.id.fab) public void selectFile() { - Utils.runWithPermission(getActivity(), - new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },() -> { + runWithPermission(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, () -> { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("application/zip"); startActivityForResult(intent, Const.ID.FETCH_ZIP); diff --git a/src/full/java/com/topjohnwu/magisk/SettingsActivity.java b/src/full/java/com/topjohnwu/magisk/SettingsActivity.java index fef383b83..8bac8ec95 100644 --- a/src/full/java/com/topjohnwu/magisk/SettingsActivity.java +++ b/src/full/java/com/topjohnwu/magisk/SettingsActivity.java @@ -166,7 +166,7 @@ public class SettingsActivity extends Activity implements Topic.Subscriber { } else { if (Utils.checkNetworkStatus()) { restoreManager.setOnPreferenceClickListener((pref) -> { - Utils.runWithPermission(getActivity(), + runWithPermission(getActivity(), new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, () -> { Intent intent = new Intent(mm, ManagerUpdate.class); intent.putExtra(Const.Key.INTENT_SET_LINK, mm.managerLink); diff --git a/src/full/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java b/src/full/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java index 5530256e0..405193a3b 100644 --- a/src/full/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java +++ b/src/full/java/com/topjohnwu/magisk/asyncs/ProcessRepoZip.java @@ -14,7 +14,6 @@ import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.components.SnackbarMaker; import com.topjohnwu.magisk.utils.Const; -import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.WebService; import com.topjohnwu.magisk.utils.ZipUtils; import com.topjohnwu.superuser.Shell; @@ -158,7 +157,8 @@ public class ProcessRepoZip extends ParallelTask { @Override public ParallelTask exec(Void... voids) { - Utils.runWithPermission(getActivity(), new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, + com.topjohnwu.magisk.components.Activity.runWithPermission( + getActivity(), new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, () -> super.exec(voids)); return this; } diff --git a/src/full/java/com/topjohnwu/magisk/components/Fragment.java b/src/full/java/com/topjohnwu/magisk/components/Fragment.java index 0bc84902c..3ca503821 100644 --- a/src/full/java/com/topjohnwu/magisk/components/Fragment.java +++ b/src/full/java/com/topjohnwu/magisk/components/Fragment.java @@ -36,4 +36,8 @@ public class Fragment extends android.support.v4.app.Fragment { public void startActivityForResult(Intent intent, int requestCode, Activity.ActivityResultListener listener) { ((Activity) getActivity()).startActivityForResult(intent, requestCode, listener); } + + public void runWithPermission(String[] permissions, Runnable callback) { + Activity.runWithPermission(getActivity(), permissions, callback); + } } diff --git a/src/full/java/com/topjohnwu/magisk/utils/ShowUI.java b/src/full/java/com/topjohnwu/magisk/utils/ShowUI.java index 11dea4ad2..009005504 100644 --- a/src/full/java/com/topjohnwu/magisk/utils/ShowUI.java +++ b/src/full/java/com/topjohnwu/magisk/utils/ShowUI.java @@ -255,7 +255,7 @@ public class ShowUI { .setMessage(mm.getString(R.string.repo_install_msg, filename)) .setCancelable(true) .setPositiveButton(R.string.install, (d, i) -> { - Utils.runWithPermission(activity, + com.topjohnwu.magisk.components.Activity.runWithPermission(activity, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, () -> { Intent intent = new Intent(mm, ManagerUpdate.class); intent.putExtra(Const.Key.INTENT_SET_LINK, mm.managerLink); diff --git a/src/main/java/com/topjohnwu/magisk/components/Activity.java b/src/main/java/com/topjohnwu/magisk/components/Activity.java index c50c00dfd..6a5ada4a7 100644 --- a/src/main/java/com/topjohnwu/magisk/components/Activity.java +++ b/src/main/java/com/topjohnwu/magisk/components/Activity.java @@ -1,16 +1,23 @@ package com.topjohnwu.magisk.components; +import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.res.Configuration; import android.support.annotation.NonNull; +import android.support.v4.app.ActivityCompat; +import android.support.v4.content.ContextCompat; import android.widget.Toast; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; +import com.topjohnwu.magisk.SplashActivity; +import com.topjohnwu.magisk.utils.Const; public abstract class Activity extends FlavorActivity { + private static Runnable permissionGrantCallback; + private ActivityResultListener activityResultListener; public Activity() { @@ -20,6 +27,33 @@ public abstract class Activity extends FlavorActivity { applyOverrideConfiguration(configuration); } + public static void runWithPermission(Context context, String[] permissions, Runnable callback) { + boolean granted = true; + for (String perm : permissions) { + if (ContextCompat.checkSelfPermission(context, perm) != PackageManager.PERMISSION_GRANTED) + granted = false; + } + if (granted) { + callback.run(); + } else { + // Passed in context should be an activity if not granted, need to show dialog! + permissionGrantCallback = callback; + if (!(context instanceof Activity)) { + // Start activity to show dialog + Intent intent = new Intent(context, SplashActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + intent.putExtra(Const.Key.INTENT_PERM, permissions); + context.startActivity(intent); + } else { + ActivityCompat.requestPermissions((android.app.Activity) context, permissions, 0); + } + } + } + + public void runWithPermission(String[] permissions, Runnable callback) { + runWithPermission(this, permissions, callback); + } + @Override public Application getApplicationContext() { return (Application) super.getApplicationContext(); @@ -32,15 +66,14 @@ public abstract class Activity extends FlavorActivity { if (result != PackageManager.PERMISSION_GRANTED) grant = false; } - Application app = getApplicationContext(); if (grant) { - if (app.permissionGrantCallback != null) { - app.permissionGrantCallback.run(); + if (permissionGrantCallback != null) { + permissionGrantCallback.run(); } } else { MagiskManager.toast(R.string.no_rw_storage, Toast.LENGTH_LONG); } - app.permissionGrantCallback = null; + permissionGrantCallback = null; } @Override diff --git a/src/main/java/com/topjohnwu/magisk/components/Application.java b/src/main/java/com/topjohnwu/magisk/components/Application.java index e2494c423..3e47fdb14 100644 --- a/src/main/java/com/topjohnwu/magisk/components/Application.java +++ b/src/main/java/com/topjohnwu/magisk/components/Application.java @@ -14,8 +14,6 @@ public abstract class Application extends android.app.Application { private static Handler mHandler = new Handler(); - public Runnable permissionGrantCallback = null; - public Application() { weakSelf = new WeakReference<>(this); } @@ -26,10 +24,6 @@ public abstract class Application extends android.app.Application { locale = defaultLocale = Locale.getDefault(); } - public void setPermissionGrantCallback(Runnable callback) { - permissionGrantCallback = callback; - } - public static void toast(CharSequence msg, int duration) { mHandler.post(() -> Toast.makeText(weakSelf.get(), msg, duration).show()); } diff --git a/src/main/java/com/topjohnwu/magisk/utils/Utils.java b/src/main/java/com/topjohnwu/magisk/utils/Utils.java index 49372f1b0..9f2fd6677 100644 --- a/src/main/java/com/topjohnwu/magisk/utils/Utils.java +++ b/src/main/java/com/topjohnwu/magisk/utils/Utils.java @@ -1,13 +1,10 @@ package com.topjohnwu.magisk.utils; import android.Manifest; -import android.app.Activity; import android.app.DownloadManager; import android.content.Context; -import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; -import android.content.pm.PackageManager; import android.content.res.Configuration; import android.database.Cursor; import android.net.ConnectivityManager; @@ -15,13 +12,11 @@ import android.net.NetworkInfo; import android.net.Uri; import android.provider.OpenableColumns; import android.support.annotation.StringRes; -import android.support.v4.app.ActivityCompat; -import android.support.v4.content.ContextCompat; import android.widget.Toast; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; -import com.topjohnwu.magisk.SplashActivity; +import com.topjohnwu.magisk.components.Activity; import com.topjohnwu.magisk.receivers.DownloadReceiver; import java.io.File; @@ -39,7 +34,8 @@ public class Utils { if (isDownloading) return; - runWithPermission(context, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, () -> { + Activity.runWithPermission(context, + new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, () -> { File file = new File(Const.EXTERNAL_PATH, getLegalFilename(filename)); if ((!file.getParentFile().exists() && !file.getParentFile().mkdirs()) @@ -47,7 +43,7 @@ public class Utils { return; } - Toast.makeText(context, context.getString(R.string.downloading_toast, filename), Toast.LENGTH_LONG).show(); + MagiskManager.toast(context.getString(R.string.downloading_toast, filename), Toast.LENGTH_LONG); isDownloading = true; DownloadManager.Request request = new DownloadManager @@ -56,7 +52,8 @@ public class Utils { DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); receiver.setDownloadID(dm.enqueue(request)).setFile(file); - context.getApplicationContext().registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); + context.getApplicationContext().registerReceiver(receiver, + new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); }); } @@ -142,29 +139,6 @@ public class Utils { return locales; } - public static void runWithPermission(Context context, String[] permissions, Runnable callback) { - boolean granted = true; - for (String perm : permissions) { - if (ContextCompat.checkSelfPermission(context, perm) != PackageManager.PERMISSION_GRANTED) - granted = false; - } - if (granted) { - callback.run(); - } else { - // Passed in context should be an activity if not granted, need to show dialog! - getMagiskManager(context).setPermissionGrantCallback(callback); - if (!(context instanceof com.topjohnwu.magisk.components.Activity)) { - // Start activity to show dialog - Intent intent = new Intent(context, SplashActivity.class); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - intent.putExtra(Const.Key.INTENT_PERM, permissions); - context.startActivity(intent); - } else { - ActivityCompat.requestPermissions((Activity) context, permissions, 0); - } - } - } - public static int dpInPx(int dp) { Context context = MagiskManager.get(); float scale = context.getResources().getDisplayMetrics().density; diff --git a/src/stub/java/com/topjohnwu/magisk/SplashActivity.java b/src/stub/java/com/topjohnwu/magisk/SplashActivity.java index be5800776..5acbc9a31 100644 --- a/src/stub/java/com/topjohnwu/magisk/SplashActivity.java +++ b/src/stub/java/com/topjohnwu/magisk/SplashActivity.java @@ -45,7 +45,7 @@ public class SplashActivity extends Activity { .setTitle(R.string.app_name) .setMessage(R.string.upgrade_msg) .setPositiveButton(R.string.yes, (d, w) -> { - Utils.runWithPermission(this, new String[] + runWithPermission(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, () -> { Intent intent = new Intent(this, ManagerUpdate.class); intent.putExtra(Const.Key.INTENT_SET_LINK, apkLink);