-
Notifications
You must be signed in to change notification settings - Fork 7
/
AccessibilityUtil2
660 lines (624 loc) · 22 KB
/
AccessibilityUtil2
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
import android.accessibilityservice.AccessibilityService
import android.accessibilityservice.GestureDescription
import android.annotation.TargetApi
import android.app.Notification
import android.app.PendingIntent
import android.graphics.Path
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.accessibility.AccessibilityEvent
import android.view.accessibility.AccessibilityNodeInfo
import com.blankj.utilcode.util.LogUtils
import java.lang.Exception
import java.lang.Thread.sleep
/**
* 1.查询类
* findOneByClazz 按类名寻找节点和子节点内的一个匹配项
* findAllByClazz 按类名寻找节点和子节点内的所有匹配项
* findFrontNode 查找节点的前兄弟节点
* findBackNode 查找节点的后兄弟节点
* findCanScrollNode 返回可滚动元素集合
* findOneByDesc 按描述寻找节点和子节点内的一个匹配项
* findOneByText 按文本(关键词)寻找节点和子节点内的一个匹配项
* findAllByText 按文本(关键词)寻找节点和子节点内的所有匹配项
*
* 2.全局操作
* globalGoBack 回退
* globalGoHome 回桌面
*
* 3.窗口操作
* printNodeClazzTree 深度搜索打印节点及其子节点
* performScrollUp 对某个节点向上滚动 未生效
* performScrollDown 对某个节点向下滚动 未生效
* performClick 对某个节点进行点击
* performXYClick 输入x, y坐标模拟点击事件
* editTextInput 编辑EditView(非粘贴 推荐)
* findTextAndClick 寻找第一个文本匹配(关键词)并点击
* findTextInput 寻找第一个EditView编辑框并输入文本
* findListOneAndClick 寻找第一个列表并点击指定条目(默认点击第一个条目)
* scrollAndFindByText 滚动并按文本寻找第一个控件
* performClickWithSon 对某个节点或子节点进行点击
* performLongClick 对某个节点或父节点进行长按
* performLongClickWithSon 对某个节点或子节点进行长按
*
*/
object AccessibilityUtil2 {
private const val tag = "AccessibilityUtil"
private const val SHORT_INTERVAL = 100L
private const val SCROLL_INTERVAL = 300L
//需要设置service以获取窗口
public var service: AccessibilityService? = null
//编辑EditView(非粘贴 推荐)
fun editTextInput(nodeInfo: AccessibilityNodeInfo?, text: String): Boolean {
val nodeInfo: AccessibilityNodeInfo = nodeInfo ?: return false
val arguments = Bundle()
arguments.putCharSequence(
AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE,
text
)
nodeInfo.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments)
return true
}
//寻找第一个文本匹配(关键词)并点击
fun findTextAndClick(nodeInfo: AccessibilityNodeInfo?, text: String): Boolean {
val textView = findOneByText(nodeInfo, text) ?: return false
return performClick(textView)
}
//寻找第一个EditView编辑框并输入文本
fun findTextInput(nodeInfo: AccessibilityNodeInfo?, text: String, root: Boolean = true): Boolean {
if (root) {
val editText = findOneByClazz(nodeInfo, "android.widget.EditText") ?: return false
val arguments = Bundle()
arguments.putCharSequence(
AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE,
text
)
editText.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments)
} else {
val editText = findOnceByClazz(nodeInfo, "android.widget.EditText") ?: return false
val arguments = Bundle()
arguments.putCharSequence(
AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE,
text
)
editText.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments)
}
return true
}
//寻找第一个列表并点击指定条目(默认点击第一个条目)
fun findListOneAndClick(nodeInfo: AccessibilityNodeInfo, index: Int = 0): Boolean {
val rv = findOnceByClazz(nodeInfo, "androidx.recyclerview.widget.RecyclerView")
val lv = findOnceByClazz(nodeInfo, "android.widget.ListView")
if (rv == null && lv == null) return false
if (rv != null && rv.childCount > index) {
performClick(rv.getChild(index))
} else if (lv != null && lv.childCount > index) {
performClick(lv.getChild(index))
}
return true
}
//滚动并按文本寻找第一个控件
fun scrollAndFindByText(
nodeInfo: AccessibilityNodeInfo,
text: String,
maxRetry: Int = 3
): AccessibilityNodeInfo? {
var index = 0
while (index++ < maxRetry) {
performScrollUp(nodeInfo, 0)
val node = findOnceByText(nodeInfo, text)
if (node != null) {
return node
}
}
while (index++ < maxRetry * 2) {
performScrollDown(nodeInfo, 0)
val node = findOnceByText(nodeInfo, text)
if (node != null) {
return node
}
}
return null
}
//输入x, y坐标模拟点击事件
@TargetApi(Build.VERSION_CODES.N)
fun performXYClick(service: AccessibilityService, x: Float, y: Float) {
val path = Path()
path.moveTo(x, y)
val builder = GestureDescription.Builder()
builder.addStroke(GestureDescription.StrokeDescription(path, 0, 1))
val gestureDescription = builder.build()
service.dispatchGesture(
gestureDescription,
object : AccessibilityService.GestureResultCallback() {
override fun onCompleted(gestureDescription: GestureDescription) {
super.onCompleted(gestureDescription)
//Log.i(Constant.TAG, "onCompleted: completed");
}
override fun onCancelled(gestureDescription: GestureDescription) {
super.onCancelled(gestureDescription)
//Log.i(Constant.TAG, "onCancelled: cancelled");
}
},
null
)
}
/**
* 对某个节点或父节点进行点击
*/
fun performClick(nodeInfo: AccessibilityNodeInfo?): Boolean {
var nodeInfo: AccessibilityNodeInfo? = nodeInfo ?: return false
while (nodeInfo != null) {
if (nodeInfo.isClickable) {
nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK)
return true
}
nodeInfo = nodeInfo.parent
}
return false
}
/**
* 对某个节点或子节点进行点击
*/
fun performClickWithSon(nodeInfo: AccessibilityNodeInfo?): Boolean {
var nodeInfo: AccessibilityNodeInfo? = nodeInfo ?: return false
while (nodeInfo != null) {
if (nodeInfo.isClickable) {
nodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK)
return true
}
if (nodeInfo.childCount > 0) {
for (i in 0 until nodeInfo.childCount) {
if (performClickWithSon(nodeInfo.getChild(i))) {
return true
}
}
} else {
nodeInfo = null
}
}
return false
}
/**
* 对某个节点或父节点进行长按
*/
fun performLongClick(nodeInfo: AccessibilityNodeInfo?): Boolean {
var nodeInfo: AccessibilityNodeInfo? = nodeInfo ?: return false
while (nodeInfo != null) {
if (nodeInfo.isLongClickable) {
nodeInfo.performAction(AccessibilityNodeInfo.ACTION_LONG_CLICK)
return true
}
nodeInfo = nodeInfo.parent
}
return false
}
/**
* 对某个节点或子节点进行长按
*/
fun performLongClickWithSon(nodeInfo: AccessibilityNodeInfo?): Boolean {
var nodeInfo: AccessibilityNodeInfo? = nodeInfo ?: return false
while (nodeInfo != null) {
if (nodeInfo.isLongClickable) {
nodeInfo.performAction(AccessibilityNodeInfo.ACTION_LONG_CLICK)
return true
}
if (nodeInfo.childCount > 0) {
for (i in 0 until nodeInfo.childCount) {
if (performLongClickWithSon(nodeInfo.getChild(i))) {
return true
}
}
} else {
nodeInfo = null
}
}
return false
}
//对某个节点向上滚动
fun performScrollUp(nodeInfo: AccessibilityNodeInfo?): Boolean {
var nodeInfo: AccessibilityNodeInfo? = nodeInfo ?: return false
while (nodeInfo != null) {
if (nodeInfo.isScrollable) {
nodeInfo.performAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD)
return true
}
nodeInfo = nodeInfo.parent
}
return false
}
//对某个节点或父节点向下滚动
fun performScrollDown(node: AccessibilityNodeInfo?): Boolean {
var node: AccessibilityNodeInfo? = node ?: return false
while (node != null) {
if (node.isScrollable) {
node.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD)
return true
}
node = node.parent
}
return false
}
//对第几个节点向上滚动
fun performScrollUp(node: AccessibilityNodeInfo?, index: Int): Boolean {
if (node == null) return false
val canScrollNodeList = findCanScrollNode(node)
if (canScrollNodeList.size > index) {
canScrollNodeList[index].performAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD)
sleep(SCROLL_INTERVAL)
return true
}
return false
}
//对第几个节点向下滚动
fun performScrollDown(node: AccessibilityNodeInfo?, index: Int): Boolean {
if (node == null) return false
val canScrollNodeList = findCanScrollNode(node)
if (canScrollNodeList.size > index) {
canScrollNodeList[index].performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD)
sleep(SCROLL_INTERVAL)
return true
}
return false
}
//返回可滚动元素集合
fun findCanScrollNode(
node: AccessibilityNodeInfo?,
list: ArrayList<AccessibilityNodeInfo> = ArrayList()
): ArrayList<AccessibilityNodeInfo> {
if (node == null) return list
if (node.isScrollable) list.add(node)
for (i in 0 until node.childCount) {
findCanScrollNode(node.getChild(i), list)
}
return list
}
//通知栏事件进入应用
fun gotoApp(event: AccessibilityEvent) {
val data = event.parcelableData
if (data != null && data is Notification) {
val intent = data.contentIntent
try {
intent.send()
} catch (e: PendingIntent.CanceledException) {
e.printStackTrace()
}
}
}
//回退
fun globalGoBack(service: AccessibilityService) {
service.performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK)
}
//回首页
fun globalGoHome(service: AccessibilityService) {
service.performGlobalAction(AccessibilityService.GLOBAL_ACTION_HOME)
}
/**
* 按描述寻找节点和子节点内的一个匹配项
* @param node 节点
* @param desc 描述
* @param timeout 检查超时时间
*/
fun findOneByDesc(
node: AccessibilityNodeInfo?,
desc: String,
timeout: Long = 5000
): AccessibilityNodeInfo? {
var node = node ?: return null
val description = node.contentDescription?.toString()
if (description == desc) {
return node
}
val startTime = System.currentTimeMillis()
var currentTime = startTime
while (currentTime - startTime <= timeout) {
val result = findOnceByDesc(node, desc)
if (result != null) return result
sleep(SHORT_INTERVAL)
node = getRoot()
currentTime = System.currentTimeMillis()
}
Log.e(tag, "findOneByDesc: not found: $desc")
return null
}
fun findOnceByDesc(
node: AccessibilityNodeInfo?,
desc: String
): AccessibilityNodeInfo? {
if (node == null) return null
val description = node.contentDescription?.toString()
if (description == desc) {
return node
}
for (i in 0 until node.childCount) {
val result = findOnceByDesc(node.getChild(i), desc)
if (result != null) return result
}
return null
}
/**
* 按文本(关键词)寻找节点和子节点内的一个匹配项
* @param node 节点
* @param text 关键词
* @param timeout 检查超时时间
*/
fun findOneByText(
node: AccessibilityNodeInfo?,
text: String,
exact: Boolean = false,
timeout: Long = 5000,
root: Boolean = true
): AccessibilityNodeInfo? {
var node = node ?: return null
val startTime = System.currentTimeMillis()
var currentTime = startTime
while (currentTime - startTime <= timeout) {
val textViewList = node.findAccessibilityNodeInfosByText(text)
LogUtils.v("text: $text count: " + textViewList.size)
if (textViewList != null && textViewList.size > 0) {
for (textView in textViewList) {
if (textView.text == text) {
return textView
}
}
if (!exact) {
return textViewList[0]
}
}
sleep(SHORT_INTERVAL)
if (root) {
node = getRoot()
} else {
node.refresh()
}
currentTime = System.currentTimeMillis()
}
Log.e(tag, "findOneByText: not found: $text")
return null
}
fun findOnceByText(
node: AccessibilityNodeInfo?,
text: String,
exact: Boolean = false
): AccessibilityNodeInfo? {
return findOneByText(node, text, exact, 0)
}
/**
* 按文本(关键词)寻找节点和子节点内的所有匹配项
* @param node 节点
* @param text 关键词
* @param timeout 检查超时时间
*/
fun findAllByText(
node: AccessibilityNodeInfo?,
text: String,
exact: Boolean = false,
timeout: Long = 5000,
root: Boolean = true
): List<AccessibilityNodeInfo> {
var node = node ?: return arrayListOf()
val startTime = System.currentTimeMillis()
var currentTime = startTime
while (currentTime - startTime <= timeout) {
val tvList = node.findAccessibilityNodeInfosByText(text)
if (tvList != null && tvList.size > 0) {
if (!exact) {
return tvList
} else if (tvList.count { it.text == text } > 0) {
return tvList.filter { it.text == text }
}
}
sleep(SHORT_INTERVAL)
if (root) {
node = getRoot()
} else {
node.refresh()
}
currentTime = System.currentTimeMillis()
}
Log.e(tag, "findAllByText: not found: $text")
return arrayListOf()
}
/**
* 按类名寻找节点和子节点内的一个匹配项
* node 节点
* clazz 类名
* limitDepth 深度 限制深度搜索深度必须匹配提供值且类名相同才返回 不填默认不限制
* timeout 检查超时时间
*/
fun findOneByClazz(
node: AccessibilityNodeInfo?,
clazz: String,
limitDepth: Int? = null,
depth: Int = 0,
timeout: Long = 5000,
root: Boolean = true
): AccessibilityNodeInfo? {
var node = node ?: return null
if (node.className == clazz) {
if (limitDepth == null || limitDepth == depth)
return node
}
val startTime = System.currentTimeMillis()
var currentTime = startTime
while (currentTime - startTime <= timeout) {
val result = findOnceByClazz(node, clazz, limitDepth, depth)
LogUtils.v("clazz: $clazz result == null: ${result == null}")
if (result != null) return result
sleep(SHORT_INTERVAL)
if (root) {
node = getRoot()
} else {
node.refresh()
}
currentTime = System.currentTimeMillis()
}
LogUtils.e("findOneByClazz Exception()")
Exception().printStackTrace()
return null
}
/**
* 按类名寻找节点和子节点内的一个匹配项
* node 节点
* clazz 类名
* limitDepth 深度 限制深度搜索深度必须匹配提供值且类名相同才返回 不填默认不限制
*/
fun findOnceByClazz(
node: AccessibilityNodeInfo?,
clazz: String,
limitDepth: Int? = null,
depth: Int = 0
): AccessibilityNodeInfo? {
if (node == null) return null
if (node.className == clazz) {
if (limitDepth == null || limitDepth == depth)
return node
}
for (i in 0 until node.childCount) {
val result = findOnceByClazz(node.getChild(i), clazz, limitDepth, depth + 1)
if (result != null) return result
}
return null
}
/**
* 按类名寻找节点和子节点内的所有匹配项
* node 节点
* clazz 类名
* limitDepth 深度 限制深度搜索深度必须匹配提供值且类名相同才返回 不填默认不限制
*/
fun findAllByClazz(
node: AccessibilityNodeInfo?,
clazz: String,
list: ArrayList<AccessibilityNodeInfo> = ArrayList(),
timeout: Long = 5000,
root: Boolean = true,
minSize: Int = 1
): ArrayList<AccessibilityNodeInfo> {
var node = node ?: return list
val startTime = System.currentTimeMillis()
var currentTime = startTime
while (currentTime - startTime <= timeout) {
val result = findAllOnceByClazz(node, clazz)
LogUtils.v("clazz: $clazz count: " + result.size)
if (result.size >= minSize) return result
sleep(SHORT_INTERVAL)
if (root) {
node = getRoot()
} else {
node.refresh()
}
currentTime = System.currentTimeMillis()
}
LogUtils.e("findAllByClazz Exception()")
Exception().printStackTrace()
return list
}
/**
* 按类名寻找节点和子节点内的所有匹配项
* node 节点
* clazz 类名
* limitDepth 深度 限制深度搜索深度必须匹配提供值且类名相同才返回 不填默认不限制
*/
fun findAllOnceByClazz(
node: AccessibilityNodeInfo?,
clazz: String,
list: ArrayList<AccessibilityNodeInfo> = ArrayList()
): ArrayList<AccessibilityNodeInfo> {
if (node == null) return list
if (node.className == clazz) list.add(node)
for (i in 0 until node.childCount) {
findAllOnceByClazz(node.getChild(i), clazz, list)
}
return list
}
/**
* 查找节点的前兄弟节点
* node 节点
*/
fun findFrontNode(node: AccessibilityNodeInfo?): AccessibilityNodeInfo? {
if (node == null) return null
var parent: AccessibilityNodeInfo? = node.parent
var son: AccessibilityNodeInfo? = node
while (parent != null) {
var index = -1
for (i in 0 until parent.childCount) {
if (parent.getChild(i) == son) {
index = i
break
}
}
if (index > 0) {
return parent.getChild(index - 1)
}
son = parent
parent = parent.parent
}
return null
}
/**
* 查找节点的后兄弟节点
* node 节点
*/
fun findBackNode(node: AccessibilityNodeInfo?): AccessibilityNodeInfo? {
if (node == null) return null
var parent: AccessibilityNodeInfo? = node.parent
var son: AccessibilityNodeInfo? = node
while (parent != null) {
var index = -1
for (i in 0 until parent.childCount) {
if (parent.getChild(i) == son) {
index = i
break
}
}
if (index < parent.childCount - 1) {
return parent.getChild(index + 1)
}
son = parent
parent = parent.parent
}
return null
}
/**
* 深度搜索打印节点及其子节点
* node 节点
*/
fun printNodeClazzTree(
node: AccessibilityNodeInfo?,
printText: Boolean = true,
depth: Int = 0
) {
if (node == null) return
var s = ""
for (i in 0 until depth) {
s += "---"
}
Log.d(tag, "$s depth: $depth className: " + node.className)
if (printText && node.text != null) {
Log.d(tag, "$s depth: $depth text: " + node.text)
}
if (printText && node.contentDescription != null) {
Log.d(tag, "$s depth: $depth desc: " + node.contentDescription)
}
for (i in 0 until node.childCount) {
printNodeClazzTree(node.getChild(i), printText, depth + 1)
}
}
/**
* 获取前台窗口
*/
fun getRoot(): AccessibilityNodeInfo {
while (true) {
val tempRoot = service?.rootInActiveWindow
val root = service?.rootInActiveWindow
if (tempRoot != root) {
LogUtils.e("tempRoot != root")
}
if (root != null) {
return root
}
sleep(1000)
}
}
}