From 120e9a3d84d9e74a2937d24726ede3fc73ca1ec1 Mon Sep 17 00:00:00 2001 From: goweii Date: Sat, 21 Sep 2019 20:43:18 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E7=94=B3=E8=AF=B7=E6=A3=80?= =?UTF-8?q?=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../goweii/anypermission/ContextHolder.java | 26 ++++++++++- .../anypermission/RuntimeRequester.java | 43 +++++++------------ 2 files changed, 40 insertions(+), 29 deletions(-) diff --git a/anypermission/src/main/java/per/goweii/anypermission/ContextHolder.java b/anypermission/src/main/java/per/goweii/anypermission/ContextHolder.java index 5f5e76f..a48fc5f 100644 --- a/anypermission/src/main/java/per/goweii/anypermission/ContextHolder.java +++ b/anypermission/src/main/java/per/goweii/anypermission/ContextHolder.java @@ -7,8 +7,11 @@ import android.support.v4.app.Fragment; import com.yanzhenjie.permission.AndPermission; +import com.yanzhenjie.permission.checker.StandardChecker; import com.yanzhenjie.permission.option.Option; +import java.lang.reflect.Field; + /** * 描述: * @@ -50,7 +53,8 @@ public class ContextHolder { mFragment = fragment; } - public Option getOption(){ + public Option getOption() { + hookAndPermission(); if (mContext != null) { return AndPermission.with(mContext); } else if (mActivity != null) { @@ -63,7 +67,7 @@ public Option getOption(){ return null; } - public Context getContext(){ + public Context getContext() { if (mContext != null) { return mContext; } else if (mActivity != null) { @@ -79,4 +83,22 @@ public Context getContext(){ } return null; } + + private void hookAndPermission() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + hookAndPermissionDoubleChecker(); + } + } + + private void hookAndPermissionDoubleChecker() { + try { + Class andPermission = Class.forName("com.yanzhenjie.permission.checker.DoubleChecker"); + Field strictChecker = andPermission.getDeclaredField("STRICT_CHECKER"); + strictChecker.setAccessible(true); + Object fieldObj = strictChecker.get(null); + strictChecker.set(fieldObj, new StandardChecker()); + } catch (Exception e) { + e.printStackTrace(); + } + } } diff --git a/anypermission/src/main/java/per/goweii/anypermission/RuntimeRequester.java b/anypermission/src/main/java/per/goweii/anypermission/RuntimeRequester.java index 3160205..23c071f 100644 --- a/anypermission/src/main/java/per/goweii/anypermission/RuntimeRequester.java +++ b/anypermission/src/main/java/per/goweii/anypermission/RuntimeRequester.java @@ -62,37 +62,17 @@ public RuntimeRequester onGoSetting(RequestInterceptor onGoSetting) { public RuntimeRequester request(@NonNull RequestListener listener) { mListener = listener; findUnGrantedPermissions(); - if (mUnGrantedPermissions.peek() == null) { - onSuccess(); - return this; - } - onBeforeRequest(); + next(); return this; } public void onActivityResult(int requestCode) { if (requestCode == mRequestCode) { - if (mUnGrantedPermissions.peek() == null) { - findUnGrantedPermissions(); - } - if (mUnGrantedPermissions.peek() == null) { - onSuccess(); - return; - } if (AndPermission.hasPermissions(mContext, mUnGrantedPermissions.peek())) { mUnGrantedPermissions.poll(); - findUnGrantedPermissions(); - if (mUnGrantedPermissions.peek() == null) { - onSuccess(); - return; - } - onBeforeRequest(); + next(); } else { - if (AndPermission.hasAlwaysDeniedPermission(mContext, mUnGrantedPermissions.peek())) { - onGoSetting(); - } else { - onBeenDenied(); - } + again(); } } } @@ -102,12 +82,12 @@ private void findUnGrantedPermissions() { mUnGrantedPermissions = new LinkedList<>(); } for (String permission : mAllNeededPermissions) { - if (!AndPermission.hasPermissions(mContext, permission)) { + if (AndPermission.hasPermissions(mContext, permission)) { + mUnGrantedPermissions.remove(permission); + } else { if (!mUnGrantedPermissions.contains(permission)) { mUnGrantedPermissions.offer(permission); } - } else { - mUnGrantedPermissions.remove(permission); } } } @@ -195,13 +175,22 @@ public void onAction(List data) { .onDenied(new Action>() { @Override public void onAction(final List data) { - next(); + again(); } }) .start(); } private void next() { + // findUnGrantedPermissions(); + if (mUnGrantedPermissions.peek() == null) { + onSuccess(); + return; + } + onBeforeRequest(); + } + + private void again() { if (mUnGrantedPermissions.peek() == null) { onSuccess(); return;