| 1 |
/* |
| 2 |
* Copyright (C) 2014 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.manifmerger; |
| 18 |
|
| 19 |
import static com.android.manifmerger.ManifestModel.NodeTypes.USES_PERMISSION; |
| 20 |
import static com.android.manifmerger.ManifestModel.NodeTypes.USES_SDK; |
| 21 |
import static com.android.manifmerger.PlaceholderHandler.KeyBasedValueResolver; |
| 22 |
|
| 23 |
import com.android.SdkConstants; |
| 24 |
import com.android.annotations.NonNull; |
| 25 |
import com.android.annotations.Nullable; |
| 26 |
import com.android.ide.common.blame.SourceFile; |
| 27 |
import com.android.ide.common.blame.SourceFilePosition; |
| 28 |
import com.android.ide.common.blame.SourcePosition; |
| 29 |
import com.android.ide.common.xml.XmlFormatPreferences; |
| 30 |
import com.android.ide.common.xml.XmlFormatStyle; |
| 31 |
import com.android.ide.common.xml.XmlPrettyPrinter; |
| 32 |
import com.android.sdklib.SdkVersionInfo; |
| 33 |
import com.android.utils.Pair; |
| 34 |
import com.android.utils.PositionXmlParser; |
| 35 |
import com.google.common.base.Optional; |
| 36 |
import com.google.common.base.Preconditions; |
| 37 |
import com.google.common.base.Strings; |
| 38 |
import com.google.common.collect.ImmutableList; |
| 39 |
|
| 40 |
import org.w3c.dom.Document; |
| 41 |
import org.w3c.dom.Element; |
| 42 |
import org.w3c.dom.Node; |
| 43 |
|
| 44 |
import java.util.concurrent.atomic.AtomicReference; |
| 45 |
|
| 46 |
/** |
| 47 |
* Represents a loaded xml document. |
| 48 |
* |
| 49 |
* Has pointers to the root {@link XmlElement} element and provides services to persist the document |
| 50 |
* to an external format. Also provides abilities to be merged with other |
| 51 |
* {@link com.android.manifmerger.XmlDocument} as well as access to the line numbers for all |
| 52 |
* document's xml elements and attributes. |
| 53 |
* |
| 54 |
*/ |
| 55 |
public class XmlDocument { |
| 56 |
|
| 57 |
private static final String DEFAULT_SDK_VERSION = "1"; |
| 58 |
|
| 59 |
/** |
| 60 |
* The document type. |
| 61 |
*/ |
| 62 |
enum Type { |
| 63 |
/** |
| 64 |
* A manifest overlay as found in the build types and variants. |
| 65 |
*/ |
| 66 |
OVERLAY, |
| 67 |
/** |
| 68 |
* The main android manifest file. |
| 69 |
*/ |
| 70 |
MAIN, |
| 71 |
/** |
| 72 |
* A library manifest that is imported in the application. |
| 73 |
*/ |
| 74 |
LIBRARY |
| 75 |
} |
| 76 |
|
| 77 |
private final Element mRootElement; |
| 78 |
// this is initialized lazily to avoid un-necessary early parsing. |
| 79 |
@NonNull |
| 80 |
private final AtomicReference<XmlElement> mRootNode = new AtomicReference<XmlElement>(null); |
| 81 |
@NonNull |
| 82 |
private final SourceFile mSourceFile; |
| 83 |
@NonNull |
| 84 |
private final KeyResolver<String> mSelectors; |
| 85 |
@NonNull |
| 86 |
private final KeyBasedValueResolver<ManifestSystemProperty> mSystemPropertyResolver; |
| 87 |
@NonNull |
| 88 |
private final Type mType; |
| 89 |
@NonNull |
| 90 |
private final Optional<String> mMainManifestPackageName; |
| 91 |
|
| 92 |
public XmlDocument( |
| 93 |
@NonNull SourceFile sourceLocation, |
| 94 |
@NonNull KeyResolver<String> selectors, |
| 95 |
@NonNull KeyBasedValueResolver<ManifestSystemProperty> systemPropertyResolver, |
| 96 |
@NonNull Element element, |
| 97 |
@NonNull Type type, |
| 98 |
@NonNull Optional<String> mainManifestPackageName) { |
| 99 |
this.mSourceFile = Preconditions.checkNotNull(sourceLocation); |
| 100 |
this.mRootElement = Preconditions.checkNotNull(element); |
| 101 |
this.mSelectors = Preconditions.checkNotNull(selectors); |
| 102 |
this.mSystemPropertyResolver = Preconditions.checkNotNull(systemPropertyResolver); |
| 103 |
this.mType = type; |
| 104 |
this.mMainManifestPackageName = mainManifestPackageName; |
| 105 |
} |
| 106 |
|
| 107 |
@NonNull |
| 108 |
public Type getFileType() { |
| 109 |
return mType; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Returns a pretty string representation of this document. |
| 114 |
*/ |
| 115 |
@NonNull |
| 116 |
public String prettyPrint() { |
| 117 |
return XmlPrettyPrinter.prettyPrint( |
| 118 |
getXml(), |
| 119 |
XmlFormatPreferences.defaults(), |
| 120 |
XmlFormatStyle.get(getRootNode().getXml()), |
| 121 |
null, /* endOfLineSeparator */ |
| 122 |
false /* endWithNewLine */); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* merge this higher priority document with a higher priority document. |
| 127 |
* @param lowerPriorityDocument the lower priority document to merge in. |
| 128 |
* @param mergingReportBuilder the merging report to record errors and actions. |
| 129 |
* @return a new merged {@link com.android.manifmerger.XmlDocument} or |
| 130 |
* {@link Optional#absent()} if there were errors during the merging activities. |
| 131 |
*/ |
| 132 |
@NonNull |
| 133 |
public Optional<XmlDocument> merge( |
| 134 |
@NonNull XmlDocument lowerPriorityDocument, |
| 135 |
@NonNull MergingReport.Builder mergingReportBuilder) { |
| 136 |
|
| 137 |
if (getFileType() == Type.MAIN) { |
| 138 |
mergingReportBuilder.getActionRecorder().recordDefaultNodeAction(getRootNode()); |
| 139 |
} |
| 140 |
|
| 141 |
getRootNode().mergeWithLowerPriorityNode( |
| 142 |
lowerPriorityDocument.getRootNode(), mergingReportBuilder); |
| 143 |
|
| 144 |
addImplicitElements(lowerPriorityDocument, mergingReportBuilder); |
| 145 |
|
| 146 |
// force re-parsing as new nodes may have appeared. |
| 147 |
return mergingReportBuilder.hasErrors() |
| 148 |
? Optional.<XmlDocument>absent() |
| 149 |
: Optional.of(reparse()); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Forces a re-parsing of the document |
| 154 |
* @return a new {@link com.android.manifmerger.XmlDocument} with up to date information. |
| 155 |
*/ |
| 156 |
@NonNull |
| 157 |
public XmlDocument reparse() { |
| 158 |
return new XmlDocument( |
| 159 |
mSourceFile, |
| 160 |
mSelectors, |
| 161 |
mSystemPropertyResolver, |
| 162 |
mRootElement, |
| 163 |
mType, |
| 164 |
mMainManifestPackageName); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Returns a {@link com.android.manifmerger.KeyResolver} capable of resolving all selectors |
| 169 |
* types |
| 170 |
*/ |
| 171 |
@NonNull |
| 172 |
public KeyResolver<String> getSelectors() { |
| 173 |
return mSelectors; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Returns the {@link com.android.manifmerger.PlaceholderHandler.KeyBasedValueResolver} capable |
| 178 |
* of resolving all injected {@link ManifestSystemProperty} |
| 179 |
*/ |
| 180 |
@NonNull |
| 181 |
public KeyBasedValueResolver<ManifestSystemProperty> getSystemPropertyResolver() { |
| 182 |
return mSystemPropertyResolver; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Compares this document to another {@link com.android.manifmerger.XmlDocument} ignoring all |
| 187 |
* attributes belonging to the {@link com.android.SdkConstants#TOOLS_URI} namespace. |
| 188 |
* |
| 189 |
* @param other the other document to compare against. |
| 190 |
* @return a {@link String} describing the differences between the two XML elements or |
| 191 |
* {@link Optional#absent()} if they are equals. |
| 192 |
*/ |
| 193 |
@SuppressWarnings("CovariantCompareTo") |
| 194 |
public Optional<String> compareTo(@NonNull XmlDocument other) { |
| 195 |
return getRootNode().compareTo(other.getRootNode()); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Returns the position of the specified {@link XmlNode}. |
| 200 |
*/ |
| 201 |
@NonNull |
| 202 |
static SourcePosition getNodePosition(@NonNull XmlNode node) { |
| 203 |
return getNodePosition(node.getXml()); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Returns the position of the specified {@link org.w3c.dom.Node}. |
| 208 |
*/ |
| 209 |
@NonNull |
| 210 |
static SourcePosition getNodePosition(@NonNull Node xml) { |
| 211 |
return PositionXmlParser.getPosition(xml); |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Returns the {@link SourceFile} associated with this XML document. |
| 216 |
* <p> |
| 217 |
* NOTE: You should <b>not</b> read the contents of the file directly; if you need to |
| 218 |
* access the content, use {@link ManifestMerger2#getFileStreamProvider()} instead. |
| 219 |
* |
| 220 |
* @return the source file |
| 221 |
*/ |
| 222 |
@NonNull |
| 223 |
public SourceFile getSourceFile() { |
| 224 |
return mSourceFile; |
| 225 |
} |
| 226 |
|
| 227 |
public synchronized XmlElement getRootNode() { |
| 228 |
if (mRootNode.get() == null) { |
| 229 |
this.mRootNode.set(new XmlElement(mRootElement, this)); |
| 230 |
} |
| 231 |
return mRootNode.get(); |
| 232 |
} |
| 233 |
|
| 234 |
public Optional<XmlElement> getByTypeAndKey( |
| 235 |
ManifestModel.NodeTypes type, |
| 236 |
@Nullable String keyValue) { |
| 237 |
|
| 238 |
return getRootNode().getNodeByTypeAndKey(type, keyValue); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Package name for this android manifest which will be used to resolve |
| 243 |
* partial path. In the case of Overlays, this is absent and the main |
| 244 |
* manifest packageName must be used. |
| 245 |
* @return the package name to do partial class names resolution. |
| 246 |
*/ |
| 247 |
public String getPackageName() { |
| 248 |
return mMainManifestPackageName.or(mRootElement.getAttribute("package")); |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Returns the package name to use to expand the attributes values with the |
| 253 |
* document's package name |
| 254 |
* @return the package name to use for attribute expansion. |
| 255 |
*/ |
| 256 |
public String getPackageNameForAttributeExpansion() { |
| 257 |
String aPackage = mRootElement.getAttribute("package"); |
| 258 |
if (aPackage != null) { |
| 259 |
return aPackage; |
| 260 |
} |
| 261 |
if (mMainManifestPackageName.isPresent()) { |
| 262 |
return mMainManifestPackageName.get(); |
| 263 |
} |
| 264 |
throw new RuntimeException("No package present in overlay or main manifest file"); |
| 265 |
} |
| 266 |
|
| 267 |
public Optional<XmlAttribute> getPackage() { |
| 268 |
Optional<XmlAttribute> packageAttribute = |
| 269 |
getRootNode().getAttribute(XmlNode.fromXmlName("package")); |
| 270 |
return packageAttribute.isPresent() |
| 271 |
? packageAttribute |
| 272 |
: getRootNode().getAttribute(XmlNode.fromNSName( |
| 273 |
SdkConstants.ANDROID_URI, "android", "package")); |
| 274 |
} |
| 275 |
|
| 276 |
public Document getXml() { |
| 277 |
return mRootElement.getOwnerDocument(); |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Returns the minSdk version specified in the uses_sdk element if present or the |
| 282 |
* default value. |
| 283 |
*/ |
| 284 |
@NonNull |
| 285 |
private String getRawMinSdkVersion() { |
| 286 |
Optional<XmlElement> usesSdk = getByTypeAndKey( |
| 287 |
ManifestModel.NodeTypes.USES_SDK, null); |
| 288 |
if (usesSdk.isPresent()) { |
| 289 |
Optional<XmlAttribute> minSdkVersion = usesSdk.get() |
| 290 |
.getAttribute(XmlNode.fromXmlName("android:minSdkVersion")); |
| 291 |
if (minSdkVersion.isPresent()) { |
| 292 |
return minSdkVersion.get().getValue(); |
| 293 |
} |
| 294 |
} |
| 295 |
return DEFAULT_SDK_VERSION; |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* Returns the minSdk version for this manifest file. It can be injected from the outer |
| 300 |
* build.gradle or can be expressed in the uses_sdk element. |
| 301 |
*/ |
| 302 |
@NonNull |
| 303 |
private String getMinSdkVersion() { |
| 304 |
// check for system properties. |
| 305 |
String injectedMinSdk = mSystemPropertyResolver.getValue(ManifestSystemProperty.MIN_SDK_VERSION); |
| 306 |
if (injectedMinSdk != null) { |
| 307 |
return injectedMinSdk; |
| 308 |
} |
| 309 |
return getRawMinSdkVersion(); |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* Returns the targetSdk version specified in the uses_sdk element if present or the |
| 314 |
* default value. |
| 315 |
*/ |
| 316 |
@NonNull |
| 317 |
private String getRawTargetSdkVersion() { |
| 318 |
|
| 319 |
Optional<XmlElement> usesSdk = getByTypeAndKey( |
| 320 |
ManifestModel.NodeTypes.USES_SDK, null); |
| 321 |
if (usesSdk.isPresent()) { |
| 322 |
Optional<XmlAttribute> targetSdkVersion = usesSdk.get() |
| 323 |
.getAttribute(XmlNode.fromXmlName("android:targetSdkVersion")); |
| 324 |
if (targetSdkVersion.isPresent()) { |
| 325 |
return targetSdkVersion.get().getValue(); |
| 326 |
} |
| 327 |
} |
| 328 |
return getRawMinSdkVersion(); |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* Returns the targetSdk version for this manifest file. It can be injected from the outer |
| 333 |
* build.gradle or can be expressed in the uses_sdk element. |
| 334 |
*/ |
| 335 |
@NonNull |
| 336 |
private String getTargetSdkVersion() { |
| 337 |
|
| 338 |
// check for system properties. |
| 339 |
String injectedTargetVersion = mSystemPropertyResolver |
| 340 |
.getValue(ManifestSystemProperty.TARGET_SDK_VERSION); |
| 341 |
if (injectedTargetVersion != null) { |
| 342 |
return injectedTargetVersion; |
| 343 |
} |
| 344 |
return getRawTargetSdkVersion(); |
| 345 |
} |
| 346 |
|
| 347 |
/** |
| 348 |
* Decodes a sdk version from either its decimal representation or from a platform code name. |
| 349 |
* @param attributeVersion the sdk version attribute as specified by users. |
| 350 |
* @return the integer representation of the platform level. |
| 351 |
*/ |
| 352 |
private static int getApiLevelFromAttribute(@NonNull String attributeVersion) { |
| 353 |
Preconditions.checkArgument(!Strings.isNullOrEmpty(attributeVersion)); |
| 354 |
if (Character.isDigit(attributeVersion.charAt(0))) { |
| 355 |
return Integer.parseInt(attributeVersion); |
| 356 |
} |
| 357 |
return SdkVersionInfo.getApiByPreviewName(attributeVersion, true); |
| 358 |
} |
| 359 |
|
| 360 |
/** |
| 361 |
* Add all implicit elements from the passed lower priority document that are |
| 362 |
* required in the target SDK. |
| 363 |
*/ |
| 364 |
@SuppressWarnings("unchecked") // compiler confused about varargs and generics. |
| 365 |
private void addImplicitElements(@NonNull XmlDocument lowerPriorityDocument, |
| 366 |
@NonNull MergingReport.Builder mergingReport) { |
| 367 |
|
| 368 |
// if this document is an overlay, tolerate the absence of uses-sdk and do not |
| 369 |
// assume implicit minimum versions. |
| 370 |
Optional<XmlElement> usesSdk = getByTypeAndKey( |
| 371 |
ManifestModel.NodeTypes.USES_SDK, null); |
| 372 |
if (mType == Type.OVERLAY && !usesSdk.isPresent()) { |
| 373 |
return; |
| 374 |
} |
| 375 |
|
| 376 |
// check that the uses-sdk element does not have any tools:node instruction. |
| 377 |
if (usesSdk.isPresent()) { |
| 378 |
XmlElement usesSdkElement = usesSdk.get(); |
| 379 |
if (usesSdkElement.getOperationType() != NodeOperationType.MERGE) { |
| 380 |
mergingReport |
| 381 |
.addMessage( |
| 382 |
new SourceFilePosition( |
| 383 |
getSourceFile(), |
| 384 |
usesSdkElement.getPosition()), |
| 385 |
MergingReport.Record.Severity.ERROR, |
| 386 |
"uses-sdk element cannot have a \"tools:node\" attribute"); |
| 387 |
return; |
| 388 |
} |
| 389 |
} |
| 390 |
int thisTargetSdk = getApiLevelFromAttribute(getTargetSdkVersion()); |
| 391 |
|
| 392 |
// when we are importing a library, we should never use the build.gradle injected |
| 393 |
// values (only valid for overlay, main manifest) so use the raw versions coming from |
| 394 |
// the AndroidManifest.xml |
| 395 |
int libraryTargetSdk = getApiLevelFromAttribute( |
| 396 |
lowerPriorityDocument.getFileType() == Type.LIBRARY |
| 397 |
? lowerPriorityDocument.getRawTargetSdkVersion() |
| 398 |
: lowerPriorityDocument.getTargetSdkVersion()); |
| 399 |
|
| 400 |
// if library is using a code name rather than an API level, make sure this document target |
| 401 |
// sdk version is using the same code name. |
| 402 |
String libraryTargetSdkVersion = lowerPriorityDocument.getTargetSdkVersion(); |
| 403 |
if (!Character.isDigit(libraryTargetSdkVersion.charAt(0))) { |
| 404 |
// this is a code name, ensure this document uses the same code name. |
| 405 |
if (!libraryTargetSdkVersion.equals(getTargetSdkVersion())) { |
| 406 |
mergingReport.addMessage(getSourceFile(), MergingReport.Record.Severity.ERROR, |
| 407 |
String.format( |
| 408 |
"uses-sdk:targetSdkVersion %1$s cannot be different than version " |
| 409 |
+ "%2$s declared in library %3$s", |
| 410 |
getTargetSdkVersion(), |
| 411 |
libraryTargetSdkVersion, |
| 412 |
lowerPriorityDocument.getSourceFile().print(false) |
| 413 |
) |
| 414 |
); |
| 415 |
return; |
| 416 |
} |
| 417 |
} |
| 418 |
// same for minSdkVersion, if the library is using a code name, the application must |
| 419 |
// also be using the same code name. |
| 420 |
String libraryMinSdkVersion = lowerPriorityDocument.getRawMinSdkVersion(); |
| 421 |
if (!Character.isDigit(libraryMinSdkVersion.charAt(0))) { |
| 422 |
// this is a code name, ensure this document uses the same code name. |
| 423 |
if (!libraryMinSdkVersion.equals(getMinSdkVersion())) { |
| 424 |
mergingReport.addMessage(getSourceFile(), MergingReport.Record.Severity.ERROR, |
| 425 |
String.format( |
| 426 |
"uses-sdk:minSdkVersion %1$s cannot be different than version " |
| 427 |
+ "%2$s declared in library %3$s", |
| 428 |
getMinSdkVersion(), |
| 429 |
libraryMinSdkVersion, |
| 430 |
lowerPriorityDocument.getSourceFile().print(false) |
| 431 |
) |
| 432 |
); |
| 433 |
return; |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
if (!checkUsesSdkMinVersion(lowerPriorityDocument, mergingReport)) { |
| 438 |
String error = String.format( |
| 439 |
"uses-sdk:minSdkVersion %1$s cannot be smaller than version " |
| 440 |
+ "%2$s declared in library %3$s\n" |
| 441 |
+ "\tSuggestion: use tools:overrideLibrary=\"%4$s\" to force usage", |
| 442 |
getMinSdkVersion(), |
| 443 |
lowerPriorityDocument.getRawMinSdkVersion(), |
| 444 |
lowerPriorityDocument.getSourceFile().print(false), |
| 445 |
lowerPriorityDocument.getPackageName()); |
| 446 |
if (usesSdk.isPresent()) { |
| 447 |
mergingReport.addMessage( |
| 448 |
new SourceFilePosition(getSourceFile(), usesSdk.get().getPosition()), |
| 449 |
MergingReport.Record.Severity.ERROR, |
| 450 |
error); |
| 451 |
} else { |
| 452 |
mergingReport.addMessage( |
| 453 |
getSourceFile(), MergingReport.Record.Severity.ERROR, error); |
| 454 |
} |
| 455 |
return; |
| 456 |
} |
| 457 |
|
| 458 |
// if the merged document target SDK is equal or smaller than the library's, nothing to do. |
| 459 |
if (thisTargetSdk <= libraryTargetSdk) { |
| 460 |
return; |
| 461 |
} |
| 462 |
|
| 463 |
// There is no need to add any implied permissions when targeting an old runtime. |
| 464 |
if (thisTargetSdk < 4) { |
| 465 |
return; |
| 466 |
} |
| 467 |
|
| 468 |
boolean hasWriteToExternalStoragePermission = |
| 469 |
lowerPriorityDocument.getByTypeAndKey( |
| 470 |
USES_PERMISSION, permission("WRITE_EXTERNAL_STORAGE")).isPresent(); |
| 471 |
|
| 472 |
if (libraryTargetSdk < 4) { |
| 473 |
addIfAbsent(mergingReport.getActionRecorder(), |
| 474 |
USES_PERMISSION, |
| 475 |
permission("WRITE_EXTERNAL_STORAGE"), |
| 476 |
lowerPriorityDocument.getPackageName() + " has a targetSdkVersion < 4"); |
| 477 |
hasWriteToExternalStoragePermission = true; |
| 478 |
|
| 479 |
addIfAbsent(mergingReport.getActionRecorder(), |
| 480 |
USES_PERMISSION, |
| 481 |
permission("READ_PHONE_STATE"), |
| 482 |
lowerPriorityDocument.getPackageName() + " has a targetSdkVersion < 4"); |
| 483 |
} |
| 484 |
|
| 485 |
// If the application has requested WRITE_EXTERNAL_STORAGE, we will |
| 486 |
// force them to always take READ_EXTERNAL_STORAGE as well. We always |
| 487 |
// do this (regardless of target API version) because we can't have |
| 488 |
// an app with write permission but not read permission. |
| 489 |
if (hasWriteToExternalStoragePermission) { |
| 490 |
|
| 491 |
addIfAbsent(mergingReport.getActionRecorder(), |
| 492 |
USES_PERMISSION, |
| 493 |
permission("READ_EXTERNAL_STORAGE"), |
| 494 |
lowerPriorityDocument.getPackageName() + " requested WRITE_EXTERNAL_STORAGE"); |
| 495 |
} |
| 496 |
|
| 497 |
// Pre-JellyBean call log permission compatibility. |
| 498 |
if (thisTargetSdk >= 16 && libraryTargetSdk < 16) { |
| 499 |
if (lowerPriorityDocument.getByTypeAndKey( |
| 500 |
USES_PERMISSION, permission("READ_CONTACTS")).isPresent()) { |
| 501 |
addIfAbsent(mergingReport.getActionRecorder(), |
| 502 |
USES_PERMISSION, permission("READ_CALL_LOG"), |
| 503 |
lowerPriorityDocument.getPackageName() |
| 504 |
+ " has targetSdkVersion < 16 and requested READ_CONTACTS"); |
| 505 |
} |
| 506 |
if (lowerPriorityDocument.getByTypeAndKey( |
| 507 |
USES_PERMISSION, permission("WRITE_CONTACTS")).isPresent()) { |
| 508 |
addIfAbsent(mergingReport.getActionRecorder(), |
| 509 |
USES_PERMISSION, permission("WRITE_CALL_LOG"), |
| 510 |
lowerPriorityDocument.getPackageName() |
| 511 |
+ " has targetSdkVersion < 16 and requested WRITE_CONTACTS"); |
| 512 |
} |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
/** |
| 517 |
* Returns true if the minSdkVersion of the application and the library are compatible, false |
| 518 |
* otherwise. |
| 519 |
*/ |
| 520 |
private boolean checkUsesSdkMinVersion(@NonNull XmlDocument lowerPriorityDocument, |
| 521 |
MergingReport.Builder mergingReport) { |
| 522 |
|
| 523 |
int thisMinSdk = getApiLevelFromAttribute(getMinSdkVersion()); |
| 524 |
int libraryMinSdk = getApiLevelFromAttribute( |
| 525 |
lowerPriorityDocument.getRawMinSdkVersion()); |
| 526 |
|
| 527 |
// the merged document minSdk cannot be lower than a library |
| 528 |
if (thisMinSdk < libraryMinSdk) { |
| 529 |
|
| 530 |
// check if this higher priority document has any tools instructions for the node |
| 531 |
Optional<XmlElement> xmlElementOptional = getByTypeAndKey(USES_SDK, null); |
| 532 |
if (!xmlElementOptional.isPresent()) { |
| 533 |
return false; |
| 534 |
} |
| 535 |
XmlElement xmlElement = xmlElementOptional.get(); |
| 536 |
|
| 537 |
// if we find a selector that applies to this library. the users wants to explicitly |
| 538 |
// allow this higher version library to be allowed. |
| 539 |
for (Selector selector : xmlElement.getOverrideUsesSdkLibrarySelectors()) { |
| 540 |
if (selector.appliesTo(lowerPriorityDocument.getRootNode())) { |
| 541 |
return true; |
| 542 |
} |
| 543 |
} |
| 544 |
return false; |
| 545 |
} |
| 546 |
return true; |
| 547 |
} |
| 548 |
|
| 549 |
/** |
| 550 |
* Adds a new element of type nodeType with a specific keyValue if the element is absent in this |
| 551 |
* document. Will also add attributes expressed through key value pairs. |
| 552 |
* |
| 553 |
* @param actionRecorder to records creation actions. |
| 554 |
* @param nodeType the node type to crete |
| 555 |
* @param keyValue the optional key for the element. |
| 556 |
* @param attributes the optional array of key value pairs for extra element attribute. |
| 557 |
* @return the Xml element whether it was created or existed or {@link Optional#absent()} if |
| 558 |
* it does not exist in this document. |
| 559 |
*/ |
| 560 |
private Optional<Element> addIfAbsent( |
| 561 |
@NonNull ActionRecorder actionRecorder, |
| 562 |
@NonNull ManifestModel.NodeTypes nodeType, |
| 563 |
@Nullable String keyValue, |
| 564 |
@Nullable String reason, |
| 565 |
@Nullable Pair<String, String>... attributes) { |
| 566 |
|
| 567 |
Optional<XmlElement> xmlElementOptional = getByTypeAndKey(nodeType, keyValue); |
| 568 |
if (xmlElementOptional.isPresent()) { |
| 569 |
return Optional.absent(); |
| 570 |
} |
| 571 |
Element elementNS = getXml() |
| 572 |
.createElementNS(SdkConstants.ANDROID_URI, "android:" + nodeType.toXmlName()); |
| 573 |
|
| 574 |
|
| 575 |
ImmutableList<String> keyAttributesNames = nodeType.getNodeKeyResolver() |
| 576 |
.getKeyAttributesNames(); |
| 577 |
if (keyAttributesNames.size() == 1) { |
| 578 |
elementNS.setAttributeNS( |
| 579 |
SdkConstants.ANDROID_URI, "android:" + keyAttributesNames.get(0), keyValue); |
| 580 |
} |
| 581 |
if (attributes != null) { |
| 582 |
for (Pair<String, String> attribute : attributes) { |
| 583 |
elementNS.setAttributeNS( |
| 584 |
SdkConstants.ANDROID_URI, "android:" + attribute.getFirst(), |
| 585 |
attribute.getSecond()); |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
// record creation. |
| 590 |
XmlElement xmlElement = new XmlElement(elementNS, this); |
| 591 |
actionRecorder.recordImpliedNodeAction(xmlElement, reason); |
| 592 |
|
| 593 |
getRootNode().getXml().appendChild(elementNS); |
| 594 |
return Optional.of(elementNS); |
| 595 |
} |
| 596 |
|
| 597 |
@NonNull |
| 598 |
private static String permission(String permissionName) { |
| 599 |
return "android.permission." + permissionName; |
| 600 |
} |
| 601 |
} |