diff --git a/HelpDoc.md b/HelpDoc.md index 32ff305..d6a64d0 100644 --- a/HelpDoc.md +++ b/HelpDoc.md @@ -134,7 +134,7 @@ public class XxxApplication extends Application { public class PermissionInterceptor implements IPermissionInterceptor { @Override - public void requestPermissions(FragmentActivity activity, OnPermissionCallback callback, List permissions) { + public void requestPermissions(Activity activity, OnPermissionCallback callback, List permissions) { new AlertDialog.Builder(activity) .setTitle("授权提示") .setMessage("使用此功能需要先授予权限") @@ -157,13 +157,13 @@ public class PermissionInterceptor implements IPermissionInterceptor { } @Override - public void grantedPermissions(FragmentActivity activity, OnPermissionCallback callback, List permissions, boolean all) { + public void grantedPermissions(Activity activity, OnPermissionCallback callback, List permissions, boolean all) { // 回调授权成功的方法 callback.onGranted(permissions, all); } @Override - public void deniedPermissions(FragmentActivity activity, OnPermissionCallback callback, List permissions, boolean never) { + public void deniedPermissions(Activity activity, OnPermissionCallback callback, List permissions, boolean never) { // 回调授权失败的方法 callback.onDenied(permissions, never); if (never) { @@ -182,7 +182,7 @@ public class PermissionInterceptor implements IPermissionInterceptor { /** * 显示授权对话框 */ - protected void showPermissionDialog(FragmentActivity activity, List permissions) { + protected void showPermissionDialog(Activity activity, List permissions) { new AlertDialog.Builder(activity) .setTitle("授权提醒") .setMessage(getPermissionHint(activity, permissions)) diff --git a/README.md b/README.md index addfd67..3102d98 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ android { dependencies { // 权限请求框架:https://github.com/getActivity/XXPermissions - implementation 'com.github.getActivity:XXPermissions:11.2' + implementation 'com.github.getActivity:XXPermissions:11.5' } ``` @@ -178,7 +178,7 @@ public class XxxActivity extends AppCompatActivity { * 在框架的日常维护中,有很多人跟我反馈过框架有 Bug,但是经过排查和定位发现,这其中有 95% 的问题来自于调用者一些不规范操作导致的,这不仅对我造成很大的困扰,同时也极大浪费了很多小伙伴的时间和精力,于是我在框架中加入了很多审查元素,在 **Debug** 模式下,一旦有某些操作不符合规范,那么框架会直接抛出异常给调用者,并在异常信息中正确指引调用者纠正错误,例如: - 1. 传入的 Activity 对象不是 FragmentActivity 的子类,框架会抛出异常,又或者传入的 Activity 的状态异常(已经 **Finishing** 或者 **Destroyed**),这种情况一般是在异步申请权限导致的,框架也会抛出异常,请在合适的时机申请权限,如果申请的时机无法预估,请在外层做好 Activity 状态判断再进行权限申请。 + 1. 传入的 Context 实例不是 Activity 对象,框架会抛出异常,又或者传入的 Activity 的状态异常(已经 **Finishing** 或者 **Destroyed**),这种情况一般是在异步申请权限导致的,框架也会抛出异常,请在合适的时机申请权限,如果申请的时机无法预估,请在外层做好 Activity 状态判断再进行权限申请。 2. 如果调用者没有传入任何权限就申请权限的话,框架会抛出异常,如果调用者传入的权限不是危险权限或者特殊权限,框架也会抛出异常,因为有的人会把普通权限当做危险权限传给框架,系统会直接拒绝。 diff --git a/XXPermissions.apk b/XXPermissions.apk index bab510c..b2574ec 100644 Binary files a/XXPermissions.apk and b/XXPermissions.apk differ diff --git a/app/build.gradle b/app/build.gradle index 5e24595..8bc3165 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -7,8 +7,8 @@ android { applicationId "com.hjq.permissions.demo" minSdkVersion 14 targetSdkVersion 30 - versionCode 1120 - versionName "11.2" + versionCode 1150 + versionName "11.5" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6bdf9bf..4ba7ae2 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -16,8 +16,8 @@ - - + + diff --git a/app/src/main/java/com/hjq/permissions/demo/MainActivity.java b/app/src/main/java/com/hjq/permissions/demo/MainActivity.java index 70de4fc..c689c6e 100644 --- a/app/src/main/java/com/hjq/permissions/demo/MainActivity.java +++ b/app/src/main/java/com/hjq/permissions/demo/MainActivity.java @@ -75,6 +75,7 @@ public void onGranted(List permissions, boolean all) { XXPermissions.with(this) .permission(Permission.ACCESS_COARSE_LOCATION) .permission(Permission.ACCESS_FINE_LOCATION) + // 如果不需要在后台使用定位功能,请不要申请此权限 .permission(Permission.ACCESS_BACKGROUND_LOCATION) .request(new OnPermissionCallback() { diff --git a/app/src/main/java/com/hjq/permissions/demo/PermissionInterceptor.java b/app/src/main/java/com/hjq/permissions/demo/PermissionInterceptor.java index bf8e86f..931b20f 100644 --- a/app/src/main/java/com/hjq/permissions/demo/PermissionInterceptor.java +++ b/app/src/main/java/com/hjq/permissions/demo/PermissionInterceptor.java @@ -1,9 +1,9 @@ package com.hjq.permissions.demo; +import android.app.Activity; import android.content.Context; import android.content.DialogInterface; import android.os.Build; -import android.support.v4.app.FragmentActivity; import android.support.v7.app.AlertDialog; import com.hjq.permissions.IPermissionInterceptor; @@ -24,7 +24,7 @@ public final class PermissionInterceptor implements IPermissionInterceptor { // @Override -// public void requestPermissions(FragmentActivity activity, OnPermissionCallback callback, List permissions) { +// public void requestPermissions(Activity activity, OnPermissionCallback callback, List permissions) { // // 这里的 Dialog 只是示例,没有用 DialogFragment 来处理 Dialog 生命周期 // new AlertDialog.Builder(activity) // .setTitle(R.string.common_permission_hint) @@ -48,13 +48,13 @@ public final class PermissionInterceptor implements IPermissionInterceptor { // } @Override - public void grantedPermissions(FragmentActivity activity, OnPermissionCallback callback, List permissions, boolean all) { + public void grantedPermissions(Activity activity, OnPermissionCallback callback, List permissions, boolean all) { // 回调授权失败的方法 callback.onGranted(permissions, all); } @Override - public void deniedPermissions(FragmentActivity activity, OnPermissionCallback callback, List permissions, boolean never) { + public void deniedPermissions(Activity activity, OnPermissionCallback callback, List permissions, boolean never) { // 回调授权失败的方法 callback.onDenied(permissions, never); if (never) { @@ -73,7 +73,7 @@ public void deniedPermissions(FragmentActivity activity, OnPermissionCallback ca /** * 显示授权对话框 */ - protected void showPermissionDialog(FragmentActivity activity, List permissions) { + protected void showPermissionDialog(Activity activity, List permissions) { // 这里的 Dialog 只是示例,没有用 DialogFragment 来处理 Dialog 生命周期 new AlertDialog.Builder(activity) .setTitle(R.string.common_permission_alert) diff --git a/library/build.gradle b/library/build.gradle index 4778b8d..7a2993d 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -5,8 +5,8 @@ android { defaultConfig { minSdkVersion 11 - versionCode 1120 - versionName "11.2" + versionCode 1150 + versionName "11.5" } // 使用 JDK 1.8 diff --git a/library/src/main/java/com/hjq/permissions/IPermissionInterceptor.java b/library/src/main/java/com/hjq/permissions/IPermissionInterceptor.java index 6f26a61..3679416 100644 --- a/library/src/main/java/com/hjq/permissions/IPermissionInterceptor.java +++ b/library/src/main/java/com/hjq/permissions/IPermissionInterceptor.java @@ -1,6 +1,6 @@ package com.hjq.permissions; -import android.support.v4.app.FragmentActivity; +import android.app.Activity; import java.util.ArrayList; import java.util.List; @@ -16,21 +16,21 @@ public interface IPermissionInterceptor { /** * 权限申请拦截,可在此处先弹 Dialog 再申请权限 */ - default void requestPermissions(FragmentActivity activity, OnPermissionCallback callback, List permissions) { + default void requestPermissions(Activity activity, OnPermissionCallback callback, List permissions) { PermissionFragment.beginRequest(activity, new ArrayList<>(permissions), callback); } /** * 权限授予回调拦截,参见 {@link OnPermissionCallback#onGranted(List, boolean)} */ - default void grantedPermissions(FragmentActivity activity, OnPermissionCallback callback, List permissions, boolean all) { + default void grantedPermissions(Activity activity, OnPermissionCallback callback, List permissions, boolean all) { callback.onGranted(permissions, all); } /** * 权限拒绝回调拦截,参见 {@link OnPermissionCallback#onDenied(List, boolean)} */ - default void deniedPermissions(FragmentActivity activity, OnPermissionCallback callback, List permissions, boolean never) { + default void deniedPermissions(Activity activity, OnPermissionCallback callback, List permissions, boolean never) { callback.onDenied(permissions, never); } } \ No newline at end of file diff --git a/library/src/main/java/com/hjq/permissions/PermissionChecker.java b/library/src/main/java/com/hjq/permissions/PermissionChecker.java index d842a9d..8ee846d 100644 --- a/library/src/main/java/com/hjq/permissions/PermissionChecker.java +++ b/library/src/main/java/com/hjq/permissions/PermissionChecker.java @@ -30,8 +30,8 @@ static boolean checkActivityStatus(Activity activity, boolean debugMode) { // 检查当前 Activity 状态是否是正常的,如果不是则不请求权限 if (activity == null) { if (debugMode) { - // 这个 Activity 对象必须是 FragmentActivity 的子类,请直接继承 AppCompatActivity - throw new IllegalArgumentException("The Activity must be a subclass of FragmentActivity, Please directly inherit AppCompatActivity"); + // Context 的实例必须是 Activity 对象 + throw new IllegalArgumentException("The instance of the Context must be an Activity object"); } return false; } @@ -104,7 +104,6 @@ static boolean checkPermissionArgument(List requestPermissions, boolean * @param requestPermissions 请求的权限组 * @param scopedStorage 是否适配了分区存储 */ - @SuppressWarnings({"ConstantConditions"}) static void checkStoragePermission(Context context, List requestPermissions, boolean scopedStorage) { // 如果请求的权限中没有包含外部存储相关的权限,那么就直接返回 if (!requestPermissions.contains(Permission.MANAGE_EXTERNAL_STORAGE) && diff --git a/library/src/main/java/com/hjq/permissions/PermissionFragment.java b/library/src/main/java/com/hjq/permissions/PermissionFragment.java index 5fa374b..011a28b 100644 --- a/library/src/main/java/com/hjq/permissions/PermissionFragment.java +++ b/library/src/main/java/com/hjq/permissions/PermissionFragment.java @@ -1,14 +1,16 @@ package com.hjq.permissions; import android.annotation.SuppressLint; +import android.annotation.TargetApi; +import android.app.Activity; +import android.app.Fragment; import android.content.Context; import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.pm.PackageManager; import android.content.res.Configuration; +import android.os.Build; import android.os.Bundle; -import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentActivity; import android.util.SparseBooleanArray; import java.util.ArrayList; @@ -21,6 +23,8 @@ * time : 2018/06/15 * desc : 权限请求处理类 */ +@SuppressWarnings("deprecation") +@TargetApi(Build.VERSION_CODES.M) public final class PermissionFragment extends Fragment implements Runnable { /** 请求的权限组 */ @@ -35,14 +39,14 @@ public final class PermissionFragment extends Fragment implements Runnable { /** 权限请求码存放集合 */ private final static SparseBooleanArray REQUEST_CODE_ARRAY = new SparseBooleanArray(); - public static void beginRequest(FragmentActivity activity, ArrayList permissions, OnPermissionCallback callback) { + public static void beginRequest(Activity activity, ArrayList permissions, OnPermissionCallback callback) { beginRequest(activity, permissions, true, callback); } /** * 开启权限申请 */ - private static void beginRequest(FragmentActivity activity, ArrayList permissions, boolean interceptor, OnPermissionCallback callback) { + private static void beginRequest(Activity activity, ArrayList permissions, boolean interceptor, OnPermissionCallback callback) { PermissionFragment fragment = new PermissionFragment(); Bundle bundle = new Bundle(); int requestCode; @@ -79,15 +83,15 @@ private static void beginRequest(FragmentActivity activity, ArrayList pe /** * 绑定 Activity */ - public void attachActivity(FragmentActivity activity) { - activity.getSupportFragmentManager().beginTransaction().add(this, this.toString()).commitAllowingStateLoss(); + public void attachActivity(Activity activity) { + activity.getFragmentManager().beginTransaction().add(this, this.toString()).commitAllowingStateLoss(); } /** * 解绑 Activity */ - public void detachActivity(FragmentActivity activity) { - activity.getSupportFragmentManager().beginTransaction().remove(this).commitAllowingStateLoss(); + public void detachActivity(Activity activity) { + activity.getFragmentManager().beginTransaction().remove(this).commitAllowingStateLoss(); } /** @@ -101,7 +105,7 @@ public void setCallBack(OnPermissionCallback callback) { @Override public void onAttach(Context context) { super.onAttach(context); - FragmentActivity activity = getActivity(); + Activity activity = getActivity(); if (activity == null) { return; } @@ -128,7 +132,7 @@ public void onAttach(Context context) { @Override public void onDetach() { super.onDetach(); - FragmentActivity activity = getActivity(); + Activity activity = getActivity(); if (activity == null || mScreenOrientation != ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) { return; } @@ -161,7 +165,7 @@ public void onResume() { */ public void requestSpecialPermission() { Bundle arguments = getArguments(); - FragmentActivity activity = getActivity(); + Activity activity = getActivity(); if (arguments == null || activity == null) { return; } @@ -218,7 +222,7 @@ public void requestSpecialPermission() { * 申请危险权限 */ public void requestDangerousPermission() { - FragmentActivity activity = getActivity(); + Activity activity = getActivity(); Bundle arguments = getArguments(); if (activity == null || arguments == null) { return; @@ -309,7 +313,7 @@ public void onDenied(List permissions, boolean never) { @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { Bundle arguments = getArguments(); - FragmentActivity activity = getActivity(); + Activity activity = getActivity(); if (activity == null || arguments == null || mCallBack == null || requestCode != arguments.getInt(REQUEST_CODE)) { return; } @@ -397,7 +401,7 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { - FragmentActivity activity = getActivity(); + Activity activity = getActivity(); Bundle arguments = getArguments(); if (activity == null || arguments == null || requestCode != arguments.getInt(REQUEST_CODE) || mDangerousRequest) { return; diff --git a/library/src/main/java/com/hjq/permissions/PermissionUtils.java b/library/src/main/java/com/hjq/permissions/PermissionUtils.java index cfe345e..3618b82 100644 --- a/library/src/main/java/com/hjq/permissions/PermissionUtils.java +++ b/library/src/main/java/com/hjq/permissions/PermissionUtils.java @@ -11,7 +11,6 @@ import android.os.Build; import android.os.Environment; import android.provider.Settings; -import android.support.v4.app.FragmentActivity; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; @@ -464,10 +463,10 @@ static int getRandomRequestCode() { /** * 寻找上下文中的 Activity 对象 */ - static FragmentActivity findFragmentActivity(Context context) { + static Activity findActivity(Context context) { do { - if (context instanceof FragmentActivity) { - return (FragmentActivity) context; + if (context instanceof Activity) { + return (Activity) context; } else if (context instanceof ContextWrapper){ context = ((ContextWrapper) context).getBaseContext(); } else { diff --git a/library/src/main/java/com/hjq/permissions/XXPermissions.java b/library/src/main/java/com/hjq/permissions/XXPermissions.java index 2efab63..823afc3 100644 --- a/library/src/main/java/com/hjq/permissions/XXPermissions.java +++ b/library/src/main/java/com/hjq/permissions/XXPermissions.java @@ -1,11 +1,10 @@ package com.hjq.permissions; import android.app.Activity; +import android.app.Fragment; import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; -import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentActivity; import java.util.List; @@ -15,6 +14,7 @@ * time : 2018/06/15 * desc : Android 危险权限请求类 */ +@SuppressWarnings("deprecation, unused") public final class XXPermissions { /** 权限设置页跳转请求码 */ @@ -42,6 +42,10 @@ public static XXPermissions with(Fragment fragment) { return with(fragment.getActivity()); } + public static XXPermissions with(android.support.v4.app.Fragment fragment) { + return with(fragment.getActivity()); + } + /** * 是否为调试模式 */ @@ -129,8 +133,8 @@ public void request(OnPermissionCallback callback) { boolean debugMode = isDebugMode(mContext); // 检查当前 Activity 状态是否是正常的,如果不是则不请求权限 - FragmentActivity fragmentActivity = PermissionUtils.findFragmentActivity(mContext); - if (!PermissionChecker.checkActivityStatus(fragmentActivity, debugMode)) { + Activity activity = PermissionUtils.findActivity(mContext); + if (!PermissionChecker.checkActivityStatus(activity, debugMode)) { return; } @@ -165,7 +169,7 @@ public void request(OnPermissionCallback callback) { } // 申请没有授予过的权限 - getInterceptor().requestPermissions(fragmentActivity, callback, mPermissions); + getInterceptor().requestPermissions(activity, callback, mPermissions); } /** @@ -220,6 +224,8 @@ public static boolean isPermanentDenied(Activity activity, List permissi return PermissionUtils.isPermissionPermanentDenied(activity, permissions); } + /* android.content.Context */ + public static void startPermissionActivity(Context context) { startPermissionActivity(context, (List) null); } @@ -238,7 +244,7 @@ public static void startPermissionActivity(Context context, String[]... permissi * @param permissions 没有授予或者被拒绝的权限组 */ public static void startPermissionActivity(Context context, List permissions) { - Activity activity = PermissionUtils.findFragmentActivity(context); + Activity activity = PermissionUtils.findActivity(context); if (activity != null) { startPermissionActivity(activity, permissions); return; @@ -250,6 +256,12 @@ public static void startPermissionActivity(Context context, List permiss context.startActivity(intent); } + /* android.app.Activity */ + + public static void startPermissionActivity(Activity activity) { + startPermissionActivity(activity, (List) null); + } + public static void startPermissionActivity(Activity activity, String... permissions) { startPermissionActivity(activity, PermissionUtils.asArrayList(permissions)); } @@ -258,10 +270,17 @@ public static void startPermissionActivity(Activity activity, String[]... permis startPermissionActivity(activity, PermissionUtils.asArrayLists(permissions)); } + /** + * 跳转到应用权限设置页 + * + * @param permissions 没有授予或者被拒绝的权限组 + */ public static void startPermissionActivity(Activity activity, List permissions) { activity.startActivityForResult(PermissionSettingPage.getSmartPermissionIntent(activity, permissions), REQUEST_CODE); } + /* android.app.Fragment */ + public static void startPermissionActivity(Fragment fragment) { startPermissionActivity(fragment, (List) null); } @@ -280,7 +299,34 @@ public static void startPermissionActivity(Fragment fragment, String[]... permis * @param permissions 没有授予或者被拒绝的权限组 */ public static void startPermissionActivity(Fragment fragment, List permissions) { - FragmentActivity activity = fragment.getActivity(); + Activity activity = fragment.getActivity(); + if (activity == null) { + return; + } + fragment.startActivityForResult(PermissionSettingPage.getSmartPermissionIntent(activity, permissions), REQUEST_CODE); + } + + /* android.support.v4.app.Fragment */ + + public static void startPermissionActivity(android.support.v4.app.Fragment fragment) { + startPermissionActivity(fragment, (List) null); + } + + public static void startPermissionActivity(android.support.v4.app.Fragment fragment, String... permissions) { + startPermissionActivity(fragment, PermissionUtils.asArrayList(permissions)); + } + + public static void startPermissionActivity(android.support.v4.app.Fragment fragment, String[]... permissions) { + startPermissionActivity(fragment, PermissionUtils.asArrayLists(permissions)); + } + + /** + * 跳转到应用权限设置页 + * + * @param permissions 没有授予或者被拒绝的权限组 + */ + public static void startPermissionActivity(android.support.v4.app.Fragment fragment, List permissions) { + Activity activity = fragment.getActivity(); if (activity == null) { return; }