<commit_before>using System;
using System.IO;
using Android.App;
using Android.Content;
using Android.Database;
using Android.OS;
using Android.Provider;
using Environment = Android.OS.Environment;
using Path = System.IO.Path;
using Uri = Android.Net.Uri;

namespace Xamarin.Media
{
	[Activity]
	internal class MediaPickerActivity
		: Activity
	{
		internal const string ExtraPath = "path";
		internal const string ExtraLocation = "location";
		internal const string ExtraType = "type";
		internal const string ExtraId = "id";
		internal const string ExtraAction = "action";

		internal static event EventHandler<MediaPickedEventArgs> MediaPicked;

		private int id;
		private string title;
		private string description;
		private string type;
		private Uri path;
		private bool isPhoto;
		private string action;

		private int seconds;
		private VideoQuality quality;

		protected override void OnSaveInstanceState (Bundle outState)
		{
			outState.PutBoolean ("ran", true);
			outState.PutString (MediaStore.MediaColumns.Title, this.title);
			outState.PutString (MediaStore.Images.ImageColumns.Description, this.description);
			outState.PutInt (ExtraId, this.id);
			outState.PutString (ExtraType, this.type);
			outState.PutString (ExtraAction, this.action);
			outState.PutInt (MediaStore.ExtraDurationLimit, this.seconds);
			outState.PutInt (MediaStore.ExtraVideoQuality, (int)this.quality);

			if (this.path != null)
				outState.PutString (ExtraPath, this.path.Path);

			base.OnSaveInstanceState (outState);
		}

		protected override void OnCreate (Bundle savedInstanceState)
		{
			base.OnCreate (savedInstanceState);

			Bundle b = (savedInstanceState ?? Intent.Extras);

			bool ran = b.GetBoolean ("ran", defaultValue: false);

			this.title = b.GetString (MediaStore.MediaColumns.Title);
			this.description = b.GetString (MediaStore.Images.ImageColumns.Description);

			this.id = b.GetInt (ExtraId, 0);
			this.type = b.GetString (ExtraType);
			if (this.type == "image/*")
				this.isPhoto = true;

			this.action = b.GetString (ExtraAction);
			Intent pickIntent = null;
			try
			{
				pickIntent = new Intent (this.action);
				if (this.action == Intent.ActionPick)
					pickIntent.SetType (type);
				else
				{
					if (!this.isPhoto)
					{
						this.seconds = b.GetInt (MediaStore.ExtraDurationLimit, 0);
						if (this.seconds != 0)
							pickIntent.PutExtra (MediaStore.ExtraDurationLimit, seconds);
					}

					this.quality = (VideoQuality)b.GetInt (MediaStore.ExtraVideoQuality, (int)VideoQuality.High);
					pickIntent.PutExtra (MediaStore.ExtraVideoQuality, GetVideoQuality (this.quality));

					if (!ran)
						this.path = GetOutputMediaFile (b.GetString (ExtraPath), this.title);
					else
						this.path = Uri.Parse (b.GetString (ExtraPath));

					if (this.isPhoto && !ran)
					{
						Touch();
						pickIntent.PutExtra (MediaStore.ExtraOutput, this.path);
					}
				}

				if (!ran)
					StartActivityForResult (pickIntent, this.id);
			}
			catch (Exception ex)
			{
				OnMediaPicked (new MediaPickedEventArgs (this.id, ex));
			}
			finally
			{ 
				if (pickIntent != null)
					pickIntent.Dispose();
			}
		}
		
		private void Touch()
		{
			if (this.path.Scheme != "file")
				return;

			File.Create (GetLocalPath (this.path)).Close();
		}

		protected override void OnActivityResult (int requestCode, Result resultCode, Intent data)
		{
			base.OnActivityResult (requestCode, resultCode, data);

			MediaPickedEventArgs args;
			if (resultCode == Result.Canceled)
				args = new MediaPickedEventArgs (requestCode, isCanceled: true);
			else
			{
				string originalPath = null;
				string filePath = null;
				if (this.action != Intent.ActionPick)
				{
					originalPath = this.path.Path;

					if (data != null && data.Data != null)
					{
						originalPath = data.DataString;
						if (TryMoveFile (data.Data))
							filePath = this.path.Path;
					}
					else
						filePath = this.path.Path;
				}
				else if (data != null && data.Data != null)
				{
					originalPath = data.DataString;
					this.path = data.Data;
					filePath = GetFilePathForUri (this.path);
				}

				if (filePath != null)
				{
					var mf = new MediaFile (filePath, () => File.OpenRead (filePath));<commit_msg>And: Further file checks in MediaPicekr<commit_after>using System;
using System.IO;
using Android.App;
using Android.Content;
using Android.Database;
using Android.OS;
using Android.Provider;
using Environment = Android.OS.Environment;
using Path = System.IO.Path;
using Uri = Android.Net.Uri;

namespace Xamarin.Media
{
	[Activity]
	internal class MediaPickerActivity
		: Activity
	{
		internal const string ExtraPath = "path";
		internal const string ExtraLocation = "location";
		internal const string ExtraType = "type";
		internal const string ExtraId = "id";
		internal const string ExtraAction = "action";

		internal static event EventHandler<MediaPickedEventArgs> MediaPicked;

		private int id;
		private string title;
		private string description;
		private string type;
		private Uri path;
		private bool isPhoto;
		private string action;

		private int seconds;
		private VideoQuality quality;

		protected override void OnSaveInstanceState (Bundle outState)
		{
			outState.PutBoolean ("ran", true);
			outState.PutString (MediaStore.MediaColumns.Title, this.title);
			outState.PutString (MediaStore.Images.ImageColumns.Description, this.description);
			outState.PutInt (ExtraId, this.id);
			outState.PutString (ExtraType, this.type);
			outState.PutString (ExtraAction, this.action);
			outState.PutInt (MediaStore.ExtraDurationLimit, this.seconds);
			outState.PutInt (MediaStore.ExtraVideoQuality, (int)this.quality);

			if (this.path != null)
				outState.PutString (ExtraPath, this.path.Path);

			base.OnSaveInstanceState (outState);
		}

		protected override void OnCreate (Bundle savedInstanceState)
		{
			base.OnCreate (savedInstanceState);

			Bundle b = (savedInstanceState ?? Intent.Extras);

			bool ran = b.GetBoolean ("ran", defaultValue: false);

			this.title = b.GetString (MediaStore.MediaColumns.Title);
			this.description = b.GetString (MediaStore.Images.ImageColumns.Description);

			this.id = b.GetInt (ExtraId, 0);
			this.type = b.GetString (ExtraType);
			if (this.type == "image/*")
				this.isPhoto = true;

			this.action = b.GetString (ExtraAction);
			Intent pickIntent = null;
			try
			{
				pickIntent = new Intent (this.action);
				if (this.action == Intent.ActionPick)
					pickIntent.SetType (type);
				else
				{
					if (!this.isPhoto)
					{
						this.seconds = b.GetInt (MediaStore.ExtraDurationLimit, 0);
						if (this.seconds != 0)
							pickIntent.PutExtra (MediaStore.ExtraDurationLimit, seconds);
					}

					this.quality = (VideoQuality)b.GetInt (MediaStore.ExtraVideoQuality, (int)VideoQuality.High);
					pickIntent.PutExtra (MediaStore.ExtraVideoQuality, GetVideoQuality (this.quality));

					if (!ran)
						this.path = GetOutputMediaFile (b.GetString (ExtraPath), this.title);
					else
						this.path = Uri.Parse (b.GetString (ExtraPath));

					if (this.isPhoto && !ran)
					{
						Touch();
						pickIntent.PutExtra (MediaStore.ExtraOutput, this.path);
					}
				}

				if (!ran)
					StartActivityForResult (pickIntent, this.id);
			}
			catch (Exception ex)
			{
				OnMediaPicked (new MediaPickedEventArgs (this.id, ex));
			}
			finally
			{ 
				if (pickIntent != null)
					pickIntent.Dispose();
			}
		}
		
		private void Touch()
		{
			if (this.path.Scheme != "file")
				return;

			File.Create (GetLocalPath (this.path)).Close();
		}

		protected override void OnActivityResult (int requestCode, Result resultCode, Intent data)
		{
			base.OnActivityResult (requestCode, resultCode, data);

			MediaPickedEventArgs args;
			if (resultCode == Result.Canceled)
				args = new MediaPickedEventArgs (requestCode, isCanceled: true);
			else
			{
				string originalPath = null;
				string filePath = null;
				if (this.action != Intent.ActionPick)
				{
					originalPath = this.path.Path;

					if (data != null && data.Data != null)
					{
						originalPath = data.DataString;
						if (TryMoveFile (data.Data))
							filePath = this.path.Path;
					}
					else
						filePath = this.path.Path;
				}
				else if (data != null && data.Data != null)
				{
					originalPath = data.DataString;
					this.path = data.Data;
					filePath = GetFilePathForUri (this.path);
				}

				if (filePath != null && File.Exists (filePath))
				{
					var mf = new MediaFile (filePath, () => File.OpenRead (filePath));
{'fraction_non_alphanumeric': 0.11373742101567985, 'fraction_numerical': 0.0007020828457758015, 'mean_word_length': 3.365168539325843, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '7054538', 'n_tokens_mistral': 3257, 'n_tokens_neox': 2566, 'n_words': 646}