3 # Copyright (c) 2014 Intel Corporation. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
8 Provide the function interface of mapping the permissions
9 from manifest.json to AndroidManifest.xml.
10 It suppports the mapping of permission fields both defined in Crosswalk
11 and supported by Android Manifest specification.
13 Sample usage from shell script:
14 python handle_permissions.py --jsonfile=/path/to/manifest.json
15 --manifest=/path/to/AndroidManifest.xml
22 from handle_xml import AddElementAttribute
23 from manifest_json_parser import ManifestJsonParser
24 from xml.dom import minidom
25 from xml.parsers.expat import ExpatError
27 # The global permission mapping table.
28 # TODO: update the permission mapping table for added permission.
29 permission_mapping_table = {
30 'contacts' : ['android.permission.READ_CONTACTS',
31 'android.permission.WRITE_CONTACTS'],
32 'geolocation' : ['android.permission.ACCESS_FINE_LOCATION'],
33 'messaging' : ['android.permission.READ_SMS',
34 'android.permission.READ_PHONE_STATE',
35 'android.permission.RECEIVE_SMS',
36 'android.permission.SEND_SMS',
37 'android.permission.WRITE_SMS'],
38 'devicecapabilities' : [],
42 'screenorientation' : [],
43 'vibration' : ['android.permission.VIBRATE']
47 def HandlePermissions(permissions, xmldoc):
48 """ Implement the mapping of permission list to the AndroidManifest.xml file.
50 permissions: the permissions defined in Crosswalk and Android Manifest
52 xmldoc: the parsed xmldoc of the AndroidManifest.xml file, used for
56 existing_permission_list = []
57 used_permissions = xmldoc.getElementsByTagName("uses-permission")
58 for item in used_permissions:
59 existing_permission_list.append(item.getAttribute("android:name"))
61 for permission in permissions.split(':'):
62 if permission.lower() not in list(permission_mapping_table.keys()):
63 print('Error: permission \'%s\' related API is not supported.'
66 permission_item = permission_mapping_table.get(permission.lower())
68 for android_permission in permission_item:
69 if android_permission not in existing_permission_list:
70 existing_permission_list.append(android_permission)
71 AddElementAttribute(xmldoc, 'uses-permission',
72 'android:name', android_permission)
76 """Respond to command mode of the mapping permission list."""
77 parser = optparse.OptionParser()
78 info = ('The input json-format file name. Such as: '
79 '--jsonfile=manifest.json')
80 parser.add_option('-j', '--jsonfile', action='store', dest='jsonfile',
82 info = ('The destination android manifest file name. Such as: '
83 '--manifest=AndroidManifest.xml')
84 parser.add_option('-m', '--manifest', action='store', dest='manifest',
86 options, _ = parser.parse_args()
90 if not options.jsonfile:
91 print('Please set the manifest.json file.')
93 if not options.manifest:
94 print('Please set the AndroidManifest.xml file.')
97 json_parser = ManifestJsonParser(os.path.expanduser(options.jsonfile))
98 if json_parser.GetPermissions():
99 options.permissions = json_parser.GetPermissions()
100 except SystemExit as ec:
101 print('Exiting with error code: %d' % ec.code)
104 xmldoc = minidom.parse(options.manifest)
105 HandlePermissions(options.permissions, xmldoc)
106 file_handle = open(options.manifest, 'wb')
107 xmldoc.writexml(file_handle)
109 except (ExpatError, IOError):
110 print('There is an error in AndroidManifest.xml.')
115 if __name__ == '__main__':
116 sys.exit(main(sys.argv))