background timer for locking

This commit is contained in:
Kyle Spearrin 2017-12-27 11:14:53 -05:00
parent ab5f1385c5
commit a3f0254fb2
3 changed files with 36 additions and 0 deletions

View file

@ -6,9 +6,11 @@ namespace Bit.App.Abstractions
{
public interface ILockService
{
bool CheckForLockInBackground { get; set; }
void UpdateLastActivity(DateTime? activityDate = null);
Task<LockType> GetLockTypeAsync(bool forceLock);
Task CheckLockAsync(bool forceLock);
bool TopPageIsLock();
void StartLockTimer();
}
}

View file

@ -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

View file

@ -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;
});
}
}
}