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

Commit

Permalink
Update WebAuthenticator.android.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
Redth authored May 2, 2022
1 parent bc74dce commit f427156
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions Xamarin.Essentials/WebAuthenticator/WebAuthenticator.android.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Threading.Tasks;
using Android.Content;
#if __ANDROID_29__
Expand Down 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 f427156

Please sign in to comment.