OSDN Git Service

BUG 1922588: SDK Updater, Needs better license display
[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 Package {\r
37 \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
40 \r
41     /** The package version, for platform, add-on and doc packages. */\r
42     private final AndroidVersion mVersion;\r
43 \r
44     /** The version, a string, for platform packages. */\r
45     private final String mVersionName;\r
46 \r
47     /**\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
50      */\r
51     private final int mMinToolsRevision;\r
52 \r
53     /**\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
56      */\r
57     public static final int MIN_TOOLS_REV_NOT_SPECIFIED = 0;\r
58 \r
59     /**\r
60      * Creates a new platform package from the attributes and elements of the given XML node.\r
61      * <p/>\r
62      * This constructor should throw an exception if the package cannot be created.\r
63      */\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
70             codeName = null;\r
71         }\r
72         mVersion = new AndroidVersion(apiLevel, codeName);\r
73 \r
74         mMinToolsRevision = XmlParserUtils.getXmlInt(packageNode, SdkRepository.NODE_MIN_TOOLS_REV,\r
75                 MIN_TOOLS_REV_NOT_SPECIFIED);\r
76     }\r
77 \r
78     /**\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
83      */\r
84     PlatformPackage(IAndroidTarget target, Properties props) {\r
85         super(  null,                       //source\r
86                 props,                      //properties\r
87                 target.getRevision(),       //revision\r
88                 null,                       //license\r
89                 target.getDescription(),    //description\r
90                 null,                       //descUrl\r
91                 Os.getCurrentOs(),          //archiveOs\r
92                 Arch.getCurrentArch(),      //archiveArch\r
93                 target.getLocation()        //archiveOsPath\r
94                 );\r
95 \r
96         mVersion = target.getVersion();\r
97         mVersionName  = target.getVersionName();\r
98 \r
99         mMinToolsRevision = Integer.parseInt(getProperty(props, PROP_MIN_TOOLS_REV,\r
100                 Integer.toString(MIN_TOOLS_REV_NOT_SPECIFIED)));\r
101     }\r
102 \r
103     /**\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
106      */\r
107     @Override\r
108     void saveProperties(Properties props) {\r
109         super.saveProperties(props);\r
110 \r
111         mVersion.saveProperties(props);\r
112 \r
113         if (mVersionName != null) {\r
114             props.setProperty(PROP_VERSION, mVersionName);\r
115         }\r
116 \r
117         if (mMinToolsRevision != MIN_TOOLS_REV_NOT_SPECIFIED) {\r
118             props.setProperty(PROP_MIN_TOOLS_REV, Integer.toString(mMinToolsRevision));\r
119         }\r
120     }\r
121 \r
122     /** Returns the version, a string, for platform packages. */\r
123     public String getVersionName() {\r
124         return mVersionName;\r
125     }\r
126 \r
127     /** Returns the package version, for platform, add-on and doc packages. */\r
128     public AndroidVersion getVersion() {\r
129         return mVersion;\r
130     }\r
131 \r
132     /**\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
135      */\r
136     public int getMinToolsRevision() {\r
137         return mMinToolsRevision;\r
138     }\r
139 \r
140     /** Returns a short description for an {@link IDescription}. */\r
141     @Override\r
142     public String getShortDescription() {\r
143         String s;\r
144 \r
145         if (mVersion.isPreview()) {\r
146             s = String.format("SDK Platform Android %1$s (Preview)", getVersionName());\r
147         } else {\r
148             s = String.format("SDK Platform Android %1$s, API %2$d",\r
149                 getVersionName(),\r
150                 mVersion.getApiLevel());\r
151         }\r
152 \r
153         if (mMinToolsRevision != MIN_TOOLS_REV_NOT_SPECIFIED) {\r
154             s += String.format(" (tools rev: %1$d)", mMinToolsRevision);\r
155         }\r
156 \r
157         return s;\r
158     }\r
159 \r
160     /** Returns a long description for an {@link IDescription}. */\r
161     @Override\r
162     public String getLongDescription() {\r
163         return getShortDescription() + ".";\r
164     }\r
165 \r
166     /**\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
169      * <p/>\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
173      *\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
179      */\r
180     @Override\r
181     public File getInstallFolder(String osSdkRoot, String suggestedDir, SdkManager sdkManager) {\r
182 \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
189             }\r
190         }\r
191 \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
194 \r
195         return folder;\r
196     }\r
197 \r
198     @Override\r
199     public boolean sameItemAs(Package pkg) {\r
200         if (pkg instanceof PlatformPackage) {\r
201             PlatformPackage newPkg = (PlatformPackage)pkg;\r
202 \r
203             // check they are the same platform.\r
204             return newPkg.getVersion().equals(this.getVersion());\r
205         }\r
206 \r
207         return false;\r
208     }\r
209 }\r