base lock timer off of Stopwatch class

This commit is contained in:
Kyle Spearrin 2017-12-28 21:14:30 -05:00
parent 6027406eef
commit c67250da2d
5 changed files with 17 additions and 83 deletions

View file

@ -5,8 +5,6 @@ namespace Bit.App.Abstractions
public interface IAppSettingsService public interface IAppSettingsService
{ {
bool Locked { get; set; } bool Locked { get; set; }
string LockTimerId { get; set; }
double LastActivityLockTime { get; set; }
DateTime LastActivity { get; set; } DateTime LastActivity { get; set; }
DateTime LastCacheClear { get; set; } DateTime LastCacheClear { get; set; }
bool AutofillPersistNotification { get; set; } bool AutofillPersistNotification { get; set; }

View file

@ -10,6 +10,5 @@ namespace Bit.App.Abstractions
Task<LockType> GetLockTypeAsync(bool forceLock); Task<LockType> GetLockTypeAsync(bool forceLock);
Task CheckLockAsync(bool forceLock); Task CheckLockAsync(bool forceLock);
bool TopPageIsLock(); bool TopPageIsLock();
void StartLockTimer();
} }
} }

View file

@ -86,8 +86,6 @@ namespace Bit.App
await Task.Run(() => FullSyncAsync()).ConfigureAwait(false); await Task.Run(() => FullSyncAsync()).ConfigureAwait(false);
}); });
} }
_lockService.StartLockTimer();
} }
protected async override void OnStart() protected async override void OnStart()

View file

@ -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 public DateTime LastActivity
{ {
get get

View file

@ -8,6 +8,7 @@ using Bit.App.Controls;
using Bit.App.Pages; using Bit.App.Pages;
using Xamarin.Forms; using Xamarin.Forms;
using System.Linq; using System.Linq;
using System.Diagnostics;
namespace Bit.App.Services namespace Bit.App.Services
{ {
@ -17,7 +18,7 @@ namespace Bit.App.Services
private readonly IAppSettingsService _appSettings; private readonly IAppSettingsService _appSettings;
private readonly IAuthService _authService; private readonly IAuthService _authService;
private readonly IFingerprint _fingerprint; private readonly IFingerprint _fingerprint;
private string _timerId = null; private Stopwatch _stopwatch = null;
public LockService( public LockService(
ISettings settings, ISettings settings,
@ -31,59 +32,32 @@ namespace Bit.App.Services
_fingerprint = fingerprint; _fingerprint = fingerprint;
} }
public double CurrentLockTime { get; set; }
public void UpdateLastActivity() public void UpdateLastActivity()
{ {
if(_appSettings.Locked) _stopwatch?.Restart();
{
return;
}
_appSettings.LastActivityLockTime = CurrentLockTime;
} }
public async Task<LockType> GetLockTypeAsync(bool forceLock) public async Task<LockType> GetLockTypeAsync(bool forceLock)
{ {
var returnNone = false;
// Only lock if they are logged in // Only lock if they are logged in
if(!_authService.IsAuthenticated) if(!_authService.IsAuthenticated)
{ {
returnNone = true; return LockType.None;
} }
// Are we forcing a lock? (i.e. clicking a button to lock the app manually, immediately) // 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 // Lock seconds tells if they want to lock the app or not
var lockSeconds = _settings.GetValueOrDefault(Constants.SettingLockSeconds, 60 * 15); var lockSeconds = _settings.GetValueOrDefault(Constants.SettingLockSeconds, 60 * 15);
if(lockSeconds == -1) var neverLock = 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? // Has it been longer than lockSeconds since the last time the app was used?
var now = CurrentLockTime; if(neverLock || (_stopwatch != null && _stopwatch.Elapsed.TotalSeconds < lockSeconds))
var elapsedSeconds = (now - _appSettings.LastActivityLockTime) / 1000;
if(now >= _appSettings.LastActivityLockTime && elapsedSeconds < lockSeconds)
{
returnNone = true;
}
}
}
// Set the new lock timer id
if(_timerId != null)
{
_appSettings.LockTimerId = _timerId;
}
if(returnNone)
{ {
return LockType.None; return LockType.None;
} }
}
// What method are we using to unlock? // What method are we using to unlock?
var fingerprintUnlock = _settings.GetValueOrDefault(Constants.SettingFingerprintUnlockOn, false); var fingerprintUnlock = _settings.GetValueOrDefault(Constants.SettingFingerprintUnlockOn, false);
@ -116,6 +90,11 @@ namespace Bit.App.Services
return; return;
} }
if(_stopwatch == null)
{
_stopwatch = Stopwatch.StartNew();
}
_appSettings.Locked = true; _appSettings.Locked = true;
switch(lockType) switch(lockType)
{ {
@ -154,21 +133,5 @@ namespace Bit.App.Services
return false; 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;
});
}
} }
} }