Skip to content

Instantly share code, notes, and snippets.

@resengupta
Last active December 19, 2017 05:33
Show Gist options
  • Select an option

  • Save resengupta/c9dbb74bd5e4f19b0789ff1279ba2a3c to your computer and use it in GitHub Desktop.

Select an option

Save resengupta/c9dbb74bd5e4f19b0789ff1279ba2a3c to your computer and use it in GitHub Desktop.

Revisions

  1. resengupta revised this gist Dec 19, 2017. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion PermissionUtils
    Original file line number Diff line number Diff line change
    @@ -87,7 +87,6 @@ public final class PermissionUtils {
    /**
    * This class contains Permission Groups for each Permission type.
    */
    @SuppressWarnings("squid:S2386")
    public static final class Permissions {

    public static final String[] CAMERA = {Manifest.permission.CAMERA};
  2. resengupta created this gist Dec 19, 2017.
    114 changes: 114 additions & 0 deletions PermissionUtils
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,114 @@
    /**
    * 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.
    */
    @SuppressWarnings("squid:S2386")
    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
    }
    }


    }