1 /*
<lambda>null2  * Copyright 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.core.telecom.test.ui
18 
19 import android.app.Activity.RESULT_OK
20 import android.content.Intent
21 import androidx.activity.compose.rememberLauncherForActivityResult
22 import androidx.activity.result.contract.ActivityResultContracts
23 import androidx.compose.foundation.layout.Arrangement
24 import androidx.compose.foundation.layout.Column
25 import androidx.compose.foundation.layout.fillMaxHeight
26 import androidx.compose.foundation.layout.fillMaxWidth
27 import androidx.compose.foundation.layout.padding
28 import androidx.compose.material3.ElevatedButton
29 import androidx.compose.material3.ExperimentalMaterial3Api
30 import androidx.compose.material3.MaterialTheme
31 import androidx.compose.material3.Scaffold
32 import androidx.compose.material3.SnackbarHost
33 import androidx.compose.material3.SnackbarHostState
34 import androidx.compose.material3.Text
35 import androidx.compose.material3.TopAppBar
36 import androidx.compose.material3.TopAppBarDefaults.topAppBarColors
37 import androidx.compose.runtime.Composable
38 import androidx.compose.runtime.remember
39 import androidx.compose.runtime.rememberCoroutineScope
40 import androidx.compose.ui.Alignment
41 import androidx.compose.ui.Modifier
42 import androidx.compose.ui.unit.dp
43 import kotlinx.coroutines.launch
44 
45 /**
46  * Screen that allows the user to request the dialer role for this application, which grants the
47  * permissions required for this application to run.
48  */
49 @OptIn(ExperimentalMaterial3Api::class)
50 @Composable
51 fun RoleRequestScreen(roleIntent: Intent, onGrantedStateChanged: (Boolean) -> Unit) {
52     val scope = rememberCoroutineScope()
53     val snackbarHostState = remember { SnackbarHostState() }
54     // Handles launching activities for result
55     val launcher =
56         rememberLauncherForActivityResult(
57             contract = ActivityResultContracts.StartActivityForResult()
58         ) {
59             when (it.resultCode) {
60                 RESULT_OK -> onGrantedStateChanged(true)
61                 else -> {
62                     scope.launch { snackbarHostState.showSnackbar("Role denied: Try again?") }
63                     onGrantedStateChanged(false)
64                 }
65             }
66         }
67     Scaffold(
68         topBar = {
69             TopAppBar(
70                 colors =
71                     topAppBarColors(
72                         containerColor = MaterialTheme.colorScheme.primaryContainer,
73                         titleContentColor = MaterialTheme.colorScheme.primary
74                     ),
75                 title = { Text("Required Permissions Request") }
76             )
77         },
78         snackbarHost = { SnackbarHost(hostState = snackbarHostState) },
79     ) { contentPadding ->
80         Column(
81             horizontalAlignment = Alignment.CenterHorizontally,
82             modifier = Modifier.padding(contentPadding).padding(12.dp).fillMaxHeight()
83         ) {
84             Column(modifier = Modifier.weight(1f), verticalArrangement = Arrangement.Center) {
85                 Text(
86                     text =
87                         "This test app is required to be the default dialer to work." +
88                             "Tap \"Request Permissions\" below and set this app as the default dialer" +
89                             " to continue."
90                 )
91             }
92             Column {
93                 ElevatedButton(
94                     modifier = Modifier.fillMaxWidth(),
95                     onClick = { launcher.launch(roleIntent) }
96                 ) {
97                     Text("Request Permissions")
98                 }
99             }
100         }
101     }
102 }
103