Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method for denying permissions #378

Merged
merged 5 commits into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,25 @@ class PermissionGranterTest {
}
}

@Test
fun denies_simple_permission() {
launchActivity {
verifyPermissionNotGranted(PERMISSION_3_CALLS)
requestPermission(PERMISSION_3_CALLS)

PermissionGranter.denyPermissions(PERMISSION_3_CALLS)

verifyPermissionNotGranted(PERMISSION_3_CALLS)
}
}


}

// We can't reuse permission from one test to another, because they stay granted after each test
private const val PERMISSION_1_CONTACTS = Manifest.permission.READ_CONTACTS
private const val PERMISSION_2_CAMERA = Manifest.permission.CAMERA
private const val PERMISSION_3_CALLS = Manifest.permission.ANSWER_PHONE_CALLS
private const val LOCATION_PERMISSION = Manifest.permission.ACCESS_FINE_LOCATION

private fun ActivityScenario<*>.requestPermission(permission: String) {
Expand Down
1 change: 1 addition & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.ANSWER_PHONE_CALLS"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<application
Expand Down