Merge pull request #1400 from vector-im/feature/bma/fixCrashPhoto

Fix crash when user wants to use the camera
This commit is contained in:
Benoit Marty 2023-09-21 16:31:36 +02:00 committed by GitHub
commit 5119ca33dd
47 changed files with 597 additions and 143 deletions

View file

@ -17,6 +17,7 @@
package io.element.android.libraries.permissions.api
sealed interface PermissionsEvents {
data object OpenSystemDialog : PermissionsEvents
data object RequestPermissions : PermissionsEvents
data object CloseDialog : PermissionsEvents
data object OpenSystemSettingAndCloseDialog : PermissionsEvents
}

View file

@ -16,67 +16,42 @@
package io.element.android.libraries.permissions.api
import android.Manifest
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.PreviewParameter
import io.element.android.libraries.designsystem.components.dialogs.ConfirmationDialog
import io.element.android.libraries.designsystem.preview.PreviewsDayNight
import io.element.android.libraries.designsystem.preview.ElementPreview
import io.element.android.libraries.ui.strings.CommonStrings
@Composable
fun PermissionsView(
state: PermissionsState,
modifier: Modifier = Modifier,
openSystemSettings: () -> Unit = {},
) {
if (state.showDialog.not()) return
when {
state.permissionGranted -> {
// Notification Granted, nothing to do
}
state.permissionAlreadyDenied -> {
// In this case, tell the user to go to the settings
ConfirmationDialog(
modifier = modifier,
title = "System",
content = "In order to let the application display notification, please grant the permission to the system settings",
submitText = "Open settings",
onSubmitClicked = {
state.eventSink.invoke(PermissionsEvents.CloseDialog)
openSystemSettings()
},
onDismiss = { state.eventSink.invoke(PermissionsEvents.CloseDialog) },
)
}
else -> {
val textToShow = if (state.shouldShowRationale) {
// TODO Move to state
// If the user has denied the permission but the rationale can be shown,
// then gently explain why the app requires this permission
// permissions_rationale_msg_notification
"To be able to receive notifications, please grant the permission. Else you will not be able to be alerted if you've got new messages."
} else {
// TODO Move to state
// If it's the first time the user lands on this feature, or the user
// doesn't want to be asked again for this permission, explain that the
// permission is required
"To be able to receive notifications, please grant the permission."
}
ConfirmationDialog(
modifier = modifier,
title = "Notifications",
content = textToShow,
submitText = "Request permission",
onSubmitClicked = {
state.eventSink.invoke(PermissionsEvents.OpenSystemDialog)
},
onCancelClicked = {
state.eventSink.invoke(PermissionsEvents.CloseDialog)
},
onDismiss = {}
)
}
ConfirmationDialog(
modifier = modifier,
title = stringResource(id = CommonStrings.common_permission),
content = state.permission.toDialogContent(),
submitText = stringResource(id = CommonStrings.action_open_settings),
onSubmitClicked = {
state.eventSink.invoke(PermissionsEvents.OpenSystemSettingAndCloseDialog)
},
onDismiss = { state.eventSink.invoke(PermissionsEvents.CloseDialog) },
)
}
@Composable
private fun String.toDialogContent(): String {
return when (this) {
Manifest.permission.POST_NOTIFICATIONS -> stringResource(id = R.string.dialog_permission_notification)
Manifest.permission.CAMERA -> stringResource(id = R.string.dialog_permission_camera)
Manifest.permission.RECORD_AUDIO -> stringResource(id = R.string.dialog_permission_microphone)
else -> stringResource(id = R.string.dialog_permission_generic)
}
}

View file

@ -22,17 +22,21 @@ import androidx.compose.ui.tooling.preview.PreviewParameterProvider
open class PermissionsViewStateProvider : PreviewParameterProvider<PermissionsState> {
override val values: Sequence<PermissionsState>
get() = sequenceOf(
aPermissionsState(),
aPermissionsState().copy(shouldShowRationale = true),
aPermissionsState().copy(permissionAlreadyDenied = true),
aPermissionsState(showDialog = true, permission = Manifest.permission.POST_NOTIFICATIONS),
aPermissionsState(showDialog = true, permission = Manifest.permission.CAMERA),
aPermissionsState(showDialog = true, permission = Manifest.permission.RECORD_AUDIO),
aPermissionsState(showDialog = true, permission = Manifest.permission.INTERNET),
)
}
fun aPermissionsState() = PermissionsState(
permission = Manifest.permission.INTERNET,
fun aPermissionsState(
showDialog: Boolean,
permission: String = Manifest.permission.POST_NOTIFICATIONS
) = PermissionsState(
permission = permission,
permissionGranted = false,
shouldShowRationale = false,
showDialog = true,
showDialog = showDialog,
permissionAlreadyAsked = false,
permissionAlreadyDenied = false,
eventSink = {}

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="dialog_permission_camera">"In order to let the application use the camera, please grant the permission to the system settings."</string>
<string name="dialog_permission_generic">"Please grant the permission to the system settings."</string>
<string name="dialog_permission_microphone">"In order to let the application use the microphone, please grant the permission to the system settings."</string>
<string name="dialog_permission_notification">"In order to let the application display notification, please grant the permission to the system settings."</string>
</resources>