Skip to content

Instantly share code, notes, and snippets.

@resengupta
Last active December 19, 2017 05:33
Show Gist options
  • Save resengupta/c9dbb74bd5e4f19b0789ff1279ba2a3c to your computer and use it in GitHub Desktop.
Save resengupta/c9dbb74bd5e4f19b0789ff1279ba2a3c to your computer and use it in GitHub Desktop.
PermissionUtils Example
/**
* Helper for accessing features related to Runtime Permissions introduced support library.
*/
public final class PermissionUtils {
private static final String ACCESS_CAMERA_TAG = "AccessCamera";
private PermissionUtils() {
// Hide Constructor
}
/**
* Iterate through all list of permissions and check if all permissions are granted by user.
*
* @param context The Activity Context
* @param permissions The List of Permissions
* @return true if all permissions are granted by user. False otherwise
*/
public static boolean isPermissionGranted(Context context, @NonNull String[] permissions) {
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(context, permission) !=
PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
/**
* Iterate through all list of permissions and check if Rationale permission should be shown
*
* @param context The Activity Context
* @param permissions The List of Permissions
* @return true if all permissions are granted by user. False otherwise
*/
public static boolean shouldShowRequestPermissionRationale(Activity context, @NonNull String[] permissions) {
for (String permission : permissions) {
if (ActivityCompat.shouldShowRequestPermissionRationale(context, permission)) {
return true;
}
}
return false;
}
/**
* Check that all given permissions have been granted by verifying that each entry in the given
* array is of the value {@link PackageManager#PERMISSION_GRANTED}.
**/
public static boolean verifyPermissions(@NonNull int[] grantResults) {
// At least one result must be checked.
if (grantResults.length < 1) {
return false;
}
// Verify that each required permission has been granted, otherwise return false.
for (int result : grantResults) {
if (result != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
/**
* Display Rationale Dialog for requested permission.
*
* @param context The Activity Context
* @param fragmentManager The fragmentManager
* @param requestCode Request code to identify permission type
*/
public static void showRationaleDialog(Context context, FragmentManager fragmentManager, int requestCode) {
String rationalMessage;
String tag;
if (requestCode == RequestCode.ACCESS_CAMERA) {
tag = ACCESS_CAMERA_TAG;
rationalMessage = context.getString(R.string.access_camera_message);
} else {
throw new IllegalArgumentException(
"Permission Request Code not allowed: " + requestCode);
}
final ConfirmationDialogFragment fragment = ConfirmationDialogFragment.newInstance(
rationalMessage, null, RequestCode.ACCESS_CAMERA);
fragment.show(fragmentManager, tag);
}
/**
* This class contains Permission Groups for each Permission type.
*/
public static final class Permissions {
public static final String[] CAMERA = {Manifest.permission.CAMERA};
private Permissions() {
// hide constructor
}
}
/**
* This class contains Request Code constants for all possible runtime permissions used in the
* application.
*/
public static final class RequestCode {
public static final int ACCESS_CAMERA = 101;
private RequestCode() {
// hide constructor
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment