Skip to content

Commit

Permalink
优化权限检查机制判断
Browse files Browse the repository at this point in the history
适配 OPPO 应用权限受阻跳转优化方案
  • Loading branch information
getActivity committed Mar 20, 2024
1 parent a2ec232 commit 2e54b24
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 26 deletions.
6 changes: 3 additions & 3 deletions README-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

* project address: [Github](https://github.com/getActivity/XXPermissions)

* [Click here to download demo apk directly](https://github.com/getActivity/XXPermissions/releases/download/18.6/XXPermissions.apk)
* [Click here to download demo apk directly](https://github.com/getActivity/XXPermissions/releases/download/18.62/XXPermissions.apk)

![](picture/en/demo_request_permission_activity.jpg) ![](picture/en/demo_request_single_permission.jpg) ![](picture/en/demo_request_group_permission.jpg)

Expand Down Expand Up @@ -55,7 +55,7 @@ android {
dependencies {
// Permission request framework:https://github.com/getActivity/XXPermissions
implementation 'com.github.getActivity:XXPermissions:18.6'
implementation 'com.github.getActivity:XXPermissions:18.62'
}
```

Expand Down Expand Up @@ -217,7 +217,7 @@ XXPermissions.setInterceptor(new OnPermissionInterceptor() {});

| Adaptation details | [XXPermissions](https://github.com/getActivity/XXPermissions) | [AndPermission](https://github.com/yanzhenjie/AndPermission) | [PermissionX](https://github.com/guolindev/PermissionX) | [AndroidUtilCode-PermissionUtils](https://github.com/Blankj/AndroidUtilCode) | [PermissionsDispatcher](https://github.com/permissions-dispatcher/PermissionsDispatcher) | [RxPermissions](https://github.com/tbruyelle/RxPermissions) | [EasyPermissions](https://github.com/googlesamples/easypermissions) |
|:-------------------------------------------------------------:| :----------------------------------------------------------------------------------------------------------------------------: | :--------------------------------------------------------------------------------------------------------------------------: | :--------------------------------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------------------------------------------------------: |
| Corresponding version | 18.6 | 2.0.3 | 1.7.1 | 1.31.0 | 4.9.2 | 0.12 | 3.0.0 |
| Corresponding version | 18.62 | 2.0.3 | 1.7.1 | 1.31.0 | 4.9.2 | 0.12 | 3.0.0 |
| Number of issues | [![](https://img.shields.io/github/issues/getActivity/XXPermissions.svg)](https://github.com/getActivity/XXPermissions/issues) | [![](https://img.shields.io/github/issues/yanzhenjie/AndPermission.svg)](https://github.com/yanzhenjie/AndPermission/issues) | [![](https://img.shields.io/github/issues/guolindev/PermissionX.svg)](https://github.com/guolindev/PermissionX/issues) | [![](https://img.shields.io/github/issues/Blankj/AndroidUtilCode.svg)](https://github.com/Blankj/AndroidUtilCode/issues) | [![](https://img.shields.io/github/issues/permissions-dispatcher/PermissionsDispatcher.svg)](https://github.com/permissions-dispatcher/PermissionsDispatcher/issues) | [![](https://img.shields.io/github/issues/tbruyelle/RxPermissions.svg)](https://github.com/tbruyelle/RxPermissions/issues) | [![](https://img.shields.io/github/issues/googlesamples/easypermissions.svg)](https://github.com/googlesamples/easypermissions/issues) |
| Framework volume | 85 KB | 127 KB | 97 KB | 500 KB | 99 KB | 28 KB | 48 KB |
| Framework Maintenance Status | **In maintenance** | stop maintenance | **In maintenance** | stop maintenance | stop maintenance | stop maintenance | stop maintenance |
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

* 博文地址:[一句代码搞定权限请求,从未如此简单](https://www.jianshu.com/p/c69ff8a445ed)

* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处可直接下载](https://github.com/getActivity/XXPermissions/releases/download/18.6/XXPermissions.apk)
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处可直接下载](https://github.com/getActivity/XXPermissions/releases/download/18.62/XXPermissions.apk)

![](picture/zh/download_demo_apk_qr_code.png)

Expand Down Expand Up @@ -61,7 +61,7 @@ android {
dependencies {
// 权限请求框架:https://github.com/getActivity/XXPermissions
implementation 'com.github.getActivity:XXPermissions:18.6'
implementation 'com.github.getActivity:XXPermissions:18.62'
}
```

Expand Down Expand Up @@ -223,7 +223,7 @@ XXPermissions.setInterceptor(new OnPermissionInterceptor() {});

| 适配细节 | [XXPermissions](https://github.com/getActivity/XXPermissions) | [AndPermission](https://github.com/yanzhenjie/AndPermission) | [PermissionX](https://github.com/guolindev/PermissionX) | [AndroidUtilCode-PermissionUtils](https://github.com/Blankj/AndroidUtilCode) | [PermissionsDispatcher](https://github.com/permissions-dispatcher/PermissionsDispatcher) | [RxPermissions](https://github.com/tbruyelle/RxPermissions) | [EasyPermissions](https://github.com/googlesamples/easypermissions) |
| :--------: | :------------: | :------------: | :------------: | :------------: | :------------: | :------------: | :------------: |
| 对应版本 | 18.6 | 2.0.3 | 1.7.1 | 1.31.0 | 4.9.2 | 0.12 | 3.0.0 |
| 对应版本 | 18.62 | 2.0.3 | 1.7.1 | 1.31.0 | 4.9.2 | 0.12 | 3.0.0 |
| issues 数 | [![](https://img.shields.io/github/issues/getActivity/XXPermissions.svg)](https://github.com/getActivity/XXPermissions/issues) | [![](https://img.shields.io/github/issues/yanzhenjie/AndPermission.svg)](https://github.com/yanzhenjie/AndPermission/issues) | [![](https://img.shields.io/github/issues/guolindev/PermissionX.svg)](https://github.com/guolindev/PermissionX/issues) | [![](https://img.shields.io/github/issues/Blankj/AndroidUtilCode.svg)](https://github.com/Blankj/AndroidUtilCode/issues) | [![](https://img.shields.io/github/issues/permissions-dispatcher/PermissionsDispatcher.svg)](https://github.com/permissions-dispatcher/PermissionsDispatcher/issues) | [![](https://img.shields.io/github/issues/tbruyelle/RxPermissions.svg)](https://github.com/tbruyelle/RxPermissions/issues) | [![](https://img.shields.io/github/issues/googlesamples/easypermissions.svg)](https://github.com/googlesamples/easypermissions/issues) |
| 框架体积 | 85 KB | 127 KB | 97 KB | 500 KB | 99 KB | 28 KB | 48 KB |
| 框架维护状态 |**维护中**| 停止维护 | 停止维护 | 停止维护 | 停止维护 | 停止维护 | 停止维护 |
Expand Down
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 16
targetSdkVersion 34
versionCode 1806
versionName "18.6"
versionCode 1862
versionName "18.62"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

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

defaultConfig {
minSdkVersion 14
versionCode 1806
versionName "18.6"
versionCode 1862
versionName "18.62"
}

// 使用 JDK 1.8
Expand Down
51 changes: 38 additions & 13 deletions library/src/main/java/com/hjq/permissions/PermissionChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ static void checkMediaLocationPermission(@NonNull Context context, @NonNull List
for (String permission : requestPermissions) {
if (PermissionUtils.equalsPermission(permission, Permission.ACCESS_MEDIA_LOCATION)
|| PermissionUtils.equalsPermission(permission, Permission.READ_MEDIA_IMAGES)
|| PermissionUtils.equalsPermission(permission, Permission.READ_MEDIA_VIDEO)
|| PermissionUtils.equalsPermission(permission, Permission.READ_EXTERNAL_STORAGE)
|| PermissionUtils.equalsPermission(permission, Permission.WRITE_EXTERNAL_STORAGE)
|| PermissionUtils.equalsPermission(permission, Permission.MANAGE_EXTERNAL_STORAGE)) {
Expand All @@ -134,9 +135,10 @@ static void checkMediaLocationPermission(@NonNull Context context, @NonNull List

if (AndroidVersion.getTargetSdkVersionCode(context) >= AndroidVersion.ANDROID_13) {
if (!PermissionUtils.containsPermission(requestPermissions, Permission.READ_MEDIA_IMAGES) &&
!PermissionUtils.containsPermission(requestPermissions, Permission.READ_MEDIA_VIDEO) &&
!PermissionUtils.containsPermission(requestPermissions, Permission.MANAGE_EXTERNAL_STORAGE)) {
// 你需要在外层手动添加 READ_MEDIA_IMAGES 或者 MANAGE_EXTERNAL_STORAGE 才可以申请 ACCESS_MEDIA_LOCATION 权限
throw new IllegalArgumentException("You must add " + Permission.READ_MEDIA_IMAGES + " or " +
// 你需要在外层手动添加 READ_MEDIA_IMAGES、READ_MEDIA_VIDEO、MANAGE_EXTERNAL_STORAGE 任一权限才可以申请 ACCESS_MEDIA_LOCATION 权限
throw new IllegalArgumentException("You must add " + Permission.READ_MEDIA_IMAGES + " or " + Permission.READ_MEDIA_VIDEO + " or " +
Permission.MANAGE_EXTERNAL_STORAGE + " rights to apply for " + Permission.ACCESS_MEDIA_LOCATION + " rights");
}
} else {
Expand Down Expand Up @@ -173,13 +175,39 @@ static void checkStoragePermission(@NonNull Context context, @NonNull List<Strin
" instead of " + Permission.READ_EXTERNAL_STORAGE);
}

// 如果申请的是 Android 13 读取媒体权限,则绕过本次检查
if (PermissionUtils.containsPermission(requestPermissions, Permission.READ_MEDIA_IMAGES) ||
PermissionUtils.containsPermission(requestPermissions, Permission.READ_MEDIA_VIDEO) ||
PermissionUtils.containsPermission(requestPermissions, Permission.READ_MEDIA_AUDIO)) {

if (PermissionUtils.containsPermission(requestPermissions, Permission.READ_EXTERNAL_STORAGE) ||
PermissionUtils.containsPermission(requestPermissions, Permission.WRITE_EXTERNAL_STORAGE)) {
// 检测是否有旧版的存储权限,有的话直接抛出异常,请不要自己动态申请这两个权限
// 框架会在 Android 13 以下的版本上自动添加并申请这两个权限
throw new IllegalArgumentException("If you have applied for media permissions, " +
"do not apply for the READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions");
}

if (PermissionUtils.containsPermission(requestPermissions, Permission.MANAGE_EXTERNAL_STORAGE)) {
// 因为 MANAGE_EXTERNAL_STORAGE 权限范围很大,有了它就可以读取媒体文件,不需要再叠加申请媒体权限
throw new IllegalArgumentException("Because the MANAGE_EXTERNAL_STORAGE permission range is very large, "
+ "you can read media files with it, and there is no need to apply for additional media permissions.");
}

// 到此结束,不需要往下走是否有分区存储的判断
return;
}

if (PermissionUtils.containsPermission(requestPermissions, Permission.MANAGE_EXTERNAL_STORAGE)) {

if (PermissionUtils.containsPermission(requestPermissions, Permission.READ_EXTERNAL_STORAGE) ||
PermissionUtils.containsPermission(requestPermissions, Permission.WRITE_EXTERNAL_STORAGE)) {
// 检测是否有旧版的存储权限,有的话直接抛出异常,请不要自己动态申请这两个权限
// 框架会在 Android 10 以下的版本上自动添加并申请这两个权限
throw new IllegalArgumentException("If you have applied for MANAGE_EXTERNAL_STORAGE permissions, " +
"do not apply for the READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions");
}
}

// 如果申请的是 Android 10 获取媒体位置权限,则绕过本次检查
if (PermissionUtils.containsPermission(requestPermissions, Permission.ACCESS_MEDIA_LOCATION)) {
return;
Expand Down Expand Up @@ -683,18 +711,15 @@ static void optimizeDeprecatedPermission(@NonNull List<String> requestPermission
// 如果本次申请包含了 Android 11 存储权限
if (PermissionUtils.containsPermission(requestPermissions, Permission.MANAGE_EXTERNAL_STORAGE)) {

if (PermissionUtils.containsPermission(requestPermissions, Permission.READ_EXTERNAL_STORAGE) ||
PermissionUtils.containsPermission(requestPermissions, Permission.WRITE_EXTERNAL_STORAGE)) {
// 检测是否有旧版的存储权限,有的话直接抛出异常,请不要自己动态申请这两个权限
// 框架会在 Android 10 以下的版本上自动添加并申请这两个权限
throw new IllegalArgumentException("If you have applied for MANAGE_EXTERNAL_STORAGE permissions, " +
"do not apply for the READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions");
}

if (!AndroidVersion.isAndroid11()) {
// 自动添加旧版的存储权限,因为旧版的系统不支持申请新版的存储权限
requestPermissions.add(Permission.READ_EXTERNAL_STORAGE);
requestPermissions.add(Permission.WRITE_EXTERNAL_STORAGE);
if (!PermissionUtils.containsPermission(requestPermissions, Permission.READ_EXTERNAL_STORAGE)) {
requestPermissions.add(Permission.READ_EXTERNAL_STORAGE);
}

if (!PermissionUtils.containsPermission(requestPermissions, Permission.WRITE_EXTERNAL_STORAGE)) {
requestPermissions.add(Permission.WRITE_EXTERNAL_STORAGE);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.net.VpnService;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import java.util.Collections;

/**
* author : Android 轮子哥
Expand Down Expand Up @@ -37,7 +38,7 @@ public Intent getPermissionIntent(@NonNull Context context, @NonNull String perm
return getVpnPermissionIntent(context);
}

return PermissionIntentManager.getApplicationDetailsIntent(context);
return PermissionIntentManager.getApplicationDetailsIntent(context, Collections.singletonList(permission));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;

/**
* author : Android 轮子哥
Expand Down Expand Up @@ -276,13 +278,28 @@ static Intent getVivoMobileManagerAppIntent(Context context) {

/* ---------------------------------------------------------------------------------------- */

@NonNull
static Intent getApplicationDetailsIntent(@NonNull Context context) {
return getApplicationDetailsIntent(context, null);
}

/**
* 获取应用详情界面意图
*/
@NonNull
static Intent getApplicationDetailsIntent(@NonNull Context context) {
static Intent getApplicationDetailsIntent(@NonNull Context context, @Nullable List<String> permissions) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(PermissionUtils.getPackageNameUri(context));
if (permissions != null && !permissions.isEmpty() && PhoneRomUtils.isColorOs()) {
// OPPO 应用权限受阻跳转优化适配:https://open.oppomobile.com/new/developmentDoc/info?id=12983
Bundle bundle = new Bundle();
// 元素为受阻权限的原生权限名字符串常量
bundle.putStringArrayList("permissionList", permissions instanceof ArrayList ?
(ArrayList<String>) permissions : new ArrayList<>(permissions));
intent.putExtras(bundle);
// 传入跳转优化标识
intent.putExtra("isGetPermission", true);
}
if (PermissionUtils.areActivityIntent(context, intent)) {
return intent;
}
Expand Down
Loading

0 comments on commit 2e54b24

Please sign in to comment.