/* * Copyright (C) 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.manifmerger; import static com.android.SdkConstants.ANDROID_URI; import static com.android.SdkConstants.ATTR_NAME; import static com.android.manifmerger.AttributeModel.Hexadecimal32BitsWithMinimumValue; import static com.android.manifmerger.AttributeModel.MultiValueValidator; import com.android.SdkConstants; import com.android.annotations.NonNull; import com.android.annotations.Nullable; import com.android.annotations.concurrency.Immutable; import com.android.utils.SdkUtils; import com.android.xml.AndroidManifest; import com.google.common.base.Joiner; import com.google.common.base.Preconditions; import com.google.common.base.Strings; import com.google.common.collect.ImmutableList; import org.w3c.dom.Attr; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * Model for the manifest file merging activities. *
*
* This model will describe each element that is eligible for merging and associated merging
* policies. It is not reusable as most of its interfaces are private but a future enhancement
* could easily make this more generic/reusable if we need to merge more than manifest files.
*
*/
@Immutable
class ManifestModel {
/**
* Interface responsible for providing a key extraction capability from a xml element.
* Some elements store their keys as an attribute, some as a sub-element attribute, some don't
* have any key.
*/
@Immutable
interface NodeKeyResolver {
/**
* Returns the key associated with this xml element.
* @param xmlElement the xml element to get the key from
* @return the key as a string to uniquely identify xmlElement from similarly typed elements
* in the xml document or null if there is no key.
*/
@Nullable String getKey(Element xmlElement);
/**
* Returns the attribute(s) used to store the xml element key.
* @return the key attribute(s) name(s) or null of this element does not have a key.
*/
@NonNull
ImmutableList
*
*
* It is of the outermost importance to keep this model correct as it is used by the merging
* engine to make all its decisions. There should not be special casing in the engine, all
* decisions must be represented here.
*
* If you find yourself needing to extend the model to support future requirements, do it here
* and modify the engine to make proper decision based on the added metadata.
*/
enum NodeTypes {
/**
* Action (contained in intent-filter)
*
*
*
* See also :
* {@link
* Action Xml documentation}
*/
ACTION(MergeType.MERGE, DEFAULT_NAME_ATTRIBUTE_RESOLVER),
/**
* Activity (contained in application)
*
* See also :
* {@link
* Activity Xml documentation}
*/
ACTIVITY(MergeType.MERGE, DEFAULT_NAME_ATTRIBUTE_RESOLVER,
AttributeModel.newModel("parentActivityName").setIsPackageDependent(),
AttributeModel.newModel(SdkConstants.ATTR_NAME).setIsPackageDependent()),
/**
* Activity-alias (contained in application)
*
* See also :
* {@link
* Activity-alias Xml documentation}
*/
ACTIVITY_ALIAS(MergeType.MERGE, DEFAULT_NAME_ATTRIBUTE_RESOLVER,
AttributeModel.newModel("targetActivity").setIsPackageDependent(),
AttributeModel.newModel(SdkConstants.ATTR_NAME).setIsPackageDependent()),
/**
* Application (contained in manifest)
*
* See also :
* {@link
* Application Xml documentation}
*/
APPLICATION(MergeType.MERGE, DEFAULT_NO_KEY_NODE_RESOLVER,
AttributeModel.newModel("backupAgent").setIsPackageDependent(),
AttributeModel.newModel(SdkConstants.ATTR_NAME).setIsPackageDependent()),
/**
* Category (contained in intent-filter)
*
* See also :
* {@link
* Category Xml documentation}
*/
CATEGORY(MergeType.MERGE, DEFAULT_NAME_ATTRIBUTE_RESOLVER),
/**
* Compatible-screens (contained in manifest)
*
* See also :
* {@link
* Category Xml documentation}
*/
COMPATIBLE_SCREENS(MergeType.MERGE, DEFAULT_NO_KEY_NODE_RESOLVER),
/**
* Data (contained in intent-filter)
*
* See also :
* {@link
* Category Xml documentation}
*/
DATA(MergeType.MERGE, DEFAULT_NO_KEY_NODE_RESOLVER),
/**
* Grant-uri-permission (contained in intent-filter)
*
* See also :
* {@link
* Category Xml documentation}
*/
GRANT_URI_PERMISSION(MergeType.MERGE, DEFAULT_NO_KEY_NODE_RESOLVER),
/**
* Instrumentation (contained in intent-filter)
*
* See also :
* {@link
* Instrunentation Xml documentation}
*/
INSTRUMENTATION(
MergeType.MERGE, DEFAULT_NO_KEY_NODE_RESOLVER,
AttributeModel.newModel("name").setMergingPolicy(AttributeModel.NO_MERGING_POLICY),
AttributeModel.newModel("targetPackage")
.setMergingPolicy(AttributeModel.NO_MERGING_POLICY),
AttributeModel.newModel("functionalTest")
.setMergingPolicy(AttributeModel.NO_MERGING_POLICY),
AttributeModel.newModel("handleProfiling")
.setMergingPolicy(AttributeModel.NO_MERGING_POLICY),
AttributeModel.newModel("label").setMergingPolicy(AttributeModel.NO_MERGING_POLICY)
),
/**
* Intent-filter (contained in activity, activity-alias, service, receiver)
*
* See also :
* {@link
* Intent-filter Xml documentation}
*/
INTENT_FILTER(MergeType.ALWAYS, INTENT_FILTER_KEY_RESOLVER,
MULTIPLE_DECLARATION_FOR_SAME_KEY_ALLOWED),
/**
* Manifest (top level node)
*
* See also :
* {@link
* Manifest Xml documentation}
*/
MANIFEST(MergeType.MERGE_CHILDREN_ONLY, DEFAULT_NO_KEY_NODE_RESOLVER),
/**
* Meta-data (contained in activity, activity-alias, application, provider, receiver)
*
* See also :
* {@link
* Meta-data Xml documentation}
*/
META_DATA(MergeType.MERGE, DEFAULT_NAME_ATTRIBUTE_RESOLVER),
/**
* Path-permission (contained in provider)
*
* See also :
* {@link
* Meta-data Xml documentation}
*/
PATH_PERMISSION(MergeType.MERGE, DEFAULT_NO_KEY_NODE_RESOLVER),
/**
* Permission-group (contained in manifest).
*
* See also :
* {@link
* Permission-group Xml documentation}
*
*/
PERMISSION_GROUP(MergeType.MERGE, DEFAULT_NAME_ATTRIBUTE_RESOLVER,
AttributeModel.newModel(SdkConstants.ATTR_NAME)),
/**
* Permission (contained in manifest).
*
* See also :
* {@link
* Permission Xml documentation}
*
*/
PERMISSION(MergeType.MERGE, DEFAULT_NAME_ATTRIBUTE_RESOLVER,
AttributeModel.newModel(SdkConstants.ATTR_NAME),
AttributeModel.newModel("protectionLevel")
.setDefaultValue("normal")
// TODO : this will need to be populated from
// sdk/platforms/android-19/data/res/values.attrs_manifest.xml
.setOnReadValidator(new MultiValueValidator(
"normal", "dangerous", "signature", "signatureOrSystem"))),
/**
* Permission-tree (contained in manifest).
*
* See also :
* {@link
* Permission-tree Xml documentation}
*
*/
PERMISSION_TREE(MergeType.MERGE, DEFAULT_NAME_ATTRIBUTE_RESOLVER,
AttributeModel.newModel(SdkConstants.ATTR_NAME)),
/**
* Provider (contained in application)
*
* See also :
* {@link
* Provider Xml documentation}
*/
PROVIDER(MergeType.MERGE, DEFAULT_NAME_ATTRIBUTE_RESOLVER,
AttributeModel.newModel(SdkConstants.ATTR_NAME)
.setIsPackageDependent()),
/**
* Receiver (contained in application)
*
* See also :
* {@link
* Receiver Xml documentation}
*/
RECEIVER(MergeType.MERGE, DEFAULT_NAME_ATTRIBUTE_RESOLVER,
AttributeModel.newModel(SdkConstants.ATTR_NAME).setIsPackageDependent()),
/**
* Screen (contained in compatible-screens)
*
* See also :
* {@link
* Receiver Xml documentation}
*/
SCREEN(MergeType.MERGE, new TwoAttributesBasedKeyResolver(
new AttributeBasedNodeKeyResolver(ANDROID_URI, "screenSize"),
new AttributeBasedNodeKeyResolver(ANDROID_URI, "screenDensity"))),
/**
* Service (contained in application)
*
* See also :
* {@link
* Service Xml documentation}
*/
SERVICE(MergeType.MERGE, DEFAULT_NAME_ATTRIBUTE_RESOLVER,
AttributeModel.newModel(SdkConstants.ATTR_NAME).setIsPackageDependent()),
/**
* Supports-gl-texture (contained in manifest)
*
* See also :
* {@link
* Support-screens Xml documentation}
*/
SUPPORTS_GL_TEXTURE(MergeType.MERGE, DEFAULT_NAME_ATTRIBUTE_RESOLVER),
/**
* Support-screens (contained in manifest)
*
* See also :
* {@link
* Support-screens Xml documentation}
*/
SUPPORTS_SCREENS(MergeType.MERGE, DEFAULT_NO_KEY_NODE_RESOLVER),
/**
* Uses-configuration (contained in manifest)
*
* See also :
* {@link
* Support-screens Xml documentation}
*/
USES_CONFIGURATION(MergeType.MERGE, DEFAULT_NO_KEY_NODE_RESOLVER),
/**
* Uses-feature (contained in manifest)
*
* See also :
* {@link
* Uses-feature Xml documentation}
*/
USES_FEATURE(MergeType.MERGE, NAME_AND_GLESVERSION_KEY_RESOLVER,
AttributeModel.newModel(AndroidManifest.ATTRIBUTE_REQUIRED)
.setDefaultValue(SdkConstants.VALUE_TRUE)
.setOnReadValidator(BOOLEAN_VALIDATOR)
.setMergingPolicy(AttributeModel.OR_MERGING_POLICY),
AttributeModel.newModel(AndroidManifest.ATTRIBUTE_GLESVERSION)
.setDefaultValue("0x00010000")
.setOnReadValidator(new Hexadecimal32BitsWithMinimumValue(0x00010000))),
/**
* Use-library (contained in application)
*
* See also :
* {@link
* Use-library Xml documentation}
*/
USES_LIBRARY(MergeType.MERGE, DEFAULT_NAME_ATTRIBUTE_RESOLVER,
AttributeModel.newModel(AndroidManifest.ATTRIBUTE_REQUIRED)
.setDefaultValue(SdkConstants.VALUE_TRUE)
.setOnReadValidator(BOOLEAN_VALIDATOR)
.setMergingPolicy(AttributeModel.OR_MERGING_POLICY)),
/**
* Uses-permission (contained in application)
*
* See also :
* {@link
* Uses-permission Xml documentation}
*/
USES_PERMISSION(MergeType.MERGE, DEFAULT_NAME_ATTRIBUTE_RESOLVER),
/**
* Uses-sdk (contained in manifest)
*
* See also :
* {@link
* Uses-sdk Xml documentation}
*/
USES_SDK(MergeType.MERGE, DEFAULT_NO_KEY_NODE_RESOLVER,
AttributeModel.newModel("minSdkVersion")
.setDefaultValue(SdkConstants.VALUE_1)
.setMergingPolicy(AttributeModel.NO_MERGING_POLICY),
AttributeModel.newModel("maxSdkVersion")
.setMergingPolicy(AttributeModel.NO_MERGING_POLICY),
// TODO : model target's default value is minSdkVersion value.
AttributeModel.newModel("targetSdkVersion")
.setMergingPolicy(AttributeModel.NO_MERGING_POLICY)
),
/**
* Custom tag for any application specific element
*/
CUSTOM(MergeType.MERGE, DEFAULT_NO_KEY_NODE_RESOLVER);
private final MergeType mMergeType;
private final NodeKeyResolver mNodeKeyResolver;
private final ImmutableList
* {@code
*
* has a xml simple name of "activity" which will resolve to {@link NodeTypes#ACTIVITY} value.
*
* Note : a runtime exception will be generated if no mapping from the simple name to a
* {@link com.android.manifmerger.ManifestModel.NodeTypes} exists.
*
* @param xmlSimpleName the xml (lower-hyphen separated words) simple name.
* @return the {@link NodeTypes} associated with that element name.
*/
static NodeTypes fromXmlSimpleName(String xmlSimpleName) {
String constantName = SdkUtils.xmlNameToConstantName(xmlSimpleName);
try {
return NodeTypes.valueOf(constantName);
} catch (IllegalArgumentException e) {
// if this element name is not a known tag, we categorize it as 'custom' which will
// be simply merged. It will prevent us from catching simple spelling mistakes but
// extensibility is a must have feature.
return NodeTypes.CUSTOM;
}
}
MergeType getMergeType() {
return mMergeType;
}
/**
* Returns true if multiple declaration for the same type and key are allowed or false if
* there must be only one declaration of this element for a particular key value.
*/
boolean areMultipleDeclarationAllowed() {
return mMultipleDeclarationAllowed;
}
}
}