Skip to content

Instantly share code, notes, and snippets.

@sembozdemir
Last active November 26, 2017 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sembozdemir/ce8bebc7000adef04360b4f47ce478fd to your computer and use it in GitHub Desktop.
Save sembozdemir/ce8bebc7000adef04360b4f47ce478fd to your computer and use it in GitHub Desktop.
@file:JvmName("PermissionExtensions")
import android.app.Activity
import android.content.pm.PackageManager
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
/**
* Invoke [onGranted] if permission is granted. Otherwise, request permission.
*
* @param permission Manifest permission (e.g. Manifest.permission.CAMERA)
* @param requestCode Application specific request code to match with a result
* reported to [Activity.onRequestPermissionsResult]. Should be >= 0.
* @param onGranted higher-order function to be invoked if permission is granted
*/
inline fun Activity.doIfGranted(permission: String,
requestCode: Int,
onGranted: () -> Unit) {
doIfGranted(arrayOf(permission), requestCode, onGranted)
}
/**
* Invoke [onGranted] if permissions are granted. Otherwise, request permission.
*
* @param permissions Manifest permissions array (e.g. {Manifest.permission.CAMERA, Manifest.permission.CALL_PHONE})
* @param requestCode Application specific request code to match with a result
* reported to [Activity.onRequestPermissionsResult]. Should be >= 0.
* @param onGranted higher-order function to be invoked if permissions are granted
*/
inline fun Activity.doIfGranted(permissions: Array<String>,
requestCode: Int,
onGranted: () -> Unit) {
val permissionsNeeded = permissions.filter { !isPermissionGranted(it) }
if (permissionsNeeded.isNotEmpty()) {
ActivityCompat.requestPermissions(this,
permissionsNeeded.toTypedArray(),
requestCode)
} else {
onGranted()
}
}
/**
* Check if permission is granted.
*
* @param permission Manifest permission (e.g. Manifest.permission.CAMERA)
* @return true if it is granted
*/
fun Activity.isPermissionGranted(permission: String): Boolean {
return ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED
}
/**
* Helper method for handling permissions result.
*
* Invoke this from [Activity.onRequestPermissionsResult]
*
* @param permissions permissions array received in [Activity.onRequestPermissionsResult]
* @param grantResults result array received in [Activity.onRequestPermissionsResult]
* @param onShowRationale optional callback for denied permissions which user did not check never ask again,
* explain the user why you need permission and ask if he/she wants to accept it
* @param onNeverAskAgain optional callback for denied permissions which user check never ask again,
* disable the features of your app or show a warning and redirect to the user app settings to grant permission
* @param onDenied optional callback for denied permissions, this callback will be invoked
* if at least one permission is denied either user check never ask again or not
* @param onGranted callback when all permissions are granted, continue your feature
*/
@JvmOverloads
inline fun Activity.handlePermissionsResult(permissions: Array<out String>,
grantResults: IntArray,
noinline onShowRationale: ((permissions: List<String>) -> Unit)? = null,
noinline onNeverAskAgain: ((permissions: List<String>) -> Unit)? = null,
noinline onDenied: (() -> Unit)? = null,
onGranted: (() -> Unit)) {
if (grantResults.isEmpty()) {
return
}
var allGranted = true
val showRationalePermissions = mutableListOf<String>()
val neverAskAgainPermissions = mutableListOf<String>()
for (i in permissions.indices) {
if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
allGranted = false
if (ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[i])) {
showRationalePermissions.add(permissions[i])
} else {
neverAskAgainPermissions.add(permissions[i])
}
}
}
if (allGranted) {
onGranted()
} else {
onDenied?.invoke()
if (showRationalePermissions.isNotEmpty()) {
onShowRationale?.invoke(showRationalePermissions.toList())
}
if (neverAskAgainPermissions.isNotEmpty()) {
onNeverAskAgain?.invoke(neverAskAgainPermissions.toList())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment