Class DynamicPermissions
java.lang.Object
com.pranavpandey.android.dynamic.support.permission.DynamicPermissions
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 Summary
Modifier and TypeMethodDescriptionboolean
Checks whether the package has overlay permission.boolean
Checks whether the activity can be launched from the background.boolean
Checks whether the package can write system settings.Returns the context used by this instance.static DynamicPermissions
Retrieves the singleton instance ofDynamicPermissions
.getPermissionItemArrayList
(String[] permissions) Converts the array of permissions to the array list ofDynamicPermissions
.Class<?>
Get the permission activity used by this manager.boolean
Checks whether the package has usage access permission.static void
initializeInstance
(Context context) Initialize permissions when application starts.boolean
isAccessibilityService
(Class<?> clazz) Checks whether the accessibility service is enabled for the supplied class.boolean
Checks whether the supplied permissions have been granted.boolean
isGranted
(Context context, String[] permissions, boolean request, Intent actionIntent, int action, int requestCode) Checks whether the supplied permissions have been granted.boolean
Checks whether the supplied permissions have been granted.boolean
isGranted
(Fragment fragment, String[] permissions, boolean request, Intent actionIntent, int action, int requestCode) Checks whether the supplied permissions have been granted.boolean
Checks whether the supplied permissions have been granted.boolean
Checks whether the supplied permissions have been granted.boolean
isIgnoringBatteryOptimizations
(boolean settings) Checks whether the battery optimizations are ignored for the package.void
requestPermissions
(Context context, String[] permissions, boolean history, Intent actionIntent, int action) Request the supplied permissions if not granted.void
requestPermissions
(Context context, String[] permissions, boolean history, Intent actionIntent, int action, int requestCode) Request the supplied permissions if not granted.void
requestPermissions
(Fragment fragment, String[] permissions, boolean history, Intent actionIntent, int action) Request the supplied permissions if not granted.void
requestPermissions
(Fragment fragment, String[] permissions, boolean history, Intent actionIntent, int action, int requestCode) Request the supplied permissions if not granted.void
requestPermissions
(String[] permissions, boolean history, Intent actionIntent, int action) Request the supplied permissions if not granted.requestPermissionsIntent
(Context context, String[] permissions, boolean history, Intent actionIntent, int action) Request the permissions activity intent for the supplied permissions.void
setContext
(Context context) Sets the context used by this instance.void
setPermissionsActivity
(Class<?> permissionsActivity) Sets the permission activity for this instance.
-
Method Details
-
initializeInstance
Initialize permissions when application starts.Must be initialized once.
- Parameters:
context
- The context to request and manage permissions.
-
getContext
Returns the context used by this instance.- Returns:
- The context used by this instance.
-
setContext
Sets the context used by this instance.- Parameters:
context
- The context to be set.
-
getInstance
Retrieves the singleton instance ofDynamicPermissions
.Must be called before accessing the public methods.
- Returns:
- The singleton instance of
DynamicPermissions
.
-
getPermissionsActivity
Get the permission activity used by this manager.- Returns:
- The permission activity used by this manager.
-
setPermissionsActivity
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
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
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
Converts the array of permissions to the array list ofDynamicPermissions
.- Parameters:
permissions
- The permissions array to be converted.- Returns:
- The array list containing
DynamicPermissions
.
-