Created
January 12, 2020 23:17
-
-
Save jonas-haeusler/05400f6cca9b65e3ccb1f91dde688391 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
package com.example.chris.OSProto.utils | |
import android.app.Activity | |
import android.content.Context | |
import android.content.Intent | |
import android.content.pm.PackageManager | |
import android.net.Uri | |
import android.provider.Settings | |
import androidx.core.app.ActivityCompat | |
object PermissionUtils { | |
/** | |
* Request code for [startApplicationSettingsActivity]. Use this in your [Activity.onActivityResult] to do | |
* something after the user returns from the settings screen. | |
*/ | |
const val REQUEST_CODE_APPLICATION_SETTINGS = 8402 | |
/** | |
* Determine whether you should show a rationale for any of the [permissions]. | |
* Returns true if you should, false otherwise. | |
*/ | |
fun shouldShowRequestPermissionRationales(activity: Activity, vararg permissions: String): Boolean { | |
permissions.forEach { permission -> | |
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) { | |
return true | |
} | |
} | |
return false | |
} | |
/** | |
* Determine whether [permissions] have been granted. | |
* Returns true when all permissions are granted, false otherwise. | |
*/ | |
fun hasPermissions(context: Context, vararg permissions: String): Boolean { | |
permissions.forEach { permission -> | |
if (ActivityCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_DENIED) { | |
return false | |
} | |
} | |
return true | |
} | |
/** | |
* Determine whether one or more [permissions] have been permanently denied. | |
* Note that due to to a limitation in the information provided by the android framework, | |
* [somePermissionPermanentlyDenied] only works after the user has denied the permission once and the application | |
* has received the `onPermissionsDenied` callback. | |
*/ | |
fun somePermissionPermanentlyDenied(activity: Activity, vararg permissions: String) = | |
!shouldShowRequestPermissionRationales(activity, *permissions) | |
/** | |
* Handle the result of a permission request. Should be called from the calling [Activity]s | |
* [Activity.onRequestPermissionsResult]. The [receiver] will be notified over any granted or denied permissions. | |
*/ | |
fun onRequestPermissionsResult( | |
requestCode: Int, | |
permissions: Array<out String>, | |
grantResults: IntArray, | |
receiver: PermissionCallbacks | |
) { | |
val granted = mutableListOf<String>() | |
val denied = mutableListOf<String>() | |
permissions.forEachIndexed { i, permission -> | |
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) { | |
granted.add(permission) | |
} else { | |
denied.add(permission) | |
} | |
} | |
if (granted.isNotEmpty()) receiver.onPermissionGranted(requestCode, granted) | |
if (denied.isNotEmpty()) receiver.onPermissionDenied(requestCode, denied) | |
} | |
/** | |
* Open the applications settings page, so the user can grant permanently denied permissions. | |
*/ | |
fun startApplicationSettingsActivity(activity: Activity) { | |
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) | |
intent.data = Uri.fromParts("package", activity.packageName, null) | |
activity.startActivityForResult(intent, REQUEST_CODE_APPLICATION_SETTINGS) | |
} | |
} | |
interface PermissionCallbacks { | |
fun onPermissionGranted(requestCode: Int, permissions: List<String>) | |
fun onPermissionDenied(requestCode: Int, permissions: List<String>) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment