assistsx-js 0.0.2048 → 0.0.2049

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/AssistsX.ts CHANGED
@@ -15,22 +15,22 @@ import { DeviceInfo } from "./DeviceInfo";
15
15
  * 无障碍事件数据结构
16
16
  */
17
17
  export interface AccessibilityEventData {
18
- packageName: string;
19
- className: string;
20
- eventType: number;
21
- action: number;
22
- texts: string[];
23
- node: Node | null;
18
+ packageName: string;
19
+ className: string;
20
+ eventType: number;
21
+ action: number;
22
+ texts: string[];
23
+ node: Node | null;
24
24
  }
25
25
 
26
26
  /**
27
27
  * 无障碍事件完整结构
28
28
  */
29
29
  export interface AccessibilityEvent {
30
- callbackId: string;
31
- code: number;
32
- data: AccessibilityEventData;
33
- message: string;
30
+ callbackId: string;
31
+ code: number;
32
+ data: AccessibilityEventData;
33
+ message: string;
34
34
  }
35
35
 
36
36
  /**
@@ -42,34 +42,34 @@ export type AccessibilityEventListener = (event: AccessibilityEvent) => void;
42
42
  * Web浮动窗口选项接口定义
43
43
  */
44
44
  export interface WebFloatingWindowOptions {
45
- initialWidth?: number;
46
- initialHeight?: number;
47
- minWidth?: number;
48
- minHeight?: number;
49
- maxWidth?: number;
50
- maxHeight?: number;
51
- initialCenter?: boolean;
45
+ initialWidth?: number;
46
+ initialHeight?: number;
47
+ minWidth?: number;
48
+ minHeight?: number;
49
+ maxWidth?: number;
50
+ maxHeight?: number;
51
+ initialCenter?: boolean;
52
52
  }
53
53
 
54
54
  /**
55
55
  * HTTP请求选项接口定义
56
56
  */
57
57
  export interface HttpRequestOptions {
58
- url: string;
59
- method?: "GET" | "POST";
60
- headers?: Record<string, string>;
61
- body?: string;
62
- timeout?: number;
58
+ url: string;
59
+ method?: "GET" | "POST";
60
+ headers?: Record<string, string>;
61
+ body?: string;
62
+ timeout?: number;
63
63
  }
64
64
 
65
65
  /**
66
66
  * HTTP响应数据接口定义
67
67
  */
68
68
  export interface HttpResponse {
69
- statusCode: number;
70
- statusMessage: string;
71
- body: string;
72
- headers: Record<string, string>;
69
+ statusCode: number;
70
+ statusMessage: string;
71
+ body: string;
72
+ headers: Record<string, string>;
73
73
  }
74
74
 
75
75
  // 回调函数存储对象
@@ -80,941 +80,963 @@ export const accessibilityEventListeners: AccessibilityEventListener[] = [];
80
80
 
81
81
  // 初始化全局回调函数
82
82
  if (typeof window !== "undefined" && !window.assistsxCallback) {
83
- window.assistsxCallback = (data: string) => {
84
- let callbackId: string | undefined;
85
- try {
86
- const json = decodeBase64UTF8(data);
87
- const response = JSON.parse(json);
88
- callbackId = response.callbackId;
89
- if (callbackId) {
90
- const callback = callbacks.get(callbackId);
91
- if (callback) {
92
- callback(json);
83
+ window.assistsxCallback = (data: string) => {
84
+ let callbackId: string | undefined;
85
+ try {
86
+ const json = decodeBase64UTF8(data);
87
+ const response = JSON.parse(json);
88
+ callbackId = response.callbackId;
89
+ if (callbackId) {
90
+ const callback = callbacks.get(callbackId);
91
+ if (callback) {
92
+ callback(json);
93
+ }
94
+ }
95
+ } catch (e) {
96
+ console.log(e);
97
+ } finally {
98
+ // 无论成功还是失败,都删除回调函数
99
+ if (callbackId) {
100
+ callbacks.delete(callbackId);
101
+ }
93
102
  }
94
- }
95
- } catch (e) {
96
- console.log(e);
97
- } finally {
98
- // 无论成功还是失败,都删除回调函数
99
- if (callbackId) {
100
- callbacks.delete(callbackId);
101
- }
102
- }
103
- };
103
+ };
104
104
  }
105
105
 
106
106
  // 初始化全局无障碍事件函数
107
107
  if (typeof window !== "undefined" && !window.onAccessibilityEvent) {
108
- window.onAccessibilityEvent = (event: string) => {
109
- // 通知所有注册的监听器,每个监听器都有独立的错误处理
110
- accessibilityEventListeners.forEach((listener) => {
111
- try {
112
- //base64 decode
113
- const decodedEvent = decodeBase64UTF8(event);
114
- const parsedEvent: AccessibilityEvent = JSON.parse(decodedEvent);
115
- listener(parsedEvent);
116
- } catch (error) {
117
- console.error("Accessibility event listener error:", error);
118
- }
119
- });
120
- };
108
+ window.onAccessibilityEvent = (event: string) => {
109
+ // 通知所有注册的监听器,每个监听器都有独立的错误处理
110
+ accessibilityEventListeners.forEach((listener) => {
111
+ try {
112
+ //base64 decode
113
+ const decodedEvent = decodeBase64UTF8(event);
114
+ const parsedEvent: AccessibilityEvent = JSON.parse(decodedEvent);
115
+ listener(parsedEvent);
116
+ } catch (error) {
117
+ console.error("Accessibility event listener error:", error);
118
+ }
119
+ });
120
+ };
121
121
  }
122
122
 
123
123
  export class AssistsX {
124
- /**
125
- * 执行同步调用
126
- * @param method 方法名
127
- * @param args 参数对象
128
- * @returns 调用响应
129
- */
130
- public static call(
131
- method: string,
132
- { args, node }: { args?: any; node?: Node } = {}
133
- ): CallResponse {
134
- const params = {
135
- method,
136
- arguments: args ? args : undefined,
137
- node: node ? node : undefined,
138
- };
139
- const result = window.assistsx.call(JSON.stringify(params));
140
- if (typeof result === "string") {
141
- const responseData = JSON.parse(result);
142
- const response = new CallResponse(
143
- responseData.code,
144
- responseData.data,
145
- responseData.callbackId
146
- );
147
- return response;
148
- }
149
- throw new Error("Call failed");
150
- }
151
-
152
- /**
153
- * 执行异步调用
154
- * @param method 方法名
155
- * @param args 参数对象
156
- * @param timeout 超时时间(秒),默认30秒
157
- * @returns Promise<调用响应>
158
- */
159
- public static async asyncCall(
160
- method: string,
161
- {
162
- args,
163
- node,
164
- nodes,
165
- timeout = 30,
166
- }: { args?: any; node?: Node; nodes?: Node[]; timeout?: number } = {}
167
- ): Promise<CallResponse> {
168
- const uuid = generateUUID();
169
- const params = {
170
- method,
171
- arguments: args ? args : undefined,
172
- node: node ? node : undefined,
173
- nodes: nodes ? nodes : undefined,
174
- callbackId: uuid,
175
- };
176
- const promise = new Promise((resolve) => {
177
- callbacks.set(uuid, (data: string) => {
178
- resolve(data);
179
- });
180
- setTimeout(() => {
181
- // 超时后删除回调函数
182
- callbacks.delete(uuid);
183
- resolve(new CallResponse(0, null, uuid));
184
- }, timeout * 1000);
185
- });
186
- const result = window.assistsx.call(JSON.stringify(params));
187
- const promiseResult = await promise;
188
- if (typeof promiseResult === "string") {
189
- const responseData = JSON.parse(promiseResult);
190
- const response = new CallResponse(
191
- responseData.code,
192
- responseData.data,
193
- responseData.callbackId
194
- );
195
- return response;
196
- }
197
- throw new Error("Call failed");
198
- }
199
-
200
- /**
201
- * 设置悬浮窗标志
202
- * @param flags 标志
203
- * @returns 是否设置成功
204
- */
205
- public static setOverlayFlags(flags: number): boolean {
206
- const response = this.call(CallMethod.setOverlayFlags, {
207
- args: { flags: flags },
208
- });
209
- return response.getDataOrDefault(false);
210
- }
211
- /**
212
- * 设置悬浮窗标志
213
- * @param flags 标志
214
- * @returns 是否设置成功
215
- */
216
- public static setOverlayFlagList(flags: number[]): boolean {
217
- const response = this.call(CallMethod.setOverlayFlags, {
218
- args: { flags: flags },
219
- });
220
- return response.getDataOrDefault(false);
221
- }
222
- /**
223
- * 获取所有符合条件的节点
224
- * @param filterClass 类名过滤
225
- * @param filterViewId 视图ID过滤
226
- * @param filterDes 描述过滤
227
- * @param filterText 文本过滤
228
- * @returns 节点数组
229
- */
230
- public static getAllNodes({
231
- filterClass,
232
- filterViewId,
233
- filterDes,
234
- filterText,
235
- }: {
236
- filterClass?: string;
237
- filterViewId?: string;
238
- filterDes?: string;
239
- filterText?: string;
240
- } = {}): Node[] {
241
- const response = this.call(CallMethod.getAllNodes, {
242
- args: { filterClass, filterViewId, filterDes, filterText },
243
- });
244
- return Node.fromJSONArray(response.getDataOrDefault("[]"));
245
- }
246
-
247
- /**
248
- * 设置节点文本
249
- * @param node 目标节点
250
- * @param text 要设置的文本
251
- * @returns 是否设置成功
252
- */
253
- public static setNodeText(node: Node, text: string): boolean {
254
- const response = this.call(CallMethod.setNodeText, {
255
- args: { text },
256
- node,
257
- });
258
- return response.getDataOrDefault(false);
259
- }
260
- /**
261
- * 获取剪贴板最新文本
262
- * @returns 剪贴板最新文本
263
- */
264
- public static getClipboardLatestText(): any {
265
- const response = this.call(CallMethod.getClipboardLatestText);
266
- return response.getDataOrDefault({});
267
- }
268
-
269
- /**
270
- * 在浏览器中打开URL
271
- * @param url 要打开的URL
272
- * @returns 是否成功打开
273
- */
274
- public static openUrlInBrowser(url: string): boolean {
275
- const response = this.call(CallMethod.openUrlInBrowser, {
276
- args: { url },
277
- });
278
- return response.getDataOrDefault(false);
279
- }
280
-
281
- /**
282
- * 保持屏幕常亮
283
- * @param tip 提示文本
284
- * @returns 是否保持屏幕常亮成功
285
- */
286
- public static keepScreenOn(tip?: string): boolean {
287
- const response = this.call(CallMethod.keepScreenOn, {
288
- args: { tip },
289
- });
290
- return response.getDataOrDefault(false);
291
- }
292
- public static clearKeepScreenOn(): boolean {
293
- const response = this.call(CallMethod.clearKeepScreenOn, {});
294
- return response.getDataOrDefault(false);
295
- }
296
- public static isAppInstalled(packageName: string): boolean {
297
- const response = this.call(CallMethod.isAppInstalled, {
298
- args: { packageName },
299
- });
300
- return response.getDataOrDefault(false);
301
- }
302
-
303
- /**
304
- * 对指定节点进行截图
305
- * @param nodes 要截图的节点数组
306
- * @param overlayHiddenScreenshotDelayMillis 截图延迟时间(毫秒)
307
- * @param timeout 超时时间(秒),默认30秒
308
- * @returns 截图路径数组
309
- */
310
- public static async takeScreenshotNodes(
311
- nodes: Node[],
312
- overlayHiddenScreenshotDelayMillis: number = 250,
313
- timeout?: number
314
- ): Promise<string[]> {
315
- const response = await this.asyncCall(CallMethod.takeScreenshot, {
316
- nodes,
317
- args: { overlayHiddenScreenshotDelayMillis },
318
- timeout,
319
- });
320
- const data = response.getDataOrDefault("");
321
- return data.images;
322
- }
323
- public static async scanQR(timeout?: number): Promise<string> {
324
- const response = await this.asyncCall(CallMethod.scanQR, { timeout });
325
- const data = response.getDataOrDefault({ value: "" });
326
- return data.value;
327
- }
328
- public static async loadWebViewOverlay(
329
- url: string,
330
- options: WebFloatingWindowOptions & { timeout?: number } = {}
331
- ): Promise<any> {
332
- const {
333
- initialWidth,
334
- initialHeight,
335
- minWidth,
336
- minHeight,
337
- maxWidth,
338
- maxHeight,
339
- initialCenter,
340
- timeout,
341
- } = options;
342
- const response = await this.asyncCall(CallMethod.loadWebViewOverlay, {
343
- args: {
344
- url,
345
- initialWidth,
346
- initialHeight,
347
- minWidth,
348
- minHeight,
349
- maxWidth,
350
- maxHeight,
351
- initialCenter,
352
- },
353
- timeout,
354
- });
355
- const data = response.getDataOrDefault({});
356
- return data;
357
- }
358
-
359
- /**
360
- * 点击节点
361
- * @param node 要点击的节点
362
- * @returns 是否点击成功
363
- */
364
- public static click(node: Node): boolean {
365
- const response = this.call(CallMethod.click, { node });
366
- return response.getDataOrDefault(false);
367
- }
368
-
369
- /**
370
- * 长按节点
371
- * @param node 要长按的节点
372
- * @returns 是否长按成功
373
- */
374
- public static longClick(node: Node): boolean {
375
- const response = this.call(CallMethod.longClick, { node });
376
- return response.getDataOrDefault(false);
377
- }
378
-
379
- /**
380
- * 启动应用
381
- * @param packageName 应用包名
382
- * @returns 是否启动成功
383
- */
384
- public static launchApp(packageName: string): boolean {
385
- const response = this.call(CallMethod.launchApp, { args: { packageName } });
386
- return response.getDataOrDefault(false);
387
- }
388
-
389
- /**
390
- * 获取当前应用包名
391
- * @returns 包名
392
- */
393
- public static getPackageName(): string {
394
- const response = this.call(CallMethod.getPackageName);
395
- return response.getDataOrDefault("");
396
- }
397
-
398
- /**
399
- * 显示悬浮提示
400
- * @param text 提示文本
401
- * @param delay 显示时长(毫秒)
402
- * @returns 是否显示成功
403
- */
404
- public static overlayToast(text: string, delay: number = 2000): boolean {
405
- const response = this.call(CallMethod.overlayToast, {
406
- args: { text, delay },
407
- });
408
- return response.getDataOrDefault(false);
409
- }
410
-
411
- /**
412
- * 通过ID查找节点
413
- * @param id 节点ID
414
- * @param filterClass 类名过滤
415
- * @param filterText 文本过滤
416
- * @param filterDes 描述过滤
417
- * @param node 父节点范围
418
- * @returns 节点数组
419
- */
420
- public static findById(
421
- id: string,
422
- {
423
- filterClass,
424
- filterText,
425
- filterDes,
426
- node,
427
- }: {
428
- filterClass?: string;
429
- filterText?: string;
430
- filterDes?: string;
431
- node?: Node;
432
- } = {}
433
- ): Node[] {
434
- const response = this.call(CallMethod.findById, {
435
- args: { id, filterClass, filterText, filterDes },
436
- node,
437
- });
438
- return Node.fromJSONArray(response.getDataOrDefault("[]"));
439
- }
440
-
441
- /**
442
- * 通过文本查找节点
443
- * @param text 要查找的文本
444
- * @param filterClass 类名过滤
445
- * @param filterViewId 视图ID过滤
446
- * @param filterDes 描述过滤
447
- * @param node 父节点范围
448
- * @returns 节点数组
449
- */
450
- public static findByText(
451
- text: string,
452
- {
453
- filterClass,
454
- filterViewId,
455
- filterDes,
456
- node,
457
- }: {
458
- filterClass?: string;
459
- filterViewId?: string;
460
- filterDes?: string;
461
- node?: Node;
462
- } = {}
463
- ): Node[] {
464
- const response = this.call(CallMethod.findByText, {
465
- args: { text, filterClass, filterViewId, filterDes },
466
- node,
467
- });
468
- return Node.fromJSONArray(response.getDataOrDefault("[]"));
469
- }
470
-
471
- /**
472
- * 通过标签查找节点
473
- * @param className 类名
474
- * @param filterText 文本过滤
475
- * @param filterViewId 视图ID过滤
476
- * @param filterDes 描述过滤
477
- * @param node 父节点范围
478
- * @returns 节点数组
479
- */
480
- public static findByTags(
481
- className: string,
482
- {
483
- filterText,
484
- filterViewId,
485
- filterDes,
486
- node,
487
- }: {
488
- filterText?: string;
489
- filterViewId?: string;
490
- filterDes?: string;
491
- node?: Node;
492
- } = {}
493
- ): Node[] {
494
- const response = this.call(CallMethod.findByTags, {
495
- args: { className, filterText, filterViewId, filterDes },
496
- node,
497
- });
498
- return Node.fromJSONArray(response.getDataOrDefault("[]"));
499
- }
500
-
501
- /**
502
- * 查找所有匹配文本的节点
503
- * @param text 要查找的文本
504
- * @returns 节点数组
505
- */
506
- public static findByTextAllMatch(text: string): Node[] {
507
- const response = this.call(CallMethod.findByTextAllMatch, {
508
- args: { text },
509
- });
510
- return Node.fromJSONArray(response.getDataOrDefault("[]"));
511
- }
512
-
513
- /**
514
- * 检查是否包含指定文本
515
- * @param text 要检查的文本
516
- * @returns 是否包含
517
- */
518
- public static containsText(text: string): boolean {
519
- const response = this.call(CallMethod.containsText, { args: { text } });
520
- return response.getDataOrDefault(false);
521
- }
522
-
523
- /**
524
- * 获取所有文本
525
- * @returns 文本数组
526
- */
527
- public static getAllText(): string[] {
528
- const response = this.call(CallMethod.getAllText);
529
- return response.getDataOrDefault("[]");
530
- }
531
-
532
- /**
533
- * 查找第一个匹配标签的父节点
534
- * @param className 类名
535
- * @returns 父节点
536
- */
537
- public static findFirstParentByTags(node: Node, className: string): Node {
538
- const response = this.call(CallMethod.findFirstParentByTags, {
539
- args: { className },
540
- node,
541
- });
542
- return Node.create(response.getDataOrDefault("{}"));
543
- }
544
-
545
- /**
546
- * 获取节点的所有子节点
547
- * @param node 父节点
548
- * @returns 子节点数组
549
- */
550
- public static getNodes(node: Node): Node[] {
551
- const response = this.call(CallMethod.getNodes, { node });
552
- return Node.fromJSONArray(response.getDataOrDefault("[]"));
553
- }
554
-
555
- /**
556
- * 获取节点的直接子节点
557
- * @param node 父节点
558
- * @returns 子节点数组
559
- */
560
- public static getChildren(node: Node): Node[] {
561
- const response = this.call(CallMethod.getChildren, { node });
562
- return Node.fromJSONArray(response.getDataOrDefault([]));
563
- }
564
-
565
- /**
566
- * 查找第一个可点击的父节点
567
- * @param node 起始节点
568
- * @returns 可点击的父节点
569
- */
570
- public static findFirstParentClickable(node: Node): Node {
571
- const response = this.call(CallMethod.findFirstParentClickable, { node });
572
- return Node.create(response.getDataOrDefault("{}"));
573
- }
574
-
575
- /**
576
- * 获取节点在屏幕中的边界
577
- * @param node 目标节点
578
- * @returns 边界对象
579
- */
580
- public static getBoundsInScreen(node: Node): Bounds {
581
- const response = this.call(CallMethod.getBoundsInScreen, { node });
582
- return Bounds.fromData(response.getDataOrDefault({}));
583
- }
584
-
585
- /**
586
- * 检查节点是否可见
587
- * @param node 目标节点
588
- * @param compareNode 比较节点
589
- * @param isFullyByCompareNode 是否完全可见
590
- * @returns 是否可见
591
- */
592
- public static isVisible(
593
- node: Node,
594
- {
595
- compareNode,
596
- isFullyByCompareNode,
597
- }: { compareNode?: Node; isFullyByCompareNode?: boolean } = {}
598
- ): boolean {
599
- const response = this.call(CallMethod.isVisible, {
600
- node,
601
- args: { compareNode, isFullyByCompareNode },
602
- });
603
- return response.getDataOrDefault(false);
604
- }
605
-
606
- /**
607
- * 执行点击手势
608
- * @param x 横坐标
609
- * @param y 纵坐标
610
- * @param duration 持续时间
611
- * @param timeout 超时时间(秒),默认30秒
612
- * @returns 是否成功
613
- */
614
- public static async clickByGesture(
615
- x: number,
616
- y: number,
617
- duration: number,
618
- timeout?: number
619
- ): Promise<boolean> {
620
- const response = await this.asyncCall(CallMethod.clickByGesture, {
621
- args: { x, y, duration },
622
- timeout,
623
- });
624
- return response.getDataOrDefault(false);
625
- }
626
-
627
- /**
628
- * 返回操作
629
- * @returns 是否成功
630
- */
631
- public static back(): boolean {
632
- const response = this.call(CallMethod.back);
633
- return response.getDataOrDefault(false);
634
- }
635
-
636
- /**
637
- * 回到主页
638
- * @returns 是否成功
639
- */
640
- public static home(): boolean {
641
- const response = this.call(CallMethod.home);
642
- return response.getDataOrDefault(false);
643
- }
644
-
645
- /**
646
- * 打开通知栏
647
- * @returns 是否成功
648
- */
649
- public static notifications(): boolean {
650
- const response = this.call(CallMethod.notifications);
651
- return response.getDataOrDefault(false);
652
- }
653
-
654
- /**
655
- * 显示最近应用
656
- * @returns 是否成功
657
- */
658
- public static recentApps(): boolean {
659
- const response = this.call(CallMethod.recentApps);
660
- return response.getDataOrDefault(false);
661
- }
662
-
663
- /**
664
- * 在节点中粘贴文本
665
- * @param node 目标节点
666
- * @param text 要粘贴的文本
667
- * @returns 是否成功
668
- */
669
- public static paste(node: Node, text: string): boolean {
670
- const response = this.call(CallMethod.paste, { args: { text }, node });
671
- return response.getDataOrDefault(false);
672
- }
673
- public static focus(node: Node): boolean {
674
- const response = this.call(CallMethod.focus, { node });
675
- return response.getDataOrDefault(false);
676
- }
677
-
678
- /**
679
- * 选择文本
680
- * @param node 目标节点
681
- * @param selectionStart 选择起始位置
682
- * @param selectionEnd 选择结束位置
683
- * @returns 是否成功
684
- */
685
- public static selectionText(
686
- node: Node,
687
- selectionStart: number,
688
- selectionEnd: number
689
- ): boolean {
690
- const response = this.call(CallMethod.selectionText, {
691
- args: { selectionStart, selectionEnd },
692
- node,
693
- });
694
- return response.getDataOrDefault(false);
695
- }
696
-
697
- /**
698
- * 向前滚动
699
- * @param node 可滚动节点
700
- * @returns 是否成功
701
- */
702
- public static scrollForward(node: Node): boolean {
703
- const response = this.call(CallMethod.scrollForward, { node });
704
- return response.getDataOrDefault(false);
705
- }
706
-
707
- /**
708
- * 向后滚动
709
- * @param node 可滚动节点
710
- * @returns 是否成功
711
- */
712
- public static scrollBackward(node: Node): boolean {
713
- const response = this.call(CallMethod.scrollBackward, { node });
714
- return response.getDataOrDefault(false);
715
- }
716
-
717
- /**
718
- * 对节点执行点击手势
719
- * @param node 目标节点
720
- * @param offsetX X轴偏移
721
- * @param offsetY Y轴偏移
722
- * @param switchWindowIntervalDelay 窗口切换延迟
723
- * @param clickDuration 点击持续时间
724
- * @param timeout 超时时间(秒),默认30秒
725
- * @returns 是否成功
726
- */
727
- public static async clickNodeByGesture(
728
- node: Node,
729
- {
730
- offsetX,
731
- offsetY,
732
- switchWindowIntervalDelay,
733
- clickDuration,
734
- timeout,
735
- }: {
736
- offsetX?: number;
737
- offsetY?: number;
738
- switchWindowIntervalDelay?: number;
739
- clickDuration?: number;
740
- timeout?: number;
741
- } = {}
742
- ): Promise<boolean> {
743
- const response = await this.asyncCall(CallMethod.clickNodeByGesture, {
744
- node,
745
- args: { offsetX, offsetY, switchWindowIntervalDelay, clickDuration },
746
- timeout,
747
- });
748
- return response.getDataOrDefault(false);
749
- }
750
-
751
- /**
752
- * 对节点执行双击手势
753
- * @param node 目标节点
754
- * @param offsetX X轴偏移
755
- * @param offsetY Y轴偏移
756
- * @param switchWindowIntervalDelay 窗口切换延迟
757
- * @param clickDuration 点击持续时间
758
- * @param clickInterval 点击间隔
759
- * @param timeout 超时时间(秒),默认30秒
760
- * @returns 是否成功
761
- */
762
- public static async doubleClickNodeByGesture(
763
- node: Node,
764
- {
765
- offsetX,
766
- offsetY,
767
- switchWindowIntervalDelay,
768
- clickDuration,
769
- clickInterval,
770
- timeout,
771
- }: {
772
- offsetX?: number;
773
- offsetY?: number;
774
- switchWindowIntervalDelay?: number;
775
- clickDuration?: number;
776
- clickInterval?: number;
777
- timeout?: number;
778
- } = {}
779
- ): Promise<boolean> {
780
- const response = await this.asyncCall(CallMethod.doubleClickNodeByGesture, {
781
- node,
782
- args: {
783
- offsetX,
784
- offsetY,
785
- switchWindowIntervalDelay,
786
- clickDuration,
787
- clickInterval,
788
- },
789
- timeout,
790
- });
791
- return response.getDataOrDefault(false);
792
- }
793
- /**
794
- * 执行线型手势
795
- * @param startPoint
796
- * @param endPoint
797
- * @param param2
798
- * @param timeout 超时时间(秒),默认30秒
799
- * @returns
800
- */
801
- public static async performLinearGesture(
802
- startPoint: { x: number; y: number },
803
- endPoint: { x: number; y: number },
804
- { duration, timeout }: { duration?: number; timeout?: number } = {}
805
- ): Promise<boolean> {
806
- const response = await this.asyncCall(CallMethod.performLinearGesture, {
807
- args: { startPoint, endPoint, duration },
808
- timeout,
809
- });
810
- return response.getDataOrDefault(false);
811
- }
812
- public static async longPressNodeByGestureAutoPaste(
813
- node: Node,
814
- text: string,
815
- {
816
- matchedPackageName,
817
- matchedText,
818
- timeoutMillis,
819
- longPressDuration,
820
- timeout,
821
- }: {
822
- matchedPackageName?: string;
823
- matchedText?: string;
824
- timeoutMillis?: number;
825
- longPressDuration?: number;
826
- timeout?: number;
827
- } = { matchedText: "粘贴", timeoutMillis: 1500, longPressDuration: 600 }
828
- ): Promise<boolean> {
829
- const response = await this.asyncCall(
830
- CallMethod.longPressGestureAutoPaste,
831
- {
832
- node,
833
- args: {
834
- text,
835
- matchedPackageName,
836
- matchedText,
837
- timeoutMillis,
838
- longPressDuration,
839
- },
840
- timeout,
841
- }
842
- );
843
- return response.getDataOrDefault(false);
844
- }
845
-
846
- public static async longPressGestureAutoPaste(
847
- point: { x: number; y: number },
848
- text: string,
849
- {
850
- matchedPackageName,
851
- matchedText,
852
- timeoutMillis,
853
- longPressDuration,
854
- timeout,
124
+ /**
125
+ * 执行同步调用
126
+ * @param method 方法名
127
+ * @param args 参数对象
128
+ * @returns 调用响应
129
+ */
130
+ public static call(
131
+ method: string,
132
+ { args, node }: { args?: any; node?: Node } = {}
133
+ ): CallResponse {
134
+ const params = {
135
+ method,
136
+ arguments: args ? args : undefined,
137
+ node: node ? node : undefined,
138
+ };
139
+ const result = window.assistsx.call(JSON.stringify(params));
140
+ if (typeof result === "string") {
141
+ const responseData = JSON.parse(result);
142
+ const response = new CallResponse(
143
+ responseData.code,
144
+ responseData.data,
145
+ responseData.callbackId
146
+ );
147
+ return response;
148
+ }
149
+ throw new Error("Call failed");
150
+ }
151
+
152
+ /**
153
+ * 执行异步调用
154
+ * @param method 方法名
155
+ * @param args 参数对象
156
+ * @param timeout 超时时间(秒),默认30秒
157
+ * @returns Promise<调用响应>
158
+ */
159
+ public static async asyncCall(
160
+ method: string,
161
+ {
162
+ args,
163
+ node,
164
+ nodes,
165
+ timeout = 30,
166
+ }: { args?: any; node?: Node; nodes?: Node[]; timeout?: number } = {}
167
+ ): Promise<CallResponse> {
168
+ const uuid = generateUUID();
169
+ const params = {
170
+ method,
171
+ arguments: args ? args : undefined,
172
+ node: node ? node : undefined,
173
+ nodes: nodes ? nodes : undefined,
174
+ callbackId: uuid,
175
+ };
176
+ const promise = new Promise((resolve) => {
177
+ callbacks.set(uuid, (data: string) => {
178
+ resolve(data);
179
+ });
180
+ setTimeout(() => {
181
+ // 超时后删除回调函数
182
+ callbacks.delete(uuid);
183
+ resolve(new CallResponse(0, null, uuid));
184
+ }, timeout * 1000);
185
+ });
186
+ const result = window.assistsx.call(JSON.stringify(params));
187
+ const promiseResult = await promise;
188
+ if (typeof promiseResult === "string") {
189
+ const responseData = JSON.parse(promiseResult);
190
+ const response = new CallResponse(
191
+ responseData.code,
192
+ responseData.data,
193
+ responseData.callbackId
194
+ );
195
+ return response;
196
+ }
197
+ throw new Error("Call failed");
198
+ }
199
+
200
+ /**
201
+ * 设置悬浮窗标志
202
+ * @param flags 标志
203
+ * @returns 是否设置成功
204
+ */
205
+ public static setOverlayFlags(flags: number): boolean {
206
+ const response = this.call(CallMethod.setOverlayFlags, {
207
+ args: { flags: flags },
208
+ });
209
+ return response.getDataOrDefault(false);
210
+ }
211
+ /**
212
+ * 设置悬浮窗标志
213
+ * @param flags 标志
214
+ * @returns 是否设置成功
215
+ */
216
+ public static setOverlayFlagList(flags: number[]): boolean {
217
+ const response = this.call(CallMethod.setOverlayFlags, {
218
+ args: { flags: flags },
219
+ });
220
+ return response.getDataOrDefault(false);
221
+ }
222
+ /**
223
+ * 获取所有符合条件的节点
224
+ * @param filterClass 类名过滤
225
+ * @param filterViewId 视图ID过滤
226
+ * @param filterDes 描述过滤
227
+ * @param filterText 文本过滤
228
+ * @returns 节点数组
229
+ */
230
+ public static getAllNodes({
231
+ filterClass,
232
+ filterViewId,
233
+ filterDes,
234
+ filterText,
855
235
  }: {
856
- matchedPackageName?: string;
857
- matchedText?: string;
858
- timeoutMillis?: number;
859
- longPressDuration?: number;
860
- timeout?: number;
861
- } = { matchedText: "粘贴", timeoutMillis: 1500, longPressDuration: 600 }
862
- ): Promise<boolean> {
863
- const response = await this.asyncCall(
864
- CallMethod.longPressGestureAutoPaste,
865
- {
866
- args: {
867
- point,
868
- text,
869
- matchedPackageName,
870
- matchedText,
871
- timeoutMillis,
872
- longPressDuration,
873
- },
874
- timeout,
875
- }
876
- );
877
- return response.getDataOrDefault(false);
878
- }
879
- public static async getAppInfo(
880
- packageName: string,
881
- timeout?: number
882
- ): Promise<AppInfo> {
883
- const response = await this.asyncCall(CallMethod.getAppInfo, {
884
- args: { packageName },
885
- timeout,
886
- });
887
- return AppInfo.fromJSON(response.getDataOrDefault({}));
888
- }
889
- public static getUniqueDeviceId(): any {
890
- const response = this.call(CallMethod.getUniqueDeviceId);
891
- return response.getDataOrDefault("");
892
- }
893
- public static getAndroidID(): any {
894
- const response = this.call(CallMethod.getAndroidID);
895
- return response.getDataOrDefault("");
896
- }
897
- public static async getMacAddress(timeout?: number): Promise<any> {
898
- const response = await this.asyncCall(CallMethod.getMacAddress, {
899
- timeout,
900
- });
901
- return response.getDataOrDefault({});
902
- }
903
- public static async getDeviceInfo(timeout?: number): Promise<DeviceInfo> {
904
- const response = await this.asyncCall(CallMethod.getDeviceInfo, {
905
- timeout,
906
- });
907
- return DeviceInfo.fromJSON(response.getDataOrDefault({}));
908
- }
909
- public static async getNetworkType(timeout?: number): Promise<any> {
910
- const response = await this.asyncCall(CallMethod.getNetworkType, {
911
- timeout,
912
- });
913
- return response.getDataOrDefault({});
914
- }
915
- public static async setAccessibilityEventFilters(
916
- value: AccessibilityEventFilter[]
917
- ): Promise<any> {
918
- const response = this.call(CallMethod.setAccessibilityEventFilters, {
919
- args: { value },
920
- });
921
- return response.getDataOrDefault({});
922
- }
923
- public static async addAccessibilityEventFilter(
924
- value: AccessibilityEventFilter
925
- ): Promise<any> {
926
- const response = this.call(CallMethod.addAccessibilityEventFilter, {
927
- args: { value },
928
- });
929
- return response.getDataOrDefault({});
930
- }
931
-
932
- /**
933
- * 获取屏幕尺寸
934
- * @returns 屏幕尺寸对象
935
- */
936
- public static getScreenSize(): any {
937
- const response = this.call(CallMethod.getScreenSize);
938
- return response.getDataOrDefault("{}");
939
- }
940
-
941
- /**
942
- * 获取应用窗口尺寸
943
- * @returns 应用窗口尺寸对象
944
- */
945
- public static getAppScreenSize(): any {
946
- const response = this.call(CallMethod.getAppScreenSize);
947
- return response.getDataOrDefault("{}");
948
- }
949
-
950
- /**
951
- * 添加无障碍事件监听器
952
- * @param listener 监听器函数
953
- */
954
- public static addAccessibilityEventListener(
955
- listener: AccessibilityEventListener
956
- ): void {
957
- accessibilityEventListeners.push(listener);
958
- }
959
- /**
960
- * 判断是否包含无障碍事件监听器
961
- * @param listener 监听器函数
962
- * @returns 是否包含
963
- */
964
- public static containsAccessibilityEventListener(
965
- listener: AccessibilityEventListener
966
- ): boolean {
967
- return accessibilityEventListeners.indexOf(listener) > -1;
968
- }
969
-
970
- /**
971
- * 移除无障碍事件监听器
972
- * @param listener 要移除的监听器函数
973
- * @returns 是否移除成功
974
- */
975
- public static removeAccessibilityEventListener(
976
- listener: AccessibilityEventListener
977
- ): boolean {
978
- const index = accessibilityEventListeners.indexOf(listener);
979
- if (index > -1) {
980
- accessibilityEventListeners.splice(index, 1);
981
- return true;
982
- }
983
- return false;
984
- }
985
-
986
- /**
987
- * 移除所有无障碍事件监听器
988
- */
989
- public static removeAllAccessibilityEventListeners(): void {
990
- accessibilityEventListeners.length = 0;
991
- }
992
-
993
- /**
994
- * 获取当前注册的无障碍事件监听器数量
995
- * @returns 监听器数量
996
- */
997
- public static getAccessibilityEventListenerCount(): number {
998
- return accessibilityEventListeners.length;
999
- }
1000
-
1001
- /**
1002
- * 发送HTTP请求
1003
- * @param options 请求选项
1004
- * @returns HTTP响应
1005
- */
1006
- // public static async httpRequest(
1007
- // options: HttpRequestOptions
1008
- // ): Promise<HttpResponse> {
1009
- // const { url, method = "GET", headers, body, timeout = 30 } = options;
1010
- // const response = await this.asyncCall(CallMethod.httpRequest, {
1011
- // args: { url, method, headers, body, timeout },
1012
- // });
1013
- // return response.getDataOrDefault({
1014
- // statusCode: -1,
1015
- // statusMessage: "",
1016
- // body: "",
1017
- // headers: {},
1018
- // });
1019
- // }
236
+ filterClass?: string;
237
+ filterViewId?: string;
238
+ filterDes?: string;
239
+ filterText?: string;
240
+ } = {}): Node[] {
241
+ const response = this.call(CallMethod.getAllNodes, {
242
+ args: { filterClass, filterViewId, filterDes, filterText },
243
+ });
244
+ return Node.fromJSONArray(response.getDataOrDefault("[]"));
245
+ }
246
+
247
+ /**
248
+ * 设置节点文本
249
+ * @param node 目标节点
250
+ * @param text 要设置的文本
251
+ * @returns 是否设置成功
252
+ */
253
+ public static setNodeText(node: Node, text: string): boolean {
254
+ const response = this.call(CallMethod.setNodeText, {
255
+ args: { text },
256
+ node,
257
+ });
258
+ return response.getDataOrDefault(false);
259
+ }
260
+ /**
261
+ * 获取剪贴板最新文本
262
+ * @returns 剪贴板最新文本
263
+ */
264
+ public static getClipboardLatestText(): any {
265
+ const response = this.call(CallMethod.getClipboardLatestText);
266
+ return response.getDataOrDefault({});
267
+ }
268
+
269
+ /**
270
+ * 在浏览器中打开URL
271
+ * @param url 要打开的URL
272
+ * @returns 是否成功打开
273
+ */
274
+ public static openUrlInBrowser(url: string): boolean {
275
+ const response = this.call(CallMethod.openUrlInBrowser, {
276
+ args: { url },
277
+ });
278
+ return response.getDataOrDefault(false);
279
+ }
280
+
281
+ /**
282
+ * 保持屏幕常亮
283
+ * @param tip 提示文本
284
+ * @returns 是否保持屏幕常亮成功
285
+ */
286
+ public static keepScreenOn(tip?: string): boolean {
287
+ const response = this.call(CallMethod.keepScreenOn, {
288
+ args: { tip },
289
+ });
290
+ return response.getDataOrDefault(false);
291
+ }
292
+ public static clearKeepScreenOn(): boolean {
293
+ const response = this.call(CallMethod.clearKeepScreenOn, {});
294
+ return response.getDataOrDefault(false);
295
+ }
296
+ public static isAppInstalled(packageName: string): boolean {
297
+ const response = this.call(CallMethod.isAppInstalled, {
298
+ args: { packageName },
299
+ });
300
+ return response.getDataOrDefault(false);
301
+ }
302
+
303
+ /**
304
+ * 对指定节点进行截图
305
+ * @param nodes 要截图的节点数组
306
+ * @param overlayHiddenScreenshotDelayMillis 截图延迟时间(毫秒)
307
+ * @param timeout 超时时间(秒),默认30秒
308
+ * @returns 截图路径数组
309
+ */
310
+ public static async takeScreenshotNodes(
311
+ nodes: Node[],
312
+ overlayHiddenScreenshotDelayMillis: number = 250,
313
+ timeout?: number
314
+ ): Promise<string[]> {
315
+ const response = await this.asyncCall(CallMethod.takeScreenshot, {
316
+ nodes,
317
+ args: { overlayHiddenScreenshotDelayMillis },
318
+ timeout,
319
+ });
320
+ const data = response.getDataOrDefault("");
321
+ return data.images;
322
+ }
323
+ public static async scanQR(timeout?: number): Promise<string> {
324
+ const response = await this.asyncCall(CallMethod.scanQR, { timeout });
325
+ const data = response.getDataOrDefault({ value: "" });
326
+ return data.value;
327
+ }
328
+ public static async loadWebViewOverlay(
329
+ url: string,
330
+ options: WebFloatingWindowOptions & { timeout?: number } = {}
331
+ ): Promise<any> {
332
+ const {
333
+ initialWidth,
334
+ initialHeight,
335
+ minWidth,
336
+ minHeight,
337
+ maxWidth,
338
+ maxHeight,
339
+ initialCenter,
340
+ timeout,
341
+ } = options;
342
+ const response = await this.asyncCall(CallMethod.loadWebViewOverlay, {
343
+ args: {
344
+ url,
345
+ initialWidth,
346
+ initialHeight,
347
+ minWidth,
348
+ minHeight,
349
+ maxWidth,
350
+ maxHeight,
351
+ initialCenter,
352
+ },
353
+ timeout,
354
+ });
355
+ const data = response.getDataOrDefault({});
356
+ return data;
357
+ }
358
+
359
+ /**
360
+ * 点击节点
361
+ * @param node 要点击的节点
362
+ * @returns 是否点击成功
363
+ */
364
+ public static click(node: Node): boolean {
365
+ const response = this.call(CallMethod.click, { node });
366
+ return response.getDataOrDefault(false);
367
+ }
368
+
369
+ /**
370
+ * 长按节点
371
+ * @param node 要长按的节点
372
+ * @returns 是否长按成功
373
+ */
374
+ public static longClick(node: Node): boolean {
375
+ const response = this.call(CallMethod.longClick, { node });
376
+ return response.getDataOrDefault(false);
377
+ }
378
+
379
+ /**
380
+ * 启动应用
381
+ * @param packageName 应用包名
382
+ * @returns 是否启动成功
383
+ */
384
+ public static launchApp(packageName: string): boolean {
385
+ const response = this.call(CallMethod.launchApp, { args: { packageName } });
386
+ return response.getDataOrDefault(false);
387
+ }
388
+
389
+ /**
390
+ * 获取当前应用包名
391
+ * @returns 包名
392
+ */
393
+ public static getPackageName(): string {
394
+ const response = this.call(CallMethod.getPackageName);
395
+ return response.getDataOrDefault("");
396
+ }
397
+
398
+ /**
399
+ * 显示悬浮提示
400
+ * @param text 提示文本
401
+ * @param delay 显示时长(毫秒)
402
+ * @returns 是否显示成功
403
+ */
404
+ public static overlayToast(text: string, delay: number = 2000): boolean {
405
+ const response = this.call(CallMethod.overlayToast, {
406
+ args: { text, delay },
407
+ });
408
+ return response.getDataOrDefault(false);
409
+ }
410
+
411
+ /**
412
+ * 通过ID查找节点
413
+ * @param id 节点ID
414
+ * @param filterClass 类名过滤
415
+ * @param filterText 文本过滤
416
+ * @param filterDes 描述过滤
417
+ * @param node 父节点范围
418
+ * @returns 节点数组
419
+ */
420
+ public static findById(
421
+ id: string,
422
+ {
423
+ filterClass,
424
+ filterText,
425
+ filterDes,
426
+ node,
427
+ }: {
428
+ filterClass?: string;
429
+ filterText?: string;
430
+ filterDes?: string;
431
+ node?: Node;
432
+ } = {}
433
+ ): Node[] {
434
+ const response = this.call(CallMethod.findById, {
435
+ args: { id, filterClass, filterText, filterDes },
436
+ node,
437
+ });
438
+ return Node.fromJSONArray(response.getDataOrDefault("[]"));
439
+ }
440
+
441
+ /**
442
+ * 通过文本查找节点
443
+ * @param text 要查找的文本
444
+ * @param filterClass 类名过滤
445
+ * @param filterViewId 视图ID过滤
446
+ * @param filterDes 描述过滤
447
+ * @param node 父节点范围
448
+ * @returns 节点数组
449
+ */
450
+ public static findByText(
451
+ text: string,
452
+ {
453
+ filterClass,
454
+ filterViewId,
455
+ filterDes,
456
+ node,
457
+ }: {
458
+ filterClass?: string;
459
+ filterViewId?: string;
460
+ filterDes?: string;
461
+ node?: Node;
462
+ } = {}
463
+ ): Node[] {
464
+ const response = this.call(CallMethod.findByText, {
465
+ args: { text, filterClass, filterViewId, filterDes },
466
+ node,
467
+ });
468
+ return Node.fromJSONArray(response.getDataOrDefault("[]"));
469
+ }
470
+
471
+ /**
472
+ * 通过标签查找节点
473
+ * @param className 类名
474
+ * @param filterText 文本过滤
475
+ * @param filterViewId 视图ID过滤
476
+ * @param filterDes 描述过滤
477
+ * @param node 父节点范围
478
+ * @returns 节点数组
479
+ */
480
+ public static findByTags(
481
+ className: string,
482
+ {
483
+ filterText,
484
+ filterViewId,
485
+ filterDes,
486
+ node,
487
+ }: {
488
+ filterText?: string;
489
+ filterViewId?: string;
490
+ filterDes?: string;
491
+ node?: Node;
492
+ } = {}
493
+ ): Node[] {
494
+ const response = this.call(CallMethod.findByTags, {
495
+ args: { className, filterText, filterViewId, filterDes },
496
+ node,
497
+ });
498
+ return Node.fromJSONArray(response.getDataOrDefault("[]"));
499
+ }
500
+
501
+ /**
502
+ * 查找所有匹配文本的节点
503
+ * @param text 要查找的文本
504
+ * @returns 节点数组
505
+ */
506
+ public static findByTextAllMatch(text: string): Node[] {
507
+ const response = this.call(CallMethod.findByTextAllMatch, {
508
+ args: { text },
509
+ });
510
+ return Node.fromJSONArray(response.getDataOrDefault("[]"));
511
+ }
512
+
513
+ /**
514
+ * 检查是否包含指定文本
515
+ * @param text 要检查的文本
516
+ * @returns 是否包含
517
+ */
518
+ public static containsText(text: string): boolean {
519
+ const response = this.call(CallMethod.containsText, { args: { text } });
520
+ return response.getDataOrDefault(false);
521
+ }
522
+
523
+ /**
524
+ * 获取所有文本
525
+ * @returns 文本数组
526
+ */
527
+ public static getAllText(): string[] {
528
+ const response = this.call(CallMethod.getAllText);
529
+ return response.getDataOrDefault("[]");
530
+ }
531
+
532
+ /**
533
+ * 查找第一个匹配标签的父节点
534
+ * @param className 类名
535
+ * @returns 父节点
536
+ */
537
+ public static findFirstParentByTags(node: Node, className: string): Node {
538
+ const response = this.call(CallMethod.findFirstParentByTags, {
539
+ args: { className },
540
+ node,
541
+ });
542
+ return Node.create(response.getDataOrDefault("{}"));
543
+ }
544
+
545
+ /**
546
+ * 获取节点的所有子节点
547
+ * @param node 父节点
548
+ * @returns 子节点数组
549
+ */
550
+ public static getNodes(node: Node): Node[] {
551
+ const response = this.call(CallMethod.getNodes, { node });
552
+ return Node.fromJSONArray(response.getDataOrDefault("[]"));
553
+ }
554
+
555
+ /**
556
+ * 获取节点的直接子节点
557
+ * @param node 父节点
558
+ * @returns 子节点数组
559
+ */
560
+ public static getChildren(node: Node): Node[] {
561
+ const response = this.call(CallMethod.getChildren, { node });
562
+ return Node.fromJSONArray(response.getDataOrDefault([]));
563
+ }
564
+
565
+ /**
566
+ * 查找第一个可点击的父节点
567
+ * @param node 起始节点
568
+ * @returns 可点击的父节点
569
+ */
570
+ public static findFirstParentClickable(node: Node): Node {
571
+ const response = this.call(CallMethod.findFirstParentClickable, { node });
572
+ return Node.create(response.getDataOrDefault("{}"));
573
+ }
574
+
575
+ /**
576
+ * 获取节点在屏幕中的边界
577
+ * @param node 目标节点
578
+ * @returns 边界对象
579
+ */
580
+ public static getBoundsInScreen(node: Node): Bounds {
581
+ const response = this.call(CallMethod.getBoundsInScreen, { node });
582
+ return Bounds.fromData(response.getDataOrDefault({}));
583
+ }
584
+
585
+ /**
586
+ * 检查节点是否可见
587
+ * @param node 目标节点
588
+ * @param compareNode 比较节点
589
+ * @param isFullyByCompareNode 是否完全可见
590
+ * @returns 是否可见
591
+ */
592
+ public static isVisible(
593
+ node: Node,
594
+ {
595
+ compareNode,
596
+ isFullyByCompareNode,
597
+ }: { compareNode?: Node; isFullyByCompareNode?: boolean } = {}
598
+ ): boolean {
599
+ const response = this.call(CallMethod.isVisible, {
600
+ node,
601
+ args: { compareNode, isFullyByCompareNode },
602
+ });
603
+ return response.getDataOrDefault(false);
604
+ }
605
+
606
+ /**
607
+ * 执行点击手势
608
+ * @param x 横坐标
609
+ * @param y 纵坐标
610
+ * @param duration 持续时间
611
+ * @param timeout 超时时间(秒),默认30秒
612
+ * @returns 是否成功
613
+ */
614
+ public static async clickByGesture(
615
+ x: number,
616
+ y: number,
617
+ duration: number,
618
+ timeout?: number
619
+ ): Promise<boolean> {
620
+ const response = await this.asyncCall(CallMethod.clickByGesture, {
621
+ args: { x, y, duration },
622
+ timeout,
623
+ });
624
+ return response.getDataOrDefault(false);
625
+ }
626
+
627
+ /**
628
+ * 返回操作
629
+ * @returns 是否成功
630
+ */
631
+ public static back(): boolean {
632
+ const response = this.call(CallMethod.back);
633
+ return response.getDataOrDefault(false);
634
+ }
635
+
636
+ /**
637
+ * 回到主页
638
+ * @returns 是否成功
639
+ */
640
+ public static home(): boolean {
641
+ const response = this.call(CallMethod.home);
642
+ return response.getDataOrDefault(false);
643
+ }
644
+
645
+ /**
646
+ * 打开通知栏
647
+ * @returns 是否成功
648
+ */
649
+ public static notifications(): boolean {
650
+ const response = this.call(CallMethod.notifications);
651
+ return response.getDataOrDefault(false);
652
+ }
653
+
654
+ /**
655
+ * 显示最近应用
656
+ * @returns 是否成功
657
+ */
658
+ public static recentApps(): boolean {
659
+ const response = this.call(CallMethod.recentApps);
660
+ return response.getDataOrDefault(false);
661
+ }
662
+
663
+ /**
664
+ * 在节点中粘贴文本
665
+ * @param node 目标节点
666
+ * @param text 要粘贴的文本
667
+ * @returns 是否成功
668
+ */
669
+ public static paste(node: Node, text: string): boolean {
670
+ const response = this.call(CallMethod.paste, { args: { text }, node });
671
+ return response.getDataOrDefault(false);
672
+ }
673
+ public static focus(node: Node): boolean {
674
+ const response = this.call(CallMethod.focus, { node });
675
+ return response.getDataOrDefault(false);
676
+ }
677
+
678
+ /**
679
+ * 选择文本
680
+ * @param node 目标节点
681
+ * @param selectionStart 选择起始位置
682
+ * @param selectionEnd 选择结束位置
683
+ * @returns 是否成功
684
+ */
685
+ public static selectionText(
686
+ node: Node,
687
+ selectionStart: number,
688
+ selectionEnd: number
689
+ ): boolean {
690
+ const response = this.call(CallMethod.selectionText, {
691
+ args: { selectionStart, selectionEnd },
692
+ node,
693
+ });
694
+ return response.getDataOrDefault(false);
695
+ }
696
+
697
+ /**
698
+ * 向前滚动
699
+ * @param node 可滚动节点
700
+ * @returns 是否成功
701
+ */
702
+ public static scrollForward(node: Node): boolean {
703
+ const response = this.call(CallMethod.scrollForward, { node });
704
+ return response.getDataOrDefault(false);
705
+ }
706
+
707
+ /**
708
+ * 向后滚动
709
+ * @param node 可滚动节点
710
+ * @returns 是否成功
711
+ */
712
+ public static scrollBackward(node: Node): boolean {
713
+ const response = this.call(CallMethod.scrollBackward, { node });
714
+ return response.getDataOrDefault(false);
715
+ }
716
+
717
+ /**
718
+ * 对节点执行点击手势
719
+ * @param node 目标节点
720
+ * @param offsetX X轴偏移
721
+ * @param offsetY Y轴偏移
722
+ * @param switchWindowIntervalDelay 窗口切换延迟
723
+ * @param clickDuration 点击持续时间
724
+ * @param timeout 超时时间(秒),默认30秒
725
+ * @returns 是否成功
726
+ */
727
+ public static async clickNodeByGesture(
728
+ node: Node,
729
+ {
730
+ offsetX,
731
+ offsetY,
732
+ switchWindowIntervalDelay,
733
+ clickDuration,
734
+ timeout,
735
+ }: {
736
+ offsetX?: number;
737
+ offsetY?: number;
738
+ switchWindowIntervalDelay?: number;
739
+ clickDuration?: number;
740
+ timeout?: number;
741
+ } = {}
742
+ ): Promise<boolean> {
743
+ const response = await this.asyncCall(CallMethod.clickNodeByGesture, {
744
+ node,
745
+ args: { offsetX, offsetY, switchWindowIntervalDelay, clickDuration },
746
+ timeout,
747
+ });
748
+ return response.getDataOrDefault(false);
749
+ }
750
+
751
+ /**
752
+ * 对节点执行双击手势
753
+ * @param node 目标节点
754
+ * @param offsetX X轴偏移
755
+ * @param offsetY Y轴偏移
756
+ * @param switchWindowIntervalDelay 窗口切换延迟
757
+ * @param clickDuration 点击持续时间
758
+ * @param clickInterval 点击间隔
759
+ * @param timeout 超时时间(秒),默认30秒
760
+ * @returns 是否成功
761
+ */
762
+ public static async doubleClickNodeByGesture(
763
+ node: Node,
764
+ {
765
+ offsetX,
766
+ offsetY,
767
+ switchWindowIntervalDelay,
768
+ clickDuration,
769
+ clickInterval,
770
+ timeout,
771
+ }: {
772
+ offsetX?: number;
773
+ offsetY?: number;
774
+ switchWindowIntervalDelay?: number;
775
+ clickDuration?: number;
776
+ clickInterval?: number;
777
+ timeout?: number;
778
+ } = {}
779
+ ): Promise<boolean> {
780
+ const response = await this.asyncCall(CallMethod.doubleClickNodeByGesture, {
781
+ node,
782
+ args: {
783
+ offsetX,
784
+ offsetY,
785
+ switchWindowIntervalDelay,
786
+ clickDuration,
787
+ clickInterval,
788
+ },
789
+ timeout,
790
+ });
791
+ return response.getDataOrDefault(false);
792
+ }
793
+ /**
794
+ * 执行线型手势
795
+ * @param startPoint
796
+ * @param endPoint
797
+ * @param param2
798
+ * @param timeout 超时时间(秒),默认30秒
799
+ * @returns
800
+ */
801
+ public static async performLinearGesture(
802
+ startPoint: { x: number; y: number },
803
+ endPoint: { x: number; y: number },
804
+ { duration, timeout }: { duration?: number; timeout?: number } = {}
805
+ ): Promise<boolean> {
806
+ const response = await this.asyncCall(CallMethod.performLinearGesture, {
807
+ args: { startPoint, endPoint, duration },
808
+ timeout,
809
+ });
810
+ return response.getDataOrDefault(false);
811
+ }
812
+ public static async longPressNodeByGestureAutoPaste(
813
+ node: Node,
814
+ text: string,
815
+ {
816
+ matchedPackageName,
817
+ matchedText,
818
+ timeoutMillis,
819
+ longPressDuration,
820
+ timeout,
821
+ }: {
822
+ matchedPackageName?: string;
823
+ matchedText?: string;
824
+ timeoutMillis?: number;
825
+ longPressDuration?: number;
826
+ timeout?: number;
827
+ } = { matchedText: "粘贴", timeoutMillis: 1500, longPressDuration: 600 }
828
+ ): Promise<boolean> {
829
+ const response = await this.asyncCall(
830
+ CallMethod.longPressGestureAutoPaste,
831
+ {
832
+ node,
833
+ args: {
834
+ text,
835
+ matchedPackageName,
836
+ matchedText,
837
+ timeoutMillis,
838
+ longPressDuration,
839
+ },
840
+ timeout,
841
+ }
842
+ );
843
+ return response.getDataOrDefault(false);
844
+ }
845
+
846
+ public static async longPressGestureAutoPaste(
847
+ point: { x: number; y: number },
848
+ text: string,
849
+ {
850
+ matchedPackageName,
851
+ matchedText,
852
+ timeoutMillis,
853
+ longPressDuration,
854
+ timeout,
855
+ }: {
856
+ matchedPackageName?: string;
857
+ matchedText?: string;
858
+ timeoutMillis?: number;
859
+ longPressDuration?: number;
860
+ timeout?: number;
861
+ } = { matchedText: "粘贴", timeoutMillis: 1500, longPressDuration: 600 }
862
+ ): Promise<boolean> {
863
+ const response = await this.asyncCall(
864
+ CallMethod.longPressGestureAutoPaste,
865
+ {
866
+ args: {
867
+ point,
868
+ text,
869
+ matchedPackageName,
870
+ matchedText,
871
+ timeoutMillis,
872
+ longPressDuration,
873
+ },
874
+ timeout,
875
+ }
876
+ );
877
+ return response.getDataOrDefault(false);
878
+ }
879
+ public static async getAppInfo(
880
+ packageName: string,
881
+ timeout?: number
882
+ ): Promise<AppInfo> {
883
+ const response = await this.asyncCall(CallMethod.getAppInfo, {
884
+ args: { packageName },
885
+ timeout,
886
+ });
887
+ return AppInfo.fromJSON(response.getDataOrDefault({}));
888
+ }
889
+ public static getUniqueDeviceId(): any {
890
+ const response = this.call(CallMethod.getUniqueDeviceId);
891
+ return response.getDataOrDefault("");
892
+ }
893
+ public static getAndroidID(): any {
894
+ const response = this.call(CallMethod.getAndroidID);
895
+ return response.getDataOrDefault("");
896
+ }
897
+ public static async getMacAddress(timeout?: number): Promise<any> {
898
+ const response = await this.asyncCall(CallMethod.getMacAddress, {
899
+ timeout,
900
+ });
901
+ return response.getDataOrDefault({});
902
+ }
903
+ public static async getDeviceInfo(timeout?: number): Promise<DeviceInfo> {
904
+ const response = await this.asyncCall(CallMethod.getDeviceInfo, {
905
+ timeout,
906
+ });
907
+ return DeviceInfo.fromJSON(response.getDataOrDefault({}));
908
+ }
909
+ public static async getNetworkType(timeout?: number): Promise<any> {
910
+ const response = await this.asyncCall(CallMethod.getNetworkType, {
911
+ timeout,
912
+ });
913
+ return response.getDataOrDefault({});
914
+ }
915
+ /**
916
+ * 播放系统电话铃声
917
+ * @param timeout 超时时间(秒),默认30秒
918
+ * @returns 播放结果消息
919
+ */
920
+ public static async audioPlayRingtone(timeout?: number): Promise<string> {
921
+ const response = await this.asyncCall(CallMethod.audioPlayRingtone, {
922
+ timeout,
923
+ });
924
+ return response.getDataOrDefault("");
925
+ }
926
+ /**
927
+ * 停止播放系统电话铃声
928
+ * @param timeout 超时时间(秒),默认30秒
929
+ * @returns 停止结果消息
930
+ */
931
+ public static async audioStopRingtone(timeout?: number): Promise<string> {
932
+ const response = await this.asyncCall(CallMethod.audioStopRingtone, {
933
+ timeout,
934
+ });
935
+ return response.getDataOrDefault("");
936
+ }
937
+ public static async setAccessibilityEventFilters(
938
+ value: AccessibilityEventFilter[]
939
+ ): Promise<any> {
940
+ const response = this.call(CallMethod.setAccessibilityEventFilters, {
941
+ args: { value },
942
+ });
943
+ return response.getDataOrDefault({});
944
+ }
945
+ public static async addAccessibilityEventFilter(
946
+ value: AccessibilityEventFilter
947
+ ): Promise<any> {
948
+ const response = this.call(CallMethod.addAccessibilityEventFilter, {
949
+ args: { value },
950
+ });
951
+ return response.getDataOrDefault({});
952
+ }
953
+
954
+ /**
955
+ * 获取屏幕尺寸
956
+ * @returns 屏幕尺寸对象
957
+ */
958
+ public static getScreenSize(): any {
959
+ const response = this.call(CallMethod.getScreenSize);
960
+ return response.getDataOrDefault("{}");
961
+ }
962
+
963
+ /**
964
+ * 获取应用窗口尺寸
965
+ * @returns 应用窗口尺寸对象
966
+ */
967
+ public static getAppScreenSize(): any {
968
+ const response = this.call(CallMethod.getAppScreenSize);
969
+ return response.getDataOrDefault("{}");
970
+ }
971
+
972
+ /**
973
+ * 添加无障碍事件监听器
974
+ * @param listener 监听器函数
975
+ */
976
+ public static addAccessibilityEventListener(
977
+ listener: AccessibilityEventListener
978
+ ): void {
979
+ accessibilityEventListeners.push(listener);
980
+ }
981
+ /**
982
+ * 判断是否包含无障碍事件监听器
983
+ * @param listener 监听器函数
984
+ * @returns 是否包含
985
+ */
986
+ public static containsAccessibilityEventListener(
987
+ listener: AccessibilityEventListener
988
+ ): boolean {
989
+ return accessibilityEventListeners.indexOf(listener) > -1;
990
+ }
991
+
992
+ /**
993
+ * 移除无障碍事件监听器
994
+ * @param listener 要移除的监听器函数
995
+ * @returns 是否移除成功
996
+ */
997
+ public static removeAccessibilityEventListener(
998
+ listener: AccessibilityEventListener
999
+ ): boolean {
1000
+ const index = accessibilityEventListeners.indexOf(listener);
1001
+ if (index > -1) {
1002
+ accessibilityEventListeners.splice(index, 1);
1003
+ return true;
1004
+ }
1005
+ return false;
1006
+ }
1007
+
1008
+ /**
1009
+ * 移除所有无障碍事件监听器
1010
+ */
1011
+ public static removeAllAccessibilityEventListeners(): void {
1012
+ accessibilityEventListeners.length = 0;
1013
+ }
1014
+
1015
+ /**
1016
+ * 获取当前注册的无障碍事件监听器数量
1017
+ * @returns 监听器数量
1018
+ */
1019
+ public static getAccessibilityEventListenerCount(): number {
1020
+ return accessibilityEventListeners.length;
1021
+ }
1022
+
1023
+ /**
1024
+ * 发送HTTP请求
1025
+ * @param options 请求选项
1026
+ * @returns HTTP响应
1027
+ */
1028
+ // public static async httpRequest(
1029
+ // options: HttpRequestOptions
1030
+ // ): Promise<HttpResponse> {
1031
+ // const { url, method = "GET", headers, body, timeout = 30 } = options;
1032
+ // const response = await this.asyncCall(CallMethod.httpRequest, {
1033
+ // args: { url, method, headers, body, timeout },
1034
+ // });
1035
+ // return response.getDataOrDefault({
1036
+ // statusCode: -1,
1037
+ // statusMessage: "",
1038
+ // body: "",
1039
+ // headers: {},
1040
+ // });
1041
+ // }
1020
1042
  }