Skip to content

Instantly share code, notes, and snippets.

@Neferetheka
Created October 7, 2016 13:32
Show Gist options
  • Save Neferetheka/026adf73af3f42ffb1fd342d6e382837 to your computer and use it in GitHub Desktop.
Save Neferetheka/026adf73af3f42ffb1fd342d6e382837 to your computer and use it in GitHub Desktop.
/**
* Compat permissions related methods.
*/
public final class PermissionUtils {
/**
* Private constructor - avoid class instantiation.
*/
private PermissionUtils() {
throw new UnsupportedOperationException();
}
/**
* Asks for the provided permissions. When necessary, informs the user with a message.
*
* @param activity the calling activity.
* @param request a request code.
* @param error the message resource identifier.
* @param permissions the checked permissions.
* @return true when the permission request started, false otherwise.
*/
public static boolean askForMPermissions(BaseActivity activity, int request, @StringRes int error,
String... permissions) {
if (activity.isMarshmallow()) {
if ((error > 0) && activity.shouldShowRequestPermissionRationale(permissions[0])) {
UiUtils.toast(activity, activity.getString(error));
}
ActivityCompat.requestPermissions(activity, permissions, request);
return true;
}
return false;
}
/**
* Checks whether the provided permissions were granted or not.
*
* @param activity the calling activity.
* @param permissions the checked permissions.
* @return true when all the permissions were granted, false otherwise.
*/
public static boolean hasPermission(Activity activity, String... permissions) {
boolean hasPermissions = true;
for (String permission : permissions) {
hasPermissions &= ContextCompat.checkSelfPermission(activity, permission)
== PackageManager.PERMISSION_GRANTED;
}
return hasPermissions;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment