From 43e1489dd4d6cf8c7a22e711b4aa0cb2dc5b4cc9 Mon Sep 17 00:00:00 2001 From: Mathieu Date: Tue, 4 Jan 2022 16:56:59 +0100 Subject: [PATCH] Moved permission stuff from Utils to PermissionsUtils.kt and kept only the essential --- .../jarvis/android/utils/PermissionsUtils.kt | 48 ++++++++++ .../jarvis/android/utils/Utils.kt | 91 +++---------------- 2 files changed, 59 insertions(+), 80 deletions(-) create mode 100644 app/src/main/java/ch/mathieubroillet/jarvis/android/utils/PermissionsUtils.kt diff --git a/app/src/main/java/ch/mathieubroillet/jarvis/android/utils/PermissionsUtils.kt b/app/src/main/java/ch/mathieubroillet/jarvis/android/utils/PermissionsUtils.kt new file mode 100644 index 0000000..85e892e --- /dev/null +++ b/app/src/main/java/ch/mathieubroillet/jarvis/android/utils/PermissionsUtils.kt @@ -0,0 +1,48 @@ +package ch.mathieubroillet.jarvis.android.utils + +import androidx.compose.material.* +import androidx.compose.runtime.Composable +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.painterResource +import androidx.compose.ui.res.stringResource +import ch.mathieubroillet.jarvis.android.R +import com.google.accompanist.permissions.ExperimentalPermissionsApi +import com.google.accompanist.permissions.PermissionRequired +import com.google.accompanist.permissions.rememberPermissionState + + +@OptIn(ExperimentalPermissionsApi::class) +@Composable +fun requestPermission(permission: String) { + val cameraPermissionState = rememberPermissionState(permission = permission) + val context = LocalContext.current + + PermissionRequired( + permissionState = cameraPermissionState, + permissionNotGrantedContent = { + Button(onClick = { + cameraPermissionState.launchPermissionRequest() + }) { + Text(text = stringResource(id = R.string.permissions_page_grant_permission)) + } + + }, + permissionNotAvailableContent = { + Button( + onClick = { openAppSettings(context) }, + colors = ButtonDefaults.buttonColors(backgroundColor = MaterialTheme.colors.error) + ) { + Text(text = stringResource(id = R.string.permissions_page_permission_denied)) + Icon( + painter = painterResource(id = R.drawable.ic_outline_settings_24), + contentDescription = "app settings icon" + ) + } + }, + content = { + Button(onClick = {}, enabled = false) { + Text(text = stringResource(id = R.string.permission_granted)) + } + } + ) +} \ No newline at end of file diff --git a/app/src/main/java/ch/mathieubroillet/jarvis/android/utils/Utils.kt b/app/src/main/java/ch/mathieubroillet/jarvis/android/utils/Utils.kt index 55fc4d9..3878003 100644 --- a/app/src/main/java/ch/mathieubroillet/jarvis/android/utils/Utils.kt +++ b/app/src/main/java/ch/mathieubroillet/jarvis/android/utils/Utils.kt @@ -1,19 +1,13 @@ package ch.mathieubroillet.jarvis.android.utils +import android.content.Context +import android.content.Intent +import android.net.Uri +import android.provider.Settings import androidx.compose.foundation.layout.* -import androidx.compose.material.Button -import androidx.compose.material.Text import androidx.compose.runtime.Composable -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.saveable.rememberSaveable -import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier -import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.unit.dp -import com.google.accompanist.permissions.ExperimentalPermissionsApi -import com.google.accompanist.permissions.PermissionRequired -import com.google.accompanist.permissions.rememberPermissionState @Composable @@ -32,75 +26,12 @@ fun DefaultBox( } -@OptIn(ExperimentalPermissionsApi::class) -@Composable -fun FeatureThatRequiresMicrophonePermission() { - var doNotShowRationale by rememberSaveable { - mutableStateOf(false) - } - - val cameraPermissionState = - rememberPermissionState(permission = android.Manifest.permission.RECORD_AUDIO) - - val context = LocalContext.current - - PermissionRequired( - permissionState = cameraPermissionState, - permissionNotGrantedContent = { - if (doNotShowRationale) { - Text("Feature not available") - } else { - PermissionNotGrantedUI( - onYesClick = { - cameraPermissionState.launchPermissionRequest() - }, onCancelClick = { - doNotShowRationale = true - }) - } - }, - permissionNotAvailableContent = { - PermissionNotAvailableContent( - onOpenSettingsClick = { /*context.openSettings()*/ }) - }, - content = { - Text("Camera Permission Granted") - } +fun openAppSettings(context: Context) { + context.startActivity( + Intent( + Settings.ACTION_APPLICATION_DETAILS_SETTINGS, + Uri + .fromParts("package", context.applicationInfo.packageName, null) + ) ) - - } - -@Composable -fun PermissionNotAvailableContent(onOpenSettingsClick: () -> Unit) { - - Column { - Text("Camera permission denied.") - Spacer(modifier = Modifier.height(8.dp)) - Button(onClick = { onOpenSettingsClick() }) { - Text("Open settings") - } - } -} - - -@Composable -fun PermissionNotGrantedUI(onYesClick: () -> Unit, onCancelClick: () -> Unit) { - Column { - Text("Camera is important for this app. Please grant the permission.") - Spacer(modifier = Modifier.height(8.dp)) - Row { - Button(onClick = { - onYesClick() - }) { - Text("Yes") - } - Spacer(modifier = Modifier.width(8.dp)) - Button(onClick = { - onCancelClick() - }) { - Text("Cancel") - } - } - } - -} \ No newline at end of file