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. |
11 | 11 |
12 1. paddingStart attribute can cause a crash on Galaxy Tab 2. | 12 1. paddingStart attribute can cause a crash on Galaxy Tab 2. |
13 2. There is a bug that paddingStart does not override paddingLeft on | 13 2. There is a bug that paddingStart does not override paddingLeft on |
14 JB-MR1. This is fixed on JB-MR2. | 14 JB-MR1. This is fixed on JB-MR2. |
15 | 15 |
16 Therefore, this resource generation script can be removed when | 16 Therefore, this resource generation script can be removed when |
17 we drop the support for JB-MR1. | 17 we drop the support for JB-MR1. |
18 | 18 |
19 Please refer to http://crbug.com/235118 for the details. | 19 Please refer to http://crbug.com/235118 for the details. |
20 """ | 20 """ |
21 | 21 |
22 import optparse | 22 import optparse |
23 import os | 23 import os |
24 import re | 24 import re |
25 import shutil | 25 import shutil |
26 import sys | 26 import sys |
27 import xml.dom.minidom as minidom | 27 import xml.dom.minidom as minidom |
28 | 28 |
29 from util import build_utils | 29 from util import build_utils # pylint: disable=F0401 |
30 | 30 |
31 # Note that we are assuming 'android:' is an alias of | 31 # Note that we are assuming 'android:' is an alias of |
32 # the namespace 'http://schemas.android.com/apk/res/android'. | 32 # the namespace 'http://schemas.android.com/apk/res/android'. |
33 | 33 |
34 GRAVITY_ATTRIBUTES = ('android:gravity', 'android:layout_gravity') | 34 GRAVITY_ATTRIBUTES = ('android:gravity', 'android:layout_gravity') |
35 | 35 |
36 # Almost all the attributes that has "Start" or "End" in | 36 # Almost all the attributes that has "Start" or "End" in |
37 # its name should be mapped. | 37 # its name should be mapped. |
38 ATTRIBUTES_TO_MAP = {'paddingStart' : 'paddingLeft', | 38 ATTRIBUTES_TO_MAP = {'paddingStart' : 'paddingLeft', |
39 'drawableStart' : 'drawableLeft', | 39 'drawableStart' : 'drawableLeft', |
40 'layout_alignStart' : 'layout_alignLeft', | 40 'layout_alignStart' : 'layout_alignLeft', |
41 'layout_marginStart' : 'layout_marginLeft', | 41 'layout_marginStart' : 'layout_marginLeft', |
42 'layout_alignParentStart' : 'layout_alignParentLeft', | 42 'layout_alignParentStart' : 'layout_alignParentLeft', |
43 'layout_toStartOf' : 'layout_toLeftOf', | 43 'layout_toStartOf' : 'layout_toLeftOf', |
44 'paddingEnd' : 'paddingRight', | 44 'paddingEnd' : 'paddingRight', |
45 'drawableEnd' : 'drawableRight', | 45 'drawableEnd' : 'drawableRight', |
46 'layout_alignEnd' : 'layout_alignRight', | 46 'layout_alignEnd' : 'layout_alignRight', |
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 | 57 |
58 def IterateXmlElements(node): | 58 def IterateXmlElements(node): |
59 """minidom helper function that iterates all the element nodes. | 59 """minidom helper function that iterates all the element nodes. |
60 Iteration order is pre-order depth-first.""" | 60 Iteration order is pre-order depth-first.""" |
61 if node.nodeType == node.ELEMENT_NODE: | 61 if node.nodeType == node.ELEMENT_NODE: |
62 yield node | 62 yield node |
63 for child_node in node.childNodes: | 63 for child_node in node.childNodes: |
64 for child_node_element in IterateXmlElements(child_node): | 64 for child_node_element in IterateXmlElements(child_node): |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 | 274 |
275 if args: | 275 if args: |
276 parser.error('No positional arguments should be given.') | 276 parser.error('No positional arguments should be given.') |
277 | 277 |
278 # Check that required options have been provided. | 278 # Check that required options have been provided. |
279 required_options = ('res_dir', 'res_v14_compatibility_dir') | 279 required_options = ('res_dir', 'res_v14_compatibility_dir') |
280 build_utils.CheckOptions(options, parser, required=required_options) | 280 build_utils.CheckOptions(options, parser, required=required_options) |
281 return options | 281 return options |
282 | 282 |
283 | 283 |
284 def main(argv): | 284 def main(): |
285 options = ParseArgs() | 285 options = ParseArgs() |
286 | 286 |
287 build_utils.DeleteDirectory(options.res_v14_compatibility_dir) | 287 build_utils.DeleteDirectory(options.res_v14_compatibility_dir) |
288 build_utils.MakeDirectory(options.res_v14_compatibility_dir) | 288 build_utils.MakeDirectory(options.res_v14_compatibility_dir) |
289 | 289 |
290 for name in os.listdir(options.res_dir): | 290 for name in os.listdir(options.res_dir): |
291 if not os.path.isdir(os.path.join(options.res_dir, name)): | 291 if not os.path.isdir(os.path.join(options.res_dir, name)): |
292 continue | 292 continue |
293 | 293 |
294 dir_pieces = name.split('-') | 294 dir_pieces = name.split('-') |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 '-'.join([resource_type] + | 335 '-'.join([resource_type] + |
336 output_qualifiers)) | 336 output_qualifiers)) |
337 GenerateV14StyleResourcesInDir(input_dir, output_v14_dir) | 337 GenerateV14StyleResourcesInDir(input_dir, output_v14_dir) |
338 elif not api_level_qualifier: | 338 elif not api_level_qualifier: |
339 ErrorIfStyleResourceExistsInDir(input_dir) | 339 ErrorIfStyleResourceExistsInDir(input_dir) |
340 | 340 |
341 if options.stamp: | 341 if options.stamp: |
342 build_utils.Touch(options.stamp) | 342 build_utils.Touch(options.stamp) |
343 | 343 |
344 if __name__ == '__main__': | 344 if __name__ == '__main__': |
345 sys.exit(main(sys.argv)) | 345 sys.exit(main()) |
346 | 346 |
OLD | NEW |