diff --git a/lib_base/src/main/java/com/android/base/permission/AndPermissionFragment.java b/lib_base/src/main/java/com/android/base/permission/AndPermissionFragment.java deleted file mode 100644 index 2344e39..0000000 --- a/lib_base/src/main/java/com/android/base/permission/AndPermissionFragment.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.android.base.permission; - -import android.content.Intent; -import android.os.Bundle; -import android.os.Handler; -import android.support.annotation.Nullable; -import android.support.v4.app.Fragment; - -/** - * @author Ztiany - * Email: ztiany3@gmail.com - * Date : 2018-04-16 17:48 - */ -public class AndPermissionFragment extends Fragment { - - static AndPermissionFragment newInstance() { - return new AndPermissionFragment(); - } - - private final Handler mHandler = new Handler(); - private final Runnable mRunnable = this::startChecked; - - private AndPermissionRequester mRequester; - private boolean mIsActivityReady = false; - - @Override - public void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - mIsActivityReady = true; - } - - @Override - public void onResume() { - super.onResume(); - mIsActivityReady = true; - } - - @Override - public void onPause() { - super.onPause(); - mHandler.removeCallbacks(mRunnable); - mIsActivityReady = false; - } - - @Override - public void onActivityResult(int requestCode, int resultCode, Intent data) { - super.onActivityResult(requestCode, resultCode, data); - if (mRequester != null) { - mRequester.onActivityResult(requestCode, resultCode, data); - } - } - - void setRequester(AndPermissionRequester requester) { - mRequester = requester; - } - - void startAsk() { - startChecked(); - } - - @Override - public void onDestroy() { - super.onDestroy(); - mHandler.removeCallbacks(mRunnable); - } - - private void startChecked() { - if (mIsActivityReady) { - if (mRequester != null) { - mRequester.onAutoPermissionFragmentReady(this); - } - mHandler.removeCallbacks(mRunnable); - } else { - mHandler.post(mRunnable); - } - } - -} \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/permission/AndPermissionRequester.java b/lib_base/src/main/java/com/android/base/permission/AndPermissionRequester.java deleted file mode 100644 index c34b529..0000000 --- a/lib_base/src/main/java/com/android/base/permission/AndPermissionRequester.java +++ /dev/null @@ -1,205 +0,0 @@ -package com.android.base.permission; - -import android.content.Intent; -import android.net.Uri; -import android.provider.Settings; -import android.support.annotation.NonNull; -import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentActivity; -import android.support.v4.app.FragmentManager; - -import com.github.dmstocking.optional.java.util.function.Consumer; -import com.yanzhenjie.permission.AndPermission; -import com.yanzhenjie.permission.runtime.PermissionRequest; - -import java.util.List; - - -/** - *
- *      1: 使用该类申请权限,当所有的权限都通过时回调权限获取成功,否则回调权限获取失败。
- *      2:不要同时调用requestPermission方法多次!!!以保证一个完整的流程。
- * 获取权限流程,以申请相机权限为例:
- *          1先检查自身是否有相机权限
- *          2如果有我们的app已经有了相机权限,则可以直接使用相机相关功能了
- *          3如果没有权限我们就需要请求权限了,但是还需要处理不再询问的设置
- *              3.1如果shouldShowRequestPermissionRationale返回false,则说明接下来的对话框不包含”不再询问“选择框,我们可以直接申请权限
- *              3.2如果shouldShowRequestPermissionRationale返回true,我们最好先弹出一个对话框来说明我们需要权限来做什么,让用户来选择是否继续授予权限,如果用户允许继续授予权限则继续申请权限
- *          4不管权限是否授予给我们的App,都可以在onRequestPermissionsResult的回调中获取结果,我们再问一次
- * 
- * - * @author Ztiany - * Email: 1169654504@qq.com - * Date : 2017-01-11 15:09 - */ -public class AndPermissionRequester { - - private final FragmentActivity mActivity; - private boolean mAskAgain = true; - private boolean mShowReason = true; - private boolean mShowTips = false; - private IPermissionUIProvider mPermissionUIProvider; - private Consumer> mOnGranted; - private Consumer> mOnDenied; - private static final int REQUEST_PERMISSION_FOR_SETTING = 999; - private List mDeniedPermission; - private String[] mPerms; - - private AndPermissionRequester(FragmentActivity activity) { - if (activity == null) { - throw new NullPointerException("activity is null"); - } - mActivity = activity; - } - - public static AndPermissionRequester with(Fragment fragment) { - return new AndPermissionRequester(fragment.getActivity()); - } - - public static AndPermissionRequester with(FragmentActivity activity) { - return new AndPermissionRequester(activity); - } - - public AndPermissionRequester permission(String... permissions) { - if (permissions == null || permissions.length == 0) { - throw new IllegalArgumentException(); - } - mPerms = permissions; - return this; - } - - public AndPermissionRequester askAgain(boolean askAgain) { - mAskAgain = askAgain; - return this; - } - - public AndPermissionRequester showReason(boolean showReason) { - mShowReason = showReason; - return this; - } - - public AndPermissionRequester showDeniedTips(boolean showTips) { - mShowTips = showTips; - return this; - } - - public AndPermissionRequester customUI(@NonNull IPermissionUIProvider uiProvider) { - mPermissionUIProvider = uiProvider; - return this; - } - - public AndPermissionRequester onGranted(@NonNull Consumer> onGranted) { - this.mOnGranted = onGranted; - return this; - } - - public AndPermissionRequester onDenied(@NonNull Consumer> onDenied) { - this.mOnDenied = onDenied; - return this; - } - - public void request() { - if (mPerms != null) { - doPermissionRequest(); - } else { - throw new IllegalStateException("no permission set"); - } - } - - private void doPermissionRequest() { - PermissionRequest permissionRequest = AndPermission.with(mActivity).runtime().permission(mPerms); - - if (mShowReason) { - permissionRequest = permissionRequest.rationale((context, data, executor) -> - getPermissionUIProvider().showPermissionRationaleDialog( - mActivity, - data.toArray(new String[0]), - (dialog, which) -> executor.execute(), - (dialog, which) -> executor.cancel())); - } - - permissionRequest - .onGranted(data -> { - if (mOnGranted != null) { - mOnGranted.accept(data); - } - }) - .onDenied(permissions -> { - if (mAskAgain) { - doAskAgain(permissions); - } else { - if (mOnDenied != null) { - mOnDenied.accept(permissions); - } - } - }) - .start(); - } - - /** - * 询问是否去设置界面 - */ - private void doAskAgain(List permissions) { - getPermissionUIProvider().showAskAgainDialog(mActivity, permissions.toArray(new String[0]), - (dialog, which) -> openSettings(permissions),/*去设置界面*/ - (dialog, which) -> { - if (mOnDenied != null) { - mOnDenied.accept(permissions);/*通知权限被拒绝*/ - } - }); - } - - private void openSettings(List permissions) { - FragmentManager supportFragmentManager = mActivity.getSupportFragmentManager(); - AndPermissionFragment fragment = (AndPermissionFragment) supportFragmentManager.findFragmentByTag(AndPermissionFragment.class.getName()); - if (fragment == null) { - fragment = AndPermissionFragment.newInstance(); - fragment.setRequester(this); - supportFragmentManager - .beginTransaction() - .add(fragment, AndPermissionFragment.class.getName()) - .commitNowAllowingStateLoss(); - } else { - fragment.setRequester(this); - } - - mDeniedPermission = permissions; - fragment.startAsk(); - } - - void onAutoPermissionFragmentReady(AndPermissionFragment autoPermissionFragment) { - Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); - Uri uri = Uri.fromParts("package", mActivity.getPackageName(), null); - intent.setData(uri); - try { - autoPermissionFragment.startActivityForResult(intent, REQUEST_PERMISSION_FOR_SETTING, null); - } catch (Exception ignore) { - } - } - - @SuppressWarnings("unused") - void onActivityResult(int requestCode, int resultCode, Intent data) { - if (requestCode == REQUEST_PERMISSION_FOR_SETTING) {//申请权限 - if (!AndUtils.hasPermission(mActivity, mPerms)) {//Setting界面回来之后,没有授予权限 - if (mOnDenied != null) { - mOnDenied.accept(mDeniedPermission); - } - if (mShowTips) { - getPermissionUIProvider().showPermissionDeniedTip(mActivity, mDeniedPermission.toArray(new String[0])); - } - } else { - if (mOnGranted != null) { - mOnGranted.accept(mDeniedPermission);//所有权限被获取 - } - } - } - } - - private IPermissionUIProvider getPermissionUIProvider() { - if (mPermissionUIProvider == null) { - mPermissionUIProvider = PermissionUIProviderFactory.getPermissionUIProvider(); - } - return mPermissionUIProvider; - } - -} diff --git a/lib_base/src/main/java/com/android/base/permission/AndUtils.java b/lib_base/src/main/java/com/android/base/permission/AndUtils.java deleted file mode 100644 index f69666a..0000000 --- a/lib_base/src/main/java/com/android/base/permission/AndUtils.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.android.base.permission; - -import android.content.Context; -import android.support.annotation.NonNull; - -import com.yanzhenjie.permission.checker.DoubleChecker; - -/** - * @author Ztiany - * Date : 2018-09-07 11:38 - */ -class AndUtils { - - /** - * direct check permissions - */ - static boolean hasPermission(@NonNull Context context, @NonNull String... permissions) { - return new DoubleChecker().hasPermission(context, permissions); - } - -} diff --git a/lib_base/src/main/java/com/android/base/permission/AutoPermissionRequester.java b/lib_base/src/main/java/com/android/base/permission/AutoPermissionRequester.java index 90f54a2..126529e 100644 --- a/lib_base/src/main/java/com/android/base/permission/AutoPermissionRequester.java +++ b/lib_base/src/main/java/com/android/base/permission/AutoPermissionRequester.java @@ -10,6 +10,8 @@ import android.support.v4.app.FragmentManager; import com.android.base.utils.android.ActFragWrapper; +import java.util.Arrays; + import timber.log.Timber; import static com.android.base.permission.PermissionCode.PERMISSION_REQUESTER_CODE; @@ -36,6 +38,7 @@ public class AutoPermissionRequester { private final FragmentActivity mActivity; private String[] mPerms; private boolean mAskAgain = true; + private boolean mShowTips = false; private OnAllPermissionGrantedListener mOnAllPermissionGrantedListener; private OnPermissionDeniedListener mOnPermissionDeniedListener; @@ -79,6 +82,11 @@ public class AutoPermissionRequester { return this; } + public AutoPermissionRequester showTips(boolean showTips) { + mShowTips = showTips; + return this; + } + public AutoPermissionRequester askAgain(boolean askAgain) { mAskAgain = askAgain; return this; @@ -111,7 +119,7 @@ public class AutoPermissionRequester { if (fragment == null) { fragment = AutoPermissionFragment.newInstance(); - mPermissionRequester = new PermissionRequester(ActFragWrapper.create(fragment), mPermissionCallback, mAskAgain, mPermissionUIProvider); + mPermissionRequester = new PermissionRequester(ActFragWrapper.create(fragment), mPermissionCallback, mAskAgain, mShowTips, mPermissionUIProvider); fragment.setRequester(getCallback()); supportFragmentManager.beginTransaction() @@ -120,7 +128,7 @@ public class AutoPermissionRequester { } else { fragment.setRequester(getCallback()); - mPermissionRequester = new PermissionRequester(ActFragWrapper.create(fragment), mPermissionCallback, mAskAgain, mPermissionUIProvider); + mPermissionRequester = new PermissionRequester(ActFragWrapper.create(fragment), mPermissionCallback, mAskAgain, mShowTips, mPermissionUIProvider); } fragment.startRequest(); @@ -147,7 +155,7 @@ public class AutoPermissionRequester { @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { - Timber.d("onRequestPermissionsResult() called with: requestCode = [" + requestCode + "], permissions = [" + permissions + "], grantResults = [" + grantResults + "]"); + Timber.d("onRequestPermissionsResult() called with: requestCode = [" + requestCode + "], permissions = [" + Arrays.toString(permissions) + "], grantResults = [" + Arrays.toString(grantResults) + "]"); if (requestCode == PERMISSION_REQUESTER_CODE && mPermissionRequester != null) { mPermissionRequester.onRequestPermissionsResult(requestCode, permissions, grantResults); } diff --git a/lib_base/src/main/java/com/android/base/permission/PermissionRequester.java b/lib_base/src/main/java/com/android/base/permission/PermissionRequester.java index ce6b49c..82beb45 100644 --- a/lib_base/src/main/java/com/android/base/permission/PermissionRequester.java +++ b/lib_base/src/main/java/com/android/base/permission/PermissionRequester.java @@ -33,18 +33,20 @@ class PermissionRequester { private PermissionCallback mPermissionCallback; private EasyPermissions.PermissionCaller mPermissionCaller; private final boolean mAskAgain; + private final boolean mShowTips; private final IPermissionUIProvider mPermissionUIProvider; - PermissionRequester(ActFragWrapper actFragWrapper, PermissionCallback permissionCallback, boolean askAgain, IPermissionUIProvider permissionUIProvider) { + PermissionRequester(ActFragWrapper actFragWrapper, PermissionCallback permissionCallback, boolean askAgain, boolean showTips, IPermissionUIProvider permissionUIProvider) { mPermissionCallback = permissionCallback; mActFragWrapper = actFragWrapper; mAskAgain = askAgain; + mShowTips = showTips; mPermissionUIProvider = permissionUIProvider; } private EasyPermissions.PermissionCaller getPermissionCaller() { if (mPermissionCaller == null) { - mPermissionCaller = new PermissionRequesterImpl(mPermissionCallback, mActFragWrapper, mAskAgain, mPermissionUIProvider); + mPermissionCaller = new PermissionRequesterImpl(mPermissionCallback, mActFragWrapper, mAskAgain, mShowTips,mPermissionUIProvider); } return mPermissionCaller; } @@ -63,7 +65,11 @@ class PermissionRequester { if (!EasyPermissions.hasPermissions(mActFragWrapper.getContext(), mPerms)) {//Setting界面回来之后,没有授予权限 String[] filter = EasyPermissions.filter(mActFragWrapper.getContext(), mPerms); mPermissionCallback.onPermissionDenied(Arrays.asList(filter));//权限被拒绝 - mPermissionCaller.getPermissionUIProvider().showPermissionDeniedTip(mActFragWrapper.getContext(), filter); + + if (mShowTips) { + mPermissionCaller.getPermissionUIProvider().showPermissionDeniedTip(mActFragWrapper.getContext(), filter); + } + } else { mPermissionCallback.onAllPermissionGranted();//所有权限被获取 } diff --git a/lib_base/src/main/java/com/android/base/permission/PermissionRequesterImpl.java b/lib_base/src/main/java/com/android/base/permission/PermissionRequesterImpl.java index 1507c66..2943619 100644 --- a/lib_base/src/main/java/com/android/base/permission/PermissionRequesterImpl.java +++ b/lib_base/src/main/java/com/android/base/permission/PermissionRequesterImpl.java @@ -14,14 +14,17 @@ import static com.android.base.permission.PermissionCode.REQUEST_PERMISSION_FOR_ class PermissionRequesterImpl implements EasyPermissions.PermissionCaller { private final boolean mShouldAskAgain; + private final boolean mShowTips; + private ActFragWrapper mContextWrapper; private PermissionCallback mPermissionCallback; private IPermissionUIProvider mIPermissionUIProvider; - PermissionRequesterImpl(PermissionCallback permissionCallback, ActFragWrapper contextWrapper, boolean shouldAskAgain, IPermissionUIProvider iPermissionUIProvider) { + PermissionRequesterImpl(PermissionCallback permissionCallback, ActFragWrapper contextWrapper, boolean shouldAskAgain, boolean showTips, IPermissionUIProvider iPermissionUIProvider) { mPermissionCallback = permissionCallback; mContextWrapper = contextWrapper; mShouldAskAgain = shouldAskAgain; + mShowTips = showTips; mIPermissionUIProvider = iPermissionUIProvider; } @@ -80,7 +83,9 @@ class PermissionRequesterImpl implements EasyPermissions.PermissionCaller { private void notifyPermissionDenied(List perms) { Timber.d("notifyPermissionDenied() called with: perms = [" + perms + "]"); mPermissionCallback.onPermissionDenied(perms); - getPermissionUIProvider().showPermissionDeniedTip(mContextWrapper.getContext(), perms.toArray(new String[0])); + if (mShowTips) { + getPermissionUIProvider().showPermissionDeniedTip(mContextWrapper.getContext(), perms.toArray(new String[0])); + } } }