internal timer for lock checking

This commit is contained in:
Kyle Spearrin 2017-12-27 22:18:11 -05:00
parent 175a41f275
commit c60cefd188
6 changed files with 58 additions and 38 deletions

View file

@ -5,6 +5,8 @@ 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

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

View file

@ -87,8 +87,7 @@ namespace Bit.App
}); });
} }
// TODO: Still testing. _lockService.StartLockTimer();
//_lockService.StartLockTimer();
} }
protected async override void OnStart() protected async override void OnStart()

View file

@ -33,8 +33,10 @@
public const string SecurityStamp = "other:securityStamp"; public const string SecurityStamp = "other:securityStamp";
public const string LastActivityDate = "other:lastActivityDate"; public const string LastActivityDate = "other:lastActivityDate";
public const string LastActivityLockTime = "other:lastActivityLockTime";
public const string LastCacheClearDate = "other:cacheClearDate"; public const string LastCacheClearDate = "other:cacheClearDate";
public const string Locked = "other:locked"; public const string Locked = "other:locked";
public const string LockTimerId = "other:lockTimerId";
public const string LastLoginEmail = "other:lastLoginEmail"; public const string LastLoginEmail = "other:lastLoginEmail";
public const string LastSync = "other:lastSync"; public const string LastSync = "other:lastSync";
public const string LastBuildKey = "LastBuild"; public const string LastBuildKey = "LastBuild";

View file

@ -26,6 +26,30 @@ 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

@ -17,8 +17,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 bool _timerCreated = false; private string _timerId = null;
private bool _firstLockCheck = false; // TODO: true when we want to support this
public LockService( public LockService(
ISettings settings, ISettings settings,
@ -33,46 +32,52 @@ namespace Bit.App.Services
} }
public bool CheckForLockInBackground { get; set; } = true; public bool CheckForLockInBackground { get; set; } = true;
public double CurrentLockTime { get; set; }
public void UpdateLastActivity(DateTime? activityDate = null) public void UpdateLastActivity()
{ {
if(_appSettings.Locked) if(_appSettings.Locked)
{ {
return; return;
} }
_appSettings.LastActivity = activityDate.GetValueOrDefault(DateTime.UtcNow); _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)
{ {
return LockType.None; returnNone = true;
} }
// Lock seconds tells if they want to lock the app or not
var lockSeconds = _settings.GetValueOrDefault(Constants.SettingLockSeconds, 60 * 15);
// 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)
if(!_firstLockCheck && !forceLock && !_appSettings.Locked) else 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) if(lockSeconds == -1)
{ {
return LockType.None; returnNone = true;
} }
// Validate timer instance
// Has it been longer than lockSeconds since the last time the app was used? else if(_appSettings.LockTimerId != null && _timerId == _appSettings.LockTimerId)
var now = DateTime.UtcNow;
if(now > _appSettings.LastActivity && (now - _appSettings.LastActivity).TotalSeconds < lockSeconds)
{ {
return LockType.None; // 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;
}
} }
} }
// Skip first lock check if not using locking // Set the new lock timer id
if(_firstLockCheck && lockSeconds == -1 && !forceLock && !_appSettings.Locked) _appSettings.LockTimerId = _timerId;
if(returnNone)
{ {
return LockType.None; return LockType.None;
} }
@ -99,13 +104,10 @@ namespace Bit.App.Services
{ {
if(TopPageIsLock()) if(TopPageIsLock())
{ {
// already locked
_firstLockCheck = false;
return; return;
} }
var lockType = await GetLockTypeAsync(forceLock); var lockType = await GetLockTypeAsync(forceLock);
_firstLockCheck = false;
if(lockType == LockType.None) if(lockType == LockType.None)
{ {
return; return;
@ -152,25 +154,16 @@ namespace Bit.App.Services
public void StartLockTimer() public void StartLockTimer()
{ {
if(_timerCreated) if(_timerId != null)
{ {
return; return;
} }
_timerCreated = true; _timerId = Guid.NewGuid().ToString();
Device.StartTimer(TimeSpan.FromMinutes(1), () => var interval = TimeSpan.FromSeconds(10);
Device.StartTimer(interval, () =>
{ {
if(CheckForLockInBackground && !_appSettings.Locked) CurrentLockTime += interval.TotalMilliseconds;
{
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; return true;
}); });
} }