mirror of
https://github.com/topjohnwu/Magisk.git
synced 2024-12-25 13:47:38 +00:00
Move runWithPermission method
This commit is contained in:
parent
3dd5a6f378
commit
c5a7ab2415
@ -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:
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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<Void, Object, Boolean> {
|
||||
|
||||
@Override
|
||||
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));
|
||||
return this;
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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
|
||||
|
@ -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());
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user