Skip to content

Commit

Permalink
优化权限拦截器方法参数结构
Browse files Browse the repository at this point in the history
优化错误检查机制 API 命名的信息提示
修正获取到的清单内容不是当前应用的 Bug
修正在某些机型上面获取清单文件失败的问题
  • Loading branch information
getActivity committed Nov 7, 2021
1 parent 1e6ed55 commit 999f2b2
Show file tree
Hide file tree
Showing 15 changed files with 283 additions and 192 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ build
captures

._*
*.apk
*.iml
.DS_Store
local.properties
39 changes: 18 additions & 21 deletions HelpDoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ public class XxxApplication extends Application {
public final class PermissionInterceptor implements IPermissionInterceptor {

@Override
public void requestPermissions(Activity activity, OnPermissionCallback callback, List<String> permissions) {
public void requestPermissions(Activity activity, OnPermissionCallback callback, List<String> allPermissions) {
// 这里的 Dialog 只是示例,没有用 DialogFragment 来处理 Dialog 生命周期
new AlertDialog.Builder(activity)
.setTitle("授权提示")
.setMessage("使用此功能需要先授予权限")
Expand All @@ -140,7 +141,7 @@ public final class PermissionInterceptor implements IPermissionInterceptor {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
PermissionFragment.beginRequest(activity, new ArrayList<>(permissions), PermissionInterceptor.this, callback);
PermissionFragment.beginRequest(activity, new ArrayList<>(allPermissions), PermissionInterceptor.this, callback);
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
Expand All @@ -154,25 +155,26 @@ public final class PermissionInterceptor implements IPermissionInterceptor {
}

@Override
public void grantedPermissions(Activity activity, OnPermissionCallback callback, List<String> permissions, boolean all) {
if (callback == null) {
return;
public void grantedPermissions(Activity activity, List<String> allPermissions, List<String> grantedPermissions,
boolean all, OnPermissionCallback callback) {
if (callback != null) {
callback.onGranted(grantedPermissions, all);
}
callback.onGranted(permissions, all);
}

@Override
public void deniedPermissions(Activity activity, OnPermissionCallback callback, List<String> permissions, boolean never) {
public void deniedPermissions(Activity activity, List<String> allPermissions, List<String> deniedPermissions,
boolean never, OnPermissionCallback callback) {
if (callback != null) {
callback.onDenied(deniedPermissions, never);
}

if (never) {
showPermissionDialog(activity, permissions);
if (callback == null) {
return;
}
callback.onDenied(permissions, never);
showPermissionDialog(activity, deniedPermissions);
return;
}

if (permissions.size() == 1 && Permission.ACCESS_BACKGROUND_LOCATION.equals(permissions.get(0))) {
if (deniedPermissions.size() == 1 && Permission.ACCESS_BACKGROUND_LOCATION.equals(deniedPermissions.get(0))) {
ToastUtils.show("没有授予后台定位权限,请您选择\"始终允许\"");
return;
}
Expand All @@ -182,15 +184,17 @@ public final class PermissionInterceptor implements IPermissionInterceptor {
if (callback == null) {
return;
}
callback.onDenied(permissions, never);
callback.onDenied(deniedPermissions, never);
}

/**
* 显示授权对话框
*/
protected void showPermissionDialog(Activity activity, List<String> permissions) {
// 这里的 Dialog 只是示例,没有用 DialogFragment 来处理 Dialog 生命周期
new AlertDialog.Builder(activity)
.setTitle("授权提醒")
.setCancelable(false)
.setMessage(getPermissionHint(activity, permissions))
.setPositiveButton("前往授权", new DialogInterface.OnClickListener() {

Expand All @@ -200,13 +204,6 @@ public final class PermissionInterceptor implements IPermissionInterceptor {
XXPermissions.startPermissionActivity(activity, permissions);
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.show();
}

Expand Down
27 changes: 18 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# 权限请求框架

![](picture/logo.png)
![](logo.png)

* 项目地址:[Github](https://github.com/getActivity/XXPermissions)[码云](https://gitee.com/getActivity/XXPermissions)

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

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

![](picture/demo_code.png)

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

Expand Down Expand Up @@ -85,6 +85,10 @@ android.enableJetifier = true

```java
XXPermissions.with(this)
// 申请单个权限
.permission(Permission.RECORD_AUDIO)
// 申请多个权限
//.permission(Permission.Group.CALENDAR)
// 申请安装包权限
//.permission(Permission.REQUEST_INSTALL_PACKAGES)
// 申请悬浮窗权限
Expand All @@ -93,10 +97,10 @@ XXPermissions.with(this)
//.permission(Permission.NOTIFICATION_SERVICE)
// 申请系统设置权限
//.permission(Permission.WRITE_SETTINGS)
// 申请单个权限
.permission(Permission.RECORD_AUDIO)
// 申请多个权限
.permission(Permission.Group.CALENDAR)
// 设置权限请求拦截器
//.interceptor(new PermissionInterceptor())
// 设置不触发错误检测机制
//.unchecked()
.request(new OnPermissionCallback() {

@Override
Expand Down Expand Up @@ -159,6 +163,11 @@ XXPermissions.isPermanentDenied(Activity activity, String... permissions);
XXPermissions.startPermissionActivity(Context context, String... permissions);
XXPermissions.startPermissionActivity(Activity activity, String... permissions);
XXPermissions.startPermissionActivity(Fragment fragment, String... permissions);

// 设置不触发错误检测机制(全局设置)
XXPermissions.setCheckMode(false);
// 设置权限申请拦截器(全局设置)
XXPermissions.setInterceptor(new IPermissionInterceptor() {});
```

#### 关于权限监听回调参数说明
Expand All @@ -177,7 +186,7 @@ XXPermissions.startPermissionActivity(Fragment fragment, String... permissions);

| 功能及细节 | [XXPermissions](https://github.com/getActivity/XXPermissions) | [AndPermission](https://github.com/yanzhenjie/AndPermission) | [PermissionX](https://github.com/guolindev/PermissionX) | [AndroidUtilCode](https://github.com/Blankj/AndroidUtilCode) | [RxPermissions](https://github.com/tbruyelle/RxPermissions) | [PermissionsDispatcher](https://github.com/permissions-dispatcher/PermissionsDispatcher) | [EasyPermissions](https://github.com/googlesamples/easypermissions) |
| :--------: | :------------: | :------------: | :------------: | :------------: | :------------: | :------------: | :------------: |
| 对应版本 | 12.6 | 2.0.3 | 1.6.1 | 1.30.6 | 0.12 | 4.9.1 | 3.0.0 |
| 对应版本 | 12.8 | 2.0.3 | 1.6.1 | 1.30.6 | 0.12 | 4.9.1 | 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/tbruyelle/RxPermissions.svg)](https://github.com/tbruyelle/RxPermissions/issues) | [![](https://img.shields.io/github/issues/permissions-dispatcher/PermissionsDispatcher.svg)](https://github.com/permissions-dispatcher/PermissionsDispatcher/issues) | [![](https://img.shields.io/github/issues/googlesamples/easypermissions.svg)](https://github.com/googlesamples/easypermissions/issues) |
| 框架体积 | 28 KB | 127 KB | 78 KB | 500 KB | 28 KB | 91 KB | 48 KB |
| 安装包权限 ||||||||
Expand Down Expand Up @@ -238,7 +247,7 @@ XXPermissions.startPermissionActivity(Fragment fragment, String... permissions);

* 如果动态申请的权限有在 `AndroidManifest.xml` 中进行注册,但是设定了不恰当的 `android:maxSdkVersion` 属性值,框架会抛出异常,举个例子:`<uses-permission android:name="xxxx" android:maxSdkVersion="29" />`,这样的设定会导致在 Android 11 (`Build.VERSION.SDK_INT >= 30`)及以上的设备申请权限,系统会认为这个权限没有在清单文件中注册,直接拒绝本次的权限申请,并且也是不会给出任何弹窗和提示,这个问题也是必现的。

* 如果你不需要上面这些检测,可通过设置 `XXPermissions.setDebugMode(false)` 来关闭,但是需要注意的是,我并不建议你去关闭这个检测,因为在 **release 模式** 时它是关闭状态,不需要你手动关闭,而它只在 **debug 模式** 下才会触发这些检测。
* 如果你不需要上面这些检测,可通过调用 `unchecked` 方法来关闭,但是需要注意的是,我并不建议你去关闭这个检测,因为在 **release 模式** 时它是关闭状态,不需要你手动关闭,而它只在 **debug 模式** 下才会触发这些检测。

* 出现这些问题的原因是,我们对这些机制不太熟悉,而如果框架不加以限制,那么引发各种奇奇怪怪的问题出现,作为框架的作者,表示不仅你们很痛苦,作为框架作者表示也很受伤。因为这些问题不是框架导致的,而是调用者的某些操作不规范导致的。我觉得这个问题最好的解决方式是,由框架做统一的检查,因为我是框架的作者,对权限申请这块知识点有**较强的专业能力和足够的经验**,知道什么该做,什么不该做,这样就可以对这些骚操作进行一一拦截。

Expand Down
Binary file removed 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 31
versionCode 1260
versionName "12.6"
versionCode 1280
versionName "12.8"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void onCreate() {
// 初始化吐司工具类
ToastUtils.init(this, new WhiteToastStyle());

// 设置权限申请拦截器
// 设置权限申请拦截器(全局设置)
XXPermissions.setInterceptor(new PermissionInterceptor());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public final class PermissionInterceptor implements IPermissionInterceptor {

// @Override
// public void requestPermissions(Activity activity, OnPermissionCallback callback, List<String> permissions) {
// public void requestPermissions(Activity activity, OnPermissionCallback callback, List<String> allPermissions) {
// // 这里的 Dialog 只是示例,没有用 DialogFragment 来处理 Dialog 生命周期
// new AlertDialog.Builder(activity)
// .setTitle(R.string.common_permission_hint)
Expand All @@ -35,7 +35,7 @@ public final class PermissionInterceptor implements IPermissionInterceptor {
// @Override
// public void onClick(DialogInterface dialog, int which) {
// dialog.dismiss();
// PermissionFragment.beginRequest(activity, new ArrayList<>(permissions), PermissionInterceptor.this, callback);
// PermissionFragment.beginRequest(activity, new ArrayList<>(allPermissions), PermissionInterceptor.this, callback);
// }
// })
// .setNegativeButton(R.string.common_permission_denied, new DialogInterface.OnClickListener() {
Expand All @@ -49,25 +49,26 @@ public final class PermissionInterceptor implements IPermissionInterceptor {
// }

@Override
public void grantedPermissions(Activity activity, OnPermissionCallback callback, List<String> permissions, boolean all) {
if (callback == null) {
return;
public void grantedPermissions(Activity activity, List<String> allPermissions, List<String> grantedPermissions,
boolean all, OnPermissionCallback callback) {
if (callback != null) {
callback.onGranted(grantedPermissions, all);
}
callback.onGranted(permissions, all);
}

@Override
public void deniedPermissions(Activity activity, OnPermissionCallback callback, List<String> permissions, boolean never) {
public void deniedPermissions(Activity activity, List<String> allPermissions, List<String> deniedPermissions,
boolean never, OnPermissionCallback callback) {
if (callback != null) {
callback.onDenied(deniedPermissions, never);
}

if (never) {
showPermissionDialog(activity, permissions);
if (callback == null) {
return;
}
callback.onDenied(permissions, never);
showPermissionDialog(activity, deniedPermissions);
return;
}

if (permissions.size() == 1 && Permission.ACCESS_BACKGROUND_LOCATION.equals(permissions.get(0))) {
if (deniedPermissions.size() == 1 && Permission.ACCESS_BACKGROUND_LOCATION.equals(deniedPermissions.get(0))) {
ToastUtils.show(R.string.common_permission_fail_4);
return;
}
Expand All @@ -77,7 +78,7 @@ public void deniedPermissions(Activity activity, OnPermissionCallback callback,
if (callback == null) {
return;
}
callback.onDenied(permissions, never);
callback.onDenied(deniedPermissions, never);
}

/**
Expand Down
18 changes: 16 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 11
versionCode 1260
versionName "12.6"
versionCode 1280
versionName "12.8"
}

// 使用 JDK 1.8
Expand All @@ -25,6 +25,11 @@ android {
outputFileName = "${rootProject.name}-${android.defaultConfig.versionName}.aar"
}
}

packagingOptions {
// 剔除这个包下的所有文件(不会移除签名信息)
exclude 'META-INF/*******'
}
}

afterEvaluate {
Expand All @@ -40,6 +45,15 @@ dependencies {
implementation 'com.android.support:support-fragment:24.2.0'
}

task makeJar(type: Copy) {
delete 'build/TestJar_V1.0.jar' //删除之前的旧jar包
from('build/intermediates/bundles/default/') //从这个目录下取出默认jar包
into('build/') //将jar包输出到指定目录下
include('classes.jar')
rename('classes.jar', 'TestJar_V1.0.jar') //自定义jar包的名字
}
makeJar.dependsOn(build)

tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
options.addStringOption('encoding', 'UTF-8')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,31 @@ public interface IPermissionInterceptor {
/**
* 权限申请拦截,可在此处先弹 Dialog 再申请权限
*/
default void requestPermissions(Activity activity, OnPermissionCallback callback, List<String> permissions) {
PermissionFragment.beginRequest(activity, new ArrayList<>(permissions), this, callback);
default void requestPermissions(Activity activity, OnPermissionCallback callback, List<String> allPermissions) {
PermissionFragment.beginRequest(activity, new ArrayList<>(allPermissions), this, callback);
}

/**
* 权限授予回调拦截,参见 {@link OnPermissionCallback#onGranted(List, boolean)}
*/
default void grantedPermissions(Activity activity, OnPermissionCallback callback, List<String> permissions, boolean all) {
default void grantedPermissions(Activity activity, List<String> allPermissions,
List<String> grantedPermissions, boolean all,
OnPermissionCallback callback) {
if (callback == null) {
return;
}
callback.onGranted(permissions, all);
callback.onGranted(grantedPermissions, all);
}

/**
* 权限拒绝回调拦截,参见 {@link OnPermissionCallback#onDenied(List, boolean)}
*/
default void deniedPermissions(Activity activity, OnPermissionCallback callback, List<String> permissions, boolean never) {
default void deniedPermissions(Activity activity, List<String> allPermissions,
List<String> deniedPermissions, boolean never,
OnPermissionCallback callback) {
if (callback == null) {
return;
}
callback.onDenied(permissions, never);
callback.onDenied(deniedPermissions, never);
}
}
Loading

0 comments on commit 999f2b2

Please sign in to comment.