FileDocCategorySizeDatePackage
DeviceCommandDialog.javaAPI DocAndroid 1.5 API13238Wed May 06 22:41:08 BST 2009com.android.ddms

DeviceCommandDialog

public class DeviceCommandDialog extends org.eclipse.swt.widgets.Dialog
Execute a command on an ADB-attached device and save the output. There are several ways to do this. One is to run a single command and show the output. Another is to have several possible commands and let the user click a button next to the one (or ones) they want. This currently uses the simple 1:1 form.

Fields Summary
public static final int
DEVICE_STATE
public static final int
APP_STATE
public static final int
RADIO_STATE
public static final int
LOGCAT
private String
mCommand
private String
mFileName
private org.eclipse.swt.widgets.Label
mStatusLabel
private org.eclipse.swt.widgets.Button
mCancelDone
private org.eclipse.swt.widgets.Button
mSave
private org.eclipse.swt.widgets.Text
mText
private org.eclipse.swt.graphics.Font
mFont
private boolean
mCancel
private boolean
mFinished
Constructors Summary
public DeviceCommandDialog(String command, String fileName, org.eclipse.swt.widgets.Shell parent)
Create with default style.



             
           
        // don't want a close button, but it seems hard to get rid of on GTK
        // keep it on all platforms for consistency
        this(command, fileName, parent,
            SWT.DIALOG_TRIM | SWT.BORDER | SWT.APPLICATION_MODAL | SWT.RESIZE);
    
public DeviceCommandDialog(String command, String fileName, org.eclipse.swt.widgets.Shell parent, int style)
Create with app-defined style.

        super(parent, style);
        mCommand = command;
        mFileName = fileName;
    
Methods Summary
private java.lang.StringcommandString()

        return mCommand;

    
private voidcreateContents(org.eclipse.swt.widgets.Shell shell)

        GridData data;

        shell.setLayout(new GridLayout(2, true));

        shell.addListener(SWT.Close, new Listener() {
            public void handleEvent(Event event) {
                if (!mFinished) {
                    Log.i("ddms", "NOT closing - cancelling command");
                    event.doit = false;
                    mCancel = true;
                }
            }
        });

        mStatusLabel = new Label(shell, SWT.NONE);
        mStatusLabel.setText("Executing '" + shortCommandString() + "'");
        data = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
        data.horizontalSpan = 2;
        mStatusLabel.setLayoutData(data);

        mText = new Text(shell, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
        mText.setEditable(false);
        mText.setFont(mFont);
        data = new GridData(GridData.FILL_BOTH);
        data.horizontalSpan = 2;
        mText.setLayoutData(data);

        // "save" button
        mSave = new Button(shell, SWT.PUSH);
        mSave.setText("Save");
        data = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
        data.widthHint = 80;
        mSave.setLayoutData(data);
        mSave.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                saveText(shell);
            }
        });
        mSave.setEnabled(false);

        // "cancel/done" button
        mCancelDone = new Button(shell, SWT.PUSH);
        mCancelDone.setText("Cancel");
        data = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
        data.widthHint = 80;
        mCancelDone.setLayoutData(data);
        mCancelDone.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                if (!mFinished)
                    mCancel = true;
                else
                    shell.close();
            }
        });
    
private java.lang.StringdefaultFileName()

        return mFileName;
    
private voidexecuteCommand(org.eclipse.swt.widgets.Shell shell, com.android.ddmlib.Device device)

        Gatherer gath = new Gatherer(shell, device, commandString(), mText);
        gath.start();
    
private org.eclipse.swt.graphics.FontfindFont(org.eclipse.swt.widgets.Display display)

        String fontStr = PrefsDialog.getStore().getString("textOutputFont");
        if (fontStr != null) {
            FontData fdat = new FontData(fontStr);
            if (fdat != null)
                return new Font(display, fdat);
        }
        return null;
    
public voidopen(com.android.ddmlib.Device currentDevice)
Prepare and display the dialog.

param
currentDevice

        Shell parent = getParent();
        Shell shell = new Shell(parent, getStyle());
        shell.setText("Remote Command");

        mFinished = false;
        mFont = findFont(shell.getDisplay());
        createContents(shell);

        // Getting weird layout behavior under Linux when Text is added --
        // looks like text widget has min width of 400 when FILL_HORIZONTAL
        // is used, and layout gets tweaked to force this.  (Might be even
        // more with the scroll bars in place -- it wigged out when the
        // file save dialog was invoked.)
        shell.setMinimumSize(500, 200);
        shell.setSize(800, 600);
        shell.open();

        executeCommand(shell, currentDevice);

        Display display = parent.getDisplay();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }

        if (mFont != null)
            mFont.dispose();
    
private voidsaveText(org.eclipse.swt.widgets.Shell shell)

        FileDialog dlg = new FileDialog(shell, SWT.SAVE);
        String fileName;

        dlg.setText("Save output...");
        dlg.setFileName(defaultFileName());
        dlg.setFilterPath(PrefsDialog.getStore().getString("lastTextSaveDir"));
        dlg.setFilterNames(new String[] {
            "Text Files (*.txt)"
        });
        dlg.setFilterExtensions(new String[] {
            "*.txt"
        });

        fileName = dlg.open();
        if (fileName != null) {
            PrefsDialog.getStore().setValue("lastTextSaveDir",
                                            dlg.getFilterPath());

            Log.i("ddms", "Saving output to " + fileName);

            /*
             * Convert to 8-bit characters.
             */
            String text = mText.getText();
            byte[] ascii;
            try {
                ascii = text.getBytes("ISO-8859-1");
            }
            catch (UnsupportedEncodingException uee) {
                uee.printStackTrace();
                ascii = new byte[0];
            }

            /*
             * Output data, converting CRLF to LF.
             */
            try {
                int length = ascii.length;

                FileOutputStream outFile = new FileOutputStream(fileName);
                BufferedOutputStream out = new BufferedOutputStream(outFile);
                for (int i = 0; i < length; i++) {
                    if (i < length-1 &&
                        ascii[i] == 0x0d && ascii[i+1] == 0x0a)
                    {
                        continue;
                    }
                    out.write(ascii[i]);
                }
                out.close();        // flush buffer, close file
            }
            catch (IOException ioe) {
                Log.w("ddms", "Unable to save " + fileName + ": " + ioe);
            }
        }
    
private java.lang.StringshortCommandString()

        String str = commandString();
        if (str.length() > 50)
            return str.substring(0, 50) + "...";
        else
            return str;
    
private voidupdateForResult(int result)

        if (result == Gatherer.RESULT_SUCCESS) {
            mStatusLabel.setText("Successfully executed '"
                + shortCommandString() + "'");
            mSave.setEnabled(true);
        } else if (result == Gatherer.RESULT_CANCELLED) {
            mStatusLabel.setText("Execution cancelled; partial results below");
            mSave.setEnabled(true);     // save partial
        } else if (result == Gatherer.RESULT_FAILURE) {
            mStatusLabel.setText("Failed");
        }
        mStatusLabel.pack();
        mCancelDone.setText("Done");
        mFinished = true;