| 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.SdkConstants.ANDROID_URI; |
| 20 |
import static com.android.SdkConstants.ATTR_NAME; |
| 21 |
import static com.android.manifmerger.AttributeModel.Hexadecimal32BitsWithMinimumValue; |
| 22 |
import static com.android.manifmerger.AttributeModel.MultiValueValidator; |
| 23 |
|
| 24 |
import com.android.SdkConstants; |
| 25 |
import com.android.annotations.NonNull; |
| 26 |
import com.android.annotations.Nullable; |
| 27 |
import com.android.annotations.concurrency.Immutable; |
| 28 |
import com.android.utils.SdkUtils; |
| 29 |
import com.android.xml.AndroidManifest; |
| 30 |
import com.google.common.base.Joiner; |
| 31 |
import com.google.common.base.Preconditions; |
| 32 |
import com.google.common.base.Strings; |
| 33 |
import com.google.common.collect.ImmutableList; |
| 34 |
|
| 35 |
import org.w3c.dom.Attr; |
| 36 |
import org.w3c.dom.Element; |
| 37 |
import org.w3c.dom.Node; |
| 38 |
import org.w3c.dom.NodeList; |
| 39 |
|
| 40 |
import java.util.ArrayList; |
| 41 |
import java.util.Collections; |
| 42 |
import java.util.List; |
| 43 |
|
| 44 |
/** |
| 45 |
* Model for the manifest file merging activities. |
| 46 |
* <p> |
| 47 |
* |
| 48 |
* This model will describe each element that is eligible for merging and associated merging |
| 49 |
* policies. It is not reusable as most of its interfaces are private but a future enhancement |
| 50 |
* could easily make this more generic/reusable if we need to merge more than manifest files. |
| 51 |
* |
| 52 |
*/ |
| 53 |
@Immutable |
| 54 |
class ManifestModel { |
| 55 |
|
| 56 |
/** |
| 57 |
* Interface responsible for providing a key extraction capability from a xml element. |
| 58 |
* Some elements store their keys as an attribute, some as a sub-element attribute, some don't |
| 59 |
* have any key. |
| 60 |
*/ |
| 61 |
@Immutable |
| 62 |
interface NodeKeyResolver { |
| 63 |
|
| 64 |
/** |
| 65 |
* Returns the key associated with this xml element. |
| 66 |
* @param xmlElement the xml element to get the key from |
| 67 |
* @return the key as a string to uniquely identify xmlElement from similarly typed elements |
| 68 |
* in the xml document or null if there is no key. |
| 69 |
*/ |
| 70 |
@Nullable String getKey(Element xmlElement); |
| 71 |
|
| 72 |
/** |
| 73 |
* Returns the attribute(s) used to store the xml element key. |
| 74 |
* @return the key attribute(s) name(s) or null of this element does not have a key. |
| 75 |
*/ |
| 76 |
@NonNull |
| 77 |
ImmutableList<String> getKeyAttributesNames(); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Implementation of {@link com.android.manifmerger.ManifestModel.NodeKeyResolver} that do not |
| 82 |
* provide any key (the element has to be unique in the xml document). |
| 83 |
*/ |
| 84 |
private static class NoKeyNodeResolver implements NodeKeyResolver { |
| 85 |
|
| 86 |
@Override |
| 87 |
@Nullable |
| 88 |
public String getKey(Element xmlElement) { |
| 89 |
return null; |
| 90 |
} |
| 91 |
|
| 92 |
@NonNull |
| 93 |
@Override |
| 94 |
public ImmutableList<String> getKeyAttributesNames() { |
| 95 |
return ImmutableList.of(); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Implementation of {@link com.android.manifmerger.ManifestModel.NodeKeyResolver} that uses an |
| 101 |
* attribute to resolve the key value. |
| 102 |
*/ |
| 103 |
private static class AttributeBasedNodeKeyResolver implements NodeKeyResolver { |
| 104 |
|
| 105 |
@Nullable private final String mNamespaceUri; |
| 106 |
private final String mAttributeName; |
| 107 |
|
| 108 |
/** |
| 109 |
* Build a new instance capable of resolving an xml element key from the passed attribute |
| 110 |
* namespace and local name. |
| 111 |
* @param namespaceUri optional namespace for the attribute name. |
| 112 |
* @param attributeName attribute name |
| 113 |
*/ |
| 114 |
private AttributeBasedNodeKeyResolver(@Nullable String namespaceUri, |
| 115 |
@NonNull String attributeName) { |
| 116 |
this.mNamespaceUri = namespaceUri; |
| 117 |
this.mAttributeName = Preconditions.checkNotNull(attributeName); |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
@Nullable |
| 122 |
public String getKey(@NonNull Element xmlElement) { |
| 123 |
String key = mNamespaceUri == null |
| 124 |
? xmlElement.getAttribute(mAttributeName) |
| 125 |
: xmlElement.getAttributeNS(mNamespaceUri, mAttributeName); |
| 126 |
if (Strings.isNullOrEmpty(key)) return null; |
| 127 |
return key; |
| 128 |
} |
| 129 |
|
| 130 |
@NonNull |
| 131 |
@Override |
| 132 |
public ImmutableList<String> getKeyAttributesNames() { |
| 133 |
return ImmutableList.of(mAttributeName); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Subclass of {@link com.android.manifmerger.ManifestModel.AttributeBasedNodeKeyResolver} that |
| 139 |
* uses "android:name" as the attribute. |
| 140 |
*/ |
| 141 |
private static final NodeKeyResolver DEFAULT_NAME_ATTRIBUTE_RESOLVER = |
| 142 |
new AttributeBasedNodeKeyResolver(ANDROID_URI, SdkConstants.ATTR_NAME); |
| 143 |
|
| 144 |
private static final NoKeyNodeResolver DEFAULT_NO_KEY_NODE_RESOLVER = new NoKeyNodeResolver(); |
| 145 |
|
| 146 |
/** |
| 147 |
* A {@link com.android.manifmerger.ManifestModel.NodeKeyResolver} capable of extracting the |
| 148 |
* element key first in an "android:name" attribute and if not value found there, in the |
| 149 |
* "android:glEsVersion" attribute. |
| 150 |
*/ |
| 151 |
@Nullable |
| 152 |
private static final NodeKeyResolver NAME_AND_GLESVERSION_KEY_RESOLVER = new NodeKeyResolver() { |
| 153 |
private final NodeKeyResolver nameAttrResolver = DEFAULT_NAME_ATTRIBUTE_RESOLVER; |
| 154 |
private final NodeKeyResolver glEsVersionResolver = |
| 155 |
new AttributeBasedNodeKeyResolver(ANDROID_URI, |
| 156 |
AndroidManifest.ATTRIBUTE_GLESVERSION); |
| 157 |
|
| 158 |
@Nullable |
| 159 |
@Override |
| 160 |
public String getKey(Element xmlElement) { |
| 161 |
@Nullable String key = nameAttrResolver.getKey(xmlElement); |
| 162 |
return Strings.isNullOrEmpty(key) |
| 163 |
? glEsVersionResolver.getKey(xmlElement) |
| 164 |
: key; |
| 165 |
} |
| 166 |
|
| 167 |
@NonNull |
| 168 |
@Override |
| 169 |
public ImmutableList<String> getKeyAttributesNames() { |
| 170 |
return ImmutableList.of(SdkConstants.ATTR_NAME, AndroidManifest.ATTRIBUTE_GLESVERSION); |
| 171 |
} |
| 172 |
}; |
| 173 |
|
| 174 |
/** |
| 175 |
* Specific {@link com.android.manifmerger.ManifestModel.NodeKeyResolver} for intent-filter |
| 176 |
* elements. |
| 177 |
* Intent filters do not have a proper key, therefore their identity is really carried by |
| 178 |
* the presence of the action and category sub-elements. |
| 179 |
* We concatenate such elements sub-keys (after sorting them to work around declaration order) |
| 180 |
* and use that for the intent-filter unique key. |
| 181 |
*/ |
| 182 |
@Nullable |
| 183 |
private static final NodeKeyResolver INTENT_FILTER_KEY_RESOLVER = new NodeKeyResolver() { |
| 184 |
@Nullable |
| 185 |
@Override |
| 186 |
public String getKey(@NonNull Element element) { |
| 187 |
@NonNull OrphanXmlElement xmlElement = new OrphanXmlElement(element); |
| 188 |
assert(xmlElement.getType() == NodeTypes.INTENT_FILTER); |
| 189 |
// concatenate all actions and categories attribute names. |
| 190 |
@NonNull List<String> allSubElementKeys = new ArrayList<String>(); |
| 191 |
NodeList childNodes = element.getChildNodes(); |
| 192 |
for (int i = 0; i < childNodes.getLength(); i++) { |
| 193 |
Node child = childNodes.item(i); |
| 194 |
if (child.getNodeType() != Node.ELEMENT_NODE) continue; |
| 195 |
@NonNull OrphanXmlElement subElement = new OrphanXmlElement((Element) child); |
| 196 |
if (subElement.getType() == NodeTypes.ACTION |
| 197 |
|| subElement.getType() == NodeTypes.CATEGORY) { |
| 198 |
Attr nameAttribute = subElement.getXml() |
| 199 |
.getAttributeNodeNS(ANDROID_URI, ATTR_NAME); |
| 200 |
if (nameAttribute != null) { |
| 201 |
allSubElementKeys.add(nameAttribute.getValue()); |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
Collections.sort(allSubElementKeys); |
| 206 |
return Joiner.on('+').join(allSubElementKeys); |
| 207 |
} |
| 208 |
|
| 209 |
@NonNull |
| 210 |
@Override |
| 211 |
public ImmutableList<String> getKeyAttributesNames() { |
| 212 |
return ImmutableList.of("action#name", "category#name"); |
| 213 |
} |
| 214 |
}; |
| 215 |
|
| 216 |
/** |
| 217 |
* Implementation of {@link com.android.manifmerger.ManifestModel.NodeKeyResolver} that |
| 218 |
* combined two attributes values to create the key value. |
| 219 |
*/ |
| 220 |
private static final class TwoAttributesBasedKeyResolver implements NodeKeyResolver { |
| 221 |
private final NodeKeyResolver firstAttributeKeyResolver; |
| 222 |
private final NodeKeyResolver secondAttributeKeyResolver; |
| 223 |
|
| 224 |
private TwoAttributesBasedKeyResolver(NodeKeyResolver firstAttributeKeyResolver, |
| 225 |
NodeKeyResolver secondAttributeKeyResolver) { |
| 226 |
this.firstAttributeKeyResolver = firstAttributeKeyResolver; |
| 227 |
this.secondAttributeKeyResolver = secondAttributeKeyResolver; |
| 228 |
} |
| 229 |
|
| 230 |
@Nullable |
| 231 |
@Override |
| 232 |
public String getKey(Element xmlElement) { |
| 233 |
@Nullable String firstKey = firstAttributeKeyResolver.getKey(xmlElement); |
| 234 |
@Nullable String secondKey = secondAttributeKeyResolver.getKey(xmlElement); |
| 235 |
|
| 236 |
return Strings.isNullOrEmpty(firstKey) |
| 237 |
? secondKey |
| 238 |
: Strings.isNullOrEmpty(secondKey) |
| 239 |
? firstKey |
| 240 |
: firstKey + "+" + secondKey; |
| 241 |
} |
| 242 |
|
| 243 |
@NonNull |
| 244 |
@Override |
| 245 |
public ImmutableList<String> getKeyAttributesNames() { |
| 246 |
return ImmutableList.of(firstAttributeKeyResolver.getKeyAttributesNames().get(0), |
| 247 |
secondAttributeKeyResolver.getKeyAttributesNames().get(0)); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
private static final AttributeModel.BooleanValidator BOOLEAN_VALIDATOR = |
| 252 |
new AttributeModel.BooleanValidator(); |
| 253 |
|
| 254 |
private static final boolean MULTIPLE_DECLARATION_FOR_SAME_KEY_ALLOWED = true; |
| 255 |
|
| 256 |
/** |
| 257 |
* Definitions of the support node types in the Android Manifest file. |
| 258 |
* {@link <a href=http://developer.android.com/guide/topics/manifest/manifest-intro.html/>} |
| 259 |
* for more details about the xml format. |
| 260 |
* |
| 261 |
* There is no DTD or schema associated with the file type so this is best effort in providing |
| 262 |
* some metadata on the elements of the Android's xml file. |
| 263 |
* |
| 264 |
* Each xml element is defined as an enum value and for each node, extra metadata is added |
| 265 |
* <ul> |
| 266 |
* <li>{@link com.android.manifmerger.MergeType} to identify how the merging engine |
| 267 |
* should process this element.</li> |
| 268 |
* <li>{@link com.android.manifmerger.ManifestModel.NodeKeyResolver} to resolve the |
| 269 |
* element's key. Elements can have an attribute like "android:name", others can use |
| 270 |
* a sub-element, and finally some do not have a key and are meant to be unique.</li> |
| 271 |
* <li>List of attributes models with special behaviors : |
| 272 |
* <ul> |
| 273 |
* <li>Smart substitution of class names to fully qualified class names using the |
| 274 |
* document's package declaration. The list's size can be 0..n</li> |
| 275 |
* <li>Implicit default value when no defined on the xml element.</li> |
| 276 |
* <li>{@link AttributeModel.Validator} to validate attribute value against.</li> |
| 277 |
* </ul> |
| 278 |
* </ul> |
| 279 |
* |
| 280 |
* It is of the outermost importance to keep this model correct as it is used by the merging |
| 281 |
* engine to make all its decisions. There should not be special casing in the engine, all |
| 282 |
* decisions must be represented here. |
| 283 |
* |
| 284 |
* If you find yourself needing to extend the model to support future requirements, do it here |
| 285 |
* and modify the engine to make proper decision based on the added metadata. |
| 286 |
*/ |
| 287 |
enum NodeTypes { |
| 288 |
|
| 289 |
/** |
| 290 |
* Action (contained in intent-filter) |
| 291 |
* <br> |
| 292 |
* <b>See also : </b> |
| 293 |
* {@link <a href=http://developer.android.com/guide/topics/manifest/action-element.html> |
| 294 |
* Action Xml documentation</a>} |
| 295 |
*/ |
| 296 |
ACTION(MergeType.MERGE, DEFAULT_NAME_ATTRIBUTE_RESOLVER), |
| 297 |
|
| 298 |
/** |
| 299 |
* Activity (contained in application) |
| 300 |
* <br> |
| 301 |
* <b>See also : </b> |
| 302 |
* {@link <a href=http://developer.android.com/guide/topics/manifest/activity-element.html> |
| 303 |
* Activity Xml documentation</a>} |
| 304 |
*/ |
| 305 |
ACTIVITY(MergeType.MERGE, DEFAULT_NAME_ATTRIBUTE_RESOLVER, |
| 306 |
AttributeModel.newModel("parentActivityName").setIsPackageDependent(), |
| 307 |
AttributeModel.newModel(SdkConstants.ATTR_NAME).setIsPackageDependent()), |
| 308 |
|
| 309 |
/** |
| 310 |
* Activity-alias (contained in application) |
| 311 |
* <br> |
| 312 |
* <b>See also : </b> |
| 313 |
* {@link <a href=http://developer.android.com/guide/topics/manifest/activity-alias-element.html> |
| 314 |
* Activity-alias Xml documentation</a>} |
| 315 |
*/ |
| 316 |
ACTIVITY_ALIAS(MergeType.MERGE, DEFAULT_NAME_ATTRIBUTE_RESOLVER, |
| 317 |
AttributeModel.newModel("targetActivity").setIsPackageDependent(), |
| 318 |
AttributeModel.newModel(SdkConstants.ATTR_NAME).setIsPackageDependent()), |
| 319 |
|
| 320 |
/** |
| 321 |
* Application (contained in manifest) |
| 322 |
* <br> |
| 323 |
* <b>See also : </b> |
| 324 |
* {@link <a href=http://developer.android.com/guide/topics/manifest/application-element.html> |
| 325 |
* Application Xml documentation</a>} |
| 326 |
*/ |
| 327 |
APPLICATION(MergeType.MERGE, DEFAULT_NO_KEY_NODE_RESOLVER, |
| 328 |
AttributeModel.newModel("backupAgent").setIsPackageDependent(), |
| 329 |
AttributeModel.newModel(SdkConstants.ATTR_NAME).setIsPackageDependent()), |
| 330 |
|
| 331 |
/** |
| 332 |
* Category (contained in intent-filter) |
| 333 |
* <br> |
| 334 |
* <b>See also : </b> |
| 335 |
* {@link <a href=http://developer.android.com/guide/topics/manifest/category-element.html> |
| 336 |
* Category Xml documentation</a>} |
| 337 |
*/ |
| 338 |
CATEGORY(MergeType.MERGE, DEFAULT_NAME_ATTRIBUTE_RESOLVER), |
| 339 |
|
| 340 |
/** |
| 341 |
* Compatible-screens (contained in manifest) |
| 342 |
* <br> |
| 343 |
* <b>See also : </b> |
| 344 |
* {@link <a href=http://developer.android.com/guide/topics/manifest/compatible-screens-element.html> |
| 345 |
* Category Xml documentation</a>} |
| 346 |
*/ |
| 347 |
COMPATIBLE_SCREENS(MergeType.MERGE, DEFAULT_NO_KEY_NODE_RESOLVER), |
| 348 |
|
| 349 |
/** |
| 350 |
* Data (contained in intent-filter) |
| 351 |
* <br> |
| 352 |
* <b>See also : </b> |
| 353 |
* {@link <a href=http://developer.android.com/guide/topics/manifest/data-element.html> |
| 354 |
* Category Xml documentation</a>} |
| 355 |
*/ |
| 356 |
DATA(MergeType.MERGE, DEFAULT_NO_KEY_NODE_RESOLVER), |
| 357 |
|
| 358 |
/** |
| 359 |
* Grant-uri-permission (contained in intent-filter) |
| 360 |
* <br> |
| 361 |
* <b>See also : </b> |
| 362 |
* {@link <a href=http://developer.android.com/guide/topics/manifest/grant-uri-permission-element.html> |
| 363 |
* Category Xml documentation</a>} |
| 364 |
*/ |
| 365 |
GRANT_URI_PERMISSION(MergeType.MERGE, DEFAULT_NO_KEY_NODE_RESOLVER), |
| 366 |
|
| 367 |
/** |
| 368 |
* Instrumentation (contained in intent-filter) |
| 369 |
* <br> |
| 370 |
* <b>See also : </b> |
| 371 |
* {@link <a href=http://developer.android.com/guide/topics/manifest/instrumentation-element.html> |
| 372 |
* Instrunentation Xml documentation</a>} |
| 373 |
*/ |
| 374 |
INSTRUMENTATION( |
| 375 |
MergeType.MERGE, DEFAULT_NO_KEY_NODE_RESOLVER, |
| 376 |
AttributeModel.newModel("name").setMergingPolicy(AttributeModel.NO_MERGING_POLICY), |
| 377 |
AttributeModel.newModel("targetPackage") |
| 378 |
.setMergingPolicy(AttributeModel.NO_MERGING_POLICY), |
| 379 |
AttributeModel.newModel("functionalTest") |
| 380 |
.setMergingPolicy(AttributeModel.NO_MERGING_POLICY), |
| 381 |
AttributeModel.newModel("handleProfiling") |
| 382 |
.setMergingPolicy(AttributeModel.NO_MERGING_POLICY), |
| 383 |
AttributeModel.newModel("label").setMergingPolicy(AttributeModel.NO_MERGING_POLICY) |
| 384 |
), |
| 385 |
|
| 386 |
/** |
| 387 |
* Intent-filter (contained in activity, activity-alias, service, receiver) |
| 388 |
* <br> |
| 389 |
* <b>See also : </b> |
| 390 |
* {@link <a href=http://developer.android.com/guide/topics/manifest/intent-filter-element.html> |
| 391 |
* Intent-filter Xml documentation</a>} |
| 392 |
*/ |
| 393 |
INTENT_FILTER(MergeType.ALWAYS, INTENT_FILTER_KEY_RESOLVER, |
| 394 |
MULTIPLE_DECLARATION_FOR_SAME_KEY_ALLOWED), |
| 395 |
|
| 396 |
/** |
| 397 |
* Manifest (top level node) |
| 398 |
* <br> |
| 399 |
* <b>See also : </b> |
| 400 |
* {@link <a href=http://developer.android.com/guide/topics/manifest/manifest-element.html> |
| 401 |
* Manifest Xml documentation</a>} |
| 402 |
*/ |
| 403 |
MANIFEST(MergeType.MERGE_CHILDREN_ONLY, DEFAULT_NO_KEY_NODE_RESOLVER), |
| 404 |
|
| 405 |
/** |
| 406 |
* Meta-data (contained in activity, activity-alias, application, provider, receiver) |
| 407 |
* <br> |
| 408 |
* <b>See also : </b> |
| 409 |
* {@link <a href=http://developer.android.com/guide/topics/manifest/meta-data-element.html> |
| 410 |
* Meta-data Xml documentation</a>} |
| 411 |
*/ |
| 412 |
META_DATA(MergeType.MERGE, DEFAULT_NAME_ATTRIBUTE_RESOLVER), |
| 413 |
|
| 414 |
/** |
| 415 |
* Path-permission (contained in provider) |
| 416 |
* <br> |
| 417 |
* <b>See also : </b> |
| 418 |
* {@link <a href=http://developer.android.com/guide/topics/manifest/path-permission-element.html> |
| 419 |
* Meta-data Xml documentation</a>} |
| 420 |
*/ |
| 421 |
PATH_PERMISSION(MergeType.MERGE, DEFAULT_NO_KEY_NODE_RESOLVER), |
| 422 |
|
| 423 |
/** |
| 424 |
* Permission-group (contained in manifest). |
| 425 |
* <br> |
| 426 |
* <b>See also : </b> |
| 427 |
* {@link <a href=http://developer.android.com/guide/topics/manifest/permission-group-element.html> |
| 428 |
* Permission-group Xml documentation</a>} |
| 429 |
* |
| 430 |
*/ |
| 431 |
PERMISSION_GROUP(MergeType.MERGE, DEFAULT_NAME_ATTRIBUTE_RESOLVER, |
| 432 |
AttributeModel.newModel(SdkConstants.ATTR_NAME)), |
| 433 |
|
| 434 |
/** |
| 435 |
* Permission (contained in manifest). |
| 436 |
* <br> |
| 437 |
* <b>See also : </b> |
| 438 |
* {@link <a href=http://developer.android.com/guide/topics/manifest/permission-element.html> |
| 439 |
* Permission Xml documentation</a>} |
| 440 |
* |
| 441 |
*/ |
| 442 |
PERMISSION(MergeType.MERGE, DEFAULT_NAME_ATTRIBUTE_RESOLVER, |
| 443 |
AttributeModel.newModel(SdkConstants.ATTR_NAME), |
| 444 |
AttributeModel.newModel("protectionLevel") |
| 445 |
.setDefaultValue("normal") |
| 446 |
// TODO : this will need to be populated from |
| 447 |
// sdk/platforms/android-19/data/res/values.attrs_manifest.xml |
| 448 |
.setOnReadValidator(new MultiValueValidator( |
| 449 |
"normal", "dangerous", "signature", "signatureOrSystem"))), |
| 450 |
|
| 451 |
/** |
| 452 |
* Permission-tree (contained in manifest). |
| 453 |
* <br> |
| 454 |
* <b>See also : </b> |
| 455 |
* {@link <a href=http://developer.android.com/guide/topics/manifest/permission-tree-element.html> |
| 456 |
* Permission-tree Xml documentation</a>} |
| 457 |
* |
| 458 |
*/ |
| 459 |
PERMISSION_TREE(MergeType.MERGE, DEFAULT_NAME_ATTRIBUTE_RESOLVER, |
| 460 |
AttributeModel.newModel(SdkConstants.ATTR_NAME)), |
| 461 |
|
| 462 |
/** |
| 463 |
* Provider (contained in application) |
| 464 |
* <br> |
| 465 |
* <b>See also : </b> |
| 466 |
* {@link <a href=http://developer.android.com/guide/topics/manifest/provider-element.html> |
| 467 |
* Provider Xml documentation</a>} |
| 468 |
*/ |
| 469 |
PROVIDER(MergeType.MERGE, DEFAULT_NAME_ATTRIBUTE_RESOLVER, |
| 470 |
AttributeModel.newModel(SdkConstants.ATTR_NAME) |
| 471 |
.setIsPackageDependent()), |
| 472 |
|
| 473 |
/** |
| 474 |
* Receiver (contained in application) |
| 475 |
* <br> |
| 476 |
* <b>See also : </b> |
| 477 |
* {@link <a href=http://developer.android.com/guide/topics/manifest/receiver-element.html> |
| 478 |
* Receiver Xml documentation</a>} |
| 479 |
*/ |
| 480 |
RECEIVER(MergeType.MERGE, DEFAULT_NAME_ATTRIBUTE_RESOLVER, |
| 481 |
AttributeModel.newModel(SdkConstants.ATTR_NAME).setIsPackageDependent()), |
| 482 |
|
| 483 |
/** |
| 484 |
* Screen (contained in compatible-screens) |
| 485 |
* <br> |
| 486 |
* <b>See also : </b> |
| 487 |
* {@link <a href=http://developer.android.com/guide/topics/manifest/compatible-screens-element.html> |
| 488 |
* Receiver Xml documentation</a>} |
| 489 |
*/ |
| 490 |
SCREEN(MergeType.MERGE, new TwoAttributesBasedKeyResolver( |
| 491 |
new AttributeBasedNodeKeyResolver(ANDROID_URI, "screenSize"), |
| 492 |
new AttributeBasedNodeKeyResolver(ANDROID_URI, "screenDensity"))), |
| 493 |
|
| 494 |
/** |
| 495 |
* Service (contained in application) |
| 496 |
* <br> |
| 497 |
* <b>See also : </b> |
| 498 |
* {@link <a href=http://developer.android.com/guide/topics/manifest/application-element.html> |
| 499 |
* Service Xml documentation</a>} |
| 500 |
*/ |
| 501 |
SERVICE(MergeType.MERGE, DEFAULT_NAME_ATTRIBUTE_RESOLVER, |
| 502 |
AttributeModel.newModel(SdkConstants.ATTR_NAME).setIsPackageDependent()), |
| 503 |
|
| 504 |
/** |
| 505 |
* Supports-gl-texture (contained in manifest) |
| 506 |
* <br> |
| 507 |
* <b>See also : </b> |
| 508 |
* {@link <a href=http://developer.android.com/guide/topics/manifest/supports-gl-texture-element.html> |
| 509 |
* Support-screens Xml documentation</a>} |
| 510 |
*/ |
| 511 |
SUPPORTS_GL_TEXTURE(MergeType.MERGE, DEFAULT_NAME_ATTRIBUTE_RESOLVER), |
| 512 |
|
| 513 |
/** |
| 514 |
* Support-screens (contained in manifest) |
| 515 |
* <br> |
| 516 |
* <b>See also : </b> |
| 517 |
* {@link <a href=http://developer.android.com/guide/topics/manifest/supports-screens-element.html> |
| 518 |
* Support-screens Xml documentation</a>} |
| 519 |
*/ |
| 520 |
SUPPORTS_SCREENS(MergeType.MERGE, DEFAULT_NO_KEY_NODE_RESOLVER), |
| 521 |
|
| 522 |
/** |
| 523 |
* Uses-configuration (contained in manifest) |
| 524 |
* <br> |
| 525 |
* <b>See also : </b> |
| 526 |
* {@link <a href=http://developer.android.com/guide/topics/manifest/uses-configuration-element.html> |
| 527 |
* Support-screens Xml documentation</a>} |
| 528 |
*/ |
| 529 |
USES_CONFIGURATION(MergeType.MERGE, DEFAULT_NO_KEY_NODE_RESOLVER), |
| 530 |
|
| 531 |
/** |
| 532 |
* Uses-feature (contained in manifest) |
| 533 |
* <br> |
| 534 |
* <b>See also : </b> |
| 535 |
* {@link <a href=http://developer.android.com/guide/topics/manifest/uses-feature-element.html> |
| 536 |
* Uses-feature Xml documentation</a>} |
| 537 |
*/ |
| 538 |
USES_FEATURE(MergeType.MERGE, NAME_AND_GLESVERSION_KEY_RESOLVER, |
| 539 |
AttributeModel.newModel(AndroidManifest.ATTRIBUTE_REQUIRED) |
| 540 |
.setDefaultValue(SdkConstants.VALUE_TRUE) |
| 541 |
.setOnReadValidator(BOOLEAN_VALIDATOR) |
| 542 |
.setMergingPolicy(AttributeModel.OR_MERGING_POLICY), |
| 543 |
AttributeModel.newModel(AndroidManifest.ATTRIBUTE_GLESVERSION) |
| 544 |
.setDefaultValue("0x00010000") |
| 545 |
.setOnReadValidator(new Hexadecimal32BitsWithMinimumValue(0x00010000))), |
| 546 |
|
| 547 |
/** |
| 548 |
* Use-library (contained in application) |
| 549 |
* <br> |
| 550 |
* <b>See also : </b> |
| 551 |
* {@link <a href=http://developer.android.com/guide/topics/manifest/uses-library-element.html> |
| 552 |
* Use-library Xml documentation</a>} |
| 553 |
*/ |
| 554 |
USES_LIBRARY(MergeType.MERGE, DEFAULT_NAME_ATTRIBUTE_RESOLVER, |
| 555 |
AttributeModel.newModel(AndroidManifest.ATTRIBUTE_REQUIRED) |
| 556 |
.setDefaultValue(SdkConstants.VALUE_TRUE) |
| 557 |
.setOnReadValidator(BOOLEAN_VALIDATOR) |
| 558 |
.setMergingPolicy(AttributeModel.OR_MERGING_POLICY)), |
| 559 |
|
| 560 |
/** |
| 561 |
* Uses-permission (contained in application) |
| 562 |
* <br> |
| 563 |
* <b>See also : </b> |
| 564 |
* {@link <a href=http://developer.android.com/guide/topics/manifest/uses-permission-element.html> |
| 565 |
* Uses-permission Xml documentation</a>} |
| 566 |
*/ |
| 567 |
USES_PERMISSION(MergeType.MERGE, DEFAULT_NAME_ATTRIBUTE_RESOLVER), |
| 568 |
|
| 569 |
/** |
| 570 |
* Uses-sdk (contained in manifest) |
| 571 |
* <br> |
| 572 |
* <b>See also : </b> |
| 573 |
* {@link <a href=http://developer.android.com/guide/topics/manifest/uses-sdk-element.html> |
| 574 |
* Uses-sdk Xml documentation</a>} |
| 575 |
*/ |
| 576 |
USES_SDK(MergeType.MERGE, DEFAULT_NO_KEY_NODE_RESOLVER, |
| 577 |
AttributeModel.newModel("minSdkVersion") |
| 578 |
.setDefaultValue(SdkConstants.VALUE_1) |
| 579 |
.setMergingPolicy(AttributeModel.NO_MERGING_POLICY), |
| 580 |
AttributeModel.newModel("maxSdkVersion") |
| 581 |
.setMergingPolicy(AttributeModel.NO_MERGING_POLICY), |
| 582 |
// TODO : model target's default value is minSdkVersion value. |
| 583 |
AttributeModel.newModel("targetSdkVersion") |
| 584 |
.setMergingPolicy(AttributeModel.NO_MERGING_POLICY) |
| 585 |
), |
| 586 |
|
| 587 |
/** |
| 588 |
* Custom tag for any application specific element |
| 589 |
*/ |
| 590 |
CUSTOM(MergeType.MERGE, DEFAULT_NO_KEY_NODE_RESOLVER); |
| 591 |
|
| 592 |
|
| 593 |
private final MergeType mMergeType; |
| 594 |
private final NodeKeyResolver mNodeKeyResolver; |
| 595 |
private final ImmutableList<AttributeModel> mAttributeModels; |
| 596 |
private final boolean mMultipleDeclarationAllowed; |
| 597 |
|
| 598 |
NodeTypes( |
| 599 |
@NonNull MergeType mergeType, |
| 600 |
@NonNull NodeKeyResolver nodeKeyResolver, |
| 601 |
@Nullable AttributeModel.Builder... attributeModelBuilders) { |
| 602 |
this(mergeType, nodeKeyResolver, false, attributeModelBuilders); |
| 603 |
} |
| 604 |
|
| 605 |
NodeTypes( |
| 606 |
@NonNull MergeType mergeType, |
| 607 |
@NonNull NodeKeyResolver nodeKeyResolver, |
| 608 |
boolean mutipleDeclarationAllowed, |
| 609 |
@Nullable AttributeModel.Builder... attributeModelBuilders) { |
| 610 |
this.mMergeType = Preconditions.checkNotNull(mergeType); |
| 611 |
this.mNodeKeyResolver = Preconditions.checkNotNull(nodeKeyResolver); |
| 612 |
@NonNull ImmutableList.Builder<AttributeModel> attributeModels = |
| 613 |
new ImmutableList.Builder<AttributeModel>(); |
| 614 |
if (attributeModelBuilders != null) { |
| 615 |
for (AttributeModel.Builder attributeModelBuilder : attributeModelBuilders) { |
| 616 |
attributeModels.add(attributeModelBuilder.build()); |
| 617 |
} |
| 618 |
} |
| 619 |
this.mAttributeModels = attributeModels.build(); |
| 620 |
this.mMultipleDeclarationAllowed = mutipleDeclarationAllowed; |
| 621 |
} |
| 622 |
|
| 623 |
@NonNull |
| 624 |
NodeKeyResolver getNodeKeyResolver() { |
| 625 |
return mNodeKeyResolver; |
| 626 |
} |
| 627 |
|
| 628 |
ImmutableList<AttributeModel> getAttributeModels() { |
| 629 |
return mAttributeModels.asList(); |
| 630 |
} |
| 631 |
|
| 632 |
@Nullable |
| 633 |
AttributeModel getAttributeModel(XmlNode.NodeName attributeName) { |
| 634 |
// mAttributeModels could be replaced with a Map if the number of models grows. |
| 635 |
for (AttributeModel attributeModel : mAttributeModels) { |
| 636 |
if (attributeModel.getName().equals(attributeName)) { |
| 637 |
return attributeModel; |
| 638 |
} |
| 639 |
} |
| 640 |
return null; |
| 641 |
} |
| 642 |
|
| 643 |
/** |
| 644 |
* Returns the Xml name for this node type |
| 645 |
*/ |
| 646 |
String toXmlName() { |
| 647 |
return SdkUtils.constantNameToXmlName(this.name()); |
| 648 |
} |
| 649 |
|
| 650 |
/** |
| 651 |
* Returns the {@link NodeTypes} instance from an xml element name (without namespace |
| 652 |
* decoration). For instance, an xml element |
| 653 |
* <pre> |
| 654 |
* {@code |
| 655 |
* <activity android:name="foo"> |
| 656 |
* ... |
| 657 |
* </activity>} |
| 658 |
* </pre> |
| 659 |
* has a xml simple name of "activity" which will resolve to {@link NodeTypes#ACTIVITY} value. |
| 660 |
* |
| 661 |
* Note : a runtime exception will be generated if no mapping from the simple name to a |
| 662 |
* {@link com.android.manifmerger.ManifestModel.NodeTypes} exists. |
| 663 |
* |
| 664 |
* @param xmlSimpleName the xml (lower-hyphen separated words) simple name. |
| 665 |
* @return the {@link NodeTypes} associated with that element name. |
| 666 |
*/ |
| 667 |
static NodeTypes fromXmlSimpleName(String xmlSimpleName) { |
| 668 |
String constantName = SdkUtils.xmlNameToConstantName(xmlSimpleName); |
| 669 |
|
| 670 |
try { |
| 671 |
return NodeTypes.valueOf(constantName); |
| 672 |
} catch (IllegalArgumentException e) { |
| 673 |
// if this element name is not a known tag, we categorize it as 'custom' which will |
| 674 |
// be simply merged. It will prevent us from catching simple spelling mistakes but |
| 675 |
// extensibility is a must have feature. |
| 676 |
return NodeTypes.CUSTOM; |
| 677 |
} |
| 678 |
} |
| 679 |
|
| 680 |
MergeType getMergeType() { |
| 681 |
return mMergeType; |
| 682 |
} |
| 683 |
|
| 684 |
/** |
| 685 |
* Returns true if multiple declaration for the same type and key are allowed or false if |
| 686 |
* there must be only one declaration of this element for a particular key value. |
| 687 |
*/ |
| 688 |
boolean areMultipleDeclarationAllowed() { |
| 689 |
return mMultipleDeclarationAllowed; |
| 690 |
} |
| 691 |
} |
| 692 |
} |