@AfterPermissionGranted(RC_CAMERA_AND_WIFI) privatevoidmethodRequiresTwoPermission(){ String[] perms = {Manifest.permission.CAMERA, Manifest.permission.CHANGE_WIFI_STATE}; if (EasyPermissions.hasPermissions(this, perms)) { // Already have permission, do the thing // ... } else { // Do not have permissions, request them now EasyPermissions.requestPermissions(this, getString(R.string.camera_and_wifi_rationale), RC_CAMERA_AND_WIFI, perms); } }
publicclassEasyPermissions{ /** * Check if the calling context has a set of permissions. * * @param context the calling context. * @param perms one ore more permissions, such as {@code android.Manifest.permission.CAMERA}. * @return true if all permissions are already granted, false if at least one permission * is not yet granted. */ publicstaticbooleanhasPermissions(Context context, String... perms){ for (String perm : perms) { //通过ContextCompat#checkSelfPermission判断 boolean hasPerm = (ContextCompat.checkSelfPermission(context, perm) == PackageManager.PERMISSION_GRANTED); if (!hasPerm) { returnfalse; } }
publicclassEasyPermissions{ publicstaticvoidrequestPermissions(final Object object, String rationale, finalint requestCode, final String... perms){ requestPermissions(object, rationale, android.R.string.ok, android.R.string.cancel, requestCode, perms); } /** * Request a set of permissions, showing rationale if the system requests it. * * @param object Activity or Fragment requesting permissions. Should implement * {@link android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback} * or * {@link android.support.v13.app.FragmentCompat.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 positiveButton custom text for positive button * @param negativeButton custom text for negative button * @param requestCode request code to track this request, must be < 256. * @param perms a set of permissions to be requested. */ publicstaticvoidrequestPermissions(final Object object, String rationale, @StringRes int positiveButton, @StringRes int negativeButton, finalint requestCode, final String... perms){ //判断传入参数是否合适 checkCallingObjectSuitability(object); //拿到PermissionCallbacks对象 final PermissionCallbacks callbacks = (PermissionCallbacks) object;
boolean shouldShowRationale = false; for (String perm : perms) { //是否需要显示理由 shouldShowRationale = shouldShowRationale || shouldShowRequestPermissionRationale(object, perm); } //如果需要的话,显示dialog进行显示 if (shouldShowRationale) { AlertDialog dialog = new AlertDialog.Builder(getActivity(object)) .setMessage(rationale) .setPositiveButton(positiveButton, new DialogInterface.OnClickListener() { @Override publicvoidonClick(DialogInterface dialog, int which){ //用户同意了,进行系统权限申请操作 executePermissionsRequest(object, perms, requestCode); } }) .setNegativeButton(negativeButton, new DialogInterface.OnClickListener() { @Override publicvoidonClick(DialogInterface dialog, int which){ // act as if the permissions were denied //没有同意的话,回调出去 callbacks.onPermissionsDenied(requestCode, Arrays.asList(perms)); } }).create(); dialog.show(); } else { //不需要显示理由,直接进行权限请求操作 executePermissionsRequest(object, perms, requestCode); } } /** * 判断传入的对象合适合法,判断规则是传入的object是不是 Fragment 或者 Activity 类,同时是否实现了 PermissionCallbacks * * @param object */ privatestaticvoidcheckCallingObjectSuitability(Object object){ // Make sure Object is an Activity or Fragment if (!((object instanceof Fragment) || (object instanceof Activity))) { thrownew IllegalArgumentException("Caller must be an Activity or a Fragment."); }
publicclassEasyPermissions{ /** * Handle the result of a permission request, should be called from the calling Activity's * {@link android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback#onRequestPermissionsResult(int, String[], int[])} * method. * <p/> * If any permissions were granted or denied, the Activity will receive the appropriate * callbacks through {@link PermissionCallbacks} and methods annotated with * {@link AfterPermissionGranted} will be run if appropriate. * * @param requestCode requestCode argument to permission result callback. * @param permissions permissions argument to permission result callback. * @param grantResults grantResults argument to permission result callback. * @param object the calling Activity or Fragment. * @throws IllegalArgumentException if the calling Activity does not implement * {@link PermissionCallbacks}. */ publicstaticvoidonRequestPermissionsResult(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<>(); //判断返回的权限数据,如果权限被授予,添加到granted的List中,没有被授予则添加到denied的List中 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); } }
// Report granted permissions, if any. //进行回调 if (!granted.isEmpty()) { // Notify callbacks callbacks.onPermissionsGranted(requestCode, granted); }
// Report denied permissions, if any. //进行回调 if (!denied.isEmpty()) { callbacks.onPermissionsDenied(requestCode, denied); }
// If 100% successful, call annotated methods //如果所有请求的权限都被授予,则调用被注解的方法 if (!granted.isEmpty() && denied.isEmpty()) { runAnnotatedMethods(object, requestCode); } } }
publicclassEasyPermissions{ /** * 通过反射的方式调用被注解了的方法 * * @param object * @param requestCode */ privatestaticvoidrunAnnotatedMethods(Object object, int requestCode){ Class clazz = object.getClass(); for (Method method : clazz.getDeclaredMethods()) { //是否被AfterPermissionGranted注解了的方法 if (method.isAnnotationPresent(AfterPermissionGranted.class)) { // Check for annotated methods with matching request code. AfterPermissionGranted ann = method.getAnnotation(AfterPermissionGranted.class); //requestCode和AfterPermissionGranted注解传入的requestCode相同的话 if (ann.value() == requestCode) { // Method must be void so that we can invoke it //必须是没有参数的方法 if (method.getParameterTypes().length > 0) { thrownew RuntimeException("Cannot execute non-void method " + method.getName()); }
@AfterPermissionGranted(RC_CAMERA_PERM) publicvoidcameraTask(){ if (EasyPermissions.hasPermissions(this, Manifest.permission.CAMERA)) { // Have permission, do the thing! Toast.makeText(this, "TODO: Camera things", Toast.LENGTH_LONG).show(); } else { // Ask for one permission EasyPermissions.requestPermissions(this, getString(R.string.rationale_camera), RC_CAMERA_PERM, Manifest.permission.CAMERA); } }