xref: /aosp_15_r20/external/accompanist/docs/permissions.md (revision fa44fe6ae8e729aa3cfe5c03eedbbf98fb44e2c6)
1# Jetpack Compose Permissions
2
3[![Maven Central](https://img.shields.io/maven-central/v/com.google.accompanist/accompanist-permissions)](https://search.maven.org/search?q=g:com.google.accompanist)
4
5A library which provides [Android runtime permissions](https://developer.android.com/guide/topics/permissions/overview) support for Jetpack Compose.
6
7!!! warning
8    The permission APIs are currently experimental and they could change at any time.
9    All of the APIs are marked with the `@ExperimentalPermissionsApi` annotation.
10
11## Usage
12
13### `rememberPermissionState` and `rememberMultiplePermissionsState` APIs
14
15The `rememberPermissionState(permission: String)` API allows you to request a certain permission
16to the user and check for the status of the permission.
17`rememberMultiplePermissionsState(permissions: List<String>)` offers the same but for multiple
18permissions at the same time.
19
20Both APIs expose properties for you to follow the workflow as described in the
21[permissions documentation](https://developer.android.com/training/permissions/requesting#workflow_for_requesting_permissions).
22
23!!! caution
24    The call to the method that requests the permission to the user (e.g. `PermissionState.launchPermissionRequest()`)
25    needs to be invoked from a non-composable scope. For example, from a side-effect or from a
26    non-composable callback such as a `Button`'s `onClick` lambda.
27
28The following code exercises the [permission request workflow](https://developer.android.com/training/permissions/requesting#workflow_for_requesting_permissions).
29
30```kotlin
31@OptIn(ExperimentalPermissionsApi::class)
32@Composable
33private fun FeatureThatRequiresCameraPermission() {
34
35    // Camera permission state
36    val cameraPermissionState = rememberPermissionState(
37        android.Manifest.permission.CAMERA
38    )
39
40    if (cameraPermissionState.status.isGranted) {
41        Text("Camera permission Granted")
42    } else {
43        Column {
44            val textToShow = if (cameraPermissionState.status.shouldShowRationale) {
45                // If the user has denied the permission but the rationale can be shown,
46                // then gently explain why the app requires this permission
47                "The camera is important for this app. Please grant the permission."
48            } else {
49                // If it's the first time the user lands on this feature, or the user
50                // doesn't want to be asked again for this permission, explain that the
51                // permission is required
52                "Camera permission required for this feature to be available. " +
53                    "Please grant the permission"
54            }
55            Text(textToShow)
56            Button(onClick = { cameraPermissionState.launchPermissionRequest() }) {
57                Text("Request permission")
58            }
59        }
60    }
61}
62```
63
64For more examples, refer to the [samples](https://github.com/google/accompanist/tree/main/sample/src/main/java/com/google/accompanist/sample/permissions).
65
66## Limitations
67
68This permissions wrapper is built on top of the available Android platform APIs. We cannot extend
69the platform's capabilities. For example, it's not possible to differentiate between the
70_it's the first time requesting the permission_ vs _the user doesn't want to be asked again_
71use cases.
72
73## Download
74
75[![Maven Central](https://img.shields.io/maven-central/v/com.google.accompanist/accompanist-permissions)](https://search.maven.org/search?q=g:com.google.accompanist)
76
77```groovy
78repositories {
79    mavenCentral()
80}
81
82dependencies {
83    implementation "com.google.accompanist:accompanist-permissions:<version>"
84}
85```
86
87Snapshots of the development version are available in [Sonatype's `snapshots` repository][snap]. These are updated on every commit.
88
89[compose]: https://developer.android.com/jetpack/compose
90[snap]: https://oss.sonatype.org/content/repositories/snapshots/com/google/accompanist/accompanist-permissions/
91