OSDN Git Service

5171454f49378c870eef0f5c8fbb9237e7f4fa4e
[android-x86/sdk.git] / sdkmanager / libs / sdklib / src / com / android / sdklib / internal / repository / DocPackage.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.SdkConstants;\r
21 import com.android.sdklib.SdkManager;\r
22 import com.android.sdklib.internal.repository.Archive.Arch;\r
23 import com.android.sdklib.internal.repository.Archive.Os;\r
24 import com.android.sdklib.repository.SdkRepoConstants;\r
25 \r
26 import org.w3c.dom.Node;\r
27 \r
28 import java.io.File;\r
29 import java.util.Map;\r
30 import java.util.Properties;\r
31 \r
32 /**\r
33  * Represents a doc XML node in an SDK repository.\r
34  * <p/>\r
35  * Note that a doc package has a version and thus implements {@link IPackageVersion}.\r
36  * However there is no mandatory dependency that limits installation so this does not\r
37  * implement {@link IPlatformDependency}.\r
38  */\r
39 public class DocPackage extends Package implements IPackageVersion {\r
40 \r
41     private final AndroidVersion mVersion;\r
42 \r
43     /**\r
44      * Creates a new doc package from the attributes and elements of the given XML node.\r
45      * This constructor should throw an exception if the package cannot be created.\r
46      *\r
47      * @param source The {@link SdkSource} where this is loaded from.\r
48      * @param packageNode The XML element being parsed.\r
49      * @param nsUri The namespace URI of the originating XML document, to be able to deal with\r
50      *          parameters that vary according to the originating XML schema.\r
51      * @param licenses The licenses loaded from the XML originating document.\r
52      */\r
53     DocPackage(SdkSource source, Node packageNode, String nsUri, Map<String,String> licenses) {\r
54         super(source, packageNode, nsUri, licenses);\r
55 \r
56         int apiLevel = XmlParserUtils.getXmlInt   (packageNode, SdkRepoConstants.NODE_API_LEVEL, 0);\r
57         String codeName = XmlParserUtils.getXmlString(packageNode, SdkRepoConstants.NODE_CODENAME);\r
58         if (codeName.length() == 0) {\r
59             codeName = null;\r
60         }\r
61         mVersion = new AndroidVersion(apiLevel, codeName);\r
62     }\r
63 \r
64     /**\r
65      * Manually create a new package with one archive and the given attributes.\r
66      * This is used to create packages from local directories in which case there must be\r
67      * one archive which 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     static Package create(SdkSource source,\r
72             Properties props,\r
73             int apiLevel,\r
74             String codename,\r
75             int revision,\r
76             String license,\r
77             String description,\r
78             String descUrl,\r
79             Os archiveOs,\r
80             Arch archiveArch,\r
81             String archiveOsPath) {\r
82         return new DocPackage(source, props, apiLevel, codename, revision, license, description,\r
83                 descUrl, archiveOs, archiveArch, archiveOsPath);\r
84     }\r
85 \r
86     private DocPackage(SdkSource source,\r
87             Properties props,\r
88             int apiLevel,\r
89             String codename,\r
90             int revision,\r
91             String license,\r
92             String description,\r
93             String descUrl,\r
94             Os archiveOs,\r
95             Arch archiveArch,\r
96             String archiveOsPath) {\r
97         super(source,\r
98                 props,\r
99                 revision,\r
100                 license,\r
101                 description,\r
102                 descUrl,\r
103                 archiveOs,\r
104                 archiveArch,\r
105                 archiveOsPath);\r
106         mVersion = new AndroidVersion(props, apiLevel, codename);\r
107     }\r
108 \r
109     /**\r
110      * Save the properties of the current packages in the given {@link Properties} object.\r
111      * These properties will later be give the constructor that takes a {@link Properties} object.\r
112      */\r
113     @Override\r
114     void saveProperties(Properties props) {\r
115         super.saveProperties(props);\r
116 \r
117         mVersion.saveProperties(props);\r
118     }\r
119 \r
120     /**\r
121      * Returns the version, for platform, add-on and doc packages.\r
122      * Can be 0 if this is a local package of unknown api-level.\r
123      */\r
124     public AndroidVersion getVersion() {\r
125         return mVersion;\r
126     }\r
127 \r
128     /**\r
129      * Returns a description of this package that is suitable for a list display.\r
130      * <p/>\r
131      * {@inheritDoc}\r
132      */\r
133     @Override\r
134     public String getListDescription() {\r
135         if (mVersion.isPreview()) {\r
136             return String.format("Documentation for Android '%1$s' Preview SDK%2$s",\r
137                     mVersion.getCodename(),\r
138                     isObsolete() ? " (Obsolete)" : "");\r
139         } else {\r
140             return String.format("Documentation for Android SDK%2$s",\r
141                     mVersion.getApiLevel(),\r
142                     isObsolete() ? " (Obsolete)" : "");\r
143         }\r
144     }\r
145 \r
146     /**\r
147      * Returns a short description for an {@link IDescription}.\r
148      */\r
149     @Override\r
150     public String getShortDescription() {\r
151         if (mVersion.isPreview()) {\r
152             return String.format("Documentation for Android '%1$s' Preview SDK, revision %2$s%3$s",\r
153                     mVersion.getCodename(),\r
154                     getRevision(),\r
155                     isObsolete() ? " (Obsolete)" : "");\r
156         } else {\r
157             return String.format("Documentation for Android SDK, API %1$d, revision %2$s%3$s",\r
158                     mVersion.getApiLevel(),\r
159                     getRevision(),\r
160                     isObsolete() ? " (Obsolete)" : "");\r
161         }\r
162     }\r
163 \r
164     /**\r
165      * Returns a long description for an {@link IDescription}.\r
166      *\r
167      * The long description is whatever the XML contains for the &lt;description&gt; field,\r
168      * or the short description if the former is empty.\r
169      */\r
170     @Override\r
171     public String getLongDescription() {\r
172         String s = getDescription();\r
173         if (s == null || s.length() == 0) {\r
174             s = getShortDescription();\r
175         }\r
176 \r
177         if (s.indexOf("revision") == -1) {\r
178             s += String.format("\nRevision %1$d%2$s",\r
179                     getRevision(),\r
180                     isObsolete() ? " (Obsolete)" : "");\r
181         }\r
182 \r
183         return s;\r
184     }\r
185 \r
186     /**\r
187      * Computes a potential installation folder if an archive of this package were\r
188      * to be installed right away in the given SDK root.\r
189      * <p/>\r
190      * A "doc" package should always be located in SDK/docs.\r
191      *\r
192      * @param osSdkRoot The OS path of the SDK root folder.\r
193      * @param sdkManager An existing SDK manager to list current platforms and addons.\r
194      * @return A new {@link File} corresponding to the directory to use to install this package.\r
195      */\r
196     @Override\r
197     public File getInstallFolder(String osSdkRoot, SdkManager sdkManager) {\r
198         return new File(osSdkRoot, SdkConstants.FD_DOCS);\r
199     }\r
200 \r
201     @Override\r
202     public boolean sameItemAs(Package pkg) {\r
203         // only one doc package so any doc package is the same item.\r
204         return pkg instanceof DocPackage;\r
205     }\r
206 \r
207     /**\r
208      * {@inheritDoc}\r
209      *\r
210      * The comparison between doc packages is a bit more complex so we override the default\r
211      * implementation.\r
212      * <p/>\r
213      * Docs are upgrade if they have a higher api, or a similar api but a higher revision.\r
214      * <p/>\r
215      * What makes this more complex is handling codename.\r
216      */\r
217     @Override\r
218     public UpdateInfo canBeUpdatedBy(Package replacementPackage) {\r
219         if (replacementPackage == null) {\r
220             return UpdateInfo.INCOMPATIBLE;\r
221         }\r
222 \r
223         // check they are the same item.\r
224         if (sameItemAs(replacementPackage) == false) {\r
225             return UpdateInfo.INCOMPATIBLE;\r
226         }\r
227 \r
228         DocPackage replacementDoc = (DocPackage)replacementPackage;\r
229 \r
230         AndroidVersion replacementVersion = replacementDoc.getVersion();\r
231 \r
232         // the new doc is an update if the api level is higher (no matter the codename on either)\r
233         if (replacementVersion.getApiLevel() > mVersion.getApiLevel()) {\r
234             return UpdateInfo.UPDATE;\r
235         }\r
236 \r
237         // Check if they're the same exact (api and codename)\r
238         if (replacementVersion.equals(mVersion)) {\r
239             // exact same version, so check the revision level\r
240             if (replacementPackage.getRevision() > this.getRevision()) {\r
241                 return UpdateInfo.UPDATE;\r
242             }\r
243         } else {\r
244             // not the same version? we check if they have the same api level and the new one\r
245             // is a preview, in which case it's also an update (since preview have the api level\r
246             // of the _previous_ version.)\r
247             if (replacementVersion.getApiLevel() == mVersion.getApiLevel() &&\r
248                     replacementVersion.isPreview()) {\r
249                 return UpdateInfo.UPDATE;\r
250             }\r
251         }\r
252 \r
253         // not an upgrade but not incompatible either.\r
254         return UpdateInfo.NOT_UPDATE;\r
255     }\r
256 }\r