Merge change 609 into donut

* changes:
  Fix the opt-in window for usage stat so that it works when running from ADT.
diff --git a/tools/ddms/app/src/com/android/ddms/Main.java b/tools/ddms/app/src/com/android/ddms/Main.java
index d63b884..d545ed9 100644
--- a/tools/ddms/app/src/com/android/ddms/Main.java
+++ b/tools/ddms/app/src/com/android/ddms/Main.java
@@ -78,7 +78,7 @@
         // the "ping" argument means to check in with the server and exit
         // the application name and version number must also be supplied
         if (args.length >= 3 && args[0].equals("ping")) {
-            SdkStatsService.ping(args[1], args[2]);
+            SdkStatsService.ping(args[1], args[2], null);
             return;
         } else if (args.length > 0) {
             Log.e("ddms", "Unknown argument: " + args[0]);
@@ -86,7 +86,7 @@
         }
 
         // ddms itself is wanted: send a ping for ourselves
-        SdkStatsService.ping("ddms", VERSION);  //$NON-NLS-1$
+        SdkStatsService.ping("ddms", VERSION, null);  //$NON-NLS-1$
 
         DebugPortManager.setProvider(DebugPortProvider.getInstance());
 
diff --git a/tools/eclipse/plugins/com.android.ide.eclipse.adt/.classpath b/tools/eclipse/plugins/com.android.ide.eclipse.adt/.classpath
index a24fc87..9898b97 100644
--- a/tools/eclipse/plugins/com.android.ide.eclipse.adt/.classpath
+++ b/tools/eclipse/plugins/com.android.ide.eclipse.adt/.classpath
@@ -5,7 +5,7 @@
 	<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
 	<classpathentry kind="lib" path="jarutils.jar"/>
 	<classpathentry kind="lib" path="androidprefs.jar"/>
-	<classpathentry kind="lib" path="sdkstats.jar"/>
+	<classpathentry kind="lib" path="sdkstats.jar" sourcepath="/SdkStatsService"/>
 	<classpathentry kind="lib" path="kxml2-2.3.0.jar"/>
 	<classpathentry kind="lib" path="layoutlib_api.jar"/>
 	<classpathentry kind="lib" path="layoutlib_utils.jar"/>
diff --git a/tools/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/AdtPlugin.java b/tools/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/AdtPlugin.java
index b5cee81..8ef0f2c 100644
--- a/tools/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/AdtPlugin.java
+++ b/tools/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/AdtPlugin.java
@@ -31,7 +31,6 @@
 import com.android.ide.eclipse.adt.sdk.Sdk.ITargetChangeListener;
 import com.android.ide.eclipse.adt.ui.EclipseUiHelper;
 import com.android.ide.eclipse.common.AndroidConstants;
-import com.android.ide.eclipse.common.SdkStatsHelper;
 import com.android.ide.eclipse.common.StreamHelper;
 import com.android.ide.eclipse.common.project.BaseProjectHelper;
 import com.android.ide.eclipse.common.project.ExportHelper;
@@ -51,6 +50,7 @@
 import com.android.ide.eclipse.editors.xml.XmlEditor;
 import com.android.sdklib.IAndroidTarget;
 import com.android.sdklib.SdkConstants;
+import com.android.sdkstats.SdkStatsService;
 
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IFolder;
@@ -983,13 +983,7 @@
             @Override
             protected IStatus run(IProgressMonitor monitor) {
                 try {
-
-                    // get the version of the plugin
-                    String versionString = (String) getBundle().getHeaders().get(
-                            Constants.BUNDLE_VERSION);
-                    Version version = new Version(versionString);
-                    
-                    SdkStatsHelper.pingUsageServer("adt", version); //$NON-NLS-1$
+                    pingUsageServer(); //$NON-NLS-1$
                     
                     return Status.OK_STATUS;
                 } catch (Throwable t) {
@@ -1389,4 +1383,22 @@
     public static synchronized OutputStream getErrorStream() {
         return sPlugin.mAndroidConsoleErrorStream;
     }
+
+    /**
+     * Pings the usage start server.
+     * @param pluginName the name of the plugin to appear in the stats
+     * @param pluginVersion the {@link Version} of the plugin.
+     */
+    private void pingUsageServer() {
+        // get the version of the plugin
+        String versionString = (String) getBundle().getHeaders().get(
+                Constants.BUNDLE_VERSION);
+        Version version = new Version(versionString);
+
+        versionString = String.format("%1$d.%2$d.%3$d", version.getMajor(), //$NON-NLS-1$
+                version.getMinor(), version.getMicro());
+
+        SdkStatsService.ping("adt", versionString, getDisplay()); //$NON-NLS-1$
+    }
+
 }
diff --git a/tools/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/common/SdkStatsHelper.java b/tools/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/common/SdkStatsHelper.java
deleted file mode 100644
index 345c663..0000000
--- a/tools/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/common/SdkStatsHelper.java
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- *
- * Licensed under the Eclipse Public License, Version 1.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.eclipse.org/org/documents/epl-v10.php
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.ide.eclipse.common;
-
-import com.android.sdkstats.SdkStatsService;
-
-import org.osgi.framework.Version;
-
-/**
- * Helper class to access the ping usage stat server.
- */
-public class SdkStatsHelper {
-
-    /**
-     * Pings the usage start server.
-     * @param pluginName the name of the plugin to appear in the stats
-     * @param pluginVersion the {@link Version} of the plugin.
-     */
-    public static void pingUsageServer(String pluginName, Version pluginVersion) {
-        String versionString = String.format("%1$d.%2$d.%3$d", pluginVersion.getMajor(),
-                pluginVersion.getMinor(), pluginVersion.getMicro());
-
-        SdkStatsService.ping(pluginName, versionString);
-    }
-}
diff --git a/tools/sdkstats/src/com/android/sdkstats/SdkStatsService.java b/tools/sdkstats/src/com/android/sdkstats/SdkStatsService.java
index 0b3d41b..688474e 100644
--- a/tools/sdkstats/src/com/android/sdkstats/SdkStatsService.java
+++ b/tools/sdkstats/src/com/android/sdkstats/SdkStatsService.java
@@ -110,8 +110,10 @@
      *
      * @param app name to report in the ping
      * @param version to report in the ping
+     * @param display an optional {@link Display} object to use, or null, if a new one should be
+     * created.
      */
-    public static void ping(final String app, final String version) {
+    public static void ping(final String app, final String version, final Display display) {
         // Validate the application and version input.
         final String normalVersion = normalizeVersion(app, version);
 
@@ -123,7 +125,7 @@
                 prefs.setValue(PING_ID, new Random().nextLong());
     
                 // Also give them a chance to opt out.
-                prefs.setValue(PING_OPT_IN, getUserPermission());
+                prefs.setValue(PING_OPT_IN, getUserPermission(display));
                 try {
                     prefs.save();
                 }
@@ -273,77 +275,90 @@
      * Prompt the user for whether they want to opt out of reporting.
      * @return whether the user allows reporting (they do not opt out).
      */
-    private static boolean getUserPermission() {
-        // Use dialog trim for the shell, but without a close button.
-        final Display display = new Display();
-        final Shell shell = new Shell(display, SWT.TITLE | SWT.BORDER);
-        shell.setText(WINDOW_TITLE_TEXT);
-        shell.setLayout(new GridLayout(1, false));  // 1 column
-
-        // Take the default font and scale it up for the title.
-        final Label title = new Label(shell, SWT.CENTER | SWT.WRAP);
-        final FontData[] fontdata = title.getFont().getFontData();
-        for (int i = 0; i < fontdata.length; i++) {
-            fontdata[i].setHeight(fontdata[i].getHeight() * 4 / 3);
-        }
-        title.setFont(new Font(display, fontdata));
-        title.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
-        title.setText(HEADER_TEXT);
-
-        final Label notice = new Label(shell, SWT.WRAP);
-        notice.setFont(title.getFont());
-        notice.setForeground(new Color(display, 255, 0, 0));
-        notice.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
-        notice.setText(NOTICE_TEXT);
-
-        final Link text = new Link(shell, SWT.WRAP);
-        text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
-        text.setText(BODY_TEXT);
-        text.addSelectionListener(new SelectionAdapter() {
-            @Override
-            public void widgetSelected(SelectionEvent event) {
-                openUrl(event.text);
-            }
-        });
-
-        final Button checkbox = new Button(shell, SWT.CHECK);
-        checkbox.setSelection(true);  // Opt-in by default.
-        checkbox.setText(CHECKBOX_TEXT);
-
-        final Link footer = new Link(shell, SWT.WRAP);
-        footer.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
-        footer.setText(FOOTER_TEXT);
-
+    private static boolean getUserPermission(Display display) {
         // Whether the user gave permission (size-1 array for writing to).
         // Initialize to false, set when the user clicks the button.
         final boolean[] permission = new boolean[] { false };
 
-        final Button button = new Button(shell, SWT.PUSH);
-        button.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
-        button.setText(BUTTON_TEXT);
-        button.addSelectionListener(new SelectionAdapter() {
-            @Override
-            public void widgetSelected(SelectionEvent event) {
-                permission[0] = checkbox.getSelection();
-                shell.close();
+        boolean dispose = false;
+        if (display == null) {
+            display = new Display();
+            dispose = true;
+        }
+
+        final Display currentDisplay = display;
+        final boolean disposeDisplay = dispose;
+
+        display.syncExec(new Runnable() {
+            public void run() {
+                final Shell shell = new Shell(currentDisplay, SWT.TITLE | SWT.BORDER);
+                shell.setText(WINDOW_TITLE_TEXT);
+                shell.setLayout(new GridLayout(1, false)); // 1 column
+
+                // Take the default font and scale it up for the title.
+                final Label title = new Label(shell, SWT.CENTER | SWT.WRAP);
+                final FontData[] fontdata = title.getFont().getFontData();
+                for (int i = 0; i < fontdata.length; i++) {
+                    fontdata[i].setHeight(fontdata[i].getHeight() * 4 / 3);
+                }
+                title.setFont(new Font(currentDisplay, fontdata));
+                title.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
+                title.setText(HEADER_TEXT);
+
+                final Label notice = new Label(shell, SWT.WRAP);
+                notice.setFont(title.getFont());
+                notice.setForeground(new Color(currentDisplay, 255, 0, 0));
+                notice.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
+                notice.setText(NOTICE_TEXT);
+
+                final Link text = new Link(shell, SWT.WRAP);
+                text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
+                text.setText(BODY_TEXT);
+                text.addSelectionListener(new SelectionAdapter() {
+                    @Override
+                    public void widgetSelected(SelectionEvent event) {
+                        openUrl(event.text);
+                    }
+                });
+
+                final Button checkbox = new Button(shell, SWT.CHECK);
+                checkbox.setSelection(true); // Opt-in by default.
+                checkbox.setText(CHECKBOX_TEXT);
+
+                final Link footer = new Link(shell, SWT.WRAP);
+                footer.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
+                footer.setText(FOOTER_TEXT);
+
+                final Button button = new Button(shell, SWT.PUSH);
+                button.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
+                button.setText(BUTTON_TEXT);
+                button.addSelectionListener(new SelectionAdapter() {
+                    @Override
+                    public void widgetSelected(SelectionEvent event) {
+                        permission[0] = checkbox.getSelection();
+                        shell.close();
+                    }
+                });
+
+                // Size the window to a fixed width, as high as necessary,
+                // centered.
+                final Point size = shell.computeSize(450, SWT.DEFAULT, true);
+                final Rectangle screen = currentDisplay.getClientArea();
+                shell.setBounds(screen.x + screen.width / 2 - size.x / 2, screen.y + screen.height
+                        / 2 - size.y / 2, size.x, size.y);
+
+                shell.open();
+                while (!shell.isDisposed()) {
+                    if (!currentDisplay.readAndDispatch())
+                        currentDisplay.sleep();
+                }
+
+                if (disposeDisplay) {
+                    currentDisplay.dispose();
+                }
             }
         });
 
-        // Size the window to a fixed width, as high as necessary, centered.
-        final Point size = shell.computeSize(450, SWT.DEFAULT, true);
-        final Rectangle screen = display.getClientArea();
-        shell.setBounds(
-            screen.x + screen.width / 2 - size.x / 2,
-            screen.y + screen.height / 2 - size.y / 2,
-            size.x, size.y);
-
-        shell.open();
-        while (!shell.isDisposed()) {
-            if (!display.readAndDispatch())
-                display.sleep();
-        }
-
-        display.dispose();  // Otherwise ddms' own Display can't be created
         return permission[0];
     }