@@ -305,29 +305,40 @@ class VaultLockManagerImpl(
305
305
}
306
306
307
307
private fun observeAppCreationChanges () {
308
+ var isFirstCreated = true
308
309
appStateManager
309
310
.appCreatedStateFlow
310
311
.onEach { appCreationState ->
311
312
when (appCreationState) {
312
- AppCreationState .CREATED -> Unit
313
- AppCreationState .DESTROYED -> handleOnDestroyed()
313
+ is AppCreationState .Created -> {
314
+ handleOnCreated(
315
+ createdForAutofill = appCreationState.isAutoFill,
316
+ isFirstCreated = isFirstCreated,
317
+ )
318
+ isFirstCreated = false
319
+ }
320
+
321
+ AppCreationState .Destroyed -> Unit
314
322
}
315
323
}
316
324
.launchIn(unconfinedScope)
317
325
}
318
326
319
- private fun handleOnDestroyed () {
320
- activeUserId?.let { userId ->
321
- checkForVaultTimeout(
322
- userId = userId,
323
- checkTimeoutReason = CheckTimeoutReason .APP_RESTARTED ,
324
- )
325
- }
327
+ private fun handleOnCreated (
328
+ createdForAutofill : Boolean ,
329
+ isFirstCreated : Boolean ,
330
+ ) {
331
+ val userId = activeUserId ? : return
332
+ checkForVaultTimeout(
333
+ userId = userId,
334
+ checkTimeoutReason = CheckTimeoutReason .AppCreated (
335
+ firstTimeCreation = isFirstCreated,
336
+ createdForAutofill = createdForAutofill,
337
+ ),
338
+ )
326
339
}
327
340
328
341
private fun observeAppForegroundChanges () {
329
- var isFirstForeground = true
330
-
331
342
appStateManager
332
343
.appForegroundStateFlow
333
344
.onEach { appForegroundState ->
@@ -336,10 +347,7 @@ class VaultLockManagerImpl(
336
347
handleOnBackground()
337
348
}
338
349
339
- AppForegroundState .FOREGROUNDED -> {
340
- handleOnForeground(isFirstForeground = isFirstForeground)
341
- isFirstForeground = false
342
- }
350
+ AppForegroundState .FOREGROUNDED -> handleOnForeground()
343
351
}
344
352
}
345
353
.launchIn(unconfinedScope)
@@ -349,19 +357,13 @@ class VaultLockManagerImpl(
349
357
val userId = activeUserId ? : return
350
358
checkForVaultTimeout(
351
359
userId = userId,
352
- checkTimeoutReason = CheckTimeoutReason .APP_BACKGROUNDED ,
360
+ checkTimeoutReason = CheckTimeoutReason .AppBackgrounded ,
353
361
)
354
362
}
355
363
356
- private fun handleOnForeground (isFirstForeground : Boolean ) {
364
+ private fun handleOnForeground () {
357
365
val userId = activeUserId ? : return
358
366
userIdTimerJobMap[userId]?.cancel()
359
- if (isFirstForeground) {
360
- checkForVaultTimeout(
361
- userId = userId,
362
- checkTimeoutReason = CheckTimeoutReason .APP_RESTARTED ,
363
- )
364
- }
365
367
}
366
368
367
369
private fun observeUserSwitchingChanges () {
@@ -461,7 +463,7 @@ class VaultLockManagerImpl(
461
463
// Check if the user's timeout action should be performed as we switch away.
462
464
checkForVaultTimeout(
463
465
userId = previousActiveUserId,
464
- checkTimeoutReason = CheckTimeoutReason .USER_CHANGED ,
466
+ checkTimeoutReason = CheckTimeoutReason .UserChanged ,
465
467
)
466
468
}
467
469
@@ -491,27 +493,38 @@ class VaultLockManagerImpl(
491
493
492
494
VaultTimeout .OnAppRestart -> {
493
495
// If this is an app restart, trigger the timeout action; otherwise ignore.
494
- if (checkTimeoutReason == CheckTimeoutReason .APP_RESTARTED ) {
495
- // On restart the vault should be locked already but we may need to soft-logout
496
- // the user.
497
- handleTimeoutAction(userId = userId, vaultTimeoutAction = vaultTimeoutAction)
496
+ if (checkTimeoutReason is CheckTimeoutReason .AppCreated ) {
497
+ // We need to check the timeout action on the first time creation no matter what
498
+ // for all subsequent creations we should check if this is for autofill and
499
+ // and if it is we skip checking the timeout action.
500
+ if (
501
+ checkTimeoutReason.firstTimeCreation ||
502
+ ! checkTimeoutReason.createdForAutofill
503
+ ) {
504
+ handleTimeoutAction(
505
+ userId = userId,
506
+ vaultTimeoutAction = vaultTimeoutAction,
507
+ )
508
+ }
498
509
}
499
510
}
500
511
501
512
else -> {
502
513
when (checkTimeoutReason) {
503
514
// Always preform the timeout action on app restart to ensure the user is
504
515
// in the correct state.
505
- CheckTimeoutReason .APP_RESTARTED -> {
506
- handleTimeoutAction(
507
- userId = userId,
508
- vaultTimeoutAction = vaultTimeoutAction,
509
- )
516
+ is CheckTimeoutReason .AppCreated -> {
517
+ if (checkTimeoutReason.firstTimeCreation) {
518
+ handleTimeoutAction(
519
+ userId = userId,
520
+ vaultTimeoutAction = vaultTimeoutAction,
521
+ )
522
+ }
510
523
}
511
524
512
525
// User no longer active or engaging with the app.
513
- CheckTimeoutReason .APP_BACKGROUNDED ,
514
- CheckTimeoutReason .USER_CHANGED ,
526
+ CheckTimeoutReason .AppBackgrounded ,
527
+ CheckTimeoutReason .UserChanged ,
515
528
-> {
516
529
handleTimeoutActionWithDelay(
517
530
userId = userId,
@@ -589,11 +602,29 @@ class VaultLockManagerImpl(
589
602
}
590
603
591
604
/* *
592
- * Helper enum that indicates the reason we are checking for timeout.
605
+ * Helper sealed class which denotes the reason to check the vault timeout.
593
606
*/
594
- private enum class CheckTimeoutReason {
595
- APP_BACKGROUNDED ,
596
- APP_RESTARTED ,
597
- USER_CHANGED ,
607
+ private sealed class CheckTimeoutReason {
608
+ /* *
609
+ * Indicates the app has been backgrounded but is still running.
610
+ */
611
+ data object AppBackgrounded : CheckTimeoutReason ()
612
+
613
+ /* *
614
+ * Indicates the app has entered a Created state.
615
+ *
616
+ * @param firstTimeCreation if this is the first time the process is being created.
617
+ * @param createdForAutofill if the the creation event is due to an activity being launched
618
+ * for autofill.
619
+ */
620
+ data class AppCreated (
621
+ val firstTimeCreation : Boolean ,
622
+ val createdForAutofill : Boolean ,
623
+ ) : CheckTimeoutReason()
624
+
625
+ /* *
626
+ * Indicates that the current user has changed.
627
+ */
628
+ data object UserChanged : CheckTimeoutReason ()
598
629
}
599
630
}
0 commit comments