Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Update Android lifecycle tracking to be more robust with introduction of more native activities (#1776)

Leendert de Borst0be3e4019de6
diff --git a/apps/mobile-app/android/app/build.gradle b/apps/mobile-app/android/app/build.gradle
index 05f49e986..65958c961 100644
--- a/apps/mobile-app/android/app/build.gradle
+++ b/apps/mobile-app/android/app/build.gradle
@@ -195,6 +195,7 @@ dependencies {
// Add Fragment and ViewModel dependencies for fragment-based UI
implementation("androidx.fragment:fragment-ktx:1.6.2")
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2")
+ implementation("androidx.lifecycle:lifecycle-process:2.6.2")
// Add vector drawable support for SVG
implementation("com.caverock:androidsvg-aar:1.4")
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/MainApplication.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/MainApplication.kt
index 3dc5a361b..7f87479fc 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/MainApplication.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/MainApplication.kt
@@ -2,6 +2,9 @@ package net.aliasvault.app
import android.app.Application
import android.content.res.Configuration
+import androidx.lifecycle.DefaultLifecycleObserver
+import androidx.lifecycle.LifecycleOwner
+import androidx.lifecycle.ProcessLifecycleOwner
import com.facebook.react.PackageList
import com.facebook.react.ReactApplication
import com.facebook.react.ReactHost
@@ -14,6 +17,9 @@ import com.facebook.soloader.SoLoader
import expo.modules.ApplicationLifecycleDispatcher
import expo.modules.ReactNativeHostWrapper
import net.aliasvault.app.nativevaultmanager.NativeVaultManagerPackage
+import net.aliasvault.app.vaultstore.VaultStore
+import net.aliasvault.app.vaultstore.keystoreprovider.AndroidKeystoreProvider
+import net.aliasvault.app.vaultstore.storageprovider.AndroidStorageProvider
/**
* The main application class.
@@ -54,6 +60,42 @@ class MainApplication : Application(), ReactApplication {
load()
}
ApplicationLifecycleDispatcher.onApplicationCreate(this)
+
+ // Setup process lifecycle observer for auto-lock functionality
+ setupProcessLifecycleObserver()
+ }
+
+ /**
+ * Setup process-level lifecycle observer to track when the app backgrounds/foregrounds.
+ */
+ private fun setupProcessLifecycleObserver() {
+ ProcessLifecycleOwner.get().lifecycle.addObserver(object : DefaultLifecycleObserver {
+ override fun onStop(owner: LifecycleOwner) {
+ // Called when app goes to background (all activities stopped)
+ try {
+ val vaultStore = VaultStore.getInstance(
+ AndroidKeystoreProvider(this@MainApplication) { null },
+ AndroidStorageProvider(this@MainApplication),
+ )
+ vaultStore.vaultAuth.onAppBackgrounded()
+ } catch (e: Exception) {
+ android.util.Log.e("MainApplication", "Error handling app background", e)
+ }
+ }
+
+ override fun onStart(owner: LifecycleOwner) {
+ // Called when app comes to foreground (at least one activity visible)
+ try {
+ val vaultStore = VaultStore.getInstance(
+ AndroidKeystoreProvider(this@MainApplication) { null },
+ AndroidStorageProvider(this@MainApplication),
+ )
+ vaultStore.vaultAuth.onAppForegrounded()
+ } catch (e: Exception) {
+ android.util.Log.e("MainApplication", "Error handling app foreground", e)
+ }
+ }
+ })
}
override fun onConfigurationChanged(newConfig: Configuration) {
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/nativevaultmanager/NativeVaultManager.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/nativevaultmanager/NativeVaultManager.kt
index 035702cd3..186d12f2b 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/nativevaultmanager/NativeVaultManager.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/nativevaultmanager/NativeVaultManager.kt
@@ -8,7 +8,6 @@ import android.util.Log
import androidx.core.net.toUri
import androidx.fragment.app.FragmentActivity
import com.facebook.react.bridge.Arguments
-import com.facebook.react.bridge.LifecycleEventListener
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactMethod
@@ -40,7 +39,7 @@ import org.json.JSONObject
@Suppress("TooManyFunctions") // Required by React Native TurboModule interface
@ReactModule(name = NativeVaultManager.NAME)
class NativeVaultManager(reactContext: ReactApplicationContext) :
- NativeVaultManagerSpec(reactContext), TurboModule, LifecycleEventListener {
+ NativeVaultManagerSpec(reactContext), TurboModule {
companion object {
/**
@@ -112,34 +111,6 @@ class NativeVaultManager(reactContext: ReactApplicationContext) :
private val webApiService = WebApiService(reactContext)
- init {
- // Register for lifecycle callbacks
- reactContext.addLifecycleEventListener(this)
- }
-
- /**
- * Called when the app enters the background.
- */
- override fun onHostPause() {
- Log.d(TAG, "App entered background")
- vaultStore.onAppBackgrounded()
- }
-
- /**
- * Called when the app enters the foreground.
- */
- override fun onHostResume() {
- Log.d(TAG, "App entered foreground")
- vaultStore.onAppForegrounded()
- }
-
- /**
- * Called when the app is destroyed.
- */
- override fun onHostDestroy() {
- // Not needed
- }
-
/**
* Get the name of the module.
* @return The name of the module
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultAuth.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultAuth.kt
index 9f06da94c..14bcb2a20 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultAuth.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultAuth.kt
@@ -1,8 +1,9 @@
package net.aliasvault.app.vaultstore
-import android.os.Handler
import android.util.Log
import net.aliasvault.app.vaultstore.storageprovider.StorageProvider
+import java.util.Timer
+import java.util.TimerTask
/**
* Handles authentication methods and auto-lock functionality for the vault.
@@ -15,8 +16,8 @@ class VaultAuth(
private const val TAG = "VaultAuth"
}
- private var autoLockHandler: Handler? = null
- private var autoLockRunnable: Runnable? = null
+ private var clearCacheTimer: Timer? = null
+ private var backgroundTimestamp: Long? = null
// region Authentication Methods
@@ -57,35 +58,70 @@ class VaultAuth(
// region Background/Foreground Handling
/**
- * Called when the app enters the background.
+ * Called when the app process enters the background (all activities are stopped).
+ * Starts a timer that will clear decrypted vault from memory after the configured auto-lock timeout.
*/
fun onAppBackgrounded() {
- Log.d(TAG, "App entered background, starting auto-lock timer with ${getAutoLockTimeout()}s")
- if (getAutoLockTimeout() > 0) {
- autoLockRunnable?.let { autoLockHandler?.removeCallbacks(it) }
-
- autoLockRunnable = Runnable {
- Log.d(TAG, "Auto-lock timer fired, clearing cache")
- onClearCache()
+ val timeout = getAutoLockTimeout()
+ Log.d(TAG, "App entered background, starting auto-lock timer with ${timeout}s")
+
+ // Cancel any existing timer
+ clearCacheTimer?.cancel()
+ clearCacheTimer = null
+
+ // Record when we backgrounded
+ backgroundTimestamp = System.currentTimeMillis()
+
+ if (timeout > 0) {
+ clearCacheTimer = Timer("VaultAutoLock", true).apply {
+ schedule(
+ object : TimerTask() {
+ override fun run() {
+ Log.d(TAG, "Auto-lock timer fired, clearing cache")
+ onClearCache()
+ }
+ },
+ timeout.toLong() * 1000,
+ )
}
- autoLockHandler?.postDelayed(autoLockRunnable!!, getAutoLockTimeout().toLong() * 1000)
}
}
/**
- * Called when the app enters the foreground.
+ * Called when the app process enters the foreground (at least one activity is visible).
+ * Cancels the auto-lock timer if it hasn't fired yet, or clears the memory if the timeout
+ * has already elapsed while the app was backgrounded.
*/
fun onAppForegrounded() {
- Log.d(TAG, "App entered foreground, canceling auto-lock timer")
- autoLockRunnable?.let { autoLockHandler?.removeCallbacks(it) }
- autoLockRunnable = null
+ Log.d(TAG, "App will enter foreground, checking auto-lock timer")
+
+ val timeout = getAutoLockTimeout()
+ val bgTime = backgroundTimestamp
+
+ // Check if timer has already elapsed while we were in background
+ if (timeout > 0 && bgTime != null) {
+ val elapsedSeconds = (System.currentTimeMillis() - bgTime) / 1000
+ if (elapsedSeconds >= timeout) {
+ Log.d(TAG, "Timer elapsed ($elapsedSeconds seconds >= $timeout seconds), clearing cache now")
+ onClearCache()
+ }
+ }
+
+ // Cancel and clear the timer
+ clearCacheTimer?.cancel()
+ clearCacheTimer = null
+ backgroundTimestamp = null
+
+ Log.d(TAG, "Auto-lock timer canceled")
}
/**
- * Set the auto-lock handler.
+ * Clean up resources when VaultAuth is destroyed.
*/
- fun setAutoLockHandler(handler: Handler) {
- this.autoLockHandler = handler
+ fun cleanup() {
+ clearCacheTimer?.cancel()
+ clearCacheTimer = null
+ backgroundTimestamp = null
}
// endregion
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultStore.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultStore.kt
index 6ce157456..75daa0a62 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultStore.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultStore.kt
@@ -1,7 +1,5 @@
package net.aliasvault.app.vaultstore
-import android.os.Handler
-import android.os.Looper
import android.util.Log
import io.requery.android.database.sqlite.SQLiteDatabase
import kotlinx.coroutines.suspendCancellableCoroutine
@@ -113,43 +111,16 @@ class VaultStore(
get() = databaseComponent.dbConnection
/**
- * Internal accessor for database connection (legacy name).
+ * Internal accessor for database connection.
*/
internal val dbConnection: SQLiteDatabase?
get() = databaseComponent.dbConnection
/**
- * Internal accessor for auto-lock handler.
+ * Internal accessor for VaultAuth.
*/
- internal var autoLockHandler: Handler? = null
-
- /**
- * Internal accessor for auto-lock runnable.
- */
- internal var autoLockRunnable: Runnable? = null
-
- // endregion
-
- init {
- autoLockHandler = Handler(Looper.getMainLooper())
- auth.setAutoLockHandler(autoLockHandler!!)
- }
-
- // region Lifecycle Methods
-
- /**
- * Called when the app enters the background.
- */
- fun onAppBackgrounded() {
- auth.onAppBackgrounded()
- }
-
- /**
- * Called when the app enters the foreground.
- */
- fun onAppForegrounded() {
- auth.onAppForegrounded()
- }
+ internal val vaultAuth: VaultAuth
+ get() = auth
// endregion