googlesamples / easypermissions

Simplify Android M system permissions
https://firebaseopensource.com/projects/googlesamples/easypermissions/
Apache License 2.0
9.86k stars 1.46k forks source link

How does this even work #284

Closed phanirithvij closed 5 years ago

phanirithvij commented 5 years ago

In the readme

@AfterPermissionGranted(RC_CAMERA_AND_LOCATION)
private void methodRequiresTwoPermission() {
    String[] perms = {Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION};
    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_location_rationale),
                RC_CAMERA_AND_LOCATION, perms);
    }
}
  1. What is RC_CAMERA_AND_LOCATION?
  2. Why are we checking if the permissions are not granted in an AfterPermissionGranted decorated function?
samtstern commented 5 years ago

@phanirithvij that method works like this:

  1. If you have CAMERA and ACCESS_FINE_LOCATION permission, do some action.
  2. If you don't, request the permissions.
  3. If the user grants the permission request, re-run the same method. Now you should hit the branch (1).

RC_CAMERA_AND_LOCATION is any constant number you want. Very similar to how you specify your own request code in startActivityForResult. It lets us know which method to come back to.