1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.sdklib.internal.repository; 18 19 import com.android.sdklib.AndroidVersion; 20 import com.android.sdklib.SdkConstants; 21 import com.android.sdklib.SdkManager; 22 import com.android.sdklib.internal.repository.Archive.Arch; 23 import com.android.sdklib.internal.repository.Archive.Os; 24 import com.android.sdklib.repository.SdkRepository; 25 26 import org.w3c.dom.Node; 27 28 import java.io.File; 29 import java.util.Map; 30 import java.util.Properties; 31 32 /** 33 * Represents a doc XML node in an SDK repository. 34 * <p/> 35 * Note that a doc package has a version and thus implements {@link IPackageVersion}. 36 * However there is no mandatory dependency that limits installation so this does not 37 * implement {@link IPlatformDependency}. 38 */ 39 public class DocPackage extends Package implements IPackageVersion { 40 41 private final AndroidVersion mVersion; 42 43 /** 44 * Creates a new doc package from the attributes and elements of the given XML node. 45 * <p/> 46 * This constructor should throw an exception if the package cannot be created. 47 */ 48 DocPackage(RepoSource source, Node packageNode, Map<String,String> licenses) { 49 super(source, packageNode, licenses); 50 51 int apiLevel = XmlParserUtils.getXmlInt (packageNode, SdkRepository.NODE_API_LEVEL, 0); 52 String codeName = XmlParserUtils.getXmlString(packageNode, SdkRepository.NODE_CODENAME); 53 if (codeName.length() == 0) { 54 codeName = null; 55 } 56 mVersion = new AndroidVersion(apiLevel, codeName); 57 } 58 59 /** 60 * Manually create a new package with one archive and the given attributes. 61 * This is used to create packages from local directories in which case there must be 62 * one archive which URL is the actual target location. 63 * <p/> 64 * By design, this creates a package with one and only one archive. 65 */ 66 DocPackage(RepoSource source, 67 Properties props, 68 int apiLevel, 69 String codename, 70 int revision, 71 String license, 72 String description, 73 String descUrl, 74 Os archiveOs, 75 Arch archiveArch, 76 String archiveOsPath) { 77 super(source, 78 props, 79 revision, 80 license, 81 description, 82 descUrl, 83 archiveOs, 84 archiveArch, 85 archiveOsPath); 86 mVersion = new AndroidVersion(props, apiLevel, codename); 87 } 88 89 /** 90 * Save the properties of the current packages in the given {@link Properties} object. 91 * These properties will later be give the constructor that takes a {@link Properties} object. 92 */ 93 @Override 94 void saveProperties(Properties props) { 95 super.saveProperties(props); 96 97 mVersion.saveProperties(props); 98 } 99 100 /** Returns the version, for platform, add-on and doc packages. 101 * Can be 0 if this is a local package of unknown api-level. */ 102 public AndroidVersion getVersion() { 103 return mVersion; 104 } 105 106 /** Returns a short description for an {@link IDescription}. */ 107 @Override 108 public String getShortDescription() { 109 if (mVersion.isPreview()) { 110 return String.format("Documentation for Android '%1$s' Preview SDK, revision %2$s%3$s", 111 mVersion.getCodename(), 112 getRevision(), 113 isObsolete() ? " (Obsolete)" : ""); 114 } else { 115 return String.format("Documentation for Android SDK, API %1$d, revision %2$s%3$s", 116 mVersion.getApiLevel(), 117 getRevision(), 118 isObsolete() ? " (Obsolete)" : ""); 119 } 120 } 121 122 /** 123 * Returns a long description for an {@link IDescription}. 124 * 125 * The long description is whatever the XML contains for the <description> field, 126 * or the short description if the former is empty. 127 */ 128 @Override 129 public String getLongDescription() { 130 String s = getDescription(); 131 if (s == null || s.length() == 0) { 132 s = getShortDescription(); 133 } 134 135 if (s.indexOf("revision") == -1) { 136 s += String.format("\nRevision %1$d%2$s", 137 getRevision(), 138 isObsolete() ? " (Obsolete)" : ""); 139 } 140 141 return s; 142 } 143 144 /** 145 * Computes a potential installation folder if an archive of this package were 146 * to be installed right away in the given SDK root. 147 * <p/> 148 * A "doc" package should always be located in SDK/docs. 149 * 150 * @param osSdkRoot The OS path of the SDK root folder. 151 * @param suggestedDir A suggestion for the installation folder name, based on the root 152 * folder used in the zip archive. 153 * @param sdkManager An existing SDK manager to list current platforms and addons. 154 * @return A new {@link File} corresponding to the directory to use to install this package. 155 */ 156 @Override 157 public File getInstallFolder(String osSdkRoot, String suggestedDir, SdkManager sdkManager) { 158 return new File(osSdkRoot, SdkConstants.FD_DOCS); 159 } 160 161 @Override 162 public boolean sameItemAs(Package pkg) { 163 // only one doc package so any doc package is the same item. 164 return pkg instanceof DocPackage; 165 } 166 167 /** 168 * {@inheritDoc} 169 * 170 * The comparison between doc packages is a bit more complex so we override the default 171 * implementation. 172 * <p/> 173 * Docs are upgrade if they have a higher api, or a similar api but a higher revision. 174 * <p/> 175 * What makes this more complex is handling codename. 176 */ 177 @Override 178 public UpdateInfo canBeUpdatedBy(Package replacementPackage) { 179 if (replacementPackage == null) { 180 return UpdateInfo.INCOMPATIBLE; 181 } 182 183 // check they are the same item. 184 if (sameItemAs(replacementPackage) == false) { 185 return UpdateInfo.INCOMPATIBLE; 186 } 187 188 DocPackage replacementDoc = (DocPackage)replacementPackage; 189 190 AndroidVersion replacementVersion = replacementDoc.getVersion(); 191 192 // the new doc is an update if the api level is higher (no matter the codename on either) 193 if (replacementVersion.getApiLevel() > mVersion.getApiLevel()) { 194 return UpdateInfo.UPDATE; 195 } 196 197 // Check if they're the same exact (api and codename) 198 if (replacementVersion.equals(mVersion)) { 199 // exact same version, so check the revision level 200 if (replacementPackage.getRevision() > this.getRevision()) { 201 return UpdateInfo.UPDATE; 202 } 203 } else { 204 // not the same version? we check if they have the same api level and the new one 205 // is a preview, in which case it's also an update (since preview have the api level 206 // of the _previous_ version.) 207 if (replacementVersion.getApiLevel() == mVersion.getApiLevel() && 208 replacementVersion.isPreview()) { 209 return UpdateInfo.UPDATE; 210 } 211 } 212 213 // not an upgrade but not incompatible either. 214 return UpdateInfo.NOT_UPDATE; 215 } 216 } 217