Grant Android Permission Library

repository·master·Indexed 19 days ago

https://github.com/anthonycr/grant

A library designed to simplify the Android permission model by abstracting the complexity of checking, requesting, and handling permissions. It utilizes a callback-based approach via PermissionsManager and PermissionsResultAction to manage permission logic outside of onRequestPermissionsResult.

Tokens
1.2K
Snippets
5
Records
6
Agent score
18%

What's inside Grant

  1. How Grant works: PermissionsManager and PermissionsResultAction

    master

    Grant simplifies Android permission handling by removing the need to manage complex logic inside onRequestPermissionsResult. It uses two primary components:

    1. PermissionsManager: A singleton class that manages all permission requests, logic, and callbacks.
    2. PermissionsResultAction: A callback interface used to define what code should execute when permissions are either granted or denied.

    Instead of manually checking permissions and handling request codes, you wrap your sensitive logic in a PermissionsResultAction and pass it to the PermissionsManager. The manager handles the checking, requesting, and callback orchestration.

  2. Initialize Grant in your Activities

    master

    To allow PermissionsManager to receive permission changes, you must notify it within your Activity's onRequestPermissionsResult callback. For API 23 and above, override onRequestPermissionsResult in every Activity from which you make permission requests and add the following line:

    @Override
    public void onRequestPermissionsResult(int requestCode, 
                                           @NonNull String[] permissions, 
                                           @NonNull int[] grantResults) {
        PermissionsManager.getInstance().notifyPermissionsChange(permissions, grantResults);
    }
  3. Request all permissions declared in Manifest

    master

    If you need to request all permissions declared in your AndroidManifest.xml at once (e.g., during app initialization), use requestAllManifestPermissionsIfNecessary.

    Note: Even when using this, you should still perform manual permission checks before executing sensitive code, as users can revoke permissions in device settings while the app is running.

    PermissionsManager.getInstance().requestAllManifestPermissionsIfNecessary(this, 
                                                          new PermissionsResultAction() {
        @Override
        public void onGranted() {
          // Proceed with initialization
        }
    
        @Override
        public void onDenied(String permission) {
          // Notify the user that you need all of the permissions
        }
    });
  4. Request specific permissions for a task

    master

    Use requestPermissionsIfNecessaryForResult to execute code only after specific permissions are granted. This method checks if the permissions are already held; if not, it requests them and triggers the PermissionsResultAction callback.

    • onGranted(): Triggered when all requested permissions are granted.
    • onDenied(String permission): Triggered if any requested permission is denied. The action is then removed from the queue.
    PermissionsManager.getInstance().requestPermissionsIfNecessaryForResult(this,
        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, new PermissionsResultAction() {
    
        @Override
        public void onGranted() {
            writeToStorage();
        }
    
        @Override
        public void onDenied(String permission) {
            Toast.makeText(MainActivity.this, 
                          "Sorry, we need the Storage Permission to do that", 
                          Toast.LENGTH_SHORT).show();
        }
    });
  5. Check if all permissions are granted

    master

    To quickly check if a set of permissions is currently held by the app without triggering a request dialog, use hasAllPermissions. This is useful for updating UI elements based on current permission states.

    boolean hasAll = PermissionsManager.getInstance().hasAllPermissions(context, new String[]{...});