├── .gitignore
├── LICENSE
├── README.md
├── README_zh.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ └── java
│ ├── android
│ ├── app
│ │ ├── IUiAutomationConnection.java
│ │ ├── UiAutomation.java
│ │ └── UiAutomationConnection.java
│ ├── content
│ │ └── IClipboard.java
│ ├── hardware
│ │ ├── display
│ │ │ ├── DisplayManager.java
│ │ │ └── IDisplayManager.java
│ │ └── input
│ │ │ └── IInputManager.java
│ ├── os
│ │ └── ServiceManager.java
│ └── view
│ │ ├── DisplayInfo.java
│ │ └── SurfaceControl.java
│ └── com
│ └── ysbing
│ └── yadb
│ ├── Main.java
│ ├── input
│ ├── ClipboardManager.java
│ ├── Keyboard.java
│ └── Touch.java
│ ├── layout
│ ├── AccessibilityNodeInfoDumper.java
│ ├── AccessibilityNodeInfoHelper.java
│ ├── Layout.java
│ ├── LayoutShell.java
│ └── XmlUtil.java
│ └── screenshot
│ └── Screenshot.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
├── windows.bat
└── yadb
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | .idea
4 | local.properties
5 | .DS_Store
6 | build
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU LESSER GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 |
9 | This version of the GNU Lesser General Public License incorporates
10 | the terms and conditions of version 3 of the GNU General Public
11 | License, supplemented by the additional permissions listed below.
12 |
13 | 0. Additional Definitions.
14 |
15 | As used herein, "this License" refers to version 3 of the GNU Lesser
16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU
17 | General Public License.
18 |
19 | "The Library" refers to a covered work governed by this License,
20 | other than an Application or a Combined Work as defined below.
21 |
22 | An "Application" is any work that makes use of an interface provided
23 | by the Library, but which is not otherwise based on the Library.
24 | Defining a subclass of a class defined by the Library is deemed a mode
25 | of using an interface provided by the Library.
26 |
27 | A "Combined Work" is a work produced by combining or linking an
28 | Application with the Library. The particular version of the Library
29 | with which the Combined Work was made is also called the "Linked
30 | Version".
31 |
32 | The "Minimal Corresponding Source" for a Combined Work means the
33 | Corresponding Source for the Combined Work, excluding any source code
34 | for portions of the Combined Work that, considered in isolation, are
35 | based on the Application, and not on the Linked Version.
36 |
37 | The "Corresponding Application Code" for a Combined Work means the
38 | object code and/or source code for the Application, including any data
39 | and utility programs needed for reproducing the Combined Work from the
40 | Application, but excluding the System Libraries of the Combined Work.
41 |
42 | 1. Exception to Section 3 of the GNU GPL.
43 |
44 | You may convey a covered work under sections 3 and 4 of this License
45 | without being bound by section 3 of the GNU GPL.
46 |
47 | 2. Conveying Modified Versions.
48 |
49 | If you modify a copy of the Library, and, in your modifications, a
50 | facility refers to a function or data to be supplied by an Application
51 | that uses the facility (other than as an argument passed when the
52 | facility is invoked), then you may convey a copy of the modified
53 | version:
54 |
55 | a) under this License, provided that you make a good faith effort to
56 | ensure that, in the event an Application does not supply the
57 | function or data, the facility still operates, and performs
58 | whatever part of its purpose remains meaningful, or
59 |
60 | b) under the GNU GPL, with none of the additional permissions of
61 | this License applicable to that copy.
62 |
63 | 3. Object Code Incorporating Material from Library Header Files.
64 |
65 | The object code form of an Application may incorporate material from
66 | a header file that is part of the Library. You may convey such object
67 | code under terms of your choice, provided that, if the incorporated
68 | material is not limited to numerical parameters, data structure
69 | layouts and accessors, or small macros, inline functions and templates
70 | (ten or fewer lines in length), you do both of the following:
71 |
72 | a) Give prominent notice with each copy of the object code that the
73 | Library is used in it and that the Library and its use are
74 | covered by this License.
75 |
76 | b) Accompany the object code with a copy of the GNU GPL and this license
77 | document.
78 |
79 | 4. Combined Works.
80 |
81 | You may convey a Combined Work under terms of your choice that,
82 | taken together, effectively do not restrict modification of the
83 | portions of the Library contained in the Combined Work and reverse
84 | engineering for debugging such modifications, if you also do each of
85 | the following:
86 |
87 | a) Give prominent notice with each copy of the Combined Work that
88 | the Library is used in it and that the Library and its use are
89 | covered by this License.
90 |
91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license
92 | document.
93 |
94 | c) For a Combined Work that displays copyright notices during
95 | execution, include the copyright notice for the Library among
96 | these notices, as well as a reference directing the user to the
97 | copies of the GNU GPL and this license document.
98 |
99 | d) Do one of the following:
100 |
101 | 0) Convey the Minimal Corresponding Source under the terms of this
102 | License, and the Corresponding Application Code in a form
103 | suitable for, and under terms that permit, the user to
104 | recombine or relink the Application with a modified version of
105 | the Linked Version to produce a modified Combined Work, in the
106 | manner specified by section 6 of the GNU GPL for conveying
107 | Corresponding Source.
108 |
109 | 1) Use a suitable shared library mechanism for linking with the
110 | Library. A suitable mechanism is one that (a) uses at run time
111 | a copy of the Library already present on the user's computer
112 | system, and (b) will operate properly with a modified version
113 | of the Library that is interface-compatible with the Linked
114 | Version.
115 |
116 | e) Provide Installation Information, but only if you would otherwise
117 | be required to provide such information under section 6 of the
118 | GNU GPL, and only to the extent that such information is
119 | necessary to install and execute a modified version of the
120 | Combined Work produced by recombining or relinking the
121 | Application with a modified version of the Linked Version. (If
122 | you use option 4d0, the Installation Information must accompany
123 | the Minimal Corresponding Source and Corresponding Application
124 | Code. If you use option 4d1, you must provide the Installation
125 | Information in the manner specified by section 6 of the GNU GPL
126 | for conveying Corresponding Source.)
127 |
128 | 5. Combined Libraries.
129 |
130 | You may place library facilities that are a work based on the
131 | Library side by side in a single library together with other library
132 | facilities that are not Applications and are not covered by this
133 | License, and convey such a combined library under terms of your
134 | choice, if you do both of the following:
135 |
136 | a) Accompany the combined library with a copy of the same work based
137 | on the Library, uncombined with any other library facilities,
138 | conveyed under the terms of this License.
139 |
140 | b) Give prominent notice with the combined library that part of it
141 | is a work based on the Library, and explaining where to find the
142 | accompanying uncombined form of the same work.
143 |
144 | 6. Revised Versions of the GNU Lesser General Public License.
145 |
146 | The Free Software Foundation may publish revised and/or new versions
147 | of the GNU Lesser General Public License from time to time. Such new
148 | versions will be similar in spirit to the present version, but may
149 | differ in detail to address new problems or concerns.
150 |
151 | Each version is given a distinguishing version number. If the
152 | Library as you received it specifies that a certain numbered version
153 | of the GNU Lesser General Public License "or any later version"
154 | applies to it, you have the option of following the terms and
155 | conditions either of that published version or of any later version
156 | published by the Free Software Foundation. If the Library as you
157 | received it does not specify a version number of the GNU Lesser
158 | General Public License, you may choose any version of the GNU Lesser
159 | General Public License ever published by the Free Software Foundation.
160 |
161 | If the Library as you received it specifies that a proxy can decide
162 | whether future versions of the GNU Lesser General Public License shall
163 | apply, that proxy's public statement of acceptance of any version is
164 | permanent authorization for you to choose that version for the
165 | Library.
166 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Language: [English](https://github.com/ysbing/YADB/blob/master/README.md) | [中文简体](https://github.com/ysbing/YADB/blob/master/README_zh.md)
2 |
3 | # YADB
4 |
5 | **YADB** is a tool that extends the functionality of the native ADB (Android Debug Bridge), offering practical features not supported by the native ADB. These features include Unicode character input, screenshots, layout extraction, and long-press screen operations.
6 |
7 | ## Features
8 |
9 | ### Unicode character Input
10 |
11 | With YADB, you can easily input Chinese characters, solving the issue of garbled text when using `adb shell input text` to input Chinese.
12 |
13 | ```bash
14 | adb push yadb /data/local/tmp && adb shell app_process -Djava.class.path=/data/local/tmp/yadb /data/local/tmp com.ysbing.yadb.Main -keyboard 你好,世界
15 | ```
16 |
17 | ### Read Clipboard
18 |
19 | ```bash
20 | adb push yadb /data/local/tmp && adb shell app_process -Djava.class.path=/data/local/tmp/yadb /data/local/tmp com.ysbing.yadb.Main -readClipboard
21 | ```
22 |
23 | ### Screenshot
24 |
25 | YADB allows you to take screenshots without considering whether the Activity forbids screenshots.
26 |
27 | ```bash
28 | adb push yadb /data/local/tmp && adb shell app_process -Djava.class.path=/data/local/tmp/yadb /data/local/tmp com.ysbing.yadb.Main -screenshot
29 | ```
30 |
31 | ### Layout Extraction
32 |
33 | Compared to `adb shell uiautomator dump`, YADB provides more efficient layout extraction, especially in interfaces where uiautomator cannot fetch the layout.
34 |
35 | ```bash
36 | adb push yadb /data/local/tmp && adb shell app_process -Djava.class.path=/data/local/tmp/yadb /data/local/tmp com.ysbing.yadb.Main -layout
37 | ```
38 |
39 | ### Long Press Screen
40 |
41 | This feature can be used for automated testing to achieve long press operations on the screen.
42 |
43 | ```bash
44 | adb push yadb /data/local/tmp && adb shell app_process -Djava.class.path=/data/local/tmp/yadb /data/local/tmp com.ysbing.yadb.Main -touch 500 500 2000
45 | ```
46 |
47 | ## License
48 |
49 | This project is released under the [LGPLv3](https://opensource.org/licenses/LGPL-3.0) license.
--------------------------------------------------------------------------------
/README_zh.md:
--------------------------------------------------------------------------------
1 | # YADB
2 |
3 | **YADB** 是基于原生 ADB (Android Debug Bridge) 进行功能扩展的工具,提供了一些原生 ADB
4 | 不支持的实用功能。这些功能包括中文输入、截屏、布局抓取和长按屏幕操作。
5 |
6 | ## 功能介绍
7 |
8 | ### 中文输入
9 |
10 | 通过 YADB,您可以轻松输入中文,解决了使用 `adb shell input text` 输入中文时出现的乱码问题。
11 |
12 | ```bash
13 | adb push yadb /data/local/tmp & adb shell app_process -Djava.class.path=/data/local/tmp/yadb /data/local/tmp com.ysbing.yadb.Main -keyboard 你好,世界
14 | ```
15 |
16 | ### 读取粘贴板
17 |
18 | ```bash
19 | adb push yadb /data/local/tmp & adb shell app_process -Djava.class.path=/data/local/tmp/yadb /data/local/tmp com.ysbing.yadb.Main -readClipboard
20 | ```
21 |
22 | ### 截屏
23 |
24 | YADB 允许您在不考虑活动 (Activity) 禁止截屏的情况下进行屏幕截图。
25 |
26 | ```bash
27 | adb push yadb /data/local/tmp & adb shell app_process -Djava.class.path=/data/local/tmp/yadb /data/local/tmp com.ysbing.yadb.Main -screenshot
28 | ```
29 |
30 | ### 布局抓取
31 |
32 | 相比 `adb shell uiautomator dump`,YADB 提供了更为高效的布局抓取功能,尤其在某些界面 uiautomator
33 | 无法获取时。
34 |
35 | ```bash
36 | adb push yadb /data/local/tmp & adb shell app_process -Djava.class.path=/data/local/tmp/yadb /data/local/tmp com.ysbing.yadb.Main -layout
37 | ```
38 |
39 | ### 长按屏幕
40 |
41 | 该功能可用于自动化测试,实现屏幕的长按操作。
42 |
43 | ```bash
44 | adb push yadb /data/local/tmp & adb shell app_process -Djava.class.path=/data/local/tmp/yadb /data/local/tmp com.ysbing.yadb.Main -touch 500 500 2000
45 | ```
46 |
47 | ## 许可证
48 |
49 | 本项目采用 [LGPLv3](https://opensource.org/licenses/LGPL-3.0) 许可证发布。
50 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 | apply plugin: 'YRouter-system'
3 |
4 | android {
5 | compileSdk 34
6 |
7 | defaultConfig {
8 | applicationId "com.ysbing.yadb"
9 | minSdk 21
10 | targetSdk 34
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | }
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
23 | -optimizationpasses 5
24 | -dontusemixedcaseclassnames
25 | -dontskipnonpubliclibraryclassmembers
26 | -dontskipnonpubliclibraryclasses
27 | -dontpreverify
28 | -verbose
29 |
30 | -keep class com.ysbing.yadb.Main{*;}
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/app/src/main/java/android/app/IUiAutomationConnection.java:
--------------------------------------------------------------------------------
1 | package android.app;
2 |
3 | import com.ysbing.yrouter.api.YRouterSystem;
4 |
5 | @YRouterSystem
6 | public interface IUiAutomationConnection {
7 | }
--------------------------------------------------------------------------------
/app/src/main/java/android/app/UiAutomation.java:
--------------------------------------------------------------------------------
1 | package android.app;
2 |
3 | import android.os.Looper;
4 | import android.view.accessibility.AccessibilityNodeInfo;
5 |
6 | import com.ysbing.yrouter.api.YRouterSystem;
7 |
8 | @YRouterSystem
9 | public final class UiAutomation {
10 | public UiAutomation(Looper looper, IUiAutomationConnection connection) {
11 | }
12 |
13 | public AccessibilityNodeInfo getRootInActiveWindow() {
14 | return null;
15 | }
16 |
17 | public int getConnectionId() {
18 | return 0;
19 | }
20 |
21 | public void connect() {
22 | }
23 |
24 | public void disconnect() {
25 | }
26 | }
--------------------------------------------------------------------------------
/app/src/main/java/android/app/UiAutomationConnection.java:
--------------------------------------------------------------------------------
1 | package android.app;
2 |
3 | import com.ysbing.yrouter.api.YRouterSystem;
4 |
5 | @YRouterSystem
6 | public final class UiAutomationConnection implements IUiAutomationConnection {
7 | }
--------------------------------------------------------------------------------
/app/src/main/java/android/content/IClipboard.java:
--------------------------------------------------------------------------------
1 | package android.content;
2 |
3 | import android.annotation.TargetApi;
4 | import android.os.Build;
5 | import android.os.IBinder;
6 |
7 | import com.ysbing.yrouter.api.YRouterSystem;
8 |
9 | @YRouterSystem
10 | public interface IClipboard {
11 | ClipData getPrimaryClip(String pkg);
12 |
13 | @TargetApi(Build.VERSION_CODES.Q)
14 | ClipData getPrimaryClip(String pkg, int user);
15 |
16 | @TargetApi(Build.VERSION_CODES.TIRAMISU)
17 | ClipData getPrimaryClip(String pkg, String attributionTag, int userId);
18 |
19 | @TargetApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
20 | ClipData getPrimaryClip(String pkg, String attributionTag, int userId, int deviceId);
21 |
22 | @TargetApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
23 | ClipData getPrimaryClip(String pkg, String attributionTag, int userId, int deviceId, String targetPackage);
24 |
25 | @TargetApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
26 | ClipData getPrimaryClip(String pkg, String attributionTag, String var1, String var2 ,int userId, int deviceId, boolean isUserOperation);
27 |
28 | void setPrimaryClip(ClipData clip, String callingPackage);
29 |
30 | @TargetApi(Build.VERSION_CODES.Q)
31 | void setPrimaryClip(ClipData clip, String callingPackage, int userId);
32 |
33 | @TargetApi(Build.VERSION_CODES.TIRAMISU)
34 | void setPrimaryClip(ClipData clip, String callingPackage, String attributionTag, int userId);
35 |
36 | @TargetApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
37 | void setPrimaryClip(ClipData clip, String callingPackage, String attributionTag, int userId,
38 | int deviceId);
39 |
40 | @TargetApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
41 | void setPrimaryClip(ClipData clip, String callingPackage, String attributionTag, int userId,
42 | int deviceId, boolean isUserOperation);
43 | @YRouterSystem
44 | class Stub {
45 | public static IClipboard asInterface(IBinder obj) {
46 | return null;
47 | }
48 | }
49 | }
--------------------------------------------------------------------------------
/app/src/main/java/android/hardware/display/DisplayManager.java:
--------------------------------------------------------------------------------
1 | package android.hardware.display;
2 |
3 | import android.view.Surface;
4 |
5 | import com.ysbing.yrouter.api.YRouterSystem;
6 |
7 | @YRouterSystem
8 | public final class DisplayManager {
9 | public static VirtualDisplay createVirtualDisplay(String name, int width, int height,
10 | int displayIdToMirror, Surface surface) {
11 | return null;
12 | }
13 | }
--------------------------------------------------------------------------------
/app/src/main/java/android/hardware/display/IDisplayManager.java:
--------------------------------------------------------------------------------
1 | package android.hardware.display;
2 |
3 | import android.content.IClipboard;
4 | import android.os.IBinder;
5 | import android.view.DisplayInfo;
6 |
7 | import com.ysbing.yrouter.api.YRouterSystem;
8 |
9 | @YRouterSystem
10 | public interface IDisplayManager {
11 | DisplayInfo getDisplayInfo(int displayId);
12 |
13 | @YRouterSystem
14 | class Stub {
15 | public static IDisplayManager asInterface(IBinder obj) {
16 | return null;
17 | }
18 | }
19 | }
--------------------------------------------------------------------------------
/app/src/main/java/android/hardware/input/IInputManager.java:
--------------------------------------------------------------------------------
1 | package android.hardware.input;
2 |
3 | import android.content.IClipboard;
4 | import android.os.IBinder;
5 | import android.view.InputEvent;
6 |
7 | import com.ysbing.yrouter.api.YRouterSystem;
8 |
9 | @YRouterSystem
10 | public interface IInputManager {
11 | boolean injectInputEvent(InputEvent ev, int mode);
12 |
13 | @YRouterSystem
14 | class Stub {
15 | public static IInputManager asInterface(IBinder obj) {
16 | return null;
17 | }
18 | }
19 | }
--------------------------------------------------------------------------------
/app/src/main/java/android/os/ServiceManager.java:
--------------------------------------------------------------------------------
1 | package android.os;
2 |
3 | import com.ysbing.yrouter.api.YRouterSystem;
4 |
5 | @YRouterSystem
6 | public final class ServiceManager {
7 | public static IBinder getService(String name) {
8 | return null;
9 | }
10 | }
--------------------------------------------------------------------------------
/app/src/main/java/android/view/DisplayInfo.java:
--------------------------------------------------------------------------------
1 | package android.view;
2 |
3 | import com.ysbing.yrouter.api.YRouterSystem;
4 |
5 | @YRouterSystem
6 | public final class DisplayInfo {
7 | public int layerStack;
8 | public int logicalWidth;
9 | public int logicalHeight;
10 | public int rotation;
11 | }
12 |
--------------------------------------------------------------------------------
/app/src/main/java/android/view/SurfaceControl.java:
--------------------------------------------------------------------------------
1 | package android.view;
2 |
3 | import android.graphics.Rect;
4 | import android.os.IBinder;
5 |
6 | import com.ysbing.yrouter.api.YRouterSystem;
7 |
8 | @YRouterSystem
9 | public final class SurfaceControl {
10 | public static void openTransaction() {
11 | }
12 |
13 | public static void closeTransaction() {
14 | }
15 |
16 | public static void setDisplayProjection(IBinder displayToken,
17 | int orientation, Rect layerStackRect, Rect displayRect) {
18 | }
19 |
20 | public static void setDisplayLayerStack(IBinder displayToken, int layerStack) {
21 | }
22 |
23 | public static void setDisplaySurface(IBinder displayToken, Surface surface) {
24 | }
25 |
26 | public static IBinder createDisplay(String name, boolean secure) {
27 | return null;
28 | }
29 |
30 | public static void destroyDisplay(IBinder displayToken) {
31 | }
32 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/ysbing/yadb/Main.java:
--------------------------------------------------------------------------------
1 | package com.ysbing.yadb;
2 |
3 | import android.os.Looper;
4 |
5 | import com.ysbing.yadb.input.Keyboard;
6 | import com.ysbing.yadb.input.Touch;
7 | import com.ysbing.yadb.layout.Layout;
8 | import com.ysbing.yadb.screenshot.Screenshot;
9 |
10 | import java.io.PrintWriter;
11 | import java.io.StringWriter;
12 |
13 | public class Main {
14 | public static final String PACKAGE_NAME = "com.android.shell";
15 | public static final int USER_ID = 0;
16 | private static final String ARG_KEY_BOARD = "-keyboard";
17 | private static final String ARG_TOUCH = "-touch";
18 | private static final String ARG_LAYOUT = "-layout";
19 | private static final String ARG_SCREENSHOT = "-screenshot";
20 | private static final String ARG_READ_CLIPBOARD = "-readClipboard";
21 |
22 | public static void main(String[] args) {
23 | try {
24 | Thread.setDefaultUncaughtExceptionHandler((t, e) -> System.out.println(t.getName() + ",UncaughtException:" + getStackTraceAsString(e)));
25 | Looper.prepareMainLooper();
26 | if (check(args[0])) {
27 | switch (args[0]) {
28 | case ARG_KEY_BOARD:
29 | Keyboard.run(args[1]);
30 | break;
31 | case ARG_TOUCH:
32 | if (args.length == 4) {
33 | Touch.run(Float.parseFloat(args[1]), Float.parseFloat(args[2]), Long.parseLong(args[3]));
34 | } else {
35 | Touch.run(Float.parseFloat(args[1]), Float.parseFloat(args[2]), -1L);
36 | }
37 | break;
38 | case ARG_LAYOUT:
39 | if (args.length == 2) {
40 | Layout.run(args[1]);
41 | } else {
42 | Layout.run(null);
43 | }
44 | break;
45 | case ARG_SCREENSHOT:
46 | if (args.length == 2) {
47 | Screenshot.run(args[1]);
48 | } else {
49 | Screenshot.run(null);
50 | }
51 | break;
52 | case ARG_READ_CLIPBOARD:
53 | Keyboard.readClipboard();
54 | break;
55 | default:
56 | break;
57 | }
58 | } else {
59 | System.out.println("Invalid argument");
60 | }
61 | } catch (Throwable e) {
62 | System.out.println("MainException:" + getStackTraceAsString(e));
63 | }
64 | }
65 |
66 | private static boolean check(String arg) {
67 | return arg.equals(ARG_KEY_BOARD) || arg.equals(ARG_TOUCH) || arg.equals(ARG_LAYOUT) || arg.equals(ARG_SCREENSHOT) || arg.equals(ARG_READ_CLIPBOARD);
68 | }
69 |
70 | private static String getStackTraceAsString(Throwable throwable) {
71 | StringWriter stringWriter = new StringWriter();
72 | PrintWriter printWriter = new PrintWriter(stringWriter);
73 | throwable.printStackTrace(printWriter);
74 | printWriter.flush();
75 | return stringWriter.toString();
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/app/src/main/java/com/ysbing/yadb/input/ClipboardManager.java:
--------------------------------------------------------------------------------
1 | package com.ysbing.yadb.input;
2 |
3 | import android.content.ClipData;
4 | import android.content.IClipboard;
5 | import android.os.Build;
6 |
7 | import com.ysbing.yadb.Main;
8 |
9 | public class ClipboardManager {
10 | private final IClipboard manager;
11 |
12 | public ClipboardManager(IClipboard manager) {
13 | this.manager = manager;
14 | }
15 |
16 | private static ClipData getPrimaryClip(IClipboard manager) {
17 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
18 | return manager.getPrimaryClip(Main.PACKAGE_NAME);
19 | } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
20 | return manager.getPrimaryClip(Main.PACKAGE_NAME, Main.USER_ID);
21 | } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
22 | try {
23 | return manager.getPrimaryClip(Main.PACKAGE_NAME, null, Main.USER_ID);
24 | } catch (NoSuchMethodError e) {
25 | return manager.getPrimaryClip(Main.PACKAGE_NAME, Main.USER_ID);
26 | }
27 | } else {
28 | try {
29 | return manager.getPrimaryClip(Main.PACKAGE_NAME, null, Main.USER_ID, 0);
30 | } catch (NoSuchMethodError e) {
31 | try {
32 | return manager.getPrimaryClip(Main.PACKAGE_NAME, null, Main.USER_ID, 0, null);
33 | } catch (NoSuchMethodError error) {
34 | return manager.getPrimaryClip(Main.PACKAGE_NAME, null, null, null, Main.USER_ID, 0, false);
35 | }
36 | }
37 | }
38 | }
39 |
40 | private static void setPrimaryClip(IClipboard manager, ClipData clipData) {
41 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
42 | manager.setPrimaryClip(clipData, Main.PACKAGE_NAME);
43 | } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
44 | manager.setPrimaryClip(clipData, Main.PACKAGE_NAME, Main.USER_ID);
45 | } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
46 | try {
47 | manager.setPrimaryClip(clipData, Main.PACKAGE_NAME, null, Main.USER_ID);
48 | } catch (NoSuchMethodError e) {
49 | manager.setPrimaryClip(clipData, Main.PACKAGE_NAME, Main.USER_ID);
50 | }
51 | } else {
52 | try {
53 | manager.setPrimaryClip(clipData, Main.PACKAGE_NAME, null, Main.USER_ID, 0);
54 | } catch (NoSuchMethodError e) {
55 | manager.setPrimaryClip(clipData, Main.PACKAGE_NAME, null, Main.USER_ID, 0, false);
56 | }
57 | }
58 | }
59 |
60 | public CharSequence getText() {
61 | try {
62 | ClipData clipData = getPrimaryClip(manager);
63 | if (clipData == null || clipData.getItemCount() == 0) {
64 | return null;
65 | }
66 | return clipData.getItemAt(0).getText();
67 | } catch (Exception e) {
68 | return null;
69 | }
70 | }
71 |
72 | public boolean setText(CharSequence text) {
73 | try {
74 | ClipData clipData = ClipData.newPlainText(null, text);
75 | setPrimaryClip(manager, clipData);
76 | return true;
77 | } catch (Exception e) {
78 | return false;
79 | }
80 | }
81 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/ysbing/yadb/input/Keyboard.java:
--------------------------------------------------------------------------------
1 | package com.ysbing.yadb.input;
2 |
3 | import android.content.Context;
4 | import android.content.IClipboard;
5 | import android.hardware.input.IInputManager;
6 | import android.os.SystemClock;
7 | import android.view.InputDevice;
8 | import android.view.InputEvent;
9 | import android.view.KeyCharacterMap;
10 | import android.view.KeyEvent;
11 |
12 | public class Keyboard {
13 | private static final String FLAG_ENTER = "\\n";
14 | private static final String FLAG_CLEAR = "~CLEAR~";
15 |
16 |
17 | public static void run(String text) {
18 | boolean clearFlags = text.contains(FLAG_CLEAR);
19 | if (clearFlags) {
20 | injectKeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_CTRL_LEFT, KeyEvent.META_CTRL_ON | KeyEvent.META_CTRL_LEFT_ON);
21 | injectKeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON | KeyEvent.META_CTRL_LEFT_ON);
22 | injectKeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_A, KeyEvent.META_CTRL_ON | KeyEvent.META_CTRL_LEFT_ON);
23 | injectKeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_CTRL_LEFT, KeyEvent.META_NUM_LOCK_ON);
24 | injectKeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL, KeyEvent.META_CTRL_ON | KeyEvent.META_CTRL_LEFT_ON);
25 | injectKeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL, KeyEvent.META_CTRL_ON | KeyEvent.META_CTRL_LEFT_ON);
26 | } else {
27 | text = text.replace(FLAG_ENTER, "\n");
28 | boolean ok = setClipboardText(text);
29 | injectKeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_CTRL_LEFT, KeyEvent.META_CTRL_ON | KeyEvent.META_CTRL_LEFT_ON);
30 | injectKeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_V, KeyEvent.META_CTRL_ON | KeyEvent.META_CTRL_LEFT_ON);
31 | injectKeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_V, KeyEvent.META_CTRL_ON | KeyEvent.META_CTRL_LEFT_ON);
32 | injectKeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_CTRL_LEFT, KeyEvent.META_NUM_LOCK_ON);
33 | System.out.println("Copy text:" + ok);
34 | }
35 | }
36 |
37 | private static void injectKeyEvent(int action, int keyCode, int metaState) {
38 | long now = SystemClock.uptimeMillis();
39 | KeyEvent event = new KeyEvent(now, now, action, keyCode, 0, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
40 | InputDevice.SOURCE_KEYBOARD);
41 | injectEvent(event);
42 | }
43 |
44 | private static boolean setClipboardText(String text) {
45 | IClipboard clipboard = IClipboard.Stub.asInterface(android.os.ServiceManager.getService(Context.CLIPBOARD_SERVICE));
46 | if (clipboard == null) {
47 | return false;
48 | }
49 | ClipboardManager clipboardManager = new ClipboardManager(clipboard);
50 | String currentClipboard = getClipboardText();
51 | if (currentClipboard != null && currentClipboard.equals(text)) {
52 | return true;
53 | }
54 |
55 | return clipboardManager.setText(text);
56 | }
57 |
58 | private static String getClipboardText() {
59 | IClipboard clipboard = IClipboard.Stub.asInterface(android.os.ServiceManager.getService(Context.CLIPBOARD_SERVICE));
60 | if (clipboard == null) {
61 | return null;
62 | }
63 | ClipboardManager clipboardManager = new ClipboardManager(clipboard);
64 | CharSequence s = clipboardManager.getText();
65 | if (s == null) {
66 | return null;
67 | }
68 | return s.toString();
69 | }
70 |
71 | public static void injectEvent(InputEvent inputEvent) {
72 | IInputManager inputManager = IInputManager.Stub.asInterface(android.os.ServiceManager.getService(Context.INPUT_SERVICE));
73 | if (inputManager == null) {
74 | return;
75 | }
76 | inputManager.injectInputEvent(inputEvent, 0);
77 | }
78 |
79 | public static void readClipboard() {
80 | String clipboard = getClipboardText();
81 | System.out.println(clipboard);
82 | }
83 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/ysbing/yadb/input/Touch.java:
--------------------------------------------------------------------------------
1 | package com.ysbing.yadb.input;
2 |
3 | import android.os.SystemClock;
4 | import android.view.InputDevice;
5 | import android.view.MotionEvent;
6 |
7 | public class Touch {
8 | public static void run(float x, float y, long pressedTime) throws InterruptedException {
9 | long downTime = SystemClock.uptimeMillis();
10 | MotionEvent.PointerProperties[] properties = new MotionEvent.PointerProperties[1];
11 | properties[0] = new MotionEvent.PointerProperties();
12 | properties[0].id = 0;
13 | properties[0].toolType = MotionEvent.TOOL_TYPE_FINGER;
14 | MotionEvent.PointerCoords[] coords = new MotionEvent.PointerCoords[1];
15 | coords[0] = new MotionEvent.PointerCoords();
16 | coords[0].x = x;
17 | coords[0].y = y;
18 | coords[0].pressure = 1;
19 | MotionEvent clickEvent = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(),
20 | MotionEvent.ACTION_DOWN, 1, properties, coords, 0,
21 | 0, 1f, 1f, -1, 0, InputDevice.SOURCE_TOUCHSCREEN, 0);
22 | Keyboard.injectEvent(clickEvent);
23 | if (pressedTime >= 0L) {
24 | Thread.sleep(pressedTime);
25 | }
26 | coords[0].pressure = 0;
27 | MotionEvent clickEvent2 = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(),
28 | MotionEvent.ACTION_UP, 1, properties, coords, 0,
29 | 1, 1f, 1f, -1, 0, InputDevice.SOURCE_TOUCHSCREEN, 0);
30 | Keyboard.injectEvent(clickEvent2);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/app/src/main/java/com/ysbing/yadb/layout/AccessibilityNodeInfoDumper.java:
--------------------------------------------------------------------------------
1 | package com.ysbing.yadb.layout;
2 |
3 | import android.os.SystemClock;
4 | import android.util.Log;
5 | import android.util.Xml;
6 | import android.view.DisplayInfo;
7 | import android.view.accessibility.AccessibilityNodeInfo;
8 |
9 | import org.xmlpull.v1.XmlSerializer;
10 |
11 | import java.io.IOException;
12 | import java.io.StringWriter;
13 | import java.util.regex.Pattern;
14 |
15 | /**
16 | * The AccessibilityNodeInfoDumper in Android Open Source Project contains a lot of bugs which will
17 | * stay in old android versions forever. By coping the code of the latest version it is ensured that
18 | * all patches become available on old android versions.
down ported bugs are e.g. { @link
19 | * https://code.google.com/p/android/issues/detail?id=62906 } { @link
20 | * https://code.google.com/p/android/issues/detail?id=58733 }
21 | */
22 | public class AccessibilityNodeInfoDumper {
23 |
24 | private static final String TAG = "AccessibilityNodeDumper";
25 |
26 | private static final String[] NAF_EXCLUDED_CLASSES = new String[]{android.widget.GridView.class.getName(), android.widget.GridLayout.class.getName(), android.widget.ListView.class.getName(), android.widget.TableLayout.class.getName()};
27 | // XML 1.0 Legal Characters (http://stackoverflow.com/a/4237934/347155)
28 | // #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
29 | private static final Pattern XML10Pattern = Pattern.compile("[^" + " \r\n" + " -\uD7FF" + "\uE000-\uFFFD" + "\ud800\udc00-\udbff\udfff" + "]");
30 |
31 | /**
32 | * Using {@link AccessibilityNodeInfo} this method will walk the layout hierarchy and return
33 | * String object of xml hierarchy
34 | *
35 | * @param root The root accessibility node.
36 | */
37 | public static String getWindowXMLHierarchy(AccessibilityNodeInfo root, DisplayInfo displayInfo) {
38 | final long startTime = SystemClock.uptimeMillis();
39 | StringWriter xmlDump = new StringWriter();
40 | try {
41 |
42 | XmlSerializer serializer = Xml.newSerializer();
43 | serializer.setOutput(xmlDump);
44 | serializer.startDocument("UTF-8", true);
45 | serializer.startTag("", "hierarchy");
46 |
47 | if (root != null) {
48 | int width = displayInfo.logicalWidth;
49 | int height = displayInfo.logicalHeight;
50 | serializer.attribute("", "rotation", Integer.toString(displayInfo.rotation));
51 | dumpNodeRec(root, serializer, 0, width, height);
52 | } else {
53 | return "";
54 | }
55 | serializer.endTag("", "hierarchy");
56 | serializer.endDocument();
57 | } catch (IOException e) {
58 | Log.e(TAG, "failed to dump window to file", e);
59 | }
60 | final long endTime = SystemClock.uptimeMillis();
61 | Log.i(TAG, "Fetch time: " + (endTime - startTime) + "ms");
62 | return xmlDump.toString();
63 | }
64 |
65 |
66 | private static void dumpNodeRec(AccessibilityNodeInfo node, XmlSerializer serializer, int index, int width, int height) throws IOException {
67 | serializer.startTag("", "node");
68 | if (!nafExcludedClass(node) && !nafCheck(node))
69 | serializer.attribute("", "NAF", Boolean.toString(true));
70 | serializer.attribute("", "index", Integer.toString(index));
71 | serializer.attribute("", "text", safeCharSeqToString(node.getText()));
72 | serializer.attribute("", "class", safeCharSeqToString(node.getClassName()));
73 | serializer.attribute("", "package", safeCharSeqToString(node.getPackageName()));
74 | serializer.attribute("", "content-desc", safeCharSeqToString(node.getContentDescription()));
75 | serializer.attribute("", "checkable", Boolean.toString(node.isCheckable()));
76 | serializer.attribute("", "checked", Boolean.toString(node.isChecked()));
77 | serializer.attribute("", "clickable", Boolean.toString(node.isClickable()));
78 | serializer.attribute("", "enabled", Boolean.toString(node.isEnabled()));
79 | serializer.attribute("", "focusable", Boolean.toString(node.isFocusable()));
80 | serializer.attribute("", "focused", Boolean.toString(node.isFocused()));
81 | serializer.attribute("", "scrollable", Boolean.toString(node.isScrollable()));
82 | serializer.attribute("", "long-clickable", Boolean.toString(node.isLongClickable()));
83 | serializer.attribute("", "password", Boolean.toString(node.isPassword()));
84 | serializer.attribute("", "selected", Boolean.toString(node.isSelected()));
85 | serializer.attribute("", "bounds", AccessibilityNodeInfoHelper.getVisibleBoundsInScreen(node, width, height).toShortString());
86 | serializer.attribute("", "resource-id", safeCharSeqToString(node.getViewIdResourceName()));
87 |
88 | node.refresh();
89 | int count = node.getChildCount();
90 | for (int i = 0; i < count; i++) {
91 | AccessibilityNodeInfo child = node.getChild(i);
92 | if (child != null) {
93 | if (child.isVisibleToUser()) {
94 | dumpNodeRec(child, serializer, i, width, height);
95 | child.recycle();
96 | } else {
97 | Log.i(TAG, String.format("Skipping invisible child: %s", child.toString()));
98 | }
99 | } else {
100 | Log.i(TAG, String.format("Null child %d/%d, parent: %s", i, count, node.toString()));
101 | }
102 | }
103 | serializer.endTag("", "node");
104 | }
105 |
106 | /**
107 | * The list of classes to exclude my not be complete. We're attempting to only reduce noise from
108 | * standard layout classes that may be falsely configured to accept clicks and are also
109 | * enabled.
110 | *
111 | * @return true if node is excluded.
112 | */
113 | private static boolean nafExcludedClass(AccessibilityNodeInfo node) {
114 | String className = safeCharSeqToString(node.getClassName());
115 | for (String excludedClassName : NAF_EXCLUDED_CLASSES) {
116 | if (className.endsWith(excludedClassName)) return true;
117 | }
118 | return false;
119 | }
120 |
121 | /**
122 | * We're looking for UI controls that are enabled, clickable but have no text nor
123 | * content-description. Such controls configuration indicate an interactive control is present
124 | * in the UI and is most likely not accessibility friendly. We refer to such controls here as
125 | * NAF controls (Not Accessibility Friendly)
126 | *
127 | * @return false if a node fails the check, true if all is OK
128 | */
129 | private static boolean nafCheck(AccessibilityNodeInfo node) {
130 | boolean isNaf = node.isClickable() && node.isEnabled() && safeCharSeqToString(node.getContentDescription()).isEmpty() && safeCharSeqToString(node.getText()).isEmpty();
131 | if (!isNaf) return true;
132 | // check children since sometimes the containing element is clickable
133 | // and NAF but a child's text or description is available. Will assume
134 | // such layout as fine.
135 | return childNafCheck(node);
136 | }
137 |
138 | /**
139 | * This should be used when it's already determined that the node is NAF and a further check of
140 | * its children is in order. A node maybe a container such as LinerLayout and may be set to be
141 | * clickable but have no text or content description but it is counting on one of its children
142 | * to fulfill the requirement for being accessibility friendly by having one or more of its
143 | * children fill the text or content-description. Such a combination is considered by this
144 | * dumper as acceptable for accessibility.
145 | *
146 | * @return false if node fails the check.
147 | */
148 | private static boolean childNafCheck(AccessibilityNodeInfo node) {
149 | int childCount = node.getChildCount();
150 | for (int x = 0; x < childCount; x++) {
151 | AccessibilityNodeInfo childNode = node.getChild(x);
152 | if (childNode == null) {
153 | Log.i(TAG, String.format("Null child %d/%d, parent: %s", x, childCount, node));
154 | continue;
155 | }
156 | if (!safeCharSeqToString(childNode.getContentDescription()).isEmpty() || !safeCharSeqToString(childNode.getText()).isEmpty())
157 | return true;
158 | if (childNafCheck(childNode)) return true;
159 | }
160 | return false;
161 | }
162 |
163 | private static String safeCharSeqToString(CharSequence cs) {
164 | if (cs == null) return "";
165 | else {
166 | return stripInvalidXMLChars(escapeXml(cs.toString()));
167 | }
168 | }
169 |
170 | private static String escapeXml(String input) {
171 | StringBuilder escaped = new StringBuilder();
172 | for (char c : input.toCharArray()) {
173 | switch (c) {
174 | case '"':
175 | escaped.append(""");
176 | break;
177 | case '\'':
178 | escaped.append("'");
179 | break;
180 | case '<':
181 | escaped.append("<");
182 | break;
183 | case '>':
184 | escaped.append(">");
185 | break;
186 | case '&':
187 | escaped.append("&");
188 | break;
189 | default:
190 | escaped.append(c);
191 | }
192 | }
193 | return escaped.toString();
194 | }
195 |
196 | // Original Google code here broke UTF characters
197 | private static String stripInvalidXMLChars(CharSequence charSequence) {
198 | return XML10Pattern.matcher(charSequence).replaceAll("?");
199 | }
200 | }
201 |
--------------------------------------------------------------------------------
/app/src/main/java/com/ysbing/yadb/layout/AccessibilityNodeInfoHelper.java:
--------------------------------------------------------------------------------
1 | package com.ysbing.yadb.layout;
2 |
3 | import android.graphics.Rect;
4 | import android.view.accessibility.AccessibilityNodeInfo;
5 |
6 | class AccessibilityNodeInfoHelper {
7 | AccessibilityNodeInfoHelper() {
8 | }
9 |
10 | static Rect getVisibleBoundsInScreen(AccessibilityNodeInfo node, int width, int height) {
11 | if (node == null) {
12 | return null;
13 | }
14 | Rect nodeRect = new Rect();
15 | node.getBoundsInScreen(nodeRect);
16 | Rect displayRect = new Rect();
17 | displayRect.top = 0;
18 | displayRect.left = 0;
19 | displayRect.right = width;
20 | displayRect.bottom = height;
21 | if (nodeRect.intersect(displayRect)) {
22 | return nodeRect;
23 | }
24 | return new Rect();
25 | }
26 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/ysbing/yadb/layout/Layout.java:
--------------------------------------------------------------------------------
1 | package com.ysbing.yadb.layout;
2 |
3 | import android.content.Context;
4 | import android.hardware.display.IDisplayManager;
5 | import android.view.Display;
6 | import android.view.DisplayInfo;
7 |
8 | import java.io.File;
9 |
10 | public class Layout {
11 | private static final File LAYOUT_DEFAULT_FILE = new File("/data/local/tmp", "yadb_layout_dump.xml");
12 |
13 | public static void run(String path) throws Exception {
14 | DisplayInfo displayInfo = getDisplayInfo();
15 | if (displayInfo == null) {
16 | return;
17 | }
18 | if (path == null) {
19 | LayoutShell.get(LAYOUT_DEFAULT_FILE, displayInfo);
20 | } else {
21 | LayoutShell.get(new File(path), displayInfo);
22 | }
23 | }
24 |
25 | private static DisplayInfo getDisplayInfo() {
26 | IDisplayManager clipboard = IDisplayManager.Stub.asInterface(android.os.ServiceManager.getService(Context.DISPLAY_SERVICE));
27 | if (clipboard == null) {
28 | return null;
29 | }
30 | return clipboard.getDisplayInfo(Display.DEFAULT_DISPLAY);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/app/src/main/java/com/ysbing/yadb/layout/LayoutShell.java:
--------------------------------------------------------------------------------
1 | package com.ysbing.yadb.layout;
2 |
3 | import android.app.UiAutomation;
4 | import android.app.UiAutomationConnection;
5 | import android.os.HandlerThread;
6 | import android.view.DisplayInfo;
7 | import android.view.accessibility.AccessibilityNodeInfo;
8 |
9 | import java.io.File;
10 | import java.io.FileWriter;
11 | import java.util.concurrent.TimeoutException;
12 |
13 | public class LayoutShell {
14 | private static final String HANDLER_THREAD_NAME = "LayoutShellThread";
15 | private final HandlerThread mHandlerThread = new HandlerThread(HANDLER_THREAD_NAME);
16 | private UiAutomation mUiAutomation = null;
17 |
18 | public static void get(File file, DisplayInfo displayInfo) throws Exception {
19 | LayoutShell shell = new LayoutShell();
20 | try {
21 | shell.connect();
22 | AccessibilityNodeInfo info;
23 | long startTime = System.currentTimeMillis();
24 | do {
25 | if (System.currentTimeMillis() - startTime >= 5000) {
26 | throw new TimeoutException();
27 | }
28 | info = shell.mUiAutomation.getRootInActiveWindow();
29 | } while (info == null);
30 | String content = AccessibilityNodeInfoDumper.getWindowXMLHierarchy(info, displayInfo);
31 | FileWriter writer = new FileWriter(file);
32 | writer.write(XmlUtil.formatXml(content));
33 | writer.close();
34 | System.out.println("layout dumped to:" + file.getAbsolutePath());
35 | } finally {
36 | shell.disconnect();
37 | }
38 | }
39 |
40 | public void connect() {
41 | if (mHandlerThread.isAlive()) {
42 | throw new IllegalStateException("Already connected!");
43 | }
44 | mHandlerThread.start();
45 | mUiAutomation = new UiAutomation(mHandlerThread.getLooper(), new UiAutomationConnection());
46 | mUiAutomation.connect();
47 | }
48 |
49 | public void disconnect() {
50 | if (!mHandlerThread.isAlive()) {
51 | throw new IllegalStateException("Already disconnected!");
52 | }
53 | mUiAutomation.disconnect();
54 | mHandlerThread.quit();
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/app/src/main/java/com/ysbing/yadb/layout/XmlUtil.java:
--------------------------------------------------------------------------------
1 | package com.ysbing.yadb.layout;
2 |
3 | import org.xml.sax.InputSource;
4 |
5 | import java.io.ByteArrayInputStream;
6 | import java.io.ByteArrayOutputStream;
7 | import java.util.regex.Matcher;
8 | import java.util.regex.Pattern;
9 |
10 | import javax.xml.transform.OutputKeys;
11 | import javax.xml.transform.Source;
12 | import javax.xml.transform.Transformer;
13 | import javax.xml.transform.sax.SAXSource;
14 | import javax.xml.transform.sax.SAXTransformerFactory;
15 | import javax.xml.transform.stream.StreamResult;
16 |
17 | public class XmlUtil {
18 | private static final Pattern unescapeXmlPattern = Pattern
19 | .compile("&((lt)|(gt)|(quot)|(apos)|(amp)|#(\\d+)|#x(\\p{XDigit}+));");
20 |
21 | public static String formatXml(String xml) {
22 | try {
23 | Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();
24 | serializer.setOutputProperty(OutputKeys.INDENT, "yes");
25 | serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
26 | Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes())));
27 | StreamResult res = new StreamResult(new ByteArrayOutputStream());
28 | serializer.transform(xmlSource, res);
29 | return XmlUtil.unescapeXml(res.getOutputStream().toString());
30 | } catch (Exception e) {
31 | return xml;
32 | }
33 | }
34 |
35 | private static String unescapeXml(String str) {
36 | return replaceAll(str, groups -> {
37 | if (groups[2] != null) {
38 | return "<";
39 | } else if (groups[3] != null) {
40 | return ">";
41 | } else if (groups[4] != null) {
42 | return "\"";
43 | } else if (groups[5] != null) {
44 | return "'";
45 | } else if (groups[6] != null) {
46 | return "&";
47 | } else if (groups[7] != null) {
48 | StringBuilder c = new StringBuilder();
49 | c.appendCodePoint(Integer.parseInt(groups[7]));
50 | return c.toString();
51 | } else if (groups[8] != null) {
52 | StringBuilder c = new StringBuilder();
53 | c.appendCodePoint(Integer.parseInt(groups[8], 16));
54 | return c.toString();
55 | }
56 | return "";
57 | });
58 | }
59 |
60 | private static String replaceAll(String str, Function replFunc) {
61 | StringBuffer sb = new StringBuffer();
62 | Matcher mat = XmlUtil.unescapeXmlPattern.matcher(str);
63 | while (mat.find()) {
64 | String[] greps = new String[mat.groupCount() + 1];
65 | for (int idx = 0; idx < mat.groupCount() + 1; idx++) {
66 | greps[idx] = mat.group(idx);
67 | }
68 | String repl = replFunc.apply(greps);
69 | mat.appendReplacement(sb, repl);
70 | }
71 | mat.appendTail(sb);
72 | return sb.toString();
73 | }
74 |
75 | public interface Function {
76 | R apply(T input);
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/app/src/main/java/com/ysbing/yadb/screenshot/Screenshot.java:
--------------------------------------------------------------------------------
1 | package com.ysbing.yadb.screenshot;
2 |
3 | import android.content.Context;
4 | import android.graphics.Bitmap;
5 | import android.graphics.Matrix;
6 | import android.graphics.PixelFormat;
7 | import android.graphics.Rect;
8 | import android.hardware.display.DisplayManager;
9 | import android.hardware.display.IDisplayManager;
10 | import android.media.Image;
11 | import android.media.ImageReader;
12 | import android.os.Build;
13 | import android.os.IBinder;
14 | import android.view.Display;
15 | import android.view.DisplayInfo;
16 | import android.view.Surface;
17 | import android.view.SurfaceControl;
18 |
19 | import java.io.BufferedOutputStream;
20 | import java.io.File;
21 | import java.io.FileOutputStream;
22 | import java.nio.ByteBuffer;
23 | import java.util.concurrent.TimeoutException;
24 |
25 | public class Screenshot {
26 | private static final File SCREENSHOT_DEFAULT_FILE = new File("/data/local/tmp", "yadb_screenshot.png");
27 |
28 | public static void run(String path) throws Exception {
29 | DisplayInfo displayInfo = getDisplayInfo();
30 | if (displayInfo == null) {
31 | return;
32 | }
33 | boolean secure = Build.VERSION.SDK_INT < Build.VERSION_CODES.R
34 | || (Build.VERSION.SDK_INT == Build.VERSION_CODES.R && !"S".equals(Build.VERSION.CODENAME));
35 | IBinder iBinder = SurfaceControl.createDisplay("yadb", secure);
36 | Rect rect = new Rect(0, 0, displayInfo.logicalWidth, displayInfo.logicalHeight);
37 | int imageWidth, imageHeight;
38 | int rotation = displayInfo.rotation;
39 | //横屏
40 | if (rotation == 1 || rotation == 3) {
41 | imageWidth = displayInfo.logicalHeight;
42 | imageHeight = displayInfo.logicalWidth;
43 | } else {
44 | imageWidth = displayInfo.logicalWidth;
45 | imageHeight = displayInfo.logicalHeight;
46 | }
47 | ImageReader imageReader = ImageReader.newInstance(imageWidth, imageHeight, PixelFormat.RGBA_8888, 1);
48 | Surface surface = imageReader.getSurface();
49 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
50 | SurfaceControl.openTransaction();
51 | try {
52 | SurfaceControl.setDisplaySurface(iBinder, surface);
53 | SurfaceControl.setDisplayProjection(iBinder, displayInfo.rotation, rect, rect);
54 | SurfaceControl.setDisplayLayerStack(iBinder, displayInfo.layerStack);
55 | } finally {
56 | SurfaceControl.closeTransaction();
57 | }
58 | } else {
59 | DisplayManager.createVirtualDisplay("yadb", imageWidth, imageHeight, Display.DEFAULT_DISPLAY, surface);
60 | }
61 | Image image;
62 | long startTime = System.currentTimeMillis();
63 | do {
64 | if (System.currentTimeMillis() - startTime >= 5000) {
65 | throw new TimeoutException();
66 | }
67 | image = imageReader.acquireLatestImage();
68 | } while (image == null);
69 | Image.Plane[] planes = image.getPlanes();
70 | ByteBuffer buffer = planes[0].getBuffer();
71 | int width = image.getWidth();
72 | int height = image.getHeight();
73 | System.out.println("image:" + image + ",width:" + width + ",height:" + height + ",rotation:" + rotation);
74 | Matrix matrix = new Matrix();
75 | switch (rotation) {
76 | case 0:
77 | matrix.postRotate(0);
78 | break;
79 | case 1:
80 | matrix.postRotate(270);
81 | break;
82 | case 2:
83 | matrix.postRotate(180);
84 | break;
85 | case 3:
86 | matrix.postRotate(90);
87 | break;
88 | default:
89 | break;
90 | }
91 | int pixelStride = planes[0].getPixelStride(), rowStride = planes[0].getRowStride(), rowPadding = rowStride - pixelStride * width;
92 | Bitmap bitmap = Bitmap.createBitmap(width + rowPadding / pixelStride, height, Bitmap.Config.ARGB_8888);
93 | bitmap.copyPixelsFromBuffer(buffer);
94 | image.close();
95 | SurfaceControl.destroyDisplay(iBinder);
96 | surface.release();
97 | Bitmap resultBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
98 | bitmap.recycle();
99 | File file;
100 | if (path == null) {
101 | file = SCREENSHOT_DEFAULT_FILE;
102 | } else {
103 | file = new File(path);
104 | }
105 | FileOutputStream fos = new FileOutputStream(file);
106 | BufferedOutputStream bos = new BufferedOutputStream(fos);
107 | resultBitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
108 | bos.flush();
109 | bos.close();
110 | fos.close();
111 | resultBitmap.recycle();
112 | System.out.println("screenshot success:" + file.getAbsolutePath());
113 | }
114 |
115 | private static DisplayInfo getDisplayInfo() {
116 | IDisplayManager clipboard = IDisplayManager.Stub.asInterface(android.os.ServiceManager.getService(Context.DISPLAY_SERVICE));
117 | if (clipboard == null) {
118 | return null;
119 | }
120 | return clipboard.getDisplayInfo(Display.DEFAULT_DISPLAY);
121 | }
122 | }
123 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 | buildscript {
3 | repositories {
4 | maven { url 'https://maven.aliyun.com/repository/public' }
5 | google()
6 | mavenCentral()
7 | }
8 | dependencies {
9 | classpath 'com.android.tools.build:gradle:7.4.2'
10 | classpath 'com.ysbing.yrouter:YRouter-gradle-plugin:1.2.3'
11 |
12 | // NOTE: Do not place your application dependencies here; they belong
13 | // in the individual module build.gradle files
14 | }
15 | }
16 |
17 | allprojects {
18 | repositories {
19 | maven { url 'https://maven.aliyun.com/repository/public' }
20 | google()
21 | mavenCentral()
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx2048m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app"s APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ysbing/YADB/fba90800fce1c088526d972711ec91a36c9272d8/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue May 30 17:11:33 CST 2023
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.4-all.zip
5 | zipStoreBase=GRADLE_USER_HOME
6 | zipStorePath=wrapper/dists
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 | rootProject.name = "yadb"
--------------------------------------------------------------------------------
/windows.bat:
--------------------------------------------------------------------------------
1 | chcp 65001
2 | adb push yadb /data/local/tmp
3 | adb shell app_process -Djava.class.path=/data/local/tmp/yadb /data/local/tmp com.ysbing.yadb.Main -keyboard \"你好,\n世 界\"
4 | pause
--------------------------------------------------------------------------------
/yadb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ysbing/YADB/fba90800fce1c088526d972711ec91a36c9272d8/yadb
--------------------------------------------------------------------------------