import struct,sys
def strpool(d,off):
    typ,hsz,sz=struct.unpack_from('<HHI',d,off)
    cnt,scnt,flags,sstart,start2=struct.unpack_from('<IIIII',d,off+8)
    utf8=bool(flags&0x100)
    offs=struct.unpack_from('<%dI'%cnt,d,off+28)
    base=off+sstart
    out=[]
    for o in offs:
        p=base+o
        if utf8:
            n=d[p]
            if n&0x80: n=((n&0x7f)<<8)|d[p+1]; p+=2
            else: p+=1
            m=d[p]
            if m&0x80: m=((m&0x7f)<<8)|d[p+1]; p+=2
            else: p+=1
            out.append(d[p:p+m].decode('utf-8','replace'))
        else:
            n=struct.unpack_from('<H',d,p)[0]; p+=2
            if n&0x8000: n=((n&0x7fff)<<16)|struct.unpack_from('<H',d,p)[0]; p+=2
            out.append(d[p:p+n*2].decode('utf-16-le','replace'))
    return out,sz
def val(t,v,S):
    if t==3: return S[v] if v<len(S) else f"@str{v}"
    if t==1: return f"@res/0x{v:08x}"
    if t==2: return f"?attr/0x{v:08x}"
    if t==16: return str(struct.unpack('<i',struct.pack('<I',v))[0])
    if t==18: return "true" if v else "false"
    if t==4: return str(struct.unpack('<f',struct.pack('<I',v))[0])
    if t in (28,29): return f"#{v:08x}"
    if t==5:
        u=["px","dp","sp","pt","in","mm"]; return f"{(v>>8)}{u[v&0xf] if (v&0xf)<6 else ''}"
    return f"0x{v:08x}(t{t})"
def main(p):
    d=open(p,'rb').read()
    S,psz=strpool(d,8)
    off=8+psz; depth=0; out=[]
    while off<len(d):
        typ,hsz,sz=struct.unpack_from('<HHI',d,off)
        if typ==0x0102:  # start elem
            ns,nm=struct.unpack_from('<iI',d,off+16)
            astart,asize,acount=struct.unpack_from('<HHH',d,off+24)
            attrs=[]
            for i in range(acount):
                a=off+16+astart+i*asize
                ans,anm,araw,tv,dv=struct.unpack_from('<iiiIi',d,a)
                t=(tv>>24)&0xff
                nmv=S[anm] if anm>=0 else '?'
                if araw>=0 and t==3: v=S[araw]
                else: v=val(t,dv&0xffffffff,S)
                pre='android:' if ans>=0 else ''
                attrs.append(f'{pre}{nmv}="{v}"')
            out.append("  "*depth + f"<{S[nm]}" + ("".join(" "+a for a in attrs)) + ">")
            depth+=1
        elif typ==0x0103:
            depth-=1
            ns,nm=struct.unpack_from('<iI',d,off+16)
            out.append("  "*depth + f"</{S[nm]}>")
        off+=sz
        if sz==0: break
    print("\n".join(out))
main(sys.argv[1])
