OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright 2013 The Chromium Authors. All rights reserved. | 3 # Copyright 2013 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 """Convert Android xml resources to API 14 compatible. | 7 """Convert Android xml resources to API 14 compatible. |
8 | 8 |
9 There are two reasons that we cannot just use API 17 attributes, | 9 There are two reasons that we cannot just use API 17 attributes, |
10 so we are generating another set of resources by this script. | 10 so we are generating another set of resources by this script. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 'layout_marginEnd' : 'layout_marginRight', | 47 'layout_marginEnd' : 'layout_marginRight', |
48 'layout_alignParentEnd' : 'layout_alignParentRight', | 48 'layout_alignParentEnd' : 'layout_alignParentRight', |
49 'layout_toEndOf' : 'layout_toRightOf'} | 49 'layout_toEndOf' : 'layout_toRightOf'} |
50 | 50 |
51 ATTRIBUTES_TO_MAP = dict(['android:' + k, 'android:' + v] for k, v | 51 ATTRIBUTES_TO_MAP = dict(['android:' + k, 'android:' + v] for k, v |
52 in ATTRIBUTES_TO_MAP.iteritems()) | 52 in ATTRIBUTES_TO_MAP.iteritems()) |
53 | 53 |
54 ATTRIBUTES_TO_MAP_REVERSED = dict([v, k] for k, v | 54 ATTRIBUTES_TO_MAP_REVERSED = dict([v, k] for k, v |
55 in ATTRIBUTES_TO_MAP.iteritems()) | 55 in ATTRIBUTES_TO_MAP.iteritems()) |
56 | 56 |
| 57 # Attributes that are allowed to use if both are used in the same element. |
| 58 ALLOWED_ATTRIBUTE_PAIRS = {'layout_marginStart' : 'layout_marginLeft', |
| 59 'layout_marginEnd' : 'layout_marginRight'} |
| 60 |
| 61 ALLOWED_ATTRIBUTE_PAIRS = dict(['android:' + k, 'android:' + v] for k, v |
| 62 in ALLOWED_ATTRIBUTE_PAIRS.iteritems()) |
| 63 |
| 64 ALLOWED_ATTRIBUTE_PAIRS.update(dict([v, k] for k, v |
| 65 in ALLOWED_ATTRIBUTE_PAIRS.iteritems())) |
57 | 66 |
58 def IterateXmlElements(node): | 67 def IterateXmlElements(node): |
59 """minidom helper function that iterates all the element nodes. | 68 """minidom helper function that iterates all the element nodes. |
60 Iteration order is pre-order depth-first.""" | 69 Iteration order is pre-order depth-first.""" |
61 if node.nodeType == node.ELEMENT_NODE: | 70 if node.nodeType == node.ELEMENT_NODE: |
62 yield node | 71 yield node |
63 for child_node in node.childNodes: | 72 for child_node in node.childNodes: |
64 for child_node_element in IterateXmlElements(child_node): | 73 for child_node_element in IterateXmlElements(child_node): |
65 yield child_node_element | 74 yield child_node_element |
66 | 75 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 assert_not_deprecated: Whether deprecated attributes (e.g. paddingLeft) will | 126 assert_not_deprecated: Whether deprecated attributes (e.g. paddingLeft) will |
118 cause an exception to be thrown. | 127 cause an exception to be thrown. |
119 | 128 |
120 Returns: | 129 Returns: |
121 True if dom is modified, False otherwise. | 130 True if dom is modified, False otherwise. |
122 """ | 131 """ |
123 is_modified = False | 132 is_modified = False |
124 | 133 |
125 # Iterate all the elements' attributes to find attributes to convert. | 134 # Iterate all the elements' attributes to find attributes to convert. |
126 for element in IterateXmlElements(dom): | 135 for element in IterateXmlElements(dom): |
| 136 attribute_names = [name for name, value in element.attributes.items()] |
127 for name, value in list(element.attributes.items()): | 137 for name, value in list(element.attributes.items()): |
| 138 # Some Left&Right attributes safe to use. So we allow those attributes, |
| 139 # while still forcing they are used in a pair as Android recommended. |
| 140 # http://android-developers.blogspot.com/2013/03/ |
| 141 # native-rtl-support-in-android-42.html |
| 142 # |
| 143 # e.g. |
| 144 # android:layout_marginStart="10dp" |
| 145 # android:layout_marginLeft="10dp" |
| 146 if (ALLOWED_ATTRIBUTE_PAIRS.get(name) in attribute_names): |
| 147 continue |
| 148 |
128 # Convert any API 17 Start/End attributes to Left/Right attributes. | 149 # Convert any API 17 Start/End attributes to Left/Right attributes. |
129 # For example, from paddingStart="10dp" to paddingLeft="10dp" | 150 # For example, from paddingStart="10dp" to paddingLeft="10dp" |
130 # Note: gravity attributes are not necessary to convert because | 151 # Note: gravity attributes are not necessary to convert because |
131 # start/end values are backward-compatible. Explained at | 152 # start/end values are backward-compatible. Explained at |
132 # https://plus.sandbox.google.com/+RomanNurik/posts/huuJd8iVVXY?e=Showroom | 153 # https://plus.sandbox.google.com/+RomanNurik/posts/huuJd8iVVXY?e=Showroom |
133 if name in ATTRIBUTES_TO_MAP: | 154 if name in ATTRIBUTES_TO_MAP: |
134 element.setAttribute(ATTRIBUTES_TO_MAP[name], value) | 155 element.setAttribute(ATTRIBUTES_TO_MAP[name], value) |
135 del element.attributes[name] | 156 del element.attributes[name] |
136 is_modified = True | 157 is_modified = True |
137 elif assert_not_deprecated: | 158 elif assert_not_deprecated: |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 build_utils.MakeDirectory(res_v14_dir) | 361 build_utils.MakeDirectory(res_v14_dir) |
341 | 362 |
342 GenerateV14Resources(options.res_dir, res_v14_dir, options.verify_only) | 363 GenerateV14Resources(options.res_dir, res_v14_dir, options.verify_only) |
343 | 364 |
344 if options.stamp: | 365 if options.stamp: |
345 build_utils.Touch(options.stamp) | 366 build_utils.Touch(options.stamp) |
346 | 367 |
347 if __name__ == '__main__': | 368 if __name__ == '__main__': |
348 sys.exit(main()) | 369 sys.exit(main()) |
349 | 370 |
OLD | NEW |