diff --git a/Xamarin.Essentials/WebAuthenticator/WebAuthenticator.android.cs b/Xamarin.Essentials/WebAuthenticator/WebAuthenticator.android.cs index 482185ba4..c117f1187 100644 --- a/Xamarin.Essentials/WebAuthenticator/WebAuthenticator.android.cs +++ b/Xamarin.Essentials/WebAuthenticator/WebAuthenticator.android.cs @@ -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)