<commit_before>﻿using System;
using System.ComponentModel;
using Bloom.Book;
using Bloom.Communication;
using L10NSharp;
using SIL.IO;
using SIL.Progress;
using SIL.Reporting;

namespace Bloom.Publish.Android
{
	public class UsbPublisher
	{
		public Action Stopped;

		private readonly IProgress _progress;
		private readonly IAndroidDeviceUsbConnection _androidDeviceUsbConnection;
		private DeviceNotFoundReportType _previousDeviceNotFoundReportType;

		public UsbPublisher(IProgress progress)
		{
			_progress = progress;
#if !__MonoCS__
			_androidDeviceUsbConnection = new AndroidDeviceUsbConnection();
#else
			_androidDeviceUsbConnection = new UnimplementedAndroidDeviceUsbConnection();
#endif
		}

		/// <summary>
		/// Attempt to connect to a device
		/// </summary>
		public void Connect()
		{
			try
			{
				_progress.WriteMessage(LocalizationManager.GetString("Publish.Android.Usb.LookingForDevice",
					"Looking for an Android device connected by USB cable and set up for MTP..."));

				_androidDeviceUsbConnection.OneReadyDeviceFound += OneReadyDeviceFound;
				_androidDeviceUsbConnection.OneReadyDeviceNotFound += OneReadyDeviceNotFound;

				var backgroundWorker = new BackgroundWorker();
				backgroundWorker.DoWork += (sender, args) => _androidDeviceUsbConnection.FindDevice();
				backgroundWorker.RunWorkerCompleted += (sender, args) =>
				{
					if (args.Error != null)
					{
						UsbFailConnect(args.Error);
					}
				};
				backgroundWorker.RunWorkerAsync();
			}
			catch (Exception e)
			{
				UsbFailConnect(e);
			}
		}

		public void Stop()
		{
			_progress.WriteMessage("Stopped");
			_androidDeviceUsbConnection.StopFindingDevice();
			_androidDeviceUsbConnection.OneReadyDeviceFound -= OneReadyDeviceFound;
			_androidDeviceUsbConnection.OneReadyDeviceNotFound -= OneReadyDeviceNotFound;
		}

		private void UsbFailConnect(Exception e)
		{
			var unableToConnectMessage = LocalizationManager.GetString("Publish.Android.Usb.UnableToConnect",
				"Unable to connect to any Android device which has Bloom Reader.");
			Stop();
			_progress.WriteError(unableToConnectMessage);
			_progress.WriteError("\tTechnical details to share with the development team: " + e);
			Logger.WriteError(e);
			Stopped();
		}

		private void OneReadyDeviceFound(object sender, EventArgs args)
		{
			_androidDeviceUsbConnection.OneReadyDeviceFound -= OneReadyDeviceFound;
			_androidDeviceUsbConnection.OneReadyDeviceNotFound -= OneReadyDeviceNotFound;

			_progress.WriteMessage(String.Format(LocalizationManager.GetString(
				"Publish.Android.Usb.UsbConnected",
				"UsbConnected to {0}...", "{0} is a device name"), _androidDeviceUsbConnection.GetDeviceName()));

			Stopped();
		}

		private void OneReadyDeviceNotFound(object sender, OneReadyDeviceNotFoundEventArgs eventArgs)
		{
			// Don't report the same thing over and over
			if (_previousDeviceNotFoundReportType == eventArgs.ReportType)
				return;

			_previousDeviceNotFoundReportType = eventArgs.ReportType;

			switch (eventArgs.ReportType)
			{
				case DeviceNotFoundReportType.NoDeviceFound:
					_progress.WriteWarning(LocalizationManager.GetString("Publish.Android.Usb.NoDeviceFound",
						"No device found. Still looking..."));
					break;
				case DeviceNotFoundReportType.NoBloomDirectory:
					_progress.WriteWarning(LocalizationManager.GetString("Publish.Android.Usb.DeviceWithoutBloomReader",
						"The following devices are connected but do not seem to have Bloom Reader installed."));
					foreach (var deviceName in eventArgs.DeviceNames)
						_progress.WriteWarning($"\t{deviceName}");
					break;
				case DeviceNotFoundReportType.MoreThanOneReadyDevice:
					_progress.WriteWarning(LocalizationManager.GetString("Publish.Android.Usb.MoreThanOne",
						"The following connected devices all have Bloom Reader installed. Please connect only one of these devices."));
					foreach (var deviceName in eventArgs.DeviceNames)
						_progress.WriteWarning($"\t{deviceName}");
					break;
			}<commit_msg>Tweak USB error message<commit_after>﻿using System;
using System.ComponentModel;
using Bloom.Book;
using Bloom.Communication;
using L10NSharp;
using SIL.IO;
using SIL.Progress;
using SIL.Reporting;

namespace Bloom.Publish.Android
{
	public class UsbPublisher
	{
		public Action Stopped;

		private readonly IProgress _progress;
		private readonly IAndroidDeviceUsbConnection _androidDeviceUsbConnection;
		private DeviceNotFoundReportType _previousDeviceNotFoundReportType;

		public UsbPublisher(IProgress progress)
		{
			_progress = progress;
#if !__MonoCS__
			_androidDeviceUsbConnection = new AndroidDeviceUsbConnection();
#else
			_androidDeviceUsbConnection = new UnimplementedAndroidDeviceUsbConnection();
#endif
		}

		/// <summary>
		/// Attempt to connect to a device
		/// </summary>
		public void Connect()
		{
			try
			{
				_progress.WriteMessage(LocalizationManager.GetString("Publish.Android.Usb.LookingForDevice",
					"Looking for an Android device connected by USB cable and set up for MTP..."));

				_androidDeviceUsbConnection.OneReadyDeviceFound += OneReadyDeviceFound;
				_androidDeviceUsbConnection.OneReadyDeviceNotFound += OneReadyDeviceNotFound;

				var backgroundWorker = new BackgroundWorker();
				backgroundWorker.DoWork += (sender, args) => _androidDeviceUsbConnection.FindDevice();
				backgroundWorker.RunWorkerCompleted += (sender, args) =>
				{
					if (args.Error != null)
					{
						UsbFailConnect(args.Error);
					}
				};
				backgroundWorker.RunWorkerAsync();
			}
			catch (Exception e)
			{
				UsbFailConnect(e);
			}
		}

		public void Stop()
		{
			_progress.WriteMessage("Stopped");
			_androidDeviceUsbConnection.StopFindingDevice();
			_androidDeviceUsbConnection.OneReadyDeviceFound -= OneReadyDeviceFound;
			_androidDeviceUsbConnection.OneReadyDeviceNotFound -= OneReadyDeviceNotFound;
		}

		private void UsbFailConnect(Exception e)
		{
			var unableToConnectMessage = LocalizationManager.GetString("Publish.Android.Usb.UnableToConnect",
				"Unable to connect to any Android device which has Bloom Reader.");
			Stop();
			_progress.WriteError(unableToConnectMessage);
			_progress.WriteError("\tTechnical details to share with the development team: " + e);
			Logger.WriteError(e);
			Stopped();
		}

		private void OneReadyDeviceFound(object sender, EventArgs args)
		{
			_androidDeviceUsbConnection.OneReadyDeviceFound -= OneReadyDeviceFound;
			_androidDeviceUsbConnection.OneReadyDeviceNotFound -= OneReadyDeviceNotFound;

			_progress.WriteMessage(String.Format(LocalizationManager.GetString(
				"Publish.Android.Usb.UsbConnected",
				"UsbConnected to {0}...", "{0} is a device name"), _androidDeviceUsbConnection.GetDeviceName()));

			Stopped();
		}

		private void OneReadyDeviceNotFound(object sender, OneReadyDeviceNotFoundEventArgs eventArgs)
		{
			// Don't report the same thing over and over
			if (_previousDeviceNotFoundReportType == eventArgs.ReportType)
				return;

			_previousDeviceNotFoundReportType = eventArgs.ReportType;

			switch (eventArgs.ReportType)
			{
				case DeviceNotFoundReportType.NoDeviceFound:
					_progress.WriteWarning(LocalizationManager.GetString("Publish.Android.Usb.NoDeviceFound",
						"No device found. Still looking..."));
					break;
				case DeviceNotFoundReportType.NoBloomDirectory:
					_progress.WriteWarning(LocalizationManager.GetString("Publish.Android.Usb.DeviceWithoutBloomReader",
						// I made this "running" instead of "installed" because I'm assuming
						// we wouldn't get a bloom directory just from installing. We don't actually need it to be
						// running, but this keeps the instructions simple.
						"The following devices are connected but do not seem to have Bloom Reader running:"));
					foreach (var deviceName in eventArgs.DeviceNames)
						_progress.WriteWarning($"\t{deviceName}");
					break;
				case DeviceNotFoundReportType.MoreThanOneReadyDevice:
					_progress.WriteWarning(LocalizationManager.GetString("Publish.Android.Usb.MoreThanOne",
						"The following connected devices all have Bloom Reader installed. Please connect only one of these devices."));
					foreach (var deviceName in eventArgs.DeviceNames)
						_progress.WriteWarning($"\t{deviceName}");
					break;
			}
{'fraction_non_alphanumeric': 0.08879184861717612, 'fraction_numerical': 0.0009704027171276079, 'mean_word_length': 4.597420230821453, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '6530960', 'n_tokens_mistral': 2732, 'n_tokens_neox': 2243, 'n_words': 524}