Skip to content

Commit

Permalink
修复一个小Bug,补充代码注释
Browse files Browse the repository at this point in the history
  • Loading branch information
HJQ committed Aug 10, 2018
1 parent 57214f5 commit 41257a2
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 74 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.2'
implementation 'com.hjq:xxpermissions:3.3'
}

#### 一句代码搞定权限请求,从未如此简单
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 30
versionName "3.0"
versionCode 33
versionName "3.3"
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 32
versionName "3.2"
versionCode 33
versionName "3.3"
}
}

publish {
userOrg = 'getactivity'//填写bintray用户名,注意大小写
groupId = 'com.hjq'//定义的maven group id最终引用形式
artifactId = 'xxpermissions'//maven的artifact id
version = '3.2'//maven 上发布版本号
version = '3.3'//maven 上发布版本号
description = 'Android 6.0 permissions adaptation framework'//描述,自己定义
website = "https://github.com/getActivity/XXPermissions"//项目在github中的地址
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.hjq.permissions;

/**
* author : HJQ
* github : https://github.com/getActivity/XXPermissions
* time : 2018/07/18
* desc : 动态申请的权限没有在清单文件中注册会抛出的异常
*/
class ManifestRegisterException extends RuntimeException {

ManifestRegisterException(String permission) {
super(permission == null ?
"No permissions are registered in the manifest file" :
(permission + ": Permissions are not registered in the manifest file"));
}
}
5 changes: 4 additions & 1 deletion library/src/main/java/com/hjq/permissions/OnPermission.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import java.util.List;

/**
* Created by HJQ on 2018-6-15.
* author : HJQ
* github : https://github.com/getActivity/XXPermissions
* time : 2018/06/15
* desc : 权限请求结果回调接口
*/
public interface OnPermission {

Expand Down
76 changes: 45 additions & 31 deletions library/src/main/java/com/hjq/permissions/Permission.java
Original file line number Diff line number Diff line change
@@ -1,66 +1,81 @@
package com.hjq.permissions;

/**
* Created by HJQ on 2018-6-15.
* author : HJQ
* github : https://github.com/getActivity/XXPermissions
* time : 2018/06/15
* desc : 权限请求实体类
*/
public final class Permission {

public static final String READ_CALENDAR = "android.permission.READ_CALENDAR";
public static final String WRITE_CALENDAR = "android.permission.WRITE_CALENDAR";
public static final String REQUEST_INSTALL_PACKAGES = "android.permission.REQUEST_INSTALL_PACKAGES"; // 8.0应用安装权限

public static final String CAMERA = "android.permission.CAMERA";
public static final String SYSTEM_ALERT_WINDOW = "android.permission.SYSTEM_ALERT_WINDOW"; // 6.0悬浮窗权限

public static final String READ_CONTACTS = "android.permission.READ_CONTACTS";
public static final String WRITE_CONTACTS = "android.permission.WRITE_CONTACTS";
public static final String GET_ACCOUNTS = "android.permission.GET_ACCOUNTS";
public static final String READ_CALENDAR = "android.permission.READ_CALENDAR"; // 读取日程提醒
public static final String WRITE_CALENDAR = "android.permission.WRITE_CALENDAR"; // 写入日程提醒

public static final String ACCESS_FINE_LOCATION = "android.permission.ACCESS_FINE_LOCATION";
public static final String ACCESS_COARSE_LOCATION = "android.permission.ACCESS_COARSE_LOCATION";
public static final String CAMERA = "android.permission.CAMERA"; // 拍照权限

public static final String RECORD_AUDIO = "android.permission.RECORD_AUDIO";
public static final String READ_CONTACTS = "android.permission.READ_CONTACTS"; // 读取联系人
public static final String WRITE_CONTACTS = "android.permission.WRITE_CONTACTS"; // 写入联系人
public static final String GET_ACCOUNTS = "android.permission.GET_ACCOUNTS"; // 访问账户列表

public static final String READ_PHONE_STATE = "android.permission.READ_PHONE_STATE";
public static final String CALL_PHONE = "android.permission.CALL_PHONE";
public static final String READ_CALL_LOG = "android.permission.READ_CALL_LOG";
public static final String WRITE_CALL_LOG = "android.permission.WRITE_CALL_LOG";
public static final String ADD_VOICEMAIL = "com.android.voicemail.permission.ADD_VOICEMAIL";
public static final String USE_SIP = "android.permission.USE_SIP";
public static final String PROCESS_OUTGOING_CALLS = "android.permission.PROCESS_OUTGOING_CALLS";
public static final String ACCESS_FINE_LOCATION = "android.permission.ACCESS_FINE_LOCATION"; // 获取精确位置
public static final String ACCESS_COARSE_LOCATION = "android.permission.ACCESS_COARSE_LOCATION"; // 获取粗略位置

public static final String BODY_SENSORS = "android.permission.BODY_SENSORS";
public static final String RECORD_AUDIO = "android.permission.RECORD_AUDIO"; // 录音权限

public static final String SEND_SMS = "android.permission.SEND_SMS";
public static final String RECEIVE_SMS = "android.permission.RECEIVE_SMS";
public static final String READ_SMS = "android.permission.READ_SMS";
public static final String RECEIVE_WAP_PUSH = "android.permission.RECEIVE_WAP_PUSH";
public static final String RECEIVE_MMS = "android.permission.RECEIVE_MMS";
public static final String READ_PHONE_STATE = "android.permission.READ_PHONE_STATE"; // 读取电话状态
public static final String CALL_PHONE = "android.permission.CALL_PHONE"; // 拨打电话
public static final String READ_CALL_LOG = "android.permission.READ_CALL_LOG"; // 读取通话记录
public static final String WRITE_CALL_LOG = "android.permission.WRITE_CALL_LOG"; // 写入通话记录
public static final String ADD_VOICEMAIL = "com.android.voicemail.permission.ADD_VOICEMAIL"; // 添加语音邮件
public static final String USE_SIP = "android.permission.USE_SIP"; // 使用SIP视频
public static final String PROCESS_OUTGOING_CALLS = "android.permission.PROCESS_OUTGOING_CALLS"; // 处理拨出电话

public static final String READ_EXTERNAL_STORAGE = "android.permission.READ_EXTERNAL_STORAGE";
public static final String WRITE_EXTERNAL_STORAGE = "android.permission.WRITE_EXTERNAL_STORAGE";
public static final String BODY_SENSORS = "android.permission.BODY_SENSORS"; // 传感器

public static final String REQUEST_INSTALL_PACKAGES = "android.permission.REQUEST_INSTALL_PACKAGES";
public static final String SEND_SMS = "android.permission.SEND_SMS"; // 发送短信
public static final String RECEIVE_SMS = "android.permission.RECEIVE_SMS"; // 接收短信
public static final String READ_SMS = "android.permission.READ_SMS"; // 读取短信
public static final String RECEIVE_WAP_PUSH = "android.permission.RECEIVE_WAP_PUSH"; // 接收WAP PUSH信息
public static final String RECEIVE_MMS = "android.permission.RECEIVE_MMS"; // 接收彩信

public static final String SYSTEM_ALERT_WINDOW = "android.permission.SYSTEM_ALERT_WINDOW";
public static final String READ_EXTERNAL_STORAGE = "android.permission.READ_EXTERNAL_STORAGE"; // 读取外部存储
public static final String WRITE_EXTERNAL_STORAGE = "android.permission.WRITE_EXTERNAL_STORAGE"; // 写入外部存储

public static final class Group {

// 安装
public static final String[] INSTALL = new String[]{Permission.REQUEST_INSTALL_PACKAGES};

// 悬浮窗
public static final String[] WINDOW = new String[]{Permission.SYSTEM_ALERT_WINDOW};

// 日历
public static final String[] CALENDAR = new String[]{
Permission.READ_CALENDAR,
Permission.WRITE_CALENDAR};

// 摄像头
public static final String[] CAMERA = new String[]{Permission.CAMERA};

// 联系人
public static final String[] CONTACTS = new String[]{
Permission.READ_CONTACTS,
Permission.WRITE_CONTACTS,
Permission.GET_ACCOUNTS};

// 位置
public static final String[] LOCATION = new String[]{
Permission.ACCESS_FINE_LOCATION,
Permission.ACCESS_COARSE_LOCATION};

// 话筒
public static final String[] MICROPHONE = new String[]{Permission.RECORD_AUDIO};

// 电话
public static final String[] PHONE = new String[]{
Permission.READ_PHONE_STATE,
Permission.CALL_PHONE,
Expand All @@ -70,21 +85,20 @@ public static final class Group {
Permission.USE_SIP,
Permission.PROCESS_OUTGOING_CALLS};

// 传感器
public static final String[] SENSORS = new String[]{Permission.BODY_SENSORS};

// 短信
public static final String[] SMS = new String[]{
Permission.SEND_SMS,
Permission.RECEIVE_SMS,
Permission.READ_SMS,
Permission.RECEIVE_WAP_PUSH,
Permission.RECEIVE_MMS};

// 存储
public static final String[] STORAGE = new String[]{
Permission.READ_EXTERNAL_STORAGE,
Permission.WRITE_EXTERNAL_STORAGE};

public static final String[] INSTALL = new String[]{Permission.REQUEST_INSTALL_PACKAGES};

public static final String[] WINDOW = new String[]{Permission.SYSTEM_ALERT_WINDOW};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
import java.util.Random;

/**
* Created by HJQ on 2018-6-15.
* author : HJQ
* github : https://github.com/getActivity/XXPermissions
* time : 2018/06/15
* desc : 权限请求处理类
*/
public final class PermissionFragment extends Fragment {

Expand Down Expand Up @@ -156,8 +159,9 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
//super.onActivityResult(requestCode, resultCode, data);
if (!isBackCall && requestCode == getArguments().getInt(REQUEST_CODE) ) {
isBackCall = true;
//延迟执行
//需要延迟执行,不然有些华为机型授权了但是获取不到权限
new Handler().postDelayed(new Runnable() {

@Override
public void run() {
//请求其他危险权限
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
import android.provider.Settings;

/**
* Created by HJQ on 2018-7-18.
* author : HJQ
* github : https://github.com/getActivity/XXPermissions
* time : 2018/07/18
* desc : 权限设置页(支持大部分国产手机)
*/
final class PermissionSettingPage {

Expand Down
24 changes: 18 additions & 6 deletions library/src/main/java/com/hjq/permissions/PermissionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
import java.util.List;

/**
* Created by HJQ on 2018-6-15.
* author : HJQ
* github : https://github.com/getActivity/XXPermissions
* time : 2018/06/15
* desc : 权限请求工具类
*/
final class PermissionUtils {

Expand Down Expand Up @@ -44,7 +47,7 @@ static List<String> getManifestPermissions(Context context) {
/**
* 是否有安装权限
*/
public static boolean isHasInstallPermission(Context context) {
static boolean isHasInstallPermission(Context context) {
if (isOverOreo()) {

//必须设置目标SDK为26及以上才能正常检测安装权限
Expand All @@ -60,7 +63,7 @@ public static boolean isHasInstallPermission(Context context) {
/**
* 是否有悬浮窗权限
*/
public static boolean isHasOverlaysPermission(Context context) {
static boolean isHasOverlaysPermission(Context context) {

if (isOverMarshmallow()) {

Expand All @@ -82,28 +85,37 @@ public static boolean isHasOverlaysPermission(Context context) {
*/
static ArrayList<String> getFailPermissions(Context context, List<String> permissions) {

//必须设置目标SDK为23及以上才能正常检测安装权限
if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.M) {
throw new RuntimeException("The targetSdkVersion SDK must be 23 or more");
}

//如果是安卓6.0以下版本就返回null
if (!PermissionUtils.isOverMarshmallow()) {
return null;
}

ArrayList<String> failPermissions = new ArrayList<>();
ArrayList<String> failPermissions = null;

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 (failPermissions == null) failPermissions = new ArrayList<>();
failPermissions.add(permission);
continue;
}

//把没有授予过的权限加入到集合中
if (context.checkSelfPermission(permission) == PackageManager.PERMISSION_DENIED) {
if (failPermissions == null) failPermissions = new ArrayList<>();
failPermissions.add(permission);
}
}
Expand Down Expand Up @@ -159,11 +171,11 @@ static void checkPermissions(Activity activity, List<String> requestPermissions)
if (manifest != null && manifest.size() != 0) {
for (String permission : requestPermissions) {
if (!manifest.contains(permission)) {
throw new ManifestPermissionException(permission);
throw new ManifestRegisterException(permission);
}
}
} else {
throw new ManifestPermissionException(null);
throw new ManifestRegisterException(null);
}
}
}
Loading

0 comments on commit 41257a2

Please sign in to comment.