Skip to content

Commit

Permalink
Add method for denying permissions
Browse files Browse the repository at this point in the history
In some cases we also need to test the app's behavior when the user denies a permission. This change adds the denyPermissions() method.
  • Loading branch information
milhauscz authored Nov 19, 2020
1 parent 87cd5a7 commit dd9f582
Showing 1 changed file with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ object PermissionGranter {
"com.android.packageinstaller:id/permission_allow_button"
)

// In API 30 the "Deny" button has the first ID when it is shown the first time. Second time
// it has the second ID (do not ask again), although text is the same. In API 29 the buttons
// are shown separately.
private val PERMISSION_DIALOG_DENY_IDS = listOf(
"com.android.permissioncontroller:id/permission_deny_button",
"com.android.permissioncontroller:id/permission_deny_and_dont_ask_again_button",
"com.android.packageinstaller:id/permission_deny_button"
)

private fun List<String>.toPermissionButtonRegex() = joinToString(
prefix = "^(",
separator = "|",
Expand All @@ -35,15 +44,20 @@ object PermissionGranter {

@JvmStatic
fun allowPermissionsIfNeeded(permissionNeeded: String) {
allowPermission(permissionNeeded, PERMISSION_DIALOG_ALLOW_FOREGROUND_IDS.toPermissionButtonRegex())
clickPermissionDialogButton(permissionNeeded, PERMISSION_DIALOG_ALLOW_FOREGROUND_IDS.toPermissionButtonRegex())
}

@JvmStatic
fun allowPermissionOneTime(permissionNeeded: String) {
allowPermission(permissionNeeded, PERMISSION_DIALOG_ALLOW_ONE_TIME_IDS.toPermissionButtonRegex())
clickPermissionDialogButton(permissionNeeded, PERMISSION_DIALOG_ALLOW_ONE_TIME_IDS.toPermissionButtonRegex())
}

@JvmStatic
fun denyPermissions(permissionRequested: String) {
clickPermissionDialogButton(permissionRequested, PERMISSION_DIALOG_DENY_IDS.toPermissionButtonRegex())
}

private fun allowPermission(permissionNeeded: String, permissionsIds: String) {
private fun clickPermissionDialogButton(permissionNeeded: String, permissionsIds: String) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !hasNeededPermission(
getApplicationContext(),
Expand Down Expand Up @@ -75,4 +89,4 @@ object PermissionGranter {
private fun checkSelfPermission(context: Context, permission: String): Int {
return context.checkPermission(permission, android.os.Process.myPid(), android.os.Process.myUid())
}
}
}

0 comments on commit dd9f582

Please sign in to comment.