From 4c60309d1dd12dee62149ad7a0e72952f6d23504 Mon Sep 17 00:00:00 2001 From: Stanislav Dmitrenko <7953703+avently@users.noreply.github.com> Date: Thu, 1 Sep 2022 22:26:30 +0300 Subject: [PATCH] android: screen lock with rotation (#1001) Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> --- .../java/chat/simplex/app/MainActivity.kt | 57 ++++++++++++------- .../java/chat/simplex/app/SimplexService.kt | 3 + 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/apps/android/app/src/main/java/chat/simplex/app/MainActivity.kt b/apps/android/app/src/main/java/chat/simplex/app/MainActivity.kt index 19dbaec96..22259fc3f 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/MainActivity.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/MainActivity.kt @@ -22,7 +22,6 @@ import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import androidx.fragment.app.FragmentActivity import androidx.lifecycle.* -import androidx.work.* import chat.simplex.app.model.ChatModel import chat.simplex.app.model.NtfManager import chat.simplex.app.ui.theme.SimpleButton @@ -38,18 +37,29 @@ import chat.simplex.app.views.newchat.connectViaUri import chat.simplex.app.views.newchat.withUriAction import chat.simplex.app.views.onboarding.* import kotlinx.coroutines.delay -import java.util.concurrent.TimeUnit -class MainActivity: FragmentActivity(), LifecycleEventObserver { +class MainActivity: FragmentActivity() { + companion object { + /** + * We don't want these values to be bound to Activity lifecycle since activities are changed often, for example, when a user + * clicks on new message in notification. In this case savedInstanceState will be null (this prevents restoring the values) + * See [SimplexService.onTaskRemoved] for another part of the logic which nullifies the values when app closed by the user + * */ + val userAuthorized = mutableStateOf(null) + val enteredBackground = mutableStateOf(null) + // Remember result and show it after orientation change + private val laFailed = mutableStateOf(false) + + fun clearAuthState() { + userAuthorized.value = null + enteredBackground.value = null + } + } private val vm by viewModels() private val chatController by lazy { (application as SimplexApp).chatController } - private val userAuthorized = mutableStateOf(null) - private val enteredBackground = mutableStateOf(null) - private val laFailed = mutableStateOf(false) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - ProcessLifecycleOwner.get().lifecycle.addObserver(this) // testJson() val m = vm.chatModel // When call ended and orientation changes, it re-process old intent, it's unneeded. @@ -83,20 +93,25 @@ class MainActivity: FragmentActivity(), LifecycleEventObserver { processIntent(intent, vm.chatModel) } - override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) { - withApi { - when (event) { - Lifecycle.Event.ON_STOP -> { - enteredBackground.value = elapsedRealtime() - } - Lifecycle.Event.ON_START -> { - val enteredBackgroundVal = enteredBackground.value - if (enteredBackgroundVal == null || elapsedRealtime() - enteredBackgroundVal >= 30 * 1e+3) { - runAuthenticate() - } - } - else -> {} - } + override fun onStart() { + super.onStart() + val enteredBackgroundVal = enteredBackground.value + if (enteredBackgroundVal == null || elapsedRealtime() - enteredBackgroundVal >= 30 * 1e+3) { + runAuthenticate() + } + } + + override fun onStop() { + super.onStop() + enteredBackground.value = elapsedRealtime() + } + + override fun onBackPressed() { + super.onBackPressed() + if (!onBackPressedDispatcher.hasEnabledCallbacks() && vm.chatModel.controller.appPrefs.performLA.get()) { + // When pressed Back and there is no one wants to process the back event, clear auth state to force re-auth on launch + clearAuthState() + laFailed.value = true } } diff --git a/apps/android/app/src/main/java/chat/simplex/app/SimplexService.kt b/apps/android/app/src/main/java/chat/simplex/app/SimplexService.kt index 134c1591e..7a4dd8ea7 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/SimplexService.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/SimplexService.kt @@ -151,6 +151,9 @@ class SimplexService: Service() { // re-schedules the task when "Clear recent apps" is pressed override fun onTaskRemoved(rootIntent: Intent) { + // Just to make sure that after restart of the app the user will need to re-authenticate + MainActivity.clearAuthState() + // If private notifications aren't enabled or battery optimization isn't disabled, we shouldn't restart the service if (!SimplexApp.context.allowToStartServiceAfterAppExit()) { return