private Task <WebAuthenticatorResult> AuthenticateAsync(Uri url, Uri callbackUrl)
        {
            if (_tcs?.Task != null && !_tcs.Task.IsCompleted)
            {
                _tcs.TrySetCanceled();
            }

            _tcs = new TaskCompletionSource <WebAuthenticatorResult>();
            _tcs.Task.ContinueWith(t =>
            {
                // Cleanup when done
                if (_customTabs != null)
                {
                    _customTabs.CustomTabsServiceConnected -= CustomTabsActivityManager_CustomTabsServiceConnected;

                    try
                    {
                        _customTabs?.Client?.Dispose();
                    }
                    finally
                    {
                        _customTabs = null;
                    }
                }
            });

            _uri = url;

            _customTabs = CustomTabsActivityManager.From(Platform.CurrentActivity);
            _customTabs.CustomTabsServiceConnected += CustomTabsActivityManager_CustomTabsServiceConnected;

            if (!_customTabs.BindService())
            {
                // 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);
            }

            return(_tcs.Task);
        }
コード例 #2
0
        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);

            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);
        }
コード例 #3
0
        public static void Init(Activity activity)
        {
            if (isInitialised)
            {
                return;
            }

            OnLaunchRequested += (uri, accentColour) =>
            {
                var manager = CustomTabsActivityManager.From(activity);
                manager.CustomTabsServiceConnected += customTabServiceConnected;
                if (!manager.BindService())
                {
                    // Fall back to opening the system browser if necessary
                    var browserIntent = new Intent(Intent.ActionView, Android.Net.Uri.Parse(uri));
                    activity.StartActivity(browserIntent);
                }

                void customTabServiceConnected(ComponentName name, CustomTabsClient client)
                {
                    manager.CustomTabsServiceConnected -= customTabServiceConnected;
                    var intent = new CustomTabsIntent.Builder()
                                 .SetDefaultColorSchemeParams(new CustomTabColorSchemeParams.Builder()
                                                              .SetToolbarColor(accentColour)
                                                              .SetNavigationBarColor(accentColour)
                                                              .Build())
                                 .Build();

                    intent.Intent.AddFlags(ActivityFlags.SingleTop | ActivityFlags.NoHistory | ActivityFlags.NewTask);
                    CustomTabsHelper.AddKeepAliveExtra(activity, intent.Intent);
                    intent.LaunchUrl(activity, Android.Net.Uri.Parse(uri));
                }
            };

            isInitialised = true;
        }
    }
コード例 #4
0
        static async Task <WebAuthenticatorResult> PlatformAuthenticateAsync(WebAuthenticatorOptions webAuthenticatorOptions)
        {
            var url         = webAuthenticatorOptions?.Url;
            var callbackUrl = webAuthenticatorOptions?.CallbackUrl;
            var packageName = Platform.AppContext.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 (!Platform.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 = Platform.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));
                    Platform.CurrentActivity.StartActivity(browserIntent);
                }

                return(await tcsResponse.Task);
            }
            finally
            {
                try
                {
                    customTabsActivityManager.Client?.Dispose();
                }
                finally
                {
                }
            }
        }
コード例 #5
0
        static Task <WebAuthenticatorResult> PlatformAuthenticateAsync(Uri url, Uri callbackUrl)
        {
            var packageName = Platform.AppContext.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
            var c = intent.ResolveActivity(Platform.AppContext.PackageManager);

            if (c == null || c.PackageName != 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>();
            tcsResponse.Task.ContinueWith(t =>
            {
                // Cleanup when done
                if (CustomTabsActivityManager != null)
                {
                    CustomTabsActivityManager.NavigationEvent            -= CustomTabsActivityManager_NavigationEvent;
                    CustomTabsActivityManager.CustomTabsServiceConnected -= CustomTabsActivityManager_CustomTabsServiceConnected;

                    try
                    {
                        CustomTabsActivityManager?.Client?.Dispose();
                    }
                    finally
                    {
                        CustomTabsActivityManager = null;
                    }
                }
            });

            uri         = url;
            RedirectUri = callbackUrl;

            CustomTabsActivityManager = CustomTabsActivityManager.From(Platform.GetCurrentActivity(true));
            CustomTabsActivityManager.NavigationEvent            += CustomTabsActivityManager_NavigationEvent;
            CustomTabsActivityManager.CustomTabsServiceConnected += CustomTabsActivityManager_CustomTabsServiceConnected;

            if (!CustomTabsActivityManager.BindService())
            {
                // 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);
            }

            return(WebAuthenticator.ResponseTask);
        }