Skip to content

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

Tweak pin unlock flow for Android (#1340)

Leendert de Borstdd1d6e64e1e3
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/MainActivity.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/MainActivity.kt
index c350e91ee..4e8cb2716 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/MainActivity.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/MainActivity.kt
@@ -37,7 +37,6 @@ class MainActivity : ReactActivity() {
override fun onResume() {
super.onResume()
// Reapply system bar configuration when app resumes
- // This ensures our settings persist even if other code tries to override them
configureSystemBars()
}
@@ -104,16 +103,64 @@ class MainActivity : ReactActivity() {
}
/**
- * Handle activity results - forward to NativeVaultManager for PIN unlock.
+ * Handle activity results - specifically for PIN unlock.
*/
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
- // Forward result to NativeVaultManager to handle PIN unlock
- val reactInstanceManager = reactNativeHost.reactInstanceManager
- val nativeModule = reactInstanceManager.currentReactContext
- ?.getNativeModule(net.aliasvault.app.nativevaultmanager.NativeVaultManager::class.java)
+ // Handle PIN unlock results directly
+ if (requestCode == net.aliasvault.app.nativevaultmanager.NativeVaultManager.PIN_UNLOCK_REQUEST_CODE) {
+ handlePinUnlockResult(resultCode, data)
+ }
+ }
+
+ /**
+ * Handle PIN unlock result directly without going through React context.
+ * This avoids race conditions with React context initialization.
+ * @param resultCode The result code from the PIN unlock activity.
+ * @param data The intent data containing the encryption key.
+ */
+ private fun handlePinUnlockResult(resultCode: Int, data: Intent?) {
+ val promise = net.aliasvault.app.nativevaultmanager.NativeVaultManager.pendingActivityResultPromise
+ net.aliasvault.app.nativevaultmanager.NativeVaultManager.pendingActivityResultPromise = null
+
+ if (promise == null) {
+ return
+ }
+
+ val vaultStore = net.aliasvault.app.vaultstore.VaultStore.getInstance(
+ net.aliasvault.app.vaultstore.keystoreprovider.AndroidKeystoreProvider(this) { null },
+ net.aliasvault.app.vaultstore.storageprovider.AndroidStorageProvider(this),
+ )
- nativeModule?.handleActivityResult(requestCode, resultCode, data)
+ when (resultCode) {
+ net.aliasvault.app.pinunlock.PinUnlockActivity.RESULT_SUCCESS -> {
+ val encryptionKeyBase64 = data?.getStringExtra(
+ net.aliasvault.app.pinunlock.PinUnlockActivity.EXTRA_ENCRYPTION_KEY,
+ )
+
+ if (encryptionKeyBase64 == null) {
+ promise.reject("UNLOCK_ERROR", "Failed to get encryption key from PIN unlock", null)
+ return
+ }
+
+ try {
+ vaultStore.storeEncryptionKey(encryptionKeyBase64)
+ vaultStore.unlockVault()
+ promise.resolve(null)
+ } catch (e: Exception) {
+ promise.reject("UNLOCK_ERROR", "Failed to unlock vault: ${e.message}", e)
+ }
+ }
+ net.aliasvault.app.pinunlock.PinUnlockActivity.RESULT_CANCELLED -> {
+ promise.reject("USER_CANCELLED", "User cancelled PIN unlock", null)
+ }
+ net.aliasvault.app.pinunlock.PinUnlockActivity.RESULT_PIN_DISABLED -> {
+ promise.reject("PIN_DISABLED", "PIN was disabled", null)
+ }
+ else -> {
+ promise.reject("UNKNOWN_ERROR", "Unknown error in PIN unlock", null)
+ }
+ }
}
}
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 d8d815fe2..4dba0c397 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
@@ -55,7 +55,15 @@ class NativeVaultManager(reactContext: ReactApplicationContext) :
/**
* Request code for PIN unlock activity.
*/
- private const val PIN_UNLOCK_REQUEST_CODE = 1001
+ const val PIN_UNLOCK_REQUEST_CODE = 1001
+
+ /**
+ * Static holder for the pending promise from showPinUnlockUI.
+ * This allows MainActivity to resolve/reject the promise directly without
+ * depending on React context availability.
+ */
+ @Volatile
+ var pendingActivityResultPromise: Promise? = null
}
private val vaultStore = VaultStore.getInstance(
@@ -1370,66 +1378,12 @@ class NativeVaultManager(reactContext: ReactApplicationContext) :
return
}
- // Store promise for later resolution
- pendingPinUnlockPromise = promise
+ // Store promise in static companion object so MainActivity can resolve it directly
+ // This avoids race conditions with React context availability
+ pendingActivityResultPromise = promise
// Launch PIN unlock activity
val intent = Intent(activity, net.aliasvault.app.pinunlock.PinUnlockActivity::class.java)
activity.startActivityForResult(intent, PIN_UNLOCK_REQUEST_CODE)
}
-
- // Store pending promise
- private var pendingPinUnlockPromise: Promise? = null
-
- /**
- * Handle activity result from PIN unlock.
- */
- fun handleActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
- if (requestCode == PIN_UNLOCK_REQUEST_CODE) {
- val promise = pendingPinUnlockPromise
- pendingPinUnlockPromise = null
-
- if (promise == null) {
- Log.w(TAG, "No pending promise for PIN unlock result")
- return
- }
-
- when (resultCode) {
- net.aliasvault.app.pinunlock.PinUnlockActivity.RESULT_SUCCESS -> {
- // Get encryption key from result
- val encryptionKeyBase64 = data?.getStringExtra(
- net.aliasvault.app.pinunlock.PinUnlockActivity.EXTRA_ENCRYPTION_KEY,
- )
-
- if (encryptionKeyBase64 == null) {
- promise.reject("UNLOCK_ERROR", "Failed to get encryption key from PIN unlock", null)
- return
- }
-
- try {
- // Store encryption key in memory
- vaultStore.storeEncryptionKey(encryptionKeyBase64)
-
- // Unlock the vault
- vaultStore.unlockVault()
-
- // Success
- promise.resolve(null)
- } catch (e: Exception) {
- Log.e(TAG, "Error after PIN unlock", e)
- promise.reject("UNLOCK_ERROR", "Failed to unlock vault: ${e.message}", e)
- }
- }
- net.aliasvault.app.pinunlock.PinUnlockActivity.RESULT_CANCELLED -> {
- promise.reject("USER_CANCELLED", "User cancelled PIN unlock", null)
- }
- net.aliasvault.app.pinunlock.PinUnlockActivity.RESULT_PIN_DISABLED -> {
- promise.reject("PIN_DISABLED", "PIN was disabled", null)
- }
- else -> {
- promise.reject("UNKNOWN_ERROR", "Unknown error in PIN unlock", null)
- }
- }
- }
- }
}
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/pinunlock/PinUnlockActivity.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/pinunlock/PinUnlockActivity.kt
index eb8eec6e1..ef0310dbc 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/pinunlock/PinUnlockActivity.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/pinunlock/PinUnlockActivity.kt
@@ -60,6 +60,22 @@ class PinUnlockActivity : AppCompatActivity() {
// Keep screen on during PIN entry
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
+ // Apply window insets to the root layout for safe area
+ findViewById<android.view.View>(android.R.id.content).setOnApplyWindowInsetsListener { view, insets ->
+ val cancelButton = findViewById<Button>(R.id.cancelButton)
+ val systemBarsInsets = insets.systemWindowInsets
+
+ // Apply top inset to cancel button
+ cancelButton.setPadding(
+ cancelButton.paddingLeft,
+ systemBarsInsets.top + cancelButton.paddingTop,
+ cancelButton.paddingRight,
+ cancelButton.paddingBottom,
+ )
+
+ insets
+ }
+
// Initialize VaultStore
vaultStore = VaultStore.getInstance(
net.aliasvault.app.vaultstore.keystoreprovider.AndroidKeystoreProvider(this) { null },
diff --git a/apps/mobile-app/android/app/src/main/res/layout/activity_pin_unlock.xml b/apps/mobile-app/android/app/src/main/res/layout/activity_pin_unlock.xml
index 84282d1b6..d9f538df1 100644
--- a/apps/mobile-app/android/app/src/main/res/layout/activity_pin_unlock.xml
+++ b/apps/mobile-app/android/app/src/main/res/layout/activity_pin_unlock.xml
@@ -15,8 +15,13 @@
android:text="@string/pin_cancel"
android:textColor="@color/primary"
style="?android:attr/borderlessButtonStyle"
- android:layout_marginTop="20dp"
- android:layout_marginEnd="20dp"
+ android:layout_marginTop="8dp"
+ android:layout_marginEnd="8dp"
+ android:paddingStart="16dp"
+ android:paddingEnd="16dp"
+ android:paddingTop="12dp"
+ android:paddingBottom="12dp"
+ android:minWidth="64dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent" />