private void HandleOneReadyDeviceNotFound(DeviceNotFoundReportType reportType, List <string> deviceNames) { // Don't report the same thing over and over if (_previousDeviceNotFoundReportType == reportType) { return; } _previousDeviceNotFoundReportType = reportType; switch (reportType) { case DeviceNotFoundReportType.NoDeviceFound: _progress.Message("NoDeviceFound", "No device found. Still looking..."); break; case DeviceNotFoundReportType.MoreThanOneReadyDevice: _progress.Message(idSuffix: "MoreThanOne", message: "Please connect only one of the following devices."); foreach (var deviceName in deviceNames) { _progress.MessageWithoutLocalizing($"\t{deviceName}"); } break; } }
/// <summary> /// Find the one Android device /// (indicated by the presence of the Android directory as a direct child of a root storage object) /// and send to it. /// </summary> /// <returns>true if it found one ready device</returns> private bool ConnectAndSendToOneDeviceInternal(IEnumerable <IDevice> devices, Book.Book book, Color backColor, AndroidPublishSettings settings = null) { List <IDevice> applicableDevices = new List <IDevice>(); foreach (var device in devices) { var androidFolderPath = GetAndroidFolderPath(device); if (androidFolderPath != null) { applicableDevices.Add(device); _bloomFolderPath = Path.GetDirectoryName(androidFolderPath) + "\\" + kBloomFolderOnDevice; _device = device; } } if (applicableDevices.Count == 1) { try { _device.CreateFolderObjectFromPath(_bloomFolderPath); } catch (Exception e) { SIL.Reporting.Logger.WriteError("Unable to create Bloom folder on device.", e); // Treat it as a no-device situation. _bloomFolderPath = null; _device = null; OneReadyDeviceNotFound?.Invoke(DeviceNotFoundReportType.NoDeviceFound, new List <string>(0)); return(false); } OneReadyDeviceFound(book, backColor, settings); return(true); } _bloomFolderPath = null; _device = null; DeviceNotFoundReportType deviceNotFoundReportType = applicableDevices.Count > 1 ? DeviceNotFoundReportType.MoreThanOneReadyDevice : DeviceNotFoundReportType.NoDeviceFound; OneReadyDeviceNotFound?.Invoke(deviceNotFoundReportType, devices.Select(d => d.Name).ToList()); return(false); }
/// <summary> /// Attempt to connect to a device /// </summary> /// <param name="book"></param> public void Connect(Book.Book book, Color backColor) { try { // Calls to this come from JavaScript, not sure they will always be on the UI thread. // Before I added this, I definitely saw race conditions with more than one thread trying // to figure out what was connected. lock (this) { AndroidView.CheckBookLayout(book, _progress); if (_connectionHandler != null) { // we're in an odd state...should only be able to click the button that calls this // while stopped. // Try to really get into the right state in case the user tries again. _androidDeviceUsbConnection.StopFindingDevice(); return; } // Create this while locked...once we have it, can't enter the main logic of this method // on another thread. _connectionHandler = new BackgroundWorker(); } _progress.Message(id: "LookingForDevice", message: "Looking for an Android device connected by USB cable and set up for MTP...", comment: "This is a progress message; MTP is an acronym for the system that allows computers to access files on devices."); _androidDeviceUsbConnection.OneReadyDeviceFound = HandleFoundAReadyDevice; _androidDeviceUsbConnection.OneReadyDeviceNotFound = HandleFoundOneNonReadyDevice; // Don't suppress the first message after (re)starting. _previousDeviceNotFoundReportType = DeviceNotFoundReportType.Unknown; _connectionHandler.DoWork += (sender, args) => _androidDeviceUsbConnection.ConnectAndSendToOneDevice(book, backColor); _connectionHandler.RunWorkerCompleted += (sender, args) => { if (args.Error != null) { UsbFailConnect(args.Error); } _connectionHandler = null; // now OK to try to connect again. }; _connectionHandler.RunWorkerAsync(); } catch (Exception e) { UsbFailConnect(e); } }
/// <summary> /// Find the one with Bloom Reader installed (indicated by the presence of the Bloom directory /// as a direct child of a root storage object) /// </summary> /// <param name="devices"></param> /// <returns>true if it found a ready device</returns> private bool ConnectAndSendToOneDeviceInternal(IEnumerable <IDevice> devices, Book.Book book, Color backColor) { List <IDevice> applicableDevices = new List <IDevice>(); int totalDevicesFound = 0; foreach (var device in devices) { _bloomFolderPath = GetBloomFolderPath(device); if (_bloomFolderPath != null) { applicableDevices.Add(device); } totalDevicesFound++; } if (applicableDevices.Count == 1) { _device = applicableDevices[0]; // Without this, we're depending on the LAST device we tried being the applicable one. _bloomFolderPath = GetBloomFolderPath(_device); OneReadyDeviceFound(book, backColor); return(true); } _bloomFolderPath = null; if (totalDevicesFound > 0 && applicableDevices.Count == 0) { OneReadyDeviceNotFound(DeviceNotFoundReportType.NoBloomDirectory, devices.Select(d => d.Name).ToList()); return(false); } DeviceNotFoundReportType deviceNotFoundReportType = DeviceNotFoundReportType.NoDeviceFound; if (applicableDevices.Count > 1) { deviceNotFoundReportType = DeviceNotFoundReportType.MoreThanOneReadyDevice; } OneReadyDeviceNotFound?.Invoke(deviceNotFoundReportType, devices.Select(d => d.Name).ToList()); return(false); }
private void HandleFoundOneNonReadyDevice(DeviceNotFoundReportType reportType, List <string> deviceNames) { // Don't report the same thing over and over if (_previousDeviceNotFoundReportType == reportType) { return; } _previousDeviceNotFoundReportType = reportType; switch (reportType) { case DeviceNotFoundReportType.NoDeviceFound: _progress.Message("NoDeviceFound", "No device found. Still looking..."); break; case DeviceNotFoundReportType.NoBloomDirectory: // 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. _progress.Message(id: "DeviceWithoutBloomReader", message: "The following devices are connected but do not seem to have Bloom Reader running:"); foreach (var deviceName in deviceNames) { _progress.MessageWithoutLocalizing($"\t{deviceName}"); } break; case DeviceNotFoundReportType.MoreThanOneReadyDevice: _progress.Message(id: "MoreThanOne", message: "The following connected devices all have Bloom Reader installed. Please connect only one of these devices."); foreach (var deviceName in deviceNames) { _progress.MessageWithoutLocalizing($"\t{deviceName}"); } break; } }
public OneReadyDeviceNotFoundEventArgs(DeviceNotFoundReportType reportType, List <string> deviceNames) { ReportType = reportType; DeviceNames = deviceNames; }