TedPermission

repository·master·Indexed 23 days ago

https://github.com/parksanggwon/tedpermission

A library for simplifying Android runtime permission checks and requests. It handles the standard permission flow and provides customizable dialogs for rationale and denied states. TedPermission supports multiple implementation styles, including normal callback-based API, Kotlin Coroutines, RxJava2, and RxJava3.

Tokens
2.1K
Snippets
4
Records
8
Agent score
33%

What's inside TedPermission

  1. Install TedPermission

    master

    Add the appropriate dependency to your app/build.gradle file based on your preferred programming style. You should choose only one of the following libraries: normal, coroutine, rx2, or rx3.

    Ensure you have google() and mavenCentral() in your repositories block.

    repositories {
      google()
      mavenCentral()
    }
    
    dependencies {
        // Normal
        implementation("io.github.ParkSangGwon:tedpermission-normal:3.4.2")
        
        // Coroutine
        implementation("io.github.ParkSangGwon:tedpermission-coroutine:3.4.2")
    
        // RxJava2
        implementation("io.github.ParkSangGwon:tedpermission-rx2:3.4.2")
        // RxJava3
        implementation("io.github.ParkSangGwon:tedpermission-rx3:3.4.2")
    }
  2. Use TedPermission with Normal (Java) API

    master

    To use the standard callback-based API, implement a PermissionListener to handle the results and use TedPermission.create() to initiate the check.

    PermissionListener provides two callbacks:

    • onPermissionGranted(): Called when all requested permissions are approved.
    • onPermissionDenied(List<String> deniedPermissions): Called when one or more permissions are denied.

    Optional methods:

    • setPermissionListener(PermissionListener)
    • setDeniedMessage(String): Custom message for the denied dialog.
    • setPermissions(String...): The list of permissions to request (e.g., Manifest.permission.CAMERA).
    • check(): Starts the permission check process.
        PermissionListener permissionlistener = new PermissionListener() {
            @Override
            public void onPermissionGranted() {
                Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_SHORT).show();
            }
    
            @Override
            public void onPermissionDenied(List<String> deniedPermissions) {
                Toast.makeText(MainActivity.this, "Permission Denied\n" + deniedPermissions.toString(), Toast.LENGTH_SHORT).show();
            }
    
    
        };
    
        TedPermission.create()
            .setPermissionListener(permissionlistener)
            .setDeniedMessage("If you reject permission,you can not use this service\n\nPlease turn on permissions at [Setting] > [Permission]")
            .setPermissions(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION)
            .check();
  3. Use TedPermission with Kotlin Coroutines

    master

    For Kotlin users, check() returns a TedPermissionResult instance that can be used within a coroutine.

    TedPermissionResult provides:

    • isGranted(): Returns true if all permissions are granted.
    • getDeniedPermissions(): Returns the list of denied permissions.
    • checkGranted(): Boolean: A convenience method to check if permissions are granted.
    val permissionResult =
        TedPermission.create()
            .setPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION)
            .check()
  4. Use TedPermission with RxJava

    master

    For RxJava users, use the request() method instead of check(). This returns an Observable (or similar) that emits a TedPermissionResult instance upon completion.

    TedPermissionResult provides:

    • isGranted(): Returns true if all permissions are granted.
    • getDeniedPermissions(): Returns the list of denied permissions.
        TedPermission.create()
            .setRationaleTitle(R.string.rationale_title)
            .setRationaleMessage(R.string.rationale_message) // "we need permission for read contact and find your location"
            .setPermissions(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION)
            .request()
            .subscribe(tedPermissionResult -> {
              if (tedPermissionResult.isGranted()) {
                Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();
              } else {
                Toast.makeText(this,
                    "Permission Denied\n" + tedPermissionResult.getDeniedPermissions().toString(), Toast.LENGTH_SHORT)
                    .show();
              }
            }, throwable -> {
            });
  5. Customize TedPermission Dialogs and Behavior

    master

    TedPermission allows customization of the rationale and denied dialogs using the following methods:

    Dialog Customization:

    • setGotoSettingButton(boolean): Enables/disables the button to go to Settings (default: true).
    • setRationaleTitle(R.string.xxx or String): Title for the rationale dialog.
    • setRationaleMessage(R.string.xxx or String): Message for the rationale dialog.
    • setRationaleConfirmText(R.string.xxx or String): Text for the rationale confirm button (default: confirm / 확인).
    • setDeniedTitle(R.string.xxx or String): Title for the denied dialog.
    • setDeniedMessage(R.string.xxx or String): Message for the denied dialog.
    • setDeniedCloseButtonText(R.string.xxx or String): Text for the denied close button (default: close / 닫기).
    • setGotoSettingButtonText(R.string.xxx or String): Text for the button that opens Settings (default: setting / 설정).

    Utility Functions:

    • isGranted(String... permissions): Checks if all specified permissions are granted.
    • isDenied(String... permissions): Checks if all specified permissions are denied.
    • getDeniedPermissions(String... permissions): Returns the list of denied permissions.
    • canRequestPermission(Activity activity, String... permissions): Returns true if a system popup can be requested; returns false if the user has selected Never ask again.
  6. Request permissions using Kotlin Coroutines with TedPermission

    master

    Use TedPermission.create().check() to request Android permissions as a suspending function. This replaces the traditional callback-based PermissionListener with a coroutine-friendly approach. The function suspends until the user interacts with the permission dialog and resumes with a TedPermissionResult.

    To check if permissions are granted without handling the denial list, use checkGranted(), which returns a Boolean.

  7. TedPermission.Builder for Coroutines

    master

    The TedPermission.Builder class (accessed via TedPermission.create()) allows you to configure permissions.

    Note: When using the Coroutine implementation, you cannot use setPermissionListener(). Attempting to use it will throw an UnsupportedOperationException. Instead, you must use the check() or checkGranted() suspending functions which handle the result via coroutine resumption.