Skip to content

Commit

Permalink
新增支持8.0的两个新权限
Browse files Browse the repository at this point in the history
  • Loading branch information
HJQ committed Oct 10, 2018
1 parent 65109db commit 06be55e
Show file tree
Hide file tree
Showing 14 changed files with 113 additions and 63 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#### 集成步骤

dependencies {
implementation 'com.hjq:xxpermissions:5.0'
implementation 'com.hjq:xxpermissions:5.2'
}

#### 一句代码搞定权限请求,从未如此简单
Expand Down
Binary file modified XXPermissions.apk
Binary file not shown.
5 changes: 3 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 50
versionName "5.0"
versionCode 52
versionName "5.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand All @@ -23,4 +23,5 @@ dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation project(':library')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.hjq:toast:2.2'
}
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>

<!-- 8.0两个新权限 -->
<uses-permission android:name="android.permission.ANSWER_PHONE_CALLS"/>
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS"/>

<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand Down
19 changes: 12 additions & 7 deletions app/src/main/java/com/hjq/permissions/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.hjq.permissions.OnPermission;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.hjq.toast.ToastUtils;
import com.hjq.toast.ToastWhiteStyle;

import java.util.List;

Expand All @@ -17,6 +18,10 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//使用吐司白底黑字样式
ToastUtils.initStyle(new ToastWhiteStyle());
//初始化吐司工具类
ToastUtils.init(getApplicationContext());
}

public void requestPermission(View view) {
Expand All @@ -29,30 +34,30 @@ public void requestPermission(View view) {
@Override
public void hasPermission(List<String> granted, boolean isAll) {
if (isAll) {
Toast.makeText(MainActivity.this, "获取权限成功", Toast.LENGTH_SHORT).show();
ToastUtils.show("获取权限成功");
}else {
Toast.makeText(MainActivity.this, "获取权限成功,部分权限未正常授予", Toast.LENGTH_SHORT).show();
ToastUtils.show("获取权限成功,部分权限未正常授予");
}
}

@Override
public void noPermission(List<String> denied, boolean quick) {
if(quick) {
Toast.makeText(MainActivity.this, "被永久拒绝授权,请手动授予权限", Toast.LENGTH_SHORT).show();
ToastUtils.show("被永久拒绝授权,请手动授予权限");
//如果是被永久拒绝就跳转到应用权限系统设置页面
XXPermissions.gotoPermissionSettings(MainActivity.this);
}else {
Toast.makeText(MainActivity.this, "获取权限失败", Toast.LENGTH_SHORT).show();
ToastUtils.show("获取权限失败");
}
}
});
}

public void isHasPermission(View view) {
if (XXPermissions.isHasPermission(MainActivity.this, Permission.Group.STORAGE)) {
Toast.makeText(MainActivity.this, "已经获取到权限,不需要再次申请了", Toast.LENGTH_SHORT).show();
ToastUtils.show("已经获取到权限,不需要再次申请了");
}else {
Toast.makeText(MainActivity.this, "还没有获取到权限或者部分权限未授予", Toast.LENGTH_SHORT).show();
ToastUtils.show("还没有获取到权限或者部分权限未授予");
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
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 50
versionName "5.0"
versionCode 52
versionName "5.2"
}
}

publish {
userOrg = 'getactivity'//填写bintray用户名,注意大小写
groupId = 'com.hjq'//定义的maven group id最终引用形式
artifactId = 'xxpermissions'//maven的artifact id
version = '5.0'//maven 上发布版本号
version = '5.2'//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 @@ -6,7 +6,7 @@
* time : 2018/07/18
* desc : 动态申请的权限没有在清单文件中注册会抛出的异常
*/
class ManifestRegisterException extends RuntimeException {
final class ManifestRegisterException extends RuntimeException {

ManifestRegisterException(String permission) {
super(permission == null ?
Expand Down
8 changes: 4 additions & 4 deletions library/src/main/java/com/hjq/permissions/OnPermission.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ public interface OnPermission {
/**
* 有权限被授予时回调
*
* @param granted 请求成功的权限组
* @param isAll 是否全部授予了
* @param granted 请求成功的权限组
* @param isAll 是否全部授予了
*/
void hasPermission(List<String> granted, boolean isAll);

/**
* 有权限被拒绝授予时回调
*
* @param denied 请求失败的权限组
* @param quick 是否有某个权限被永久拒绝了
* @param denied 请求失败的权限组
* @param quick 是否有某个权限被永久拒绝了
*/
void noPermission(List<String> denied, boolean quick);
}
2 changes: 2 additions & 0 deletions library/src/main/java/com/hjq/permissions/Permission.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public final class Permission {
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 ANSWER_PHONE_CALLS = "android.permission.ANSWER_PHONE_CALLS";// 8.0危险权限:允许您的应用通过编程方式接听呼入电话。要在您的应用中处理呼入电话,您可以使用 acceptRingingCall() 函数
public static final String READ_PHONE_NUMBERS = "android.permission.READ_PHONE_NUMBERS";// 8.0危险权限:权限允许您的应用读取设备中存储的电话号码

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

Expand Down
32 changes: 22 additions & 10 deletions library/src/main/java/com/hjq/permissions/PermissionFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.util.SparseArray;

Expand All @@ -20,7 +19,7 @@
* time : 2018/06/15
* desc : 权限请求处理类
*/
public final class PermissionFragment extends Fragment {
public final class PermissionFragment extends Fragment implements Runnable {

private static final String PERMISSION_GROUP = "permission_group";//请求的权限
private static final String REQUEST_CODE = "request_code";
Expand Down Expand Up @@ -103,6 +102,7 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in

for (int i = 0; i < permissions.length; i++) {

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

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

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

//检查当前的安卓版本是否符合要求
if (!PermissionUtils.isOverOreo()) {
grantResults[i] = PackageManager.PERMISSION_GRANTED;
}
}
}

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

@Override
public void run() {
//请求其他危险权限
requestPermission();
}
}, 500);
getActivity().getWindow().getDecorView().postDelayed(this, 500);
}
}

/**
* {@link Runnable#run()}
*/
@Override
public void run() {
//请求其他危险权限
requestPermission();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class PermissionSettingPage {
*/
static void start(Context context, boolean newTask) {

Intent intent;
Intent intent = null;
if (MARK.contains("huawei")) {
intent = huawei(context);
} else if (MARK.contains("xiaomi")) {
Expand All @@ -37,7 +37,9 @@ static void start(Context context, boolean newTask) {
intent = vivo(context);
} else if (MARK.contains("meizu")) {
intent = meizu(context);
} else {
}

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

Expand All @@ -47,7 +49,7 @@ static void start(Context context, boolean newTask) {

try {
context.startActivity(intent);
} catch (Exception e) {
} catch (Exception ignored) {
intent = google(context);
context.startActivity(intent);
}
Expand Down
Loading

0 comments on commit 06be55e

Please sign in to comment.