2016-05-02 09:52:09 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Bit.App.Abstractions;
|
2016-05-03 00:50:16 +03:00
|
|
|
|
using Bit.App.Pages;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
using Xamarin.Forms;
|
2016-05-21 19:32:34 +03:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using Plugin.Fingerprint.Abstractions;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Plugin.Settings.Abstractions;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
|
|
|
|
namespace Bit.App
|
|
|
|
|
{
|
|
|
|
|
public class App : Application
|
|
|
|
|
{
|
|
|
|
|
private readonly IDatabaseService _databaseService;
|
2016-05-03 00:50:16 +03:00
|
|
|
|
private readonly IAuthService _authService;
|
2016-05-21 19:32:34 +03:00
|
|
|
|
private readonly IFingerprint _fingerprint;
|
|
|
|
|
private readonly ISettings _settings;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-05-21 19:32:34 +03:00
|
|
|
|
public App(
|
|
|
|
|
IAuthService authService,
|
|
|
|
|
IDatabaseService databaseService,
|
|
|
|
|
IFingerprint fingerprint,
|
|
|
|
|
ISettings settings)
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
|
|
|
|
_databaseService = databaseService;
|
2016-05-03 00:50:16 +03:00
|
|
|
|
_authService = authService;
|
2016-05-21 19:32:34 +03:00
|
|
|
|
_fingerprint = fingerprint;
|
|
|
|
|
_settings = settings;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
|
|
|
|
if(authService.IsAuthenticated)
|
|
|
|
|
{
|
2016-05-03 00:50:16 +03:00
|
|
|
|
MainPage = new MainPage();
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-05-03 00:50:16 +03:00
|
|
|
|
MainPage = new LoginNavigationPage();
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MainPage.BackgroundColor = Color.FromHex("ecf0f5");
|
2016-05-21 19:32:34 +03:00
|
|
|
|
|
2016-05-22 06:26:35 +03:00
|
|
|
|
MessagingCenter.Subscribe<Application, bool>(Current, "Lock", async (sender, args) =>
|
2016-05-21 19:32:34 +03:00
|
|
|
|
{
|
2016-05-22 06:26:35 +03:00
|
|
|
|
await CheckLockAsync(args);
|
2016-05-21 19:32:34 +03:00
|
|
|
|
});
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnStart()
|
|
|
|
|
{
|
|
|
|
|
// Handle when your app starts
|
2016-05-22 06:26:35 +03:00
|
|
|
|
CheckLockAsync(false);
|
2016-05-02 09:52:09 +03:00
|
|
|
|
_databaseService.CreateTables();
|
2016-05-21 19:32:34 +03:00
|
|
|
|
|
|
|
|
|
Debug.WriteLine("OnStart");
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnSleep()
|
|
|
|
|
{
|
|
|
|
|
// Handle when your app sleeps
|
2016-05-21 19:32:34 +03:00
|
|
|
|
Debug.WriteLine("OnSleep");
|
2016-06-05 05:35:03 +03:00
|
|
|
|
|
|
|
|
|
if(Device.OS == TargetPlatform.Android)
|
|
|
|
|
{
|
|
|
|
|
_settings.AddOrUpdateValue(Constants.SettingLastBackgroundedDate, DateTime.UtcNow);
|
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnResume()
|
|
|
|
|
{
|
|
|
|
|
// Handle when your app resumes
|
2016-05-21 19:32:34 +03:00
|
|
|
|
Debug.WriteLine("OnResume");
|
2016-06-05 05:35:03 +03:00
|
|
|
|
|
|
|
|
|
if(Device.OS == TargetPlatform.Android)
|
|
|
|
|
{
|
|
|
|
|
CheckLockAsync(false);
|
|
|
|
|
}
|
2016-06-07 04:13:00 +03:00
|
|
|
|
|
2016-06-08 02:19:21 +03:00
|
|
|
|
var lockPinPage = Current.MainPage.Navigation.ModalStack.LastOrDefault() as LockPinPage;
|
|
|
|
|
if(lockPinPage != null)
|
2016-06-07 04:13:00 +03:00
|
|
|
|
{
|
2016-06-08 02:19:21 +03:00
|
|
|
|
lockPinPage.PinControl.Entry.Focus();
|
2016-06-07 04:13:00 +03:00
|
|
|
|
}
|
2016-05-21 19:32:34 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-22 06:26:35 +03:00
|
|
|
|
private async Task CheckLockAsync(bool forceLock)
|
2016-05-21 19:32:34 +03:00
|
|
|
|
{
|
2016-05-22 05:50:15 +03:00
|
|
|
|
// Only lock if they are logged in
|
|
|
|
|
if(!_authService.IsAuthenticated)
|
2016-05-21 19:32:34 +03:00
|
|
|
|
{
|
2016-05-22 05:50:15 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-22 06:26:35 +03:00
|
|
|
|
// Are we forcing a lock? (i.e. clicking a button to lock the app manually, immediately)
|
|
|
|
|
if(!forceLock)
|
2016-05-22 05:50:15 +03:00
|
|
|
|
{
|
2016-05-22 06:26:35 +03:00
|
|
|
|
// Lock seconds tells if if they want to lock the app or not
|
|
|
|
|
var lockSeconds = _settings.GetValueOrDefault<int?>(Constants.SettingLockSeconds);
|
|
|
|
|
if(!lockSeconds.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-05-22 05:50:15 +03:00
|
|
|
|
|
2016-05-22 06:26:35 +03:00
|
|
|
|
// Has it been longer than lockSeconds since the last time the app was backgrounded?
|
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
var lastBackground = _settings.GetValueOrDefault(Constants.SettingLastBackgroundedDate, now.AddYears(-1));
|
|
|
|
|
if((now - lastBackground).TotalSeconds < lockSeconds.Value)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-05-22 05:50:15 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// What method are we using to unlock?
|
|
|
|
|
var fingerprintUnlock = _settings.GetValueOrDefault<bool>(Constants.SettingFingerprintUnlockOn);
|
|
|
|
|
var pinUnlock = _settings.GetValueOrDefault<bool>(Constants.SettingPinUnlockOn);
|
|
|
|
|
if(fingerprintUnlock && _fingerprint.IsAvailable)
|
|
|
|
|
{
|
2016-06-05 05:35:03 +03:00
|
|
|
|
if(Current.MainPage.Navigation.ModalStack.LastOrDefault() as LockFingerprintPage == null)
|
2016-05-22 05:50:15 +03:00
|
|
|
|
{
|
2016-06-05 05:35:03 +03:00
|
|
|
|
await Current.MainPage.Navigation.PushModalAsync(new LockFingerprintPage(!forceLock), false);
|
2016-05-22 05:50:15 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-13 06:35:04 +03:00
|
|
|
|
else if(pinUnlock && !string.IsNullOrWhiteSpace(_authService.PIN))
|
2016-05-22 05:50:15 +03:00
|
|
|
|
{
|
2016-06-05 07:17:15 +03:00
|
|
|
|
if(Current.MainPage.Navigation.ModalStack.LastOrDefault() as LockPinPage == null)
|
|
|
|
|
{
|
|
|
|
|
await Current.MainPage.Navigation.PushModalAsync(new LockPinPage(), false);
|
|
|
|
|
}
|
2016-05-22 05:50:15 +03:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Use master password to unlock if no other methods are set
|
2016-05-21 19:32:34 +03:00
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|