From 8252f737f973f89f62aaf71d5c6ed5c671190305 Mon Sep 17 00:00:00 2001
From: David Perez <david@livefront.com>
Date: Tue, 21 Jan 2025 14:55:15 -0600
Subject: [PATCH] Vault timeout logging

---
 .../data/platform/manager/AppStateManagerImpl.kt         | 3 +++
 .../bitwarden/data/vault/manager/VaultLockManagerImpl.kt | 9 +++++++++
 2 files changed, 12 insertions(+)

diff --git a/app/src/main/java/com/x8bit/bitwarden/data/platform/manager/AppStateManagerImpl.kt b/app/src/main/java/com/x8bit/bitwarden/data/platform/manager/AppStateManagerImpl.kt
index 96ee8ecdc..696384dbe 100644
--- a/app/src/main/java/com/x8bit/bitwarden/data/platform/manager/AppStateManagerImpl.kt
+++ b/app/src/main/java/com/x8bit/bitwarden/data/platform/manager/AppStateManagerImpl.kt
@@ -12,6 +12,7 @@ import com.x8bit.bitwarden.data.platform.manager.model.AppForegroundState
 import kotlinx.coroutines.flow.MutableStateFlow
 import kotlinx.coroutines.flow.StateFlow
 import kotlinx.coroutines.flow.asStateFlow
+import timber.log.Timber
 
 /**
  * Primary implementation of [AppStateManager].
@@ -38,10 +39,12 @@ class AppStateManagerImpl(
     private inner class AppForegroundObserver : DefaultLifecycleObserver {
         override fun onStart(owner: LifecycleOwner) {
             mutableAppForegroundStateFlow.value = AppForegroundState.FOREGROUNDED
+            Timber.e("App Foregrounded")
         }
 
         override fun onStop(owner: LifecycleOwner) {
             mutableAppForegroundStateFlow.value = AppForegroundState.BACKGROUNDED
+            Timber.e("App Backgrounded")
         }
     }
 
diff --git a/app/src/main/java/com/x8bit/bitwarden/data/vault/manager/VaultLockManagerImpl.kt b/app/src/main/java/com/x8bit/bitwarden/data/vault/manager/VaultLockManagerImpl.kt
index 9fadf13d1..1ddfb1093 100644
--- a/app/src/main/java/com/x8bit/bitwarden/data/vault/manager/VaultLockManagerImpl.kt
+++ b/app/src/main/java/com/x8bit/bitwarden/data/vault/manager/VaultLockManagerImpl.kt
@@ -50,6 +50,7 @@ import kotlinx.coroutines.flow.onCompletion
 import kotlinx.coroutines.flow.onEach
 import kotlinx.coroutines.flow.update
 import kotlinx.coroutines.launch
+import timber.log.Timber
 import kotlin.time.Duration.Companion.minutes
 
 /**
@@ -254,6 +255,7 @@ class VaultLockManagerImpl(
         if (!wasVaultUnlocked) {
             mutableVaultStateEventSharedFlow.tryEmit(VaultStateEvent.Unlocked(userId = userId))
         }
+        Timber.e("Vault Unlocked -- $userId")
     }
 
     private fun setVaultToLocked(userId: String) {
@@ -269,12 +271,14 @@ class VaultLockManagerImpl(
         if (!wasVaultLocked) {
             mutableVaultStateEventSharedFlow.tryEmit(VaultStateEvent.Locked(userId = userId))
         }
+        Timber.e("Vault Locked -- $userId")
     }
 
     private fun setVaultToUnlocking(userId: String) {
         mutableVaultUnlockDataStateFlow.update {
             it.update(userId, VaultUnlockData.Status.UNLOCKING)
         }
+        Timber.e("Vault Unlocking -- $userId")
     }
 
     private fun setVaultToNotUnlocking(userId: String) {
@@ -283,6 +287,7 @@ class VaultLockManagerImpl(
         mutableVaultUnlockDataStateFlow.update {
             it.update(userId, null)
         }
+        Timber.e("Vault Not Unlocking -- $userId")
     }
 
     private fun storeUserAutoUnlockKeyIfNecessary(userId: String) {
@@ -363,6 +368,7 @@ class VaultLockManagerImpl(
 
     private fun handleOnForeground() {
         val userId = activeUserId ?: return
+        Timber.e("Canceling Timeout Delay -- $userId")
         userIdTimerJobMap[userId]?.cancel()
     }
 
@@ -460,6 +466,7 @@ class VaultLockManagerImpl(
     ) {
         // Make sure to clear the now-active user's timeout job.
         userIdTimerJobMap[currentActiveUserId]?.cancel()
+        Timber.e("Canceling Timeout Delay -- $currentActiveUserId")
         // Check if the user's timeout action should be performed as we switch away.
         checkForVaultTimeout(
             userId = previousActiveUserId,
@@ -551,6 +558,7 @@ class VaultLockManagerImpl(
         vaultTimeoutAction: VaultTimeoutAction,
         delayInMs: Long,
     ) {
+        Timber.e("Setup Timeout Delay ($delayInMs) $vaultTimeoutAction -- $userId")
         userIdTimerJobMap[userId]?.cancel()
         userIdTimerJobMap[userId] = unconfinedScope.launch {
             delay(timeMillis = delayInMs)
@@ -568,6 +576,7 @@ class VaultLockManagerImpl(
     ) {
         when (vaultTimeoutAction) {
             VaultTimeoutAction.LOCK -> {
+                Timber.e("Locking for timeout -- $userId")
                 setVaultToLocked(userId = userId)
             }