Move runWithPermission method

This commit is contained in:
topjohnwu 2018-06-09 17:14:24 +08:00
parent 3dd5a6f378
commit c5a7ab2415
10 changed files with 54 additions and 53 deletions

View File

@ -21,7 +21,6 @@ import com.topjohnwu.magisk.asyncs.ParallelTask;
import com.topjohnwu.magisk.components.Fragment; import com.topjohnwu.magisk.components.Fragment;
import com.topjohnwu.magisk.components.SnackbarMaker; import com.topjohnwu.magisk.components.SnackbarMaker;
import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.Const;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.superuser.CallbackList; import com.topjohnwu.superuser.CallbackList;
import com.topjohnwu.superuser.Shell; import com.topjohnwu.superuser.Shell;
@ -88,8 +87,7 @@ public class MagiskLogFragment extends Fragment {
new LogManager().read(); new LogManager().read();
return true; return true;
case R.id.menu_save: case R.id.menu_save:
Utils.runWithPermission(getActivity(), runWithPermission(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },
new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },
() -> new LogManager().save()); () -> new LogManager().save());
return true; return true;
case R.id.menu_clear: case R.id.menu_clear:

View File

@ -21,7 +21,6 @@ import com.topjohnwu.magisk.components.Fragment;
import com.topjohnwu.magisk.container.Module; import com.topjohnwu.magisk.container.Module;
import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.Const;
import com.topjohnwu.magisk.utils.Topic; import com.topjohnwu.magisk.utils.Topic;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.superuser.Shell; import com.topjohnwu.superuser.Shell;
import java.util.ArrayList; import java.util.ArrayList;
@ -40,8 +39,7 @@ public class ModulesFragment extends Fragment implements Topic.Subscriber {
@BindView(R.id.empty_rv) TextView emptyRv; @BindView(R.id.empty_rv) TextView emptyRv;
@OnClick(R.id.fab) @OnClick(R.id.fab)
public void selectFile() { public void selectFile() {
Utils.runWithPermission(getActivity(), runWithPermission(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, () -> {
new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },() -> {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/zip"); intent.setType("application/zip");
startActivityForResult(intent, Const.ID.FETCH_ZIP); startActivityForResult(intent, Const.ID.FETCH_ZIP);

View File

@ -166,7 +166,7 @@ public class SettingsActivity extends Activity implements Topic.Subscriber {
} else { } else {
if (Utils.checkNetworkStatus()) { if (Utils.checkNetworkStatus()) {
restoreManager.setOnPreferenceClickListener((pref) -> { restoreManager.setOnPreferenceClickListener((pref) -> {
Utils.runWithPermission(getActivity(), runWithPermission(getActivity(),
new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, () -> { new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, () -> {
Intent intent = new Intent(mm, ManagerUpdate.class); Intent intent = new Intent(mm, ManagerUpdate.class);
intent.putExtra(Const.Key.INTENT_SET_LINK, mm.managerLink); intent.putExtra(Const.Key.INTENT_SET_LINK, mm.managerLink);

View File

@ -14,7 +14,6 @@ import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.components.SnackbarMaker; import com.topjohnwu.magisk.components.SnackbarMaker;
import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.Const;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.magisk.utils.WebService; import com.topjohnwu.magisk.utils.WebService;
import com.topjohnwu.magisk.utils.ZipUtils; import com.topjohnwu.magisk.utils.ZipUtils;
import com.topjohnwu.superuser.Shell; import com.topjohnwu.superuser.Shell;
@ -158,7 +157,8 @@ public class ProcessRepoZip extends ParallelTask<Void, Object, Boolean> {
@Override @Override
public ParallelTask<Void, Object, Boolean> exec(Void... voids) { public ParallelTask<Void, Object, Boolean> 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)); () -> super.exec(voids));
return this; return this;
} }

View File

@ -36,4 +36,8 @@ public class Fragment extends android.support.v4.app.Fragment {
public void startActivityForResult(Intent intent, int requestCode, Activity.ActivityResultListener listener) { public void startActivityForResult(Intent intent, int requestCode, Activity.ActivityResultListener listener) {
((Activity) getActivity()).startActivityForResult(intent, requestCode, listener); ((Activity) getActivity()).startActivityForResult(intent, requestCode, listener);
} }
public void runWithPermission(String[] permissions, Runnable callback) {
Activity.runWithPermission(getActivity(), permissions, callback);
}
} }

View File

@ -255,7 +255,7 @@ public class ShowUI {
.setMessage(mm.getString(R.string.repo_install_msg, filename)) .setMessage(mm.getString(R.string.repo_install_msg, filename))
.setCancelable(true) .setCancelable(true)
.setPositiveButton(R.string.install, (d, i) -> { .setPositiveButton(R.string.install, (d, i) -> {
Utils.runWithPermission(activity, com.topjohnwu.magisk.components.Activity.runWithPermission(activity,
new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, () -> { new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, () -> {
Intent intent = new Intent(mm, ManagerUpdate.class); Intent intent = new Intent(mm, ManagerUpdate.class);
intent.putExtra(Const.Key.INTENT_SET_LINK, mm.managerLink); intent.putExtra(Const.Key.INTENT_SET_LINK, mm.managerLink);

View File

@ -1,16 +1,23 @@
package com.topjohnwu.magisk.components; package com.topjohnwu.magisk.components;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.widget.Toast; import android.widget.Toast;
import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.SplashActivity;
import com.topjohnwu.magisk.utils.Const;
public abstract class Activity extends FlavorActivity { public abstract class Activity extends FlavorActivity {
private static Runnable permissionGrantCallback;
private ActivityResultListener activityResultListener; private ActivityResultListener activityResultListener;
public Activity() { public Activity() {
@ -20,6 +27,33 @@ public abstract class Activity extends FlavorActivity {
applyOverrideConfiguration(configuration); 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 @Override
public Application getApplicationContext() { public Application getApplicationContext() {
return (Application) super.getApplicationContext(); return (Application) super.getApplicationContext();
@ -32,15 +66,14 @@ public abstract class Activity extends FlavorActivity {
if (result != PackageManager.PERMISSION_GRANTED) if (result != PackageManager.PERMISSION_GRANTED)
grant = false; grant = false;
} }
Application app = getApplicationContext();
if (grant) { if (grant) {
if (app.permissionGrantCallback != null) { if (permissionGrantCallback != null) {
app.permissionGrantCallback.run(); permissionGrantCallback.run();
} }
} else { } else {
MagiskManager.toast(R.string.no_rw_storage, Toast.LENGTH_LONG); MagiskManager.toast(R.string.no_rw_storage, Toast.LENGTH_LONG);
} }
app.permissionGrantCallback = null; permissionGrantCallback = null;
} }
@Override @Override

View File

@ -14,8 +14,6 @@ public abstract class Application extends android.app.Application {
private static Handler mHandler = new Handler(); private static Handler mHandler = new Handler();
public Runnable permissionGrantCallback = null;
public Application() { public Application() {
weakSelf = new WeakReference<>(this); weakSelf = new WeakReference<>(this);
} }
@ -26,10 +24,6 @@ public abstract class Application extends android.app.Application {
locale = defaultLocale = Locale.getDefault(); locale = defaultLocale = Locale.getDefault();
} }
public void setPermissionGrantCallback(Runnable callback) {
permissionGrantCallback = callback;
}
public static void toast(CharSequence msg, int duration) { public static void toast(CharSequence msg, int duration) {
mHandler.post(() -> Toast.makeText(weakSelf.get(), msg, duration).show()); mHandler.post(() -> Toast.makeText(weakSelf.get(), msg, duration).show());
} }

View File

@ -1,13 +1,10 @@
package com.topjohnwu.magisk.utils; package com.topjohnwu.magisk.utils;
import android.Manifest; import android.Manifest;
import android.app.Activity;
import android.app.DownloadManager; import android.app.DownloadManager;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.database.Cursor; import android.database.Cursor;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
@ -15,13 +12,11 @@ import android.net.NetworkInfo;
import android.net.Uri; import android.net.Uri;
import android.provider.OpenableColumns; import android.provider.OpenableColumns;
import android.support.annotation.StringRes; import android.support.annotation.StringRes;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.widget.Toast; import android.widget.Toast;
import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.SplashActivity; import com.topjohnwu.magisk.components.Activity;
import com.topjohnwu.magisk.receivers.DownloadReceiver; import com.topjohnwu.magisk.receivers.DownloadReceiver;
import java.io.File; import java.io.File;
@ -39,7 +34,8 @@ public class Utils {
if (isDownloading) if (isDownloading)
return; 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)); File file = new File(Const.EXTERNAL_PATH, getLegalFilename(filename));
if ((!file.getParentFile().exists() && !file.getParentFile().mkdirs()) if ((!file.getParentFile().exists() && !file.getParentFile().mkdirs())
@ -47,7 +43,7 @@ public class Utils {
return; 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; isDownloading = true;
DownloadManager.Request request = new DownloadManager DownloadManager.Request request = new DownloadManager
@ -56,7 +52,8 @@ public class Utils {
DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
receiver.setDownloadID(dm.enqueue(request)).setFile(file); 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; 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) { public static int dpInPx(int dp) {
Context context = MagiskManager.get(); Context context = MagiskManager.get();
float scale = context.getResources().getDisplayMetrics().density; float scale = context.getResources().getDisplayMetrics().density;

View File

@ -45,7 +45,7 @@ public class SplashActivity extends Activity {
.setTitle(R.string.app_name) .setTitle(R.string.app_name)
.setMessage(R.string.upgrade_msg) .setMessage(R.string.upgrade_msg)
.setPositiveButton(R.string.yes, (d, w) -> { .setPositiveButton(R.string.yes, (d, w) -> {
Utils.runWithPermission(this, new String[] runWithPermission(new String[]
{ Manifest.permission.WRITE_EXTERNAL_STORAGE }, () -> { { Manifest.permission.WRITE_EXTERNAL_STORAGE }, () -> {
Intent intent = new Intent(this, ManagerUpdate.class); Intent intent = new Intent(this, ManagerUpdate.class);
intent.putExtra(Const.Key.INTENT_SET_LINK, apkLink); intent.putExtra(Const.Key.INTENT_SET_LINK, apkLink);