phone-tools 0.1.0__tar.gz

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.
@@ -0,0 +1,587 @@
1
+ Metadata-Version: 2.4
2
+ Name: phone-tools
3
+ Version: 0.1.0
4
+ Summary: Device-state helpers for an adb-attached Android phone, with a fake-adb test harness
5
+ Requires-Python: >=3.10
6
+ Description-Content-Type: text/markdown
7
+
8
+ # phone-tools
9
+
10
+ The Android-side toolkit shared by the phone projects: five stdlib-only
11
+ commands that report one phone's state, wake it, clear the keyguard, prepare it
12
+ for a device suite, and wait for a reboot. The same repository also carries the
13
+ DokeOS autostart toggler.
14
+
15
+ - `phone_tools/` — the Python package and the five console scripts:
16
+ `phone-state`, `phone-wake`, `phone-unlock`, `phone-prepare`, `phone-reboot`.
17
+ - `autotoggle/` — accessibility service that drives DokeOS's per-app
18
+ *Auto Start Management* list.
19
+
20
+ ## Device work
21
+
22
+ Read [`AGENTS.md`](AGENTS.md) before touching a phone: `phone-prepare --json
23
+ --stay-on` first, stop on a non-zero exit, the PIN never on a command line, and
24
+ `unknown` is not success. It is the contract the phone projects point at.
25
+
26
+ ## Install
27
+
28
+ ```bash
29
+ pip install phone-tools # PyPI, all five commands
30
+ nix run github:MBanucu/phone-tools # phone-state on the attached phone
31
+ nix run github:MBanucu/phone-tools -- --version
32
+ ```
33
+
34
+ From a checkout, with no build step and no third-party dependency:
35
+
36
+ ```bash
37
+ git clone https://github.com/MBanucu/phone-tools.git
38
+ cd phone-tools
39
+ pip install -e .
40
+ phone-prepare --json
41
+ ```
42
+
43
+ The Nix flake exposes `packages.<system>.phone-tools` for `x86_64-linux` and
44
+ `aarch64-linux`, with `default` pointing at it, and a `devShell` that puts the
45
+ package, `python3` and `adb` on `PATH`. It reads the same version as pip:
46
+
47
+ ```bash
48
+ nix build .#phone-tools
49
+ nix develop
50
+ nix run .# -- --version
51
+ ```
52
+
53
+ `phone_tools/VERSION` is the single version source. `pyproject.toml`, the flake
54
+ and `.github/workflows/release.yml` all read that one file; bump it once and
55
+ the tag must match it. Every command prints `<command> <version>` for
56
+ `--version`.
57
+
58
+ ## CLI contract
59
+
60
+ All five commands share one device-selection rule, one PIN rule and one set of
61
+ exit codes, so a suite can branch on the code instead of parsing prose.
62
+
63
+ | command | contract |
64
+ | --- | --- |
65
+ | `phone-state` | one JSON object describing the selected phone; never changes it |
66
+ | `phone-wake` | `KEYCODE_WAKEUP`, verified from display and power state; idempotent |
67
+ | `phone-unlock` | wake if needed, type the PIN, verify; bounded attempts, lockout-aware |
68
+ | `phone-prepare` | wake, `stay-on usb`, unlock, print the prepared state; one call per suite |
69
+ | `phone-reboot` | reboot, wait for `sys.boot_completed` and the package manager, optionally unlock |
70
+
71
+ Every command accepts `--serial`, `--adb PATH` and `--version`. The four
72
+ commands with human output also accept `--json` and `--verbose`;
73
+ `phone-state` always prints one JSON object and has neither flag. `--pin-file`
74
+ and `--attempts` are accepted by the commands that can type a PIN. `--json`
75
+ writes exactly **one compact JSON object on stdout, one line, with no prose**;
76
+ diagnostics go to stderr.
77
+
78
+ ### JSON shape
79
+
80
+ The fields are stable and snake_case. `unknown` is a real value for a field the
81
+ device does not expose (and `pin_source` is `null`); it is never a guess.
82
+
83
+ | command | keys and example |
84
+ | --- | --- |
85
+ | `phone-state` | `{"serial":"R58M12345AB","transport":"usb","screen":"on","keyguard":"dismissed","bouncer":"hidden","lockout":"none","stay_on":"usb","pin_source":"file"}` |
86
+ | `phone-wake` | `{"serial":"R58M12345AB","screen":"on","action":"woke"}` — `action` is `woke`, `already` or `failed` |
87
+ | `phone-unlock` | `{"serial":"R58M12345AB","action":"unlocked","attempts":1,"screen":"on","keyguard":"dismissed","bouncer":"hidden","lockout":"none","pin_source":"file"}` |
88
+ | `phone-prepare` | `{"serial":"R58M12345AB","transport":"usb","screen":"on","keyguard":"dismissed","stay_on":"usb","pin_source":"env"}` |
89
+ | `phone-reboot` | `{"serial":"R58M12345AB","transport":"usb","boot_completed":true,"pm_ready":true,"screen":"on","keyguard":"dismissed","stay_on":"usb","pin_source":"env"}` |
90
+
91
+ `phone-prepare --restore --json` prints `{"serial":...,"restored":true,"stay_on":...}`
92
+ instead of the prepared state. The PIN itself never appears in any object.
93
+
94
+ ### Exit codes
95
+
96
+ | exit | meaning |
97
+ | --- | --- |
98
+ | `0` | done, or already in the requested state |
99
+ | `1` | the device was reachable but the goal was not reached: the panel would not wake, the keyguard stayed locked, or the reboot wait timed out |
100
+ | `2` | usage error, including a missing `--yes` on a non-TTY and `--restore` with no record |
101
+ | `3` | no device, an ambiguous device, a requested serial that is not attached, adb could not run, or the state could not be read (`phone-prepare` also when the stay-on setting cannot be read) |
102
+ | `4` | lockout; the PIN was not typed again |
103
+ | `5` | no usable PIN source; `phone-reboot` exits 5 before rebooting |
104
+
105
+ Which commands use which: `phone-state` uses `0`, `2`, `3`; `phone-wake` uses
106
+ `0`, `1`, `2`, `3`; `phone-unlock`, `phone-prepare` and `phone-reboot` use all
107
+ six.
108
+
109
+ ### Device selection, and the two transports
110
+
111
+ `--serial` > `$ANDROID_SERIAL` > exactly one attached phone. USB and TCP entries
112
+ with the same `model`/`device` fingerprint are treated as **one phone**, which
113
+ is normal here: the same handset is often attached over USB and wireless adb at
114
+ the same time. The USB transport is chosen even when the requested serial was
115
+ the TCP one, and the same rule applies after `phone-reboot` reconnects it.
116
+
117
+ Two different phones, no usable device, a requested serial that is not attached,
118
+ or the same fingerprint twice with no USB entry is exit `3`, with the
119
+ `adb devices -l` list on stderr. The tool never guesses and never picks the
120
+ first line `adb` printed.
121
+
122
+ ### PIN resolution, and never on the command line
123
+
124
+ The PIN is resolved in this order:
125
+
126
+ 1. `$PHONE_PIN` when it is non-empty;
127
+ 2. `--pin-file PATH`;
128
+ 3. `$PHONE_SECRETS`;
129
+ 4. `/etc/nixos/phone-socks-secrets`.
130
+
131
+ There is deliberately **no `--pin`**. A PIN in `argv` is visible in `ps` and
132
+ shell history, so the value is read here, piped on stdin to the remote shell's
133
+ `read`, and passed to `input text` on the phone. It is absent from every adb
134
+ `argv`, from stdout, stderr and `--verbose`; `phone-state` only reports the
135
+ source as `env`, `file` or `null`.
136
+
137
+ If `$PHONE_SECRETS` points at a readable copy — for example
138
+ `~/.config/phone-tools/phone-socks-secrets`, a symlink to
139
+ `~/MEGA/unimportant files/phone-socks-secrets` — the tool needs no `sudo`. It
140
+ never calls `sudo` itself: an unreadable file is one message naming the file and
141
+ the two ways to provide the PIN, then exit `5`.
142
+
143
+ ### Attempts and lockout
144
+
145
+ `--attempts` defaults to `2` and is capped at `5`. An already-dismissed keyguard
146
+ is success and sends no `input text`. After a wrong entry the full state is read
147
+ again; if it reports a lockout the run stops immediately with exit `4` instead
148
+ of spending the remaining attempts. Every invocation starts with a fresh
149
+ lockout read, so repeating the command cannot build a wrong-PIN loop. A build
150
+ that exposes no lockout field reports `unknown`, which is not treated as
151
+ `none`; the state read and the post-attempt recheck are what bound the damage.
152
+
153
+ ## `phone-state`
154
+
155
+ `phone_tools/` is Python ≥3.10, standard library only, and shells out to `adb`;
156
+ it needs no Android SDK and no third-party package. `phone-state` prints one
157
+ JSON object and exits 0:
158
+
159
+ ```json
160
+ {"serial":"R58M12345AB","transport":"usb","screen":"on","keyguard":"dismissed","bouncer":"hidden","lockout":"none","stay_on":"usb","pin_source":"file"}
161
+ ```
162
+
163
+ | field | source |
164
+ | --- | --- |
165
+ | `serial`, `transport` | selected from `adb devices -l`; `transport` is `usb` or `tcp` |
166
+ | `screen` | `dumpsys display`'s `Display State=` and `dumpsys power`'s `mWakefulness=`; both must agree |
167
+ | `keyguard`, `bouncer` | one `dumpsys activity service com.android.systemui` dump: `mIsKeyguardShowing:` and `mBouncerVisible=` |
168
+ | `lockout` | lockout fields in the same SystemUI dump; `unknown` when the build exposes none |
169
+ | `stay_on` | `settings get global stay_on_while_plugged_in` read back as a bitmask |
170
+ | `pin_source` | `env` if `$PHONE_PIN` is set, else `file` if `$PHONE_SECRETS` has a `PHONE_PIN=` line, else `null` |
171
+
172
+ `unknown` is a real answer: a field the device does not expose prints
173
+ `unknown` (and `pin_source` prints `null`), never a guess. The vocabularies are
174
+ `screen`: `on`/`off`/`unknown`; `keyguard`: `showing`/`dismissed`/`unknown`;
175
+ `bouncer`: `visible`/`hidden`/`unknown`; `lockout`: `none`/`timer`/`unknown`;
176
+ `stay_on`: `none`, `ac`, `usb`, `wireless`, combinations such as `ac|usb`, or
177
+ `unknown`; `pin_source`: `env`/`file`/`null`. The PIN itself is never read into
178
+ the output, never printed, and never passed to `adb` as an argument;
179
+ `pin_source` only says where a PIN *could* come from.
180
+
181
+ ### Usage
182
+
183
+ ```bash
184
+ phone-state
185
+ phone-state --serial 192.168.77.232:5555
186
+ phone-state --adb ~/Android/Sdk/platform-tools/adb
187
+ phone-state --version
188
+ ```
189
+
190
+ ## `phone-wake`
191
+
192
+ `phone-wake` turns the panel on with `KEYCODE_WAKEUP` and nothing else. The
193
+ power key is a toggle on this device — a retry with it can switch the panel
194
+ back off — so `KEYCODE_POWER` does not appear in the package at all. An
195
+ already-on panel is success and sends no key.
196
+
197
+ When the panel is off, one `adb shell` runs the retry loop **on the phone**:
198
+ `KEYCODE_WAKEUP`, then `dumpsys display` and `dumpsys power` must both report
199
+ `Display State=ON` and `mWakefulness=Awake`. There is no host-side sleeping
200
+ between attempts.
201
+
202
+ ```bash
203
+ phone-wake # human text
204
+ phone-wake --json # {"serial":...,"screen":"on","action":"woke"}
205
+ phone-wake --serial 192.168.77.232:5555
206
+ ```
207
+
208
+ | option | meaning |
209
+ | --- | --- |
210
+ | `--serial SERIAL` | adb serial (default: `$ANDROID_SERIAL`, else the only attached phone) |
211
+ | `--adb PATH` | adb executable (default: `adb`) |
212
+ | `--json` | one JSON object instead of human text |
213
+ | `--verbose` | log the states and decisions on stderr |
214
+ | `--version` | print the package version |
215
+
216
+ Exit `0` means the panel is on (woken now or already on); `1` means it stayed
217
+ off; `2` is usage; `3` is no or ambiguous device.
218
+
219
+ ## `phone-unlock`
220
+
221
+ `phone-unlock` resolves the device and PIN first, then reads the state. An
222
+ already-dismissed keyguard is success and the bouncer is never touched: no
223
+ `input text` is sent. Otherwise it wakes the panel, raises the PIN bouncer with
224
+ `wm dismiss-keyguard`, types the PIN **once**, polls the keyguard on the phone,
225
+ and re-reads the full state to verify.
226
+
227
+ ```bash
228
+ PHONE_PIN=1234 phone-unlock
229
+ phone-unlock --pin-file ~/.phone-secrets --json
230
+ phone-unlock --attempts 3 --verbose
231
+ ```
232
+
233
+ | option | meaning |
234
+ | --- | --- |
235
+ | `--serial SERIAL` | adb serial (default: `$ANDROID_SERIAL`, else the only attached phone) |
236
+ | `--adb PATH` | adb executable (default: `adb`) |
237
+ | `--pin-file PATH` | secrets file with a `PHONE_PIN=` line (default: `$PHONE_SECRETS`, else `/etc/nixos/phone-socks-secrets`) |
238
+ | `--attempts N` | PIN entries to allow, default 2, capped at 5 |
239
+ | `--json` | one JSON object instead of human text |
240
+ | `--verbose` | log the states and decisions on stderr |
241
+ | `--version` | print the package version |
242
+
243
+ There is deliberately **no `--pin`**: the PIN comes from `$PHONE_PIN` or a
244
+ secrets file, never from the command line. The value is piped to the remote
245
+ shell's `read` on stdin and passed to `input text` there, so it is absent from
246
+ every adb argv, from `ps` on the host, from stdout, from stderr and from
247
+ `--verbose`. The tool never calls `sudo`; an unreadable secrets file is one
248
+ actionable message naming the file and the two ways to provide the PIN.
249
+
250
+ After a wrong entry the state is read again. If it reports a lockout, the run
251
+ stops immediately with exit 4 instead of spending the remaining attempts.
252
+ Repeating the command cannot build a wrong-PIN loop, because every run starts
253
+ with a fresh lockout read.
254
+
255
+ | exit | meaning |
256
+ | --- | --- |
257
+ | `0` | unlocked, or already unlocked |
258
+ | `1` | still locked after the allowed attempts |
259
+ | `2` | usage error |
260
+ | `3` | no or ambiguous device |
261
+ | `4` | lockout; the PIN was not typed again |
262
+ | `5` | no usable PIN source |
263
+
264
+ ### Field shapes to confirm on the real device
265
+
266
+ The fake-adb fixtures encode the AOSP shapes the parser targets, but these are
267
+ vendor dependent and need one recorded XPLORE 2 run:
268
+
269
+ - `lockout` — `mLockoutAttemptDeadline` / `mKeyguardLockoutDeadline` (elapsed
270
+ realtime milliseconds, `> 0` means counting down) or a boolean lockout field
271
+ in the SystemUI dump. A build that prints none reports `unknown`.
272
+ - `keyguard`/`bouncer` — `mIsKeyguardShowing:` and `mBouncerVisible=` in
273
+ `dumpsys activity service com.android.systemui`. A dump missing either line
274
+ is read once more, then the missing field reports `unknown`.
275
+ - `screen` — `Display State=ON|OFF` plus `mWakefulness=Awake|Asleep`. `DOZE`
276
+ and the suspend states count as off; anything else is `unknown`.
277
+ - `stay_on` — a bitmask (`1` ac, `2` usb, `4` wireless); values outside `0..7`
278
+ report `unknown`. The previous value is read from
279
+ `settings get global stay_on_while_plugged_in`, not parsed from `svc`; the
280
+ prepared run assumes the device writes that setting synchronously and
281
+ accepts `svc power stayon ac|wireless`.
282
+ - `boot` — `getprop sys.boot_completed` plus a non-empty `pm path android`.
283
+ A device that reports the old `1` before adbd goes down is a race the first
284
+ poll can see; the fake and the timeout make that visible rather than hiding
285
+ it.
286
+
287
+ ### Tests
288
+
289
+ `test/fake_adb/adb` is an executable fake driven by the files in
290
+ `test/fixtures/`, so the suite needs neither adb nor a phone:
291
+
292
+ ```bash
293
+ python3 -m unittest discover -s test -t .
294
+ ```
295
+
296
+ The fake records every invocation to `calls.log`; the tests assert the exact
297
+ JSON line for each fixture and that a `$PHONE_PIN` never appears in argv,
298
+ stdout or stderr. The wake and unlock cases drive the same fake through a
299
+ state change (panel off, bouncer raised, wrong PIN, lockout), and count the
300
+ recorded `input text` scripts so an extra or missing PIN entry fails. The
301
+ suite runs with a `PATH` that contains the fake adb but no real one.
302
+
303
+
304
+ ## `phone-prepare`
305
+
306
+ `phone-prepare` is **the one preflight a device suite calls** before it starts.
307
+ It does four things, in this order:
308
+
309
+ 1. select the serial — the same implementation as `phone-state` (`--serial` >
310
+ `$ANDROID_SERIAL` > one device > USB preferred over TCP; anything else is
311
+ exit 3 with the device list);
312
+ 2. wake the panel with `phone-wake`'s `KEYCODE_WAKEUP` verification;
313
+ 3. apply `svc power stayon usb` (or `--stay-on ac|wireless|none`);
314
+ 4. unlock with `phone-unlock`'s bounded, lockout-aware loop.
315
+
316
+ ```bash
317
+ PHONE_PIN=1234 phone-prepare --json
318
+ phone-prepare --json --stay-on usb
319
+ phone-prepare --stay-on none # leave the setting alone
320
+ phone-prepare --restore # put the recorded value back
321
+ ```
322
+
323
+ `--json` prints exactly one line with the subset of `phone-state` fields that
324
+ the prepare contract promises (and never the PIN):
325
+
326
+ ```json
327
+ {"serial":"R58M12345AB","transport":"usb","screen":"on","keyguard":"dismissed","stay_on":"usb","pin_source":"env"}
328
+ ```
329
+
330
+ | option | meaning |
331
+ | --- | --- |
332
+ | `--serial SERIAL` | adb serial (default: `$ANDROID_SERIAL`, else the only attached phone) |
333
+ | `--adb PATH` | adb executable (default: `adb`) |
334
+ | `--pin-file PATH` | secrets file with a `PHONE_PIN=` line (default: `$PHONE_SECRETS`, else `/etc/nixos/phone-socks-secrets`) |
335
+ | `--attempts N` | PIN entries to allow, default 2, capped at 5 |
336
+ | `--stay-on MODE` | `usb` (default), `ac`, `wireless`, or `none`; bare `--stay-on` means `usb` |
337
+ | `--restore` | write the stay-on value recorded by the last run back |
338
+ | `--json` | one JSON object instead of human text |
339
+ | `--verbose` | log the states and decisions on stderr (never the PIN) |
340
+ | `--version` | print the package version |
341
+
342
+ ### Stay-on and restore contract
343
+
344
+ `svc power stayon` overwrites `settings get global stay_on_while_plugged_in`,
345
+ so the old value is the useful one. Before changing it, `phone-prepare` reads
346
+ the raw value, prints it in human output, and records it in
347
+ `$PHONE_TOOLS_STATE` (default `$XDG_STATE_HOME/phone-tools/stay_on.json`, else
348
+ `~/.local/state/phone-tools/stay_on.json`):
349
+
350
+ ```
351
+ phone-prepare: R58M12345AB usb screen=on keyguard=dismissed stay_on=usb; \
352
+ previous stay_on_while_plugged_in=7 (restore with phone-prepare --restore)
353
+ ```
354
+
355
+ `phone-prepare --restore` writes that exact value back with `settings put`
356
+ (or `settings delete` when the recorded value was `null`) and removes the
357
+ record. Restoring with no record is a usage error (exit 2), not a guess; a
358
+ record that names another serial is refused the same way.
359
+
360
+ `--stay-on none` never calls `svc power stayon`. It still records the current
361
+ value so a following `--restore` is a successful no-op instead of an error.
362
+
363
+ Every non-zero exit leaves the setting untouched or restores it:
364
+
365
+ | exit | meaning |
366
+ | --- | --- |
367
+ | `0` | prepared: awake, stay-on applied (or left alone), keyguard dismissed |
368
+ | `1` | still locked or the panel would not wake |
369
+ | `2` | usage error, including `--restore` with no record |
370
+ | `3` | no or ambiguous device, or the stay-on setting could not be read |
371
+ | `4` | lockout; the PIN was not retried |
372
+ | `5` | no usable PIN source |
373
+
374
+ A suite that gets a non-zero code stops with that code instead of running and
375
+ failing three checks later.
376
+
377
+ ## `phone-reboot`
378
+
379
+ `phone-reboot` sends `adb reboot`, then waits until **both**
380
+ `getprop sys.boot_completed` is `1` and `pm path android` answers. A boot flag
381
+ with an unresponsive package manager is not ready. `--timeout` defaults to
382
+ 180 seconds; a timeout exits `1` and reports the last state it saw.
383
+
384
+ ```bash
385
+ phone-reboot --yes
386
+ phone-reboot --yes --unlock --stay-on usb
387
+ phone-reboot --yes --timeout 300 --json
388
+ ```
389
+
390
+ Without `--yes` and without a TTY, the command exits `2` before it runs any
391
+ adb command, so a suite that forgot the flag changes nothing. On a TTY it asks
392
+ once instead.
393
+
394
+ | option | meaning |
395
+ | --- | --- |
396
+ | `--serial SERIAL` | adb serial (default: `$ANDROID_SERIAL`, else the only attached phone) |
397
+ | `--adb PATH` | adb executable (default: `adb`) |
398
+ | `--timeout SECONDS` | boot and package-manager wait, default 180 |
399
+ | `--yes` | reboot without asking (required when stdin is not a TTY) |
400
+ | `--unlock` | run the shared unlock **once** after boot |
401
+ | `--pin-file PATH` | secrets file for `--unlock` |
402
+ | `--attempts N` | PIN entries for `--unlock`, default 2, capped at 5 |
403
+ | `--stay-on MODE` | apply after boot: `usb`, `ac`, `wireless`, or `none` (default `none`); recorded for `phone-prepare --restore` |
404
+ | `--json` | print the final state |
405
+ | `--verbose` | log the states and decisions on stderr (never the PIN) |
406
+ | `--version` | print the package version |
407
+
408
+ `--json` includes the subset of `phone-state` fields that the reboot contract
409
+ promises plus the two wait signals:
410
+
411
+ ```json
412
+ {"serial":"R58M12345AB","transport":"usb","boot_completed":true,"pm_ready":true,"screen":"on","keyguard":"dismissed","stay_on":"usb","pin_source":"env"}
413
+ ```
414
+
415
+ | exit | meaning |
416
+ | --- | --- |
417
+ | `0` | booted and ready (and unlocked/stay-on applied if requested) |
418
+ | `1` | timed out waiting for boot or the package manager |
419
+ | `2` | usage error, or no `--yes` on a non-TTY stdin |
420
+ | `3` | no or ambiguous device, or adb failed |
421
+ | `4` | lockout during `--unlock` |
422
+ | `5` | no usable PIN source for `--unlock`; nothing was rebooted |
423
+
424
+
425
+ ## Observed device facts
426
+
427
+ These are the measurements the implementation is built around, taken on the
428
+ reference XPLORE 2. They are not tunables; changing them changes the commands.
429
+
430
+ - **`POWER` is a toggle.** On an awake phone `KEYCODE_POWER` turns the panel
431
+ **off**, so pressing it again is not an idempotent wake. `KEYCODE_WAKEUP` is
432
+ the repeatable wake: it works from off and is harmless when already on.
433
+ `phone-wake` uses `KEYCODE_WAKEUP` only, and `KEYCODE_POWER` does not appear
434
+ in the package at all.
435
+ - **The bouncer is `FLAG_SECURE`.** A screenshot or `screencap` of the PIN
436
+ screen is black, so unlock cannot be verified from pixels. Verification is
437
+ system state: `mIsKeyguardShowing` / `mBouncerVisible` in the SystemUI dump,
438
+ with the display and power state. The tool never OCRs and never screenshots.
439
+ - **`dumpsys activity` is slower and keyguard-only.** The
440
+ `dumpsys activity service com.android.systemui` dump that carries the
441
+ keyguard and bouncer fields costs noticeably more than `dumpsys display` or
442
+ `dumpsys power`, and it says nothing about whether the panel is on. A state
443
+ snapshot reads it once; the wake retry loop on the phone uses only the cheap
444
+ display and power reads.
445
+ - **`svc power stayon` overwrites the setting.** It is not additive, so
446
+ `phone-prepare` records the previous value of
447
+ `stay_on_while_plugged_in` and `--restore` writes it back.
448
+ - **Lockout fields vary by build.** `mLockoutAttemptDeadline` (elapsed
449
+ realtime milliseconds, `> 0` means counting down), a boolean lockout field,
450
+ or neither. `unknown` is reported when the build exposes none; it is not
451
+ rounded down to `none`.
452
+
453
+ ## Out of scope
454
+
455
+ - Finding, pairing or keeping the adb connection. That is `adb-harness`; this
456
+ tool starts from a transport that is already attached and returns exit `3`
457
+ when it cannot pick exactly one.
458
+ - `phone-cmd` and the Termux command channel. That is `termux-adb-bridge`; no
459
+ Termux app is required here.
460
+ - Generating, storing, rotating or validating secrets. The PIN is read from
461
+ `$PHONE_PIN` or a secrets file and never written anywhere.
462
+ - Driving anything past the keyguard. There is no app UI automation here.
463
+
464
+ ## Why the toggler exists
465
+
466
+ DokeOS gates boot starts per package, and the list behind that gate has no
467
+ public API. A row only takes effect for the copy of the app that was installed
468
+ when the row was written, so after every install the row has to go **off and
469
+ back on** and the app has to be opened once. Doing that by hand is slow and
470
+ misses things; this service does it over accessibility instead.
471
+
472
+ ## Build and install
473
+
474
+ The build runs on the phone under Termux (`aapt`/`d8`/`javac`/`apksigner`); it
475
+ wants `android.jar` (in `./android.jar` or `$HOME/android-sdk/platforms/*/`) and
476
+ the debug keystore:
477
+
478
+ ```bash
479
+ cd ~/phone-tools/autotoggle && bash build.sh
480
+ adb install -r build/phone-link-autotoggle.apk
481
+ ```
482
+
483
+ **Installing resets the accessibility grant.** Re-enable it afterwards, or the
484
+ service binds nothing and every command silently does nothing:
485
+
486
+ ```bash
487
+ adb shell settings put secure enabled_accessibility_services \
488
+ org.phonelink.autotoggle/org.phonelink.autotoggle.ToggleService
489
+ adb shell settings put secure accessibility_enabled 1
490
+ adb shell dumpsys accessibility | grep -m1 'Bound services'
491
+ ```
492
+
493
+ The same grant is cleared on every reboot; `wireless-adb-keeper` puts it back
494
+ from its boot-time `ensure` step.
495
+
496
+ ## Command interface
497
+
498
+ Both commands go to the receiver `org.phonelink.autotoggle/.Command`.
499
+
500
+ **Toggle rows** (labels separated by `;`, matched against the list's own
501
+ labels):
502
+
503
+ ```bash
504
+ adb shell "am broadcast -n org.phonelink.autotoggle/.Command \
505
+ -a org.phonelink.autotoggle.TOGGLE --es labels 'Phone link proxy;Termux:Boot'"
506
+ ```
507
+
508
+ For each label the service drives the switch off and back on, then launches the
509
+ app once and returns to the list. A run ends with a summary in logcat.
510
+
511
+ **List rows** — useful to discover the exact labels and their state:
512
+
513
+ ```bash
514
+ adb shell "am broadcast -n org.phonelink.autotoggle/.Command \
515
+ -a org.phonelink.autotoggle.LIST"
516
+ ```
517
+
518
+ Results are logged under the tag `ATOGGLE`:
519
+
520
+ ```bash
521
+ adb logcat -s ATOGGLE
522
+ # ROW on Phone link proxy
523
+ # ROW off Termux:GUI
524
+ # rows=103
525
+ # Phone link proxy end=on
526
+ # summary=9/9 re-registered, 9 opened
527
+ # done in 11813 ms
528
+ ```
529
+
530
+ Failures are reported per label: `=not found`, `=no switch`,
531
+ `=tap ignored (stale entry?)`.
532
+
533
+ ## How it finds a row
534
+
535
+ The vendor draws each row as a label and a `Switch` with nothing tying them
536
+ together, so a text search misses rows that are plainly on screen. The service
537
+ pairs them by vertical overlap instead.
538
+
539
+ Every label found by a full scan is remembered with the page it was on
540
+ (`SharedPreferences` named `pages`), so a repeat run jumps straight there and
541
+ only falls back to scanning when the hint is stale. Waits poll the switch state
542
+ instead of sleeping, and a switch sitting at the screen edge is brought fully
543
+ into view with a half-page nudge.
544
+
545
+ ## Boot workflow
546
+
547
+ `phone-reboot` sends the reboot and waits for both `sys.boot_completed=1` and a
548
+ responsive package manager, so a caller never races the old adbd or a boot that
549
+ is only half up:
550
+
551
+ ```bash
552
+ phone-reboot --yes # wait up to 180 s
553
+ phone-reboot --yes --timeout 120
554
+ phone-reboot --yes --unlock # unlock once it is up
555
+ ```
556
+
557
+ Without `--yes` on a non-interactive stdin it refuses and changes nothing. The
558
+ wait is bounded: on timeout it exits non-zero and prints the last boot and
559
+ package-manager state instead of pretending the phone is ready.
560
+
561
+ `phone-unlock` is the only unlock: it wakes the panel, raises the PIN bouncer,
562
+ types the PIN once and verifies the keyguard from system state. It takes the
563
+ PIN from `$PHONE_PIN` or `--pin-file`, never from an argument:
564
+
565
+ ```bash
566
+ phone-unlock # PIN from $PHONE_PIN or $PHONE_SECRETS
567
+ phone-unlock --pin-file ~/.phone-secrets --json
568
+ ```
569
+
570
+ A typical pre-unlock check, remembering that the phone is the laptop's only
571
+ uplink and has to be unlocked again at the end of it:
572
+
573
+ ```bash
574
+ phone-reboot --yes
575
+ sleep 30
576
+ adb shell dumpsys user | grep -m1 'State:' # RUNNING_LOCKED
577
+ # ... inspect whatever is supposed to run while locked ...
578
+ phone-unlock
579
+ ```
580
+
581
+ ## Caveats
582
+
583
+ - Vendor specific: the list activity
584
+ (`com.blackview.systemmanager/com.freeme.freemanager.autoboot.AutoStartMainActivity`)
585
+ and the switch layout are Blackview/DokeOS details.
586
+ - Labels must match what the list prints — use `LIST` rather than guessing.
587
+ - Rows are per package: reinstalling an app means toggling its row again.