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 = ActivityStateManager.Default.GetCurrentActivity(true); var customTabsActivityManager = CustomTabsActivityManager.From(parentActivity); try { if (await BindServiceAsync(customTabsActivityManager)) { var customTabsIntent = new CustomTabsIntent.Builder(customTabsActivityManager.Session) .SetShowTitle(true) .Build(); customTabsIntent.Intent.SetData(global::Android.Net.Uri.Parse(url.OriginalString)); if (customTabsIntent.Intent.ResolveActivity(parentActivity.PackageManager) != null) { WebAuthenticatorIntermediateActivity.StartActivity(parentActivity, customTabsIntent.Intent); success = true; } } } finally { try { customTabsActivityManager.Client?.Dispose(); } finally { } } return(success); }
public async Task <WebAuthenticatorResult> AuthenticateAsync(WebAuthenticatorOptions webAuthenticatorOptions) { var url = webAuthenticatorOptions?.Url; var callbackUrl = webAuthenticatorOptions?.CallbackUrl; var packageName = Application.Context.PackageName; // Create an intent to see if the app developer wired up the callback activity correctly var intent = new Intent(Intent.ActionView); intent.AddCategory(Intent.CategoryBrowsable); intent.AddCategory(Intent.CategoryDefault); intent.SetPackage(packageName); intent.SetData(global::Android.Net.Uri.Parse(callbackUrl.OriginalString)); // Try to find the activity for the callback intent if (!PlatformUtils.IsIntentSupported(intent, packageName)) { throw new InvalidOperationException($"You must subclass the `{nameof(WebAuthenticatorCallbackActivity)}` and create an IntentFilter for it which matches your `{nameof(callbackUrl)}`."); } // Cancel any previous task that's still pending if (tcsResponse?.Task != null && !tcsResponse.Task.IsCompleted) { tcsResponse.TrySetCanceled(); } tcsResponse = new TaskCompletionSource <WebAuthenticatorResult>(); currentRedirectUri = callbackUrl; var parentActivity = ActivityStateManager.Default.GetCurrentActivity(true); var customTabsActivityManager = CustomTabsActivityManager.From(parentActivity); try { if (await BindServiceAsync(customTabsActivityManager)) { var customTabsIntent = new CustomTabsIntent.Builder(customTabsActivityManager.Session) .SetShowTitle(true) .Build(); 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)); ActivityStateManager.Default.GetCurrentActivity().StartActivity(browserIntent); } return(await tcsResponse.Task); } finally { try { customTabsActivityManager.Client?.Dispose(); } finally { } } }