From a3f0254fb231eaccc658350984ed34c0ab21501d Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 27 Dec 2017 11:14:53 -0500 Subject: [PATCH] background timer for locking --- src/App/Abstractions/Services/ILockService.cs | 2 ++ src/App/App.cs | 6 ++++ src/App/Services/LockService.cs | 28 +++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/src/App/Abstractions/Services/ILockService.cs b/src/App/Abstractions/Services/ILockService.cs index a91ebe0ab..405b773aa 100644 --- a/src/App/Abstractions/Services/ILockService.cs +++ b/src/App/Abstractions/Services/ILockService.cs @@ -6,9 +6,11 @@ namespace Bit.App.Abstractions { public interface ILockService { + bool CheckForLockInBackground { get; set; } void UpdateLastActivity(DateTime? activityDate = null); 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 ed42aea3c..9ad9b1421 100644 --- a/src/App/App.cs +++ b/src/App/App.cs @@ -86,11 +86,15 @@ namespace Bit.App await Task.Run(() => FullSyncAsync()).ConfigureAwait(false); }); } + + // TODO: Still testing. + //_lockService.StartLockTimer(); } protected async override void OnStart() { // Handle when your app starts + _lockService.CheckForLockInBackground = false; await _lockService.CheckLockAsync(false); if(string.IsNullOrWhiteSpace(_options.Uri)) @@ -113,6 +117,7 @@ namespace Bit.App protected override void OnSleep() { // Handle when your app sleeps + _lockService.CheckForLockInBackground = true; Debug.WriteLine("OnSleep"); SetMainPageFromAutofill(); @@ -126,6 +131,7 @@ namespace Bit.App protected async override void OnResume() { base.OnResume(); + _lockService.CheckForLockInBackground = false; // workaround for app compat bug // ref https://forums.xamarin.com/discussion/62414/app-resuming-results-in-crash-with-formsappcompatactivity diff --git a/src/App/Services/LockService.cs b/src/App/Services/LockService.cs index 967e8b268..f5215a627 100644 --- a/src/App/Services/LockService.cs +++ b/src/App/Services/LockService.cs @@ -17,6 +17,7 @@ namespace Bit.App.Services private readonly IAppSettingsService _appSettings; private readonly IAuthService _authService; private readonly IFingerprint _fingerprint; + private bool _timerCreated = false; public LockService( ISettings settings, @@ -30,6 +31,8 @@ namespace Bit.App.Services _fingerprint = fingerprint; } + public bool CheckForLockInBackground { get; set; } = true; + public void UpdateLastActivity(DateTime? activityDate = null) { if(_appSettings.Locked) @@ -136,5 +139,30 @@ namespace Bit.App.Services return false; } + + public void StartLockTimer() + { + if(_timerCreated) + { + return; + } + + _timerCreated = true; + Device.StartTimer(TimeSpan.FromMinutes(1), () => + { + if(CheckForLockInBackground && !_appSettings.Locked) + { + System.Diagnostics.Debug.WriteLine("Check lock from timer at " + DateTime.Now); + var lockType = GetLockTypeAsync(false).GetAwaiter().GetResult(); + if(lockType != LockType.None) + { + System.Diagnostics.Debug.WriteLine("Locked from timer at " + DateTime.Now); + _appSettings.Locked = true; + } + } + + return true; + }); + } } }