Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2002 from xamarin/webauth-android-activity-check
Browse files Browse the repository at this point in the history
Android WebAuthenticator fallback to system browser if Chrome Custom tabs are not available
  • Loading branch information
jfversluis committed May 2, 2022
2 parents bc74dce + a9ad6e2 commit 31f3de2
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions Xamarin.Essentials/WebAuthenticator/WebAuthenticator.android.cs
Expand Up @@ -71,6 +71,21 @@ static async Task<WebAuthenticatorResult> PlatformAuthenticateAsync(WebAuthentic
tcsResponse = new TaskCompletionSource<WebAuthenticatorResult>();
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<bool> 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);
Expand All @@ -84,16 +99,12 @@ static async Task<WebAuthenticatorResult> 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
{
Expand All @@ -105,6 +116,8 @@ static async Task<WebAuthenticatorResult> PlatformAuthenticateAsync(WebAuthentic
{
}
}

return success;
}

static Task<bool> BindServiceAsync(CustomTabsActivityManager manager)
Expand Down

0 comments on commit 31f3de2

Please sign in to comment.