2 * Copyright (C) 2009 The Android Open Source Project
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package com.android.sdklib.internal.repository;
\r
19 import com.android.sdklib.AndroidVersion;
\r
20 import com.android.sdklib.IAndroidTarget;
\r
21 import com.android.sdklib.SdkConstants;
\r
22 import com.android.sdklib.SdkManager;
\r
23 import com.android.sdklib.internal.repository.Archive.Arch;
\r
24 import com.android.sdklib.internal.repository.Archive.Os;
\r
25 import com.android.sdklib.repository.SdkRepository;
\r
27 import org.w3c.dom.Node;
\r
29 import java.io.File;
\r
30 import java.util.Map;
\r
31 import java.util.Properties;
\r
34 * Represents a platform XML node in an SDK repository.
\r
36 public class PlatformPackage extends MinToolsPackage implements IPackageVersion {
\r
38 protected static final String PROP_VERSION = "Platform.Version"; //$NON-NLS-1$
\r
40 /** The package version, for platform, add-on and doc packages. */
\r
41 private final AndroidVersion mVersion;
\r
43 /** The version, a string, for platform packages. */
\r
44 private final String mVersionName;
\r
47 * Creates a new platform package from the attributes and elements of the given XML node.
\r
49 * This constructor should throw an exception if the package cannot be created.
\r
51 PlatformPackage(RepoSource source, Node packageNode, Map<String,String> licenses) {
\r
52 super(source, packageNode, licenses);
\r
54 mVersionName = XmlParserUtils.getXmlString(packageNode, SdkRepository.NODE_VERSION);
\r
55 int apiLevel = XmlParserUtils.getXmlInt (packageNode, SdkRepository.NODE_API_LEVEL, 0);
\r
56 String codeName = XmlParserUtils.getXmlString(packageNode, SdkRepository.NODE_CODENAME);
\r
57 if (codeName.length() == 0) {
\r
60 mVersion = new AndroidVersion(apiLevel, codeName);
\r
64 * Creates a new platform package based on an actual {@link IAndroidTarget} (which
\r
65 * must have {@link IAndroidTarget#isPlatform()} true) from the {@link SdkManager}.
\r
66 * This is used to list local SDK folders in which case there is one archive which
\r
67 * URL is the actual target location.
\r
69 * By design, this creates a package with one and only one archive.
\r
71 PlatformPackage(IAndroidTarget target, Properties props) {
\r
72 super( null, //source
\r
74 target.getRevision(), //revision
\r
76 target.getDescription(), //description
\r
78 Os.getCurrentOs(), //archiveOs
\r
79 Arch.getCurrentArch(), //archiveArch
\r
80 target.getLocation() //archiveOsPath
\r
83 mVersion = target.getVersion();
\r
84 mVersionName = target.getVersionName();
\r
88 * Save the properties of the current packages in the given {@link Properties} object.
\r
89 * These properties will later be given to a constructor that takes a {@link Properties} object.
\r
92 void saveProperties(Properties props) {
\r
93 super.saveProperties(props);
\r
95 mVersion.saveProperties(props);
\r
97 if (mVersionName != null) {
\r
98 props.setProperty(PROP_VERSION, mVersionName);
\r
101 if (getMinToolsRevision() != MIN_TOOLS_REV_NOT_SPECIFIED) {
\r
102 props.setProperty(PROP_MIN_TOOLS_REV, Integer.toString(getMinToolsRevision()));
\r
106 /** Returns the version, a string, for platform packages. */
\r
107 public String getVersionName() {
\r
108 return mVersionName;
\r
111 /** Returns the package version, for platform, add-on and doc packages. */
\r
112 public AndroidVersion getVersion() {
\r
116 /** Returns a short description for an {@link IDescription}. */
\r
118 public String getShortDescription() {
\r
121 if (mVersion.isPreview()) {
\r
122 s = String.format("SDK Platform Android %1$s Preview, revision %2$s",
\r
126 s = String.format("SDK Platform Android %1$s, API %2$d, revision %3$s",
\r
128 mVersion.getApiLevel(),
\r
136 * Returns a long description for an {@link IDescription}.
\r
138 * The long description is whatever the XML contains for the <description> field,
\r
139 * or the short description if the former is empty.
\r
142 public String getLongDescription() {
\r
143 String s = getDescription();
\r
144 if (s == null || s.length() == 0) {
\r
145 s = getShortDescription();
\r
148 if (s.indexOf("revision") == -1) {
\r
149 s += String.format("\nRevision %1$d", getRevision());
\r
156 * Computes a potential installation folder if an archive of this package were
\r
157 * to be installed right away in the given SDK root.
\r
159 * A platform package is typically installed in SDK/platforms/android-"version".
\r
160 * However if we can find a different directory under SDK/platform that already
\r
161 * has this platform version installed, we'll use that one.
\r
163 * @param osSdkRoot The OS path of the SDK root folder.
\r
164 * @param suggestedDir A suggestion for the installation folder name, based on the root
\r
165 * folder used in the zip archive.
\r
166 * @param sdkManager An existing SDK manager to list current platforms and addons.
\r
167 * @return A new {@link File} corresponding to the directory to use to install this package.
\r
170 public File getInstallFolder(String osSdkRoot, String suggestedDir, SdkManager sdkManager) {
\r
172 // First find if this platform is already installed. If so, reuse the same directory.
\r
173 for (IAndroidTarget target : sdkManager.getTargets()) {
\r
174 if (target.isPlatform() &&
\r
175 target.getVersion().equals(mVersion) &&
\r
176 target.getVersionName().equals(getVersionName())) {
\r
177 return new File(target.getLocation());
\r
181 File platforms = new File(osSdkRoot, SdkConstants.FD_PLATFORMS);
\r
182 File folder = new File(platforms, String.format("android-%s", getVersionName())); //$NON-NLS-1$
\r
188 public boolean sameItemAs(Package pkg) {
\r
189 if (pkg instanceof PlatformPackage) {
\r
190 PlatformPackage newPkg = (PlatformPackage)pkg;
\r
192 // check they are the same platform.
\r
193 return newPkg.getVersion().equals(this.getVersion());
\r