-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathview.js
More file actions
353 lines (327 loc) · 12.4 KB
/
Copy pathview.js
File metadata and controls
353 lines (327 loc) · 12.4 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import Java from "frida-java-bridge";
function findFrontMostVisibleViewRoot() {
let TAG = "findFrontMostVisibleViewRoot";
// 获取到 WindowManagerGlobal 实例
let WindowManagerGlobal = Java.use("android.view.WindowManagerGlobal");
let WindowLayoutParams = Java.use("android.view.WindowManager$LayoutParams");
let View = Java.use("android.view.View");
// 获取 WindowManagerGlobal 的实例
let instance = WindowManagerGlobal.getInstance();
// 获取实例的 mViews
let views = instance.mViews.value;
// 获取 mParams 属性
let params = instance.mParams.value;
// 遍历 viewsArray
let focusedView = null;
let maxType = -1;
for (let i = 0; i < views.size(); i++) {
let view = Java.cast(views.get(i), View);
let visible = view.getVisibility() === View.VISIBLE.value;
let windowParams = Java.cast(params.get(i), WindowLayoutParams);
let windowType = windowParams.type.value;
if (visible && windowType > maxType) {
focusedView = view;
maxType = windowType;
}
send({ type: 'debug', message: `View[${i}]: ${view}, Type: ${view.$className}, Visibility: ${visible ? "VISIBLE" : "INVISIBLE"}, Window Type: ${windowType}` });
}
send({ type: 'info', message: `Focused View: ${focusedView}, Max Type: ${maxType}` });
return focusedView;
}
function isAssignableFrom(ins, clazz) {
return clazz.isInstance(ins)
}
function buildViewList(rootView) {
let ViewGroup = Java.use("android.view.ViewGroup");
let result = [];
function traverse(view) {
if (!view) return;
result.push(view);
if (isAssignableFrom(view, ViewGroup.class)) {
let group = Java.cast(view, ViewGroup);
let count = group.getChildCount();
for (let i = 0; i < count; i++) {
let child = group.getChildAt(i);
traverse(child);
}
}
}
traverse(rootView);
return result;
}
// traverse view hierarchy to get all views, use BFS
// return a tree of views
function buildViewTree(rootView) {
let ViewGroup = Java.use("android.view.ViewGroup");
function buildTree(view) {
let node = {
view: view,
children: []
};
if (isAssignableFrom(view, ViewGroup.class)) {
let group = Java.cast(view, ViewGroup);
let count = group.getChildCount();
for (let i = 0; i < count; i++) {
let child = group.getChildAt(i);
if (child !== null) {
node.children.push(buildTree(child));
}
}
}
return node;
}
return buildTree(rootView);
}
function flattenTree(node) {
// Recursively flatten the tree into a flat array of views
let result = [];
function traverse(n) {
if (!n) return;
result.push(n.view);
if (n.children && n.children.length > 0) {
for (let i = 0; i < n.children.length; i++) {
traverse(n.children[i]);
}
}
}
traverse(node);
return result;
}
function visibilityToString(visibility) {
let View = Java.use("android.view.View");
switch (visibility) {
case View.VISIBLE.value:
return "VISIBLE";
case View.INVISIBLE.value:
return "INVISIBLE";
case View.GONE.value:
return "GONE";
}
}
function viewToString(view) {
let TextView = Java.use("android.widget.TextView");
let ImageView = Java.use("android.widget.ImageView");
let EditText = Java.use("android.widget.EditText");
let Button = Java.use("android.widget.Button");
let System = Java.use("java.lang.System");
let className = view.$className;
let hashCode = System.identityHashCode(view);
let id = view.getId();
let visibility = view.getVisibility();
let width = view.getWidth();
let height = view.getHeight();
let x = view.getX();
let y = view.getY();
let clickable = view.isClickable();
let enabled = view.isEnabled();
let focused = view.isFocused();
let contentDescription = view.getContentDescription();
let text = "";
if (isAssignableFrom(view, TextView.class)) {
let textView = Java.cast(view, TextView);
text = textView.getText().toString();
} else if (isAssignableFrom(view, EditText.class)) {
let editText = Java.cast(view, EditText);
text = editText.getText().toString();
} else if (isAssignableFrom(view, Button.class)) {
let button = Java.cast(view, Button);
text = button.getText().toString();
} else if (isAssignableFrom(view, ImageView.class)) {
let imageView = Java.cast(view, ImageView);
text = "[ImageView]";
}
if (contentDescription) {
contentDescription = contentDescription.toString();
} else {
contentDescription = "";
}
if (!text) {
text = "";
}
// get output and ignore empty values
let parts = [];
parts.push(`Class: ${className}`);
parts.push(`Hash: 0x${(hashCode >>> 0).toString(16)}`);
if (id !== -1) {
// try to get resource name
let resName = "";
try {
let context = view.getContext();
let resources = context.getResources();
resName = resources.getResourceEntryName(id);
} catch (e) {
resName = "";
}
if (resName) {
parts.push(`ID: ${resName}`);
} else {
parts.push(`ID: ${id}`);
}
}
parts.push(`Visibility: ${visibilityToString(visibility)}`);
parts.push(`Position: (${x}, ${y})`);
parts.push(`Size: (${width}x${height})`);
parts.push(`Clickable: ${clickable}`);
parts.push(`Enabled: ${enabled}`);
parts.push(`Focused: ${focused}`);
if (contentDescription) {
parts.push(`ContentDescription: "${contentDescription}"`);
}
if (text) {
parts.push(`Text: "${text}"`);
}
return parts.join(", ");
}
function getViewsByType(views, type) {
let clazz = Java.use(type);
return views.filter(view => isAssignableFrom(view, clazz.class));
}
function treeToString(node, depth) {
let indent = ' '.repeat(depth);
let levelInfo = `[L${depth}]`;
let lines = [indent + levelInfo + ' ' + viewToString(node.view)];
if (node.children && node.children.length > 0) {
for (let i = 0; i < node.children.length; i++) {
lines.push(treeToString(node.children[i], depth + 1));
}
}
return lines.join('\n');
}
function dumpViewHierarchy() {
let viewRoot = findFrontMostVisibleViewRoot();
if (!viewRoot) {
send({ type: 'error', message: 'No visible view found.' });
return;
}
let tree = buildViewTree(viewRoot);
let treeStr = treeToString(tree, 0);
send({ type: 'info', message: 'View Hierarchy (tree):\n' + treeStr });
send({ type: 'finish', message: 'Dump finished' });
}
function dumpViewsByType(type) {
let viewRoot = findFrontMostVisibleViewRoot();
if (!viewRoot) {
send({ type: 'error', message: 'No visible view found.' });
return;
}
let allViews = buildViewList(viewRoot);
let filteredViews = getViewsByType(allViews, type);
if (filteredViews.length === 0) {
send({ type: 'info', message: `No views found of type ${type}.` });
return;
}
let viewStrings = filteredViews.map(view => viewToString(view));
let result = viewStrings.join('\n');
send({ type: 'info', message: `Views of type ${type}:\n` + result });
send({ type: 'finish', message: `Found ${filteredViews.length} views of type ${type}.` });
}
function findMatchingOverload(overloads, args) {
function convertArg(arg, typeName) {
if (typeName === 'int' || typeName === 'java.lang.Integer') {
if (typeof arg === 'number') return Math.trunc(arg);
}
if (typeName === 'float' || typeName === 'java.lang.Float') {
if (typeof arg === 'number') return arg;
}
if (typeName === 'double' || typeName === 'java.lang.Double') {
if (typeof arg === 'number') return arg;
}
if (typeName === 'boolean' || typeName === 'java.lang.Boolean') {
if (typeof arg === 'boolean') return arg;
if (typeof arg === 'number') return arg !== 0;
}
if (typeName === 'java.lang.String') {
if (typeof arg === 'string') return arg;
if (typeof arg === 'number' || typeof arg === 'boolean') {
let String = Java.use('java.lang.String');
return String.$new(arg);
}
}
if (typeName === 'java.lang.CharSequence') {
// Accept string, number, boolean, or Java String
if (typeof arg === 'string') return arg;
if (typeof arg === 'number' || typeof arg === 'boolean') {
let String = Java.use('java.lang.String');
return String.$new(arg);
}
// Accept Java String or CharSequence
if (arg && typeof arg.$className === 'string' && (arg.$className === 'java.lang.String' || arg.$className === 'java.lang.CharSequence')) {
return arg;
}
}
// Allow null/undefined for any reference type
if ((arg === null || arg === undefined) && !typeName.match(/^(int|float|double|boolean)$/)) {
return null;
}
// Fallback: allow any for object
if (typeName === 'java.lang.Object') {
return arg;
}
// If not matched, return a special marker
return Symbol('no-match');
}
overloadLoop:
for (let i = 0; i < overloads.length; i++) {
let o = overloads[i];
if (o.argumentTypes.length !== args.length) continue;
let convertedArgs = [];
for (let j = 0; j < args.length; j++) {
let typeName = o.argumentTypes[j].className;
let conv = convertArg(args[j], typeName);
if (typeof conv === 'symbol') {
continue overloadLoop;
}
convertedArgs.push(conv);
}
return { overload: o, convertedArgs };
}
return null;
}
function callViewMethod(viewHash, methodName, ...args) {
let System = Java.use("java.lang.System");
let viewRoot = findFrontMostVisibleViewRoot();
if (!viewRoot) {
send({ type: 'error', message: 'No visible view found.' });
send({ type: 'finish', message: 'Method call finished.' });
return;
}
let allViews = buildViewList(viewRoot);
let targetView = allViews.find(v => System.identityHashCode(v) === viewHash);
if (!targetView) {
send({ type: 'error', message: `No view found with hash 0x${(viewHash >>> 0).toString(16)}` });
send({ type: 'finish', message: 'Method call finished.' });
return;
}
try {
args = args.filter(arg => arg !== undefined && arg !== null);
send({ type: 'debug', message: `args: ${JSON.stringify(args)}, args.length: ${args.length}` });
send({ type: 'debug', message: `Looking for method ${methodName} with args: ${args}` });
targetView = Java.cast(targetView, Java.use(targetView.$className));
const methodOverloads = targetView[methodName].overloads;
const match = findMatchingOverload(methodOverloads, args);
if (!match) {
let overloadSigs = methodOverloads.map(o => o.argumentTypes.map(t => t.className).join(", ")).join(" | ");
send({ type: 'error', message: `Method ${methodName} with ${args.length} arguments not found. Overloads: ${overloadSigs}` });
send({ type: 'finish', message: 'Method call finished.' });
return;
}
Java.scheduleOnMainThread(() => {
try {
let result = match.overload.apply(targetView, match.convertedArgs);
send({ type: 'info', message: `Method ${methodName} called on view. Result: ${result}` });
} catch (e) {
send({ type: 'error', message: `Error calling method ${methodName} on main thread: ${e.message}` });
}
send({ type: 'finish', message: 'Method call finished.' });
});
return;
} catch (e) {
send({ type: 'error', message: `Error calling method ${methodName}: ${e.message}` });
}
send({ type: 'finish', message: 'Method call finished.' });
}
rpc.exports = {
dumpviewhierarchy: dumpViewHierarchy,
dumpviewsbytype: dumpViewsByType,
callviewmethod: callViewMethod
};