Skip to content
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

feat: improve permission handling (WPB-6154) #2655

Merged
merged 18 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.wire.android.ui.common.colorsScheme
import com.wire.android.ui.common.dimensions
import com.wire.android.ui.theme.wireDimensions
import com.wire.android.ui.theme.wireTypography
import com.wire.android.util.permission.PermissionDenialType

@Composable
fun CallOptionsControls(
Expand All @@ -43,7 +44,8 @@ fun CallOptionsControls(
isSpeakerOn: Boolean,
toggleSpeaker: () -> Unit,
toggleMute: () -> Unit,
toggleVideo: () -> Unit
toggleVideo: () -> Unit,
onPermissionPermanentlyDenied: (type: PermissionDenialType) -> Unit
) {
ConstraintLayout(
modifier = Modifier
Expand All @@ -70,7 +72,7 @@ fun CallOptionsControls(
end.linkTo(speakerIcon.start)
},
isCameraOn = isCameraOn,
onCameraPermissionDenied = { },
onPermissionPermanentlyDenied = onPermissionPermanentlyDenied,
onCameraButtonClicked = toggleVideo
)
CallControlLabel(stringResource(id = R.string.calling_button_label_camera), cameraText, cameraIcon)
Expand Down Expand Up @@ -117,6 +119,7 @@ fun PreviewCallOptionsControls() {
isSpeakerOn = false,
toggleSpeaker = { },
toggleMute = { },
toggleVideo = { }
toggleVideo = { },
onPermissionPermanentlyDenied = {}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@

package com.wire.android.ui.calling.controlbuttons

import android.content.Context
import androidx.activity.compose.ManagedActivityResultLauncher
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.size
Expand All @@ -31,34 +27,31 @@ import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.tooling.preview.Preview
import com.wire.android.R
import com.wire.android.appLogger
import com.wire.android.ui.common.dimensions
import com.wire.android.util.extension.checkPermission
import com.wire.android.util.permission.PermissionDenialType
import com.wire.android.util.permission.rememberCallingCameraRequestFlow

@Composable
fun CameraButton(
modifier: Modifier = Modifier.size(dimensions().defaultCallingControlsSize),
isCameraOn: Boolean = false,
onCameraPermissionDenied: () -> Unit,
onCameraButtonClicked: () -> Unit
onCameraButtonClicked: () -> Unit,
onPermissionPermanentlyDenied: (type: PermissionDenialType) -> Unit
) {
val context = LocalContext.current

val cameraPermissionLauncher = rememberLauncherForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted ->
if (isGranted) {
onCameraButtonClicked()
} else {
onCameraPermissionDenied()
val cameraPermissionCheck = CameraPermissionCheckFlow(
onPermissionGranted = onCameraButtonClicked,
onPermanentPermissionDecline = {
onPermissionPermanentlyDenied(
PermissionDenialType.CallingCamera
)
}
}
)

WireCallControlButton(
isSelected = isCameraOn,
Expand All @@ -67,16 +60,15 @@ fun CameraButton(
Icon(
modifier = Modifier
.wrapContentSize()
.clickable(interactionSource = remember { MutableInteractionSource() },
indication = rememberRipple(bounded = false, radius = dimensions().defaultCallingControlsSize / 2),
.clickable(
interactionSource = remember { MutableInteractionSource() },
indication = rememberRipple(
bounded = false,
radius = dimensions().defaultCallingControlsSize / 2
),
role = Role.Button,
onClick = {
verifyCameraPermission(
context = context,
cameraPermissionLauncher = cameraPermissionLauncher,
onCameraButtonClicked = onCameraButtonClicked
)
}),
onClick = cameraPermissionCheck::launch,
),
painter = painterResource(
id = if (isCameraOn) {
R.drawable.ic_camera_on
Expand All @@ -93,18 +85,21 @@ fun CameraButton(
}
}

private fun verifyCameraPermission(
context: Context, cameraPermissionLauncher: ManagedActivityResultLauncher<String, Boolean>, onCameraButtonClicked: () -> Unit
) {
if (context.checkPermission(android.Manifest.permission.CAMERA).not()) {
cameraPermissionLauncher.launch(android.Manifest.permission.CAMERA)
} else {
onCameraButtonClicked()
}
}
@Composable
private fun CameraPermissionCheckFlow(
onPermissionGranted: () -> Unit,
onPermanentPermissionDecline: () -> Unit
) = rememberCallingCameraRequestFlow(
onPermissionGranted = {
appLogger.d("Camera permission granted")
onPermissionGranted()
},
onPermissionDenied = { },
onPermissionPermanentlyDenied = onPermanentPermissionDecline
)

@Preview
@Composable
fun PreviewComposableCameraButton() {
CameraButton(onCameraPermissionDenied = { }, onCameraButtonClicked = { })
CameraButton(onCameraButtonClicked = { }, onPermissionPermanentlyDenied = { })
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,24 @@ import com.wire.android.ui.common.button.WirePrimaryButton
import com.wire.android.ui.common.dimensions
import com.wire.android.ui.theme.wireDimensions
import com.wire.android.ui.theme.wireTypography
import com.wire.android.util.permission.PermissionDenialType
import com.wire.android.util.permission.rememberCallingRecordAudioRequestFlow

@Composable
fun JoinButton(
buttonClick: () -> Unit,
onPermanentPermissionDecline: () -> Unit,
onPermissionPermanentlyDenied: (type: PermissionDenialType) -> Unit,
minSize: DpSize = MaterialTheme.wireDimensions.buttonMediumMinSize,
minClickableSize: DpSize = MaterialTheme.wireDimensions.buttonMinClickableSize,
horizontalPadding: Dp = MaterialTheme.wireDimensions.spacing8x,
) {
val audioPermissionCheck = AudioPermissionCheckFlow(
onJoinCall = buttonClick,
onPermanentPermissionDecline = onPermanentPermissionDecline
onPermanentPermissionDecline = {
onPermissionPermanentlyDenied(
PermissionDenialType.CallingMicrophone
)
}
)

WirePrimaryButton(
Expand Down Expand Up @@ -87,6 +92,6 @@ private fun AudioPermissionCheckFlow(
fun PreviewJoinButton() {
JoinButton(
buttonClick = {},
onPermanentPermissionDecline = {}
onPermissionPermanentlyDenied = {}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,23 @@ import com.wire.android.ui.common.button.WireButtonState
import com.wire.android.ui.common.button.WireSecondaryButton
import com.wire.android.ui.common.dimensions
import com.wire.android.ui.theme.wireDimensions
import com.wire.android.util.permission.PermissionDenialType
import com.wire.android.util.permission.rememberCallingRecordAudioRequestFlow
import com.wire.android.util.ui.PreviewMultipleThemes

@Composable
fun StartCallButton(
onPhoneButtonClick: () -> Unit,
onPermanentPermissionDecline: () -> Unit,
onPermissionPermanentlyDenied: (type: PermissionDenialType) -> Unit,
isCallingEnabled: Boolean
) {
val audioPermissionCheck = AudioPermissionCheckFlow(
startCall = onPhoneButtonClick,
onPermanentPermissionDecline = onPermanentPermissionDecline
onPermanentPermissionDecline = {
onPermissionPermanentlyDenied(
PermissionDenialType.CallingMicrophone
)
}
)

WireSecondaryButton(
Expand Down Expand Up @@ -86,7 +91,7 @@ private fun AudioPermissionCheckFlow(
fun PreviewStartCallButton() {
StartCallButton(
onPhoneButtonClick = {},
onPermanentPermissionDecline = {},
onPermissionPermanentlyDenied = {},
isCallingEnabled = true
)
}
Loading
Loading