Skip to content

Commit

Permalink
优化权限设置界面的跳转逻辑
Browse files Browse the repository at this point in the history
修正在清单中没有注册任何权限的情况下出现的空指针异常
  • Loading branch information
getActivity committed Aug 18, 2020
1 parent eeb5ccb commit 8480153
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 54 deletions.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#### 集成步骤

dependencies {
implementation 'com.hjq:xxpermissions:6.5'
implementation 'com.hjq:xxpermissions:6.8'
}

#### 一句代码搞定权限请求,从未如此简单
Expand Down Expand Up @@ -66,10 +66,6 @@

* 本框架不依赖 Support 库,兼容 Eclipse 和 Studio

#### 混淆规则

-keep class com.hjq.permissions.** {*;}

#### 常见问题

> 我们都知道,如果用户全部授予只会调用hasPermission方法,如果用户全部拒绝只会调用noPermission方法。
Expand Down
Binary file modified XXPermissions.apk
Binary file not shown.
11 changes: 7 additions & 4 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 28
versionCode 65
versionName "6.5"
versionCode 68
versionName "6.8"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand All @@ -22,10 +22,13 @@ android {
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation project(':library')
implementation 'com.android.support:appcompat-v7:28.0.0'

// 谷歌兼容库:https://developer.android.google.cn/jetpack/androidx/releases/appcompat?hl=zh-cn
implementation 'androidx.appcompat:appcompat:1.3.0-alpha01'
implementation 'com.google.android.material:material:1.3.0-alpha01'

// 吐司工具类:https://github.com/getActivity/ToastUtils
implementation 'com.hjq:toast:8.2'
implementation 'com.hjq:toast:8.6'
// 内存泄漏捕捉:https://github.com/square/leakcanary
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.4'
}
12 changes: 7 additions & 5 deletions app/src/main/java/com/hjq/permissions/demo/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.hjq.permissions.demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.hjq.permissions.OnPermission;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
Expand Down Expand Up @@ -33,7 +34,8 @@ public void requestPermission(View view) {
XXPermissions.with(this)
// 可设置被拒绝后继续申请,直到用户授权或者永久拒绝
//.constantRequest()
// 支持请求6.0悬浮窗权限8.0请求安装权限
// 支持请求 6.0 悬浮窗权限 8.0 请求安装权限
//.permission(Permission.SYSTEM_ALERT_WINDOW)
//.permission(Permission.REQUEST_INSTALL_PACKAGES)
// 不指定权限则自动获取清单中的危险权限
.permission(Permission.Group.STORAGE, Permission.Group.CALENDAR)
Expand All @@ -43,14 +45,14 @@ public void requestPermission(View view) {
public void hasPermission(List<String> granted, boolean all) {
if (all) {
ToastUtils.show("获取权限成功");
}else {
} else {
ToastUtils.show("获取权限成功,部分权限未正常授予");
}
}

@Override
public void noPermission(List<String> denied, boolean quick) {
if(quick) {
if (quick) {
ToastUtils.show("被永久拒绝授权,请手动授予权限");
//如果是被永久拒绝就跳转到应用权限系统设置页面
XXPermissions.startPermissionActivity(MainActivity.this);
Expand All @@ -64,7 +66,7 @@ public void noPermission(List<String> denied, boolean quick) {
public void hasPermission(View view) {
if (XXPermissions.hasPermission(MainActivity.this, Permission.Group.STORAGE)) {
ToastUtils.show("已经获取到权限,不需要再次申请了");
}else {
} else {
ToastUtils.show("还没有获取到权限或者部分权限未授予");
}
}
Expand Down
5 changes: 5 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ org.gradle.jvmargs=-Xmx1536m
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

# 表示使用 AndroidX
android.useAndroidX = true
# 表示将第三方库迁移到 AndroidX
android.enableJetifier = true
6 changes: 3 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ android {
defaultConfig {
minSdkVersion 11
targetSdkVersion 26
versionCode 65
versionName "6.5"
versionCode 68
versionName "6.8"
}
}

publish {
userOrg = 'getactivity'
groupId = 'com.hjq'
artifactId = 'xxpermissions'
version = '6.5'
version = '6.8'
description = 'Android 6.0 permissions adaptation framework'
website = "https://github.com/getActivity/XXPermissions"
}
Expand Down
11 changes: 8 additions & 3 deletions library/src/main/java/com/hjq/permissions/Permission.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.hjq.permissions;

import android.Manifest;

/**
* author : Android 轮子哥
* github : https://github.com/getActivity/XXPermissions
* time : 2018/06/15
* desc : 权限请求实体类
* desc : 权限请求实体类,参考 {@link Manifest.permission}
*/
public final class Permission {

private Permission() {}

/** 应用安装权限(需要 8.0 及以上) */
public static final String REQUEST_INSTALL_PACKAGES = "android.permission.REQUEST_INSTALL_PACKAGES";

Expand Down Expand Up @@ -51,9 +55,9 @@ public final class Permission {
public static final String USE_SIP = "android.permission.USE_SIP";
/** 处理拨出电话 */
public static final String PROCESS_OUTGOING_CALLS = "android.permission.PROCESS_OUTGOING_CALLS";
/** 8.0危险权限:允许您的应用通过编程方式接听呼入电话。要在您的应用中处理呼入电话,您可以使用 acceptRingingCall() 函数 */
/** 8.0 危险权限:允许您的应用通过编程方式接听呼入电话。要在您的应用中处理呼入电话,您可以使用 acceptRingingCall() 函数 */
public static final String ANSWER_PHONE_CALLS = "android.permission.ANSWER_PHONE_CALLS";
/** 8.0危险权限:权限允许您的应用读取设备中存储的电话号码 */
/** 8.0 危险权限:权限允许您的应用读取设备中存储的电话号码 */
public static final String READ_PHONE_NUMBERS = "android.permission.READ_PHONE_NUMBERS";

/** 传感器 */
Expand All @@ -72,6 +76,7 @@ public final class Permission {

/** 读取外部存储 */
public static final String READ_EXTERNAL_STORAGE = "android.permission.READ_EXTERNAL_STORAGE";

/** 写入外部存储 */
public static final String WRITE_EXTERNAL_STORAGE = "android.permission.WRITE_EXTERNAL_STORAGE";

Expand Down
22 changes: 11 additions & 11 deletions library/src/main/java/com/hjq/permissions/PermissionFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public static PermissionFragment newInstance(ArrayList<String> permissions, bool
int requestCode;
// 请求码随机生成,避免随机产生之前的请求码,必须进行循环判断
do {
// Studio编译的APK请求码必须小于 65536
// Eclipse编译的APK请求码必须小于 256
// Studio 编译的 APK 请求码必须小于 65536
// Eclipse 编译的 APK 请求码必须小于 256
requestCode = new Random().nextInt(255);
} while (CALLBACKS.get(requestCode) != null);
bundle.putInt(REQUEST_CODE, requestCode);
Expand Down Expand Up @@ -73,23 +73,23 @@ public void onActivityCreated(Bundle savedInstanceState) {
return;
}

boolean isRequestPermission = false;
if (permissions.contains(Permission.REQUEST_INSTALL_PACKAGES) && !PermissionUtils.isHasInstallPermission(getActivity())) {
boolean requestPermission = false;
if (permissions.contains(Permission.REQUEST_INSTALL_PACKAGES) && !PermissionUtils.hasInstallPermission(getActivity())) {
// 跳转到允许安装未知来源设置页面
Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, Uri.parse("package:" + getContext().getPackageName()));
startActivityForResult(intent, getArguments().getInt(REQUEST_CODE));
isRequestPermission = true;
requestPermission = true;
}

if (permissions.contains(Permission.SYSTEM_ALERT_WINDOW) && !PermissionUtils.isHasOverlaysPermission(getActivity())) {
if (permissions.contains(Permission.SYSTEM_ALERT_WINDOW) && !PermissionUtils.hasOverlaysPermission(getActivity())) {
// 跳转到悬浮窗设置页面
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getContext().getPackageName()));
startActivityForResult(intent, getArguments().getInt(REQUEST_CODE));
isRequestPermission = true;
requestPermission = true;
}

// 当前必须没有跳转到悬浮窗或者安装权限界面
if (!isRequestPermission) {
if (!requestPermission) {
requestPermission();
}
}
Expand Down Expand Up @@ -122,7 +122,7 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in

// 重新检查安装权限
if (Permission.REQUEST_INSTALL_PACKAGES.equals(permissions[i])) {
if (PermissionUtils.isHasInstallPermission(getActivity())) {
if (PermissionUtils.hasInstallPermission(getActivity())) {
grantResults[i] = PackageManager.PERMISSION_GRANTED;
} else {
grantResults[i] = PackageManager.PERMISSION_DENIED;
Expand All @@ -131,14 +131,14 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in

// 重新检查悬浮窗权限
if (Permission.SYSTEM_ALERT_WINDOW.equals(permissions[i])) {
if (PermissionUtils.isHasOverlaysPermission(getActivity())) {
if (PermissionUtils.hasOverlaysPermission(getActivity())) {
grantResults[i] = PackageManager.PERMISSION_GRANTED;
} else {
grantResults[i] = PackageManager.PERMISSION_DENIED;
}
}

// 重新检查8.0的两个新权限
// 重新检查 8.0 的两个新权限
if (Permission.ANSWER_PHONE_CALLS.equals(permissions[i]) || Permission.READ_PHONE_NUMBERS.equals(permissions[i])) {

// 检查当前的安卓版本是否符合要求
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static void start(Context context, boolean newTask) {
intent = meizu(context);
}

if (intent == null || !hasIntent(context, intent)) {
if (intent == null) {
intent = google(context);
}

Expand All @@ -55,12 +55,6 @@ static void start(Context context, boolean newTask) {
}
}

private static Intent google(Context context) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.fromParts("package", context.getPackageName(), null));
return intent;
}

private static Intent huawei(Context context) {
Intent intent = new Intent();

Expand All @@ -79,7 +73,7 @@ private static Intent huawei(Context context) {
return intent;
}

return intent;
return null;
}

private static Intent xiaomi(Context context) {
Expand All @@ -100,7 +94,11 @@ private static Intent xiaomi(Context context) {
}

intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
return intent;
if (hasIntent(context, intent)) {
return intent;
}

return null;
}

private static Intent oppo(Context context) {
Expand Down Expand Up @@ -131,7 +129,8 @@ private static Intent oppo(Context context) {
if (hasIntent(context, intent)) {
return intent;
}
return intent;

return null;
}

private static Intent vivo(Context context) {
Expand Down Expand Up @@ -163,16 +162,34 @@ private static Intent vivo(Context context) {
}

intent.setClassName("com.iqoo.secure", "com.iqoo.secure.safeguard.SoftPermissionDetailActivity");
return intent;
if (hasIntent(context, intent)) {
return intent;
}

return null;
}

private static Intent meizu(Context context) {
Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");
intent.putExtra("packageName", context.getPackageName());
intent.setClassName("com.meizu.safe", "com.meizu.safe.security.AppSecActivity");

if (hasIntent(context, intent)) {
return intent;
}

return null;
}

private static Intent google(Context context) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.fromParts("package", context.getPackageName(), null));
return intent;
}

/**
* 判断是否有这种意图
*/
private static boolean hasIntent(Context context, Intent intent) {
return !context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY).isEmpty();
}
Expand Down
Loading

0 comments on commit 8480153

Please sign in to comment.