<commit_before>using System;
using System;
using Android.Content;
using System.Threading.Tasks;
using System.Threading;

using Plugin.FilePicker.Abstractions;
using Java.IO;

namespace Plugin.FilePicker
{
	/// <summary>
	/// Implementation for Feature
	/// </summary>
	/// 
	[Android.Runtime.Preserve(AllMembers = true)]
	public class FilePickerImplementation : IFilePicker
	{

		private Context context;
		private int requestId;
		private TaskCompletionSource<FileData> completionSource;

		public FilePickerImplementation()
		{
			this.context = Android.App.Application.Context;
		}

		public async Task<FileData> PickFile()
		{
			var media = await TakeMediaAsync("file/*", Intent.ActionGetContent);

			return media;
		}

		private Task<FileData> TakeMediaAsync(string type, string action)
		{
			int id = GetRequestId();

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

			try
			{
				Intent pickerIntent = new Intent(this.context, typeof(FilePickerActivity));
				pickerIntent.SetFlags(ActivityFlags.NewTask);

				this.context.StartActivity(pickerIntent);

				EventHandler<FilePickerEventArgs> handler = null;
				EventHandler<EventArgs> cancelledHandler = null;

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

					FilePickerActivity.FilePicked -= handler;

					tcs.SetResult(new FileData()
					{
						DataArray = e.FileByte,
						FileName = e.FileName
					});
				};

				cancelledHandler = (s, e) =>
				{
					var tcs = Interlocked.Exchange(ref this.completionSource, null);

					FilePickerActivity.FilePickCancelled -= cancelledHandler;

					tcs.SetResult(null);
				};

				FilePickerActivity.FilePickCancelled += cancelledHandler;
				FilePickerActivity.FilePicked += handler;
			}
			catch (Exception exAct)
			{
				System.Diagnostics.Debug.Write(exAct);
			}

			return completionSource.Task;
		}

		private int GetRequestId()
		{<commit_msg>Properly disconnect eventhandlers when (not) picking a file.<commit_after>using System;
using System;
using Android.Content;
using System.Threading.Tasks;
using System.Threading;

using Plugin.FilePicker.Abstractions;
using Java.IO;

namespace Plugin.FilePicker
{
	/// <summary>
	/// Implementation for Feature
	/// </summary>
	/// 
	[Android.Runtime.Preserve(AllMembers = true)]
	public class FilePickerImplementation : IFilePicker
	{

		private Context context;
		private int requestId;
		private TaskCompletionSource<FileData> completionSource;

		public FilePickerImplementation()
		{
			this.context = Android.App.Application.Context;
		}

		public async Task<FileData> PickFile()
		{
			var media = await TakeMediaAsync("file/*", Intent.ActionGetContent);

			return media;
		}

		private Task<FileData> TakeMediaAsync(string type, string action)
		{
			int id = GetRequestId();

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

			try
			{
				Intent pickerIntent = new Intent(this.context, typeof(FilePickerActivity));
				pickerIntent.SetFlags(ActivityFlags.NewTask);

				this.context.StartActivity(pickerIntent);

				EventHandler<FilePickerEventArgs> handler = null;
				EventHandler<EventArgs> cancelledHandler = null;

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

					FilePickerActivity.FilePicked -= handler;
					FilePickerActivity.FilePickCancelled -= cancelledHandler;

					tcs.SetResult(new FileData()
					{
						DataArray = e.FileByte,
						FileName = e.FileName
					});
				};

				cancelledHandler = (s, e) =>
				{
					var tcs = Interlocked.Exchange(ref this.completionSource, null);

					FilePickerActivity.FilePicked -= handler;
					FilePickerActivity.FilePickCancelled -= cancelledHandler;

					tcs.SetResult(null);
				};

				FilePickerActivity.FilePickCancelled += cancelledHandler;
				FilePickerActivity.FilePicked += handler;
			}
			catch (Exception exAct)
			{
				System.Diagnostics.Debug.Write(exAct);
			}

			return completionSource.Task;
		}

		private int GetRequestId()
		{
{'fraction_non_alphanumeric': 0.10218140068886337, 'fraction_numerical': 0.0, 'mean_word_length': 3.792079207920792, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, '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': '5278038', 'n_tokens_mistral': 1561, 'n_tokens_neox': 1242, 'n_words': 281}