diff options
author | Raphael <raphael@google.com> | 2010-03-25 13:31:37 -0700 |
---|---|---|
committer | Raphael <raphael@google.com> | 2010-03-25 13:31:37 -0700 |
commit | 1a6379e626588d1b3306ee89e4dc05f7b79ec103 (patch) | |
tree | a61c1c960cffd36100b6b7e3ad8b3c5370a9b5c0 | |
parent | 532bfee321d688084e0f25e5274429f73e397a66 (diff) | |
download | sdk-1a6379e626588d1b3306ee89e4dc05f7b79ec103.zip sdk-1a6379e626588d1b3306ee89e4dc05f7b79ec103.tar.gz sdk-1a6379e626588d1b3306ee89e4dc05f7b79ec103.tar.bz2 |
SDK Manager: fix crash when creating new AVD with no home.
This also fixes the logging:
- properly propagate the ISdkLog to the AvdCreateDialog.
- properly use the ADT console for logging when invoked from Eclipse.
SDK Bug 2535112
Change-Id: I9e059fe30fe02c4f5d3e70054b4454f5703df515
11 files changed, 298 insertions, 135 deletions
diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/launch/DeviceChooserDialog.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/launch/DeviceChooserDialog.java index bb4d580..d6c9cd8 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/launch/DeviceChooserDialog.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/launch/DeviceChooserDialog.java @@ -25,6 +25,7 @@ import com.android.ddmuilib.IImageLoader; import com.android.ddmuilib.ImageHelper; import com.android.ddmuilib.TableHelper; import com.android.ide.eclipse.adt.AdtPlugin; +import com.android.ide.eclipse.adt.internal.sdk.AdtConsoleSdkLog; import com.android.ide.eclipse.adt.internal.sdk.Sdk; import com.android.ide.eclipse.ddms.DdmsPlugin; import com.android.sdklib.AndroidVersion; @@ -400,7 +401,8 @@ public class DeviceChooserDialog extends Dialog implements IDeviceChangeListener mSdk.getSdkLocation(), mSdk.getAvdManager(), new NonRunningAvdFilter(), - DisplayMode.SIMPLE_SELECTION); + DisplayMode.SIMPLE_SELECTION, + new AdtConsoleSdkLog()); mPreferredAvdSelector.setTableHeightHint(100); mPreferredAvdSelector.setEnabled(false); mPreferredAvdSelector.setSelectionListener(new SelectionAdapter() { diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/launch/EmulatorConfigTab.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/launch/EmulatorConfigTab.java index 36bd2fc..a4c7c1f 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/launch/EmulatorConfigTab.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/launch/EmulatorConfigTab.java @@ -20,6 +20,7 @@ import com.android.ide.eclipse.adt.AdtPlugin; import com.android.ide.eclipse.adt.internal.launch.AndroidLaunchConfiguration.TargetMode; import com.android.ide.eclipse.adt.internal.preferences.AdtPrefs; import com.android.ide.eclipse.adt.internal.project.BaseProjectHelper; +import com.android.ide.eclipse.adt.internal.sdk.AdtConsoleSdkLog; import com.android.ide.eclipse.adt.internal.sdk.Sdk; import com.android.ide.eclipse.ddms.DdmsPlugin; import com.android.prefs.AndroidLocation.AndroidLocationException; @@ -199,7 +200,8 @@ public class EmulatorConfigTab extends AbstractLaunchConfigurationTab { mPreferredAvdSelector = new AvdSelector(offsetComp, Sdk.getCurrent().getSdkLocation(), null /* avd manager */, - DisplayMode.SIMPLE_CHECK); + DisplayMode.SIMPLE_CHECK, + new AdtConsoleSdkLog()); mPreferredAvdSelector.setTableHeightHint(100); mPreferredAvdSelector.setSelectionListener(new SelectionAdapter() { @Override diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/sdk/AdtConsoleSdkLog.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/sdk/AdtConsoleSdkLog.java new file mode 100755 index 0000000..062c2d4 --- /dev/null +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/sdk/AdtConsoleSdkLog.java @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2010 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.adt.internal.sdk; + +import com.android.ide.eclipse.adt.AdtPlugin; +import com.android.sdklib.ISdkLog; + +/** + * An {@link ISdkLog} logger that outputs to the ADT console. + */ +public class AdtConsoleSdkLog implements ISdkLog { + + private static final String TAG = "SDK Manager"; //$NON-NLS-1$ + + public void error(Throwable t, String errorFormat, Object... args) { + if (t != null) { + AdtPlugin.logAndPrintError(t, TAG, "Error: " + errorFormat, args); + } else { + AdtPlugin.printErrorToConsole(TAG, String.format(errorFormat, args)); + } + } + + public void printf(String msgFormat, Object... args) { + String msg = String.format(msgFormat, args); + for (String s : msg.split("\n")) { + if (s.trim().length() > 0) { + AdtPlugin.printToConsole(TAG, s); + } + } + } + + public void warning(String warningFormat, Object... args) { + AdtPlugin.printToConsole(TAG, String.format("Warning: " + warningFormat, args)); + } +} diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/actions/AvdManagerAction.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/actions/AvdManagerAction.java index 1254c31..15c31f9 100755 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/actions/AvdManagerAction.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/wizards/actions/AvdManagerAction.java @@ -17,8 +17,8 @@ package com.android.ide.eclipse.adt.internal.wizards.actions;
import com.android.ide.eclipse.adt.AdtPlugin;
+import com.android.ide.eclipse.adt.internal.sdk.AdtConsoleSdkLog;
import com.android.ide.eclipse.adt.internal.sdk.Sdk;
-import com.android.sdklib.ISdkLog;
import com.android.sdkuilib.repository.UpdaterWindow;
import org.eclipse.jface.action.IAction;
@@ -45,9 +45,12 @@ public class AvdManagerAction implements IWorkbenchWindowActionDelegate, IObject public void run(IAction action) {
Sdk sdk = Sdk.getCurrent();
if (sdk != null) {
+
+ // Runs the updater window, directing all logs to the ADT console.
+
UpdaterWindow window = new UpdaterWindow(
AdtPlugin.getDisplay().getActiveShell(),
- new AdtSdkLogger(),
+ new AdtConsoleSdkLog(),
sdk.getSdkLocation(),
false /*userCanChangeSdkRoot*/);
window.addListeners(new UpdaterWindow.ISdkListener() {
@@ -68,26 +71,4 @@ public class AvdManagerAction implements IWorkbenchWindowActionDelegate, IObject public void setActivePart(IAction action, IWorkbenchPart targetPart) {
// nothing to do.
}
-
- private static class AdtSdkLogger implements ISdkLog {
-
- private static final String TAG = "SDK Manager"; //$NON-NLS-1$
-
- public void error(Throwable t, String errorFormat, Object... args) {
- if (t != null) {
- AdtPlugin.logAndPrintError(t, TAG, "Error: " + errorFormat, args);
- } else {
- AdtPlugin.printErrorToConsole(TAG, String.format(errorFormat, args));
- }
- }
-
- public void printf(String msgFormat, Object... args) {
- AdtPlugin.printToConsole(TAG, String.format(msgFormat, args));
- }
-
- public void warning(String warningFormat, Object... args) {
- AdtPlugin.printToConsole(TAG, String.format("Warning: " + warningFormat, args));
- }
-
- }
}
diff --git a/sdkmanager/app/src/com/android/sdkmanager/Main.java b/sdkmanager/app/src/com/android/sdkmanager/Main.java index 334928b..58a9a98 100644 --- a/sdkmanager/app/src/com/android/sdkmanager/Main.java +++ b/sdkmanager/app/src/com/android/sdkmanager/Main.java @@ -35,8 +35,10 @@ import com.android.sdklib.xml.AndroidXPathFactory; import com.android.sdkmanager.internal.repository.AboutPage; import com.android.sdkmanager.internal.repository.SettingsPage; import com.android.sdkuilib.internal.repository.LocalPackagesPage; +import com.android.sdkuilib.internal.widgets.MessageBoxLog; import com.android.sdkuilib.repository.UpdaterWindow; +import org.eclipse.swt.widgets.Display; import org.xml.sax.InputSource; import java.io.File; @@ -94,8 +96,8 @@ public class Main { /** * Creates the {@link #mSdkLog} object. - * <p/> * This must be done before {@link #init()} as it will be used to report errors. + * This logger prints to the attached console. */ private void createLogger() { mSdkLog = new ISdkLog() { @@ -263,9 +265,15 @@ public class Main { // display a message talking about the command line version System.out.printf("No command line parameters provided, launching UI.\n" + "See 'android --help' for operations from the command line.\n"); + + MessageBoxLog errorLogger = new MessageBoxLog( + "SDK Manager", + Display.getCurrent(), + true /*logErrorsOnly*/); + UpdaterWindow window = new UpdaterWindow( null /* parentShell */, - mSdkLog, + errorLogger, mOsSdkFolder, false /*userCanChangeSdkRoot*/); window.registerPage("Settings", SettingsPage.class); @@ -275,6 +283,9 @@ public class Main { window.setRequestAutoUpdate(true); } window.open(); + + errorLogger.displayResult(true); + } catch (Exception e) { e.printStackTrace(); } diff --git a/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/avd/AvdManager.java b/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/avd/AvdManager.java index 4fdc7d5..8df6935 100644 --- a/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/avd/AvdManager.java +++ b/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/avd/AvdManager.java @@ -428,6 +428,7 @@ public final class AvdManager { * @return the matching AvdInfo or <code>null</code> if none were found. */ public AvdInfo getAvd(String name, boolean validAvdOnly) { + if (validAvdOnly) { for (AvdInfo info : getValidAvds()) { if (info.getName().equals(name)) { @@ -684,23 +685,30 @@ public final class AvdManager { File configIniFile = new File(avdFolder, CONFIG_INI); writeIniFile(configIniFile, values); + // Generate the log report first because we want to control where line breaks + // are located when generating the hardware config list. + StringBuilder report = new StringBuilder(); + if (target.isPlatform()) { - log.printf("Created AVD '%1$s' based on %2$s", name, target.getName()); + report.append(String.format("Created AVD '%1$s' based on %2$s", + name, target.getName())); } else { - log.printf("Created AVD '%1$s' based on %2$s (%3$s)", name, - target.getName(), target.getVendor()); + report.append(String.format("Created AVD '%1$s' based on %2$s (%3$s)", name, + target.getName(), target.getVendor())); } // display the chosen hardware config if (finalHardwareValues.size() > 0) { - log.printf(", with the following hardware config:\n"); + report.append(",\nwith the following hardware config:\n"); for (Entry<String, String> entry : finalHardwareValues.entrySet()) { - log.printf("%s=%s\n",entry.getKey(), entry.getValue()); + report.append(String.format("%s=%s\n",entry.getKey(), entry.getValue())); } } else { - log.printf("\n"); + report.append("\n"); } + log.printf(report.toString()); + // create the AvdInfo object, and add it to the list AvdInfo newAvdInfo = new AvdInfo(name, avdFolder.getAbsolutePath(), diff --git a/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/AvdManagerPage.java b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/AvdManagerPage.java index 77ebb1f..8e63cf9 100755 --- a/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/AvdManagerPage.java +++ b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/AvdManagerPage.java @@ -55,7 +55,8 @@ public class AvdManagerPage extends Composite implements ISdkListener { mAvdSelector = new AvdSelector(parent,
mUpdaterData.getOsSdkRoot(),
mUpdaterData.getAvdManager(),
- DisplayMode.MANAGER);
+ DisplayMode.MANAGER,
+ mUpdaterData.getSdkLog());
mAvdSelector.setSettingsController(mUpdaterData.getSettingsController());
}
diff --git a/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/SettingsController.java b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/SettingsController.java index cb60d57..87f0544 100755 --- a/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/SettingsController.java +++ b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/SettingsController.java @@ -169,7 +169,7 @@ public class SettingsController { } catch (Exception e) {
ISdkLog log = mUpdaterData.getSdkLog();
if (log != null) {
- log.error(e, "Failed to load settings from '%1$s'", path);
+ log.error(e, "Failed to load settings from .android folder. Path is '%1$s'.", path);
}
} finally {
if (fis != null) {
diff --git a/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/widgets/AvdCreationDialog.java b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/widgets/AvdCreationDialog.java index 0c6b0c3..3734a42 100644 --- a/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/widgets/AvdCreationDialog.java +++ b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/widgets/AvdCreationDialog.java @@ -19,6 +19,7 @@ package com.android.sdkuilib.internal.widgets; import com.android.prefs.AndroidLocation; import com.android.prefs.AndroidLocation.AndroidLocationException; import com.android.sdklib.IAndroidTarget; +import com.android.sdklib.ISdkLog; import com.android.sdklib.SdkConstants; import com.android.sdklib.SdkManager; import com.android.sdklib.internal.avd.AvdManager; @@ -26,7 +27,6 @@ import com.android.sdklib.internal.avd.HardwareProperties; import com.android.sdklib.internal.avd.AvdManager.AvdInfo; import com.android.sdklib.internal.avd.HardwareProperties.HardwareProperty; import com.android.sdkuilib.internal.repository.icons.ImageFactory; -import com.android.sdkuilib.internal.widgets.AvdSelector.SdkLog; import com.android.sdkuilib.ui.GridDialog; import org.eclipse.jface.dialogs.IDialogConstants; @@ -92,6 +92,8 @@ final class AvdCreationDialog extends GridDialog { private final Map<String, String> mProperties = new HashMap<String, String>(); // a list of user-edited properties. private final ArrayList<String> mEditedProperties = new ArrayList<String>(); + private final ImageFactory mImageFactory; + private final ISdkLog mSdkLog; private Text mAvdName; private Combo mTargetCombo; @@ -119,7 +121,6 @@ final class AvdCreationDialog extends GridDialog { private Label mStatusIcon; private Label mStatusLabel; private Composite mStatusComposite; - private final ImageFactory mImageFactory; /** * {@link VerifyListener} for {@link Text} widgets that should only contains numbers. @@ -171,16 +172,33 @@ final class AvdCreationDialog extends GridDialog { } } - protected AvdCreationDialog(Shell parentShell, AvdManager avdManager, - ImageFactory imageFactory) { + protected AvdCreationDialog(Shell parentShell, + AvdManager avdManager, + ImageFactory imageFactory, + ISdkLog log) { super(parentShell, 2, false); mAvdManager = avdManager; mImageFactory = imageFactory; + mSdkLog = log; + + File hardwareDefs = null; + + SdkManager sdkMan = avdManager.getSdkManager(); + if (sdkMan != null) { + String sdkPath = sdkMan.getLocation(); + if (sdkPath != null) { + hardwareDefs = new File (sdkPath + File.separator + + SdkConstants.OS_SDK_TOOLS_LIB_FOLDER, SdkConstants.FN_HARDWARE_INI); + } + } - File hardwareDefs = new File (avdManager.getSdkManager().getLocation() + File.separator + - SdkConstants.OS_SDK_TOOLS_LIB_FOLDER, SdkConstants.FN_HARDWARE_INI); - mHardwareMap = HardwareProperties.parseHardwareDefinitions( + if (hardwareDefs == null) { + log.error(null, "Failed to load file %s from SDK", SdkConstants.FN_HARDWARE_INI); + mHardwareMap = new HashMap<String, HardwareProperty>(); + } else { + mHardwareMap = HardwareProperties.parseHardwareDefinitions( hardwareDefs, null /*sdkLog*/); + } } @Override @@ -918,12 +936,17 @@ final class AvdCreationDialog extends GridDialog { skinName = mSkinSizeWidth.getText() + "x" + mSkinSizeHeight.getText(); //$NON-NLS-1$ } - SdkLog log = new SdkLog( - String.format("Result of creating AVD '%s':", avdName), - getContents().getDisplay(), - false /*logErrorsOnly*/); + ISdkLog log = mSdkLog; + if (log == null || log instanceof MessageBoxLog) { + // If the current logger is a message box, we use our own (to make sure + // to display errors right away and customize the title). + log = new MessageBoxLog( + String.format("Result of creating AVD '%s':", avdName), + getContents().getDisplay(), + false /*logErrorsOnly*/); + } - File avdFolder; + File avdFolder = null; try { avdFolder = new File( AndroidLocation.getFolder() + AndroidLocation.FOLDER_AVD, @@ -947,7 +970,9 @@ final class AvdCreationDialog extends GridDialog { success = avdInfo != null; - log.displayResult(success); + if (log instanceof MessageBoxLog) { + ((MessageBoxLog) log).displayResult(success); + } return success; } } diff --git a/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/widgets/AvdSelector.java b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/widgets/AvdSelector.java index 2631dd0..60a3c82 100644 --- a/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/widgets/AvdSelector.java +++ b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/widgets/AvdSelector.java @@ -101,6 +101,8 @@ public final class AvdSelector { private SettingsController mController; + private final ISdkLog mSdkLog; + /** * The display mode of the AVD Selector. @@ -193,18 +195,19 @@ public final class AvdSelector { * @param manager the AVD manager. * @param filter When non-null, will allow filtering the AVDs to display. * @param displayMode The display mode ({@link DisplayMode}). - * - * TODO: pass an ISdkLog and use it when reloading, starting the emulator, etc. + * @param sdkLog The logger. Cannot be null. */ public AvdSelector(Composite parent, String osSdkPath, AvdManager manager, IAvdFilter filter, - DisplayMode displayMode) { + DisplayMode displayMode, + ISdkLog sdkLog) { mOsSdkPath = osSdkPath; mAvdManager = manager; mTargetFilter = filter; mDisplayMode = displayMode; + mSdkLog = sdkLog; // get some bitmaps. mImageFactory = new ImageFactory(parent.getDisplay()); @@ -367,12 +370,14 @@ public final class AvdSelector { * @param parent The parent composite where the selector will be added. * @param manager the AVD manager. * @param displayMode The display mode ({@link DisplayMode}). + * @param sdkLog The logger. Cannot be null. */ public AvdSelector(Composite parent, String osSdkPath, AvdManager manager, - DisplayMode displayMode) { - this(parent, osSdkPath, manager, (IAvdFilter)null /* filter */, displayMode); + DisplayMode displayMode, + ISdkLog sdkLog) { + this(parent, osSdkPath, manager, (IAvdFilter)null /* filter */, displayMode, sdkLog); } /** @@ -385,13 +390,15 @@ public final class AvdSelector { * @param manager the AVD manager. * @param filter Only shows the AVDs matching this target (must not be null). * @param displayMode The display mode ({@link DisplayMode}). + * @param sdkLog The logger. Cannot be null. */ public AvdSelector(Composite parent, String osSdkPath, AvdManager manager, IAndroidTarget filter, - DisplayMode displayMode) { - this(parent, osSdkPath, manager, new TargetBasedFilter(filter), displayMode); + DisplayMode displayMode, + ISdkLog sdkLog) { + this(parent, osSdkPath, manager, new TargetBasedFilter(filter), displayMode, sdkLog); } /** @@ -826,8 +833,10 @@ public final class AvdSelector { } private void onNew() { - AvdCreationDialog dlg = new AvdCreationDialog(mTable.getShell(), mAvdManager, - mImageFactory); + AvdCreationDialog dlg = new AvdCreationDialog(mTable.getShell(), + mAvdManager, + mImageFactory, + mSdkLog); if (dlg.open() == Window.OK) { refresh(false /*reload*/); } @@ -879,16 +888,23 @@ public final class AvdSelector { } // log for this action. - SdkLog log = new SdkLog( + ISdkLog log = mSdkLog; + if (log == null || log instanceof MessageBoxLog) { + // If the current logger is a message box, we use our own (to make sure + // to display errors right away and customize the title). + log = new MessageBoxLog( String.format("Result of deleting AVD '%s':", avdInfo.getName()), display, false /*logErrorsOnly*/); + } // delete the AVD boolean success = mAvdManager.deleteAvd(avdInfo, log); // display the result - log.displayResult(success); + if (log instanceof MessageBoxLog) { + ((MessageBoxLog) log).displayResult(success); + } if (success) { refresh(false /*reload*/); @@ -907,22 +923,31 @@ public final class AvdSelector { final Display display = mTable.getDisplay(); // log for this action. - SdkLog log = new SdkLog( + ISdkLog log = mSdkLog; + if (log == null || log instanceof MessageBoxLog) { + // If the current logger is a message box, we use our own (to make sure + // to display errors right away and customize the title). + log = new MessageBoxLog( String.format("Result of updating AVD '%s':", avdInfo.getName()), display, false /*logErrorsOnly*/); + } // delete the AVD try { mAvdManager.updateAvd(avdInfo, log); // display the result - log.displayResult(true /* success */); - + if (log instanceof MessageBoxLog) { + ((MessageBoxLog) log).displayResult(true /* success */); + } refresh(false /*reload*/); + } catch (IOException e) { log.error(e, null); - log.displayResult(false /* success */); + if (log instanceof MessageBoxLog) { + ((MessageBoxLog) log).displayResult(false /* success */); + } } } @@ -932,7 +957,12 @@ public final class AvdSelector { Display display = mTable.getDisplay(); // log for this action. - SdkLog log = new SdkLog("Result of SDK Manager", display, true /*logErrorsOnly*/); + ISdkLog log = mSdkLog; + if (log == null || log instanceof MessageBoxLog) { + // If the current logger is a message box, we use our own (to make sure + // to display errors right away and customize the title). + log = new MessageBoxLog("Result of SDK Manager", display, true /*logErrorsOnly*/); + } UpdaterWindow window = new UpdaterWindow( mTable.getShell(), @@ -941,7 +971,10 @@ public final class AvdSelector { false /*userCanChangeSdkRoot*/); window.open(); refresh(true /*reload*/); // UpdaterWindow uses its own AVD manager so this one must reload. - log.displayResult(true); + + if (log instanceof MessageBoxLog) { + ((MessageBoxLog) log).displayResult(true); + } } private void onStart() { @@ -1069,75 +1102,6 @@ public final class AvdSelector { }.start(); } - /** - * Collects all log from the AVD action and displays it in a dialog. - */ - static class SdkLog implements ISdkLog { - - final ArrayList<String> logMessages = new ArrayList<String>(); - private final String mMessage; - private final Display mDisplay; - private final boolean mLogErrorsOnly; - - public SdkLog(String message, Display display, boolean logErrorsOnly) { - mMessage = message; - mDisplay = display; - mLogErrorsOnly = logErrorsOnly; - } - - public void error(Throwable throwable, String errorFormat, Object... arg) { - if (errorFormat != null) { - logMessages.add(String.format("Error: " + errorFormat, arg)); - } - - if (throwable != null) { - logMessages.add(throwable.getMessage()); - } - } - - public void warning(String warningFormat, Object... arg) { - if (!mLogErrorsOnly) { - logMessages.add(String.format("Warning: " + warningFormat, arg)); - } - } - - public void printf(String msgFormat, Object... arg) { - if (!mLogErrorsOnly) { - logMessages.add(String.format(msgFormat, arg)); - } - } - - /** - * Displays the log if anything was captured. - */ - public void displayResult(final boolean success) { - if (logMessages.size() > 0) { - final StringBuilder sb = new StringBuilder(mMessage + "\n\n"); - for (String msg : logMessages) { - sb.append(msg); - } - - // display the message - // dialog box only run in ui thread.. - mDisplay.asyncExec(new Runnable() { - public void run() { - Shell shell = mDisplay.getActiveShell(); - // Use the success icon if the call indicates success. - // However just use the error icon if the logger was only recording errors. - if (success && !mLogErrorsOnly) { - MessageDialog.openInformation(shell, "Android Virtual Devices Manager", - sb.toString()); - } else { - MessageDialog.openError(shell, "Android Virtual Devices Manager", - sb.toString()); - - } - } - }); - } - } - } - private boolean isAvdRepairable(AvdStatus avdStatus) { return avdStatus == AvdStatus.ERROR_IMAGE_DIR; } diff --git a/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/widgets/MessageBoxLog.java b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/widgets/MessageBoxLog.java new file mode 100755 index 0000000..d5c1818 --- /dev/null +++ b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/widgets/MessageBoxLog.java @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0 + * + * 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.sdkuilib.internal.widgets; + +import com.android.sdklib.ISdkLog; + +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; + +import java.util.ArrayList; + + +/** + * Collects all log and displays it in a message box dialog. + * <p/> + * This is good if only a few lines of log are expected. + * If you pass <var>logErrorsOnly</var> to the constructor, the message box + * will be shown only if errors were generated, which is the typical use-case. + * <p/> + * To use this: </br> + * - Construct a new {@link MessageBoxLog}. </br> + * - Pass the logger to the action. </br> + * - Once the action is completed, call {@link #displayResult(boolean)} + * indicating whether the operation was successful or not. + * + * When <var>logErrorsOnly</var> is true, if the operation was not successful or errors + * were generated, this will display the message box. + */ +public final class MessageBoxLog implements ISdkLog { + + final ArrayList<String> logMessages = new ArrayList<String>(); + private final String mMessage; + private final Display mDisplay; + private final boolean mLogErrorsOnly; + + /** + * Creates a logger that will capture all logs and eventually display them + * in a simple message box. + * + * @param message + * @param display + * @param logErrorsOnly + */ + public MessageBoxLog(String message, Display display, boolean logErrorsOnly) { + mMessage = message; + mDisplay = display; + mLogErrorsOnly = logErrorsOnly; + } + + public void error(Throwable throwable, String errorFormat, Object... arg) { + if (errorFormat != null) { + logMessages.add(String.format("Error: " + errorFormat, arg)); + } + + if (throwable != null) { + logMessages.add(throwable.getMessage()); + } + } + + public void warning(String warningFormat, Object... arg) { + if (!mLogErrorsOnly) { + logMessages.add(String.format("Warning: " + warningFormat, arg)); + } + } + + public void printf(String msgFormat, Object... arg) { + if (!mLogErrorsOnly) { + logMessages.add(String.format(msgFormat, arg)); + } + } + + /** + * Displays the log if anything was captured. + * <p/> + * @param success Used only when the logger was constructed with <var>logErrorsOnly</var>==true. + * In this case the dialog will only be shown either if success if false or some errors + * where captured. + */ + public void displayResult(final boolean success) { + if (logMessages.size() > 0) { + final StringBuilder sb = new StringBuilder(mMessage + "\n\n"); + for (String msg : logMessages) { + sb.append(msg); + } + + // display the message + // dialog box only run in ui thread.. + mDisplay.asyncExec(new Runnable() { + public void run() { + Shell shell = mDisplay.getActiveShell(); + // Use the success icon if the call indicates success. + // However just use the error icon if the logger was only recording errors. + if (success && !mLogErrorsOnly) { + MessageDialog.openInformation(shell, "Android Virtual Devices Manager", + sb.toString()); + } else { + MessageDialog.openError(shell, "Android Virtual Devices Manager", + sb.toString()); + + } + } + }); + } + } +} |