SDK Updater: SettingsController that handles the settings.
There is not any longer any logic in the SettingsPage, it is handled now by a "SettingsControllers" which is available via UpdaterData. The page only takes care of notifying the controller via a callback when settings have changed in the UI.
This commit is contained in:
@@ -16,8 +16,6 @@
|
||||
|
||||
package com.android.sdkmanager.internal.repository;
|
||||
|
||||
import com.android.prefs.AndroidLocation;
|
||||
import com.android.prefs.AndroidLocation.AndroidLocationException;
|
||||
import com.android.sdkuilib.internal.repository.ISettingsPage;
|
||||
|
||||
import org.eclipse.swt.SWT;
|
||||
@@ -31,28 +29,15 @@ import org.eclipse.swt.widgets.Group;
|
||||
import org.eclipse.swt.widgets.Label;
|
||||
import org.eclipse.swt.widgets.Text;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.Properties;
|
||||
|
||||
/*
|
||||
* TODO list
|
||||
* - The window should probably set a callback to be notified when settings are changed.
|
||||
* - Actually use the settings.
|
||||
*/
|
||||
|
||||
public class SettingsPage extends Composite implements ISettingsPage {
|
||||
|
||||
private static final String SETTINGS_FILENAME = "androidtool.cfg"; //$NON-NLS-1$
|
||||
|
||||
/** Java system setting picked up by {@link URL} for http proxy port */
|
||||
private static final String JAVA_HTTP_PROXY_PORT = "http.proxyPort"; //$NON-NLS-1$
|
||||
/** Java system setting picked up by {@link URL} for http proxy host */
|
||||
private static final String JAVA_HTTP_PROXY_HOST = "http.proxyHost"; //$NON-NLS-1$
|
||||
// data members
|
||||
private SettingsChangedCallback mSettingsChangedCallback;
|
||||
|
||||
// UI widgets
|
||||
private Group mProxySettingsGroup;
|
||||
private Group mPlaceholderGroup;
|
||||
private Button mApplyButton;
|
||||
@@ -130,84 +115,36 @@ public class SettingsPage extends Composite implements ISettingsPage {
|
||||
private void postCreate() {
|
||||
}
|
||||
|
||||
/** Loads settings from the given {@link Properties} container and update the page UI. */
|
||||
public void loadSettings(Properties in_settings) {
|
||||
mProxyServerText.setText(in_settings.getProperty(JAVA_HTTP_PROXY_HOST, "")); //$NON-NLS-1$
|
||||
mProxyPortText.setText(in_settings.getProperty(JAVA_HTTP_PROXY_PORT, "")); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
/** Called by the application to retrieve settings from the UI and store them in
|
||||
* the given {@link Properties} container. */
|
||||
public void retrieveSettings(Properties out_settings) {
|
||||
out_settings.put(JAVA_HTTP_PROXY_HOST, mProxyServerText.getText());
|
||||
out_settings.put(JAVA_HTTP_PROXY_PORT, mProxyPortText.getText());
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by the application to give a callback that the page should invoke when
|
||||
* settings must be applied. The page does not apply the settings itself, instead
|
||||
* it notifies the application.
|
||||
*/
|
||||
public void setOnSettingsChanged(SettingsChangedCallback settingsChangedCallback) {
|
||||
mSettingsChangedCallback = settingsChangedCallback;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify the application that settings have changed.
|
||||
*/
|
||||
private void onApplySelected() {
|
||||
applySettings();
|
||||
saveSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update Java system properties for the HTTP proxy.
|
||||
*/
|
||||
public void applySettings() {
|
||||
Properties props = System.getProperties();
|
||||
props.put(JAVA_HTTP_PROXY_HOST, mProxyServerText.getText());
|
||||
props.put(JAVA_HTTP_PROXY_PORT, mProxyPortText.getText());
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves settings.
|
||||
*/
|
||||
private void saveSettings() {
|
||||
Properties props = new Properties();
|
||||
props.put(JAVA_HTTP_PROXY_HOST, mProxyServerText.getText());
|
||||
props.put(JAVA_HTTP_PROXY_PORT, mProxyPortText.getText());
|
||||
|
||||
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
String folder = AndroidLocation.getFolder();
|
||||
File f = new File(folder, SETTINGS_FILENAME);
|
||||
|
||||
fos = new FileOutputStream(f);
|
||||
|
||||
props.store( fos, "## Settings for Android Tool"); //$NON-NLS-1$
|
||||
|
||||
} catch (AndroidLocationException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (fos != null) {
|
||||
try {
|
||||
fos.close();
|
||||
} catch (IOException e) {
|
||||
if (mSettingsChangedCallback != null) {
|
||||
mSettingsChangedCallback.onSettingsChanged(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load settings and puts them in the UI.
|
||||
*/
|
||||
public void loadSettings() {
|
||||
FileInputStream fis = null;
|
||||
try {
|
||||
String folder = AndroidLocation.getFolder();
|
||||
File f = new File(folder, SETTINGS_FILENAME);
|
||||
if (f.exists()) {
|
||||
fis = new FileInputStream(f);
|
||||
|
||||
Properties props = new Properties();
|
||||
props.load(fis);
|
||||
|
||||
mProxyServerText.setText(props.getProperty(JAVA_HTTP_PROXY_HOST, "")); //$NON-NLS-1$
|
||||
mProxyPortText.setText(props.getProperty(JAVA_HTTP_PROXY_PORT, "")); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
} catch (AndroidLocationException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (fis != null) {
|
||||
try {
|
||||
fis.close();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// End of hiding from SWT Designer
|
||||
//$hide<<$
|
||||
|
||||
@@ -26,7 +26,7 @@ public class SdkRepository {
|
||||
|
||||
/** The URL of the official Google sdk-repository site. */
|
||||
public static final String URL_GOOGLE_SDK_REPO_SITE =
|
||||
"https://dl.google.com/android/eclipse/repository/repository.xml"; //$NON-NLS-1$
|
||||
"https://dl.google.com/android/repository/repository.xml"; //$NON-NLS-1$
|
||||
|
||||
/** The XML namespace of the sdk-repository XML. */
|
||||
public static final String NS_SDK_REPOSITORY =
|
||||
|
||||
@@ -16,15 +16,42 @@
|
||||
|
||||
package com.android.sdkuilib.internal.repository;
|
||||
|
||||
import java.net.URL;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* Interface that a settings page must implement.
|
||||
*/
|
||||
public interface ISettingsPage {
|
||||
|
||||
/** Loads settings. Does not apply them. */
|
||||
public abstract void loadSettings();
|
||||
/** Java system setting picked up by {@link URL} for http proxy port */
|
||||
public static final String JAVA_HTTP_PROXY_PORT = "http.proxyPort"; //$NON-NLS-1$
|
||||
/** Java system setting picked up by {@link URL} for http proxy host */
|
||||
public static final String JAVA_HTTP_PROXY_HOST = "http.proxyHost"; //$NON-NLS-1$
|
||||
|
||||
/** Applies current settings. */
|
||||
public abstract void applySettings();
|
||||
/** Loads settings from the given {@link Properties} container and update the page UI. */
|
||||
public abstract void loadSettings(Properties in_settings);
|
||||
|
||||
/** Called by the application to retrieve settings from the UI and store them in
|
||||
* the given {@link Properties} container. */
|
||||
public abstract void retrieveSettings(Properties out_settings);
|
||||
|
||||
/**
|
||||
* Called by the application to give a callback that the page should invoke when
|
||||
* settings have changed.
|
||||
*/
|
||||
public abstract void setOnSettingsChanged(SettingsChangedCallback settingsChangedCallback);
|
||||
|
||||
/**
|
||||
* Callback used to notify the application that settings have changed and need to be
|
||||
* applied.
|
||||
*/
|
||||
public interface SettingsChangedCallback {
|
||||
/**
|
||||
* Invoked by the settings page when settings have changed and need to be
|
||||
* applied. The application will call {@link ISettingsPage#retrieveSettings(Properties)}
|
||||
* and apply the new settings.
|
||||
*/
|
||||
public abstract void onSettingsChanged(ISettingsPage page);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
/*
|
||||
* 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.repository;
|
||||
|
||||
import com.android.prefs.AndroidLocation;
|
||||
import com.android.prefs.AndroidLocation.AndroidLocationException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class SettingsController {
|
||||
|
||||
private static final String SETTINGS_FILENAME = "androidtool.cfg"; //$NON-NLS-1$
|
||||
|
||||
private final Properties mProperties = new Properties();
|
||||
|
||||
private ISettingsPage mSettingsPage;
|
||||
|
||||
public SettingsController() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Associate the given {@link ISettingsPage} with this {@link SettingsController}.
|
||||
*
|
||||
* This loads the current properties into the setting page UI.
|
||||
* It then associates the SettingsChanged callback with this controller.
|
||||
*/
|
||||
public void setSettingsPage(ISettingsPage settingsPage) {
|
||||
|
||||
mSettingsPage = settingsPage;
|
||||
mSettingsPage.loadSettings(mProperties);
|
||||
|
||||
settingsPage.setOnSettingsChanged(new ISettingsPage.SettingsChangedCallback() {
|
||||
public void onSettingsChanged(ISettingsPage page) {
|
||||
SettingsController.this.onSettingsChanged();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Load settings from the settings file.
|
||||
*/
|
||||
public void loadSettings() {
|
||||
FileInputStream fis = null;
|
||||
try {
|
||||
String folder = AndroidLocation.getFolder();
|
||||
File f = new File(folder, SETTINGS_FILENAME);
|
||||
if (f.exists()) {
|
||||
fis = new FileInputStream(f);
|
||||
|
||||
mProperties.load(fis);
|
||||
}
|
||||
|
||||
} catch (AndroidLocationException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (fis != null) {
|
||||
try {
|
||||
fis.close();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves settings to the settings file.
|
||||
*/
|
||||
public void saveSettings() {
|
||||
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
String folder = AndroidLocation.getFolder();
|
||||
File f = new File(folder, SETTINGS_FILENAME);
|
||||
|
||||
fos = new FileOutputStream(f);
|
||||
|
||||
mProperties.store( fos, "## Settings for Android Tool"); //$NON-NLS-1$
|
||||
|
||||
} catch (AndroidLocationException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (fos != null) {
|
||||
try {
|
||||
fos.close();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* When settings have changed: retrieve the new settings, apply them and save them.
|
||||
*
|
||||
* This updats Java system properties for the HTTP proxy.
|
||||
*/
|
||||
private void onSettingsChanged() {
|
||||
if (mSettingsPage == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
mSettingsPage.retrieveSettings(mProperties);
|
||||
applySettings();
|
||||
saveSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the current settings.
|
||||
*/
|
||||
public void applySettings() {
|
||||
Properties props = System.getProperties();
|
||||
props.put(ISettingsPage.JAVA_HTTP_PROXY_HOST,
|
||||
mProperties.getProperty(ISettingsPage.JAVA_HTTP_PROXY_HOST, "")); //$NON-NLS-1$
|
||||
props.put(ISettingsPage.JAVA_HTTP_PROXY_PORT,
|
||||
mProperties.getProperty(ISettingsPage.JAVA_HTTP_PROXY_PORT, "")); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
}
|
||||
@@ -55,6 +55,8 @@ class UpdaterData {
|
||||
|
||||
private ImageFactory mImageFactory;
|
||||
|
||||
private final SettingsController mSettingsController = new SettingsController();
|
||||
|
||||
private final ArrayList<ISdkListener> mListeners = new ArrayList<ISdkListener>();
|
||||
|
||||
public interface ISdkListener {
|
||||
@@ -128,6 +130,10 @@ class UpdaterData {
|
||||
return mAvdManager;
|
||||
}
|
||||
|
||||
public SettingsController getSettingsController() {
|
||||
return mSettingsController;
|
||||
}
|
||||
|
||||
public void addListeners(ISdkListener listener) {
|
||||
if (mListeners.contains(listener) == false) {
|
||||
mListeners.add(listener);
|
||||
|
||||
@@ -321,16 +321,21 @@ public class UpdaterWindowImpl {
|
||||
|
||||
/**
|
||||
* Initializes settings.
|
||||
* Thist must be called after addExtraPages(), which created a settings page.
|
||||
* Iterate through all the pages and it one is a setting page, load and apply these
|
||||
* settings.
|
||||
* This must be called after addExtraPages(), which created a settings page.
|
||||
* Iterate through all the pages to find the first (and supposedly unique) setting page,
|
||||
* and use it to load and apply these settings.
|
||||
*/
|
||||
private void initializeSettings() {
|
||||
SettingsController c = mUpdaterData.getSettingsController();
|
||||
c.loadSettings();
|
||||
c.applySettings();
|
||||
|
||||
for (Object page : mPages) {
|
||||
if (page instanceof ISettingsPage) {
|
||||
ISettingsPage settingsPage = (ISettingsPage) page;
|
||||
settingsPage.loadSettings();
|
||||
settingsPage.applySettings();
|
||||
|
||||
c.setSettingsPage(settingsPage);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user