summaryrefslogtreecommitdiff
path: root/tests/HierarchyViewerTest/src/com/android/test/hierarchyviewer/ViewDumpParser.java
blob: 2ad0da98c409f610798ac7d138ca2778c9c75eff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.android.test.hierarchyviewer;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class ViewDumpParser {
    private Map<String, Short> mIds;
    private List<Map<Short,Object>> mViews;

    public void parse(byte[] data) {
        Decoder d = new Decoder(ByteBuffer.wrap(data));

        mViews = new ArrayList<>(100);

        boolean dataIncludesWindowPosition = (data[0] == 'S');
        Short windowLeftKey = null, windowTopKey = null;
        Integer windowLeftValue = null, windowTopValue = null;
        if (dataIncludesWindowPosition) {
            windowLeftKey = (Short) d.readObject();
            windowLeftValue = (Integer) d.readObject();
            windowTopKey = (Short) d.readObject();
            windowTopValue = (Integer) d.readObject();
        }

        while (d.hasRemaining()) {
            Object o = d.readObject();
            if (o instanceof Map) {
                //noinspection unchecked
                mViews.add((Map<Short, Object>) o);
            }
        }

        if (mViews.isEmpty()) {
            return;
        }

        if (dataIncludesWindowPosition) {
          mViews.get(0).put(windowLeftKey, windowLeftValue);
          mViews.get(0).put(windowTopKey, windowTopValue);
        }

        // the last one is the property map
        Map<Short,Object> idMap = mViews.remove(mViews.size() - 1);
        mIds = reverse(idMap);
    }

    public String getFirstView() {
        if (mViews.isEmpty()) {
            return null;
        }

        Map<Short, Object> props = mViews.get(0);
        Object name = getProperty(props, "__name__");
        Object hash = getProperty(props, "__hash__");

        if (name instanceof String && hash instanceof Integer) {
            return String.format(Locale.US, "%s@%x", name, hash);
        } else {
            return null;
        }
    }

    private Object getProperty(Map<Short, Object> props, String key) {
        return props.get(mIds.get(key));
    }

    private static Map<String, Short> reverse(Map<Short, Object> m) {
        Map<String, Short> r = new HashMap<String, Short>(m.size());

        for (Map.Entry<Short, Object> e : m.entrySet()) {
            r.put((String)e.getValue(), e.getKey());
        }

        return r;
    }

    public List<Map<Short, Object>> getViews() {
        return mViews;
    }

    public Map<String, Short> getIds() {
        return mIds;
    }

}