diff --git a/app/src/main/kotlin/io/element/android/x/MainActivity.kt b/app/src/main/kotlin/io/element/android/x/MainActivity.kt index 7fcae7040e..2f020a89ac 100644 --- a/app/src/main/kotlin/io/element/android/x/MainActivity.kt +++ b/app/src/main/kotlin/io/element/android/x/MainActivity.kt @@ -33,6 +33,7 @@ import com.bumble.appyx.core.integration.NodeHost import com.bumble.appyx.core.integrationpoint.NodeActivity import com.bumble.appyx.core.plugin.NodeReadyObserver import io.element.android.features.lockscreen.api.handleSecureFlag +import io.element.android.features.lockscreen.api.isLocked import io.element.android.libraries.architecture.bindings import io.element.android.libraries.core.log.logger.LoggerTag import io.element.android.libraries.designsystem.utils.snackbar.LocalSnackbarDispatcher @@ -46,7 +47,6 @@ private val loggerTag = LoggerTag("MainActivity") class MainActivity : NodeActivity() { private lateinit var mainNode: MainNode - private lateinit var appBindings: AppBindings override fun onCreate(savedInstanceState: Bundle?) { @@ -61,6 +61,19 @@ class MainActivity : NodeActivity() { } } + @Deprecated("") + override fun onBackPressed() { + // If the app is locked, we need to intercept onBackPressed before it goes to OnBackPressedDispatcher. + // Indeed, otherwise we would need to trick Appyx backstack management everywhere. + // Without this trick, we would get pop operations on the hidden backstack. + if (appBindings.lockScreenService().isLocked) { + //Do not kill the app in this case, just go to background. + moveTaskToBack(false) + } else { + super.onBackPressed() + } + } + @Composable private fun MainContent(appBindings: AppBindings) { ElementTheme { diff --git a/appnav/src/main/kotlin/io/element/android/appnav/LoggedInFlowNode.kt b/appnav/src/main/kotlin/io/element/android/appnav/LoggedInFlowNode.kt index ed286c71b9..23c35b119a 100644 --- a/appnav/src/main/kotlin/io/element/android/appnav/LoggedInFlowNode.kt +++ b/appnav/src/main/kotlin/io/element/android/appnav/LoggedInFlowNode.kt @@ -362,23 +362,18 @@ class LoggedInFlowNode @AssistedInject constructor( override fun View(modifier: Modifier) { Box(modifier = modifier) { val lockScreenState by lockScreenStateService.lockState.collectAsState() - when (lockScreenState) { - LockScreenLockState.Unlocked -> { - Children( - navModel = backstack, - modifier = Modifier, - // Animate navigation to settings and to a room - transitionHandler = rememberDefaultTransitionHandler(), - ) - val isFtueDisplayed by ftueState.shouldDisplayFlow.collectAsState() - if (!isFtueDisplayed) { - PermanentChild(permanentNavModel = permanentNavModel, navTarget = NavTarget.LoggedInPermanent) - } - } - LockScreenLockState.Locked -> { - MoveActivityToBackgroundBackHandler() - PermanentChild(permanentNavModel = permanentNavModel, navTarget = NavTarget.LockPermanent) - } + Children( + navModel = backstack, + modifier = Modifier, + // Animate navigation to settings and to a room + transitionHandler = rememberDefaultTransitionHandler(), + ) + val isFtueDisplayed by ftueState.shouldDisplayFlow.collectAsState() + if (!isFtueDisplayed) { + PermanentChild(permanentNavModel = permanentNavModel, navTarget = NavTarget.LoggedInPermanent) + } + if (lockScreenState == LockScreenLockState.Locked) { + PermanentChild(permanentNavModel = permanentNavModel, navTarget = NavTarget.LockPermanent) } } } diff --git a/appnav/src/main/kotlin/io/element/android/appnav/MoveActivityToBackgroundBackHandler.kt b/appnav/src/main/kotlin/io/element/android/appnav/MoveActivityToBackgroundBackHandler.kt deleted file mode 100644 index 5d959f9464..0000000000 --- a/appnav/src/main/kotlin/io/element/android/appnav/MoveActivityToBackgroundBackHandler.kt +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2023 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.element.android.appnav - -import android.content.Context -import android.content.ContextWrapper -import androidx.activity.ComponentActivity -import androidx.activity.compose.BackHandler -import androidx.compose.runtime.Composable -import androidx.compose.ui.platform.LocalContext - -@Composable -fun MoveActivityToBackgroundBackHandler(enabled: Boolean = true) { - - fun Context.findActivity(): ComponentActivity? = when (this) { - is ComponentActivity -> this - is ContextWrapper -> baseContext.findActivity() - else -> null - } - - val context = LocalContext.current - BackHandler(enabled = enabled) { - context.findActivity()?.moveTaskToBack(false) - } -} diff --git a/features/lockscreen/api/src/main/kotlin/io/element/android/features/lockscreen/api/LockScreenService.kt b/features/lockscreen/api/src/main/kotlin/io/element/android/features/lockscreen/api/LockScreenService.kt index 2a628585a1..4e7a03c476 100644 --- a/features/lockscreen/api/src/main/kotlin/io/element/android/features/lockscreen/api/LockScreenService.kt +++ b/features/lockscreen/api/src/main/kotlin/io/element/android/features/lockscreen/api/LockScreenService.kt @@ -44,6 +44,12 @@ interface LockScreenService { fun isPinSetup(): Flow } +/** + * Check if the app is currently locked. + */ +val LockScreenService.isLocked: Boolean + get() = lockState.value == LockScreenLockState.Locked + /** * Makes sure the secure flag is set on the activity if the pin is setup. * @param activity the activity to set the flag on.