Skip to content

Commit

Permalink
修正永久拒绝权限的判断
Browse files Browse the repository at this point in the history
  • Loading branch information
HJQ committed Aug 13, 2018
1 parent 41257a2 commit 7c139e8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#### 集成步骤

dependencies {
implementation 'com.hjq:xxpermissions:3.3'
implementation 'com.hjq:xxpermissions:3.5'
}

#### 一句代码搞定权限请求,从未如此简单
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 14
targetSdkVersion 26
versionCode 33
versionName "3.3"
versionCode 35
versionName "3.5"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
6 changes: 3 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ android {
defaultConfig {
minSdkVersion 11
targetSdkVersion 26
versionCode 33
versionName "3.3"
versionCode 35
versionName "3.5"
}
}

publish {
userOrg = 'getactivity'//填写bintray用户名,注意大小写
groupId = 'com.hjq'//定义的maven group id最终引用形式
artifactId = 'xxpermissions'//maven的artifact id
version = '3.3'//maven 上发布版本号
version = '3.5'//maven 上发布版本号
description = 'Android 6.0 permissions adaptation framework'//描述,自己定义
website = "https://github.com/getActivity/XXPermissions"//项目在github中的地址
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public final class PermissionFragment extends Fragment {
private static final String REQUEST_CONSTANT = "request_constant";

private final static SparseArray<OnPermission> sContainer = new SparseArray<>();
private final static int TIME_DELAY = 200;//延迟时间,用于是否是系统拒绝的
private static long sRequestTime;//请求的时间

public static PermissionFragment newInstant(ArrayList<String> permissions, boolean constant) {
Expand Down Expand Up @@ -132,15 +131,19 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in
//代表申请的所有的权限都授予了
call.hasPermission(succeedPermissions, true);
} else {
if (getArguments().getBoolean(REQUEST_CONSTANT) && System.currentTimeMillis() - sRequestTime > TIME_DELAY) {

//获取拒绝权限
List<String> failPermissions = PermissionUtils.getFailPermissions(permissions, grantResults);

if (getArguments().getBoolean(REQUEST_CONSTANT) && PermissionUtils.checkPermissionPermanentDenied(getActivity(), failPermissions)) {
//继续请求权限直到用户授权或者永久拒绝
requestPermission();
return;
}

//获取拒绝权限
List<String> failPermissions = PermissionUtils.getFailPermissions(permissions, grantResults);
//代表申请的权限中有不同意授予的,如果拒绝的时间过快证明是系统自动拒绝
call.noPermission(failPermissions, System.currentTimeMillis() - sRequestTime < TIME_DELAY);
call.noPermission(failPermissions, PermissionUtils.checkPermissionPermanentDenied(getActivity(), failPermissions));

//证明还有一部分权限被成功授予,回调成功接口
if (!succeedPermissions.isEmpty()) {
call.hasPermission(succeedPermissions, false);
Expand Down
38 changes: 29 additions & 9 deletions library/src/main/java/com/hjq/permissions/PermissionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,9 @@ static ArrayList<String> getFailPermissions(Context context, List<String> permis

for (String permission : permissions) {

//检测安装权限
if (permission.equals(Permission.REQUEST_INSTALL_PACKAGES) && !isHasInstallPermission(context)) {
if (failPermissions == null) failPermissions = new ArrayList<>();
failPermissions.add(permission);
continue;
}

//检查悬浮窗权限
if (permission.equals(Permission.SYSTEM_ALERT_WINDOW) && !isHasOverlaysPermission(context)) {
//检测安装权限和悬浮窗权限
if ((permission.equals(Permission.REQUEST_INSTALL_PACKAGES) && !isHasInstallPermission(context)) ||
(permission.equals(Permission.SYSTEM_ALERT_WINDOW) && !isHasOverlaysPermission(context))) {
if (failPermissions == null) failPermissions = new ArrayList<>();
failPermissions.add(permission);
continue;
Expand All @@ -123,6 +117,32 @@ static ArrayList<String> getFailPermissions(Context context, List<String> permis
return failPermissions;
}

/**
* 检查某个权限是否被永久拒绝
*
* @param activity Activity对象
* @param permissions 请求的权限
*/
static boolean checkPermissionPermanentDenied(Activity activity, List<String> permissions) {

for (String permission : permissions) {

//安装权限和浮窗权限不算在内
if (permission.equals(Permission.REQUEST_INSTALL_PACKAGES) || permission.equals(Permission.SYSTEM_ALERT_WINDOW)) {
continue;
}

if (PermissionUtils.isOverMarshmallow()) {
if (activity.checkSelfPermission(permission) == PackageManager.PERMISSION_DENIED) {
if (!activity.shouldShowRequestPermissionRationale(permission)) {
return true;
}
}
}
}
return false;
}

/**
* 获取没有授予的权限
*
Expand Down

0 comments on commit 7c139e8

Please sign in to comment.