1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-10-03 04:48:03 +02:00
bitwarden-mobile/src/App/App.cs

328 lines
11 KiB
C#
Raw Normal View History

2016-05-02 08:52:09 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bit.App.Abstractions;
2016-05-02 23:50:16 +02:00
using Bit.App.Pages;
2016-05-02 08:52:09 +02:00
using Xamarin.Forms;
using System.Diagnostics;
using Plugin.Fingerprint.Abstractions;
using System.Threading.Tasks;
using Plugin.Settings.Abstractions;
2016-06-17 06:01:25 +02:00
using Bit.App.Controls;
using Plugin.Connectivity.Abstractions;
using System.Net;
using Acr.UserDialogs;
using PushNotification.Plugin.Abstractions;
2016-05-02 08:52:09 +02:00
namespace Bit.App
{
public class App : Application
{
private readonly IDatabaseService _databaseService;
private readonly IConnectivity _connectivity;
private readonly IUserDialogs _userDialogs;
private readonly ISyncService _syncService;
2016-05-02 23:50:16 +02:00
private readonly IAuthService _authService;
private readonly IFingerprint _fingerprint;
private readonly ISettings _settings;
private readonly IPushNotification _pushNotification;
2016-05-02 08:52:09 +02:00
public App(
IAuthService authService,
IConnectivity connectivity,
IUserDialogs userDialogs,
IDatabaseService databaseService,
ISyncService syncService,
IFingerprint fingerprint,
ISettings settings,
IPushNotification pushNotification)
2016-05-02 08:52:09 +02:00
{
_databaseService = databaseService;
_connectivity = connectivity;
_userDialogs = userDialogs;
_syncService = syncService;
2016-05-02 23:50:16 +02:00
_authService = authService;
_fingerprint = fingerprint;
_settings = settings;
_pushNotification = pushNotification;
2016-05-02 08:52:09 +02:00
2016-06-17 06:01:25 +02:00
SetStyles();
2016-05-02 08:52:09 +02:00
if(authService.IsAuthenticated)
{
2016-05-02 23:50:16 +02:00
MainPage = new MainPage();
2016-05-02 08:52:09 +02:00
}
else
{
MainPage = new HomePage();
2016-05-02 08:52:09 +02:00
}
MessagingCenter.Subscribe<Application, bool>(Current, "Resumed", async (sender, args) =>
{
await CheckLockAsync(args);
await Task.Run(() => IncrementalSyncAsync()).ConfigureAwait(false);
});
2016-05-22 05:26:35 +02:00
MessagingCenter.Subscribe<Application, bool>(Current, "Lock", async (sender, args) =>
{
2016-05-22 05:26:35 +02:00
await CheckLockAsync(args);
});
MessagingCenter.Subscribe<Application, string>(Current, "Logout", (sender, args) =>
{
Device.BeginInvokeOnMainThread(() => Logout(args));
});
2016-05-02 08:52:09 +02:00
}
protected async override void OnStart()
2016-05-02 08:52:09 +02:00
{
// Handle when your app starts
await CheckLockAsync(false);
2016-05-02 08:52:09 +02:00
_databaseService.CreateTables();
await Task.Run(() => FullSyncAsync()).ConfigureAwait(false);
Debug.WriteLine("OnStart");
2016-05-02 08:52:09 +02:00
}
protected override void OnSleep()
{
// Handle when your app sleeps
Debug.WriteLine("OnSleep");
2016-06-05 04:35:03 +02:00
if(Device.OS == TargetPlatform.Android)
{
_settings.AddOrUpdateValue(Constants.SettingLastBackgroundedDate, DateTime.UtcNow);
}
2016-05-02 08:52:09 +02:00
}
protected async override void OnResume()
2016-05-02 08:52:09 +02:00
{
// Handle when your app resumes
Debug.WriteLine("OnResume");
2016-06-05 04:35:03 +02:00
if(Device.OS == TargetPlatform.Android)
{
await CheckLockAsync(false);
2016-06-05 04:35:03 +02:00
}
2016-06-07 03:13:00 +02:00
var lockPinPage = Current.MainPage.Navigation.ModalStack.LastOrDefault() as LockPinPage;
if(lockPinPage != null)
2016-06-07 03:13:00 +02:00
{
lockPinPage.PinControl.Entry.Focus();
2016-06-07 03:13:00 +02:00
}
}
private async Task IncrementalSyncAsync()
{
if(_connectivity.IsConnected)
{
var attempt = 0;
do
{
try
{
await _syncService.IncrementalSyncAsync();
break;
}
catch(WebException)
{
Debug.WriteLine("Failed to incremental sync.");
if(attempt >= 1)
{
break;
}
else
{
await Task.Delay(1000);
}
attempt++;
}
} while(attempt <= 1);
}
else
{
Debug.WriteLine("Not connected.");
}
}
private async Task FullSyncAsync()
{
if(_connectivity.IsConnected)
{
var attempt = 0;
do
{
try
{
await _syncService.FullSyncAsync();
break;
}
catch(WebException)
{
Debug.WriteLine("Failed to full sync.");
if(attempt >= 1)
{
break;
}
else
{
await Task.Delay(1000);
}
attempt++;
}
} while(attempt <= 1);
}
}
private void Logout(string logoutMessage)
{
_authService.LogOut();
_pushNotification.Unregister();
Current.MainPage = new HomePage();
if(!string.IsNullOrWhiteSpace(logoutMessage))
{
_userDialogs.WarnToast("Logged out", logoutMessage);
}
}
2016-05-22 05:26:35 +02:00
private async Task CheckLockAsync(bool forceLock)
{
// Only lock if they are logged in
if(!_authService.IsAuthenticated)
{
return;
}
2016-05-22 05:26:35 +02:00
// Are we forcing a lock? (i.e. clicking a button to lock the app manually, immediately)
if(!forceLock && !_settings.GetValueOrDefault(Constants.SettingLocked, false))
{
2016-05-22 05:26:35 +02: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:26:35 +02: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;
}
}
// What method are we using to unlock?
var fingerprintUnlock = _settings.GetValueOrDefault<bool>(Constants.SettingFingerprintUnlockOn);
var pinUnlock = _settings.GetValueOrDefault<bool>(Constants.SettingPinUnlockOn);
2016-06-28 02:56:59 +02:00
var currentPage = Current.MainPage.Navigation.ModalStack.LastOrDefault() as ExtendedNavigationPage;
if(fingerprintUnlock && _fingerprint.IsAvailable)
{
2016-06-28 02:56:59 +02:00
if((currentPage?.CurrentPage as LockFingerprintPage) == null)
{
2016-06-28 02:56:59 +02:00
await Current.MainPage.Navigation.PushModalAsync(new ExtendedNavigationPage(new LockFingerprintPage(!forceLock)), false);
}
}
2016-06-13 05:35:04 +02:00
else if(pinUnlock && !string.IsNullOrWhiteSpace(_authService.PIN))
{
var lockPinPage = (currentPage?.CurrentPage as LockPinPage);
if(lockPinPage == null)
2016-06-05 06:17:15 +02:00
{
lockPinPage = new LockPinPage();
await Current.MainPage.Navigation.PushModalAsync(new ExtendedNavigationPage(lockPinPage), false);
lockPinPage.PinControl.Entry.Focus();
2016-06-05 06:17:15 +02:00
}
}
else
{
2016-07-19 01:16:27 +02:00
if((currentPage?.CurrentPage as LockPasswordPage) == null)
{
await Current.MainPage.Navigation.PushModalAsync(new ExtendedNavigationPage(new LockPasswordPage()), false);
}
}
2016-05-02 08:52:09 +02:00
}
2016-06-17 06:01:25 +02:00
private void SetStyles()
{
var gray = Color.FromHex("333333");
var grayLight = Color.FromHex("777777");
var grayLighter = Color.FromHex("d2d6de");
var primaryColor = Color.FromHex("3c8dbc");
2016-06-23 05:50:38 +02:00
var primaryColorAccent = Color.FromHex("286090");
2016-06-17 06:01:25 +02:00
Resources = new ResourceDictionary();
// Labels
Resources.Add("text-muted", new Style(typeof(Label))
{
Setters = {
new Setter { Property = Label.TextColorProperty, Value = grayLight }
}
});
// Buttons
Resources.Add("btn-default", new Style(typeof(Button))
{
Setters = {
new Setter { Property = Button.TextColorProperty, Value = gray }
}
});
2016-06-23 05:50:38 +02:00
Resources.Add("btn-primary", new Style(typeof(Button))
{
Setters = {
new Setter { Property = Button.TextColorProperty, Value = Color.White },
new Setter { Property = Button.BackgroundColorProperty, Value = primaryColor },
new Setter { Property = Button.FontAttributesProperty, Value = FontAttributes.Bold },
new Setter { Property = Button.BorderRadiusProperty, Value = 0 }
}
});
Resources.Add("btn-primaryAccent", new Style(typeof(Button))
{
Setters = {
new Setter { Property = Button.TextColorProperty, Value = primaryColorAccent }
}
});
2016-06-24 05:03:00 +02:00
Resources.Add("btn-white", new Style(typeof(Button))
{
Setters = {
new Setter { Property = Button.BackgroundColorProperty, Value = Color.White },
new Setter { Property = Button.TextColorProperty, Value = primaryColor },
new Setter { Property = Button.FontAttributesProperty, Value = FontAttributes.Bold },
new Setter { Property = Button.BorderRadiusProperty, Value = 0 }
}
});
2016-06-17 06:01:25 +02:00
Resources.Add(new Style(typeof(Button))
{
Setters = {
new Setter { Property = Button.TextColorProperty, Value = primaryColor }
}
});
// List View
Resources.Add(new Style(typeof(ListView))
{
Setters = {
new Setter { Property = ListView.SeparatorColorProperty, Value = grayLighter }
}
});
// Search Bar
Resources.Add(new Style(typeof(SearchBar))
{
Setters = {
new Setter { Property = SearchBar.CancelButtonColorProperty, Value = primaryColor }
}
});
2016-06-17 06:01:25 +02:00
}
2016-05-02 08:52:09 +02:00
}
}