From f6bd221d16f8001f2a7106f76527f1ca80dcb64f Mon Sep 17 00:00:00 2001 From: Raphael Moll Date: Tue, 30 Aug 2011 14:26:22 -0700 Subject: SDK Manager: remember window positions. Remember window positions & sizes for the SDK Manager 2 and the AVD Manager 1. When restoring the windows, make sure they fit on the visible monitor(s) area. Also fixed a race condition where closing the AvdManager1 window would dispose some bitmaps but it turns out the AVD Manager can be launched from the SDK Manager and the latter still need these bitmaps... Hilarity ensues. Change-Id: I2312e760029bc32561355f557a621d23ec1b6916 --- .../repository/sdkman2/AvdManagerWindowImpl1.java | 20 ++- .../repository/sdkman2/SdkUpdaterWindowImpl2.java | 7 +- .../repository/sdkman2/ShellSizeAndPos.java | 166 +++++++++++++++++++++ 3 files changed, 187 insertions(+), 6 deletions(-) create mode 100755 sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/sdkman2/ShellSizeAndPos.java (limited to 'sdkmanager/libs/sdkuilib') diff --git a/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/sdkman2/AvdManagerWindowImpl1.java b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/sdkman2/AvdManagerWindowImpl1.java index b4f4fd7..12ddf6e 100755 --- a/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/sdkman2/AvdManagerWindowImpl1.java +++ b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/sdkman2/AvdManagerWindowImpl1.java @@ -62,7 +62,7 @@ public class AvdManagerWindowImpl1 { private static final String APP_NAME = "Android Virtual Device Manager"; private static final String APP_NAME_MAC_MENU = "AVD Manager"; - + private static final String SIZE_POS_PREFIX = "avdman1"; //$NON-NLS-1$ private final Shell mParentShell; private final AvdInvocationContext mContext; @@ -135,23 +135,31 @@ public class AvdManagerWindowImpl1 { mShell.open(); mShell.layout(); - if (postCreateContent()) { //$hide$ (hide from SWT designer) + boolean ok = postCreateContent(); + + if (ok && mContext == AvdInvocationContext.STANDALONE) { Display display = Display.getDefault(); while (!mShell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } - } - dispose(); //$hide$ + dispose(); //$hide$ + } } private void createShell() { mShell = new Shell(mParentShell, SWT.SHELL_TRIM | SWT.APPLICATION_MODAL); mShell.addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent e) { - onAndroidSdkUpdaterDispose(); //$hide$ (hide from SWT designer) + ShellSizeAndPos.saveSizeAndPos(mShell, SIZE_POS_PREFIX); + + if (mContext != AvdInvocationContext.SDK_MANAGER) { + // When invoked from the sdk manager, don't dispose + // resources that the sdk manager still needs. + onAndroidSdkUpdaterDispose(); //$hide$ (hide from SWT designer) + } } }); @@ -165,6 +173,8 @@ public class AvdManagerWindowImpl1 { mShell.setMinimumSize(new Point(500, 300)); mShell.setSize(700, 500); mShell.setText(APP_NAME); + + ShellSizeAndPos.loadSizeAndPos(mShell, SIZE_POS_PREFIX); } private void createContents() { diff --git a/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/sdkman2/SdkUpdaterWindowImpl2.java b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/sdkman2/SdkUpdaterWindowImpl2.java index 6ae6653..1d0c484 100755 --- a/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/sdkman2/SdkUpdaterWindowImpl2.java +++ b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/sdkman2/SdkUpdaterWindowImpl2.java @@ -73,6 +73,8 @@ import java.util.ArrayList; public class SdkUpdaterWindowImpl2 implements ISdkUpdaterWindow { private static final String APP_NAME = "Android SDK Manager"; + private static final String SIZE_POS_PREFIX = "sdkman2"; //$NON-NLS-1$ + private final Shell mParentShell; private final SdkInvocationContext mContext; /** Internal data shared between the window and its pages. */ @@ -166,6 +168,7 @@ public class SdkUpdaterWindowImpl2 implements ISdkUpdaterWindow { mShell = new Shell(mParentShell, SWT.SHELL_TRIM | SWT.APPLICATION_MODAL); mShell.addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent e) { + ShellSizeAndPos.saveSizeAndPos(mShell, SIZE_POS_PREFIX); onAndroidSdkUpdaterDispose(); //$hide$ (hide from SWT designer) } }); @@ -180,6 +183,8 @@ public class SdkUpdaterWindowImpl2 implements ISdkUpdaterWindow { mShell.setMinimumSize(new Point(500, 300)); mShell.setSize(700, 500); mShell.setText(APP_NAME); + + ShellSizeAndPos.loadSizeAndPos(mShell, SIZE_POS_PREFIX); } private void createContents() { @@ -671,7 +676,7 @@ public class SdkUpdaterWindowImpl2 implements ISdkUpdaterWindow { } getShell().setText( - String.format("%1$s - %2$s", APP_NAME, content.getPageTitle())); //$NON-NLS-1$ + String.format("%1$s - %2$s", APP_NAME, content.getPageTitle())); Label filler = new Label(shell, SWT.NONE); GridDataBuilder.create(filler).hFill().hGrab(); diff --git a/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/sdkman2/ShellSizeAndPos.java b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/sdkman2/ShellSizeAndPos.java new file mode 100755 index 0000000..e53c8ae --- /dev/null +++ b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/sdkman2/ShellSizeAndPos.java @@ -0,0 +1,166 @@ +/* + * Copyright (C) 2011 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.repository.sdkman2; + + +import com.android.prefs.AndroidLocation; + +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.graphics.Rectangle; +import org.eclipse.swt.widgets.Monitor; +import org.eclipse.swt.widgets.Shell; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Properties; + +/** + * Utility to save & restore the size and position on a window + * using a common config file. + */ +public class ShellSizeAndPos { + + private static final String SETTINGS_FILENAME = "androidwin.cfg"; //$NON-NLS-1$ + private static final String PX = "_px"; //$NON-NLS-1$ + private static final String PY = "_py"; //$NON-NLS-1$ + private static final String SX = "_sx"; //$NON-NLS-1$ + private static final String SY = "_sy"; //$NON-NLS-1$ + + public static void loadSizeAndPos(Shell shell, String prefix) { + Properties props = loadProperties(); + + try { + int px = Integer.parseInt(props.getProperty(prefix + PX)); + int py = Integer.parseInt(props.getProperty(prefix + PY)); + int sx = Integer.parseInt(props.getProperty(prefix + SX)); + int sy = Integer.parseInt(props.getProperty(prefix + SY)); + + Point p1 = new Point(px, py); + Point p2 = new Point(px + sx, py + sy); + Rectangle r = new Rectangle(px, py, sy, sy); + + Monitor bestMatch = null; + int bestSurface = -1; + for (Monitor monitor : shell.getDisplay().getMonitors()) { + Rectangle area = monitor.getClientArea(); + if (area.contains(p1) && area.contains(p2)) { + // The shell is fully visible on this monitor. Just use that. + bestMatch = monitor; + bestSurface = Integer.MAX_VALUE; + break; + } else { + // Find which monitor displays the largest surface of the window. + // We'll use this one to center the window there, to make sure we're not + // starting split between several monitors. + Rectangle i = area.intersection(r); + int surface = i.width * i.height; + if (surface > bestSurface) { + bestSurface = surface; + bestMatch = monitor; + } + } + } + + if (bestMatch != null && bestSurface != Integer.MAX_VALUE) { + // Recenter the window on this monitor and make sure it fits + Rectangle area = bestMatch.getClientArea(); + + sx = Math.min(sx, area.width); + sy = Math.min(sy, area.height); + px = area.x + (area.width - sx) / 2; + py = area.y + (area.height - sy) / 2; + } + + shell.setLocation(px, py); + shell.setSize(sx, sy); + + } catch ( Exception e) { + // Ignore exception. We could typically get NPE from the getProperty + // or NumberFormatException from parseInt calls. Either way, do + // nothing if anything goes wrong. + } + } + + public static void saveSizeAndPos(Shell shell, String prefix) { + Properties props = loadProperties(); + + Point loc = shell.getLocation(); + Point size = shell.getSize(); + + props.setProperty(prefix + PX, Integer.toString(loc.x)); + props.setProperty(prefix + PY, Integer.toString(loc.y)); + props.setProperty(prefix + SX, Integer.toString(size.x)); + props.setProperty(prefix + SY, Integer.toString(size.y)); + + saveProperties(props); + } + + /** + * Load properties saved in {@link #SETTINGS_FILENAME}. + * If the file does not exists or doesn't load properly, just return an + * empty set of properties. + */ + private static Properties loadProperties() { + Properties props = new Properties(); + FileInputStream fis = null; + + try { + String folder = AndroidLocation.getFolder(); + File f = new File(folder, SETTINGS_FILENAME); + if (f.exists()) { + fis = new FileInputStream(f); + + props.load(fis); + } + } catch (Exception e) { + // Ignore + } finally { + if (fis != null) { + try { + fis.close(); + } catch (IOException e) { + } + } + } + + return props; + } + + private static void saveProperties(Properties props) { + FileOutputStream fos = null; + + try { + String folder = AndroidLocation.getFolder(); + File f = new File(folder, SETTINGS_FILENAME); + fos = new FileOutputStream(f); + + props.store(fos, "## Size and Pos for SDK Manager Windows"); //$NON-NLS-1$ + + } catch (Exception e) { + // ignore + } finally { + if (fos != null) { + try { + fos.close(); + } catch (IOException e) { + } + } + } + } +} -- cgit v1.1