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

323 lines
11 KiB
C#
Raw Normal View History

2016-05-02 08:52:09 +02:00
using System;
using System.Linq;
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 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 System.Reflection;
using Bit.App.Resources;
using Bit.App.Utilities;
using Bit.App.Models;
2016-05-02 08:52:09 +02:00
namespace Bit.App
{
public class App : Application
{
private AppOptions _options;
private readonly IAuthService _authService;
2016-05-02 08:52:09 +02:00
private readonly IDatabaseService _databaseService;
private readonly IConnectivity _connectivity;
private readonly ISyncService _syncService;
private readonly ISettings _settings;
private readonly ILockService _lockService;
private readonly ILocalizeService _localizeService;
private readonly IAppInfoService _appInfoService;
private readonly IAppSettingsService _appSettingsService;
2017-07-13 16:51:45 +02:00
private readonly IDeviceActionService _deviceActionService;
public App(
AppOptions options,
IAuthService authService,
IConnectivity connectivity,
IDatabaseService databaseService,
ISyncService syncService,
ISettings settings,
ILockService lockService,
ILocalizeService localizeService,
IAppInfoService appInfoService,
2017-07-13 16:51:45 +02:00
IAppSettingsService appSettingsService,
IDeviceActionService deviceActionService)
2016-05-02 08:52:09 +02:00
{
_options = options ?? new AppOptions();
_authService = authService;
2016-05-02 08:52:09 +02:00
_databaseService = databaseService;
_connectivity = connectivity;
_syncService = syncService;
_settings = settings;
_lockService = lockService;
_localizeService = localizeService;
_appInfoService = appInfoService;
_appSettingsService = appSettingsService;
2017-07-13 16:51:45 +02:00
_deviceActionService = deviceActionService;
2016-05-02 08:52:09 +02:00
SetCulture();
2016-06-17 06:01:25 +02:00
SetStyles();
if(authService.IsAuthenticated)
{
if(_options.FromAutofillFramework && _options.SaveType.HasValue)
{
MainPage = new ExtendedNavigationPage(new VaultAddCipherPage(_options));
}
else if(_options.Uri != null)
{
MainPage = new ExtendedNavigationPage(new VaultAutofillListCiphersPage(_options));
}
else
{
MainPage = new MainPage();
}
2016-05-02 08:52:09 +02:00
}
else
{
MainPage = new ExtendedNavigationPage(new HomePage());
2016-05-02 08:52:09 +02:00
}
MessagingCenter.Subscribe<Application, bool>(Current, "Resumed", async (sender, forceLock) =>
{
Device.BeginInvokeOnMainThread(async () => await _lockService.CheckLockAsync(forceLock));
if(Device.RuntimePlatform == Device.iOS)
{
await Task.Run(() => FullSyncAsync()).ConfigureAwait(false);
}
});
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 _lockService.CheckLockAsync(false);
if(string.IsNullOrWhiteSpace(_options.Uri))
{
2017-11-02 01:33:05 +01:00
var updated = Helpers.PerformUpdateTasks(_settings, _appInfoService, _databaseService, _syncService);
if(!updated)
{
await Task.Run(() => FullSyncAsync()).ConfigureAwait(false);
}
}
if((DateTime.UtcNow - _appSettingsService.LastCacheClear).TotalDays >= 1)
{
await Task.Run(() => _deviceActionService.ClearCache()).ConfigureAwait(false);
}
Debug.WriteLine("OnStart");
2016-05-02 08:52:09 +02:00
}
protected override void OnSleep()
2016-05-02 08:52:09 +02:00
{
// Handle when your app sleeps
Debug.WriteLine("OnSleep");
SetMainPageFromAutofill();
if(Device.RuntimePlatform == Device.Android && !_lockService.TopPageIsLock())
2016-06-05 04:35:03 +02:00
{
_lockService.UpdateLastActivity();
2016-06-05 04:35:03 +02:00
}
2016-05-02 08:52:09 +02:00
}
protected async override void OnResume()
2016-05-02 08:52:09 +02:00
{
base.OnResume();
// workaround for app compat bug
// ref https://forums.xamarin.com/discussion/62414/app-resuming-results-in-crash-with-formsappcompatactivity
await Task.Delay(10);
2016-05-02 08:52:09 +02:00
// Handle when your app resumes
Debug.WriteLine("OnResume");
2016-06-05 04:35:03 +02:00
2017-05-30 20:13:53 +02:00
if(Device.RuntimePlatform == Device.Android)
2016-06-05 04:35:03 +02:00
{
await _lockService.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.FocusWithDelay();
2016-06-07 03:13:00 +02:00
}
2017-05-30 20:13:53 +02:00
if(Device.RuntimePlatform == Device.Android)
{
await Task.Run(() => FullSyncAsync()).ConfigureAwait(false);
}
var now = DateTime.UtcNow;
if((now - _appSettingsService.LastCacheClear).TotalDays >= 1
&& (now - _appSettingsService.LastActivity).TotalHours >= 1)
{
await Task.Run(() => _deviceActionService.ClearCache()).ConfigureAwait(false);
}
}
private void SetMainPageFromAutofill()
{
if(Device.RuntimePlatform == Device.Android && !string.IsNullOrWhiteSpace(_options.Uri) &&
!_options.FromAutofillFramework)
{
2017-02-17 04:22:19 +01:00
Task.Run(() =>
{
2017-02-17 04:22:19 +01:00
Device.BeginInvokeOnMainThread(() =>
{
Current.MainPage = new MainPage();
_options.Uri = null;
2017-02-17 04:22:19 +01:00
});
});
}
}
private async Task FullSyncAsync()
{
if(_connectivity.IsConnected)
{
var attempt = 0;
do
{
try
{
await _syncService.FullSyncAsync(TimeSpan.FromMinutes(30)).ConfigureAwait(false);
break;
}
catch(WebException)
{
Debug.WriteLine("Failed to full sync.");
if(attempt >= 1)
{
break;
}
else
{
await Task.Delay(1000);
}
attempt++;
}
catch(Exception e) when(e is TaskCanceledException || e is OperationCanceledException)
{
Debug.WriteLine("Cancellation exception.");
break;
}
} while(attempt <= 1);
}
else
{
Debug.WriteLine("Not connected.");
}
}
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 }
}
});
Resources.Add(new Style(typeof(ExtendedButton))
{
Setters = {
new Setter { Property = Button.TextColorProperty, Value = primaryColor }
}
});
2016-06-17 06:01:25 +02:00
// List View
2017-12-31 03:33:29 +01:00
Resources.Add(new Style(typeof(ExtendedListView))
2016-06-17 06:01:25 +02:00
{
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
}
private void SetCulture()
{
Debug.WriteLine("====== resource debug info =========");
var assembly = typeof(App).GetTypeInfo().Assembly;
foreach(var res in assembly.GetManifestResourceNames())
{
Debug.WriteLine("found resource: " + res);
}
Debug.WriteLine("====================================");
// This lookup NOT required for Windows platforms - the Culture will be automatically set
2017-05-30 20:13:53 +02:00
if(Device.RuntimePlatform == Device.iOS || Device.RuntimePlatform == Device.Android)
{
var ci = _localizeService.GetCurrentCultureInfo();
AppResources.Culture = ci;
_localizeService.SetLocale(ci);
}
}
2016-05-02 08:52:09 +02:00
}
}