OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / sdk / 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.SdkRepository;\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      * <p/>\r
46      * This constructor should throw an exception if the package cannot be created.\r
47      */\r
48     DocPackage(RepoSource source, Node packageNode, Map<String,String> licenses) {\r
49         super(source, packageNode, licenses);\r
50 \r
51         int apiLevel = XmlParserUtils.getXmlInt   (packageNode, SdkRepository.NODE_API_LEVEL, 0);\r
52         String codeName = XmlParserUtils.getXmlString(packageNode, SdkRepository.NODE_CODENAME);\r
53         if (codeName.length() == 0) {\r
54             codeName = null;\r
55         }\r
56         mVersion = new AndroidVersion(apiLevel, codeName);\r
57     }\r
58 \r
59     /**\r
60      * Manually create a new package with one archive and the given attributes.\r
61      * This is used to create packages from local directories in which case there must be\r
62      * one archive which URL is the actual target location.\r
63      * <p/>\r
64      * By design, this creates a package with one and only one archive.\r
65      */\r
66     DocPackage(RepoSource source,\r
67             Properties props,\r
68             int apiLevel,\r
69             String codename,\r
70             int revision,\r
71             String license,\r
72             String description,\r
73             String descUrl,\r
74             Os archiveOs,\r
75             Arch archiveArch,\r
76             String archiveOsPath) {\r
77         super(source,\r
78                 props,\r
79                 revision,\r
80                 license,\r
81                 description,\r
82                 descUrl,\r
83                 archiveOs,\r
84                 archiveArch,\r
85                 archiveOsPath);\r
86         mVersion = new AndroidVersion(props, apiLevel, codename);\r
87     }\r
88 \r
89     /**\r
90      * Save the properties of the current packages in the given {@link Properties} object.\r
91      * These properties will later be give the constructor that takes a {@link Properties} object.\r
92      */\r
93     @Override\r
94     void saveProperties(Properties props) {\r
95         super.saveProperties(props);\r
96 \r
97         mVersion.saveProperties(props);\r
98     }\r
99 \r
100     /** Returns the version, for platform, add-on and doc packages.\r
101      *  Can be 0 if this is a local package of unknown api-level. */\r
102     public AndroidVersion getVersion() {\r
103         return mVersion;\r
104     }\r
105 \r
106     /** Returns a short description for an {@link IDescription}. */\r
107     @Override\r
108     public String getShortDescription() {\r
109         if (mVersion.isPreview()) {\r
110             return String.format("Documentation for Android '%1$s' Preview SDK, revision %2$s%3$s",\r
111                     mVersion.getCodename(),\r
112                     getRevision(),\r
113                     isObsolete() ? " (Obsolete)" : "");\r
114         } else {\r
115             return String.format("Documentation for Android SDK, API %1$d, revision %2$s%3$s",\r
116                     mVersion.getApiLevel(),\r
117                     getRevision(),\r
118                     isObsolete() ? " (Obsolete)" : "");\r
119         }\r
120     }\r
121 \r
122     /**\r
123      * Returns a long description for an {@link IDescription}.\r
124      *\r
125      * The long description is whatever the XML contains for the &lt;description&gt; field,\r
126      * or the short description if the former is empty.\r
127      */\r
128     @Override\r
129     public String getLongDescription() {\r
130         String s = getDescription();\r
131         if (s == null || s.length() == 0) {\r
132             s = getShortDescription();\r
133         }\r
134 \r
135         if (s.indexOf("revision") == -1) {\r
136             s += String.format("\nRevision %1$d%2$s",\r
137                     getRevision(),\r
138                     isObsolete() ? " (Obsolete)" : "");\r
139         }\r
140 \r
141         return s;\r
142     }\r
143 \r
144     /**\r
145      * Computes a potential installation folder if an archive of this package were\r
146      * to be installed right away in the given SDK root.\r
147      * <p/>\r
148      * A "doc" package should always be located in SDK/docs.\r
149      *\r
150      * @param osSdkRoot The OS path of the SDK root folder.\r
151      * @param suggestedDir A suggestion for the installation folder name, based on the root\r
152      *                     folder used in the zip archive.\r
153      * @param sdkManager An existing SDK manager to list current platforms and addons.\r
154      * @return A new {@link File} corresponding to the directory to use to install this package.\r
155      */\r
156     @Override\r
157     public File getInstallFolder(String osSdkRoot, String suggestedDir, SdkManager sdkManager) {\r
158         return new File(osSdkRoot, SdkConstants.FD_DOCS);\r
159     }\r
160 \r
161     @Override\r
162     public boolean sameItemAs(Package pkg) {\r
163         // only one doc package so any doc package is the same item.\r
164         return pkg instanceof DocPackage;\r
165     }\r
166 \r
167     /**\r
168      * {@inheritDoc}\r
169      *\r
170      * The comparison between doc packages is a bit more complex so we override the default\r
171      * implementation.\r
172      * <p/>\r
173      * Docs are upgrade if they have a higher api, or a similar api but a higher revision.\r
174      * <p/>\r
175      * What makes this more complex is handling codename.\r
176      */\r
177     @Override\r
178     public UpdateInfo canBeUpdatedBy(Package replacementPackage) {\r
179         if (replacementPackage == null) {\r
180             return UpdateInfo.INCOMPATIBLE;\r
181         }\r
182 \r
183         // check they are the same item.\r
184         if (sameItemAs(replacementPackage) == false) {\r
185             return UpdateInfo.INCOMPATIBLE;\r
186         }\r
187 \r
188         DocPackage replacementDoc = (DocPackage)replacementPackage;\r
189 \r
190         AndroidVersion replacementVersion = replacementDoc.getVersion();\r
191 \r
192         // the new doc is an update if the api level is higher (no matter the codename on either)\r
193         if (replacementVersion.getApiLevel() > mVersion.getApiLevel()) {\r
194             return UpdateInfo.UPDATE;\r
195         }\r
196 \r
197         // Check if they're the same exact (api and codename)\r
198         if (replacementVersion.equals(mVersion)) {\r
199             // exact same version, so check the revision level\r
200             if (replacementPackage.getRevision() > this.getRevision()) {\r
201                 return UpdateInfo.UPDATE;\r
202             }\r
203         } else {\r
204             // not the same version? we check if they have the same api level and the new one\r
205             // is a preview, in which case it's also an update (since preview have the api level\r
206             // of the _previous_ version.)\r
207             if (replacementVersion.getApiLevel() == mVersion.getApiLevel() &&\r
208                     replacementVersion.isPreview()) {\r
209                 return UpdateInfo.UPDATE;\r
210             }\r
211         }\r
212 \r
213         // not an upgrade but not incompatible either.\r
214         return UpdateInfo.NOT_UPDATE;\r
215     }\r
216 }\r