androidx
Zhanty 5 years ago
parent fd3961280e
commit 5a9941fd13
  1. 78
      lib_base/src/main/java/com/android/base/permission/AndPermissionFragment.java
  2. 205
      lib_base/src/main/java/com/android/base/permission/AndPermissionRequester.java
  3. 21
      lib_base/src/main/java/com/android/base/permission/AndUtils.java
  4. 14
      lib_base/src/main/java/com/android/base/permission/AutoPermissionRequester.java
  5. 10
      lib_base/src/main/java/com/android/base/permission/PermissionRequester.java
  6. 7
      lib_base/src/main/java/com/android/base/permission/PermissionRequesterImpl.java

@ -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);
}
}
}

@ -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;
/**
* <pre>
* 1: 使用该类申请权限当所有的权限都通过时回调权限获取成功否则回调权限获取失败
* 2:不要同时调用requestPermission方法多次以保证一个完整的流程
* 获取权限流程,以申请相机权限为例
* 1先检查自身是否有相机权限
* 2如果有我们的app已经有了相机权限则可以直接使用相机相关功能了
* 3如果没有权限我们就需要请求权限了但是还需要处理不再询问的设置
* &nbsp; 3.1如果shouldShowRequestPermissionRationale返回false则说明接下来的对话框不包含不再询问选择框我们可以直接申请权限
* &nbsp; 3.2如果shouldShowRequestPermissionRationale返回true我们最好先弹出一个对话框来说明我们需要权限来做什么让用户来选择是否继续授予权限如果用户允许继续授予权限则继续申请权限
* 4不管权限是否授予给我们的App都可以在onRequestPermissionsResult的回调中获取结果我们再问一次
* </pre>
*
* @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<List<String>> mOnGranted;
private Consumer<List<String>> mOnDenied;
private static final int REQUEST_PERMISSION_FOR_SETTING = 999;
private List<String> 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<List<String>> onGranted) {
this.mOnGranted = onGranted;
return this;
}
public AndPermissionRequester onDenied(@NonNull Consumer<List<String>> 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<String> 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<String> 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;
}
}

@ -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);
}
}

@ -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);
}

@ -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));//权限被拒绝
if (mShowTips) {
mPermissionCaller.getPermissionUIProvider().showPermissionDeniedTip(mActFragWrapper.getContext(), filter);
}
} else {
mPermissionCallback.onAllPermissionGranted();//所有权限被获取
}

@ -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<String> perms) {
Timber.d("notifyPermissionDenied() called with: perms = [" + perms + "]");
mPermissionCallback.onPermissionDenied(perms);
if (mShowTips) {
getPermissionUIProvider().showPermissionDeniedTip(mContextWrapper.getContext(), perms.toArray(new String[0]));
}
}
}

Loading…
Cancel
Save