| 1 |
package com.android.test.hierarchyviewer; |
| 2 |
|
| 3 |
import java.nio.ByteBuffer; |
| 4 |
import java.util.ArrayList; |
| 5 |
import java.util.HashMap; |
| 6 |
import java.util.List; |
| 7 |
import java.util.Locale; |
| 8 |
import java.util.Map; |
| 9 |
|
| 10 |
public class ViewDumpParser { |
| 11 |
private Map<String, Short> mIds; |
| 12 |
private List<Map<Short,Object>> mViews; |
| 13 |
|
| 14 |
public void parse(byte[] data) { |
| 15 |
Decoder d = new Decoder(ByteBuffer.wrap(data)); |
| 16 |
|
| 17 |
mViews = new ArrayList<>(100); |
| 18 |
|
| 19 |
boolean dataIncludesWindowPosition = (data[0] == 'S'); |
| 20 |
Short windowLeftKey = null, windowTopKey = null; |
| 21 |
Integer windowLeftValue = null, windowTopValue = null; |
| 22 |
if (dataIncludesWindowPosition) { |
| 23 |
windowLeftKey = (Short) d.readObject(); |
| 24 |
windowLeftValue = (Integer) d.readObject(); |
| 25 |
windowTopKey = (Short) d.readObject(); |
| 26 |
windowTopValue = (Integer) d.readObject(); |
| 27 |
} |
| 28 |
|
| 29 |
while (d.hasRemaining()) { |
| 30 |
Object o = d.readObject(); |
| 31 |
if (o instanceof Map) { |
| 32 |
|
| 33 |
mViews.add((Map<Short, Object>) o); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
if (mViews.isEmpty()) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
if (dataIncludesWindowPosition) { |
| 42 |
mViews.get(0).put(windowLeftKey, windowLeftValue); |
| 43 |
mViews.get(0).put(windowTopKey, windowTopValue); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
Map<Short,Object> idMap = mViews.remove(mViews.size() - 1); |
| 48 |
mIds = reverse(idMap); |
| 49 |
} |
| 50 |
|
| 51 |
public String getFirstView() { |
| 52 |
if (mViews.isEmpty()) { |
| 53 |
return null; |
| 54 |
} |
| 55 |
|
| 56 |
Map<Short, Object> props = mViews.get(0); |
| 57 |
Object name = getProperty(props, "__name__"); |
| 58 |
Object hash = getProperty(props, "__hash__"); |
| 59 |
|
| 60 |
if (name instanceof String && hash instanceof Integer) { |
| 61 |
return String.format(Locale.US, "%s@%x", name, (Integer) hash); |
| 62 |
} else { |
| 63 |
return null; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
private Object getProperty(Map<Short, Object> props, String key) { |
| 68 |
return props.get(mIds.get(key)); |
| 69 |
} |
| 70 |
|
| 71 |
private static Map<String, Short> reverse(Map<Short, Object> m) { |
| 72 |
Map<String, Short> r = new HashMap<String, Short>(m.size()); |
| 73 |
|
| 74 |
for (Map.Entry<Short, Object> e : m.entrySet()) { |
| 75 |
r.put((String)e.getValue(), e.getKey()); |
| 76 |
} |
| 77 |
|
| 78 |
return r; |
| 79 |
} |
| 80 |
|
| 81 |
public List<Map<Short, Object>> getViews() { |
| 82 |
return mViews; |
| 83 |
} |
| 84 |
|
| 85 |
public Map<String, Short> getIds() { |
| 86 |
return mIds; |
| 87 |
} |
| 88 |
|
| 89 |
} |