FileDocCategorySizeDatePackage
DeviceBridge.javaAPI DocAndroid 1.5 API6417Wed May 06 22:41:10 BST 2009com.android.hierarchyviewer.device

DeviceBridge

public class DeviceBridge extends Object

Fields Summary
private static com.android.ddmlib.AndroidDebugBridge
bridge
private static final HashMap
devicePortMap
private static int
nextLocalPort
Constructors Summary
Methods Summary
private static java.lang.StringbuildIsServerRunningShellCommand()

        return String.format("service call window %d",
                Configuration.SERVICE_CODE_IS_SERVER_RUNNING);
    
private static java.lang.StringbuildStartServerShellCommand(int port)

        return String.format("service call window %d i32 %d",
                Configuration.SERVICE_CODE_START_SERVER, port);
    
private static java.lang.StringbuildStopServerShellCommand()

        return String.format("service call window %d", Configuration.SERVICE_CODE_STOP_SERVER);
    
public static intgetDeviceLocalPort(com.android.ddmlib.Device device)

        synchronized (devicePortMap) {
            Integer port = devicePortMap.get(device);
            if (port != null) {
                return port;
            }
            
            Log.e("hierarchy", "Missing forwarded port for " + device.getSerialNumber());
            return -1;
        }
        
    
public static com.android.ddmlib.Device[]getDevices()

        return bridge.getDevices();
    
public static voidinitDebugBridge()


        
        if (bridge == null) {
            AndroidDebugBridge.init(false /* debugger support */);
        }
        if (bridge == null || !bridge.isConnected()) {
            String adbLocation = System.getProperty("hierarchyviewer.adb");
            if (adbLocation != null && adbLocation.length() != 0) {
                adbLocation += File.separator + "adb";
            } else {
                adbLocation = "adb";
            }

            bridge = AndroidDebugBridge.createBridge(adbLocation, true);
        }
    
public static booleanisViewServerRunning(com.android.ddmlib.Device device)

        initDebugBridge();
        final boolean[] result = new boolean[1];
        try {
            if (device.isOnline()) {
                device.executeShellCommand(buildIsServerRunningShellCommand(),
                        new BooleanResultReader(result));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result[0];
    
public static voidremoveDeviceForward(com.android.ddmlib.Device device)

        synchronized (devicePortMap) {
            final Integer localPort = devicePortMap.get(device);
            if (localPort != null) {
                device.removeForward(localPort, Configuration.DEFAULT_SERVER_PORT);
                devicePortMap.remove(device);
            }
        }
    
public static voidsetupDeviceForward(com.android.ddmlib.Device device)
Sets up a just-connected device to work with the view server.

This starts a port forwarding between a local port and a port on the device.

param
device

        synchronized (devicePortMap) {
            if (device.getState() == Device.DeviceState.ONLINE) {
                int localPort = nextLocalPort++;
                device.createForward(localPort, Configuration.DEFAULT_SERVER_PORT);
                devicePortMap.put(device, localPort);
            }
        }
    
public static voidstartListenForDevices(AndroidDebugBridge.IDeviceChangeListener listener)

        AndroidDebugBridge.addDeviceChangeListener(listener);
    
public static booleanstartViewServer(com.android.ddmlib.Device device)

        return startViewServer(device, Configuration.DEFAULT_SERVER_PORT);
    
public static booleanstartViewServer(com.android.ddmlib.Device device, int port)

        initDebugBridge();
        final boolean[] result = new boolean[1];
        try {
            if (device.isOnline()) {
                device.executeShellCommand(buildStartServerShellCommand(port),
                        new BooleanResultReader(result));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result[0];
    
public static voidstopListenForDevices(AndroidDebugBridge.IDeviceChangeListener listener)

        AndroidDebugBridge.removeDeviceChangeListener(listener);
    
public static booleanstopViewServer(com.android.ddmlib.Device device)

        initDebugBridge();
        final boolean[] result = new boolean[1];
        try {
            if (device.isOnline()) {
                device.executeShellCommand(buildStopServerShellCommand(),
                        new BooleanResultReader(result));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result[0];
    
public static voidterminate()

        AndroidDebugBridge.terminate();