From 99c74b31be15a239fdffe4a727d2d83feb52b8bb Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Mon, 6 Nov 2017 05:36:20 +0800 Subject: [PATCH] Improve dynamic permissions --- .../com/topjohnwu/magisk/MagiskManager.java | 5 +++++ .../com/topjohnwu/magisk/MainActivity.java | 6 ++++++ .../com/topjohnwu/magisk/SplashActivity.java | 6 ++---- .../topjohnwu/magisk/components/Activity.java | 12 ++++-------- .../java/com/topjohnwu/magisk/utils/Const.java | 2 ++ .../com/topjohnwu/magisk/utils/ShowUI.java | 11 +++++++---- .../java/com/topjohnwu/magisk/utils/Utils.java | 18 +++++++++++++----- 7 files changed, 39 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java b/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java index 71cf3f6b3..018f75c92 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java @@ -84,6 +84,7 @@ public class MagiskManager extends Application { public SuDatabaseHelper suDB; public RepoDatabaseHelper repoDB; public Shell shell; + public Runnable permissionGrantCallback = null; private static Handler mHandler = new Handler(); @@ -203,4 +204,8 @@ public class MagiskManager extends Application { magiskHide = true; } } + + public void setPermissionGrantCallback(Runnable callback) { + permissionGrantCallback = callback; + } } diff --git a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java index a9a5d4a43..991c290a2 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java @@ -6,6 +6,7 @@ import android.os.Bundle; import android.os.Handler; import android.support.annotation.NonNull; import android.support.design.widget.NavigationView; +import android.support.v4.app.ActivityCompat; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.support.v4.widget.DrawerLayout; @@ -52,6 +53,11 @@ public class MainActivity extends Activity finish(); } + String perm = getIntent().getStringExtra(Const.Key.INTENT_PERM); + if (perm != null) { + ActivityCompat.requestPermissions(this, new String[] { perm }, 0); + } + prefs = mm.prefs; if (mm.isDarkTheme) { diff --git a/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java b/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java index 349462931..4cba4fb39 100644 --- a/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java @@ -130,10 +130,8 @@ public class SplashActivity extends Activity { mm.hasInit = true; Intent intent = new Intent(this, MainActivity.class); - String section = getIntent().getStringExtra(Const.Key.OPEN_SECTION); - if (section != null) { - intent.putExtra(Const.Key.OPEN_SECTION, section); - } + intent.putExtra(Const.Key.OPEN_SECTION, getIntent().getStringExtra(Const.Key.OPEN_SECTION)); + intent.putExtra(Const.Key.INTENT_PERM, getIntent().getStringExtra(Const.Key.INTENT_PERM)); startActivity(intent); finish(); } diff --git a/app/src/main/java/com/topjohnwu/magisk/components/Activity.java b/app/src/main/java/com/topjohnwu/magisk/components/Activity.java index 910583925..1e7d52b00 100644 --- a/app/src/main/java/com/topjohnwu/magisk/components/Activity.java +++ b/app/src/main/java/com/topjohnwu/magisk/components/Activity.java @@ -18,7 +18,6 @@ import com.topjohnwu.magisk.utils.Utils; public class Activity extends AppCompatActivity { - private Runnable permissionGrantCallback = null; private AssetManager mAssetManager = null; private Resources mResources = null; @@ -47,12 +46,13 @@ public class Activity extends AppCompatActivity { @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + MagiskManager mm = getMagiskManager(); if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { - if (permissionGrantCallback != null) { - permissionGrantCallback.run(); + if (mm.permissionGrantCallback != null) { + mm.permissionGrantCallback.run(); } } - permissionGrantCallback = null; + mm.permissionGrantCallback = null; } @Override @@ -65,10 +65,6 @@ public class Activity extends AppCompatActivity { return mResources == null ? super.getResources() : mResources; } - public void setPermissionGrantCallback(Runnable callback) { - permissionGrantCallback = callback; - } - public MagiskManager getMagiskManager() { return (MagiskManager) super.getApplicationContext(); } diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Const.java b/app/src/main/java/com/topjohnwu/magisk/utils/Const.java index 356c63551..bfc991d93 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Const.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Const.java @@ -80,11 +80,13 @@ public class Const { public static final String OPEN_SECTION = "section"; public static final String INTENT_SET_VERSION = "version"; public static final String INTENT_SET_LINK = "link"; + public static final String INTENT_PERM = "perm_dialog"; public static final String FLASH_ACTION = "action"; public static final String FLASH_SET_BOOT = "boot"; public static final String FLASH_SET_ENC = "enc"; public static final String FLASH_SET_VERITY = "verity"; + // others public static final String UPDATE_NOTIFICATION = "notification"; public static final String UPDATE_CHANNEL = "update_channel"; 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 fcb410599..ae1bb152e 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/ShowUI.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/ShowUI.java @@ -1,5 +1,6 @@ package com.topjohnwu.magisk.utils; +import android.Manifest; import android.app.Activity; import android.app.NotificationManager; import android.app.PendingIntent; @@ -216,10 +217,12 @@ public class ShowUI { mm.remoteManagerVersionString + ".apk"))) .setCancelable(true) .setPositiveButton(R.string.install, (d, i) -> { - Intent intent = new Intent(mm, ManagerUpdate.class); - intent.putExtra(Const.Key.INTENT_SET_LINK, mm.managerLink); - intent.putExtra(Const.Key.INTENT_SET_VERSION, mm.remoteManagerVersionString); - mm.sendBroadcast(intent); + Utils.runWithPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE, () -> { + Intent intent = new Intent(mm, ManagerUpdate.class); + intent.putExtra(Const.Key.INTENT_SET_LINK, mm.managerLink); + intent.putExtra(Const.Key.INTENT_SET_VERSION, mm.remoteManagerVersionString); + mm.sendBroadcast(intent); + }); }) .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 f3a8b9c3f..ad2cb4012 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java @@ -4,6 +4,7 @@ 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; @@ -24,6 +25,7 @@ import android.widget.Toast; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; +import com.topjohnwu.magisk.SplashActivity; import com.topjohnwu.magisk.components.SnackbarMaker; import com.topjohnwu.magisk.receivers.DownloadReceiver; @@ -193,11 +195,17 @@ public class Utils { public static void runWithPermission(Context context, String permission, Runnable callback) { if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) { // Passed in context should be an activity if not granted, need to show dialog! - if (!(context instanceof com.topjohnwu.magisk.components.Activity)) - return; - com.topjohnwu.magisk.components.Activity activity = (com.topjohnwu.magisk.components.Activity) context; - activity.setPermissionGrantCallback(callback); - ActivityCompat.requestPermissions(activity, new String[] { permission }, 0); + Utils.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, permission); + context.startActivity(intent); + } else { + ActivityCompat.requestPermissions((Activity) context, new String[] { permission }, 0); + } + } else { callback.run(); }