Home | History | Annotate | Download | only in tools
      1 #!/usr/bin/env python
      2 
      3 import sys
      4 import xml.etree.ElementTree as ET
      5 
      6 def 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 
     26 tree = None
     27 with open(sys.argv[1], 'rt') as f:
     28     tree = ET.parse(f)
     29 
     30 attrs = []
     31 for 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 
     37 print "#include <string>"
     38 print "#include <unordered_map>"
     39 print
     40 print "namespace aapt {"
     41 print
     42 print "static std::unordered_map<std::u16string, size_t> sAttrMap = {"
     43 print ",\n    ".join(attrs)
     44 print "};"
     45 print
     46 print "size_t findAttributeSdkLevel(const std::u16string& name) {"
     47 print "    auto iter = sAttrMap.find(name);"
     48 print "    if (iter != sAttrMap.end()) {"
     49 print "        return iter->second;"
     50 print "    }"
     51 print "    return 0;"
     52 print "}"
     53 print
     54 print "} // namespace aapt"
     55 print
     56