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

Commit 31f3de2

Browse files
authored
Merge pull request #2002 from xamarin/webauth-android-activity-check
Android WebAuthenticator fallback to system browser if Chrome Custom tabs are not available
2 parents bc74dce + a9ad6e2 commit 31f3de2

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

Xamarin.Essentials/WebAuthenticator/WebAuthenticator.android.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,21 @@ static async Task<WebAuthenticatorResult> PlatformAuthenticateAsync(WebAuthentic
7171
tcsResponse = new TaskCompletionSource<WebAuthenticatorResult>();
7272
currentRedirectUri = callbackUrl;
7373

74+
if (!(await StartCustomTabsActivity(url)))
75+
{
76+
// Fall back to opening the system-registered browser if necessary
77+
var urlOriginalString = url.OriginalString;
78+
var browserIntent = new Intent(Intent.ActionView, global::Android.Net.Uri.Parse(urlOriginalString));
79+
Platform.CurrentActivity.StartActivity(browserIntent);
80+
}
81+
82+
return await tcsResponse.Task;
83+
}
84+
85+
static async Task<bool> StartCustomTabsActivity(Uri url)
86+
{
87+
// Is only set to true if BindServiceAsync succeeds and no exceptions are thrown
88+
var success = false;
7489
var parentActivity = Platform.GetCurrentActivity(true);
7590

7691
var customTabsActivityManager = CustomTabsActivityManager.From(parentActivity);
@@ -84,16 +99,12 @@ static async Task<WebAuthenticatorResult> PlatformAuthenticateAsync(WebAuthentic
8499

85100
customTabsIntent.Intent.SetData(global::Android.Net.Uri.Parse(url.OriginalString));
86101

87-
WebAuthenticatorIntermediateActivity.StartActivity(parentActivity, customTabsIntent.Intent);
88-
}
89-
else
90-
{
91-
// Fall back to opening the system browser if necessary
92-
var browserIntent = new Intent(Intent.ActionView, global::Android.Net.Uri.Parse(url.OriginalString));
93-
Platform.CurrentActivity.StartActivity(browserIntent);
102+
if (customTabsIntent.Intent.ResolveActivity(parentActivity.PackageManager) != null)
103+
{
104+
WebAuthenticatorIntermediateActivity.StartActivity(parentActivity, customTabsIntent.Intent);
105+
success = true;
106+
}
94107
}
95-
96-
return await tcsResponse.Task;
97108
}
98109
finally
99110
{
@@ -105,6 +116,8 @@ static async Task<WebAuthenticatorResult> PlatformAuthenticateAsync(WebAuthentic
105116
{
106117
}
107118
}
119+
120+
return success;
108121
}
109122

110123
static Task<bool> BindServiceAsync(CustomTabsActivityManager manager)

0 commit comments

Comments
 (0)