telegps: Auto-connect to any base stations plugged in at startup
[fw/altos] / altosui / AltosFlashUI.java
index 5ec5ea8a2bc49a645640a05d8b3d2c1e09afa0a8..d8c70a0675136b23112bbd18b9edd7cc756ef5e8 100644 (file)
@@ -23,8 +23,8 @@ import javax.swing.*;
 import javax.swing.filechooser.FileNameExtensionFilter;
 import java.io.*;
 import java.util.concurrent.*;
-import org.altusmetrum.altoslib_2.*;
-import org.altusmetrum.altosuilib_1.*;
+import org.altusmetrum.altoslib_4.*;
+import org.altusmetrum.altosuilib_2.*;
 
 public class AltosFlashUI
        extends AltosUIDialog
@@ -69,9 +69,18 @@ public class AltosFlashUI
        };
 
        private boolean is_pair_programmed() {
-               String  name = file.getName();
-               for (int i = 0; i < pair_programmed.length; i++) {
-                       if (name.startsWith(pair_programmed[i]))
+
+               if (file != null) {
+                       String  name = file.getName();
+                       for (int i = 0; i < pair_programmed.length; i++) {
+                               if (name.startsWith(pair_programmed[i]))
+                                       return true;
+                       }
+               }
+               if (device != null) {
+                       if (!device.matchProduct(AltosLib.product_altusmetrum) &&
+                           (device.matchProduct(AltosLib.product_teledongle) ||
+                            device.matchProduct(AltosLib.product_telebt)))
                                return true;
                }
                return false;
@@ -222,8 +231,8 @@ public class AltosFlashUI
                javax.swing.filechooser.FileFilter ihx_filter = new FileNameExtensionFilter("Flash Image", "ihx");
                hexfile_chooser.addChoosableFileFilter(ihx_filter);
                hexfile_chooser.setFileFilter(ihx_filter);
-               
-               if (!device.matchProduct(AltosLib.product_altusmetrum)) {
+
+               if (!is_pair_programmed() && !device.matchProduct(AltosLib.product_altusmetrum)) {
                        for (int i = 0; i < filters.length; i++) {
                                if (device != null && device.matchProduct(filters[i].product))
                                        hexfile_chooser.setFileFilter(filters[i]);
@@ -238,7 +247,7 @@ public class AltosFlashUI
                if (file == null)
                        return false;
                AltosUIPreferences.set_firmwaredir(file.getParentFile());
-               
+
                return true;
        }
 
@@ -356,7 +365,7 @@ public class AltosFlashUI
 
        flash_task      flasher;
 
-       private boolean open_device() {
+       private boolean open_device() throws InterruptedException {
                try {
                        link = new AltosSerial(device);
                        if (is_pair_programmed())
@@ -368,7 +377,15 @@ public class AltosFlashUI
                        while (!link.is_loader()) {
                                link.to_loader();
 
-                               java.util.List<AltosDevice> devices = AltosUSBDevice.list(AltosLib.product_altusmetrum);
+                               java.util.List<AltosDevice> devices = null;
+
+                               for (int tries = 0; tries < 10; tries++) {
+                                       Thread.sleep(100);
+                                       devices = AltosUSBDevice.list(AltosLib.product_altusmetrum);
+                                       if (devices.size() != 0)
+                                               break;
+                               }
+
                                if (devices.size() == 1)
                                        device = devices.get(0);
                                else {
@@ -399,8 +416,12 @@ public class AltosFlashUI
                        return;
                if (!select_source_file())
                        return;
-               if (!open_device())
+               try {
+                       if (!open_device())
+                               return;
+               } catch (InterruptedException ie) {
                        return;
+               }
                build_dialog();
                flash_task      f = new flash_task(this);
        }
@@ -416,4 +437,4 @@ public class AltosFlashUI
 
                frame = in_frame;
        }
-}
\ No newline at end of file
+}