= arrayOf("android.permission.CAMERA")
fun MainActivity.showCameraWithCheck() {
if (PermissionUtils.hasSelfPermissions(this, PERMISSION_SHOWCAMERA)) {
showCamera()
} else {
if (PermissionUtils.shouldShowRequestPermissionRationale(this, PERMISSION_SHOWCAMERA)) {
showRationaleForCamera(ShowCameraPermissionRequest(this))
} else {
ActivityCompat.requestPermissions(this, PERMISSION_SHOWCAMERA, REQUEST_SHOWCAMERA)
}
}
}
fun MainActivity.onRequestPermissionsResult(requestCode: Int, grantResults: IntArray): Unit {
when (requestCode) {
REQUEST_SHOWCAMERA ->
if (PermissionUtils.verifyPermissions(*grantResults)) {
showCamera()
} else {
if (!PermissionUtils.shouldShowRequestPermissionRationale(this, PERMISSION_SHOWCAMERA)) {
onCameraNeverAskAgain()
} else {
onCameraDenied()
}
}
}
}
private class ShowCameraPermissionRequest(target: MainActivity) : PermissionRequest {
private val weakTarget: WeakReference<MainActivity> = WeakReference(target)
override fun proceed() {
val target = weakTarget.get() ?: return
ActivityCompat.requestPermissions(target, PERMISSION_SHOWCAMERA, REQUEST_SHOWCAMERA)
}
override fun cancel() {
val target = weakTarget.get() ?: return
target.onCameraDenied()
}
}