Merge change 2300 into donut

* changes:
  SDK Updater: some refactoring and some new features.
This commit is contained in:
Android (Google) Code Review
2009-05-22 12:30:35 -07:00
23 changed files with 1096 additions and 456 deletions

View File

@@ -0,0 +1,54 @@
/*
* 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.sdklib.internal.repository;
import com.android.sdklib.repository.SdkRepository;
import org.w3c.dom.Node;
/**
*
*/
public class AddonPackage extends Package {
private final String mVendor;
private final String mName;
private final String mApiLevel;
public AddonPackage(Node packageNode) {
super(packageNode);
mVendor = getXmlString(packageNode, SdkRepository.NODE_VENDOR);
mName = getXmlString(packageNode, SdkRepository.NODE_NAME);
mApiLevel = getXmlString(packageNode, SdkRepository.NODE_API_LEVEL);
// TODO libs
}
public String getVendor() {
return mVendor;
}
public String getName() {
return mName;
}
public String getApiLevel() {
return mApiLevel;
}
}

View File

@@ -0,0 +1,29 @@
/*
* 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.sdklib.internal.repository;
import org.w3c.dom.Node;
/**
*
*/
public class DocPackage extends Package {
public DocPackage(Node packageNode) {
super(packageNode);
}
}

View File

@@ -0,0 +1,40 @@
/*
* 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.sdklib.internal.repository;
/**
* Interface for elements that can provide a description of themselves.
*/
public interface IDescription {
/**
* Returns a description of the given element. Cannot be null.
* <p/>
* A description is a multi-line of text, typically much more
* elaborate than what {@link #toString()} would provide.
*/
public abstract String getShortDescription();
/**
* Returns a description of the given element. Cannot be null.
* <p/>
* A description is a multi-line of text, typically much more
* elaborate than what {@link #toString()} would provide.
*/
public abstract String getLongDescription();
}

View File

@@ -0,0 +1,26 @@
/*
* 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.sdklib.internal.repository;
/**
* A task that executes and can update a monitor to display its status.
* The task will generally be run in a separate thread.
*/
public interface ITask {
public abstract void run(ITaskMonitor monitor);
}

View File

@@ -0,0 +1,25 @@
/*
* 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.sdklib.internal.repository;
/**
* A factory that can start and run new {@link ITask}s.
*/
public interface ITaskFactory {
public abstract void start(String title, ITask task);
}

View File

@@ -0,0 +1,63 @@
/*
* 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.sdklib.internal.repository;
/**
* A monitor interface for a {@link ITask}.
* <p/>
* Depending on the task factory that created the task, there might not be any UI
* or it might not implement all the methods, in which case calling them would be
* a no-op but is guaranteed not to crash.
* <p/>
* If the task runs in a non-UI worker thread, the task factory implementation
* will take care of the update the UI in the correct thread. The task itself
* must not have to deal with it.
*/
public interface ITaskMonitor {
/**
* Sets the description in the current task dialog.
* This method can be invoked from a non-UI thread.
*/
public void setDescription(String description);
/**
* Sets the result text in the current task dialog.
* This method can be invoked from a non-UI thread.
*/
public void setResult(String result);
/**
* Sets the max value of the progress bar.
* This method can be invoked from a non-UI thread.
*/
public void setProgressMax(int max);
/**
* Increments the current value of the progress bar.
* This method can be invoked from a non-UI thread.
*/
public void incProgress(int delta);
/**
* Returns true if the user requested to cancel the operation.
* It is up to the task thread to pool this and exit as soon
* as possible.
*/
public boolean cancelRequested();
}

View File

@@ -0,0 +1,92 @@
/*
* 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.sdklib.internal.repository;
import com.android.sdklib.repository.SdkRepository;
import org.w3c.dom.Node;
/**
*
*/
public class Package {
private final int mRevision;
private final String mDescription;
private final String mDescUrl;
private Package(int revision, String description, String descUrl) {
mRevision = revision;
mDescription = description;
mDescUrl = descUrl;
}
public Package(Node packageNode) {
this(getXmlInt (packageNode, SdkRepository.NODE_REVISION, 0),
getXmlString(packageNode, SdkRepository.NODE_DESCRIPTION),
getXmlString(packageNode, SdkRepository.NODE_DESC_URL));
// TODO archives
}
/** The revision, an int > 0, for all packages (platform, add-on, tool, doc). */
public int getRevision() {
return mRevision;
}
/** The optional description for all packages (platform, add-on, tool, doc) or for a lib. */
public String getDescription() {
return mDescription;
}
/** The optional description URL for all packages (platform, add-on, tool, doc).
* Can be empty but not null. */
public String getDescUrl() {
return mDescUrl;
}
/**
* Retrieves the value of that XML element as a string.
* Returns an empty string when the element is missing.
*/
protected static String getXmlString(Node node, String xmlLocalName) {
for(Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
if (child.getNodeType() == Node.ELEMENT_NODE &&
child.getNamespaceURI().equals(SdkRepository.NS_SDK_REPOSITORY)) {
if (xmlLocalName == null || child.getLocalName().equals(xmlLocalName)) {
return child.getTextContent();
}
}
}
return "";
}
/**
* Retrieves the value of that XML element as an integer.
* Returns the default value when the element is missing or is not an integer.
*/
protected static int getXmlInt(Node node, String xmlLocalName, int defaultValue) {
String s = getXmlString(node, xmlLocalName);
try {
return Integer.parseInt(s);
} catch (NumberFormatException e) {
return defaultValue;
}
}
}

View File

@@ -0,0 +1,46 @@
/*
* 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.sdklib.internal.repository;
import com.android.sdklib.repository.SdkRepository;
import org.w3c.dom.Node;
/**
*
*/
public class PlatformPackage extends Package {
private final String mVersion;
private final String mApiLevel;
public PlatformPackage(Node packageNode) {
super(packageNode);
mVersion = getXmlString(packageNode, SdkRepository.NODE_VERSION);
mApiLevel = getXmlString(packageNode, SdkRepository.NODE_API_LEVEL);
}
public String getVersion() {
return mVersion;
}
public String getApiLevel() {
return mApiLevel;
}
}

View File

@@ -0,0 +1,331 @@
/*
* 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.sdklib.internal.repository;
import com.android.sdklib.repository.SdkRepository;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URL;
import java.util.ArrayList;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
/**
* An sdk-repository source. It may be a full repository or an add-on only repository.
*/
public class RepoSource implements IDescription {
private final String mUrl;
private final boolean mAddonOnly;
private ArrayList<String> mPackages;
private String mDescription;
/**
* Constructs a new source for the given repository URL.
*/
public RepoSource(String url, boolean addonOnly) {
mUrl = url;
mAddonOnly = addonOnly;
setDefaultDescription();
}
/** Returns the URL of the source repository. */
public String getUrl() {
return mUrl;
}
/**
* Returns the list of known packages. This is null when the source hasn't been loaded yet.
*/
public ArrayList<String> getPackages() {
return mPackages;
}
public String getShortDescription() {
return mUrl;
}
public String getLongDescription() {
return mDescription == null ? "" : mDescription; //$NON-NLS-1$
}
/**
* Tries to fetch the repository index for the given URL.
*/
public void load(ITaskFactory taskFactory) {
taskFactory.start("Init SDK Updater", new ITask() {
public void run(ITaskMonitor monitor) {
monitor.setProgressMax(4);
setDefaultDescription();
monitor.setDescription(String.format("Fetching %1$s", mUrl));
monitor.incProgress(1);
String xml = fetchUrl(mUrl, monitor);
if (xml == null) {
mDescription += String.format("\nFailed to fetch URL %1$s", mUrl);
return;
}
monitor.setDescription("Validate XML");
monitor.incProgress(1);
if (!validateXml(xml, monitor)) {
mDescription += String.format("\nFailed to validate XML at %1$s", mUrl);
return;
}
monitor.setDescription("Parse XML");
monitor.incProgress(1);
parsePackages(xml, monitor);
if (mPackages.size() == 0) {
mDescription += "\nNo packages found.";
} else if (mPackages.size() == 1) {
mDescription += "\nOne package found.";
} else {
mDescription += String.format("\n%1$d packages found.", mPackages.size());
}
// done
monitor.incProgress(1);
}
});
}
private void setDefaultDescription() {
if (mAddonOnly) {
mDescription = String.format("Add-on Source: %1$s", mUrl);
} else {
mDescription = String.format("SDK Source: %1$s", mUrl);
}
}
/*
* References:
* Java URL Connection: http://java.sun.com/docs/books/tutorial/networking/urls/readingWriting.html
* Java URL Reader: http://java.sun.com/docs/books/tutorial/networking/urls/readingURL.html
* Java set Proxy: http://java.sun.com/docs/books/tutorial/networking/urls/_setProxy.html
*/
private String fetchUrl(String urlString, ITaskMonitor monitor) {
URL url;
try {
url = new URL(urlString);
StringBuilder xml = new StringBuilder();
InputStream is = null;
BufferedReader br = null;
try {
is = url.openStream();
br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
xml.append(line);
}
return xml.toString();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
// pass
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
// pass
}
}
}
} catch (IOException e) {
monitor.setResult(e.getMessage());
}
return null;
}
private boolean validateXml(String xml, ITaskMonitor monitor) {
try {
Validator validator = getValidator();
validator.validate(new StreamSource(new StringReader(xml)));
return true;
} catch (SAXException e) {
monitor.setResult(e.getMessage());
} catch (IOException e) {
monitor.setResult(e.getMessage());
}
return false;
}
/** Helper method that returns a validator for our XSD */
private Validator getValidator() throws SAXException {
InputStream xsdStream = SdkRepository.getXsdStream();
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// This may throw a SAX Exception if the schema itself is not a valid XSD
Schema schema = factory.newSchema(new StreamSource(xsdStream));
Validator validator = schema.newValidator();
return validator;
}
private boolean parsePackages(String xml, ITaskMonitor monitor) {
try {
Document doc = getDocument(xml);
Node root = getFirstChild(doc, SdkRepository.NODE_SDK_REPOSITORY);
if (root != null) {
mPackages = new ArrayList<String>();
for (Node child = root.getFirstChild();
child != null;
child = child.getNextSibling()) {
if (child.getNodeType() == Node.ELEMENT_NODE &&
child.getNamespaceURI().equals(SdkRepository.NS_SDK_REPOSITORY)) {
String name = child.getLocalName();
if (SdkRepository.NODE_ADD_ON.equals(name)) {
parseAddon(child, mPackages, monitor);
} else if (!mAddonOnly) {
if (SdkRepository.NODE_PLATFORM.equals(name)) {
parsePlatform(child, mPackages, monitor);
} else if (SdkRepository.NODE_DOC.equals(name)) {
parseDoc(child, mPackages, monitor);
} else if (SdkRepository.NODE_TOOL.equals(name)) {
parseTool(child, mPackages, monitor);
}
}
}
}
return true;
}
} catch (ParserConfigurationException e) {
monitor.setResult("Failed to create XML document builder for %1$s");
} catch (SAXException e) {
monitor.setResult("Failed to parse XML document %1$s");
} catch (IOException e) {
monitor.setResult("Failed to read XML document");
}
return false;
}
private Node getFirstChild(Node node, String xmlLocalName) {
for(Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
if (child.getNodeType() == Node.ELEMENT_NODE &&
child.getNamespaceURI().equals(SdkRepository.NS_SDK_REPOSITORY)) {
if (xmlLocalName == null || child.getLocalName().equals(xmlLocalName)) {
return child;
}
}
}
return null;
}
private Document getDocument(String xml)
throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringComments(true);
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xml)));
return doc;
}
private void parseAddon(Node addon, ArrayList<String> packages, ITaskMonitor monitor) {
// TODO Auto-generated method stub
String s = String.format("addon %1$s by %2$s, api %3$s, rev %4$s",
getFirstChild(addon, SdkRepository.NODE_NAME).getTextContent(),
getFirstChild(addon, SdkRepository.NODE_VENDOR).getTextContent(),
getFirstChild(addon, SdkRepository.NODE_API_LEVEL).getTextContent(),
getFirstChild(addon, SdkRepository.NODE_REVISION).getTextContent()
);
packages.add(s);
}
private void parsePlatform(Node platform, ArrayList<String> packages, ITaskMonitor monitor) {
// TODO Auto-generated method stub
String s = String.format("platform %1$s, api %2$s, rev %3$s",
getFirstChild(platform, SdkRepository.NODE_VERSION).getTextContent(),
getFirstChild(platform, SdkRepository.NODE_API_LEVEL).getTextContent(),
getFirstChild(platform, SdkRepository.NODE_REVISION).getTextContent()
);
packages.add(s);
}
private void parseDoc(Node doc, ArrayList<String> packages, ITaskMonitor monitor) {
// TODO Auto-generated method stub
String s = String.format("doc for api %1$s, rev %2$s",
getFirstChild(doc, SdkRepository.NODE_API_LEVEL).getTextContent(),
getFirstChild(doc, SdkRepository.NODE_REVISION).getTextContent()
);
packages.add(s);
}
private void parseTool(Node tool, ArrayList<String> packages, ITaskMonitor monitor) {
// TODO Auto-generated method stub
String s = String.format("tool, rev %1$s",
getFirstChild(tool, SdkRepository.NODE_REVISION).getTextContent()
);
packages.add(s);
}
}

View File

@@ -0,0 +1,40 @@
/*
* 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.sdklib.internal.repository;
import com.android.sdklib.repository.SdkRepository;
import org.w3c.dom.Node;
/**
*
*/
public class ToolPackage extends Package {
private final String mApiLevel;
public ToolPackage(Node packageNode) {
super(packageNode);
mApiLevel = getXmlString(packageNode, SdkRepository.NODE_API_LEVEL);
}
public String getApiLevel() {
return mApiLevel;
}
}

View File

@@ -19,24 +19,64 @@ package com.android.sdklib.repository;
import java.io.InputStream;
/**
* Constants for the sdk-repository XML Schema
* Public constants for the sdk-repository XML Schema.
*/
public class SdkRepository {
/** The XML namespace of the sdk-repository XML. */
public static final String NS_SDK_REPOSITORY =
"http://schemas.android.com/sdk/android/repository/1"; //$NON-NLS-1$
public static final String NODE_VERSION = "version"; //$NON-NLS-1$
public static final String NODE_REVISION = "revision"; //$NON-NLS-1$
public static final String NODE_API_LEVEL = "api-level"; //$NON-NLS-1$
public static final String NODE_VENDOR = "vendor"; //$NON-NLS-1$
public static final String NODE_NAME = "name"; //$NON-NLS-1$
public static final String NODE_TOOL = "tool"; //$NON-NLS-1$
public static final String NODE_DOC = "doc"; //$NON-NLS-1$
public static final String NODE_PLATFORM = "platform"; //$NON-NLS-1$
public static final String NODE_ADD_ON = "add-on"; //$NON-NLS-1$
/** The root sdk-repository element */
public static final String NODE_SDK_REPOSITORY = "sdk-repository"; //$NON-NLS-1$
/** A platform package. */
public static final String NODE_PLATFORM = "platform"; //$NON-NLS-1$
/** An add-on package. */
public static final String NODE_ADD_ON = "add-on"; //$NON-NLS-1$
/** A tool package. */
public static final String NODE_TOOL = "tool"; //$NON-NLS-1$
/** A doc package. */
public static final String NODE_DOC = "doc"; //$NON-NLS-1$
/** The revision, an int > 0, for all packages (platform, add-on, tool, doc). */
public static final String NODE_REVISION = "revision"; //$NON-NLS-1$
/** The optional description for all packages (platform, add-on, tool, doc) or for a lib. */
public static final String NODE_DESCRIPTION = "description"; //$NON-NLS-1$
/** The optional description URL for all packages (platform, add-on, tool, doc). */
public static final String NODE_DESC_URL = "desc-url"; //$NON-NLS-1$
/** The version, a string, for platform packages. */
public static final String NODE_VERSION = "version"; //$NON-NLS-1$
/** The api-level, an int > 0, for platform, add-on and doc packages. */
public static final String NODE_API_LEVEL = "api-level"; //$NON-NLS-1$
/** The vendor, a string, for add-on packages. */
public static final String NODE_VENDOR = "vendor"; //$NON-NLS-1$
/** The name, a string, for add-on packages or for libraries. */
public static final String NODE_NAME = "name"; //$NON-NLS-1$
/** The libs container, optional for an add-on. */
public static final String NODE_LIBS = "libs"; //$NON-NLS-1$
/** A lib element in a libs container. */
public static final String NODE_LIB = "lib"; //$NON-NLS-1$
/** The archives container, for all packages. */
public static final String NODE_ARCHVIES = "archives"; //$NON-NLS-1$
/** An archive element, for the archives container. */
public static final String NODE_ARCHVIE = "archive"; //$NON-NLS-1$
/** An archive size, an int > 0. */
public static final String NODE_SIZE = "size"; //$NON-NLS-1$
/** A sha1 archive checksum, as a 40-char hex. */
public static final String NODE_CHECKSUM = "checksum"; //$NON-NLS-1$
/** A download archive URL, either absolute or relative to the repository xml. */
public static final String NODE_URL = "url"; //$NON-NLS-1$
/** An archive OS attribute, mandatory. */
public static final String NODE_OS = "os"; //$NON-NLS-1$
/** An optional archive Architecture attribute. */
public static final String NODE_ARCH = "arch"; //$NON-NLS-1$
public static InputStream getXsdStream() {
return SdkRepository.class.getResourceAsStream("sdk-repository.xsd"); //$NON-NLS-1$
}