@flxapps/adbax 0.0.0-stage → 0.1.0
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/LICENSE +202 -0
- package/NOTICE +2 -0
- package/README.md +132 -2
- package/bin/adbax.js +15 -0
- package/docs/architecture.md +166 -0
- package/docs/commands.md +163 -0
- package/package.json +18 -4
- package/server/adbax-server.jar +0 -0
- package/skill/SKILL.md +143 -0
- package/src/adb.js +120 -0
- package/src/args.js +37 -0
- package/src/backends/server.js +297 -0
- package/src/backends/shell.js +69 -0
- package/src/cli.js +80 -0
- package/src/commands/apps.js +90 -0
- package/src/commands/common.js +39 -0
- package/src/commands/device.js +73 -0
- package/src/commands/screen.js +112 -0
- package/src/commands/server.js +34 -0
- package/src/commands/system.js +102 -0
- package/src/commands/touch.js +210 -0
- package/src/device.js +197 -0
- package/src/select.js +95 -0
- package/src/tree.js +183 -0
- package/src/util.js +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,133 @@
|
|
|
1
|
-
#
|
|
1
|
+
# adbax
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
adbax drives an Android phone or emulator by its accessibility tree. Run
|
|
4
|
+
`adbax ui` and you get the screen as a numbered list of labels with their
|
|
5
|
+
positions; `adbax tap "Sign in"` then taps whatever is labelled Sign in. You
|
|
6
|
+
never squint at a screenshot or guess a coordinate. The name is adb plus AX, a
|
|
7
|
+
common shorthand for accessibility.
|
|
8
|
+
|
|
9
|
+
I built it for Claude Code, after watching it screenshot the emulator, estimate
|
|
10
|
+
where a button was and tap beside it, again and again. Typed by hand or run from
|
|
11
|
+
a shell script, the commands behave the same.
|
|
12
|
+
|
|
13
|
+
```console
|
|
14
|
+
$ adbax ui
|
|
15
|
+
# com.android.settings/.Settings 1080x1920 11 nodes
|
|
16
|
+
[0] TAP "Search settings" id=search_action_bar @540,147 1038x126
|
|
17
|
+
[5] TAP "Network & Internet / Wi‑Fi, mobile, data usage" id=dashboard_tile @540,903 1080x189
|
|
18
|
+
[6] TAP "Connected devices / Bluetooth, Cast" id=dashboard_tile @540,1092 1080x189
|
|
19
|
+
...
|
|
20
|
+
$ adbax tap "Network & Internet" --ui
|
|
21
|
+
tapped TAP "Network & Internet" id=dashboard_tile @540,903
|
|
22
|
+
# com.android.settings/.Settings$NetworkDashboardActivity 1080x1920 12 nodes
|
|
23
|
+
[0] TAP "Navigate up" @74,137 147x147
|
|
24
|
+
...
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Install
|
|
28
|
+
|
|
29
|
+
You need Node 18 or later and adb, either on the PATH or in the SDK that
|
|
30
|
+
`ANDROID_HOME` points to. adbax itself has zero dependencies.
|
|
31
|
+
|
|
32
|
+
```sh
|
|
33
|
+
npm install -g @flxapps/adbax
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
npm wouldn't take the bare name, hence the scope. The command is `adbax`.
|
|
37
|
+
|
|
38
|
+
## Usage
|
|
39
|
+
|
|
40
|
+
```sh
|
|
41
|
+
adbax ui # the screen; --all for every node, --json for scripts
|
|
42
|
+
adbax tap "Sign in" # by text, content-desc or resource-id
|
|
43
|
+
adbax tap 5 # row 5 of the last `ui`, no fresh read
|
|
44
|
+
adbax tap "Sign in" --ui # act, then print the screen it led to
|
|
45
|
+
adbax type you@example.com --into Email --enter
|
|
46
|
+
adbax scroll-to "Delete account" --tap
|
|
47
|
+
adbax wait "Welcome" --timeout 20
|
|
48
|
+
adbax launch com.example.app # returns once the activity is drawn
|
|
49
|
+
adbax screenshot --small # 540 px wide JPEG
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Every invocation is a separate process, which means steps string together with
|
|
53
|
+
`&&`.
|
|
54
|
+
|
|
55
|
+
Matching ignores case and prefers an exact hit, then a prefix, then a
|
|
56
|
+
substring. If what matched can't be tapped itself, like the title text inside a
|
|
57
|
+
list row, adbax goes up to the closest clickable parent. When a selector fits
|
|
58
|
+
more than one node, adbax won't choose for you. It lists them and exits with
|
|
59
|
+
status 2:
|
|
60
|
+
|
|
61
|
+
```console
|
|
62
|
+
$ adbax tap "Play Store"
|
|
63
|
+
adbax: "Play Store" matches 2 nodes:
|
|
64
|
+
--nth 0 TAP "Play Store" @158,1679
|
|
65
|
+
--nth 1 TAP "Play Store" @923,2003
|
|
66
|
+
Narrow the query, or add --nth N / --first.
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
A game, or anything else painted onto a canvas, exposes no tree at all. There,
|
|
70
|
+
`adbax screenshot --grid` draws a labelled coordinate grid over the image and
|
|
71
|
+
`adbax tap 270,480 --shot` takes coordinates in that image's own pixels. Whoever
|
|
72
|
+
reads the picture, usually a model, taps what it sees without converting to
|
|
73
|
+
screen size.
|
|
74
|
+
|
|
75
|
+
Dark mode, font scale, logcat, the environment variables and every other
|
|
76
|
+
command are in [docs/commands.md](docs/commands.md).
|
|
77
|
+
|
|
78
|
+
## The on-device server
|
|
79
|
+
|
|
80
|
+
Reading the screen with `uiautomator dump` costs roughly two seconds, even when
|
|
81
|
+
there's hardly anything on it. Each run boots a fresh VM, and since a brand-new
|
|
82
|
+
connection has no idea when the screen last changed, it then sits through a
|
|
83
|
+
whole second of waiting for things to go idle.
|
|
84
|
+
|
|
85
|
+
adbax gets around that. The first time you use it, it copies a 13 KB jar to
|
|
86
|
+
`/data/local/tmp` and launches it through `app_process` as the shell user:
|
|
87
|
+
there's no APK, and nothing asks for permission. That server holds one
|
|
88
|
+
accessibility connection for as long as it runs, so it always knows when the
|
|
89
|
+
screen last moved. Measured end to end on an arm64 Android 13 emulator,
|
|
90
|
+
`adbax ui` goes from 2.2 s down to 0.21-0.27 s, and most of what's left is Node
|
|
91
|
+
starting up. Only the very first call is slower, about a second, because it
|
|
92
|
+
starts the server. Taps, keys and swipes travel over that connection too, and
|
|
93
|
+
after each one adbax waits until the accessibility events die down rather than
|
|
94
|
+
sleeping for some fixed delay.
|
|
95
|
+
|
|
96
|
+
The server wants Android 7.0 (API 24) or later, and I've tried it on 8.1, 13
|
|
97
|
+
and 17. Older devices, or `--no-server`, get the same commands on top of
|
|
98
|
+
`uiautomator dump` and `input`, at the old speed.
|
|
99
|
+
|
|
100
|
+
There is one catch. Android hands out a single UiAutomation connection at a
|
|
101
|
+
time, so while the server holds it, UiAutomator and screenshot tests, Appium and
|
|
102
|
+
Maestro are locked out. After ten minutes without a request it exits on its
|
|
103
|
+
own, and `adbax server stop` releases it immediately.
|
|
104
|
+
|
|
105
|
+
The internals are written up in [docs/architecture.md](docs/architecture.md).
|
|
106
|
+
|
|
107
|
+
## Compared with an MCP server
|
|
108
|
+
|
|
109
|
+
Shell commands can be chained. For an agent,
|
|
110
|
+
`adbax launch com.example.app && adbax wait Start && adbax tap Settings --ui` is a
|
|
111
|
+
single tool call. Through an MCP server the same steps take four calls, and
|
|
112
|
+
every one of them goes back through the model. You can also type the identical
|
|
113
|
+
commands yourself, put them in CI, or hand them to any agent with shell access,
|
|
114
|
+
with no client to configure.
|
|
115
|
+
|
|
116
|
+
The price is that you need the shell. Chat apps that don't have one can't run
|
|
117
|
+
adbax at all. There's no schema either, so an agent picks the commands up from
|
|
118
|
+
`adbax help` or from the skill below. And a screenshot arrives as a path the
|
|
119
|
+
agent has to open, whereas an MCP tool returns the picture directly.
|
|
120
|
+
|
|
121
|
+
## With Claude Code
|
|
122
|
+
|
|
123
|
+
[`skill/SKILL.md`](skill/SKILL.md) shows an agent how to work with it: read the
|
|
124
|
+
tree, act on labels, chain steps, and keep screenshots for screens that have
|
|
125
|
+
nothing to read.
|
|
126
|
+
|
|
127
|
+
```sh
|
|
128
|
+
ln -s "$(npm root -g)/@flxapps/adbax/skill" ~/.claude/skills/android-device
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## License
|
|
132
|
+
|
|
133
|
+
Apache 2.0, see [LICENSE](LICENSE).
|
package/bin/adbax.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { main } from '../src/cli.js';
|
|
4
|
+
|
|
5
|
+
main(process.argv.slice(2)).then(
|
|
6
|
+
(code) => process.exit(code),
|
|
7
|
+
(err) => {
|
|
8
|
+
if (typeof err.code === 'number') {
|
|
9
|
+
process.stderr.write(`adbax: ${err.message}\n`);
|
|
10
|
+
process.exit(err.code);
|
|
11
|
+
}
|
|
12
|
+
process.stderr.write(`adbax: ${err.stack || err}\n`);
|
|
13
|
+
process.exit(1);
|
|
14
|
+
},
|
|
15
|
+
);
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# How adbax works
|
|
2
|
+
|
|
3
|
+
Two halves: a Node CLI on the host, and a small Java server that runs on the
|
|
4
|
+
device. The CLI works without the server, just slower.
|
|
5
|
+
|
|
6
|
+
```
|
|
7
|
+
bin/adbax.js entry point, turns errors into exit codes
|
|
8
|
+
src/cli.js command table and help
|
|
9
|
+
src/args.js argv into command, positionals and flags
|
|
10
|
+
src/adb.js finding adb, choosing the device, running adb
|
|
11
|
+
src/device.js one device: picks a backend, reads the tree, acts
|
|
12
|
+
src/backends/server.js host half of the on-device server
|
|
13
|
+
src/backends/shell.js the fallback: uiautomator dump and `input`
|
|
14
|
+
src/tree.js XML into nodes, nodes into rows, rows into text
|
|
15
|
+
src/select.js matching a selector against the tree
|
|
16
|
+
src/commands/*.js one file per group of commands
|
|
17
|
+
server/src/ the on-device server
|
|
18
|
+
server/build.sh javac + d8 into server/adbax-server.jar
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Commands only ever talk to a `Device`. Which backend sits behind it is decided
|
|
22
|
+
once per call, the first time it is needed.
|
|
23
|
+
|
|
24
|
+
## Why `uiautomator dump` is slow
|
|
25
|
+
|
|
26
|
+
`adb shell uiautomator dump` runs `/system/framework/uiautomator.jar` through
|
|
27
|
+
`app_process`, so every call:
|
|
28
|
+
|
|
29
|
+
1. starts a VM (a few hundred ms, more on old releases),
|
|
30
|
+
2. registers a new UiAutomation connection with the accessibility manager,
|
|
31
|
+
3. calls `waitForIdle(1000, 10000)`. A fresh connection has never seen an
|
|
32
|
+
event, so it counts "idle" from the moment it connected and always waits the
|
|
33
|
+
full second,
|
|
34
|
+
4. dumps the active window to a file and exits.
|
|
35
|
+
|
|
36
|
+
Steps 1 and 3 are almost all of the two seconds, and both exist only because
|
|
37
|
+
the process is new.
|
|
38
|
+
|
|
39
|
+
## The server
|
|
40
|
+
|
|
41
|
+
`server/src/es/flx/adbax` is about 870 lines of Java, compiled against
|
|
42
|
+
`android.jar` and dexed with d8. adbax pushes the jar to
|
|
43
|
+
`/data/local/tmp/adbax-server.jar` and starts it:
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
CLASSPATH=/data/local/tmp/adbax-server.jar nohup app_process / --nice-name=adbax-server \
|
|
47
|
+
es.flx.adbax.Main --socket adbax --token <jar md5> --serial <adb serial> --idle 600
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
It runs as the shell user, the same as `uiautomator`, so it may inject input and
|
|
51
|
+
read every window, and it needs no APK and no permission prompt.
|
|
52
|
+
|
|
53
|
+
- `Automation` creates and connects the UiAutomation through hidden API, by
|
|
54
|
+
reflection. The constructor takes a Looper up to Android 13 and may take a
|
|
55
|
+
Context later, so both are tried. It connects with
|
|
56
|
+
`FLAG_DONT_SUPPRESS_ACCESSIBILITY_SERVICES`, so TalkBack keeps running. The
|
|
57
|
+
same class finds the top activity: `getStackInfo(getFocusedStackId())` up to
|
|
58
|
+
8.1, `getFocusedStackInfo` from 9, on IActivityTaskManager from 10,
|
|
59
|
+
`getFocusedRootTaskInfo` from 12.
|
|
60
|
+
- `Hierarchy` writes the tree in the format `uiautomator dump` uses: the same
|
|
61
|
+
attributes in the same order, the same rule of skipping children that are not
|
|
62
|
+
visible to the user, bounds clipped to the display. The host parses both with
|
|
63
|
+
one parser, and the fallback prints the same rows.
|
|
64
|
+
- `Input` injects touches and keys through the connection, and types through
|
|
65
|
+
the virtual keyboard's key map, falling back to `ACTION_SET_TEXT` for
|
|
66
|
+
characters the map does not have.
|
|
67
|
+
- `Grid` draws the screenshot grid. Its labels are stroked seven-segment digits:
|
|
68
|
+
an `app_process` has no font setup, and drawing text on Android 8 aborts it.
|
|
69
|
+
- `Server` listens on the abstract unix socket `@adbax`.
|
|
70
|
+
|
|
71
|
+
### Protocol
|
|
72
|
+
|
|
73
|
+
One request per connection. The host writes one line of JSON and the server
|
|
74
|
+
answers with one line of JSON, then the payload (XML or image bytes) until it
|
|
75
|
+
closes the socket.
|
|
76
|
+
|
|
77
|
+
```
|
|
78
|
+
> {"cmd":"dump","settle":150,"idle":300,"timeout":3000}
|
|
79
|
+
< {"ok":true,"busy":false,"activity":"com.android.settings/.SubSettings","width":1080,
|
|
80
|
+
"height":2400,"rotation":0,"waitMs":212,"dumpMs":18,"token":"7fa7…","serial":"emulator-5554"}
|
|
81
|
+
< <?xml version='1.0' …><hierarchy …>…</hierarchy>
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
| cmd | |
|
|
85
|
+
| --- | --- |
|
|
86
|
+
| `ping` | sdk, pid, uptime |
|
|
87
|
+
| `dump` | `settle`, `idle`, `timeout`, `windows` |
|
|
88
|
+
| `screenshot` | `width`, `format` (`png`/`jpeg`), `quality`, `grid` |
|
|
89
|
+
| `tap` | `x`, `y` |
|
|
90
|
+
| `swipe` | `x1`, `y1`, `x2`, `y2`, `ms` |
|
|
91
|
+
| `key` | `code` or `name`, `long` |
|
|
92
|
+
| `text` | `text`: key events, or `ACTION_SET_TEXT` for what the key map lacks |
|
|
93
|
+
| `settext` | `text`, `append`: always `ACTION_SET_TEXT` |
|
|
94
|
+
| `focus` | the node with input focus: editable, text, password |
|
|
95
|
+
| `activity` | the top activity |
|
|
96
|
+
| `quit` | disconnect and exit |
|
|
97
|
+
|
|
98
|
+
Requests are served one at a time. A client that connects and sends nothing is
|
|
99
|
+
dropped after 10 s.
|
|
100
|
+
|
|
101
|
+
### Reading after an action
|
|
102
|
+
|
|
103
|
+
A plain `dump` waits up to 2 s for 300 ms without accessibility events. On a
|
|
104
|
+
settled screen the last event is long past and that returns at once. That is
|
|
105
|
+
the part a fresh connection cannot do.
|
|
106
|
+
|
|
107
|
+
After an action the host sends `settle: 150`. The server sleeps that long so
|
|
108
|
+
the app has started reacting, waits for the events to stop, and then compares
|
|
109
|
+
the top activity's package with the package of the active window. When they
|
|
110
|
+
differ, an activity switch has not reached the accessibility tree yet, and it
|
|
111
|
+
keeps waiting until they agree or `timeout` runs out. It only checks that after
|
|
112
|
+
an action: with the notification shade or a system dialog open the two disagree
|
|
113
|
+
for as long as it stays open, and a plain read should not wait that out.
|
|
114
|
+
|
|
115
|
+
One gap remains: a new activity of the same package can go quiet before its
|
|
116
|
+
content is attached, which reads as an empty screen. If a read after an action
|
|
117
|
+
has no rows, the host reads again every 250 ms until something shows or two
|
|
118
|
+
reads come back identical. A game's canvas has no rows either, and two
|
|
119
|
+
identical reads end the loop for it after a quarter of a second.
|
|
120
|
+
|
|
121
|
+
### The node cache
|
|
122
|
+
|
|
123
|
+
The framework caches accessibility nodes per connection and invalidates them
|
|
124
|
+
from accessibility events. A long-lived connection depends on every toolkit
|
|
125
|
+
sending the right events, and they do not: a Compose text field kept reporting
|
|
126
|
+
the text of three edits ago. So the server clears the cache before every dump
|
|
127
|
+
and before every look at the focused field. `UiAutomation.clearCache()` exists
|
|
128
|
+
from Android 14; before that it is `AccessibilityInteractionClient.clearCache(id)`,
|
|
129
|
+
which is what `setServiceInfo()` calls first. On a settled screen a full read
|
|
130
|
+
still takes 10-15 ms.
|
|
131
|
+
|
|
132
|
+
### Lifecycle and versioning
|
|
133
|
+
|
|
134
|
+
- **Start.** On the first command that needs it. The host pushes the jar when
|
|
135
|
+
the MD5 on the device differs, kills any older server (only one UiAutomation
|
|
136
|
+
client may exist), starts the new one and polls `ping` until it answers.
|
|
137
|
+
- **Version.** The jar's MD5 is passed as `--token` and echoed in every reply. A
|
|
138
|
+
server answering with another token is an older build, so the host sends it
|
|
139
|
+
`quit` and starts the current one.
|
|
140
|
+
- **Port.** `adb forward tcp:0 localabstract:adbax` picks a free port, which is
|
|
141
|
+
cached in `~/.cache/adbax/port-<serial>` to save an `adb forward --list` per
|
|
142
|
+
call. The server echoes its `--serial`, so a cached port that adb has since
|
|
143
|
+
given to another device is noticed rather than used.
|
|
144
|
+
- **Exit.** After `--idle` seconds without a request (600 by default), on
|
|
145
|
+
`quit`, or when killed. The UiAutomation registration dies with the process.
|
|
146
|
+
- **Failure.** A server that cannot start on a device (below API 24, or a stack
|
|
147
|
+
trace in its log) is recorded in `~/.cache/adbax/server-<serial>.json` against
|
|
148
|
+
the jar's MD5, and adbax stays on the shell backend for that device until the
|
|
149
|
+
jar changes or `adbax server start` asks again. If another UiAutomation client
|
|
150
|
+
holds the connection, adbax says so and does not record anything.
|
|
151
|
+
|
|
152
|
+
### Interactive windows
|
|
153
|
+
|
|
154
|
+
`getWindows()` only works with `FLAG_RETRIEVE_INTERACTIVE_WINDOWS` set. With that
|
|
155
|
+
flag on, the framework also reports views scrolled underneath other views as
|
|
156
|
+
invisible, so a dump stops matching what `uiautomator dump` prints for the same
|
|
157
|
+
screen (on the Android 17 Settings home, 81 nodes against 88). The server
|
|
158
|
+
therefore turns the flag on only for the duration of a `--windows` dump.
|
|
159
|
+
|
|
160
|
+
## Building
|
|
161
|
+
|
|
162
|
+
`server/build.sh` compiles with `javac -source 8 -target 8` against the newest
|
|
163
|
+
`android.jar` in the SDK and dexes with d8 at `--min-api 24`. The android.jar
|
|
164
|
+
stubs have no `LambdaMetafactory`, so the server uses anonymous classes instead
|
|
165
|
+
of lambdas. The jar is about 13 KB and checked in, so neither a clone nor the
|
|
166
|
+
npm package needs an Android SDK.
|