From 6f3999016ffd257073b365adecf962a380c0cee3 Mon Sep 17 00:00:00 2001 From: Jake Fink Date: Wed, 19 Jan 2022 09:09:30 -0500 Subject: [PATCH] Supress lock and logout when showing fileswitcher on Android (#1626) * Supress lock and logout when showing fileswitcher on Android * convert suppress bool to delay long - move HandleVaultTimeoutAsync to vaultTimeoutService --- src/App/App.xaml.cs | 31 ++-------------- .../Pages/Vault/AttachmentsPageViewModel.cs | 7 ++++ src/Core/Abstractions/IVaultTimeoutService.cs | 1 + src/Core/Services/VaultTimeoutService.cs | 35 +++++++++++++------ 4 files changed, 36 insertions(+), 38 deletions(-) diff --git a/src/App/App.xaml.cs b/src/App/App.xaml.cs index ab31a0818..8c671bb65 100644 --- a/src/App/App.xaml.cs +++ b/src/App/App.xaml.cs @@ -176,6 +176,8 @@ namespace Bit.App if (Device.RuntimePlatform == Device.Android) { await _vaultTimeoutService.CheckVaultTimeoutAsync(); + // Reset delay on every start + _vaultTimeoutService.DelayLockAndLogoutMs = null; } _messagingService.Send("startEventTimer"); } @@ -208,7 +210,7 @@ namespace Bit.App private async Task SleptAsync() { - await HandleVaultTimeoutAsync(); + await _vaultTimeoutService.CheckVaultTimeoutAsync(); _messagingService.Send("stopEventTimer"); } @@ -279,33 +281,6 @@ namespace Bit.App } } - private async Task HandleVaultTimeoutAsync() - { - if (await _vaultTimeoutService.IsLockedAsync()) - { - return; - } - var authed = await _userService.IsAuthenticatedAsync(); - if (!authed) - { - return; - } - var vaultTimeout = await _storageService.GetAsync(Constants.VaultTimeoutKey); - vaultTimeout = vaultTimeout.GetValueOrDefault(-1); - if (vaultTimeout == 0) - { - var action = await _storageService.GetAsync(Constants.VaultTimeoutActionKey); - if (action == "logOut") - { - await _vaultTimeoutService.LogOutAsync(); - } - else - { - await _vaultTimeoutService.LockAsync(true); - } - } - } - private async Task ClearCacheIfNeededAsync() { var lastClear = await _storageService.GetAsync(Constants.LastFileCacheClearKey); diff --git a/src/App/Pages/Vault/AttachmentsPageViewModel.cs b/src/App/Pages/Vault/AttachmentsPageViewModel.cs index bed4ecf85..08f859202 100644 --- a/src/App/Pages/Vault/AttachmentsPageViewModel.cs +++ b/src/App/Pages/Vault/AttachmentsPageViewModel.cs @@ -18,6 +18,7 @@ namespace Bit.App.Pages private readonly ICipherService _cipherService; private readonly ICryptoService _cryptoService; private readonly IUserService _userService; + private readonly IVaultTimeoutService _timeoutService; private readonly IPlatformUtilsService _platformUtilsService; private CipherView _cipher; private Cipher _cipherDomain; @@ -33,6 +34,7 @@ namespace Bit.App.Pages _cryptoService = ServiceContainer.Resolve("cryptoService"); _platformUtilsService = ServiceContainer.Resolve("platformUtilsService"); _userService = ServiceContainer.Resolve("userService"); + _timeoutService = ServiceContainer.Resolve("vaultTimeoutService"); Attachments = new ExtendedObservableCollection(); DeleteAttachmentCommand = new Command(DeleteAsync); PageTitle = AppResources.Attachments; @@ -135,6 +137,11 @@ namespace Bit.App.Pages public async Task ChooseFileAsync() { + // Prevent Android from locking if vault timeout set to "immediate" + if (Device.RuntimePlatform == Device.Android) + { + _timeoutService.DelayLockAndLogoutMs = 60000; + } await _deviceActionService.SelectFileAsync(); } diff --git a/src/Core/Abstractions/IVaultTimeoutService.cs b/src/Core/Abstractions/IVaultTimeoutService.cs index 1c780a169..bcdb776a8 100644 --- a/src/Core/Abstractions/IVaultTimeoutService.cs +++ b/src/Core/Abstractions/IVaultTimeoutService.cs @@ -8,6 +8,7 @@ namespace Bit.Core.Abstractions { EncString PinProtectedKey { get; set; } bool BiometricLocked { get; set; } + long? DelayLockAndLogoutMs { get; set; } Task CheckVaultTimeoutAsync(); Task ClearAsync(); diff --git a/src/Core/Services/VaultTimeoutService.cs b/src/Core/Services/VaultTimeoutService.cs index 013611374..b6768e602 100644 --- a/src/Core/Services/VaultTimeoutService.cs +++ b/src/Core/Services/VaultTimeoutService.cs @@ -58,6 +58,7 @@ namespace Bit.Core.Services public EncString PinProtectedKey { get; set; } = null; public bool BiometricLocked { get; set; } = true; + public long? DelayLockAndLogoutMs { get; set; } public async Task IsLockedAsync() { @@ -93,25 +94,39 @@ namespace Bit.Core.Services { return; } + if (vaultTimeoutMinutes == 0 && !DelayLockAndLogoutMs.HasValue) + { + await LockOrLogout(); + } var lastActiveTime = await _storageService.GetAsync(Constants.LastActiveTimeKey); if (lastActiveTime == null) { return; } var diffMs = _platformUtilsService.GetActiveTime() - lastActiveTime; + if (DelayLockAndLogoutMs.HasValue && diffMs < DelayLockAndLogoutMs) + { + return; + } var vaultTimeoutMs = vaultTimeoutMinutes * 60000; if (diffMs >= vaultTimeoutMs) { - // Pivot based on saved action - var action = await _storageService.GetAsync(Constants.VaultTimeoutActionKey); - if (action == "logOut") - { - await LogOutAsync(); - } - else - { - await LockAsync(true); - } + await LockOrLogout(); + } + + } + + private async Task LockOrLogout() + { + // Pivot based on saved action + var action = await _storageService.GetAsync(Constants.VaultTimeoutActionKey); + if (action == "logOut") + { + await LogOutAsync(); + } + else + { + await LockAsync(true); } }