diff --git a/src/Core/App.xaml.cs b/src/Core/App.xaml.cs index 8124ac3ee..7deed659d 100644 --- a/src/Core/App.xaml.cs +++ b/src/Core/App.xaml.cs @@ -46,6 +46,7 @@ namespace Bit.App // This queue keeps those actions so that when the app has resumed they can still be executed. // Links: https://github.com/dotnet/maui/issues/11501 and https://bitwarden.atlassian.net/wiki/spaces/NMME/pages/664862722/MainPage+Assignments+not+working+on+Android+on+Background+or+App+resume private readonly Queue _onResumeActions = new Queue(); + private bool _hasNavigatedToAutofillWindow; #if ANDROID @@ -89,7 +90,7 @@ namespace Bit.App { Options = appOptions ?? new AppOptions(); } - + protected override Window CreateWindow(IActivationState activationState) { //When executing from AutofillExternalActivity we don't have "Options" so we need to filter "manually" @@ -106,18 +107,37 @@ namespace Bit.App if (Options != null && (Options.FromAutofillFramework || Options.Uri != null || Options.OtpData != null || Options.CreateSend != null)) { _isResumed = true; //Specifically for the Autofill scenario we need to manually set the _isResumed here + _hasNavigatedToAutofillWindow = true; return new AutoFillWindow(new NavigationPage(new AndroidNavigationRedirectPage())); } + var homePage = new HomePage(Options); + // WORKAROUND: If the user autofills with Accessibility Services enabled and goes back to the application then there is currently an issue + // where this method is called again + // thus it goes through here and the user goes to HomePage as we see here. + // So to solve this, the next flag check has been added which then turns on a flag on the home page + // that will trigger a navigation on the accounts manager when it loads; workarounding this behavior and navigating the user + // to the proper page depending on its state. + // WARNING: this doens't navigate the user to where they were but it acts as if the user had changed their account. + if(_hasNavigatedToAutofillWindow) + { + homePage.PerformNavigationOnAccountChangedOnLoad = true; + // this is needed because when coming back from AutofillWindow OnResume won't be called and we need this flag + // so that void Navigate(NavigationTarget navTarget, INavigationParams navParams) doesn't enqueue the navigation + // and it performs it directly. + _isResumed = true; + _hasNavigatedToAutofillWindow = false; + } + //If we have an existing MainAppWindow we can use that one var mainAppWindow = Windows.OfType().FirstOrDefault(); if (mainAppWindow != null) { - mainAppWindow.PendingPage = new NavigationPage(new HomePage(Options)); + mainAppWindow.PendingPage = new NavigationPage(homePage); } //Create new main window - return new MainAppWindow(new NavigationPage(new HomePage(Options))); + return new MainAppWindow(new NavigationPage(homePage)); } #else //iOS doesn't use the CreateWindow override used in Android so we just set the Application.Current.MainPage directly diff --git a/src/Core/Pages/Accounts/HomePage.xaml b/src/Core/Pages/Accounts/HomePage.xaml index 006161248..fe6992155 100644 --- a/src/Core/Pages/Accounts/HomePage.xaml +++ b/src/Core/Pages/Accounts/HomePage.xaml @@ -9,6 +9,7 @@ x:DataType="pages:HomeViewModel" HideSoftInputOnTapped="True" x:Name="_page" + Loaded="HomePage_Loaded" Title="{Binding PageTitle}"> diff --git a/src/Core/Pages/Accounts/HomePage.xaml.cs b/src/Core/Pages/Accounts/HomePage.xaml.cs index 03f8c8da4..11c5b36ba 100644 --- a/src/Core/Pages/Accounts/HomePage.xaml.cs +++ b/src/Core/Pages/Accounts/HomePage.xaml.cs @@ -1,8 +1,8 @@ -using Bit.App.Models; +using Bit.App.Abstractions; +using Bit.App.Models; using Bit.App.Utilities; using Bit.Core.Abstractions; using Bit.Core.Utilities; -using Microsoft.Maui.Platform; namespace Bit.App.Pages { @@ -44,6 +44,21 @@ namespace Bit.App.Pages } } + public bool PerformNavigationOnAccountChangedOnLoad { get; internal set; } + + void HomePage_Loaded(System.Object sender, System.EventArgs e) + { +#if ANDROID + // WORKAROUND: This is needed to fix the navigation when coming back from autofill when Accessibility Services is enabled + // See App.xaml.cs -> CreateWindow(...) for more info. + if (PerformNavigationOnAccountChangedOnLoad && ServiceContainer.TryResolve(out var accountsManager)) + { + PerformNavigationOnAccountChangedOnLoad = false; + accountsManager.NavigateOnAccountChangeAsync().FireAndForget(); + } +#endif + } + public async Task DismissRegisterPageAndLogInAsync(string email) { await Navigation.PopModalAsync();