<commit_before>using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Provider;
using Plugin.Media.Abstractions;
using Plugin.Permissions;
using Android.Media;
using Android.Graphics;
using System.Text.RegularExpressions;
using Plugin.CurrentActivity;
using System.Collections.Generic;
using System.Linq;
using Android.App;
using Permission = Plugin.Permissions.Abstractions.Permission;
using Plugin.Permissions.Abstractions;

namespace Plugin.Media
{
    /// <summary>
    /// Implementation for Feature
    /// </summary>
    [Android.Runtime.Preserve(AllMembers = true)]
    public class MediaImplementation : IMedia
    {
		const string TAG_PIXEL_X_DIMENSION = "PixelXDimension";
		const string TAG_PIXEL_Y_DIMENSION = "PixelYDimension";

	    internal static event EventHandler CancelRequested;

		/// <summary>
		/// Implementation
		/// </summary>
		public MediaImplementation()
        {

            this.context = Application.Context;
            IsCameraAvailable = context.PackageManager.HasSystemFeature(PackageManager.FeatureCamera);

            if (Build.VERSION.SdkInt >= BuildVersionCodes.Gingerbread)
                IsCameraAvailable |= context.PackageManager.HasSystemFeature(PackageManager.FeatureCameraFront);
        }

        ///<inheritdoc/>
        public Task<bool> Initialize() => Task.FromResult(true);

        /// <inheritdoc/>
        public bool IsCameraAvailable { get; }
        /// <inheritdoc/>
        public bool IsTakePhotoSupported => true;

        /// <inheritdoc/>
        public bool IsPickPhotoSupported => true;

        /// <inheritdoc/>
        public bool IsTakeVideoSupported => true;
        /// <inheritdoc/>
        public bool IsPickVideoSupported => true;


        bool IsValidExif(ExifInterface exif)
        {
            //if null, then not falid
            if (exif == null)
                return false;

			try
			{
				//if has thumb, but is <= 0, then not valid
				if (exif.HasThumbnail && (exif.GetThumbnail()?.Length ?? 0) <= 0)
					return false;
			}
			catch(Exception ex)
			{
				System.Diagnostics.Debug.WriteLine("Unable to get thumbnail exif: " + ex);
				return false;
			}

            return true;
        }

        /// <summary>
        /// Picks a photo from the default gallery
        /// </summary>
        /// <returns>Media file or null if canceled</returns>
        public async Task<MediaFile> PickPhotoAsync(PickMediaOptions options = null, CancellationToken token = default(CancellationToken))
        {
            if (!(await RequestStoragePermission()))
            {
                throw new MediaPermissionException(Permission.Storage);
            }
            var media = await TakeMediaAsync("image/*", Intent.ActionPick, null, token);

            if (options == null)
                options = new PickMediaOptions();

            //check to see if we picked a file, and if so then try to fix orientation and resize
            if (!string.IsNullOrWhiteSpace(media?.Path))
            {
                try
                {
	                await FixOrientationAndResize(options, media);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Unable to check orientation: " + ex);
                }
            }

            return media;
        }

	    public async Task<List<MediaFile>> PickPhotosAsync(PickMediaOptions options = null, MultiPickerOptions pickerOptions = null, CancellationToken token = default(CancellationToken))
		{
			if (!await RequestStoragePermission())
			{
				return null;
			}

			var medias = await TakeMediasAsync("image/*", Intent.ActionPick, new StorePickerMediaOptions { MultiPicker = true }, token);

			if (options == null)
				options = new PickMediaOptions();

			foreach (var media in medias)
			{
				//check to see if we picked a file, and if so then try to fix orientation and resize
				if (!string.IsNullOrWhiteSpace(media?.Path))
				{
					try
					{
						await FixOrientationAndResize(options, media);
					}<commit_msg>Fixed Android NRE on canceling/dismissing PickPhotosAsync.<commit_after>using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Provider;
using Plugin.Media.Abstractions;
using Plugin.Permissions;
using Android.Media;
using Android.Graphics;
using System.Text.RegularExpressions;
using Plugin.CurrentActivity;
using System.Collections.Generic;
using System.Linq;
using Android.App;
using Permission = Plugin.Permissions.Abstractions.Permission;
using Plugin.Permissions.Abstractions;

namespace Plugin.Media
{
    /// <summary>
    /// Implementation for Feature
    /// </summary>
    [Android.Runtime.Preserve(AllMembers = true)]
    public class MediaImplementation : IMedia
    {
		const string TAG_PIXEL_X_DIMENSION = "PixelXDimension";
		const string TAG_PIXEL_Y_DIMENSION = "PixelYDimension";

	    internal static event EventHandler CancelRequested;

		/// <summary>
		/// Implementation
		/// </summary>
		public MediaImplementation()
        {

            this.context = Application.Context;
            IsCameraAvailable = context.PackageManager.HasSystemFeature(PackageManager.FeatureCamera);

            if (Build.VERSION.SdkInt >= BuildVersionCodes.Gingerbread)
                IsCameraAvailable |= context.PackageManager.HasSystemFeature(PackageManager.FeatureCameraFront);
        }

        ///<inheritdoc/>
        public Task<bool> Initialize() => Task.FromResult(true);

        /// <inheritdoc/>
        public bool IsCameraAvailable { get; }
        /// <inheritdoc/>
        public bool IsTakePhotoSupported => true;

        /// <inheritdoc/>
        public bool IsPickPhotoSupported => true;

        /// <inheritdoc/>
        public bool IsTakeVideoSupported => true;
        /// <inheritdoc/>
        public bool IsPickVideoSupported => true;


        bool IsValidExif(ExifInterface exif)
        {
            //if null, then not falid
            if (exif == null)
                return false;

			try
			{
				//if has thumb, but is <= 0, then not valid
				if (exif.HasThumbnail && (exif.GetThumbnail()?.Length ?? 0) <= 0)
					return false;
			}
			catch(Exception ex)
			{
				System.Diagnostics.Debug.WriteLine("Unable to get thumbnail exif: " + ex);
				return false;
			}

            return true;
        }

        /// <summary>
        /// Picks a photo from the default gallery
        /// </summary>
        /// <returns>Media file or null if canceled</returns>
        public async Task<MediaFile> PickPhotoAsync(PickMediaOptions options = null, CancellationToken token = default(CancellationToken))
        {
            if (!(await RequestStoragePermission()))
            {
                throw new MediaPermissionException(Permission.Storage);
            }
            var media = await TakeMediaAsync("image/*", Intent.ActionPick, null, token);

            if (options == null)
                options = new PickMediaOptions();

            //check to see if we picked a file, and if so then try to fix orientation and resize
            if (!string.IsNullOrWhiteSpace(media?.Path))
            {
                try
                {
	                await FixOrientationAndResize(options, media);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Unable to check orientation: " + ex);
                }
            }

            return media;
        }

	    public async Task<List<MediaFile>> PickPhotosAsync(PickMediaOptions options = null, MultiPickerOptions pickerOptions = null, CancellationToken token = default(CancellationToken))
		{
			if (!await RequestStoragePermission())
			{
				return null;
			}

			var medias = await TakeMediasAsync("image/*", Intent.ActionPick, new StorePickerMediaOptions { MultiPicker = true }, token);

			if (medias == null)
				return null;

			if (options == null)
				options = new PickMediaOptions();

			foreach (var media in medias)
			{
				//check to see if we picked a file, and if so then try to fix orientation and resize
				if (!string.IsNullOrWhiteSpace(media?.Path))
				{
					try
					{
						await FixOrientationAndResize(options, media);
					}
{'fraction_non_alphanumeric': 0.09754059216809934, 'fraction_numerical': 0.0007163323782234957, 'mean_word_length': 2.3374501992031873, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7652043', 'n_tokens_mistral': 2405, 'n_tokens_neox': 2161, 'n_words': 590}