对google的动态权限解决方案进行删减和添加解释
public class EasyPermissions{
private static final String TAG = "EasyPermissions";
public interface PermissionCallbacks extends
ActivityCompat.OnRequestPermissionsResultCallback {
void onPermissionsGranted(int requestCode, List<String> perms);
void onPermissionsDenied(int requestCode, List<String> perms);
}
/**
* (1)
* 检查有没有权限
*
* @param context the calling context.
* @param perms 可以注册一个或者多个权限, such as {@code android.Manifest.permission.CAMERA}.
* @return 只有在全部权限都被允许的情况返回 true, 有一个权限不被允许则返回 false
*/
public static boolean hasPermissions(Context context, String... perms) {
// 如果低于版本M,就返回true,因为已经拥有权限
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return true;
}
for (String perm : perms) {
boolean hasPerm = (ContextCompat.checkSelfPermission(context, perm) ==
PackageManager.PERMISSION_GRANTED);
if (!hasPerm) {
return false;
}
}
return true;
}
/**
* (2)
* 注册权限,并且向用户提示权限的用途。
* 这里默认使用系统提供的文字,作为 "确认Button" 和 "取消Button" 的Text
*
* @param object Activity or Fragment requesting permissions. Should implement
* {@link android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback}
* @param rationale a message explaining why the application needs this set of permissions, will
* be displayed if the user rejects the request the first time.
* @param requestCode request code to track this request, must be < 256.
* @param perms a set of permissions to be requested.
*/
public static void requestPermissions(final Object object, String rationale,
final int requestCode, final String... perms) {
requestPermissions(object, rationale,
android.R.string.ok,
android.R.string.cancel,
requestCode, perms);
}
public static void requestPermissions(final Object object, String rationale,
@StringRes int positiveButton,
@StringRes int negativeButton,
final int requestCode, final String... perms) {
checkCallingObjectSuitability(object);
final PermissionCallbacks callbacks = (PermissionCallbacks) object;
boolean shouldShowRationale = false;
for (String perm : perms) {
shouldShowRationale =
shouldShowRationale || shouldShowRequestPermissionRationale(object, perm);
}
//如果用户之前拒绝过此权限,但是没有选择不再询问,再一次弹出自定义的提示框,
if (shouldShowRationale) {
Activity activity = getActivity(object);
if (null == activity) {
return;
}
AlertDialog dialog = new AlertDialog.Builder(activity)
.setMessage(rationale)
.setPositiveButton(positiveButton, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//用户同意
executePermissionsRequest(object, perms, requestCode);
}
})
.setNegativeButton(negativeButton, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 用户拒绝,调用失败的回调方法
callbacks.onPermissionsDenied(requestCode, Arrays.asList(perms));
}
}).create();
dialog.show();
} else {//由于用户已经选择了不再询问,所以要直接注册,弹出系统框
executePermissionsRequest(object, perms, requestCode);
}
}
/**
* 用户,对选择权限的弹框的操作,回调
**/
public static void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults, Object object) {
checkCallingObjectSuitability(object);
PermissionCallbacks callbacks = (PermissionCallbacks) object;
// Make a collection of granted and denied permissions from the request.
ArrayList<String> granted = new ArrayList<>();
ArrayList<String> denied = new ArrayList<>();
for (int i = 0; i < permissions.length; i++) {
String perm = permissions[i];
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
granted.add(perm);
} else {
denied.add(perm);
}
}
//下面三个判断是为了区分哪些权限已经获得,哪些权限获取失败,都获取成功了
// 这里面都是已经获取成功的权限
if (!granted.isEmpty()) {
callbacks.onPermissionsGranted(requestCode, granted);
}
// 这里面都是获取失败的权限
if (!denied.isEmpty()) {
callbacks.onPermissionsDenied(requestCode, denied);
}
// 都获取成功了
if (!granted.isEmpty() && denied.isEmpty()) {
}
}
/**
用户拒绝并且选择不再询问时,可调用
* If user denied permissions with the flag NEVER ASK AGAIN, open a dialog explaining the
* permissions rationale again and directing the user to the app settings.
* <p/>
* NOTE: use of this method is optional, should be called from
* {@link PermissionCallbacks#onPermissionsDenied(int, List)}
*
* @param object the calling Activity or Fragment.
* @param deniedPerms the set of denied permissions.
* @return {@code true} if user denied at least one permission with the flag NEVER ASK AGAIN.
*/
public static boolean checkDeniedPermissionsNeverAskAgain(Object object, String rationale,
@StringRes int positiveButton,
@StringRes int negativeButton,
List<String> deniedPerms) {
boolean shouldShowRationale;
for (String perm : deniedPerms) {
shouldShowRationale = shouldShowRequestPermissionRationale(object, perm);
if (!shouldShowRationale) {
final Activity activity = getActivity(object);
if (null == activity) {
return true;
}
AlertDialog dialog = new AlertDialog.Builder(activity)
.setMessage(rationale)
.setPositiveButton(positiveButton, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
intent.setData(uri);
activity.startActivity(intent);
}
})
.setNegativeButton(negativeButton, null)
.create();
dialog.show();
return true;
}
}
return false;
}
/**
* @return true 因为我们之前就注册这些权限,但是用户选择了拒绝,这时候就返回true;
* 用户拒绝并且选择不再询问,返回false
**/
@TargetApi(23)
private static boolean shouldShowRequestPermissionRationale(Object object, String perm) {
if (object instanceof Activity) {
return ActivityCompat.shouldShowRequestPermissionRationale((Activity) object, perm);
} else if (object instanceof Fragment) {
return ((Fragment) object).shouldShowRequestPermissionRationale(perm);
} else if (object instanceof android.app.Fragment) {
return ((android.app.Fragment) object).shouldShowRequestPermissionRationale(perm);
} else {
return false;
}
}
/**
* 直接注册
**/
@TargetApi(23)
private static void executePermissionsRequest(Object object, String[] perms, int requestCode) {
checkCallingObjectSuitability(object);
if (object instanceof Activity) {
ActivityCompat.requestPermissions((Activity) object, perms, requestCode);
} else if (object instanceof Fragment) {
((Fragment) object).requestPermissions(perms, requestCode);
} else if (object instanceof android.app.Fragment) {
((android.app.Fragment) object).requestPermissions(perms, requestCode);
}
}
/***
* @return activity
***/
@TargetApi(11)
private static Activity getActivity(Object object) {
if (object instanceof Activity) {
return ((Activity) object);
} else if (object instanceof Fragment) {
return ((Fragment) object).getActivity();
} else if (object instanceof android.app.Fragment) {
return ((android.app.Fragment) object).getActivity();
} else {
return null;
}
}
/**
* @param object 对该参数进行检查,
* 必须是Fragment和Activity,
* 并且implements接口PermissionCallbacks
**/
private static void checkCallingObjectSuitability(Object object) {
// Make sure Object is an Activity or Fragment
boolean isActivity = object instanceof Activity;
boolean isSupportFragment = object instanceof Fragment;
boolean isAppFragment = object instanceof android.app.Fragment;
boolean isMinSdkM = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
if (!(isSupportFragment || isActivity || (isAppFragment && isMinSdkM))) {
if (isAppFragment) {
throw new IllegalArgumentException(
"Target SDK needs to be greater than 23 if caller is android.app.Fragment");
} else {
throw new IllegalArgumentException("Caller must be an Activity or a Fragment.");
}
}
// Make sure Object implements callbacks
if (!(object instanceof PermissionCallbacks)) {
throw new IllegalArgumentException("Caller must implement PermissionCallbacks.");
}
}
}
网友评论