Skip to content

Add permissions example #106

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ sense.
|-------|-------|
|[How can I navigate to different screen in Jetpack Compose?](https://github.com/vinaygaba/Learn-Jetpack-Compose-By-Example/blob/master/app/src/main/java/com/example/jetpackcompose/navigation/ComposeNavigationActivity.kt) | <img src ="screenshots/compose_navigation_example.gif" width=214 height=400>|


### Permissions
| Example |Preview|
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------|
| [How can I request for permissions in Jetpack Compose?](https://github.com/vinaygaba/Learn-Jetpack-Compose-By-Example/blob/master/app/src/main/java/com/example/jetpackcompose/permissions/PermissionActivity.kt) | <img src ="screenshots/compose_permission_example.gif" width=214 height=400>|

### Testing
|Example|Preview|
|-------|-------|
Expand Down
6 changes: 6 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ android {
}
kotlinOptions {
jvmTarget = '11'
freeCompilerArgs += [
'-Xopt-in=com.google.accompanist.permissions.ExperimentalPermissionsApi'
]
}
buildFeatures {
compose true
Expand Down Expand Up @@ -65,6 +68,9 @@ dependencies {
implementation deps.support.ktxLiveData
implementation deps.support.lifecycleExtensions

// Accompanist
implementation deps.accompanist.permissions

// CardView
implementation deps.cardView

Expand Down
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package="com.example.jetpackcompose">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

<application
android:allowBackup="true"
Expand Down Expand Up @@ -193,6 +194,11 @@
android:exported="true"
android:label="@string/title_compose_navigation_example"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".permissions.PermissionActivity"
android:exported="true"
android:label="@string/title_permission_example"
android:theme="@style/AppTheme.NoActionBar" />
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import com.example.jetpackcompose.material.FlowRowActivity
import com.example.jetpackcompose.material.MaterialActivity
import com.example.jetpackcompose.material.ShadowActivity
import com.example.jetpackcompose.navigation.ComposeNavigationActivity
import com.example.jetpackcompose.permissions.PermissionActivity
import com.example.jetpackcompose.scrollers.HorizontalScrollableActivity
import com.example.jetpackcompose.scrollers.VerticalScrollableActivity
import com.example.jetpackcompose.stack.StackActivity
Expand Down Expand Up @@ -190,4 +191,8 @@ class MainActivity : AppCompatActivity() {
fun startComposeNavigationExample(view: View) {
startActivity(Intent(this, ComposeNavigationActivity::class.java))
}

fun startPermissionExample(view: View) {
startActivity(Intent(this, PermissionActivity::class.java))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.example.jetpackcompose.permissions

import android.Manifest
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.google.accompanist.permissions.isGranted
import com.google.accompanist.permissions.rememberPermissionState
import com.google.accompanist.permissions.shouldShowRationale

class PermissionActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContent {
App()
}
}
}

/**
* In this example, we are going to use Google's Accompanist Library - Jetpack Compose Permissions,
* to request for the permissions from the user.
* Reference: https://google.github.io/accompanist/permissions/
*
* Please Note: The version of the library depends on the version of the Jetpack Compose version
* that you're using in the application. So, please make sure that the Accompanist library version
* matches the Jetpack Compose version.
* Version Reference: https://github.com/google/accompanist#compose-versions
*/
@Composable
private fun App() {
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
) {
val contactsPermissionState = rememberPermissionState(
permission = Manifest.permission.READ_CONTACTS
)

if (contactsPermissionState.status.isGranted) {
Text("Contacts permission is granted")
} else {
Column {
val text = if (contactsPermissionState.status.shouldShowRationale) {
// Shown if the user has denied the requested permissions but
// the rationale can be shown, informing the user why the permission
// is needed.
"Contacts permission is required to display list of contact."
} else {
// Shown if it is the first time the user reached this page, and
// the app is requesting the permission for the first time.
"Please grant the Contacts permission to display list of contacts."
}

Text(text)
Button(
onClick = { contactsPermissionState.launchPermissionRequest() },
) {
Text("Request Permission")
}
}
}
}
}
19 changes: 19 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -677,5 +677,24 @@
android:layout_gravity="center"
android:fontFamily="monospace" />
</androidx.cardview.widget.CardView>

<androidx.cardview.widget.CardView
android:id="@+id/permission_example"
android:layout_width="match_parent"
android:layout_height="@dimen/cardview_height"
app:cardBackgroundColor="@color/colorPrimary"
android:padding="@dimen/default_padding"
android:onClick="startPermissionExample"
android:layout_marginTop="@dimen/default_padding"
app:cardCornerRadius="@dimen/card_corner_radius">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tv_permission_example"
android:textColor="@color/white"
android:layout_gravity="center"
android:fontFamily="monospace" />
</androidx.cardview.widget.CardView>
</LinearLayout>
</ScrollView>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,6 @@
<string name="tv_back_press_example">Back Press Component</string>
<string name="tv_zoomable_example">Zoomable Component</string>
<string name="tv_compose_navigation_example">Compose Navigation</string>
<string name="title_permission_example">Permission Example</string>
<string name="tv_permission_example">Jetpack Compose Permissions</string>
</resources>
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

buildscript {
ext.versions = [
'accompanistPermissions': '0.25.1',
'cardView': '1.0.0',
'compose': '1.2.0-rc01',
'composeActivity': '1.4.0',
Expand All @@ -24,6 +25,9 @@ buildscript {
'testRunner': '1.2.0',
]
ext.deps = [
'accompanist': [
'permissions': "com.google.accompanist:accompanist-permissions:$versions.accompanistPermissions"
],
'cardView': "androidx.cardview:cardview:${versions.cardView}",
'compose': [
'activityCompose': "androidx.activity:activity-compose:${versions.composeActivity}",
Expand Down
Binary file added screenshots/compose_permission_example.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.