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 Package {
\r
38 private static final String PROP_VERSION = "Platform.Version"; //$NON-NLS-1$
\r
39 private static final String PROP_MIN_TOOLS_REV = "Platform.MinToolsRev"; //$NON-NLS-1$
\r
41 /** The package version, for platform, add-on and doc packages. */
\r
42 private final AndroidVersion mVersion;
\r
44 /** The version, a string, for platform packages. */
\r
45 private final String mVersionName;
\r
48 * The minimal revision of the tools package required by this extra package, if > 0,
\r
49 * or {@link #MIN_TOOLS_REV_NOT_SPECIFIED} if there is no such requirement.
\r
51 private final int mMinToolsRevision;
\r
54 * The value of {@link #mMinToolsRevision} when the {@link SdkRepository#NODE_MIN_TOOLS_REV}
\r
55 * was not specified in the XML source.
\r
57 public static final int MIN_TOOLS_REV_NOT_SPECIFIED = 0;
\r
60 * Creates a new platform package from the attributes and elements of the given XML node.
\r
62 * This constructor should throw an exception if the package cannot be created.
\r
64 PlatformPackage(RepoSource source, Node packageNode, Map<String,String> licenses) {
\r
65 super(source, packageNode, licenses);
\r
66 mVersionName = XmlParserUtils.getXmlString(packageNode, SdkRepository.NODE_VERSION);
\r
67 int apiLevel = XmlParserUtils.getXmlInt (packageNode, SdkRepository.NODE_API_LEVEL, 0);
\r
68 String codeName = XmlParserUtils.getXmlString(packageNode, SdkRepository.NODE_CODENAME);
\r
69 if (codeName.length() == 0) {
\r
72 mVersion = new AndroidVersion(apiLevel, codeName);
\r
74 mMinToolsRevision = XmlParserUtils.getXmlInt(packageNode, SdkRepository.NODE_MIN_TOOLS_REV,
\r
75 MIN_TOOLS_REV_NOT_SPECIFIED);
\r
79 * Creates a new platform package based on an actual {@link IAndroidTarget} (which
\r
80 * must have {@link IAndroidTarget#isPlatform()} true) from the {@link SdkManager}.
\r
81 * This is used to list local SDK folders in which case there is one archive which
\r
82 * URL is the actual target location.
\r
84 PlatformPackage(IAndroidTarget target, Properties props) {
\r
85 super( null, //source
\r
87 target.getRevision(), //revision
\r
89 target.getDescription(), //description
\r
91 Os.getCurrentOs(), //archiveOs
\r
92 Arch.getCurrentArch(), //archiveArch
\r
93 target.getLocation() //archiveOsPath
\r
96 mVersion = target.getVersion();
\r
97 mVersionName = target.getVersionName();
\r
99 mMinToolsRevision = Integer.parseInt(getProperty(props, PROP_MIN_TOOLS_REV,
\r
100 Integer.toString(MIN_TOOLS_REV_NOT_SPECIFIED)));
\r
104 * Save the properties of the current packages in the given {@link Properties} object.
\r
105 * These properties will later be given to a constructor that takes a {@link Properties} object.
\r
108 void saveProperties(Properties props) {
\r
109 super.saveProperties(props);
\r
111 mVersion.saveProperties(props);
\r
113 if (mVersionName != null) {
\r
114 props.setProperty(PROP_VERSION, mVersionName);
\r
117 if (mMinToolsRevision != MIN_TOOLS_REV_NOT_SPECIFIED) {
\r
118 props.setProperty(PROP_MIN_TOOLS_REV, Integer.toString(mMinToolsRevision));
\r
122 /** Returns the version, a string, for platform packages. */
\r
123 public String getVersionName() {
\r
124 return mVersionName;
\r
127 /** Returns the package version, for platform, add-on and doc packages. */
\r
128 public AndroidVersion getVersion() {
\r
133 * The minimal revision of the tools package required by this extra package, if > 0,
\r
134 * or {@link #MIN_TOOLS_REV_NOT_SPECIFIED} if there is no such requirement.
\r
136 public int getMinToolsRevision() {
\r
137 return mMinToolsRevision;
\r
140 /** Returns a short description for an {@link IDescription}. */
\r
142 public String getShortDescription() {
\r
145 if (mVersion.isPreview()) {
\r
146 s = String.format("SDK Platform Android %1$s (Preview)", getVersionName());
\r
148 s = String.format("SDK Platform Android %1$s, API %2$d",
\r
150 mVersion.getApiLevel());
\r
153 if (mMinToolsRevision != MIN_TOOLS_REV_NOT_SPECIFIED) {
\r
154 s += String.format(" (tools rev: %1$d)", mMinToolsRevision);
\r
160 /** Returns a long description for an {@link IDescription}. */
\r
162 public String getLongDescription() {
\r
163 return getShortDescription() + ".";
\r
167 * Computes a potential installation folder if an archive of this package were
\r
168 * to be installed right away in the given SDK root.
\r
170 * A platform package is typically installed in SDK/platforms/android-"version".
\r
171 * However if we can find a different directory under SDK/platform that already
\r
172 * has this platform version installed, we'll use that one.
\r
174 * @param osSdkRoot The OS path of the SDK root folder.
\r
175 * @param suggestedDir A suggestion for the installation folder name, based on the root
\r
176 * folder used in the zip archive.
\r
177 * @param sdkManager An existing SDK manager to list current platforms and addons.
\r
178 * @return A new {@link File} corresponding to the directory to use to install this package.
\r
181 public File getInstallFolder(String osSdkRoot, String suggestedDir, SdkManager sdkManager) {
\r
183 // First find if this platform is already installed. If so, reuse the same directory.
\r
184 for (IAndroidTarget target : sdkManager.getTargets()) {
\r
185 if (target.isPlatform() &&
\r
186 target.getVersion().equals(mVersion) &&
\r
187 target.getVersionName().equals(getVersionName())) {
\r
188 return new File(target.getLocation());
\r
192 File platforms = new File(osSdkRoot, SdkConstants.FD_PLATFORMS);
\r
193 File folder = new File(platforms, String.format("android-%s", getVersionName())); //$NON-NLS-1$
\r
199 public boolean sameItemAs(Package pkg) {
\r
200 if (pkg instanceof PlatformPackage) {
\r
201 PlatformPackage newPkg = (PlatformPackage)pkg;
\r
203 // check they are the same platform.
\r
204 return newPkg.getVersion().equals(this.getVersion());
\r