Skip to content

Commit

Permalink
优化 API 命名和注释
Browse files Browse the repository at this point in the history
优化传入非法权限的异常提示
移除 startApplicationDetails 方法
  • Loading branch information
getActivity committed Mar 20, 2021
1 parent 36c883c commit afea438
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 91 deletions.
14 changes: 6 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/.gradle
/.idea
/build
*/build
/captures
/.cxx
*/.cxx
/.externalNativeBuild
.gradle
.idea
.cxx
.externalNativeBuild
build
captures

._*
*.iml
Expand Down
6 changes: 4 additions & 2 deletions HelpDoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* [Android 11 存储权限适配](#android-11-存储权限适配)

* [什么情况下需要适配分区存储特性](#什么情况下需要适配分区存储特性)

* [我想在申请前和申请后统一弹对话框该怎么处理](#我想在申请前和申请后统一弹对话框该怎么处理)

* [如何在回调中判断哪些权限被永久拒绝了](#如何在回调中判断哪些权限被永久拒绝了)
Expand Down Expand Up @@ -119,7 +121,7 @@ public class MyApplication extends Application {
public void onCreate() {
super.onCreate();
// 设置权限申请拦截器
XXPermissions.setPermissionInterceptor(new PermissionInterceptor());
XXPermissions.setInterceptor(new PermissionInterceptor());
}
}
```
Expand Down Expand Up @@ -229,7 +231,7 @@ XXPermissions.with(this)
@Override
public void onDenied(List<String> permissions, boolean never) {
if (never && permissions.contains(Permission.RECORD_AUDIO) &&
XXPermissions.isPermissionPermanentDenied(MainActivity.this, Permission.RECORD_AUDIO)) {
XXPermissions.isPermanentDenied(MainActivity.this, Permission.RECORD_AUDIO)) {
toast("录音权限被永久拒绝了");
}
}
Expand Down
24 changes: 11 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ android {
dependencies {
// 权限请求框架:https://github.com/getActivity/XXPermissions
implementation 'com.hjq:xxpermissions:10.2'
implementation 'com.hjq:xxpermissions:10.5'
}
```

Expand All @@ -51,7 +51,7 @@ public final class XxxApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// 告诉框架,当前项目已适配分区存储特性
// 当前项目是否已经适配了分区存储的特性
XXPermissions.setScopedStorage(true);
}
}
Expand Down Expand Up @@ -107,8 +107,8 @@ public class XxxActivity extends AppCompatActivity {
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == XXPermissions.REQUEST_CODE) {
if (XXPermissions.isGrantedPermission(this, Permission.RECORD_AUDIO) &&
XXPermissions.isGrantedPermission(this, Permission.Group.CALENDAR)) {
if (XXPermissions.isGranted(this, Permission.RECORD_AUDIO) &&
XXPermissions.isGranted(this, Permission.Group.CALENDAR)) {
toast("用户已经在权限设置页授予了录音和日历权限");
} else {
toast("用户没有在权限设置页授予权限");
Expand All @@ -122,7 +122,7 @@ public class XxxActivity extends AppCompatActivity {

* 我们都知道,如果用户全部授予只会调用 **onGranted** 方法,如果用户全部拒绝只会调用 **onDenied** 方法。

* 但是还有一种情况,如果在请求多组权限的情况下,这些权限不是被全部授予或者全部拒绝了,而是部分授权部分拒绝这种情况,框架会如何处理回调呢?
* 但是还有一种情况,如果在请求多个权限的情况下,这些权限不是被全部授予或者全部拒绝了,而是部分授权部分拒绝这种情况,框架会如何处理回调呢?

* 框架会先调用 **onDenied** 方法,再调用 **onGranted** 方法。其中我们可以通过 **onGranted** 方法中的 **all** 参数来判断权限是否全部授予了。

Expand All @@ -134,8 +134,8 @@ public class XxxActivity extends AppCompatActivity {

| 功能及细节 | [XXPermissions](https://github.com/getActivity/XXPermissions) | [AndPermission](https://github.com/yanzhenjie/AndPermission) | [RxPermissions](https://github.com/tbruyelle/RxPermissions) | [PermissionsDispatcher](https://github.com/permissions-dispatcher/PermissionsDispatcher) | [EasyPermissions](https://github.com/googlesamples/easypermissions) | [PermissionX](https://github.com/guolindev/PermissionX) | [AndroidUtilCode](https://github.com/Blankj/AndroidUtilCode) |
| :--------: | :------------: | :------------: | :------------: | :------------: | :------------: | :------------: | :------------: |
| 对应版本 | 10.2 | 2.0.3 | 0.12 | 4.8.0 | 3.0.0 | 1.4.0 | 1.30.5 |
| 框架体积 | [25 KB](https://bintray.com/getactivity/maven/xxpermissions#files/com/hjq/xxpermissions) | [127 KB](https://mvnrepository.com/artifact/com.yanzhenjie/permission) | [28 KB](https://jitpack.io/#com.github.tbruyelle/rxpermissions) | [91 KB](https://bintray.com/hotchemi/org.permissionsdispatcher/permissionsdispatcher-processor#files/org/permissionsdispatcher/permissionsdispatcher-processor) | [48 KB](https://bintray.com/easygoogle/EasyPermissions/easypermissions#files/pub/devrel/easypermissions) | [32 KB](https://bintray.com/guolindev/maven/permissionx#files/com/permissionx/guolindev/permissionx) | [483 KB](https://bintray.com/blankj/maven/UtilCode#files/com/blankj/utilcode) |
| 对应版本 | 10.5 | 2.0.3 | 0.12 | 4.8.0 | 3.0.0 | 1.4.0 | 1.30.5 |
| 框架体积 | [24 KB](https://bintray.com/getactivity/maven/xxpermissions#files/com/hjq/xxpermissions) | [127 KB](https://mvnrepository.com/artifact/com.yanzhenjie/permission) | [28 KB](https://jitpack.io/#com.github.tbruyelle/rxpermissions) | [91 KB](https://bintray.com/hotchemi/org.permissionsdispatcher/permissionsdispatcher-processor#files/org/permissionsdispatcher/permissionsdispatcher-processor) | [48 KB](https://bintray.com/easygoogle/EasyPermissions/easypermissions#files/pub/devrel/easypermissions) | [32 KB](https://bintray.com/guolindev/maven/permissionx#files/com/permissionx/guolindev/permissionx) | [483 KB](https://bintray.com/blankj/maven/UtilCode#files/com/blankj/utilcode) |
| 安装包权限 ||||||||
| 悬浮窗权限 ||||||||
| 通知栏权限 ||||||||
Expand Down Expand Up @@ -179,17 +179,15 @@ public class XxxActivity extends AppCompatActivity {

#### 框架亮点

* 简洁易用,采用链式调用的方式,使用只需一句代码

* 体积感人,功能在同类框架中最全的,但是体积是最小的

* 首款也是唯一一款适配 Android 11 的权限请求框架

* 首款也是唯一一款适配所有 Android 版本的权限请求框架

* 无论在多么极端恶劣的环境下申请权限,框架依然坚挺
* 简洁易用:采用链式调用的方式,使用只需一句代码

* 体积感人:功能在同类框架中最全的,但是体积是最小的

* 支持单个权限、多个权限、单个权限组、多个权限组请求
* 适配极端情况:无论在多么极端恶劣的环境下申请权限,框架依然坚挺

* 向下兼容属性:新权限在旧系统可以正常申请,框架会做自动适配,无需调用者适配

Expand Down
Binary file modified XXPermissions.apk
Binary file not shown.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.hjq.permissions.demo"
minSdkVersion 14
targetSdkVersion 30
versionCode 1020
versionName "10.2"
versionCode 1050
versionName "10.5"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void onCreate() {
ToastUtils.init(this, new ToastWhiteStyle(getApplicationContext()));

// 设置权限申请拦截器
XXPermissions.setPermissionInterceptor(new PermissionInterceptor());
XXPermissions.setInterceptor(new PermissionInterceptor());

// 告诉框架,当前项目已适配分区存储特性
//XXPermissions.setScopedStorage(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void onGranted(List<String> permissions, boolean all) {

} else if (viewId == R.id.btn_main_app_details) {

XXPermissions.startApplicationDetails(this);
XXPermissions.startPermissionActivity(this);
}
}

Expand Down
6 changes: 3 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {

defaultConfig {
minSdkVersion 11
versionCode 1020
versionName "10.2"
versionCode 1050
versionName "10.5"
}

// 使用 JDK 1.8
Expand All @@ -30,7 +30,7 @@ publish {
userOrg = 'getactivity'
groupId = 'com.hjq'
artifactId = 'xxpermissions'
publishVersion = '10.2'
publishVersion = '10.5'
desc = 'Android 6.0 permissions adaptation framework'
website = "https://github.com/getActivity/XXPermissions"
}
Expand Down
4 changes: 1 addition & 3 deletions library/src/main/java/com/hjq/permissions/Permission.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@ private Permission() {}
*/
public static final class Group {

/**
* 存储权限
*/
/** 存储权限 */
public static final String[] STORAGE = new String[]{
Permission.READ_EXTERNAL_STORAGE,
Permission.WRITE_EXTERNAL_STORAGE};
Expand Down
11 changes: 6 additions & 5 deletions library/src/main/java/com/hjq/permissions/PermissionChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ static boolean checkActivityStatus(Activity activity, boolean debugMode) {

if (activity.isFinishing()) {
if (debugMode) {
// 这个 Activity 对象当前不能是结束状态,这种情况常出现在执行异步请求后申请权限,请手动在外层代码做判断
// 这个 Activity 对象当前不能是关闭状态,这种情况常出现在执行异步请求后申请权限,请自行在外层判断 Activity 状态是否正常之后再进入权限申请
throw new IllegalStateException("The Activity has been finishing, Please manually determine the status of the Activity");
}
return false;
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && activity.isDestroyed()) {
if (debugMode) {
// 这个 Activity 对象当前不能是销毁状态,这种情况常出现在执行异步请求后申请权限,请手动在外层代码做判断
// 这个 Activity 对象当前不能是销毁状态,这种情况常出现在执行异步请求后申请权限,请自行在外层判断 Activity 状态是否正常之后再进入权限申请
throw new IllegalStateException("The Activity has been destroyed, Please manually determine the status of the Activity");
}
return false;
Expand Down Expand Up @@ -86,7 +86,7 @@ static boolean checkPermissionArgument(List<String> requestPermissions, boolean
for (String permission : requestPermissions) {
if (!allPermissions.contains(permission)) {
// 请不要申请危险权限和特殊权限之外的权限
throw new IllegalArgumentException("Please do not apply for permissions other than dangerous and special permissions");
throw new IllegalArgumentException("The " + permission + " is not a dangerous permission or special permission");
}
}
}
Expand All @@ -108,7 +108,7 @@ static void checkStoragePermission(Context context, List<String> requestPermissi
return;
}

int cookie = PermissionUtils.findApkCookie(context);
int cookie = PermissionUtils.findApkPathCookie(context);
if (cookie == -1) {
return;
}
Expand Down Expand Up @@ -138,9 +138,10 @@ static void checkStoragePermission(Context context, List<String> requestPermissi
// 如果在已经适配 Android 11 的情况下
if (targetSdkVersion >= Build.VERSION_CODES.R &&
!requestPermissions.contains(Permission.MANAGE_EXTERNAL_STORAGE) && !scopedStorage) {
// 1. 适配分区存储的特性,并在 Application.onCreate 中调用 XXPermissions.setScopedStorage(true)
// 1. 适配分区存储的特性,并在 Application 初始化时调用 XXPermissions.setScopedStorage(true)
// 2. 如果不想适配分区存储,则需要使用 Permission.MANAGE_EXTERNAL_STORAGE 来申请权限
// 上面两种方式需要二选一,否则无法在 Android 11 的设备上正常读写外部存储上的文件
// 如果不知道该怎么选择,可以看文档:https://github.com/getActivity/XXPermissions/blob/master/HelpDoc
throw new IllegalArgumentException("Please adapt the scoped storage, or use the MANAGE_EXTERNAL_STORAGE permission");
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,19 +339,19 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in
// 如果请求成功的权限集合大小和请求的数组一样大时证明权限已经全部授予
if (grantedPermission.size() == permissions.length) {
// 代表申请的所有的权限都授予了
XXPermissions.getPermissionInterceptor().grantedPermissions(activity, callBack, grantedPermission, true);
XXPermissions.getInterceptor().grantedPermissions(activity, callBack, grantedPermission, true);
return;
}

// 获取被拒绝的权限
List<String> deniedPermission = PermissionUtils.getDeniedPermissions(permissions, grantResults);

// 代表申请的权限中有不同意授予的,如果有某个权限被永久拒绝就返回 true 给开发人员,让开发者引导用户去设置界面开启权限
XXPermissions.getPermissionInterceptor().deniedPermissions(activity, callBack, deniedPermission, PermissionUtils.isPermissionPermanentDenied(activity, deniedPermission));
XXPermissions.getInterceptor().deniedPermissions(activity, callBack, deniedPermission, PermissionUtils.isPermissionPermanentDenied(activity, deniedPermission));

// 证明还有一部分权限被成功授予,回调成功接口
if (!grantedPermission.isEmpty()) {
XXPermissions.getPermissionInterceptor().grantedPermissions(activity, callBack, grantedPermission, false);
XXPermissions.getInterceptor().grantedPermissions(activity, callBack, grantedPermission, false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ static boolean isGrantedStoragePermission(Context context) {
if (isAndroid11()) {
return Environment.isExternalStorageManager();
}
return XXPermissions.isGrantedPermission(context, Permission.Group.STORAGE);
return XXPermissions.isGranted(context, Permission.Group.STORAGE);
}

/**
Expand Down Expand Up @@ -444,7 +444,7 @@ static FragmentActivity findFragmentActivity(Context context) {
*/
@SuppressWarnings("JavaReflectionMemberAccess")
@SuppressLint("PrivateApi")
static int findApkCookie(Context context) {
static int findApkPathCookie(Context context) {
AssetManager assets = context.getAssets();
String path = context.getApplicationInfo().sourceDir;
int cookie = -1;
Expand Down
Loading

0 comments on commit afea438

Please sign in to comment.