OSDN Git Service

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