Skip to content

Commit

Permalink
权限管理修改为permissionx
Browse files Browse the repository at this point in the history
  • Loading branch information
sdwfqin committed Jul 23, 2020
1 parent d43b5d8 commit cffa3d4
Show file tree
Hide file tree
Showing 12 changed files with 257 additions and 132 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ android {
signingConfig signingConfigs.sdwfqinSigning
}
debug {
minifyEnabled false
shrinkResources false
signingConfig signingConfigs.sdwfqinSigning
}
}
Expand Down
Binary file modified app/release/app-release.apk
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private void initTabs() {
private void getPermissions() {

String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
initCheckPermissions(perms, true, false, new OnPermissionCallback() {
initCheckPermissions(perms, new OnPermissionCallback() {
@Override
public void onSuccess() {
Toast.makeText(mContext, "取得权限", Toast.LENGTH_SHORT).show();
Expand Down
7 changes: 3 additions & 4 deletions config.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ext {

versionCode = 202007201
versionName = "4.2.0"
versionCode = 202007231
versionName = "4.3.0"

qmui = "2.0.0-alpha10"
okhttp = "4.7.2"
Expand Down Expand Up @@ -72,8 +72,7 @@ ext {
]

utilsDependencies = [
// easypermissions
"easypermissions": "pub.devrel:easypermissions:3.0.0",
"permissionx": "com.permissionx.guolindev:permissionx:1.3.0",
// utils
"utilcodex" : "com.blankj:utilcodex:1.29.0",
// agentweb
Expand Down
2 changes: 1 addition & 1 deletion quicklib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ dependencies {

api utilsDependencies["utilcodex"]
api utilsDependencies["agentweb"]
api utilsDependencies["easypermissions"]
api utilsDependencies["permissionx"]
api utilsDependencies["arouter"]
api utilsDependencies["gson"]

Expand Down
132 changes: 23 additions & 109 deletions quicklib/src/main/java/com/sdwfqin/quicklib/base/BaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;

Expand All @@ -12,10 +11,12 @@
import androidx.viewbinding.ViewBinding;

import com.blankj.utilcode.util.ToastUtils;
import com.permissionx.guolindev.PermissionX;
import com.qmuiteam.qmui.arch.QMUIActivity;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.sdwfqin.quicklib.R;
import com.sdwfqin.quicklib.dialog.PermissionCustomDialog;
import com.sdwfqin.quicklib.mvp.BaseView;
import com.sdwfqin.quicklib.utils.AppManager;
import com.sdwfqin.quicklib.utils.eventbus.Event;
Expand All @@ -24,21 +25,15 @@
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.disposables.Disposable;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

/**
* 描述:Activity基类
*
* @author 张钦
*/
public abstract class BaseActivity<V extends ViewBinding> extends QMUIActivity implements BaseView, EasyPermissions.PermissionCallbacks {

private static final int PERMS_REQUEST_CODE = 1122;
public abstract class BaseActivity<V extends ViewBinding> extends QMUIActivity implements BaseView {

protected Activity mContext;
protected V mBinding;
Expand All @@ -55,17 +50,6 @@ public abstract class BaseActivity<V extends ViewBinding> extends QMUIActivity i
* TipDialog
*/
protected QMUITipDialog mQmuiTipDialog;
/**
* 权限相关
*/
private String[] mPerms;
private OnPermissionCallback mOnPermissionCallback;
private boolean mShowPermissionsDialog;
private boolean mLoopPermissionsDialog;
/**
* 权限请求次数
*/
private int mPermissionsRequestNum = 0;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Expand Down Expand Up @@ -254,101 +238,31 @@ public interface OnPermissionCallback {
void onError();
}

/**
* 检查权限是否全部获取
*
* @param perms
* @param onPermissionCallback
*/
public void initCheckPermissions(String[] perms, OnPermissionCallback onPermissionCallback) {
initCheckPermissions(perms, false, false, onPermissionCallback);
}

/**
* 初始化权限检查
*
* @param perms 权限列表
* @param showPermissionsDialog true:拒绝显示弹窗
* @param loopPermissionsDialog true:弹窗关闭继续弹出弹窗
* @param onPermissionCallback 权限回掉接口
* @param perms 权限列表
* @param onPermissionCallback 权限回掉接口
*/
public void initCheckPermissions(String[] perms, boolean showPermissionsDialog, boolean loopPermissionsDialog,
OnPermissionCallback onPermissionCallback) {
mPerms = perms;
mShowPermissionsDialog = showPermissionsDialog;
mLoopPermissionsDialog = loopPermissionsDialog;
mOnPermissionCallback = onPermissionCallback;

checkPermissions();
}

/**
* 检查权限是否全部获取
*/
public void checkPermissions() {

if (EasyPermissions.hasPermissions(this, mPerms)) {
// Already have permission, do the thing
mOnPermissionCallback.onSuccess();
} else {
// Do not have permissions, request them now
if (mShowPermissionsDialog) {
mPermissionsRequestNum++;
new Handler().postDelayed(() -> {
EasyPermissions.requestPermissions(this, getString(R.string.quick_permissions_check_error),
PERMS_REQUEST_CODE, mPerms);
}, 100);
} else {
mOnPermissionCallback.onError();
}
}
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// Forward results to EasyPermissions
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}

@Override
public void onPermissionsGranted(int requestCode, List<String> perms) {
// Some permissions have been granted
if (perms.size() >= mPerms.length) {
mOnPermissionCallback.onSuccess();
}
}

@Override
public void onPermissionsDenied(int requestCode, List<String> perms) {
// Some permissions have been denied
if (mLoopPermissionsDialog || (mPermissionsRequestNum == 1 && EasyPermissions.somePermissionPermanentlyDenied(this, perms))) {
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
AppSettingsDialog.Builder builder = new AppSettingsDialog.Builder(this);
AppSettingsDialog appSettingsDialog = builder.setTitle(R.string.quick_permissions_dialog_title_settings)
.setRationale(R.string.quick_permissions_dialog_rationale_ask_again)
.setNegativeButton(R.string.quick_permissions_dialog_cancel)
.setPositiveButton(R.string.quick_permissions_dialog_to_setting)
.build();
appSettingsDialog.show();
} else {
checkPermissions();
}
} else {
mOnPermissionCallback.onError();
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) {
// Do something after user returned from app settings screen, like showing a Toast.
checkPermissions();
}
public void initCheckPermissions(String[] perms, OnPermissionCallback onPermissionCallback) {
PermissionX.init(this)
.permissions(perms)
.onExplainRequestReason((scope, deniedList, beforeRequest) -> {
PermissionCustomDialog dialog = new PermissionCustomDialog(mContext, getString(R.string.quick_permissions_title, getString(R.string.app_name)), deniedList);
scope.showRequestReasonDialog(dialog);
})
.onForwardToSettings((scope, deniedList) -> {
PermissionCustomDialog dialog = new PermissionCustomDialog(mContext, getString(R.string.quick_permissions_forward), deniedList);
scope.showForwardToSettingsDialog(dialog);
})
.request((allGranted, grantedList, deniedList) -> {
if (allGranted) {
onPermissionCallback.onSuccess();
} else {
onPermissionCallback.onError();
}
});
}

// ==================== 提供的接口 ====================

protected void initViewModel() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package com.sdwfqin.quicklib.dialog;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ScreenUtils;
import com.permissionx.guolindev.request.RationaleDialog;
import com.qmuiteam.qmui.skin.QMUISkinManager;
import com.sdwfqin.quicklib.R;
import com.sdwfqin.quicklib.databinding.QuickDialogPermissionBinding;
import com.sdwfqin.quicklib.databinding.QuickItemPermissionsBinding;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* 自定义权限弹窗
* <p>
*
* @author 张钦
* @date 2020/7/23
*/
public class PermissionCustomDialog extends RationaleDialog {

private Map<String, String> permissionMap = new HashMap<>();
private Set<String> groupSet = new HashSet<>();
private String message;
private List<String> permissions;
private QuickDialogPermissionBinding mBinding;

{
permissionMap.put(Manifest.permission.READ_CALENDAR, Manifest.permission_group.CALENDAR);
permissionMap.put(Manifest.permission.WRITE_CALENDAR, Manifest.permission_group.CALENDAR);
permissionMap.put(Manifest.permission.READ_CALL_LOG, Manifest.permission_group.CALL_LOG);
permissionMap.put(Manifest.permission.WRITE_CALL_LOG, Manifest.permission_group.CALL_LOG);
permissionMap.put(Manifest.permission.PROCESS_OUTGOING_CALLS, Manifest.permission_group.CALL_LOG);
permissionMap.put(Manifest.permission.CAMERA, Manifest.permission_group.CAMERA);
permissionMap.put(Manifest.permission.READ_CONTACTS, Manifest.permission_group.CONTACTS);
permissionMap.put(Manifest.permission.WRITE_CONTACTS, Manifest.permission_group.CONTACTS);
permissionMap.put(Manifest.permission.GET_ACCOUNTS, Manifest.permission_group.CONTACTS);
permissionMap.put(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission_group.LOCATION);
permissionMap.put(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission_group.LOCATION);
permissionMap.put(Manifest.permission.ACCESS_BACKGROUND_LOCATION, Manifest.permission_group.LOCATION);
permissionMap.put(Manifest.permission.RECORD_AUDIO, Manifest.permission_group.MICROPHONE);
permissionMap.put(Manifest.permission.READ_PHONE_STATE, Manifest.permission_group.PHONE);
permissionMap.put(Manifest.permission.READ_PHONE_NUMBERS, Manifest.permission_group.PHONE);
permissionMap.put(Manifest.permission.CALL_PHONE, Manifest.permission_group.PHONE);
permissionMap.put(Manifest.permission.ANSWER_PHONE_CALLS, Manifest.permission_group.PHONE);
permissionMap.put(Manifest.permission.ADD_VOICEMAIL, Manifest.permission_group.PHONE);
permissionMap.put(Manifest.permission.USE_SIP, Manifest.permission_group.PHONE);
permissionMap.put(Manifest.permission.ACCEPT_HANDOVER, Manifest.permission_group.PHONE);
permissionMap.put(Manifest.permission.BODY_SENSORS, Manifest.permission_group.SENSORS);
permissionMap.put(Manifest.permission.ACTIVITY_RECOGNITION, Manifest.permission_group.ACTIVITY_RECOGNITION);
permissionMap.put(Manifest.permission.SEND_SMS, Manifest.permission_group.SMS);
permissionMap.put(Manifest.permission.RECEIVE_SMS, Manifest.permission_group.SMS);
permissionMap.put(Manifest.permission.READ_SMS, Manifest.permission_group.SMS);
permissionMap.put(Manifest.permission.RECEIVE_WAP_PUSH, Manifest.permission_group.SMS);
permissionMap.put(Manifest.permission.RECEIVE_MMS, Manifest.permission_group.SMS);
permissionMap.put(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission_group.STORAGE);
permissionMap.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission_group.STORAGE);
permissionMap.put(Manifest.permission.ACCESS_MEDIA_LOCATION, Manifest.permission_group.STORAGE);
}

public PermissionCustomDialog(@NonNull Context context, String message, List<String> permissions) {
super(context, R.style.transactionDialog);
this.message = message;
this.permissions = permissions;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = QuickDialogPermissionBinding.inflate(getLayoutInflater());
setContentView(mBinding.getRoot());

mBinding.messageText.setText(message);
buildPermissionsLayout();
if (getWindow() != null) {
int width = (int) (ScreenUtils.getScreenWidth() * 0.8);
getWindow()
.setLayout(width, WindowManager.LayoutParams.WRAP_CONTENT);
getWindow().setGravity(Gravity.CENTER);
}
}

@Override
public void onStart() {
super.onStart();
QMUISkinManager.defaultInstance(getContext()).register(this);
}

private void buildPermissionsLayout() {
for (String permission : permissions) {
String permissionGroup = permissionMap.get(permission);
if (permissionGroup != null && !groupSet.contains(permissionGroup)) {
QuickItemPermissionsBinding permissionsItemBinding = QuickItemPermissionsBinding.inflate(getLayoutInflater());
try {
permissionsItemBinding.bodyItem.setText(getContext().getPackageManager().getPermissionGroupInfo(permissionGroup, 0).loadLabel(getContext().getPackageManager()));
mBinding.permissionsLayout.addView(permissionsItemBinding.getRoot());
groupSet.add(permissionGroup);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
}
}

@NonNull
@Override
public View getPositiveButton() {
return mBinding.positiveBtn;
}

@Nullable
@Override
public View getNegativeButton() {
return mBinding.negativeBtn;
}

@NonNull
@Override
public List<String> getPermissionsToRequest() {
return permissions;
}
}
6 changes: 6 additions & 0 deletions quicklib/src/main/res/drawable/quick_permission_dialog_bg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="?attr/app_content_bg_primary_color" />
<corners android:radius="10dp" />
</shape>
Loading

0 comments on commit cffa3d4

Please sign in to comment.