Skip to content

Commit

Permalink
Fixed #74 - race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
mlopatkin committed Oct 24, 2011
1 parent 193e21e commit ea2bac4
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions src/org/bitbucket/mlopatkin/android/logviewer/MainFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
import org.bitbucket.mlopatkin.android.logviewer.widgets.DecoratingRendererTable;
import org.bitbucket.mlopatkin.android.logviewer.widgets.UiHelper;

import com.android.ddmlib.AndroidDebugBridge.IDeviceChangeListener;
import com.android.ddmlib.IDevice;
import com.android.ddmlib.AndroidDebugBridge.IDeviceChangeListener;

public class MainFrame extends JFrame implements DialogResultReceiver {
private static final Logger logger = Logger.getLogger(MainFrame.class);
Expand Down Expand Up @@ -442,27 +442,24 @@ public void actionPerformed(ActionEvent e) {
* Wait for device to connect.
*/
public void waitForDevice() {
synchronized (this) {
isWaitingForDevice = true;
}
pendingAttacher = new AdbDeviceManager.AbstractDeviceListener() {
@Override
public void deviceConnected(final IDevice device) {
if (device.isOnline()) {
connectDevice(device);
connectDevicePending(device);
}
}

@Override
public void deviceChanged(IDevice device, int changeMask) {
if ((changeMask & IDevice.CHANGE_STATE) != 0 && device.isOnline()) {
connectDevice(device);
connectDevicePending(device);
}
};

private void connectDevice(IDevice device) {
assert device.isOnline();
DeviceDisconnectedNotifier.startWatching(device);
setSourceAsync(new AdbDataSource(device));
stopWaitingForDevice();
}
};
AdbDeviceManager.addDeviceChangeListener(pendingAttacher);
EventQueue.invokeLater(new Runnable() {
Expand All @@ -471,6 +468,22 @@ public void run() {
showSourceMessage("Waiting for device...");
}
});
IDevice device = AdbDeviceManager.getDefaultDevice();
if (device != null) {
connectDevicePending(device);
}
}

private volatile boolean isWaitingForDevice;

private synchronized void connectDevicePending(IDevice device) {
if (!isWaitingForDevice) {
return;
}
isWaitingForDevice = false;
stopWaitingForDevice();
DeviceDisconnectedNotifier.startWatching(device);
setSourceAsync(new AdbDataSource(device));
}

private void stopWaitingForDevice() {
Expand Down

0 comments on commit ea2bac4

Please sign in to comment.