예제 #1
0
        public static Task OpenAsync(Uri uri, BrowserLaunchType launchType)
        {
            if (uri == null)
            {
                throw new ArgumentNullException(nameof(uri), "Uri cannot be null.");
            }

            var nativeUri = AndroidUri.Parse(uri.OriginalString);

            switch (launchType)
            {
            case BrowserLaunchType.SystemPreferred:
                var tabsBuilder = new CustomTabsIntent.Builder();
                var tabsIntent  = tabsBuilder.Build();
                tabsBuilder.SetShowTitle(true);
                tabsIntent.LaunchUrl(Platform.CurrentContext, nativeUri);
                break;

            case BrowserLaunchType.External:
                var intent = new Intent(Intent.ActionView, nativeUri);
                intent.SetFlags(ActivityFlags.ClearTop);
                intent.SetFlags(ActivityFlags.NewTask);

                if (!Platform.IsIntentSupported(intent))
                {
                    throw new FeatureNotSupportedException();
                }

                Platform.CurrentContext.StartActivity(intent);
                break;
            }

            return(Task.CompletedTask);
        }
예제 #2
0
        static Task PlatformOpenAsync(Uri uri, BrowserLaunchMode launchMode)
        {
            var nativeUri = AndroidUri.Parse(uri.AbsoluteUri);

            switch (launchMode)
            {
            case BrowserLaunchMode.SystemPreferred:
                var tabsBuilder = new CustomTabsIntent.Builder();
                tabsBuilder.SetShowTitle(true);
                var tabsIntent = tabsBuilder.Build();
                tabsIntent.Intent.SetFlags(ActivityFlags.ClearTop);
                tabsIntent.Intent.SetFlags(ActivityFlags.NewTask);
                tabsIntent.LaunchUrl(Platform.AppContext, nativeUri);
                break;

            case BrowserLaunchMode.External:
                var intent = new Intent(Intent.ActionView, nativeUri);
                intent.SetFlags(ActivityFlags.ClearTop);
                intent.SetFlags(ActivityFlags.NewTask);

                if (!Platform.IsIntentSupported(intent))
                {
                    throw new FeatureNotSupportedException();
                }

                Platform.AppContext.StartActivity(intent);
                break;
            }

            return(Task.CompletedTask);
        }
예제 #3
0
        static Task <bool> PlatformOpenAsync(Uri uri, BrowserLaunchOptions options)
        {
            var nativeUri = AndroidUri.Parse(uri.AbsoluteUri);

            switch (options.LaunchMode)
            {
            case BrowserLaunchMode.SystemPreferred:
                var tabsBuilder = new CustomTabsIntent.Builder();
                tabsBuilder.SetShowTitle(true);
                if (options.PreferredToolbarColor.HasValue)
                {
                    tabsBuilder.SetToolbarColor(options.PreferredToolbarColor.Value.ToInt());
                }
                if (options.TitleMode != BrowserTitleMode.Default)
                {
                    tabsBuilder.SetShowTitle(options.TitleMode == BrowserTitleMode.Show);
                }

                var tabsIntent = tabsBuilder.Build();
                var flags      = ActivityFlags.ClearTop | ActivityFlags.NewTask;
#if __ANDROID_24__
                if (Platform.HasApiLevelN)
                {
                    flags |= ActivityFlags.LaunchAdjacent;
                }
#endif
                tabsIntent.Intent.SetFlags(flags);

#if __ANDROID_25__
                tabsIntent.LaunchUrl(Platform.AppContext, nativeUri);
#else
                tabsIntent.LaunchUrl(Platform.GetCurrentActivity(true), nativeUri);
#endif
                break;

            case BrowserLaunchMode.External:
                var intent = new Intent(Intent.ActionView, nativeUri);
                flags = ActivityFlags.ClearTop | ActivityFlags.NewTask;
#if __ANDROID_24__
                if (Platform.HasApiLevelN)
                {
                    flags |= ActivityFlags.LaunchAdjacent;
                }
#endif
                intent.SetFlags(flags);

                if (!Platform.IsIntentSupported(intent))
                {
                    throw new FeatureNotSupportedException();
                }

                Platform.AppContext.StartActivity(intent);
                break;
            }

            return(Task.FromResult(true));
        }
예제 #4
0
        static async Task <FileResult> PlatformCaptureAsync(MediaPickerOptions options, bool photo)
        {
            await Permissions.EnsureGrantedAsync <Permissions.Camera>();

            await Permissions.EnsureGrantedAsync <Permissions.StorageWrite>();

            var capturePhotoIntent = new Intent(photo ? MediaStore.ActionImageCapture : MediaStore.ActionVideoCapture);

            if (!Platform.IsIntentSupported(capturePhotoIntent))
            {
                throw new FeatureNotSupportedException($"Either there was no camera on the device or '{capturePhotoIntent.Action}' was not added to the <queries> element in the app's manifest file. See more: https://developer.android.com/about/versions/11/privacy/package-visibility");
            }

            capturePhotoIntent.AddFlags(ActivityFlags.GrantReadUriPermission);
            capturePhotoIntent.AddFlags(ActivityFlags.GrantWriteUriPermission);

            try
            {
                var activity = Platform.GetCurrentActivity(true);

                // Create the temporary file
                var ext = photo
                    ? FileSystem.Extensions.Jpg
                    : FileSystem.Extensions.Mp4;
                var fileName = Guid.NewGuid().ToString("N") + ext;
                var tmpFile  = FileSystem.GetEssentialsTemporaryFile(Platform.AppContext.CacheDir, fileName);

                // Set up the content:// uri
                AndroidUri outputUri = null;
                void OnCreate(Intent intent)
                {
                    // Android requires that using a file provider to get a content:// uri for a file to be called
                    // from within the context of the actual activity which may share that uri with another intent
                    // it launches.

                    outputUri ??= FileProvider.GetUriForFile(tmpFile);

                    intent.PutExtra(MediaStore.ExtraOutput, outputUri);
                }

                // Start the capture process
                await IntermediateActivity.StartAsync(capturePhotoIntent, Platform.requestCodeMediaCapture, OnCreate);

                // Return the file that we just captured
                return(new FileResult(tmpFile.AbsolutePath));
            }
            catch (OperationCanceledException)
            {
                return(null);
            }
        }
        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;

            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);
        }
예제 #6
0
        static Task <bool> PlatformOpenAsync(Uri uri, BrowserLaunchOptions options)
        {
            var nativeUri = AndroidUri.Parse(uri.AbsoluteUri);

            switch (options.LaunchMode)
            {
            case BrowserLaunchMode.SystemPreferred:
                var tabsBuilder = new CustomTabsIntent.Builder();
                tabsBuilder.SetShowTitle(true);
                if (options.PreferredToolbarColor.HasValue)
#pragma warning disable CS0618 // Type or member is obsolete
                {
                    tabsBuilder.SetToolbarColor(options.PreferredToolbarColor.Value.ToInt());
                }
#pragma warning restore CS0618 // Type or member is obsolete
                if (options.TitleMode != BrowserTitleMode.Default)
                {
                    tabsBuilder.SetShowTitle(options.TitleMode == BrowserTitleMode.Show);
                }

                var           tabsIntent = tabsBuilder.Build();
                ActivityFlags?tabsFlags  = null;

                Context context = Platform.GetCurrentActivity(false);

                if (context == null)
                {
                    context = Platform.AppContext;

                    // If using ApplicationContext we need to set ClearTop/NewTask (See #225)
                    tabsFlags = ActivityFlags.ClearTop | ActivityFlags.NewTask;
                }

#if __ANDROID_24__
                if (Platform.HasApiLevelN && options.HasFlag(BrowserLaunchFlags.LaunchAdjacent))
                {
                    if (tabsFlags.HasValue)
                    {
                        tabsFlags |= ActivityFlags.LaunchAdjacent | ActivityFlags.NewTask;
                    }
                    else
                    {
                        tabsFlags = ActivityFlags.LaunchAdjacent | ActivityFlags.NewTask;
                    }
                }
#endif

                // Check if there's flags specified to use
                if (tabsFlags.HasValue)
                {
                    tabsIntent.Intent.SetFlags(tabsFlags.Value);
                }

                tabsIntent.LaunchUrl(context, nativeUri);

                break;

            case BrowserLaunchMode.External:
                var intent = new Intent(Intent.ActionView, nativeUri);
                var flags  = ActivityFlags.ClearTop | ActivityFlags.NewTask;
#if __ANDROID_24__
                if (Platform.HasApiLevelN && options.HasFlag(BrowserLaunchFlags.LaunchAdjacent))
                {
                    flags |= ActivityFlags.LaunchAdjacent;
                }
#endif
                intent.SetFlags(flags);

                if (!Platform.IsIntentSupported(intent))
                {
                    throw new FeatureNotSupportedException();
                }

                Platform.AppContext.StartActivity(intent);
                break;
            }

            return(Task.FromResult(true));
        }
예제 #7
0
        static async 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
            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
                {
                }
            }
        }