summaryrefslogtreecommitdiffstats
path: root/tests/HierarchyViewerTest/src/com/android/test/hierarchyviewer/ViewDumpParser.java
blob: 0111bc6316508b4cd6d945df5c9823d5afd54609 (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
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);
        while (d.hasRemaining()) {
            Object o = d.readObject();
            if (o instanceof Map) {
                //noinspection unchecked
                mViews.add((Map<Short, Object>) o);
            }
        }

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

        // 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;
    }

}