Bitwarden-app-android-iphon.../src/iOS/AppDelegate.cs

350 lines
16 KiB
C#
Raw Normal View History

2019-03-28 01:12:44 +01:00
using System;
2019-06-12 05:04:41 +02:00
using System.Collections.Generic;
2019-06-11 21:20:28 +02:00
using System.IO;
using System.Threading.Tasks;
using Bit.App.Abstractions;
2019-06-12 05:04:41 +02:00
using Bit.App.Resources;
2019-06-11 21:20:28 +02:00
using Bit.App.Services;
using Bit.App.Utilities;
2019-06-11 21:20:28 +02:00
using Bit.Core;
using Bit.Core.Abstractions;
using Bit.Core.Services;
using Bit.Core.Utilities;
using Bit.iOS.Core.Services;
2019-06-12 05:04:41 +02:00
using Bit.iOS.Core.Utilities;
2019-06-11 21:20:28 +02:00
using Bit.iOS.Services;
2019-06-12 04:20:59 +02:00
using CoreNFC;
2019-03-28 01:12:44 +01:00
using Foundation;
2019-06-12 05:04:41 +02:00
using HockeyApp.iOS;
2019-03-28 01:12:44 +01:00
using UIKit;
2019-06-26 15:35:18 +02:00
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
2019-03-28 01:12:44 +01:00
namespace Bit.iOS
{
[Register("AppDelegate")]
2019-03-28 04:44:54 +01:00
public partial class AppDelegate : Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
2019-03-28 01:12:44 +01:00
{
2019-06-11 21:20:28 +02:00
private const string AppId = "com.8bit.bitwarden";
private const string AppGroupId = "group.com.8bit.bitwarden";
private const string AccessGroup = "LTZ2PFU5D6.com.8bit.bitwarden";
2019-06-12 04:20:59 +02:00
private NFCNdefReaderSession _nfcSession = null;
private iOSPushNotificationHandler _pushHandler = null;
private NFCReaderDelegate _nfcDelegate = null;
2019-06-11 21:20:28 +02:00
2019-06-12 05:04:41 +02:00
private IDeviceActionService _deviceActionService;
private IMessagingService _messagingService;
private IBroadcasterService _broadcasterService;
private IStorageService _storageService;
2019-03-28 01:12:44 +01:00
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
2019-03-28 04:44:54 +01:00
Xamarin.Forms.Forms.Init();
2019-06-11 21:20:28 +02:00
InitApp();
2019-06-11 22:01:56 +02:00
Bootstrap();
2019-06-12 05:04:41 +02:00
_deviceActionService = ServiceContainer.Resolve<IDeviceActionService>("deviceActionService");
_messagingService = ServiceContainer.Resolve<IMessagingService>("messagingService");
_broadcasterService = ServiceContainer.Resolve<IBroadcasterService>("broadcasterService");
_storageService = ServiceContainer.Resolve<IStorageService>("storageService");
2019-04-22 23:08:37 +02:00
2019-05-17 18:03:35 +02:00
LoadApplication(new App.App(null));
2019-06-12 05:04:41 +02:00
AppearanceAdjustments();
2019-05-23 02:28:31 +02:00
ZXing.Net.Mobile.Forms.iOS.Platform.Init();
2019-06-12 05:04:41 +02:00
_broadcasterService.Subscribe(nameof(AppDelegate), (message) =>
{
if(message.Command == "scheduleLockTimer")
{
var lockOptionMinutes = (int)message.Data;
}
else if(message.Command == "cancelLockTimer")
{
2019-06-26 15:35:18 +02:00
2019-06-12 05:04:41 +02:00
}
else if(message.Command == "updatedTheme")
{
// ThemeManager.SetThemeStyle(message.Data as string);
2019-06-12 05:04:41 +02:00
}
else if(message.Command == "copiedToClipboard")
{
}
else if(message.Command == "listenYubiKeyOTP")
{
ListenYubiKey((bool)message.Data);
}
else if(message.Command == "showAppExtension")
{
}
else if(message.Command == "showStatusBar")
{
UIApplication.SharedApplication.SetStatusBarHidden(!(bool)message.Data, false);
}
else if(message.Command == "syncCompleted")
{
if(message.Data is Dictionary<string, object> data && data.ContainsKey("successfully"))
{
var success = data["successfully"] as bool?;
if(success.GetValueOrDefault())
{
2019-06-26 15:35:18 +02:00
2019-06-12 05:04:41 +02:00
}
}
}
else if(message.Command == "addedCipher" || message.Command == "editedCipher")
{
}
else if(message.Command == "deletedCipher")
{
}
else if(message.Command == "loggedOut")
{
}
});
2019-03-28 01:12:44 +01:00
return base.FinishedLaunching(app, options);
}
2019-06-11 21:20:28 +02:00
2019-06-12 05:04:41 +02:00
public override void DidEnterBackground(UIApplication uiApplication)
{
var view = new UIView(UIApplication.SharedApplication.KeyWindow.Frame)
{
Tag = 4321
};
var backgroundView = new UIView(UIApplication.SharedApplication.KeyWindow.Frame)
{
2019-06-26 15:35:18 +02:00
BackgroundColor = ((Color)Xamarin.Forms.Application.Current.Resources["SplashBackgroundColor"])
.ToUIColor()
2019-06-12 05:04:41 +02:00
};
2019-06-26 15:35:18 +02:00
var theme = ThemeManager.GetTheme(false);
var darkbasedTheme = theme == "dark" || theme == "black" || theme == "nord";
var logo = new UIImage(darkbasedTheme ? "logo_white.png" : "logo.png");
var imageView = new UIImageView(logo)
2019-06-12 05:04:41 +02:00
{
Center = new CoreGraphics.CGPoint(view.Center.X, view.Center.Y - 30)
};
view.AddSubview(backgroundView);
view.AddSubview(imageView);
UIApplication.SharedApplication.KeyWindow.AddSubview(view);
UIApplication.SharedApplication.KeyWindow.BringSubviewToFront(view);
UIApplication.SharedApplication.KeyWindow.EndEditing(true);
UIApplication.SharedApplication.SetStatusBarHidden(true, false);
_storageService.SaveAsync(Bit.Core.Constants.LastActiveKey, DateTime.UtcNow);
base.DidEnterBackground(uiApplication);
}
public override void OnActivated(UIApplication uiApplication)
{
base.OnActivated(uiApplication);
UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
var view = UIApplication.SharedApplication.KeyWindow.ViewWithTag(4321);
if(view != null)
{
view.RemoveFromSuperview();
UIApplication.SharedApplication.SetStatusBarHidden(false, false);
}
}
public override void WillEnterForeground(UIApplication uiApplication)
{
2019-06-24 23:02:05 +02:00
_messagingService.Send("resumed");
2019-06-12 05:04:41 +02:00
base.WillEnterForeground(uiApplication);
}
public override bool OpenUrl(UIApplication application, NSUrl url, string sourceApplication,
NSObject annotation)
{
return true;
}
2019-06-11 21:20:28 +02:00
public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
{
_pushHandler?.OnErrorReceived(error);
}
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
_pushHandler?.OnRegisteredSuccess(deviceToken);
}
public override void DidRegisterUserNotificationSettings(UIApplication application,
UIUserNotificationSettings notificationSettings)
{
application.RegisterForRemoteNotifications();
}
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo,
Action<UIBackgroundFetchResult> completionHandler)
{
_pushHandler?.OnMessageReceived(userInfo);
}
public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
{
_pushHandler?.OnMessageReceived(userInfo);
}
private void InitApp()
{
if(ServiceContainer.RegisteredServices.Count > 0)
{
return;
}
RegisterLocalServices();
ServiceContainer.Init();
_pushHandler = new iOSPushNotificationHandler(
ServiceContainer.Resolve<IPushNotificationListenerService>("pushNotificationListenerService"));
2019-06-12 05:04:41 +02:00
_nfcDelegate = new NFCReaderDelegate((success, message) =>
2019-06-24 23:02:05 +02:00
_messagingService.Send("gotYubiKeyOTP", message));
2019-06-12 05:04:41 +02:00
var crashManagerDelegate = new HockeyAppCrashManagerDelegate(
ServiceContainer.Resolve<IAppIdService>("appIdService"),
ServiceContainer.Resolve<IUserService>("userService"));
var manager = BITHockeyManager.SharedHockeyManager;
manager.Configure("51f96ae568ba45f699a18ad9f63046c3", crashManagerDelegate);
manager.CrashManager.CrashManagerStatus = BITCrashManagerStatus.AutoSend;
manager.StartManager();
manager.Authenticator.AuthenticateInstallation();
manager.DisableMetricsManager = manager.DisableFeedbackManager = manager.DisableUpdateManager = true;
var task = crashManagerDelegate.InitAsync(manager);
2019-06-11 21:20:28 +02:00
}
private void RegisterLocalServices()
{
ServiceContainer.Register<ILogService>("logService", new ConsoleLogService());
ServiceContainer.Register("settingsShim", new App.Migration.SettingsShim());
if(false && App.Migration.MigrationHelpers.NeedsMigration())
{
ServiceContainer.Register<App.Migration.Abstractions.IOldSecureStorageService>(
"oldSecureStorageService", new Migration.KeyChainStorageService());
}
// Note: This might cause a race condition. Investigate more.
Task.Run(() =>
{
FFImageLoading.Forms.Platform.CachedImageRenderer.Init();
FFImageLoading.ImageService.Instance.Initialize(new FFImageLoading.Config.Configuration
{
FadeAnimationEnabled = false,
FadeAnimationForCachedImages = false
});
});
var preferencesStorage = new PreferencesStorageService(AppGroupId);
var appGroupContainer = new NSFileManager().GetContainerUrl(AppGroupId);
var liteDbStorage = new LiteDbStorageService(
Path.Combine(appGroupContainer.Path, "Library", "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 KeyChainStorageService(AppId, AccessGroup);
var cryptoPrimitiveService = new CryptoPrimitiveService();
var mobileStorageService = new MobileStorageService(preferencesStorage, liteDbStorage);
var deviceActionService = new DeviceActionService(mobileStorageService, messagingService);
var platformUtilsService = new MobilePlatformUtilsService(deviceActionService, messagingService,
broadcasterService);
ServiceContainer.Register<IBroadcasterService>("broadcasterService", broadcasterService);
ServiceContainer.Register<IMessagingService>("messagingService", messagingService);
ServiceContainer.Register<ILocalizeService>("localizeService", localizeService);
ServiceContainer.Register<II18nService>("i18nService", i18nService);
ServiceContainer.Register<ICryptoPrimitiveService>("cryptoPrimitiveService", cryptoPrimitiveService);
ServiceContainer.Register<IStorageService>("storageService", mobileStorageService);
ServiceContainer.Register<IStorageService>("secureStorageService", secureStorageService);
ServiceContainer.Register<IDeviceActionService>("deviceActionService", deviceActionService);
ServiceContainer.Register<IPlatformUtilsService>("platformUtilsService", platformUtilsService);
// Push
var notificationListenerService = new PushNotificationListenerService();
ServiceContainer.Register<IPushNotificationListenerService>(
"pushNotificationListenerService", notificationListenerService);
var iosPushNotificationService = new iOSPushNotificationService();
ServiceContainer.Register<IPushNotificationService>(
"pushNotificationService", iosPushNotificationService);
}
private void Bootstrap()
{
(ServiceContainer.Resolve<II18nService>("i18nService") as MobileI18nService).Init();
ServiceContainer.Resolve<IAuthService>("authService").Init();
// Note: This is not awaited
var bootstrapTask = BootstrapAsync();
}
private async Task BootstrapAsync()
{
var disableFavicon = await ServiceContainer.Resolve<IStorageService>("storageService").GetAsync<bool?>(
2019-06-12 05:04:41 +02:00
Bit.Core.Constants.DisableFaviconKey);
await ServiceContainer.Resolve<IStateService>("stateService").SaveAsync(
Bit.Core.Constants.DisableFaviconKey, disableFavicon);
2019-06-11 21:20:28 +02:00
await ServiceContainer.Resolve<IEnvironmentService>("environmentService").SetUrlsFromStorageAsync();
}
2019-06-12 04:20:59 +02:00
private void AppearanceAdjustments()
{
2019-06-24 20:29:23 +02:00
ThemeHelpers.SetAppearance(ThemeManager.GetTheme(false));
/*
2019-06-12 05:04:41 +02:00
var primaryColor = new UIColor(red: 0.24f, green: 0.55f, blue: 0.74f, alpha: 1.0f);
var grayLight = new UIColor(red: 0.47f, green: 0.47f, blue: 0.47f, alpha: 1.0f);
2019-06-12 04:20:59 +02:00
UINavigationBar.Appearance.ShadowImage = new UIImage();
UINavigationBar.Appearance.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
2019-06-12 05:04:41 +02:00
UIBarButtonItem.AppearanceWhenContainedIn(new Type[] { typeof(UISearchBar) }).TintColor = primaryColor;
UIButton.AppearanceWhenContainedIn(new Type[] { typeof(UISearchBar) }).SetTitleColor(primaryColor,
UIControlState.Normal);
UIButton.AppearanceWhenContainedIn(new Type[] { typeof(UISearchBar) }).TintColor = primaryColor;
UIStepper.Appearance.TintColor = grayLight;
UISlider.Appearance.TintColor = primaryColor;
2019-06-24 20:29:23 +02:00
*/
2019-06-12 05:04:41 +02:00
UIApplication.SharedApplication.StatusBarHidden = false;
UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.LightContent;
}
private void ListenYubiKey(bool listen)
{
if(_deviceActionService.SupportsNfc())
{
_nfcSession?.InvalidateSession();
_nfcSession?.Dispose();
_nfcSession = null;
if(listen)
{
_nfcSession = new NFCNdefReaderSession(_nfcDelegate, null, true)
{
AlertMessage = AppResources.HoldYubikeyNearTop
};
_nfcSession.BeginSession();
}
}
}
private void ShowAppExtension()
{
var itemProvider = new NSItemProvider(new NSDictionary(), Core.Constants.UTTypeAppExtensionSetup);
var extensionItem = new NSExtensionItem();
extensionItem.Attachments = new NSItemProvider[] { itemProvider };
var activityViewController = new UIActivityViewController(new NSExtensionItem[] { extensionItem }, null);
activityViewController.CompletionHandler = (activityType, completed) =>
{
// TODO
//page.EnabledExtension(completed && activityType == "com.8bit.bitwarden.find-login-action-extension");
};
var modal = UIApplication.SharedApplication.KeyWindow.RootViewController.ModalViewController;
if(activityViewController.PopoverPresentationController != null)
{
activityViewController.PopoverPresentationController.SourceView = modal.View;
var frame = UIScreen.MainScreen.Bounds;
frame.Height /= 2;
activityViewController.PopoverPresentationController.SourceRect = frame;
}
modal.PresentViewController(activityViewController, true, null);
2019-06-12 04:20:59 +02:00
}
2019-03-28 01:12:44 +01:00
}
}