Last active
November 1, 2018 03:42
-
-
Save muthuraj57/32b2e1b4b31ad3adb50bf0028682a07e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PermissionUtil { | |
/* | |
* Check if version is marshmallow and above. | |
* Used in deciding to ask runtime permission | |
* */ | |
public static boolean shouldAskPermission() { | |
return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M); | |
} | |
private static boolean shouldAskPermission(Context context, String permission){ | |
if (shouldAskPermission()) { | |
int permissionResult = ActivityCompat.checkSelfPermission(context, permission); | |
if (permissionResult != PackageManager.PERMISSION_GRANTED) { | |
return true; | |
} | |
} | |
return false; | |
} | |
public static void checkPermission(Context context, String permission, PermissionAskListener listener){ | |
/* | |
* If permission is not granted | |
* */ | |
if (shouldAskPermission(context, permission)){ | |
/* | |
* If permission denied previously | |
* */ | |
if (context.shouldShowRequestPermissionRationale(permission)) { | |
listener.onPermissionPreviouslyDenied(); | |
} else { | |
/* | |
* Permission denied or first time requested | |
* */ | |
if (PreferencesUtil.isFirstTimeAskingPermission(context, permission)) { | |
PreferencesUtil.firstTimeAskingPermission(context, permission, false); | |
listener.onNeedPermission(); | |
} else { | |
/* | |
* Handle the feature without permission or ask user to manually allow permission | |
* */ | |
listener.onPermissionDisabled(); | |
} | |
} | |
} else { | |
listener.onPermissionGranted(); | |
} | |
} | |
/* | |
* Callback on various cases on checking permission | |
* | |
* 1. Below M, runtime permission not needed. In that case onPermissionGranted() would be called. | |
* If permission is already granted, onPermissionGranted() would be called. | |
* | |
* 2. Above M, if the permission is being asked first time onNeedPermission() would be called. | |
* | |
* 3. Above M, if the permission is previously asked but not granted, onPermissionPreviouslyDenied() | |
* would be called. | |
* | |
* 4. Above M, if the permission is disabled by device policy or the user checked "Never ask again" | |
* check box on previous request permission, onPermissionDisabled() would be called. | |
* */ | |
public interface PermissionAskListener { | |
/* | |
* Callback to ask permission | |
* */ | |
void onNeedPermission(); | |
/* | |
* Callback on permission denied | |
* */ | |
void onPermissionPreviouslyDenied(); | |
/* | |
* Callback on permission "Never show again" checked and denied | |
* */ | |
void onPermissionDisabled(); | |
/* | |
* Callback on permission granted | |
* */ | |
void onPermissionGranted(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I can not compile the file, revise checkPermission() method:
public static void checkPermission(Activity context, String permission, PermissionAskListener listener){
/*
* If permission is not granted
* /
if (shouldAskPermission(context, permission)){
/
* If permission denied previously
* /
if (ActivityCompat.shouldShowRequestPermissionRationale(context, permission)) {
listener.onPermissionPreviouslyDenied();
} else {
/
* Permission denied or first time requested
* /
if (PreferencesUtil.isFirstTimeAskingPermission(context, permission)) {
PreferencesUtil.firstTimeAskingPermission(context, permission, false);
listener.onNeedPermission();
} else {
/
* Handle the feature without permission or ask user to manually allow permission
* */
listener.onPermissionDisabled();
}
}
} else {
listener.onPermissionGranted();
}
}