Skip to content

Commit

Permalink
Merge pull request #1 from bugyun/develop
Browse files Browse the repository at this point in the history
正式包 MissPermission
  • Loading branch information
bugyun authored Aug 5, 2019
2 parents f3942a6 + 336af76 commit 6b886a0
Show file tree
Hide file tree
Showing 52 changed files with 1,767 additions and 1,239 deletions.
2 changes: 1 addition & 1 deletion MissPermission/jcenter.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ publish {
userOrg = 'bugyun' //bintray.com用户名
groupId = 'vip.ruoyun.permission' //jcenter group
artifactId = 'miss-core' //项目名称 name
publishVersion = '1.0.0'//版本号 version
publishVersion = '1.0.1'//版本号 version
desc = 'Android 权限请求库 MissPermission'//描述
website = 'https://github.com/bugyun/MissPermission' // github 网址
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,18 @@ public static Builder with(Fragment fragment) {
}

public static void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (MissPermission.getInstance().builder.request.getRequestCode() == requestCode) {
MissPermission.getInstance().builder.request.onRequestPermissionsResult(permissions, grantResults);
if (MissPermission.getInstance().builder != null) {
if (MissPermission.getInstance().builder.request.getRequestCode() == requestCode) {
MissPermission.getInstance().builder.request.onRequestPermissionsResult(permissions, grantResults);
}
}
}

public static class Builder {

private PermissionRequest request;

Builder(Activity activity) {
private Builder(Activity activity) {
request = new PermissionRequest(activity);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,24 @@
*/
public class PermissionRequest {

private Set<String> permissionList = new HashSet<>();
private Set<String> agreePermissionList = new HashSet<>();
private Set<String> rejectPermissionList = new HashSet<>();
private PermissionListener permissionListener;
private final WeakReference<Activity> activityWeakReference;
private int requestCode = 9898;
//
private Set<String> permissionList = new HashSet<>();//请求的权限
private Set<String> agreePermissionList = new HashSet<>();//同意的权限
private Set<String> deniedPermissionList = new HashSet<>();//拒绝的权限

private boolean alwaysDenied = false;//是否总是拒绝
private boolean isOver23;//是不是棉花糖,大于:true 小于 false Build.VERSION_CODES.M

public boolean isOver23() {
return isOver23;
}

public Context getContext() {
return activityWeakReference.get();
}
//
private int requestCode = 9898;
private PermissionListener permissionListener;
private final WeakReference<Activity> activityWeakReference;

PermissionRequest(Activity activity) {
activityWeakReference = new WeakReference<>(activity);
}

void addPermission(String permission) {
permissionList.add(permission);
}

void addPermissions(List<String> permissions) {
permissionList.addAll(permissions);
}

public Set<String> getPermissionList() {
return permissionList;
}

//开始
void start(PermissionListener permissionListener) {
this.permissionListener = permissionListener;
checkPermission();
Expand All @@ -77,10 +62,10 @@ private void checkPermission() {
if (checkSelfPermission == PackageManager.PERMISSION_GRANTED) {//如果同意
agreePermissionList.add(permission);
} else {//如果拒绝
rejectPermissionList.add(permission);
deniedPermissionList.add(permission);
}
}
switch (permissionListener.onChecked(agreePermissionList, rejectPermissionList, this)) {
switch (permissionListener.onChecked(this)) {
case MissPermission.NEXT_STEP:
next();
break;
Expand All @@ -106,13 +91,13 @@ private void checkPermission() {
if (checkSelfPermission == PermissionChecker.PERMISSION_GRANTED) {//已授权
agreePermissionList.add(permission);
} else {//未授权
rejectPermissionList.add(permission);
deniedPermissionList.add(permission);
}
}
if (permissionList.size() == agreePermissionList.size()) {
permissionListener.onSuccess(this); //所有权限都通过
} else {
permissionListener.onDenied(rejectPermissionList, true, this);
permissionListener.onDenied(this);
}
}
}
Expand All @@ -122,56 +107,46 @@ private void requestPermission() {
//所有权限都通过
permissionListener.onSuccess(this);
} else {
requestPermissionsAgain(rejectPermissionList);
requestPermissionsAgain();
}
}

public void requestPermissionsAgain(Set<String> permissionLists) {
public void requestPermissionsAgain() {
if (activityWeakReference.get() == null) {
permissionListener.onFailure(new PermissionException("activity 为空"));
return;
}
// ActivityCompat.requestPermissions(activityWeakReference.get(), permissionLists.toArray(new String[permissionLists.size()]), requestCode);
ActivityCompat.requestPermissions(activityWeakReference.get(), permissionLists.toArray(new String[0]), requestCode);
ActivityCompat.requestPermissions(activityWeakReference.get(), deniedPermissionList.toArray(new String[0]), requestCode);
}

void setRequestCode(int requestCode) {
this.requestCode = requestCode;
}

int getRequestCode() {
return requestCode;
}

public interface PermissionListener {
/**
* @param agreePermissions
* @param deniedPermissions
* @param request
* @return NEXT_STEP:直接下一步,不用等待
* STOP_STEP:直接停止,不执行下一步
* PAUSE_STEP:等待,等待命令唤起下一步
*/
int onChecked(Set<String> agreePermissions, Set<String> deniedPermissions, PermissionRequest request);//检查结束
int onChecked(PermissionRequest request);//检查结束

/**
* @param deniedPermissions
* @param alwaysDenied
* @param request
*/
void onDenied(Set<String> deniedPermissions, boolean alwaysDenied, PermissionRequest request);
void onDenied(PermissionRequest request);

void onSuccess(PermissionRequest request);//权限完成

void onFailure(PermissionException exception);//失败
}


/**
* @param permissions 请求的权限列表
* @param grantResults 请求权限的结果列表
*/
void onRequestPermissionsResult(String[] permissions, int[] grantResults) {
boolean alwaysDenied = false;
if (grantResults.length > 0) {
Set<String> deniedList = new HashSet<>();
// List<String> agreeList = new ArrayList<>();
// 遍历所有申请的权限,把被拒绝的权限放入集合
for (int i = 0; i < grantResults.length; i++) {
int grantResult = grantResults[i];
Expand All @@ -181,13 +156,59 @@ void onRequestPermissionsResult(String[] permissions, int[] grantResults) {
alwaysDenied = true;
}
deniedList.add(permissions[i]);
} else {
agreePermissionList.add(permissions[i]);
}
}
deniedPermissionList = deniedList;
if (!deniedList.isEmpty()) {
permissionListener.onDenied(deniedList, alwaysDenied, this);
permissionListener.onDenied(this);
} else {
permissionListener.onSuccess(this);
}
}
}

//build 方法 get set

void addPermission(String permission) {
permissionList.add(permission);
}

void addPermissions(List<String> permissions) {
permissionList.addAll(permissions);
}

void setRequestCode(int requestCode) {
this.requestCode = requestCode;
}

int getRequestCode() {
return requestCode;
}

//对外的 get set
public boolean isOver23() {
return isOver23;
}

public Context getContext() {
return activityWeakReference.get();
}

public Set<String> getPermissionList() {
return permissionList;
}

public boolean isAlwaysDenied() {
return alwaysDenied;
}

public Set<String> getAgreePermissionList() {
return agreePermissionList;
}

public Set<String> getDeniedPermissionList() {
return deniedPermissionList;
}
}
12 changes: 9 additions & 3 deletions MissPermissionHelper/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.library'
//apply from: 'jcenter.gradle'//上传 jcenter 的插件

android {
compileSdkVersion 28
Expand All @@ -21,14 +22,19 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
lintOptions {
abortOnError false
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
compileOnly 'com.android.support:appcompat-v7:28.0.0'
compileOnly 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support:support-annotations:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation project(path: ':MissPermission')
api 'vip.ruoyun.helper:avoid-onresult-helper:1.0.1'
}
20 changes: 20 additions & 0 deletions MissPermissionHelper/jcenter.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//上传 jcenter 的插件
apply plugin: 'com.novoda.bintray-release' // 新增

/**
* 参考
* 'com.android.support.test.espresso:espresso-core:3.0.2'
* group: 'com.android.support.test.espresso', name: 'espresso-core', version: '3.0.2'
*/

// 新增
publish {
userOrg = 'bugyun' //bintray.com用户名
groupId = 'vip.ruoyun.permission' //jcenter group
artifactId = 'miss-helper' //项目名称 name
publishVersion = '1.0.0'//版本号 version
desc = 'Android 权限请求库 MissPermissionHelper'//描述
website = 'https://github.com/bugyun/MissPermission' // github 网址
}

// ./gradlew clean build bintrayUpload -PbintrayUser=bintray.user -PbintrayKey=bintray.apikey -PdryRun=false
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package vip.ruoyun.permission.helper;


import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.Window;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import vip.ruoyun.permission.helper.ui.DialogUtil;
import vip.ruoyun.permission.helper.ui.MissPermissionView;
import vip.ruoyun.permission.helper.ui.PermissionAdapter;

public class DefaultAction implements IAction {

private Dialog mDialog;

@Override
public void checkedAction(final PermissionRequest request, Set<PermissionGroup> permissionGroups) {
if (request.getDeniedPermissionList().size() == 0) {
request.next();
return;
}
mDialog = new Dialog(request.getContext());
MissPermissionView contentView = new MissPermissionView(request.getContext());
contentView.setGridViewColum(permissionGroups.size() < 3 ? permissionGroups.size() : 3);
contentView.setGridViewAdapter(new PermissionAdapter(new ArrayList<>(permissionGroups)));
// if (request.getFilterColor() != 0) {
// int mFilterColor = request.getContext().getResources().getColor(request.getFilterColor());
// contentView.setFilterColor(mFilterColor);
// }
contentView.setTitle(request.getTitle());
contentView.setMsg(request.getMsg());
contentView.setStyleId(request.getStyleResId());
contentView.setBtnOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mDialog != null && mDialog.isShowing()) {
mDialog.dismiss();
}
request.next();
}
});
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setContentView(contentView);

mDialog.setCanceledOnTouchOutside(false);
mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
mDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
dialog.dismiss();
}
});
mDialog.show();
}

@Override
public void deniedAction(final PermissionRequest request) {
StringBuilder sBuilder = new StringBuilder();
Set<PermissionGroup> permissionGroups = new HashSet<>();
for (String deniedPermission : request.getDeniedPermissionList()) {
PermissionGroup permissionGroup = PermissionGroup.permissionGroupHashMap.get(deniedPermission);
if (permissionGroup != null) {
permissionGroups.add(permissionGroup);
}
}
for (PermissionGroup permissionGroup : permissionGroups) {
sBuilder.append(permissionGroup.permissionName);
sBuilder.append(",");
}
if (sBuilder.length() > 0) {
sBuilder.deleteCharAt(sBuilder.length() - 1);
}
if (request.isAlwaysDenied()) {
DialogUtil.showPermissionManagerDialog(request.getContext(), sBuilder.toString());
} else {
new AlertDialog.Builder(request.getContext()).setTitle("温馨提示").setMessage("我们需要这些权限才能正常使用该功能").setNegativeButton("取消", null).setPositiveButton("验证权限", new DialogInterface.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onClick(DialogInterface dialog, int which) {
request.requestPermissionsAgain();
}
}).setCancelable(false).show();
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package vip.ruoyun.permission.helper;

import java.util.Set;


public interface IAction {

void checkedAction(PermissionRequest request, Set<PermissionGroup> permissionGroups);

void deniedAction(PermissionRequest request);
}
Loading

0 comments on commit 6b886a0

Please sign in to comment.