コード例 #1
0
        public void LaunchNativeEmbeddedBrowser(string url)
        {
#pragma warning disable CS0618 // Type or member is obsolete
            var activity = Forms.Context as Activity;
#pragma warning restore CS0618 // Type or member is obsolete

            if (activity == null)
            {
                return;
            }

            var mgr = new CustomTabsActivityManager(activity);

            mgr.CustomTabsServiceConnected += (name, client) =>
            {
                mgr.LaunchUrl(url);
            };

            if (!mgr.BindService())
            {
                var uri    = Android.Net.Uri.Parse(url);
                var intent = new Intent(Intent.ActionView, uri);
                activity.StartActivity(intent);
            }
        }
        public void LaunchNativeEmbeddedBrowser(string url)
        {
            // TODO: We need the current actiivty. Forms.Context is deprecated, but I had issues
            // trying to use Android.App.Appllication.Context when castign to Activity, sooo...?
            var activity = Forms.Context as Activity;

            if (activity == null)
            {
                return;
            }

            customTabs = new CustomTabsActivityManager(activity);
            customTabs.CustomTabsServiceConnected += (name, client) =>
            {
                // Add custom options here, such as Share
            };

            var mgr = new CustomTabsActivityManager(activity);

            mgr.CustomTabsServiceConnected += delegate {
                mgr.LaunchUrl(url);
            };

            mgr.BindService();
        }
コード例 #3
0
        void UseCustomTabsActivityManagerOnly()
        {
            CustomTabsActivityManager mgr = new CustomTabsActivityManager(this);

            mgr.CustomTabsServiceConnected += delegate
            {
                mgr.LaunchUrl(url);
            };
            mgr.BindService();
        }
コード例 #4
0
ファイル: BrowserService.cs プロジェクト: msraom/APO-Chan
    /// <summary>
    /// OpenBrowser
    /// </summary>
    /// <param name="url"></param>
    public static void OpenUrl(Activity activity, string url)
    {
        var builder = new CustomTabsIntent.Builder()
                      .SetToolbarColor(System.Drawing.Color.Black.ToArgb());
        var intent = builder.Build();
        var mgr    = new CustomTabsActivityManager(activity);

        mgr.CustomTabsServiceConnected += delegate {
            mgr.LaunchUrl(url, intent);
        };
        mgr.BindService();
    }
コード例 #5
0
        protected override void OnAttachedToWindow()
        {
            base.OnAttachedToWindow();

            var mgr = new CustomTabsActivityManager(Context as Activity);

            mgr.CustomTabsServiceConnected += (name, client) =>
            {
                mgr.LaunchUrl(_loginUrl.AbsoluteUri);
            };

            mgr.BindService();
        }
コード例 #6
0
        //CustomTabsActivityManager customTabs;

        public void LaunchNativeEmbeddedBrowser(string url)
        {
            // TODO: We need the current actiivty. Forms.Context is deprecated, but I had issues
            // trying to use Android.App.Appllication.Context
            var activity = Forms.Context as Activity;

            if (activity == null)
            {
                return;
            }

            var mgr = new CustomTabsActivityManager(activity);

            mgr.CustomTabsServiceConnected += delegate {
                mgr.LaunchUrl(url);
            };

            mgr.BindService();
        }
コード例 #7
0
        void BeginLoadingInitialUrl()
        {
            var initalUri = state.Authenticator.GetInitialUrlAsync().Result;
            var mgr       = new CustomTabsActivityManager(this);

            mgr.CustomTabsServiceConnected += delegate {
                var builder = new CustomTabsIntent.Builder(mgr.Session);
                builder.EnableUrlBarHiding();
                builder.SetStartAnimations(this, Resource.Animation.slide_in_right, Resource.Animation.slide_out_left);
                builder.SetExitAnimations(this, global::Android.Resource.Animation.SlideInLeft, global::Android.Resource.Animation.SlideOutRight);
                mgr.LaunchUrl(initalUri.AbsoluteUri, builder.Build());
            };
            if (!mgr.BindService())
            {
                var intent = new Intent(Intent.ActionView);
                intent.SetData(global::Android.Net.Uri.Parse(initalUri.AbsoluteUri));
                StartActivity(intent);
            }
        }
コード例 #8
0
        static Task <bool> BindServiceAsync(CustomTabsActivityManager manager)
        {
            var tcs = new TaskCompletionSource <bool>();

            manager.CustomTabsServiceConnected += OnCustomTabsServiceConnected;

            if (!manager.BindService())
            {
                manager.CustomTabsServiceConnected -= OnCustomTabsServiceConnected;
                tcs.TrySetResult(false);
            }

            return(tcs.Task);

            void OnCustomTabsServiceConnected(ComponentName name, CustomTabsClient client)
            {
                manager.CustomTabsServiceConnected -= OnCustomTabsServiceConnected;
                tcs.TrySetResult(true);
            }
        }
コード例 #9
0
        /// <summary>
        /// Opens the URL on a Custom Tab if possible. Otherwise fallsback to opening it on a WebView.
        /// </summary>
        /// <param name="activity"> The host activity. </param>
        /// <param name="custom_tabs_intent"> a CustomTabsIntent to be used if Custom Tabs is available. </param>
        /// <param name="uri"> the Uri to be opened. </param>
        /// <param name="fallback"> a CustomTabFallback to be used if Custom Tabs is not available. </param>
        public /*static*/ void LaunchUrlWithCustomTabsOrFallback
        (
            Activity a,
            CustomTabsIntent custom_tabs_intent,
            Android.Net.Uri u,
            ICustomTabFallback fallback
        )
        {
            uri      = u;
            activity = a;
            string packageName = PackageManagerHelper.GetPackageNameToUse(activity, uri.ToString());

            //If we cant find a package name, it means theres no browser that supports
            //Chrome Custom Tabs installed. So, we fallback to the webview
            if (packageName == null)
            {
                if (fallback != null)
                {
                    fallback.OpenUri(activity, uri);
                }
            }
            else
            {
                custom_tabs_intent.Intent.SetPackage(packageName);

                // direct call to CustomtTasIntent.LaunchUrl was ported from java samples and refactored
                // seems like API changed
                //
                // custom_tabs_intent.LaunchUrl(activity, uri);
                custom_tabs_activit_manager = new CustomTabsActivityManager(activity);
                custom_tabs_activit_manager.CustomTabsServiceConnected += custom_tabs_activit_manager_CustomTabsServiceConnected;
                service_bound = custom_tabs_activit_manager.BindService();

                if (service_bound == false)
                {
                    // No Packages that support CustomTabs
                }
            }

            return;
        }
        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);
        }
        internal static Task <AuthResult> NavigateAsync(Uri navigateUri, Uri redirectUri, global::Android.App.Activity parentActivity)
        {
            isWaiting = true;

            tcsResponse    = new TaskCompletionSource <AuthResult>();
            uri            = navigateUri;
            ParentActivity = parentActivity;
            RedirectUri    = redirectUri;

            CustomTabsActivityManager = new CustomTabsActivityManager(ParentActivity);
            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("http://www.google.com"));
                parentActivity.StartActivity(browserIntent);
            }

            return(WebAuthenticator.ResponseTask);
        }
コード例 #12
0
        public void LaunchNativeEmbeddedBrowser(string url)
        {
            var activity = CrossCurrentActivity.Current.Activity;

            if (activity == null)
            {
                return;
            }

            var customTabsActivityManager = new CustomTabsActivityManager(activity);

            customTabsActivityManager.CustomTabsServiceConnected += (name, client) =>
            {
                customTabsActivityManager.LaunchUrl(url);
            };

            if (!customTabsActivityManager.BindService())
            {
                var uri    = Android.Net.Uri.Parse(url);
                var intent = new Intent(Intent.ActionView, uri);
                activity.StartActivity(intent);
            }
        }
コード例 #13
0
        /// <summary>
        /// Opens the URL on a Custom Tab if possible. Otherwise fallsback to opening it on a WebView.
        /// </summary>
        /// <param name="activity"> The host activity. </param>
        /// <param name="custom_tabs_intent"> a CustomTabsIntent to be used if Custom Tabs is available. </param>
        /// <param name="uri"> the Uri to be opened. </param>
        /// <param name="fallback"> a CustomTabFallback to be used if Custom Tabs is not available. </param>
        public /*static*/ void LaunchUrlWithCustomTabsOrFallback
        (
            Activity a,
            CustomTabsIntent custom_tabs_intent,
            string package_name_for_custom_tabs,
            Android.Net.Uri u,
            ICustomTabFallback fallback
        )
        {
            uri      = u;
            activity = a;
            bool fallback_neccessary = false;

            //If we cant find a package name, it means theres no browser that supports
            //Chrome Custom Tabs installed. So, we fallback to the webview
            if (package_name_for_custom_tabs == null)
            {
                fallback_neccessary = true;
            }
            else
            {
                custom_tabs_activity_manager = new CustomTabsActivityManager(this.activity);

                custom_tabs_activity_manager.BindService(package_name_for_custom_tabs);
                //custom_tabs_intent.Intent.SetPackage(package_name_for_custom_tabs);

                custom_tabs_session = custom_tabs_activity_manager.Session;

                custom_tabs_intent_builder = new CustomTabsIntent.Builder(custom_tabs_session);


                // direct call to CustomtTasIntent.LaunchUrl was ported from java samples and refactored
                // seems like API changed
                //------------------------------------------------------------------------------
                //custom_tabs_intent.LaunchUrl(activity, uri);
                //return;
                //------------------------------------------------------------------------------
                custom_tabs_activity_manager = new CustomTabsActivityManager(activity);
                custom_tabs_intent           = custom_tabs_intent_builder.Build();

                CustomTabsHelper.AddKeepAliveExtra(activity, custom_tabs_intent.Intent);

                custom_tabs_intent.LaunchUrl(activity, uri);
                custom_tabs_activity_manager.CustomTabsServiceConnected +=
                    // custom_tabs_activit_manager_CustomTabsServiceConnected
                    delegate
                {
                    System.Diagnostics.Debug.WriteLine("CustomTabsActivityManager.CustomTabsServiceConnected");

                    custom_tabs_activity_manager.LaunchUrl(uri.ToString());

                    System.Diagnostics.Debug.WriteLine("CustomTabsActivityManager.LaunchUrl");

                    return;
                }
                ;
                System.Diagnostics.Debug.WriteLine($"CustomTabsActivityManager.BindService({package_name_for_custom_tabs})");
                service_bound = custom_tabs_activity_manager.BindService(package_name_for_custom_tabs);
                //custom_tabs_activity_manager.LaunchUrl(uri.ToString());
                //------------------------------------------------------------------------------

                if (service_bound == false)
                {
                    System.Diagnostics.Debug.WriteLine($"FALLBACK: No Packages that support CustomTabs");
                    // No Packages that support CustomTabs
                    fallback_neccessary = true;
                }
            }

            if (fallback_neccessary == true && fallback != null)
            {
                fallback.OpenUri(activity, uri);
            }

            return;
        }
コード例 #14
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);
        }
コード例 #15
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            editText        = FindViewById <EditText> (Resource.Id.edit);
            connectButton   = FindViewById <Button> (Resource.Id.connect_button);
            warmupButton    = FindViewById <Button> (Resource.Id.warmup_button);
            mayLaunchButton = FindViewById <Button> (Resource.Id.may_launch_button);
            launchButton    = FindViewById <Button> (Resource.Id.launch_button);
            Spinner spinner = FindViewById <Spinner> (Resource.Id.spinner);

            customTabs = new CustomTabsActivityManager(this);
            customTabs.CustomTabsServiceConnected += (name, client) => {
                connectButton.Enabled   = false;
                warmupButton.Enabled    = true;
                mayLaunchButton.Enabled = true;
                launchButton.Enabled    = true;
            };
            customTabs.CustomTabsServiceDisconnected += (name) => {
                connectButton.Enabled   = true;
                warmupButton.Enabled    = false;
                mayLaunchButton.Enabled = false;
                launchButton.Enabled    = false;
            };
            customTabs.NavigationEvent += (navigationEvent, extras) => {
                Android.Util.Log.Debug(TAG, "Navigation: " + navigationEvent);
            };


            simpleLaunch        = FindViewById <Button> (Resource.Id.buttonSimpleLaunch);
            simpleLaunch.Click += (sender, e) => {
                var mgr = new CustomTabsActivityManager(this);
                mgr.CustomTabsServiceConnected += delegate {
                    mgr.LaunchUrl("http://xamarin.com");
                };
                mgr.BindService();
            };

            editText.RequestFocus();
            connectButton.Click += delegate {
                if (!customTabs.BindService(packageNameToBind))
                {
                    Toast.MakeText(this, "Custom Tabs Not Supported - try installing Chrome", ToastLength.Long).Show();
                }
            };
            warmupButton.Click += delegate {
                warmupButton.Enabled = customTabs.Warmup();

                Toast.MakeText(this, "Warmed Up", ToastLength.Short).Show();
            };
            mayLaunchButton.Click += delegate {
                var url = editText.Text;

                mayLaunchButton.Enabled = customTabs.MayLaunchUrl(url, null, null);

                Toast.MakeText(this, "May Launch Called", ToastLength.Short).Show();
            };
            launchButton.Click += delegate {
                var url = editText.Text;

                var builder = new CustomTabsIntent.Builder(customTabs.Session)
                              .SetToolbarColor(Color.Argb(255, 52, 152, 219))
                              .SetShowTitle(true)
                              .SetStartAnimations(this, Resource.Animation.slide_in_right, Resource.Animation.slide_out_left)
                              .SetExitAnimations(this, Resource.Animation.slide_in_left, Resource.Animation.slide_out_right);

                prepareMenuItems(builder);
                prepareActionButton(builder);

                var customTabsIntent = builder.Build();

                CustomTabsHelper.AddKeepAliveExtra(this, customTabsIntent.Intent);

                customTabsIntent.LaunchUrl(this, Android.Net.Uri.Parse(url));
            };

            spinner.Adapter = new ArrayAdapter <string>(this, Android.Resource.Layout.SimpleSpinnerDropDownItem,
                                                        CustomTabsHelper.Packages);


            spinner.ItemSelected += (sender, e) => {
                if (string.IsNullOrEmpty(e.Parent.GetItemAtPosition(e.Position).ToString()))
                {
                    packageNameToBind = null;
                }
            };
            spinner.NothingSelected += (sender, e) => {
                packageNameToBind = null;
            };
        }