OSDN Git Service

android-2.1_r1 snapshot
[android-x86/sdk.git] / sdkmanager / libs / sdklib / src / com / android / sdklib / internal / repository / PlatformPackage.java
1 /*\r
2  * Copyright (C) 2009 The Android Open Source Project\r
3  *\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
7  *\r
8  *      http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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
15  */\r
16 \r
17 package com.android.sdklib.internal.repository;\r
18 \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
26 \r
27 import org.w3c.dom.Node;\r
28 \r
29 import java.io.File;\r
30 import java.util.Map;\r
31 import java.util.Properties;\r
32 \r
33 /**\r
34  * Represents a platform XML node in an SDK repository.\r
35  */\r
36 public class PlatformPackage extends MinToolsPackage implements IPackageVersion {\r
37 \r
38     protected static final String PROP_VERSION       = "Platform.Version";      //$NON-NLS-1$\r
39 \r
40     /** The package version, for platform, add-on and doc packages. */\r
41     private final AndroidVersion mVersion;\r
42 \r
43     /** The version, a string, for platform packages. */\r
44     private final String mVersionName;\r
45 \r
46     /**\r
47      * Creates a new platform package from the attributes and elements of the given XML node.\r
48      * <p/>\r
49      * This constructor should throw an exception if the package cannot be created.\r
50      */\r
51     PlatformPackage(RepoSource source, Node packageNode, Map<String,String> licenses) {\r
52         super(source, packageNode, licenses);\r
53 \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
58             codeName = null;\r
59         }\r
60         mVersion = new AndroidVersion(apiLevel, codeName);\r
61     }\r
62 \r
63     /**\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
68      * <p/>\r
69      * By design, this creates a package with one and only one archive.\r
70      */\r
71     PlatformPackage(IAndroidTarget target, Properties props) {\r
72         super(  null,                       //source\r
73                 props,                      //properties\r
74                 target.getRevision(),       //revision\r
75                 null,                       //license\r
76                 target.getDescription(),    //description\r
77                 null,                       //descUrl\r
78                 Os.getCurrentOs(),          //archiveOs\r
79                 Arch.getCurrentArch(),      //archiveArch\r
80                 target.getLocation()        //archiveOsPath\r
81                 );\r
82 \r
83         mVersion = target.getVersion();\r
84         mVersionName  = target.getVersionName();\r
85     }\r
86 \r
87     /**\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
90      */\r
91     @Override\r
92     void saveProperties(Properties props) {\r
93         super.saveProperties(props);\r
94 \r
95         mVersion.saveProperties(props);\r
96 \r
97         if (mVersionName != null) {\r
98             props.setProperty(PROP_VERSION, mVersionName);\r
99         }\r
100 \r
101         if (getMinToolsRevision() != MIN_TOOLS_REV_NOT_SPECIFIED) {\r
102             props.setProperty(PROP_MIN_TOOLS_REV, Integer.toString(getMinToolsRevision()));\r
103         }\r
104     }\r
105 \r
106     /** Returns the version, a string, for platform packages. */\r
107     public String getVersionName() {\r
108         return mVersionName;\r
109     }\r
110 \r
111     /** Returns the package version, for platform, add-on and doc packages. */\r
112     public AndroidVersion getVersion() {\r
113         return mVersion;\r
114     }\r
115 \r
116     /** Returns a short description for an {@link IDescription}. */\r
117     @Override\r
118     public String getShortDescription() {\r
119         String s;\r
120 \r
121         if (mVersion.isPreview()) {\r
122             s = String.format("SDK Platform Android %1$s Preview, revision %2$s",\r
123                     getVersionName(),\r
124                     getRevision());\r
125         } else {\r
126             s = String.format("SDK Platform Android %1$s, API %2$d, revision %3$s",\r
127                 getVersionName(),\r
128                 mVersion.getApiLevel(),\r
129                 getRevision());\r
130         }\r
131 \r
132         return s;\r
133     }\r
134 \r
135     /**\r
136      * Returns a long description for an {@link IDescription}.\r
137      *\r
138      * The long description is whatever the XML contains for the &lt;description&gt; field,\r
139      * or the short description if the former is empty.\r
140      */\r
141     @Override\r
142     public String getLongDescription() {\r
143         String s = getDescription();\r
144         if (s == null || s.length() == 0) {\r
145             s = getShortDescription();\r
146         }\r
147 \r
148         if (s.indexOf("revision") == -1) {\r
149             s += String.format("\nRevision %1$d", getRevision());\r
150         }\r
151 \r
152         return s;\r
153     }\r
154 \r
155     /**\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
158      * <p/>\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
162      *\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
168      */\r
169     @Override\r
170     public File getInstallFolder(String osSdkRoot, String suggestedDir, SdkManager sdkManager) {\r
171 \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
178             }\r
179         }\r
180 \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
183 \r
184         return folder;\r
185     }\r
186 \r
187     @Override\r
188     public boolean sameItemAs(Package pkg) {\r
189         if (pkg instanceof PlatformPackage) {\r
190             PlatformPackage newPkg = (PlatformPackage)pkg;\r
191 \r
192             // check they are the same platform.\r
193             return newPkg.getVersion().equals(this.getVersion());\r
194         }\r
195 \r
196         return false;\r
197     }\r
198 }\r