Commits

Christian Fischer committed 69d5ede

LogCat: multiple devices support (2)

Comments (0)

Files changed (1)

logcat_multiple_devices.patch

 # HG changeset patch
 # Date 1309392817 -7200
-# Parent 6d07d93fb2ccc8e7da47ab364c1ece53a4978d82
+# Parent 20fb85cab183b91583c00d2ddcab6f86ef8e28a1
 LogCat: Added support for multiple devices
 
-diff -r 6d07d93fb2cc logcat/src/org/nyerel/nbandroid/logcat/LogDevicesComboBoxSupport.java
+diff -r 20fb85cab183 logcat/src/org/nyerel/nbandroid/logcat/LogDevicesComboBoxSupport.java
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
-+++ b/logcat/src/org/nyerel/nbandroid/logcat/LogDevicesComboBoxSupport.java	Thu Jun 30 05:20:52 2011 +0200
-@@ -0,0 +1,197 @@
++++ b/logcat/src/org/nyerel/nbandroid/logcat/LogDevicesComboBoxSupport.java	Fri Jul 01 03:54:38 2011 +0200
+@@ -0,0 +1,245 @@
 +/*
 + * Licensed under the Apache License, Version 2.0 (the "License");
 + * you may not use this file except in compliance with the License.
 + */
 +public class LogDevicesComboBoxSupport
 +    implements  ComboBoxModel,
-+                ListCellRenderer<IDevice>,
++                ListCellRenderer,
 +                PropertyChangeListener,
 +                AndroidDebugBridge.IDeviceChangeListener
 +{
 +    }
 +
 +    private void add(IDevice device) {
-+        if(!devices.contains(device)) {
-+            devices.add(device);
++        int insertIndex = devices.size();
++        
++        for(int i=devices.size(); --i>=0;) {
++            if (devices.get(i).getSerialNumber().equals(device.getSerialNumber())) {
++                devices.remove(i);
++                fireListDataEvent(ListDataEvent.INTERVAL_REMOVED, i);
++                
++                // replace this device
++                insertIndex = i;
++                
++                break;
++            }
++        }
++        
++        if (insertIndex != -1) {
++            devices.add(insertIndex, device);
 +
 +            // send notification
-+            ListDataEvent event = new ListDataEvent(
-+                    this,
-+                    ListDataEvent.INTERVAL_ADDED,
-+                    devices.size() - 1,
-+                    devices.size() - 1
-+            );
-+
-+            for(ListDataListener listener : dataListeners) {
-+                listener.intervalAdded(event);
-+            }
++            fireListDataEvent(ListDataEvent.INTERVAL_ADDED, insertIndex);
 +        }
 +
 +        return;
 +    }
 +
-+    private void remove(IDevice decice) {
++    private void remove(IDevice device) {
++       for(int i=devices.size(); --i>=0;) {
++            if (devices.get(i).getSerialNumber().equals(device.getSerialNumber())) {
++                devices.remove(i);
++                fireListDataEvent(ListDataEvent.INTERVAL_REMOVED, i);
++            }
++        }
++        
 +        return;
 +    }
 +
 +    public void removeListDataListener(ListDataListener l) {
 +        dataListeners.remove(l);
 +    }
++    
++    private void fireContentsChangedEvent() {
++        fireListDataEvent(ListDataEvent.CONTENTS_CHANGED, -1);
++    }
++    
++    private void fireListDataEvent(int type, int index) {
++        fireListDataEvent(type, index, index);
++    }
++    
++    private void fireListDataEvent(int type, int index1, int index2) {
++        ListDataEvent event = new ListDataEvent(this, type, index1, index2);
++        
++        for(ListDataListener listener : dataListeners) {
++            switch(type) {
++                case ListDataEvent.CONTENTS_CHANGED: {
++                    listener.contentsChanged(event);
++                    break;
++                }
++                    
++                case ListDataEvent.INTERVAL_ADDED: {
++                    listener.intervalAdded(event);
++                    break;
++                }
++                    
++                case ListDataEvent.INTERVAL_REMOVED: {
++                    listener.intervalRemoved(event);
++                    break;
++                }
++            }
++            
++        }
++        
++        return;
++    }
 +
 +
 +    @Override
-+    public Component getListCellRendererComponent(JList<? extends IDevice> list, IDevice value, int index, boolean isSelected, boolean cellHasFocus) {
++    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
 +        JLabel label;
 +        
-+        if (value != null) {
-+            String deviceName   = value.getAvdName();
-+            String deviceSerial = value.getSerialNumber();
-+            String deviceModel  = value.getProperty("ro.product.model");
++        if (value != null && value instanceof IDevice) {
++            IDevice device = (IDevice)value;
++            String deviceName   = device.getAvdName();
++            String deviceSerial = device.getSerialNumber();
++            String deviceModel  = device.getProperty("ro.product.model");
 +            String deviceLabel;
 +            
 +            if (deviceName != null) {
 +        
 +        if (isSelected) {
 +            label.setBackground(list.getSelectionBackground());
-+            label.setForeground(list.getSelectionBackground());
++            label.setForeground(list.getSelectionForeground());
++            label.setOpaque(true);
 +        }
 +        
 +        return label;
 +        // do nothing
 +    }
 +}
-diff -r 6d07d93fb2cc logcat/src/org/nyerel/nbandroid/logcat/LogReader.java
---- a/logcat/src/org/nyerel/nbandroid/logcat/LogReader.java	Sun Jun 26 16:04:36 2011 +0200
-+++ b/logcat/src/org/nyerel/nbandroid/logcat/LogReader.java	Thu Jun 30 05:20:52 2011 +0200
+diff -r 20fb85cab183 logcat/src/org/nyerel/nbandroid/logcat/LogReader.java
+--- a/logcat/src/org/nyerel/nbandroid/logcat/LogReader.java	Thu Jun 30 21:54:42 2011 +0200
++++ b/logcat/src/org/nyerel/nbandroid/logcat/LogReader.java	Fri Jul 01 03:54:38 2011 +0200
 @@ -22,6 +22,8 @@
  import com.android.ddmlib.MultiLineReceiver;
  import com.android.ddmlib.ShellCommandUnresponsiveException;
      private static final Logger LOG = Logger.getLogger(LogReader.class.getName());
      private static final Set<String> ignoredLines;
  
-@@ -54,6 +60,7 @@
+@@ -54,7 +60,9 @@
      }
  
      private Set<LogListener> listeners;
+-    private IDevice device;
 +    private PropertyChangeSupport changeSupprt;
-     private IDevice device;
++    private IDevice currentDevice;
++    private IDevice wantDevice;
      private LogCatOutputReceiver receiver;
      private LogEventInfo lastLogEventInfo;
-@@ -66,17 +73,17 @@
+     private static Pattern sLogPattern = Pattern.compile(
+@@ -66,17 +74,17 @@
      private Timer checkReadingStatusTimer;
      private int checkingPeriod = 10000;
      
          
          adb = AndroidDebugBridgeFactory.getDefault();
          checkReadingStatusTimer = new Timer();
-@@ -94,6 +101,32 @@
+@@ -94,17 +102,44 @@
              }
          }, checkingPeriod, checkingPeriod);
      }
 +    
 +    
 +    public IDevice getCurrentDevice() {
-+        return device;
++        if (wantDevice != null) {
++            return wantDevice;
++        }
++        
++        return currentDevice;
 +    }
 +    
 +    
 +    public void setCurrentDevice(IDevice device) {
-+        if (this.device != device) {
++        if (this.wantDevice != device) {
 +            // set new device
-+            this.device = device;
++            this.wantDevice = device;
 +            
 +            // stop reading on current device
 +            stopReading();
-+
-+            // and start reading on it
-+           // startReading();
 +        }
 +        
 +        return;
 +    }
  
      private boolean deviceReallyConnected() {
-         if (adb == null || !adb.isConnected() || !reading || device == null || 
-@@ -114,6 +147,19 @@
+-        if (adb == null || !adb.isConnected() || !reading || device == null || 
+-            device.isOffline() || !device.isOnline()) {
++        if (adb == null || !adb.isConnected() || !reading || currentDevice == null || 
++            currentDevice.isOffline() || !currentDevice.isOnline()) {
+             return false;
+         }
+         boolean gotIt = false;
+-        LOG.info("searching for device with sn: " + device.getSerialNumber());
++        LOG.info("searching for device with sn: " + currentDevice.getSerialNumber());
+         for (IDevice d: adb.getDevices()) {
+             LOG.info("device: " + d.getSerialNumber());
+-            if (d.equals(device)) {
++            if (d.equals(currentDevice)) {
+                 gotIt = true;
+             }
+         }
+@@ -114,6 +149,19 @@
          }
          return true;
      }
  
      public void addLogListener(LogListener listener) {
          listeners.add(listener);
-@@ -151,17 +197,19 @@
+@@ -151,17 +199,19 @@
  
      private final class LogCatOutputReceiver extends MultiLineReceiver {
  
              }
          }
  
-@@ -190,30 +238,26 @@
+@@ -190,30 +240,26 @@
      }
  
      private void reallyStartReading() {
 -        } else {
 -            errorMessage("No devices found!");
 -            return;
-+        if (device == null) {
++        if (wantDevice == null) {
 +            // need to connect to a new device
 +            IDevice[] devs = adb.getDevices();
 +            if (devs != null && devs.length > 0) {
-+                device = devs[0];
++                wantDevice = devs[0];
 +            } else {
 +                errorMessage("No devices found!");
 +                return;
 +            }
          }
          
-         if (device.isOnline()) {
+-        if (device.isOnline()) {
 -            
 -           // Clear log on newly connected devices
 -            if(lastDeviceSN != null && !lastDeviceSN.equals(device.getSerialNumber())) {
 -            }
 -            
 -            receiver = new LogCatOutputReceiver();
-+            receiver = new LogCatOutputReceiver(device);
-+            final IDevice originalDevice = device;
++        if (wantDevice.isOnline()) {
++            currentDevice = wantDevice;
++            receiver = new LogCatOutputReceiver(currentDevice);
              RequestProcessor.getDefault().post(new Runnable() {
  
                  @Override public void run() {
                      try {
                          reading = true;
 -                        device.executeShellCommand("logcat -v long", receiver);
-+                        device.executeShellCommand("logcat -v long", receiver, 0);
++                        currentDevice.executeShellCommand("logcat -v long", receiver, 0);
                      } catch (TimeoutException e) {
                        LOG.log(Level.FINE, null, e);
                        reading = false;
-@@ -228,6 +272,11 @@
+@@ -228,6 +274,11 @@
                        reading = false;
                      } finally {
                          receiver = null;
 +                        
 +                        // when the device has changted, we restart the logging process
-+                        if (device != originalDevice) {
++                        if (currentDevice != wantDevice) {
 +                            startReading();
 +                        }
                      }
                  }
              });
-@@ -237,7 +286,10 @@
+@@ -237,7 +288,10 @@
      }
  
      public void stopReading() {
          shouldBeReading = false;
      }
  
-@@ -248,7 +300,7 @@
+@@ -248,7 +302,7 @@
          return false;
      }
  
          for (String line : lines) {
              // ignore empty lines.
              if (line.length() > 0) {
-@@ -286,6 +338,16 @@
+@@ -286,6 +340,16 @@
                      }
                      
                      LogEvent event = new LogEvent(lastLogEventInfo, message);
                      if(!loggedEvents.contains(event)) {
                          loggedEvents.add(event);
                          sendNewLogEvent(event);
-@@ -330,13 +392,4 @@
+@@ -330,13 +394,4 @@
  
          return nameref;
      }
 -        loggedEvents.clear();
 -    }
  }
-diff -r 6d07d93fb2cc logcat/src/org/nyerel/nbandroid/logcat/LogTopComponent.form
---- a/logcat/src/org/nyerel/nbandroid/logcat/LogTopComponent.form	Sun Jun 26 16:04:36 2011 +0200
-+++ b/logcat/src/org/nyerel/nbandroid/logcat/LogTopComponent.form	Thu Jun 30 05:20:52 2011 +0200
+diff -r 20fb85cab183 logcat/src/org/nyerel/nbandroid/logcat/LogTopComponent.form
+--- a/logcat/src/org/nyerel/nbandroid/logcat/LogTopComponent.form	Thu Jun 30 21:54:42 2011 +0200
++++ b/logcat/src/org/nyerel/nbandroid/logcat/LogTopComponent.form	Fri Jul 01 03:54:38 2011 +0200
 @@ -171,12 +171,7 @@
  
        <Layout class="org.netbeans.modules.form.compat2.layouts.DesignGridBagLayout"/>
            <Constraints>
              <Constraint layoutClass="org.netbeans.modules.form.compat2.layouts.DesignGridBagLayout" value="org.netbeans.modules.form.compat2.layouts.DesignGridBagLayout$GridBagConstraintsDescription">
                <GridBagConstraints gridX="-1" gridY="-1" gridWidth="1" gridHeight="1" fill="0" ipadX="0" ipadY="0" insetsTop="0" insetsLeft="0" insetsBottom="0" insetsRight="0" anchor="10" weightX="0.0" weightY="0.0"/>
-diff -r 6d07d93fb2cc logcat/src/org/nyerel/nbandroid/logcat/LogTopComponent.java
---- a/logcat/src/org/nyerel/nbandroid/logcat/LogTopComponent.java	Sun Jun 26 16:04:36 2011 +0200
-+++ b/logcat/src/org/nyerel/nbandroid/logcat/LogTopComponent.java	Thu Jun 30 05:20:52 2011 +0200
+diff -r 20fb85cab183 logcat/src/org/nyerel/nbandroid/logcat/LogTopComponent.java
+--- a/logcat/src/org/nyerel/nbandroid/logcat/LogTopComponent.java	Thu Jun 30 21:54:42 2011 +0200
++++ b/logcat/src/org/nyerel/nbandroid/logcat/LogTopComponent.java	Fri Jul 01 03:54:38 2011 +0200
 @@ -84,7 +84,9 @@
      
      private List<LogTableManager> tabManagers;
      private javax.swing.JScrollPane jScrollPane1;
      private javax.swing.JTable jTable1;
      private javax.swing.JPanel panCenter;
+@@ -692,6 +697,9 @@
+               public void rowsInserted(int firstRow, int endRow) {
+                 try {
+                   super.rowsInserted(firstRow, endRow);
++                } catch (NullPointerException ex) {
++                  LOG.log(Level.INFO, 
++                      "Ignoring exception caused by http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6582564", ex);
+                 } catch (IndexOutOfBoundsException ex) {
+                   LOG.log(Level.INFO, 
+                       "Ignoring exception caused by http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6582564", ex);