| 1 |
/* |
| 2 |
* Copyright (C) 2016 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 |
package com.android.manifmerger; |
| 17 |
|
| 18 |
import com.android.SdkConstants; |
| 19 |
import com.android.annotations.NonNull; |
| 20 |
import com.android.ide.common.blame.SourceFilePosition; |
| 21 |
import com.android.ide.common.blame.SourcePosition; |
| 22 |
import com.android.utils.SdkUtils; |
| 23 |
import com.android.utils.XmlUtils; |
| 24 |
|
| 25 |
import org.w3c.dom.Attr; |
| 26 |
import org.w3c.dom.Element; |
| 27 |
import org.w3c.dom.NodeList; |
| 28 |
|
| 29 |
/** |
| 30 |
* List of manifest files properties that can be directly overridden without using a |
| 31 |
* placeholder. |
| 32 |
*/ |
| 33 |
public enum ManifestSystemProperty implements ManifestMerger2.AutoAddingProperty { |
| 34 |
|
| 35 |
/** |
| 36 |
* Allow setting the merged manifest file package name. |
| 37 |
*/ |
| 38 |
PACKAGE { |
| 39 |
@Override |
| 40 |
public void addTo(@NonNull ActionRecorder actionRecorder, |
| 41 |
@NonNull XmlDocument document, |
| 42 |
@NonNull String value) { |
| 43 |
addToElement(this, actionRecorder, value, document.getRootNode()); |
| 44 |
} |
| 45 |
}, |
| 46 |
/** |
| 47 |
* @see <a href="http://developer.android.com/guide/topics/manifest/manifest-element.html#vcode"> |
| 48 |
* http://developer.android.com/guide/topics/manifest/manifest-element.html#vcode</a> |
| 49 |
*/ |
| 50 |
VERSION_CODE { |
| 51 |
@Override |
| 52 |
public void addTo(@NonNull ActionRecorder actionRecorder, |
| 53 |
@NonNull XmlDocument document, |
| 54 |
@NonNull String value) { |
| 55 |
addToElementInAndroidNS(this, actionRecorder, value, document.getRootNode()); |
| 56 |
} |
| 57 |
}, |
| 58 |
/** |
| 59 |
* @see <a href="http://developer.android.com/guide/topics/manifest/manifest-element.html#vname"> |
| 60 |
* http://developer.android.com/guide/topics/manifest/manifest-element.html#vname</a> |
| 61 |
*/ |
| 62 |
VERSION_NAME { |
| 63 |
@Override |
| 64 |
public void addTo(@NonNull ActionRecorder actionRecorder, |
| 65 |
@NonNull XmlDocument document, |
| 66 |
@NonNull String value) { |
| 67 |
addToElementInAndroidNS(this, actionRecorder, value, document.getRootNode()); |
| 68 |
} |
| 69 |
}, |
| 70 |
/** |
| 71 |
* @see <a href="http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#min"> |
| 72 |
* http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#min</a> |
| 73 |
*/ |
| 74 |
MIN_SDK_VERSION { |
| 75 |
@Override |
| 76 |
public void addTo(@NonNull ActionRecorder actionRecorder, |
| 77 |
@NonNull XmlDocument document, |
| 78 |
@NonNull String value) { |
| 79 |
addToElementInAndroidNS(this, actionRecorder, value, |
| 80 |
createOrGetUseSdk(actionRecorder, document)); |
| 81 |
} |
| 82 |
}, |
| 83 |
/** |
| 84 |
* @see <a href="http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#target"> |
| 85 |
* http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#target</a> |
| 86 |
*/ |
| 87 |
TARGET_SDK_VERSION { |
| 88 |
@Override |
| 89 |
public void addTo(@NonNull ActionRecorder actionRecorder, |
| 90 |
@NonNull XmlDocument document, |
| 91 |
@NonNull String value) { |
| 92 |
addToElementInAndroidNS(this, actionRecorder, value, |
| 93 |
createOrGetUseSdk(actionRecorder, document)); |
| 94 |
} |
| 95 |
}, |
| 96 |
/** |
| 97 |
* @see <a href="http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#max"> |
| 98 |
* http://developer.android.com/guide/topics/manifest/uses-sdk-element.html#max</a> |
| 99 |
*/ |
| 100 |
MAX_SDK_VERSION { |
| 101 |
@Override |
| 102 |
public void addTo(@NonNull ActionRecorder actionRecorder, |
| 103 |
@NonNull XmlDocument document, |
| 104 |
@NonNull String value) { |
| 105 |
addToElementInAndroidNS(this, actionRecorder, value, |
| 106 |
createOrGetUseSdk(actionRecorder, document)); |
| 107 |
} |
| 108 |
}, |
| 109 |
/** |
| 110 |
* Name of the instrumentation runner. |
| 111 |
* |
| 112 |
* @see <a href="http://developer.android.com/guide/topics/manifest/instrumentation-element.html"> |
| 113 |
* http://developer.android.com/guide/topics/manifest/instrumentation-element.html</a> |
| 114 |
*/ |
| 115 |
NAME { |
| 116 |
@Override |
| 117 |
public void addTo(@NonNull ActionRecorder actionRecorder, |
| 118 |
@NonNull XmlDocument document, |
| 119 |
@NonNull String value) { |
| 120 |
addToElementInAndroidNS(this, actionRecorder, value, |
| 121 |
createOrGetInstrumentation(actionRecorder, document)); |
| 122 |
} |
| 123 |
}, |
| 124 |
/** |
| 125 |
* Target package for the instrumentation. |
| 126 |
* |
| 127 |
* @see <a href="http://developer.android.com/guide/topics/manifest/instrumentation-element.html"> |
| 128 |
* http://developer.android.com/guide/topics/manifest/instrumentation-element.html</a> |
| 129 |
*/ |
| 130 |
TARGET_PACKAGE { |
| 131 |
@Override |
| 132 |
public void addTo(@NonNull ActionRecorder actionRecorder, |
| 133 |
@NonNull XmlDocument document, |
| 134 |
@NonNull String value) { |
| 135 |
addToElementInAndroidNS(this, actionRecorder, value, |
| 136 |
createOrGetInstrumentation(actionRecorder, document)); |
| 137 |
} |
| 138 |
}, |
| 139 |
/** |
| 140 |
* Functional test attribute for the instrumentation. |
| 141 |
* |
| 142 |
* @see <a href="http://developer.android.com/guide/topics/manifest/instrumentation-element.html"> |
| 143 |
* http://developer.android.com/guide/topics/manifest/instrumentation-element.html</a> |
| 144 |
*/ |
| 145 |
FUNCTIONAL_TEST { |
| 146 |
@Override |
| 147 |
public void addTo(@NonNull ActionRecorder actionRecorder, |
| 148 |
@NonNull XmlDocument document, |
| 149 |
@NonNull String value) { |
| 150 |
addToElementInAndroidNS(this, actionRecorder, value, |
| 151 |
createOrGetInstrumentation(actionRecorder, document)); |
| 152 |
} |
| 153 |
}, |
| 154 |
/** |
| 155 |
* Handle profiling attribute for the instrumentation. |
| 156 |
* |
| 157 |
* @see <a href="http://developer.android.com/guide/topics/manifest/instrumentation-element.html"> |
| 158 |
* http://developer.android.com/guide/topics/manifest/instrumentation-element.html</a> |
| 159 |
*/ |
| 160 |
HANDLE_PROFILING { |
| 161 |
@Override |
| 162 |
public void addTo(@NonNull ActionRecorder actionRecorder, |
| 163 |
@NonNull XmlDocument document, |
| 164 |
@NonNull String value) { |
| 165 |
addToElementInAndroidNS(this, actionRecorder, value, |
| 166 |
createOrGetInstrumentation(actionRecorder, document)); |
| 167 |
} |
| 168 |
}, |
| 169 |
/** |
| 170 |
* Label attribute for the instrumentation. |
| 171 |
* |
| 172 |
* @see <a href="http://developer.android.com/guide/topics/manifest/instrumentation-element.html"> |
| 173 |
* http://developer.android.com/guide/topics/manifest/instrumentation-element.html</a> |
| 174 |
*/ |
| 175 |
LABEL { |
| 176 |
@Override |
| 177 |
public void addTo(@NonNull ActionRecorder actionRecorder, |
| 178 |
@NonNull XmlDocument document, |
| 179 |
@NonNull String value) { |
| 180 |
addToElementInAndroidNS(this, actionRecorder, value, |
| 181 |
createOrGetInstrumentation(actionRecorder, document)); |
| 182 |
} |
| 183 |
}; |
| 184 |
|
| 185 |
public String toCamelCase() { |
| 186 |
return SdkUtils.constantNameToCamelCase(name()); |
| 187 |
} |
| 188 |
|
| 189 |
// utility method to add an attribute which name is derived from the enum name(). |
| 190 |
private static void addToElement( |
| 191 |
@NonNull ManifestSystemProperty manifestSystemProperty, |
| 192 |
@NonNull ActionRecorder actionRecorder, |
| 193 |
String value, |
| 194 |
@NonNull XmlElement to) { |
| 195 |
|
| 196 |
to.getXml().setAttribute(manifestSystemProperty.toCamelCase(), value); |
| 197 |
XmlAttribute xmlAttribute = new XmlAttribute(to, |
| 198 |
to.getXml().getAttributeNode(manifestSystemProperty.toCamelCase()), null); |
| 199 |
actionRecorder.recordAttributeAction(xmlAttribute, new Actions.AttributeRecord( |
| 200 |
Actions.ActionType.INJECTED, |
| 201 |
new SourceFilePosition(to.getSourceFile(), SourcePosition.UNKNOWN), |
| 202 |
xmlAttribute.getId(), |
| 203 |
null, /* reason */ |
| 204 |
null /* attributeOperationType */)); |
| 205 |
} |
| 206 |
|
| 207 |
// utility method to add an attribute in android namespace which local name is derived from |
| 208 |
// the enum name(). |
| 209 |
private static void addToElementInAndroidNS( |
| 210 |
@NonNull ManifestSystemProperty manifestSystemProperty, |
| 211 |
@NonNull ActionRecorder actionRecorder, |
| 212 |
String value, |
| 213 |
@NonNull XmlElement to) { |
| 214 |
|
| 215 |
String toolsPrefix = getAndroidPrefix(to.getXml()); |
| 216 |
to.getXml().setAttributeNS(SdkConstants.ANDROID_URI, |
| 217 |
toolsPrefix + XmlUtils.NS_SEPARATOR + manifestSystemProperty.toCamelCase(), |
| 218 |
value); |
| 219 |
Attr attr = to.getXml().getAttributeNodeNS(SdkConstants.ANDROID_URI, |
| 220 |
manifestSystemProperty.toCamelCase()); |
| 221 |
|
| 222 |
XmlAttribute xmlAttribute = new XmlAttribute(to, attr, null); |
| 223 |
actionRecorder.recordAttributeAction(xmlAttribute, |
| 224 |
new Actions.AttributeRecord( |
| 225 |
Actions.ActionType.INJECTED, |
| 226 |
new SourceFilePosition(to.getSourceFile(), SourcePosition.UNKNOWN), |
| 227 |
xmlAttribute.getId(), |
| 228 |
null, /* reason */ |
| 229 |
null /* attributeOperationType */ |
| 230 |
) |
| 231 |
); |
| 232 |
|
| 233 |
} |
| 234 |
|
| 235 |
// utility method to create or get an existing use-sdk xml element under manifest. |
| 236 |
// this could be made more generic by adding more metadata to the enum but since there is |
| 237 |
// only one case so far, keep it simple. |
| 238 |
@NonNull |
| 239 |
private static XmlElement createOrGetUseSdk( |
| 240 |
@NonNull ActionRecorder actionRecorder, @NonNull XmlDocument document) { |
| 241 |
return createOrGetElement(actionRecorder, document, |
| 242 |
ManifestModel.NodeTypes.USES_SDK, "use-sdk injection requested"); |
| 243 |
} |
| 244 |
|
| 245 |
/** See above for details, similar like for uses-sdk tag*/ |
| 246 |
@NonNull |
| 247 |
private static XmlElement createOrGetInstrumentation( |
| 248 |
@NonNull ActionRecorder actionRecorder, @NonNull XmlDocument document) { |
| 249 |
return createOrGetElement(actionRecorder, document, |
| 250 |
ManifestModel.NodeTypes.INSTRUMENTATION, "instrumentation injection requested"); |
| 251 |
} |
| 252 |
|
| 253 |
@NonNull |
| 254 |
private static XmlElement createOrGetElement( |
| 255 |
@NonNull ActionRecorder actionRecorder, @NonNull XmlDocument document, |
| 256 |
@NonNull ManifestModel.NodeTypes nodeType, @NonNull String message) { |
| 257 |
|
| 258 |
Element manifest = document.getXml().getDocumentElement(); |
| 259 |
NodeList nodes = manifest.getElementsByTagName(nodeType.toXmlName()); |
| 260 |
if (nodes.getLength() == 0) { |
| 261 |
nodes = manifest.getElementsByTagNameNS( |
| 262 |
SdkConstants.ANDROID_URI, nodeType.toXmlName()); |
| 263 |
} |
| 264 |
if (nodes.getLength() == 0) { |
| 265 |
// create it first. |
| 266 |
Element node = manifest.getOwnerDocument().createElement(nodeType.toXmlName()); |
| 267 |
manifest.appendChild(node); |
| 268 |
XmlElement xmlElement = new XmlElement(node, document); |
| 269 |
Actions.NodeRecord nodeRecord = new Actions.NodeRecord( |
| 270 |
Actions.ActionType.INJECTED, |
| 271 |
new SourceFilePosition(xmlElement.getSourceFile(), SourcePosition.UNKNOWN), |
| 272 |
xmlElement.getId(), |
| 273 |
message, |
| 274 |
NodeOperationType.STRICT); |
| 275 |
actionRecorder.recordNodeAction(xmlElement, nodeRecord); |
| 276 |
return xmlElement; |
| 277 |
} else { |
| 278 |
return new XmlElement((Element) nodes.item(0), document); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
private static String getAndroidPrefix(@NonNull Element xml) { |
| 283 |
String toolsPrefix = XmlUtils.lookupNamespacePrefix( |
| 284 |
xml, SdkConstants.ANDROID_URI, SdkConstants.ANDROID_NS_NAME, false); |
| 285 |
if (!toolsPrefix.equals(SdkConstants.ANDROID_NS_NAME) && xml.getOwnerDocument() |
| 286 |
.getDocumentElement().getAttribute("xmlns:" + toolsPrefix) == null) { |
| 287 |
// this is weird, the document is using "android" prefix but it's not bound |
| 288 |
// to our namespace. Add the proper xmlns declaration. |
| 289 |
xml.setAttribute("xmlns:" + toolsPrefix, SdkConstants.ANDROID_URI); |
| 290 |
} |
| 291 |
return toolsPrefix; |
| 292 |
} |
| 293 |
|
| 294 |
} |