Class DynamicPermissions

java.lang.Object
com.pranavpandey.android.dynamic.support.permission.DynamicPermissions

public class DynamicPermissions extends Object
Helper class to request and manage runtime permissions introduced in API 23.

It must be initialized before using any of its functions or requesting any permissions.

Register the DynamicPermissionsActivity via setPermissionsActivity(Class) to request the permissions via this manager.

See Also:
  • Method Details

    • initializeInstance

      public static void initializeInstance(@Nullable Context context)
      Initialize permissions when application starts.

      Must be initialized once.

      Parameters:
      context - The context to request and manage permissions.
    • getContext

      @NonNull public Context getContext()
      Returns the context used by this instance.
      Returns:
      The context used by this instance.
    • setContext

      public void setContext(@NonNull Context context)
      Sets the context used by this instance.
      Parameters:
      context - The context to be set.
    • getInstance

      @NonNull public static DynamicPermissions getInstance()
      Retrieves the singleton instance of DynamicPermissions.

      Must be called before accessing the public methods.

      Returns:
      The singleton instance of DynamicPermissions.
    • getPermissionsActivity

      @NonNull public Class<?> getPermissionsActivity()
      Get the permission activity used by this manager.
      Returns:
      The permission activity used by this manager.
    • setPermissionsActivity

      public void setPermissionsActivity(@NonNull Class<?> permissionsActivity)
      Sets the permission activity for this instance.
      Parameters:
      permissionsActivity - The permission activity class to be set.
    • requestPermissions

      public void requestPermissions(@NonNull Context context, @NonNull String[] permissions, boolean history, @Nullable Intent actionIntent, int action, int requestCode)
      Request the supplied permissions if not granted.
      Parameters:
      context - The context to start the activity.
      permissions - The array of permissions to be requested.
      history - false to exclude the system settings activity from the recents.
      actionIntent - The intent which should be called after all the permissions have been granted.
      action - The intent action, either start an activity or a service.
      requestCode - The request code for the result.
    • requestPermissionsIntent

      @Nullable public Intent requestPermissionsIntent(@NonNull Context context, @NonNull String[] permissions, boolean history, @Nullable Intent actionIntent, int action)
      Request the permissions activity intent for the supplied permissions.
      Parameters:
      context - The context to build the intent.
      permissions - The array of permissions to be requested.
      history - false to exclude the system settings activity from the recents.
      actionIntent - The intent which should be called after all the permissions have been granted.
      action - The intent action, either start an activity or a service.
      Returns:
      The permissions activity intent for the supplied permissions.
    • requestPermissions

      public void requestPermissions(@NonNull Fragment fragment, @NonNull String[] permissions, boolean history, @Nullable Intent actionIntent, int action, int requestCode)
      Request the supplied permissions if not granted.
      Parameters:
      fragment - The fragment to start the activity.
      permissions - The array of permissions to be requested.
      history - false to exclude the system settings activity from the recents.
      actionIntent - The intent which should be called after all the permissions have been granted.
      action - The intent action, either start an activity or a service.
      requestCode - The request code for the result.
    • requestPermissions

      public void requestPermissions(@NonNull Context context, @NonNull String[] permissions, boolean history, @Nullable Intent actionIntent, int action)
      Request the supplied permissions if not granted.
      Parameters:
      context - The context to start the activity.
      permissions - The array of permissions to be requested.
      history - false to exclude the system settings activity from the recents.
      actionIntent - The intent which should be called after all the permissions have been granted.
      action - The intent action, either start an activity or a service.
    • requestPermissions

      public void requestPermissions(@NonNull Fragment fragment, @NonNull String[] permissions, boolean history, @Nullable Intent actionIntent, int action)
      Request the supplied permissions if not granted.
      Parameters:
      fragment - The fragment to start the activity.
      permissions - The array of permissions to be requested.
      history - false to exclude the system settings activity from the recents.
      actionIntent - The intent which should be called after all the permissions have been granted.
      action - The intent action, either start an activity or a service.
    • requestPermissions

      public void requestPermissions(@NonNull String[] permissions, boolean history, @Nullable Intent actionIntent, int action)
      Request the supplied permissions if not granted.
      Parameters:
      permissions - The array of permissions to be requested.
      history - false to exclude the system settings activity from the recents.
      actionIntent - The intent which should be called after all the permissions have been granted.
      action - The intent action, either start an activity or a service.
    • isGranted

      public boolean isGranted(@NonNull Context context, @NonNull String[] permissions, boolean request, @Nullable Intent actionIntent, int action, int requestCode)
      Checks whether the supplied permissions have been granted. It can also be used to automatically request the permissions those are denied or not requested yet by using the permission activity.
      Parameters:
      context - The context to start the activity.
      permissions - The array of permissions to be requested.
      request - true to automatically request the permissions if not granted.
      actionIntent - The intent which should be called after all the permissions have been granted.
      action - The intent action, either start an activity or a service.
      requestCode - The request code for the result.
      Returns:
      true if all the supplied permissions have been granted.
    • isGranted

      public boolean isGranted(@NonNull Fragment fragment, @NonNull String[] permissions, boolean request, @Nullable Intent actionIntent, int action, int requestCode)
      Checks whether the supplied permissions have been granted. It can also be used to automatically request the permissions those are denied or not requested yet by using the permission activity.
      Parameters:
      fragment - The fragment to start the activity.
      permissions - The array of permissions to be requested.
      request - true to automatically request the permissions if not granted.
      actionIntent - The intent which should be called after all the permissions have been granted.
      action - The intent action, either start an activity or a service.
      requestCode - The request code for the result.
      Returns:
      true if all the supplied permissions have been granted.
    • isGranted

      public boolean isGranted(@NonNull String[] permissions, boolean request, @Nullable Intent actionIntent, int action)
      Checks whether the supplied permissions have been granted. It can also be used to automatically request the permissions those are denied or not requested yet by using the permission activity.
      Parameters:
      permissions - The array of permissions to be requested.
      request - true to automatically request the permissions if not granted.
      actionIntent - The intent which should be called after all the permissions have been granted.
      action - The intent action, either start an activity or a service.
      Returns:
      true if all the supplied permissions have been granted.
    • isGranted

      public boolean isGranted(@NonNull Context context, @NonNull String[] permissions, boolean request, int requestCode)
      Checks whether the supplied permissions have been granted. It can also be used to automatically request the permissions those are denied or not requested yet by using the permission activity.
      Parameters:
      context - The context to start the activity.
      permissions - The array of permissions to be requested.
      request - true to automatically request the permissions if not granted.
      requestCode - The request code for the result.
      Returns:
      true if all the supplied permissions have been granted.
    • isGranted

      public boolean isGranted(@NonNull Fragment fragment, @NonNull String[] permissions, boolean request, int requestCode)
      Checks whether the supplied permissions have been granted. It can also be used to automatically request the permissions those are denied or not requested yet by using the permission activity.
      Parameters:
      fragment - The fragment to start the activity.
      permissions - The array of permissions to be requested.
      request - true to automatically request the permissions if not granted.
      requestCode - The request code for the result.
      Returns:
      true if all the supplied permissions have been granted.
    • isGranted

      public boolean isGranted(@NonNull String[] permissions, boolean request)
      Checks whether the supplied permissions have been granted. It can also be used to automatically request the permissions those are denied or not requested yet by using the permission activity.
      Parameters:
      permissions - The array of permissions to be requested.
      request - true to automatically request the permissions if not granted.
      Returns:
      true if all the supplied permissions have been granted.
    • canWriteSystemSettings

      public boolean canWriteSystemSettings()
      Checks whether the package can write system settings.
      Returns:
      true if can write system settings.
      See Also:
    • canDrawOverlays

      public boolean canDrawOverlays()
      Checks whether the package has overlay permission.
      Returns:
      true if can draw overlays.
      See Also:
    • hasUsageAccess

      public boolean hasUsageAccess()
      Checks whether the package has usage access permission.
      Returns:
      true if has usage access.
      See Also:
    • isAccessibilityService

      public boolean isAccessibilityService(@NonNull Class<?> clazz)
      Checks whether the accessibility service is enabled for the supplied class.
      Parameters:
      clazz - The class to be checked.
      Returns:
      true if the accessibility service is enabled for the supplied class.
      See Also:
    • isIgnoringBatteryOptimizations

      public boolean isIgnoringBatteryOptimizations(boolean settings)
      Checks whether the battery optimizations are ignored for the package.
      Parameters:
      settings - true to open the battery optimization settings if the package is not ignored.
      Returns:
      true if the battery optimizations are ignored for the package.
    • canLaunchFromBackground

      public boolean canLaunchFromBackground()
      Checks whether the activity can be launched from the background.
      Returns:
      true if activity can be launched from the background.
    • getPermissionItemArrayList

      @NonNull public List<DynamicPermission> getPermissionItemArrayList(@NonNull String[] permissions)
      Converts the array of permissions to the array list of DynamicPermissions.
      Parameters:
      permissions - The permissions array to be converted.
      Returns:
      The array list containing DynamicPermissions.