com.android.ddmlib
Class AndroidDebugBridge

java.lang.Object
  extended by com.android.ddmlib.AndroidDebugBridge

public final class AndroidDebugBridge
extends java.lang.Object

A connection to the host-side android debug bridge (adb)

This is the central point to communicate with any devices, emulators, or the applications running on them.

init(boolean) must be called before anything is done.


Nested Class Summary
static interface AndroidDebugBridge.IClientChangeListener
          Classes which implement this interface provide methods that deal with Client changes.
static interface AndroidDebugBridge.IDebugBridgeChangeListener
          Classes which implement this interface provide a method that deals with AndroidDebugBridge changes.
static interface AndroidDebugBridge.IDeviceChangeListener
          Classes which implement this interface provide methods that deal with IDevice addition, deletion, and changes.
 
Method Summary
static void addClientChangeListener(AndroidDebugBridge.IClientChangeListener listener)
          Adds the listener to the collection of listeners who will be notified when a Client property changed, by sending it one of the messages defined in the AndroidDebugBridge.IClientChangeListener interface.
static void addDebugBridgeChangeListener(AndroidDebugBridge.IDebugBridgeChangeListener listener)
          Adds the listener to the collection of listeners who will be notified when a new AndroidDebugBridge is connected, by sending it one of the messages defined in the AndroidDebugBridge.IDebugBridgeChangeListener interface.
static void addDeviceChangeListener(AndroidDebugBridge.IDeviceChangeListener listener)
          Adds the listener to the collection of listeners who will be notified when a IDevice is connected, disconnected, or when its properties or its Client list changed, by sending it one of the messages defined in the AndroidDebugBridge.IDeviceChangeListener interface.
static AndroidDebugBridge createBridge()
          Creates a AndroidDebugBridge that is not linked to any particular executable.
static AndroidDebugBridge createBridge(java.lang.String osLocation, boolean forceNewBridge)
          Creates a new debug bridge from the location of the command line tool.
static void disconnectBridge()
          Disconnects the current debug bridge, and destroy the object.
static AndroidDebugBridge getBridge()
          Returns the current debug bridge.
 int getConnectionAttemptCount()
          Returns the number of times the AndroidDebugBridge object attempted to connect to the adb daemon.
 IDevice[] getDevices()
          Returns the devices.
 int getRestartAttemptCount()
          Returns the number of times the AndroidDebugBridge object attempted to restart the adb daemon.
static java.net.InetSocketAddress getSocketAddress()
          Returns the socket address of the ADB server on the host.
 boolean hasInitialDeviceList()
          Returns whether the bridge has acquired the initial list from adb after being created.
static void init(boolean clientSupport)
          Initializes the ddm library.
static void initIfNeeded(boolean clientSupport)
          Initialized the library only if needed.
 boolean isConnected()
          Returns whether the AndroidDebugBridge object is still connected to the adb daemon.
static void removeClientChangeListener(AndroidDebugBridge.IClientChangeListener listener)
          Removes the listener from the collection of listeners who will be notified when a Client property changed.
static void removeDebugBridgeChangeListener(AndroidDebugBridge.IDebugBridgeChangeListener listener)
          Removes the listener from the collection of listeners who will be notified when a new AndroidDebugBridge is started.
static void removeDeviceChangeListener(AndroidDebugBridge.IDeviceChangeListener listener)
          Removes the listener from the collection of listeners who will be notified when a IDevice is connected, disconnected, or when its properties or its Client list changed.
 boolean restart()
          Restarts adb, but not the services around it.
 void setSelectedClient(Client selectedClient)
          Sets the client to accept debugger connection on the custom "Selected debug port".
static void terminate()
          Terminates the ddm library.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

initIfNeeded

public static void initIfNeeded(boolean clientSupport)
Initialized the library only if needed.

Parameters:
clientSupport - Indicates whether the library should enable the monitoring and interaction with applications running on the devices.
See Also:
init(boolean)

init

public static void init(boolean clientSupport)
Initializes the ddm library.

This must be called once before any call to createBridge(String, boolean).

The library can be initialized in 2 ways:

Only one tool can run in mode 1 at the same time.

Note that mode 1 does not prevent debugging of applications running on devices. Mode 1 lets debuggers connect to ddmlib which acts as a proxy between the debuggers and the applications to debug. See Client.getDebuggerListenPort().

The preferences of ddmlib should also be initialized with whatever default values were changed from the default values.

When the application quits, terminate() should be called.

Parameters:
clientSupport - Indicates whether the library should enable the monitoring and interaction with applications running on the devices.
See Also:
createBridge(String, boolean), DdmPreferences

terminate

public static void terminate()
Terminates the ddm library. This must be called upon application termination.


getSocketAddress

public static java.net.InetSocketAddress getSocketAddress()
Returns the socket address of the ADB server on the host.


createBridge

public static AndroidDebugBridge createBridge()
Creates a AndroidDebugBridge that is not linked to any particular executable.

This bridge will expect adb to be running. It will not be able to start/stop/restart adb.

If a bridge has already been started, it is directly returned with no changes (similar to calling getBridge()).

Returns:
a connected bridge.

createBridge

public static AndroidDebugBridge createBridge(java.lang.String osLocation,
                                              boolean forceNewBridge)
Creates a new debug bridge from the location of the command line tool.

Any existing server will be disconnected, unless the location is the same and forceNewBridge is set to false.

Parameters:
osLocation - the location of the command line tool 'adb'
forceNewBridge - force creation of a new bridge even if one with the same location already exists.
Returns:
a connected bridge.

getBridge

public static AndroidDebugBridge getBridge()
Returns the current debug bridge. Can be null if none were created.


disconnectBridge

public static void disconnectBridge()
Disconnects the current debug bridge, and destroy the object.

This also stops the current adb host server.

A new object will have to be created with createBridge(String, boolean).


addDebugBridgeChangeListener

public static void addDebugBridgeChangeListener(AndroidDebugBridge.IDebugBridgeChangeListener listener)
Adds the listener to the collection of listeners who will be notified when a new AndroidDebugBridge is connected, by sending it one of the messages defined in the AndroidDebugBridge.IDebugBridgeChangeListener interface.

Parameters:
listener - The listener which should be notified.

removeDebugBridgeChangeListener

public static void removeDebugBridgeChangeListener(AndroidDebugBridge.IDebugBridgeChangeListener listener)
Removes the listener from the collection of listeners who will be notified when a new AndroidDebugBridge is started.

Parameters:
listener - The listener which should no longer be notified.

addDeviceChangeListener

public static void addDeviceChangeListener(AndroidDebugBridge.IDeviceChangeListener listener)
Adds the listener to the collection of listeners who will be notified when a IDevice is connected, disconnected, or when its properties or its Client list changed, by sending it one of the messages defined in the AndroidDebugBridge.IDeviceChangeListener interface.

Parameters:
listener - The listener which should be notified.

removeDeviceChangeListener

public static void removeDeviceChangeListener(AndroidDebugBridge.IDeviceChangeListener listener)
Removes the listener from the collection of listeners who will be notified when a IDevice is connected, disconnected, or when its properties or its Client list changed.

Parameters:
listener - The listener which should no longer be notified.

addClientChangeListener

public static void addClientChangeListener(AndroidDebugBridge.IClientChangeListener listener)
Adds the listener to the collection of listeners who will be notified when a Client property changed, by sending it one of the messages defined in the AndroidDebugBridge.IClientChangeListener interface.

Parameters:
listener - The listener which should be notified.

removeClientChangeListener

public static void removeClientChangeListener(AndroidDebugBridge.IClientChangeListener listener)
Removes the listener from the collection of listeners who will be notified when a Client property changed.

Parameters:
listener - The listener which should no longer be notified.

getDevices

public IDevice[] getDevices()
Returns the devices.

See Also:
hasInitialDeviceList()

hasInitialDeviceList

public boolean hasInitialDeviceList()
Returns whether the bridge has acquired the initial list from adb after being created.

Calling getDevices() right after createBridge(String, boolean) will generally result in an empty list. This is due to the internal asynchronous communication mechanism with adb that does not guarantee that the IDevice list has been built before the call to getDevices().

The recommended way to get the list of IDevice objects is to create a AndroidDebugBridge.IDeviceChangeListener object.


setSelectedClient

public void setSelectedClient(Client selectedClient)
Sets the client to accept debugger connection on the custom "Selected debug port".

Parameters:
selectedClient - the client. Can be null.

isConnected

public boolean isConnected()
Returns whether the AndroidDebugBridge object is still connected to the adb daemon.


getConnectionAttemptCount

public int getConnectionAttemptCount()
Returns the number of times the AndroidDebugBridge object attempted to connect to the adb daemon.


getRestartAttemptCount

public int getRestartAttemptCount()
Returns the number of times the AndroidDebugBridge object attempted to restart the adb daemon.


restart

public boolean restart()
Restarts adb, but not the services around it.

Returns:
true if success.