mirror of
https://github.com/bitwarden/mobile
synced 2025-12-24 04:04:34 +00:00
* Account Switching (#1720) * Account switching * WIP * wip * wip * updates to send test logic * fixed Send tests * fixes for theme handling on account switching and re-adding existing account * switch fixes * fixes * fixes * cleanup * vault timeout fixes * account list status enhancements * logout fixes and token handling improvements * merge latest (#1727) * remove duplicate dependency * fix for initial login token storage paradox (#1730) * Fix avatar color update toolbar item issue on iOS for account switching (#1735) * Updated account switching menu UI (#1733) * updated account switching menu UI * additional changes * add key suffix to constant * GetFirstLetters method tweaks * Fix crash on account switching when logging out when having more than user at a time (#1740) * single account migration to multi-account on app update (#1741) * Account Switching Tap to dismiss (#1743) * Added tap to dismiss on the Account switching overlay and improved a bit the code * Fix account switching overlay background transparent on the proper place * Fixed transparent background and the shadow on the account switching overlay * Fix iOS top space on Account switching list overlay after modal (#1746) * Fix top space added to Account switching list overlay after closing modal * Fix top space added to Account switching list overlay after closing modal on lock, login and home views just in case we add modals in the future there as well * Usability: dismiss account list on certain events (#1748) * dismiss account list on certain events * use new FireAndForget method for back button logic * Create and use Account Switching overlay control (#1753) * Added Account switching overlay control and its own ViewModel and refactored accordingly * Fix account switching Accounts list binding update * Implemented dismiss account switching overlay when changing tabs and when selecting the same tab. Also updated the deprecated listener on CustomTabbedRenderer on Android (#1755) * Overriden Equals on AvatarImageSource so it doesn't get set multiple times when it's the same image thus producing blinking on tab chaged (#1756) * Usability improvements for logout on vault timeout (#1781) * accountswitching fixes (#1784) * Fix for invalid PIN lock state when switching accounts (#1792) * fix for pin lock flow * named tuple values and updated async * clear send service cache on account switch (#1796) * Global theme and account removal (#1793) * Global theme and account removal * remove redundant call to hide account list overlay * cleanup and additional tweaks * add try/catch to remove account dialog flow Co-authored-by: Federico Maccaroni <fedemkr@gmail.com>
402 lines
17 KiB
C#
402 lines
17 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using AuthenticationServices;
|
|
using Bit.App.Abstractions;
|
|
using Bit.App.Pages;
|
|
using Bit.App.Services;
|
|
using Bit.App.Utilities;
|
|
using Bit.Core;
|
|
using Bit.Core.Abstractions;
|
|
using Bit.Core.Enums;
|
|
using Bit.Core.Services;
|
|
using Bit.Core.Utilities;
|
|
using Bit.iOS.Core.Utilities;
|
|
using Bit.iOS.Services;
|
|
using CoreNFC;
|
|
using Foundation;
|
|
using UIKit;
|
|
using Xamarin.Forms;
|
|
using Xamarin.Forms.Platform.iOS;
|
|
|
|
namespace Bit.iOS
|
|
{
|
|
[Register("AppDelegate")]
|
|
public partial class AppDelegate : FormsApplicationDelegate
|
|
{
|
|
private NFCNdefReaderSession _nfcSession = null;
|
|
private iOSPushNotificationHandler _pushHandler = null;
|
|
private Core.NFCReaderDelegate _nfcDelegate = null;
|
|
private NSTimer _clipboardTimer = null;
|
|
private nint _clipboardBackgroundTaskId;
|
|
private NSTimer _eventTimer = null;
|
|
private nint _eventBackgroundTaskId;
|
|
|
|
private IDeviceActionService _deviceActionService;
|
|
private IMessagingService _messagingService;
|
|
private IBroadcasterService _broadcasterService;
|
|
private IStorageService _storageService;
|
|
private IStateService _stateService;
|
|
private IEventService _eventService;
|
|
|
|
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
|
|
{
|
|
Forms.Init();
|
|
InitApp();
|
|
|
|
_deviceActionService = ServiceContainer.Resolve<IDeviceActionService>("deviceActionService");
|
|
_messagingService = ServiceContainer.Resolve<IMessagingService>("messagingService");
|
|
_broadcasterService = ServiceContainer.Resolve<IBroadcasterService>("broadcasterService");
|
|
_storageService = ServiceContainer.Resolve<IStorageService>("storageService");
|
|
_stateService = ServiceContainer.Resolve<IStateService>("stateService");
|
|
_eventService = ServiceContainer.Resolve<IEventService>("eventService");
|
|
|
|
LoadApplication(new App.App(null));
|
|
iOSCoreHelpers.AppearanceAdjustments();
|
|
ZXing.Net.Mobile.Forms.iOS.Platform.Init();
|
|
|
|
_broadcasterService.Subscribe(nameof(AppDelegate), async (message) =>
|
|
{
|
|
if (message.Command == "startEventTimer")
|
|
{
|
|
StartEventTimer();
|
|
}
|
|
else if (message.Command == "stopEventTimer")
|
|
{
|
|
var task = StopEventTimerAsync();
|
|
}
|
|
else if (message.Command == "updatedTheme")
|
|
{
|
|
Device.BeginInvokeOnMainThread(() =>
|
|
{
|
|
iOSCoreHelpers.AppearanceAdjustments();
|
|
});
|
|
}
|
|
else if (message.Command == "listenYubiKeyOTP")
|
|
{
|
|
iOSCoreHelpers.ListenYubiKey((bool)message.Data, _deviceActionService, _nfcSession, _nfcDelegate);
|
|
}
|
|
else if (message.Command == "unlocked")
|
|
{
|
|
var needsAutofillReplacement = await _storageService.GetAsync<bool?>(
|
|
Core.Constants.AutofillNeedsIdentityReplacementKey);
|
|
if (needsAutofillReplacement.GetValueOrDefault())
|
|
{
|
|
await ASHelpers.ReplaceAllIdentities();
|
|
}
|
|
}
|
|
else if (message.Command == "showAppExtension")
|
|
{
|
|
Device.BeginInvokeOnMainThread(() => ShowAppExtension((ExtensionPageViewModel)message.Data));
|
|
}
|
|
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() && _deviceActionService.SystemMajorVersion() >= 12)
|
|
{
|
|
await ASHelpers.ReplaceAllIdentities();
|
|
}
|
|
}
|
|
}
|
|
else if (message.Command == "addedCipher" || message.Command == "editedCipher" ||
|
|
message.Command == "restoredCipher")
|
|
{
|
|
if (_deviceActionService.SystemMajorVersion() >= 12)
|
|
{
|
|
if (await ASHelpers.IdentitiesCanIncremental())
|
|
{
|
|
var cipherId = message.Data as string;
|
|
if (message.Command == "addedCipher" && !string.IsNullOrWhiteSpace(cipherId))
|
|
{
|
|
var identity = await ASHelpers.GetCipherIdentityAsync(cipherId);
|
|
if (identity == null)
|
|
{
|
|
return;
|
|
}
|
|
await ASCredentialIdentityStore.SharedStore?.SaveCredentialIdentitiesAsync(
|
|
new ASPasswordCredentialIdentity[] { identity });
|
|
return;
|
|
}
|
|
}
|
|
await ASHelpers.ReplaceAllIdentities();
|
|
}
|
|
}
|
|
else if (message.Command == "deletedCipher" || message.Command == "softDeletedCipher")
|
|
{
|
|
if (_deviceActionService.SystemMajorVersion() >= 12)
|
|
{
|
|
if (await ASHelpers.IdentitiesCanIncremental())
|
|
{
|
|
var identity = ASHelpers.ToCredentialIdentity(
|
|
message.Data as Bit.Core.Models.View.CipherView);
|
|
if (identity == null)
|
|
{
|
|
return;
|
|
}
|
|
await ASCredentialIdentityStore.SharedStore?.RemoveCredentialIdentitiesAsync(
|
|
new ASPasswordCredentialIdentity[] { identity });
|
|
return;
|
|
}
|
|
await ASHelpers.ReplaceAllIdentities();
|
|
}
|
|
}
|
|
else if (message.Command == "logout")
|
|
{
|
|
if (_deviceActionService.SystemMajorVersion() >= 12)
|
|
{
|
|
var extras = message.Data as Tuple<string, bool, bool>;
|
|
var userId = extras?.Item1;
|
|
var userInitiated = extras?.Item2;
|
|
var expired = extras?.Item3;
|
|
// TODO make specific to userId
|
|
// await ASCredentialIdentityStore.SharedStore?.RemoveAllCredentialIdentitiesAsync();
|
|
}
|
|
}
|
|
else if ((message.Command == "softDeletedCipher" || message.Command == "restoredCipher")
|
|
&& _deviceActionService.SystemMajorVersion() >= 12)
|
|
{
|
|
await ASHelpers.ReplaceAllIdentities();
|
|
}
|
|
else if (message.Command == "vaultTimeoutActionChanged")
|
|
{
|
|
var timeoutAction = await _stateService.GetVaultTimeoutActionAsync();
|
|
if (timeoutAction == VaultTimeoutAction.Logout)
|
|
{
|
|
var userId = await _stateService.GetActiveUserIdAsync();
|
|
// TODO make specific to userId
|
|
// await ASCredentialIdentityStore.SharedStore?.RemoveAllCredentialIdentitiesAsync();
|
|
}
|
|
else
|
|
{
|
|
await ASHelpers.ReplaceAllIdentities();
|
|
}
|
|
}
|
|
});
|
|
|
|
return base.FinishedLaunching(app, options);
|
|
}
|
|
|
|
public override void OnResignActivation(UIApplication uiApplication)
|
|
{
|
|
var view = new UIView(UIApplication.SharedApplication.KeyWindow.Frame)
|
|
{
|
|
Tag = 4321
|
|
};
|
|
var backgroundView = new UIView(UIApplication.SharedApplication.KeyWindow.Frame)
|
|
{
|
|
BackgroundColor = ThemeManager.GetResourceColor("SplashBackgroundColor").ToUIColor()
|
|
};
|
|
var logo = new UIImage(!ThemeManager.UsingLightTheme ? "logo_white.png" : "logo.png");
|
|
var imageView = new UIImageView(logo)
|
|
{
|
|
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);
|
|
base.OnResignActivation(uiApplication);
|
|
}
|
|
|
|
public override void DidEnterBackground(UIApplication uiApplication)
|
|
{
|
|
_stateService?.SetLastActiveTimeAsync(_deviceActionService.GetActiveTime());
|
|
_messagingService?.Send("slept");
|
|
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();
|
|
}
|
|
|
|
ThemeManager.UpdateThemeOnPagesAsync();
|
|
}
|
|
|
|
public override void WillEnterForeground(UIApplication uiApplication)
|
|
{
|
|
_messagingService?.Send("resumed");
|
|
base.WillEnterForeground(uiApplication);
|
|
}
|
|
|
|
public override bool OpenUrl(UIApplication application, NSUrl url, string sourceApplication,
|
|
NSObject annotation)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
|
|
{
|
|
if (Xamarin.Essentials.Platform.OpenUrl(app, url, options))
|
|
{
|
|
return true;
|
|
}
|
|
return base.OpenUrl(app, url, options);
|
|
}
|
|
|
|
public override bool ContinueUserActivity(UIApplication application, NSUserActivity userActivity,
|
|
UIApplicationRestorationHandler completionHandler)
|
|
{
|
|
if (Xamarin.Essentials.Platform.ContinueUserActivity(application, userActivity, completionHandler))
|
|
{
|
|
return true;
|
|
}
|
|
return base.ContinueUserActivity(application, userActivity, completionHandler);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
public void InitApp()
|
|
{
|
|
if (ServiceContainer.RegisteredServices.Count > 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// Migration services
|
|
ServiceContainer.Register<ILogService>("logService", new ConsoleLogService());
|
|
|
|
// 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
|
|
});
|
|
});
|
|
|
|
iOSCoreHelpers.RegisterLocalServices();
|
|
RegisterPush();
|
|
var deviceActionService = ServiceContainer.Resolve<IDeviceActionService>("deviceActionService");
|
|
ServiceContainer.Init(deviceActionService.DeviceUserAgent, Constants.ClearCiphersCacheKey,
|
|
Constants.iOSAllClearCipherCacheKeys);
|
|
iOSCoreHelpers.RegisterAppCenter();
|
|
_pushHandler = new iOSPushNotificationHandler(
|
|
ServiceContainer.Resolve<IPushNotificationListenerService>("pushNotificationListenerService"));
|
|
_nfcDelegate = new Core.NFCReaderDelegate((success, message) =>
|
|
_messagingService.Send("gotYubiKeyOTP", message));
|
|
|
|
iOSCoreHelpers.Bootstrap(async () => await ApplyManagedSettingsAsync());
|
|
}
|
|
|
|
private void RegisterPush()
|
|
{
|
|
var notificationListenerService = new PushNotificationListenerService();
|
|
ServiceContainer.Register<IPushNotificationListenerService>(
|
|
"pushNotificationListenerService", notificationListenerService);
|
|
var iosPushNotificationService = new iOSPushNotificationService();
|
|
ServiceContainer.Register<IPushNotificationService>(
|
|
"pushNotificationService", iosPushNotificationService);
|
|
}
|
|
|
|
private void ShowAppExtension(ExtensionPageViewModel extensionPageViewModel)
|
|
{
|
|
var itemProvider = new NSItemProvider(new NSDictionary(), Core.Constants.UTTypeAppExtensionSetup);
|
|
var extensionItem = new NSExtensionItem
|
|
{
|
|
Attachments = new NSItemProvider[] { itemProvider }
|
|
};
|
|
var activityViewController = new UIActivityViewController(new NSExtensionItem[] { extensionItem }, null)
|
|
{
|
|
CompletionHandler = (activityType, completed) =>
|
|
{
|
|
extensionPageViewModel.EnabledExtension(completed && activityType == iOSCoreHelpers.AppExtensionId);
|
|
}
|
|
};
|
|
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);
|
|
}
|
|
|
|
private void StartEventTimer()
|
|
{
|
|
_eventTimer?.Invalidate();
|
|
_eventTimer?.Dispose();
|
|
_eventTimer = null;
|
|
Device.BeginInvokeOnMainThread(() =>
|
|
{
|
|
_eventTimer = NSTimer.CreateScheduledTimer(60, true, timer =>
|
|
{
|
|
var task = Task.Run(() => _eventService.UploadEventsAsync());
|
|
});
|
|
});
|
|
}
|
|
|
|
private async Task StopEventTimerAsync()
|
|
{
|
|
_eventTimer?.Invalidate();
|
|
_eventTimer?.Dispose();
|
|
_eventTimer = null;
|
|
if (_eventBackgroundTaskId > 0)
|
|
{
|
|
UIApplication.SharedApplication.EndBackgroundTask(_eventBackgroundTaskId);
|
|
_eventBackgroundTaskId = 0;
|
|
}
|
|
_eventBackgroundTaskId = UIApplication.SharedApplication.BeginBackgroundTask(() =>
|
|
{
|
|
UIApplication.SharedApplication.EndBackgroundTask(_eventBackgroundTaskId);
|
|
_eventBackgroundTaskId = 0;
|
|
});
|
|
await _eventService.UploadEventsAsync();
|
|
UIApplication.SharedApplication.EndBackgroundTask(_eventBackgroundTaskId);
|
|
_eventBackgroundTaskId = 0;
|
|
}
|
|
|
|
private async Task ApplyManagedSettingsAsync()
|
|
{
|
|
var userDefaults = NSUserDefaults.StandardUserDefaults;
|
|
var managedSettings = userDefaults.DictionaryForKey("com.apple.configuration.managed");
|
|
if (managedSettings != null && managedSettings.Count > 0)
|
|
{
|
|
var dict = new Dictionary<string, string>();
|
|
foreach (var setting in managedSettings)
|
|
{
|
|
dict.Add(setting.Key.ToString(), setting.Value?.ToString());
|
|
}
|
|
await AppHelpers.SetPreconfiguredSettingsAsync(dict);
|
|
}
|
|
}
|
|
}
|
|
}
|