1#!/usr/bin/env python 2 3import sys 4import xml.etree.ElementTree as ET 5 6def findSdkLevelForAttribute(id): 7 intId = int(id, 16) 8 packageId = 0x000000ff & (intId >> 24) 9 typeId = 0x000000ff & (intId >> 16) 10 entryId = 0x0000ffff & intId 11 12 if packageId != 0x01 or typeId != 0x01: 13 return 0 14 15 levels = [(1, 0x021c), (2, 0x021d), (3, 0x0269), (4, 0x028d), 16 (5, 0x02ad), (6, 0x02b3), (7, 0x02b5), (8, 0x02bd), 17 (9, 0x02cb), (11, 0x0361), (12, 0x0366), (13, 0x03a6), 18 (16, 0x03ae), (17, 0x03cc), (18, 0x03da), (19, 0x03f1), 19 (20, 0x03f6), (21, 0x04ce)] 20 for level, attrEntryId in levels: 21 if entryId <= attrEntryId: 22 return level 23 return 22 24 25 26tree = None 27with open(sys.argv[1], 'rt') as f: 28 tree = ET.parse(f) 29 30attrs = [] 31for node in tree.iter('public'): 32 if node.get('type') == 'attr': 33 sdkLevel = findSdkLevelForAttribute(node.get('id', '0')) 34 if sdkLevel > 1 and sdkLevel < 22: 35 attrs.append("{{ u\"{}\", {} }}".format(node.get('name'), sdkLevel)) 36 37print "#include <string>" 38print "#include <unordered_map>" 39print 40print "namespace aapt {" 41print 42print "static std::unordered_map<std::u16string, size_t> sAttrMap = {" 43print ",\n ".join(attrs) 44print "};" 45print 46print "size_t findAttributeSdkLevel(const std::u16string& name) {" 47print " auto iter = sAttrMap.find(name);" 48print " if (iter != sAttrMap.end()) {" 49print " return iter->second;" 50print " }" 51print " return 0;" 52print "}" 53print 54print "} // namespace aapt" 55print 56