using System; using System.IO; using System.Threading.Tasks; using Android.App; using Android.Content; using Android.OS; using Android.Runtime; using Bit.App.Abstractions; using Bit.App.Services; using Bit.Core; using Bit.Core.Abstractions; using Bit.Core.Services; using Bit.Core.Utilities; using Bit.Droid.Services; using Bit.Droid.Utilities; using Plugin.CurrentActivity; using Plugin.Fingerprint; using Xamarin.Android.Net; #if !FDROID using Android.Gms.Security; #endif namespace Bit.Droid { #if DEBUG [Application(Debuggable = true)] #else [Application(Debuggable = false)] #endif [Register("com.x8bit.bitwarden.MainApplication")] #if FDROID public class MainApplication : Application #else public class MainApplication : Application, ProviderInstaller.IProviderInstallListener #endif { public MainApplication(IntPtr handle, JniHandleOwnership transer) : base(handle, transer) { if(ServiceContainer.RegisteredServices.Count == 0) { RegisterLocalServices(); ServiceContainer.Init(); if(App.Migration.MigrationHelpers.NeedsMigration()) { var task = App.Migration.MigrationHelpers.PerformMigrationAsync(); Task.Delay(2000).Wait(); } } #if !FDROID if(Build.VERSION.SdkInt <= BuildVersionCodes.Kitkat) { ProviderInstaller.InstallIfNeededAsync(ApplicationContext, this); } #endif } public override void OnCreate() { base.OnCreate(); Bootstrap(); CrossCurrentActivity.Current.Init(this); } public void OnProviderInstallFailed(int errorCode, Intent recoveryIntent) { } public void OnProviderInstalled() { } private void RegisterLocalServices() { ServiceContainer.Register("logService", new AndroidLogService()); ServiceContainer.Register("settingsShim", new App.Migration.SettingsShim()); if(App.Migration.MigrationHelpers.NeedsMigration()) { ServiceContainer.Register( "oldSecureStorageService", new Migration.AndroidKeyStoreStorageService()); } Refractored.FabControl.Droid.FloatingActionButtonViewRenderer.Init(); // Note: This might cause a race condition. Investigate more. Task.Run(() => { FFImageLoading.Forms.Platform.CachedImageRenderer.Init(true); FFImageLoading.ImageService.Instance.Initialize(new FFImageLoading.Config.Configuration { FadeAnimationEnabled = false, FadeAnimationForCachedImages = false }); ZXing.Net.Mobile.Forms.Android.Platform.Init(); }); CrossFingerprint.SetCurrentActivityResolver(() => CrossCurrentActivity.Current.Activity); CrossFingerprint.SetDialogFragmentType(); var preferencesStorage = new PreferencesStorageService(null); var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); var liteDbStorage = new LiteDbStorageService(Path.Combine(documentsPath, "bitwarden.db")); liteDbStorage.InitAsync(); var localizeService = new LocalizeService(); var broadcasterService = new BroadcasterService(); var messagingService = new MobileBroadcasterMessagingService(broadcasterService); var i18nService = new MobileI18nService(localizeService.GetCurrentCultureInfo()); var secureStorageService = new SecureStorageService(); var cryptoPrimitiveService = new CryptoPrimitiveService(); var mobileStorageService = new MobileStorageService(preferencesStorage, liteDbStorage); var deviceActionService = new DeviceActionService(mobileStorageService, messagingService, broadcasterService, () => ServiceContainer.Resolve("eventService")); var platformUtilsService = new MobilePlatformUtilsService(deviceActionService, messagingService, broadcasterService); ServiceContainer.Register("broadcasterService", broadcasterService); ServiceContainer.Register("messagingService", messagingService); ServiceContainer.Register("localizeService", localizeService); ServiceContainer.Register("i18nService", i18nService); ServiceContainer.Register("cryptoPrimitiveService", cryptoPrimitiveService); ServiceContainer.Register("storageService", mobileStorageService); ServiceContainer.Register("secureStorageService", secureStorageService); ServiceContainer.Register("deviceActionService", deviceActionService); ServiceContainer.Register("platformUtilsService", platformUtilsService); // Push #if FDROID ServiceContainer.Register( "pushNotificationListenerService", new NoopPushNotificationListenerService()); ServiceContainer.Register( "pushNotificationService", new NoopPushNotificationService()); #else var notificationListenerService = new PushNotificationListenerService(); ServiceContainer.Register( "pushNotificationListenerService", notificationListenerService); var androidPushNotificationService = new AndroidPushNotificationService( mobileStorageService, notificationListenerService); ServiceContainer.Register( "pushNotificationService", androidPushNotificationService); #endif } private void Bootstrap() { (ServiceContainer.Resolve("i18nService") as MobileI18nService).Init(); ServiceContainer.Resolve("authService").Init(); // Note: This is not awaited var bootstrapTask = BootstrapAsync(); } private async Task BootstrapAsync() { var disableFavicon = await ServiceContainer.Resolve("storageService") .GetAsync(Constants.DisableFaviconKey); await ServiceContainer.Resolve("stateService").SaveAsync( Constants.DisableFaviconKey, disableFavicon); await ServiceContainer.Resolve("environmentService").SetUrlsFromStorageAsync(); } } }