FileDocCategorySizeDatePackage
DdmsPlugin.javaAPI DocAndroid 1.5 API20711Wed May 06 22:41:10 BST 2009com.android.ide.eclipse.ddms

DdmsPlugin

public final class DdmsPlugin extends org.eclipse.ui.plugin.AbstractUIPlugin implements com.android.ddmlib.AndroidDebugBridge.IDeviceChangeListener, com.android.ddmuilib.DevicePanel.IUiSelectionListener
The activator class controls the plug-in life cycle

Fields Summary
public static final String
PLUGIN_ID
private static final String
ADB_LOCATION
private static DdmsPlugin
sPlugin
The singleton instance
private static String
sAdbLocation
Location of the adb command line executable
private static IDebugLauncher
sRunningAppDebugLauncher
Debug Launcher for already running apps
private org.eclipse.ui.console.MessageConsole
mDdmsConsole
Console for DDMS log message
private ImageLoader
mLoader
Image loader object
private com.android.ddmlib.Device
mCurrentDevice
private com.android.ddmlib.Client
mCurrentClient
private boolean
mListeningToUiSelection
private final ArrayList
mListeners
private org.eclipse.swt.graphics.Color
mRed
private boolean
mDdmlibInitialized
Constructors Summary
public DdmsPlugin()
The constructor


                 
       
             
    
    
                         
       
        
                                    
           
        
                                    
           
    

           
      
        sPlugin = this;
    
Methods Summary
public synchronized voidaddSelectionListener(com.android.ide.eclipse.ddms.DdmsPlugin$ISelectionListener listener)

        mListeners.add(listener);
        
        // notify the new listener of the current selection
       listener.selectionChanged(mCurrentDevice);
       listener.selectionChanged(mCurrentClient);
    
public voiddeviceChanged(com.android.ddmlib.Device device, int changeMask)
Sent when a device data changed, or when clients are started/terminated on the device.

This is sent from a non UI thread.

param
device the device that was updated.
param
changeMask the mask indicating what changed.
see
IDeviceChangeListener#deviceChanged(Device)

        // if we are listening to selection coming from the ui, then we do nothing, as
        // any change in the devices/clients, will be handled by the UI, and we'll receive
        // selection notification through our implementation of IUiSelectionListener.
        if (mListeningToUiSelection == false) {
            
            // check if this is our device
            if (device == mCurrentDevice) {
                if (mCurrentClient == null) {
                    handleDefaultSelection(device);
                } else {
                    // get the clients and make sure ours is still in there.
                    Client[] clients = device.getClients();
                    boolean foundClient = false;
                    for (Client client : clients) {
                        if (client == mCurrentClient) {
                            foundClient = true;
                            break;
                        }
                    }
                    
                    // if we haven't found our client, lets look for a new one
                    if (foundClient == false) {
                        mCurrentClient = null;
                        handleDefaultSelection(device);
                    }
                }
            }
        }
    
public voiddeviceConnected(com.android.ddmlib.Device device)
Sent when the a device is connected to the {@link AndroidDebugBridge}.

This is sent from a non UI thread.

param
device the new device.
see
IDeviceChangeListener#deviceConnected(Device)

        // if we are listening to selection coming from the ui, then we do nothing, as
        // any change in the devices/clients, will be handled by the UI, and we'll receive
        // selection notification through our implementation of IUiSelectionListener.
        if (mListeningToUiSelection == false) {
            if (mCurrentDevice == null) {
                handleDefaultSelection(device);
            }
        }
    
public voiddeviceDisconnected(com.android.ddmlib.Device device)
Sent when the a device is disconnected to the {@link AndroidDebugBridge}.

This is sent from a non UI thread.

param
device the new device.
see
IDeviceChangeListener#deviceDisconnected(Device)

        // if we are listening to selection coming from the ui, then we do nothing, as
        // any change in the devices/clients, will be handled by the UI, and we'll receive
        // selection notification through our implementation of IUiSelectionListener.
        if (mListeningToUiSelection == false) {
            // test if the disconnected device was the default selection.
            if (mCurrentDevice == device) {
                // try to find a new device
                AndroidDebugBridge bridge = AndroidDebugBridge.getBridge();
                if (bridge != null) {
                    // get the device list
                    Device[] devices = bridge.getDevices();
                    
                    // check if we still have devices
                    if (devices.length == 0) {
                        handleDefaultSelection((Device)null);
                    } else {
                        handleDefaultSelection(devices[0]);
                    }
                } else {
                    handleDefaultSelection((Device)null);
                }
            }
        }
    
public static java.lang.StringgetAdb()

        return sAdbLocation;
    
public static com.android.ide.eclipse.ddms.DdmsPlugingetDefault()
Returns the shared instance

return
the shared instance

        return sPlugin;
    
public static org.eclipse.swt.widgets.DisplaygetDisplay()

        IWorkbench bench = sPlugin.getWorkbench();
        if (bench != null) {
            return bench.getDisplay();
        }
        return null;
    
public static ImageLoadergetImageLoader()
Return the image loader for the plugin

        if (sPlugin != null) {
            return sPlugin.mLoader;
        }
        return null;
    
private static java.lang.StringgetMessageTag(java.lang.String tag)
Creates a string containing the current date/time, and the tag

param
tag The tag associated to the message. Can be null
return
The dateTag

        Calendar c = Calendar.getInstance();

        if (tag == null) {
            return String.format("[%1$tF %1$tT]", c);
        }

        return String.format("[%1$tF %1$tT - %2$s]", c, tag);
    
public static com.android.ide.eclipse.ddms.DdmsPlugin$IDebugLaunchergetRunningAppDebugLauncher()

        return sRunningAppDebugLauncher;
    
private voidhandleDefaultSelection(com.android.ddmlib.Device device)
Handles a default selection of a {@link Device} and {@link Client}.

param
device the selected device

        // because the listener expect to receive this from the UI thread, and this is called
        // from the AndroidDebugBridge notifications, we need to run this in the UI thread.
        try {
            Display display = getDisplay();
            
            display.asyncExec(new Runnable() {
                public void run() {
                    // set the new device if different.
                    boolean newDevice = false;
                    if (mCurrentDevice != device) {
                        mCurrentDevice = device;
                        newDevice = true;
                
                        // notify of the new default device
                        for (ISelectionListener listener : mListeners) {
                            listener.selectionChanged(mCurrentDevice);
                        }
                    }
                    
                    if (device != null) {
                        // if this is a device switch or the same device but we didn't find a valid
                        // client the last time, we go look for a client to use again.
                        if (newDevice || mCurrentClient == null) {
                            // now get the new client
                            Client[] clients =  device.getClients();
                            if (clients.length > 0) {
                                handleDefaultSelection(clients[0]);
                            } else {
                                handleDefaultSelection((Client)null);
                            }
                        }
                    } else {
                        handleDefaultSelection((Client)null);
                    }
                }
            });
        } catch (SWTException e) {
            // display is disposed. Do nothing since we're quitting anyway.
        }
    
private voidhandleDefaultSelection(com.android.ddmlib.Client client)

        mCurrentClient = client;
        
        // notify of the new default client
        for (ISelectionListener listener : mListeners) {
            listener.selectionChanged(mCurrentClient);
        }
    
private synchronized voidinitDdmlib()

        if (mDdmlibInitialized == false) {
            // set the preferences.
            PreferenceInitializer.setupPreferences();
    
            // init the lib
            AndroidDebugBridge.init(true /* debugger support */);
            
            mDdmlibInitialized = true;
        }
    
private static synchronized voidprintToStream(org.eclipse.ui.console.MessageConsoleStream stream, java.lang.String tag, java.lang.String message)
Prints a message, associated with a project to the specified stream

param
stream The stream to write to
param
tag The tag associated to the message. Can be null
param
message The message to print.

        String dateTag = getMessageTag(tag);

        stream.print(dateTag);
        stream.println(message);
    
public synchronized voidremoveSelectionListener(com.android.ide.eclipse.ddms.DdmsPlugin$ISelectionListener listener)

        mListeners.remove(listener);
    
public synchronized voidselectionChanged(com.android.ddmlib.Device selectedDevice, com.android.ddmlib.Client selectedClient)
Sent when a new {@link Device} and {@link Client} are selected.

param
selectedDevice the selected device. If null, no devices are selected.
param
selectedClient The selected client. If null, no clients are selected.

        if (mCurrentDevice != selectedDevice) {
            mCurrentDevice = selectedDevice;

            // notify of the new default device
            for (ISelectionListener listener : mListeners) {
                listener.selectionChanged(mCurrentDevice);
            }
        }

        if (mCurrentClient != selectedClient) {
            mCurrentClient = selectedClient;
            
            // notify of the new default client
            for (ISelectionListener listener : mListeners) {
                listener.selectionChanged(mCurrentClient);
            }
        }
    
public static voidsetAdb(java.lang.String adb, boolean startAdb)
Set the location of the adb executable and optionally starts adb

param
adb location of adb
param
startAdb flag to start adb

        sAdbLocation = adb;

        // store the location for future ddms only start.
        sPlugin.getPreferenceStore().setValue(ADB_LOCATION, sAdbLocation);

        // starts the server in a thread in case this is blocking.
        if (startAdb) {
            new Thread() {
                @Override
                public void run() {
                    // init ddmlib if needed
                    getDefault().initDdmlib();

                    // create and start the bridge
                    AndroidDebugBridge.createBridge(sAdbLocation, false /* forceNewBridge */);
                }
            }.start();
        }
    
public synchronized voidsetListeningState(boolean state)

        mListeningToUiSelection = state;
    
public static voidsetRunningAppDebugLauncher(com.android.ide.eclipse.ddms.DdmsPlugin$IDebugLauncher launcher)
Sets the launcher responsible for connecting the debugger to running applications.

param
launcher The launcher.

        sRunningAppDebugLauncher = launcher;

        // if the process view is already running, give it the launcher.
        // This method could be called from a non ui thread, so we make sure to do that
        // in the ui thread.
        Display display = getDisplay();
        if (display != null && display.isDisposed() == false) {
            display.asyncExec(new Runnable() {
                public void run() {
                    DeviceView dv = DeviceView.getInstance();
                    if (dv != null) {
                        dv.setDebugLauncher(sRunningAppDebugLauncher);
                    }
                }
            });
        }
    
public voidstart(org.osgi.framework.BundleContext context)

        super.start(context);
        
        final Display display = getDisplay();

        // get the eclipse store
        final IPreferenceStore eclipseStore = getPreferenceStore();

        AndroidDebugBridge.addDeviceChangeListener(this);
        
        DdmUiPreferences.setStore(eclipseStore);

        //DdmUiPreferences.displayCharts();

        // set the consoles.
        mDdmsConsole = new MessageConsole("DDMS", null); // $NON-NLS-1$
        ConsolePlugin.getDefault().getConsoleManager().addConsoles(
                new IConsole[] {
                    mDdmsConsole
                });

        final MessageConsoleStream consoleStream = mDdmsConsole.newMessageStream();
        final MessageConsoleStream errorConsoleStream = mDdmsConsole.newMessageStream();
        mRed = new Color(display, 0xFF, 0x00, 0x00);

        // because this can be run, in some cases, by a non UI thread, and because
        // changing the console properties update the UI, we need to make this change
        // in the UI thread.
        display.asyncExec(new Runnable() {
            public void run() {
                errorConsoleStream.setColor(mRed);
            }
        });

        // set up the ddms log to use the ddms console.
        Log.setLogOutput(new ILogOutput() {
            public void printLog(LogLevel logLevel, String tag, String message) {
                if (logLevel.getPriority() >= LogLevel.ERROR.getPriority()) {
                    printToStream(errorConsoleStream, tag, message);
                    ConsolePlugin.getDefault().getConsoleManager().showConsoleView(mDdmsConsole);
                } else {
                    printToStream(consoleStream, tag, message);
                }
            }

            public void printAndPromptLog(final LogLevel logLevel, final String tag,
                    final String message) {
                printLog(logLevel, tag, message);
                // dialog box only run in UI thread..
                display.asyncExec(new Runnable() {
                    public void run() {
                        Shell shell = display.getActiveShell();
                        if (logLevel == LogLevel.ERROR) {
                            MessageDialog.openError(shell, tag, message);
                        } else {
                            MessageDialog.openWarning(shell, tag, message);
                        }
                    }
                });
            }
            
        });

        // create the loader that's able to load the images
        mLoader = new ImageLoader(this);
        
        // set the listener for the preference change
        Preferences prefs = getPluginPreferences();
        prefs.addPropertyChangeListener(new IPropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent event) {
                // get the name of the property that changed.
                String property = event.getProperty();

                if (PreferenceInitializer.ATTR_DEBUG_PORT_BASE.equals(property)) {
                    DdmPreferences.setDebugPortBase(
                            eclipseStore.getInt(PreferenceInitializer.ATTR_DEBUG_PORT_BASE));
                } else if (PreferenceInitializer.ATTR_SELECTED_DEBUG_PORT.equals(property)) {
                    DdmPreferences.setSelectedDebugPort(
                            eclipseStore.getInt(PreferenceInitializer.ATTR_SELECTED_DEBUG_PORT));
                } else if (PreferenceInitializer.ATTR_THREAD_INTERVAL.equals(property)) {
                    DdmUiPreferences.setThreadRefreshInterval(
                            eclipseStore.getInt(PreferenceInitializer.ATTR_THREAD_INTERVAL));
                } else if (PreferenceInitializer.ATTR_LOG_LEVEL.equals(property)) {
                    DdmPreferences.setLogLevel(
                            eclipseStore.getString(PreferenceInitializer.ATTR_LOG_LEVEL));
                }
            }
        });
        
        // read the adb location from the prefs to attempt to start it properly without
        // having to wait for ADT to start
        sAdbLocation = eclipseStore.getString(ADB_LOCATION);

        // start it in a thread to return from start() asap.
        new Thread() {
            @Override
            public void run() {
                // init ddmlib if needed
                getDefault().initDdmlib();

                // create and start the first bridge
                AndroidDebugBridge.createBridge(sAdbLocation, true /* forceNewBridge */);
            }
        }.start();
    
public voidstop(org.osgi.framework.BundleContext context)

        AndroidDebugBridge.removeDeviceChangeListener(this);
        
        AndroidDebugBridge.terminate();
        
        mRed.dispose();

        sPlugin = null;
        super.stop(context);