From c7af81bf0c92f55046b785d11184c1aff1daa5bf Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 6 Feb 2017 19:39:07 -0500 Subject: [PATCH] Cleanup hacks because of Intent LaunchedFromHistory bug --- src/Android/AutofillActivity.cs | 52 +++++++++---------- src/Android/MainActivity.cs | 3 +- src/App/App.cs | 45 ++++------------ .../Vault/VaultAutofillListLoginsPage.cs | 2 +- 4 files changed, 36 insertions(+), 66 deletions(-) diff --git a/src/Android/AutofillActivity.cs b/src/Android/AutofillActivity.cs index e9688df02..56c69b45c 100644 --- a/src/Android/AutofillActivity.cs +++ b/src/Android/AutofillActivity.cs @@ -51,39 +51,37 @@ namespace Bit.Android if(data == null) { LastCredentials = null; - return; } - - try + else { - if(data.GetStringExtra("canceled") != null) + try + { + if(data.GetStringExtra("canceled") != null) + { + LastCredentials = null; + } + else + { + var uri = data.GetStringExtra("uri"); + var username = data.GetStringExtra("username"); + var password = data.GetStringExtra("password"); + + LastCredentials = new AutofillCredentials + { + Username = username, + Password = password, + Uri = uri, + LastUri = _lastQueriedUri + }; + } + } + catch { LastCredentials = null; } - else - { - var uri = data.GetStringExtra("uri"); - var username = data.GetStringExtra("username"); - var password = data.GetStringExtra("password"); + } - LastCredentials = new AutofillCredentials - { - Username = username, - Password = password, - Uri = uri, - LastUri = _lastQueriedUri - }; - } - } - catch - { - LastCredentials = null; - } - finally - { - Xamarin.Forms.MessagingCenter.Send(Xamarin.Forms.Application.Current, "SetMainPage"); - Finish(); - } + Finish(); } private void LaunchMainActivity(Intent callingIntent, int requestCode) diff --git a/src/Android/MainActivity.cs b/src/Android/MainActivity.cs index 902116edf..c288b6604 100644 --- a/src/Android/MainActivity.cs +++ b/src/Android/MainActivity.cs @@ -28,7 +28,7 @@ namespace Bit.Android protected override void OnCreate(Bundle bundle) { - var uri = Intent.GetStringExtra("uri"); + var uri = Intent.Flags.HasFlag(ActivityFlags.LaunchedFromHistory) ? null : Intent.GetStringExtra("uri"); if(Intent.HasExtra("uri")) { Intent.RemoveExtra("uri"); @@ -99,7 +99,6 @@ namespace Bit.Android private void ReturnCredentials(VaultListPageModel.Login login) { - App.App.FromAutofillService = true; Intent data = new Intent(); if(login == null) { diff --git a/src/App/App.cs b/src/App/App.cs index a9fd2e00d..b81761339 100644 --- a/src/App/App.cs +++ b/src/App/App.cs @@ -34,9 +34,6 @@ namespace Bit.App private readonly IGoogleAnalyticsService _googleAnalyticsService; private readonly ILocalizeService _localizeService; private readonly IAppInfoService _appInfoService; - private CancellationTokenSource _setMainPageCancellationTokenSource = null; - - public static bool FromAutofillService { get; set; } = false; public App( string uri, @@ -68,7 +65,6 @@ namespace Bit.App SetCulture(); SetStyles(); - FromAutofillService = !string.IsNullOrWhiteSpace(_uri); if(authService.IsAuthenticated && _uri != null) { MainPage = new ExtendedNavigationPage(new VaultAutofillListLoginsPage(_uri)); @@ -98,9 +94,9 @@ namespace Bit.App Device.BeginInvokeOnMainThread(() => Logout(args)); }); - MessagingCenter.Subscribe(Current, "SetMainPage", (sender, ms) => + MessagingCenter.Subscribe(Current, "SetMainPage", (sender) => { - _setMainPageCancellationTokenSource = SetMainPageFromAutofill(_setMainPageCancellationTokenSource, ms); + SetMainPageFromAutofill(); }); } @@ -109,7 +105,7 @@ namespace Bit.App // Handle when your app starts await CheckLockAsync(false); - if(!FromAutofillService) + if(string.IsNullOrWhiteSpace(_uri)) { var lastBuild = _settings.GetValueOrDefault(LastBuildKey); if(lastBuild == null || lastBuild != _appInfoService.Build) @@ -129,7 +125,7 @@ namespace Bit.App // Handle when your app sleeps Debug.WriteLine("OnSleep"); - _setMainPageCancellationTokenSource = SetMainPageFromAutofill(_setMainPageCancellationTokenSource, 500); + SetMainPageFromAutofill(); if(Device.OS == TargetPlatform.Android && !TopPageIsLock()) { _settings.AddOrUpdateValue(Constants.LastActivityDate, DateTime.UtcNow); @@ -159,38 +155,15 @@ namespace Bit.App } } - private CancellationTokenSource SetMainPageFromAutofill(CancellationTokenSource previousCts, int delay) + private void SetMainPageFromAutofill() { - if(Device.OS != TargetPlatform.Android) + if(Device.OS != TargetPlatform.Android || string.IsNullOrWhiteSpace(_uri)) { - return null; + return; } - previousCts?.Cancel(); - if(!FromAutofillService || string.IsNullOrWhiteSpace(_uri)) - { - return null; - } - - var cts = new CancellationTokenSource(); - Task.Run(async () => - { - await Task.Delay(delay); - if(cts.Token.IsCancellationRequested) - { - return; - } - - Device.BeginInvokeOnMainThread(() => - { - MainPage = new MainPage(); - }); - - _uri = null; - FromAutofillService = false; - }, cts.Token); - - return cts; + MainPage = new MainPage(); + _uri = null; } private async Task IncrementalSyncAsync() diff --git a/src/App/Pages/Vault/VaultAutofillListLoginsPage.cs b/src/App/Pages/Vault/VaultAutofillListLoginsPage.cs index 4d1e401aa..1fcb6dfca 100644 --- a/src/App/Pages/Vault/VaultAutofillListLoginsPage.cs +++ b/src/App/Pages/Vault/VaultAutofillListLoginsPage.cs @@ -262,7 +262,7 @@ namespace Bit.App.Pages private void ClickedItem(object sender, EventArgs e) { _page.GoogleAnalyticsService.TrackExtensionEvent("Closed", _page.Uri.StartsWith("http") ? "Website" : "App"); - MessagingCenter.Send(Application.Current, "SetMainPage", 0); + MessagingCenter.Send(Application.Current, "SetMainPage"); } } }