From f4271563e1bc8021907031df9eaa3bafb8faa5b1 Mon Sep 17 00:00:00 2001 From: Jonathan Dick Date: Mon, 2 May 2022 09:14:28 -0400 Subject: [PATCH] Update WebAuthenticator.android.cs --- .../WebAuthenticator.android.cs | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/Xamarin.Essentials/WebAuthenticator/WebAuthenticator.android.cs b/Xamarin.Essentials/WebAuthenticator/WebAuthenticator.android.cs index 482185ba4..31915a9a1 100644 --- a/Xamarin.Essentials/WebAuthenticator/WebAuthenticator.android.cs +++ b/Xamarin.Essentials/WebAuthenticator/WebAuthenticator.android.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Threading.Tasks; using Android.Content; #if __ANDROID_29__ @@ -71,6 +71,21 @@ static async Task PlatformAuthenticateAsync(WebAuthentic tcsResponse = new TaskCompletionSource(); currentRedirectUri = callbackUrl; + if (!(await StartCustomTabsActivity(url))) + { + // Fall back to opening the system-registered browser if necessary + var urlOriginalString = url.OriginalString; + var browserIntent = new Intent(Intent.ActionView, global::Android.Net.Uri.Parse(urlOriginalString)); + Platform.CurrentActivity.StartActivity(browserIntent); + } + + return await tcsResponse.Task; + } + + static async Task StartCustomTabsActivity(Uri url) + { + // Is only set to true if BindServiceAsync succeeds and no exceptions are thrown + var success = false; var parentActivity = Platform.GetCurrentActivity(true); var customTabsActivityManager = CustomTabsActivityManager.From(parentActivity); @@ -84,16 +99,12 @@ static async Task PlatformAuthenticateAsync(WebAuthentic customTabsIntent.Intent.SetData(global::Android.Net.Uri.Parse(url.OriginalString)); - WebAuthenticatorIntermediateActivity.StartActivity(parentActivity, customTabsIntent.Intent); - } - else - { - // Fall back to opening the system browser if necessary - var browserIntent = new Intent(Intent.ActionView, global::Android.Net.Uri.Parse(url.OriginalString)); - Platform.CurrentActivity.StartActivity(browserIntent); + if (customTabsIntent.Intent.ResolveActivity(parentActivity.PackageManager) != null) + { + WebAuthenticatorIntermediateActivity.StartActivity(parentActivity, customTabsIntent.Intent); + success = true; + } } - - return await tcsResponse.Task; } finally { @@ -105,6 +116,8 @@ static async Task PlatformAuthenticateAsync(WebAuthentic { } } + + return success; } static Task BindServiceAsync(CustomTabsActivityManager manager)