/// <summary>
        /// Setups the controller.
        /// </summary>
        /// <param name="mpDelegate">The mp delegate.</param>
        /// <param name="sourceType">Type of the source.</param>
        /// <param name="mediaType">Type of the media.</param>
        /// <param name="options">The options.</param>
        /// <returns>MediaPickerController.</returns>
        private static MediaPickerController SetupController(
            MediaPickerDelegate mpDelegate,
            UIImagePickerControllerSourceType sourceType,
            string mediaType,
            MediaStorageOptions options = null)
        {
            var picker = new MediaPickerController(mpDelegate)
            {
                MediaTypes = new[] { mediaType }, SourceType = sourceType
            };

            if (sourceType == UIImagePickerControllerSourceType.Camera)
            {
                if (mediaType == TypeImage && options is CameraMediaStorageOptions)
                {
                    picker.CameraDevice      = GetCameraDevice(((CameraMediaStorageOptions)options).DefaultCamera);
                    picker.CameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Photo;
                }
                else if (mediaType == TypeMovie && options is VideoMediaStorageOptions)
                {
                    var voptions = (VideoMediaStorageOptions)options;

                    picker.CameraDevice         = GetCameraDevice(voptions.DefaultCamera);
                    picker.CameraCaptureMode    = UIImagePickerControllerCameraCaptureMode.Video;
                    picker.VideoQuality         = GetQuailty(voptions.Quality);
                    picker.VideoMaximumDuration = voptions.DesiredLength.TotalSeconds;
                }
            }

            return(picker);
        }
Beispiel #2
0
        /// <summary>
        /// Creates the media intent.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <param name="type">The type of intent.</param>
        /// <param name="action">The action.</param>
        /// <param name="options">The options.</param>
        /// <param name="tasked">if set to <c>true</c> [tasked].</param>
        /// <returns>Intent to create media.</returns>
        private Intent CreateMediaIntent(int id, string type, string action, MediaStorageOptions options, bool tasked = true)
        {
            var pickerIntent = new Intent(Context, typeof(MediaPickerActivity));

            pickerIntent.SetFlags(ActivityFlags.NewTask);
            pickerIntent.PutExtra(MediaPickerActivity.ExtraId, id);
            pickerIntent.PutExtra(MediaPickerActivity.ExtraType, type);
            pickerIntent.PutExtra(MediaPickerActivity.ExtraAction, action);
            pickerIntent.PutExtra(MediaPickerActivity.ExtraTasked, tasked);

            if (options != null)
            {
                pickerIntent.PutExtra(MediaPickerActivity.ExtraPath, options.Directory);
                pickerIntent.PutExtra(MediaStore.Images.ImageColumns.Title, options.Name);

                var vidOptions = options as VideoMediaStorageOptions;
                if (vidOptions != null)
                {
                    pickerIntent.PutExtra(MediaStore.ExtraDurationLimit, (int)vidOptions.DesiredLength.TotalSeconds);
                    pickerIntent.PutExtra(MediaStore.ExtraVideoQuality, (int)vidOptions.Quality);
                }
            }

            return(pickerIntent);
        }
Beispiel #3
0
        /// <summary>
        /// Gets the output file name with folder.
        /// </summary>
        /// <param name="self">The self.</param>
        /// <param name="rootPath">The root folder.</param>
        /// <returns>System.String.</returns>
        public static string GetMediaFileWithPath(this MediaStorageOptions self, string rootPath)
        {
            var isPhoto   = !(self is VideoMediaStorageOptions);
            var name      = (self != null) ? self.Name : null;
            var directory = (self != null) ? self.Directory : null;

            return(MediaFileHelpers.GetMediaFileWithPath(isPhoto, rootPath, directory, name));
        }
Beispiel #4
0
 /// <summary>
 /// Verifies the options.
 /// </summary>
 /// <param name="options">The options.</param>
 /// <exception cref="ArgumentNullException">options</exception>
 /// <exception cref="ArgumentException">options.Directory must be a relative path;options</exception>
 private static void VerifyOptions(MediaStorageOptions options)
 {
     if (options == null)
     {
         throw new ArgumentNullException("options");
     }
     if (options.Directory != null && Path.IsPathRooted(options.Directory))
     {
         throw new ArgumentException("options.Directory must be a relative path", "options");
     }
 }
Beispiel #5
0
        /// <summary>
        /// Gets the unique filepath.
        /// </summary>
        /// <param name="self">The self.</param>
        /// <param name="rootPath">The root folder.</param>
        /// <param name="checkExists">The check exists.</param>
        /// <returns>System.String.</returns>
        public static string GetUniqueMediaFileWithPath(this MediaStorageOptions self, string rootPath,
                                                        Func <string, bool> checkExists)
        {
            var isPhoto = !(self is VideoMediaStorageOptions);
            var path    = self.GetMediaFileWithPath(rootPath);

            var folder = Path.GetDirectoryName(path);
            var name   = Path.GetFileNameWithoutExtension(path);

            return(MediaFileHelpers.GetUniqueMediaFileWithPath(isPhoto, folder, name, checkExists));
        }
Beispiel #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MediaPickerDelegate"/> class.
        /// </summary>
        /// <param name="viewController">The view controller.</param>
        /// <param name="sourceType">Type of the source.</param>
        /// <param name="options">The options.</param>
        internal MediaPickerDelegate(
            UIViewController viewController,
            UIImagePickerControllerSourceType sourceType,
            MediaStorageOptions options)
        {
            _viewController = viewController;
            _source         = sourceType;
            _options        = options ?? new CameraMediaStorageOptions();

            if (viewController != null)
            {
                UIDevice.CurrentDevice.BeginGeneratingDeviceOrientationNotifications();
                _observer = NSNotificationCenter.DefaultCenter.AddObserver(UIDevice.OrientationDidChangeNotification, DidRotate);
            }
        }
		/// <summary>
		/// Initializes a new instance of the <see cref="MediaPickerDelegate"/> class.
		/// </summary>
		/// <param name="viewController">The view controller.</param>
		/// <param name="sourceType">Type of the source.</param>
		/// <param name="options">The options.</param>
		internal MediaPickerDelegate(
			UIViewController viewController,
			UIImagePickerControllerSourceType sourceType,
			MediaStorageOptions options)
		{
			_viewController = viewController;
			_source = sourceType;
			_options = options ?? new CameraMediaStorageOptions();

			if (viewController != null)
			{
				UIDevice.CurrentDevice.BeginGeneratingDeviceOrientationNotifications();
				_observer = NSNotificationCenter.DefaultCenter.AddObserver(UIDevice.OrientationDidChangeNotification, DidRotate);
			}
		}
Beispiel #8
0
        /// <summary>
        /// Takes the media asynchronous.
        /// </summary>
        /// <param name="type">The type of intent.</param>
        /// <param name="action">The action.</param>
        /// <param name="options">The options.</param>
        /// <returns>Task with a return type of MediaFile.</returns>
        /// <exception cref="System.InvalidOperationException">Only one operation can be active at a time.</exception>
        private Task <MediaFile> TakeMediaAsync(string type, string action, MediaStorageOptions options)
        {
            var id = GetRequestId();

            var ntcs = new TaskCompletionSource <MediaFile>(id);

            _completionSource = ntcs;
            //if (Interlocked.CompareExchange(ref _completionSource, ntcs, null) != null)
            //{
            //    throw new InvalidOperationException("Only one operation can be active at a time");
            //}

            Context.StartActivity(CreateMediaIntent(id, type, action, options));

            EventHandler <MediaPickedEventArgs> handler = null;

            handler = (s, e) =>
            {
                var tcs = _completionSource; //Interlocked.Exchange(ref _completionSource, null);

                MediaPickerActivity.MediaPicked -= handler;

                if (e.RequestId != id)
                {
                    return;
                }

                if (e.Error != null)
                {
                    tcs.SetException(e.Error);
                }
                else if (e.IsCanceled)
                {
                    tcs.SetCanceled();
                }
                else
                {
                    tcs.SetResult(e.Media);
                }
                _completionSource = null;
            };

            MediaPickerActivity.MediaPicked += handler;

            return(ntcs.Task);
        }
Beispiel #9
0
 /// <summary>
 /// Verifies the options.
 /// </summary>
 /// <param name="self">The self.</param>
 /// <exception cref="System.ArgumentNullException">options</exception>
 /// <exception cref="System.ArgumentException">options.Directory must be a relative folder;options</exception>
 public static void VerifyOptions(this MediaStorageOptions self)
 {
     if (self == null)
     {
         throw new ArgumentNullException("self");
     }
     //if (!Enum.IsDefined (typeof(MediaFileStoreLocation), options.Location))
     //    throw new ArgumentException ("options.Location is not a member of MediaFileStoreLocation");
     //if (options.Location == MediaFileStoreLocation.Local)
     //{
     //if (String.IsNullOrWhiteSpace (options.Directory))
     //	throw new ArgumentNullException ("options", "For local storage, options.Directory must be set");
     if (Path.IsPathRooted(self.Directory))
     {
         throw new ArgumentException("options.Directory must be a relative folder", "self");
     }
     //}
 }
Beispiel #10
0
		/// <summary>
		/// Verifies the options.
		/// </summary>
		/// <param name="options">The options.</param>
		/// <exception cref="ArgumentNullException">options</exception>
		/// <exception cref="ArgumentException">options.Directory must be a relative path;options</exception>
		private static void VerifyOptions(MediaStorageOptions options)
		{
			if (options == null)
			{
				throw new ArgumentNullException("options");
			}
			if (options.Directory != null && Path.IsPathRooted(options.Directory))
			{
				throw new ArgumentException("options.Directory must be a relative path", "options");
			}
		}
Beispiel #11
0
		/// <summary>
		/// Setups the controller.
		/// </summary>
		/// <param name="mpDelegate">The mp delegate.</param>
		/// <param name="sourceType">Type of the source.</param>
		/// <param name="mediaType">Type of the media.</param>
		/// <param name="options">The options.</param>
		/// <returns>MediaPickerController.</returns>
		private static MediaPickerController SetupController(
			MediaPickerDelegate mpDelegate,
			UIImagePickerControllerSourceType sourceType,
			string mediaType,
			MediaStorageOptions options = null)
		{
		    var picker = new MediaPickerController(mpDelegate) { MediaTypes = new[] { mediaType }, SourceType = sourceType};

		    if (sourceType == UIImagePickerControllerSourceType.Camera)
			{
                if (mediaType == TypeImage && options is CameraMediaStorageOptions)
				{
					picker.CameraDevice = GetCameraDevice(((CameraMediaStorageOptions)options).DefaultCamera);
					picker.CameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Photo;
				}
				else if (mediaType == TypeMovie && options is VideoMediaStorageOptions)
				{
					var voptions = (VideoMediaStorageOptions)options;

					picker.CameraDevice = GetCameraDevice (voptions.DefaultCamera);
					picker.CameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Video;
					picker.VideoQuality = GetQuailty(voptions.Quality);
					picker.VideoMaximumDuration = voptions.DesiredLength.TotalSeconds;
				}
			}

			return picker;
		}
Beispiel #12
0
		/// <summary>
		/// Gets the media asynchronous.
		/// </summary>
		/// <param name="sourceType">Type of the source.</param>
		/// <param name="mediaType">Type of the media.</param>
		/// <param name="options">The options.</param>
		/// <returns>Task&lt;MediaFile&gt;.</returns>
		/// <exception cref="InvalidOperationException">
		/// There's no current active window
		/// or
		/// Could not find current view controller
		/// or
		/// Only one operation can be active at at time
		/// </exception>
		private Task<MediaFile> GetMediaAsync(
			UIImagePickerControllerSourceType sourceType,
			string mediaType,
			MediaStorageOptions options = null)
		{
			var window = UIApplication.SharedApplication.KeyWindow;
			if (window == null)
			{
				throw new InvalidOperationException("There's no current active window");
			}

			var viewController = window.RootViewController;

#if __IOS_10__
			if (viewController == null || (viewController.PresentedViewController != null && viewController.PresentedViewController.GetType() == typeof(UIAlertController)))
			{
				window =
					UIApplication.SharedApplication.Windows.OrderByDescending(w => w.WindowLevel)
						.FirstOrDefault(w => w.RootViewController != null);

				if (window == null)
				{
					throw new InvalidOperationException("Could not find current view controller");
				}

				viewController = window.RootViewController;
			}
#endif 
			while (viewController.PresentedViewController != null)
			{
				viewController = viewController.PresentedViewController;
			}

			var ndelegate = new MediaPickerDelegate(viewController, sourceType, options);
			var od = Interlocked.CompareExchange(ref _pickerDelegate, ndelegate, null);
			if (od != null)
			{
				throw new InvalidOperationException("Only one operation can be active at at time");
			}

			var picker = SetupController(ndelegate, sourceType, mediaType, options);

			if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad
			    && sourceType == UIImagePickerControllerSourceType.PhotoLibrary)
			{
			    ndelegate.Popover = new UIPopoverController(picker)
			    {
			        Delegate = new MediaPickerPopoverDelegate(ndelegate, picker)
			    };
			    ndelegate.DisplayPopover();
			}
			else
			{
				viewController.PresentViewController(picker, true, null);
			}

			return ndelegate.Task.ContinueWith(
				t =>
					{
						if (_popover != null)
						{
							_popover.Dispose();
							_popover = null;
						}

						Interlocked.Exchange(ref _pickerDelegate, null);
						return t;
					}).Unwrap();
		}
Beispiel #13
0
        /// <summary>
        /// Gets the media asynchronous.
        /// </summary>
        /// <param name="sourceType">Type of the source.</param>
        /// <param name="mediaType">Type of the media.</param>
        /// <param name="options">The options.</param>
        /// <returns>Task&lt;MediaFile&gt;.</returns>
        /// <exception cref="InvalidOperationException">
        /// There's no current active window
        /// or
        /// Could not find current view controller
        /// or
        /// Only one operation can be active at at time
        /// </exception>
        private Task <MediaFile> GetMediaAsync(
            UIImagePickerControllerSourceType sourceType,
            string mediaType,
            MediaStorageOptions options = null)
        {
            var window = UIApplication.SharedApplication.KeyWindow;

            if (window == null)
            {
                throw new InvalidOperationException("There's no current active window");
            }

            var viewController = window.RootViewController;

            if (viewController == null)
            {
                window         = UIApplication.SharedApplication.Windows.OrderByDescending(w => w.WindowLevel).FirstOrDefault(w => w.RootViewController != null);
                viewController = window.RootViewController;
            }

#if __IOS_10__
            if (viewController == null || (viewController.PresentedViewController != null && viewController.PresentedViewController.GetType() == typeof(UIAlertController)))
            {
                window =
                    UIApplication.SharedApplication.Windows.OrderByDescending(w => w.WindowLevel)
                    .FirstOrDefault(w => w.RootViewController != null);

                if (window == null)
                {
                    throw new InvalidOperationException("Could not find current view controller");
                }

                viewController = window.RootViewController;
            }
#endif
            while (viewController.PresentedViewController != null)
            {
                viewController = viewController.PresentedViewController;
            }

            var ndelegate = new MediaPickerDelegate(viewController, sourceType, options);
            //var od = Interlocked.CompareExchange(ref _pickerDelegate, ndelegate, null);
            //if (od != null)
            //{
            //	throw new InvalidOperationException("Only one operation can be active at at time");
            //}
            _pickerDelegate = ndelegate;

            var picker = SetupController(ndelegate, sourceType, mediaType, options);

            if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad &&
                sourceType == UIImagePickerControllerSourceType.PhotoLibrary)
            {
                ndelegate.Popover = new UIPopoverController(picker)
                {
                    Delegate = new MediaPickerPopoverDelegate(ndelegate, picker)
                };
                ndelegate.DisplayPopover();
            }
            else
            {
                viewController.PresentViewController(picker, true, null);
            }

            return(ndelegate.Task.ContinueWith(
                       t =>
            {
                if (_popover != null)
                {
                    _popover.Dispose();
                    _popover = null;
                }

                //Interlocked.Exchange(ref _pickerDelegate, null);
                _pickerDelegate = null;
                return t;
            }).Unwrap());
        }
        /// <summary>
        /// Takes the media asynchronous.
        /// </summary>
        /// <param name="type">The type of intent.</param>
        /// <param name="action">The action.</param>
        /// <param name="options">The options.</param>
        /// <returns>Task with a return type of MediaFile.</returns>
        /// <exception cref="System.InvalidOperationException">Only one operation can be active at a time.</exception>
        private Task<MediaFile> TakeMediaAsync(string type, string action, MediaStorageOptions options)
        {
            var id = GetRequestId();

            var ntcs = new TaskCompletionSource<MediaFile>(id);
            if (Interlocked.CompareExchange(ref _completionSource, ntcs, null) != null)
            {
                throw new InvalidOperationException("Only one operation can be active at a time");
            }

            Context.StartActivity(CreateMediaIntent(id, type, action, options));

            EventHandler<MediaPickedEventArgs> handler = null;
            handler = (s, e) =>
            {
                var tcs = Interlocked.Exchange(ref _completionSource, null);

                MediaPickerActivity.MediaPicked -= handler;

                if (e.RequestId != id)
                {
                    return;
                }

                if (e.Error != null)
                {
                    tcs.SetException(e.Error);
                } 
                else if (e.IsCanceled)
                {
                    tcs.SetCanceled();
                }
                else
                {
                    tcs.SetResult(e.Media);
                }
            };

            MediaPickerActivity.MediaPicked += handler;

            return ntcs.Task;
        }
        /// <summary>
        /// Creates the media intent.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <param name="type">The type of intent.</param>
        /// <param name="action">The action.</param>
        /// <param name="options">The options.</param>
        /// <param name="tasked">if set to <c>true</c> [tasked].</param>
        /// <returns>Intent to create media.</returns>
        private Intent CreateMediaIntent(int id, string type, string action, MediaStorageOptions options, bool tasked = true)
        {
            var pickerIntent = new Intent(Context, typeof(MediaPickerActivity));
            pickerIntent.SetFlags(ActivityFlags.NewTask);
            pickerIntent.PutExtra(MediaPickerActivity.ExtraId, id);
            pickerIntent.PutExtra(MediaPickerActivity.ExtraType, type);
            pickerIntent.PutExtra(MediaPickerActivity.ExtraAction, action);
            pickerIntent.PutExtra(MediaPickerActivity.ExtraTasked, tasked);

            if (options != null)
            {
                pickerIntent.PutExtra(MediaPickerActivity.ExtraPath, options.Directory);
                pickerIntent.PutExtra(MediaStore.Images.ImageColumns.Title, options.Name);

                var vidOptions = options as VideoMediaStorageOptions;
                if (vidOptions != null)
                {
                    pickerIntent.PutExtra(MediaStore.ExtraDurationLimit, (int) vidOptions.DesiredLength.TotalSeconds);
                    pickerIntent.PutExtra(MediaStore.ExtraVideoQuality, (int) vidOptions.Quality);
                }
            }

            return pickerIntent;
        }