mirror of
https://github.com/bitwarden/android.git
synced 2024-12-24 01:48:25 +03:00
base lock timer off of Stopwatch class
This commit is contained in:
parent
6027406eef
commit
c67250da2d
5 changed files with 17 additions and 83 deletions
|
@ -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; }
|
||||
|
|
|
@ -10,6 +10,5 @@ namespace Bit.App.Abstractions
|
|||
Task<LockType> GetLockTypeAsync(bool forceLock);
|
||||
Task CheckLockAsync(bool forceLock);
|
||||
bool TopPageIsLock();
|
||||
void StartLockTimer();
|
||||
}
|
||||
}
|
|
@ -86,8 +86,6 @@ namespace Bit.App
|
|||
await Task.Run(() => FullSyncAsync()).ConfigureAwait(false);
|
||||
});
|
||||
}
|
||||
|
||||
_lockService.StartLockTimer();
|
||||
}
|
||||
|
||||
protected async override void OnStart()
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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<LockType> 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;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue