Skip to content

Commit

Permalink
feat(dangerous permission):添加串行申请危险权限
Browse files Browse the repository at this point in the history
  • Loading branch information
junhua committed Jun 12, 2019
1 parent 19b27f0 commit 0d4b6f0
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import cn.junhua.android.permission.agent.PermissionHandlerFactory;
import cn.junhua.android.permission.agent.check.PermissionChecker;
import cn.junhua.android.permission.dangerous.DangerousPermissionAgent;
import cn.junhua.android.permission.dangerous.EachDangerousPermissionAgent;
import cn.junhua.android.permission.dangerous.checker.DoublePermissionChecker;
import cn.junhua.android.permission.dangerous.checker.Permission;
import cn.junhua.android.permission.impl.ActivityHolder;
Expand Down Expand Up @@ -54,7 +55,7 @@ public void init(Application application) {
}

/**
* 危险(Dangerous)权限
* 并行申请危险(Dangerous)权限
*
* @param permissions 权限列表
* @return Agent危险权限申请操作
Expand All @@ -64,7 +65,7 @@ public Agent<List<String>> request(String... permissions) {
}

/**
* 危险(Dangerous)权限
* 并行申请危险(Dangerous)权限
*
* @param permissions 权限列表
* @return Agent危险权限申请操作
Expand All @@ -73,6 +74,26 @@ public Agent<List<String>> request(List<String> permissions) {
return request(permissions.toArray(new String[0]));
}

/**
* 串行申请危险(Dangerous)权限
*
* @param permissions 权限列表
* @return Agent危险权限申请操作
*/
public Agent<List<String>> requestEach(String... permissions) {
return new EachDangerousPermissionAgent(mExecutor, getPermissionHandler(), permissions);
}

/**
* 串行申请危险(Dangerous)权限
*
* @param permissions 权限列表
* @return Agent危险权限申请操作
*/
public Agent<List<String>> requestEach(List<String> permissions) {
return requestEach(permissions.toArray(new String[0]));
}

/**
* 特殊(Special)权限
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,34 @@ public class DangerousPermissionAgent extends BaseAgent<List<String>> implements
public DangerousPermissionAgent(Executor executor, PermissionHandler permissionHandler, String[] permissions) {
super(executor);
mPermissionHandler = permissionHandler;
mPermissions = Permission.handleGroup(permissions);//处理权限组
mPermissions = onInitPermissions(permissions);
mPermissionHandler.setOnPermissionResultCallback(this);
}

public List<String> onInitPermissions(String[] permissions) {
//处理权限组
return Permission.handleGroup(permissions);
}

public List<String> getPermissions() {
return mPermissions;
}

/**
* 执行请求操作
*/
public void apply() {
post(new Runnable() {
mExecutor.post(new Runnable() {
@Override
public void run() {
if (STANDARD_CHECKER.hasPermissions(mPermissionHandler.getActivity(), mPermissions)) {
dispatchGranted(mPermissions);
if (STANDARD_CHECKER.hasPermissions(mPermissionHandler.getActivity(), getPermissions())) {
dispatchGranted(getPermissions());
return;
}

//给用户提示再请求权限
List<String> rationaleList = new ArrayList<>(1);
for (String permission : mPermissions) {
for (String permission : getPermissions()) {
if (mPermissionHandler.shouldShowRationale(permission)) {
rationaleList.add(permission);
}
Expand All @@ -66,22 +74,22 @@ public void run() {
dispatchRationale(rationaleList, DangerousPermissionAgent.this);
}
}
});
}, 100);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) {
AgentLog.d(TAG, "onRequestPermissionsResult() called with: requestCode = [" + requestCode + "], permissions = " + Arrays.toString(permissions) + ", grantResults = " + Arrays.toString(grantResults));
if (requestCode != mRequestCode || grantResults.length <= 0) return;

asyncPost(new Runnable() {
mExecutor.asyncPost(new Runnable() {
@Override
public void run() {
Context context = mPermissionHandler.getContext();
List<String> grantedList = new ArrayList<>(1);
List<String> deniedList = new ArrayList<>(1);

for (String permission : mPermissions) {
for (String permission : permissions) {
if (DOUBLE_CHECKER.hasPermissions(context, permission)) {
grantedList.add(permission);
} else {
Expand All @@ -106,16 +114,16 @@ public void run() {

@Override
public void execute() {
post(new Runnable() {
mExecutor.post(new Runnable() {
@Override
public void run() {
mPermissionHandler.requestPermissions(mPermissions.toArray(new String[0]), mRequestCode);
mPermissionHandler.requestPermissions(getPermissions().toArray(new String[0]), mRequestCode);
}
});
}

@Override
public void cancel() {
dispatchDenied(mPermissions);
dispatchDenied(getPermissions());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cn.junhua.android.permission.dangerous;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.junhua.android.permission.agent.PermissionHandler;
import cn.junhua.android.permission.dangerous.checker.Permission;
import cn.junhua.android.permission.utils.Executor;

/**
* 串行申请危险权限
*
* @author [email protected]<br/>
* CREATED 2018/12/19 13:39
*/
public class EachDangerousPermissionAgent extends DangerousPermissionAgent {

private List<String> mPermissionListTemp = new ArrayList<>(1);

public EachDangerousPermissionAgent(Executor executor, PermissionHandler permissionHandler, String[] permissions) {
super(executor, permissionHandler, permissions);
}

@Override
public List<String> onInitPermissions(String[] permissions) {
return new ArrayList<>(Arrays.asList(permissions));
}

@Override
public List<String> getPermissions() {
return mPermissionListTemp;
}

@Override
public void apply() {
List<String> permissions = super.getPermissions();
if (permissions.isEmpty()) return;

mPermissionListTemp.clear();
mPermissionListTemp.addAll(Permission.handleGroup(permissions.remove(0)));
super.apply();
}

@Override
protected void dispatchGranted(List<String> permissions) {
super.dispatchGranted(permissions);
apply();
}

@Override
protected void dispatchDenied(List<String> permissions) {
super.dispatchDenied(permissions);
apply();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public abstract class BaseAgent<T> implements Agent<T>, AgentExecutor {
private static final int REQUEST_CODE = 0x1226;

protected int mRequestCode = REQUEST_CODE;
private Executor mExecutor;
protected Executor mExecutor;

private OnGrantedCallback<T> mOnGrantedCallback;
private OnDeniedCallback<T> mOnDeniedCallback;
Expand Down Expand Up @@ -57,7 +57,7 @@ public Agent<T> onRationale(OnRationaleCallback<T> rationale) {

protected void dispatchGranted(final T permissions) {
if (mOnGrantedCallback != null) {
post(new Runnable() {
mExecutor.post(new Runnable() {
@Override
public void run() {
mOnGrantedCallback.onGranted(permissions);
Expand All @@ -68,7 +68,7 @@ public void run() {

protected void dispatchDenied(final T permissions) {
if (mOnDeniedCallback != null) {
post(new Runnable() {
mExecutor.post(new Runnable() {
@Override
public void run() {
mOnDeniedCallback.onDenied(permissions);
Expand All @@ -79,7 +79,7 @@ public void run() {

protected void dispatchRationale(final T permissions, final AgentExecutor executor) {
if (mOnRationaleCallback != null) {
post(new Runnable() {
mExecutor.post(new Runnable() {
@Override
public void run() {
mOnRationaleCallback.onRationale(permissions, executor);
Expand All @@ -88,12 +88,4 @@ public void run() {
}
}

protected void post(Runnable runnable) {
mExecutor.post(runnable);
}

protected void asyncPost(Runnable runnable) {
mExecutor.asyncPost(runnable);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import cn.junhua.android.permission.utils.AgentLog;
import cn.junhua.android.permission.utils.Executor;

import static android.app.Activity.RESULT_OK;

/**
* 特殊权限申请
*
Expand All @@ -35,7 +33,7 @@ private boolean checkPermission() {

@Override
public void apply() {
post(new Runnable() {
mExecutor.post(new Runnable() {
@Override
public void run() {
if (checkPermission()) {
Expand All @@ -56,7 +54,7 @@ public void onActivityResultCallback(final int requestCode, final int resultCode
return;
}

post(new Runnable() {
mExecutor.post(new Runnable() {
@Override
public void run() {
if (checkPermission()) {
Expand All @@ -70,7 +68,7 @@ public void run() {

@Override
public void execute() {
post(new Runnable() {
mExecutor.post(new Runnable() {
@Override
public void run() {
mSpecialPermission.startActivityForResult(mPermissionHandler, mRequestCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public void post(Runnable runnable) {
mHandler.post(runnable);
}

public void post(Runnable runnable, long delayMillis) {
mHandler.postDelayed(runnable, delayMillis);
}

public void asyncPost(Runnable runnable) {
mExecutor.execute(runnable);
}
Expand Down

0 comments on commit 0d4b6f0

Please sign in to comment.