1
1
package com .stardust .view .accessibility ;
2
2
3
+ import android .content .Context ;
4
+ import android .content .pm .PackageManager ;
5
+ import android .content .res .Resources ;
3
6
import android .graphics .Rect ;
4
7
import android .support .annotation .Keep ;
5
8
import android .support .annotation .NonNull ;
10
13
import com .stardust .automator .UiObject ;
11
14
12
15
import java .util .ArrayList ;
16
+ import java .util .HashMap ;
13
17
import java .util .List ;
14
18
15
19
/**
@@ -23,6 +27,7 @@ public class NodeInfo {
23
27
private Rect mBoundsInParent = new Rect ();
24
28
25
29
public String id ;
30
+ public String idHex ;
26
31
public String desc ;
27
32
public String className ;
28
33
public String packageName ;
@@ -54,8 +59,7 @@ public class NodeInfo {
54
59
public NodeInfo parent ;
55
60
56
61
57
-
58
- public NodeInfo (UiObject node , NodeInfo parent ) {
62
+ public NodeInfo (Resources resources , UiObject node , NodeInfo parent ) {
59
63
id = simplifyId (node .getViewIdResourceName ());
60
64
desc = node .desc ();
61
65
className = node .className ();
@@ -92,7 +96,9 @@ public NodeInfo(UiObject node, NodeInfo parent) {
92
96
indexInParent = node .indexInParent ();
93
97
94
98
this .parent = parent ;
95
-
99
+ if (resources != null && packageName != null && id != null ) {
100
+ idHex = "0x" + Integer .toHexString (resources .getIdentifier (node .getViewIdResourceName (), null , null ));
101
+ }
96
102
}
97
103
98
104
private String simplifyId (String idResourceName ) {
@@ -116,21 +122,35 @@ public static String boundsToString(Rect rect) {
116
122
}
117
123
118
124
119
- public static NodeInfo capture (@ NonNull UiObject uiObject , @ Nullable NodeInfo parent ) {
120
- NodeInfo nodeInfo = new NodeInfo (uiObject , parent );
125
+ static NodeInfo capture (HashMap <String , Resources > resourcesCache , Context context , @ NonNull UiObject uiObject , @ Nullable NodeInfo parent ) {
126
+ String pkg = uiObject .packageName ();
127
+ Resources resources = null ;
128
+ if (pkg != null ) {
129
+ resources = resourcesCache .get (pkg );
130
+ if (resources == null ) {
131
+ try {
132
+ resources = context .getPackageManager ().getResourcesForApplication (pkg );
133
+ resourcesCache .put (pkg , resources );
134
+ } catch (PackageManager .NameNotFoundException e ) {
135
+ e .printStackTrace ();
136
+ }
137
+ }
138
+ }
139
+ NodeInfo nodeInfo = new NodeInfo (resources , uiObject , parent );
121
140
int childCount = uiObject .getChildCount ();
122
141
for (int i = 0 ; i < childCount ; i ++) {
123
142
UiObject child = uiObject .child (i );
124
143
if (child != null ) {
125
- nodeInfo .children .add (capture (child , nodeInfo ));
144
+ nodeInfo .children .add (capture (resourcesCache , context , child , nodeInfo ));
126
145
}
127
146
}
128
147
return nodeInfo ;
129
148
}
130
149
131
- public static NodeInfo capture (@ NonNull AccessibilityNodeInfo root ) {
150
+ public static NodeInfo capture (Context context , @ NonNull AccessibilityNodeInfo root ) {
132
151
UiObject r = UiObject .createRoot (root );
133
- return capture (r , null );
152
+ HashMap <String , Resources > resourcesCache = new HashMap <>();
153
+ return capture (resourcesCache , context , r , null );
134
154
}
135
155
136
156
@ NonNull
0 commit comments