Skip to content

Commit

Permalink
feat: improve permission handling (WPB-6154) (#2655)
Browse files Browse the repository at this point in the history
  • Loading branch information
ohassine authored Feb 12, 2024
1 parent 0be4dc8 commit 32085af
Show file tree
Hide file tree
Showing 61 changed files with 830 additions and 415 deletions.

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

0 comments on commit 32085af

Please sign in to comment.