From c67250da2d3fed8159def56f4594203d112ddae5 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 28 Dec 2017 21:14:30 -0500 Subject: [PATCH] base lock timer off of Stopwatch class --- .../Services/IAppSettingsService.cs | 2 - src/App/Abstractions/Services/ILockService.cs | 1 - src/App/App.cs | 2 - src/App/Services/AppSettingsService.cs | 24 ------- src/App/Services/LockService.cs | 71 +++++-------------- 5 files changed, 17 insertions(+), 83 deletions(-) diff --git a/src/App/Abstractions/Services/IAppSettingsService.cs b/src/App/Abstractions/Services/IAppSettingsService.cs index 3780c5672..f10fe8a5f 100644 --- a/src/App/Abstractions/Services/IAppSettingsService.cs +++ b/src/App/Abstractions/Services/IAppSettingsService.cs @@ -5,8 +5,6 @@ namespace Bit.App.Abstractions public interface IAppSettingsService { bool Locked { get; set; } - string LockTimerId { get; set; } - double LastActivityLockTime { get; set; } DateTime LastActivity { get; set; } DateTime LastCacheClear { get; set; } bool AutofillPersistNotification { get; set; } diff --git a/src/App/Abstractions/Services/ILockService.cs b/src/App/Abstractions/Services/ILockService.cs index 08e8ba474..160e65f31 100644 --- a/src/App/Abstractions/Services/ILockService.cs +++ b/src/App/Abstractions/Services/ILockService.cs @@ -10,6 +10,5 @@ namespace Bit.App.Abstractions Task GetLockTypeAsync(bool forceLock); Task CheckLockAsync(bool forceLock); bool TopPageIsLock(); - void StartLockTimer(); } } \ No newline at end of file diff --git a/src/App/App.cs b/src/App/App.cs index a7add896b..dcd3ecd2c 100644 --- a/src/App/App.cs +++ b/src/App/App.cs @@ -86,8 +86,6 @@ namespace Bit.App await Task.Run(() => FullSyncAsync()).ConfigureAwait(false); }); } - - _lockService.StartLockTimer(); } protected async override void OnStart() diff --git a/src/App/Services/AppSettingsService.cs b/src/App/Services/AppSettingsService.cs index a60d524bd..be2bc2f01 100644 --- a/src/App/Services/AppSettingsService.cs +++ b/src/App/Services/AppSettingsService.cs @@ -26,30 +26,6 @@ namespace Bit.App.Services } } - public string LockTimerId - { - get - { - return _settings.GetValueOrDefault(Constants.LockTimerId, null); - } - set - { - _settings.AddOrUpdateValue(Constants.LockTimerId, value); - } - } - - public double LastActivityLockTime - { - get - { - return _settings.GetValueOrDefault(Constants.LastActivityLockTime, Double.MinValue); - } - set - { - _settings.AddOrUpdateValue(Constants.LastActivityLockTime, value); - } - } - public DateTime LastActivity { get diff --git a/src/App/Services/LockService.cs b/src/App/Services/LockService.cs index d295a62f9..b4a8dc3c6 100644 --- a/src/App/Services/LockService.cs +++ b/src/App/Services/LockService.cs @@ -8,6 +8,7 @@ using Bit.App.Controls; using Bit.App.Pages; using Xamarin.Forms; using System.Linq; +using System.Diagnostics; namespace Bit.App.Services { @@ -17,7 +18,7 @@ namespace Bit.App.Services private readonly IAppSettingsService _appSettings; private readonly IAuthService _authService; private readonly IFingerprint _fingerprint; - private string _timerId = null; + private Stopwatch _stopwatch = null; public LockService( ISettings settings, @@ -31,58 +32,31 @@ namespace Bit.App.Services _fingerprint = fingerprint; } - public double CurrentLockTime { get; set; } - public void UpdateLastActivity() { - if(_appSettings.Locked) - { - return; - } - - _appSettings.LastActivityLockTime = CurrentLockTime; + _stopwatch?.Restart(); } public async Task GetLockTypeAsync(bool forceLock) { - var returnNone = false; - // Only lock if they are logged in if(!_authService.IsAuthenticated) { - returnNone = true; + return LockType.None; } + // Are we forcing a lock? (i.e. clicking a button to lock the app manually, immediately) - else if(!forceLock && !_appSettings.Locked) + if(!forceLock && !_appSettings.Locked) { // Lock seconds tells if they want to lock the app or not var lockSeconds = _settings.GetValueOrDefault(Constants.SettingLockSeconds, 60 * 15); - if(lockSeconds == -1) - { - returnNone = true; - } - // Validate timer instance - else if(_appSettings.LockTimerId != null && _timerId == _appSettings.LockTimerId) - { - // Has it been longer than lockSeconds since the last time the app was used? - var now = CurrentLockTime; - var elapsedSeconds = (now - _appSettings.LastActivityLockTime) / 1000; - if(now >= _appSettings.LastActivityLockTime && elapsedSeconds < lockSeconds) - { - returnNone = true; - } - } - } + var neverLock = lockSeconds == -1; - // Set the new lock timer id - if(_timerId != null) - { - _appSettings.LockTimerId = _timerId; - } - - if(returnNone) - { - return LockType.None; + // Has it been longer than lockSeconds since the last time the app was used? + if(neverLock || (_stopwatch != null && _stopwatch.Elapsed.TotalSeconds < lockSeconds)) + { + return LockType.None; + } } // What method are we using to unlock? @@ -116,6 +90,11 @@ namespace Bit.App.Services return; } + if(_stopwatch == null) + { + _stopwatch = Stopwatch.StartNew(); + } + _appSettings.Locked = true; switch(lockType) { @@ -154,21 +133,5 @@ namespace Bit.App.Services return false; } - - public void StartLockTimer() - { - if(_timerId != null) - { - return; - } - - _timerId = Guid.NewGuid().ToString(); - var interval = TimeSpan.FromSeconds(10); - Device.StartTimer(interval, () => - { - CurrentLockTime += interval.TotalMilliseconds; - return true; - }); - } } }