456884a4d0
You called this one: the watcher was not running any more, and the reason was mine. It ran under set -euo pipefail, and two helpers ended in a pipeline to head -1; when head exits first sed takes a SIGPIPE, pipefail turns that into a failed assignment and -e turns that into an exit. So the script answered the first dialog or two and then vanished without a word, which is exactly what a run that stops after its first install looks like. It now runs without -e, the helper pipelines end in || true, and it prints a heartbeat every thirty seconds so is it still running can be answered from the log. Measured after the fix: two consecutive uninstall/install cycles both succeeded, the script stayed alive through a 75 s watch, and in a full benchmark run it was still alive at the end with two dialogs answered. That run lasted 1m29s instead of the previous 35s and one test got as far as driving the app, so the installs do sometimes get through now. One was still refused, and the run still failed overall.
234 lines
10 KiB
Bash
Executable File
234 lines
10 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
#
|
|
# Answers MIUI's "Install via USB" dialog while installs are happening on the device.
|
|
#
|
|
# ./tools/benchmark-watch-install.sh # ten minutes, poll every 2 s
|
|
# ./tools/benchmark-watch-install.sh --interval 5 # poll less often when idle
|
|
# ./tools/benchmark-watch-install.sh --burst 30 # keep watching fast for 30 s after a dialog
|
|
# ./tools/benchmark-watch-install.sh --dry-run # report, never tap
|
|
#
|
|
# Run this for *every* install, not just the first: a benchmark run installs the test APK and then, per
|
|
# test, the app under test. Measured the hard way — the first version of this script spent three to four
|
|
# seconds reading one dialog, and the next dialog opened and closed inside that window.
|
|
#
|
|
# How it answers a dialog:
|
|
#
|
|
# 1. If the Install button's position for this dialog was measured before (and saved), tap it — but
|
|
# only after re-checking, immediately beforehand, that an installer window is still focused. That
|
|
# check is what stops a tap from landing on the home screen once the dialog closes.
|
|
# 2. Otherwise read the screen (`uiautomator dump`), find the button by label — refusing anything that
|
|
# means "no" — verify the installer is still focused, then tap its own bounds and save the position
|
|
# for the dialogs that cannot be read in time.
|
|
# 3. If no Install button can be read and no position is known, tap nothing and say so.
|
|
#
|
|
# After every dialog the script watches in "burst" mode (every 0.4 s instead of the idle interval),
|
|
# because installs arrive in pairs and the second one used to slip through while the first was read.
|
|
#
|
|
# Measured on the Xiaomi phone (Android 12, MIUI 14): an adb-started install raises the dialog after
|
|
# about 1.7 s with a ten second deny countdown, so there is time for one dump and one tap; the same
|
|
# dialog is FLAG_SECURE, so a screencap of it shows whatever is behind it.
|
|
# No `-e`: this is a watcher that has to survive for the whole run, and a single failed adb call must not
|
|
# end it. It ended silently once before, in the middle of a benchmark run, which is why it looked like it
|
|
# had "stopped running" — a helper ending in `| head -1` takes a SIGPIPE when `head` exits first, and
|
|
# `pipefail` plus `-e` turned that into an exit.
|
|
set -uo pipefail
|
|
|
|
seconds=600
|
|
interval=2
|
|
burst=30
|
|
wait=10
|
|
dry_run=0
|
|
|
|
while [ $# -gt 0 ]; do
|
|
case "$1" in
|
|
--seconds) seconds=$2; shift 2 ;;
|
|
--interval) interval=$2; shift 2 ;;
|
|
--burst) burst=$2; shift 2 ;;
|
|
--wait) wait=$2; shift 2 ;;
|
|
--dry-run) dry_run=1; shift ;;
|
|
-h|--help) sed -n '2,26p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
|
|
*) echo "usage: $0 [--seconds N] [--interval N] [--burst N] [--wait N] [--dry-run]" >&2; exit 2 ;;
|
|
esac
|
|
done
|
|
|
|
command -v adb >/dev/null || { echo "benchmark-watch-install: adb is not on PATH" >&2; exit 1; }
|
|
command -v python3 >/dev/null || { echo "benchmark-watch-install: python3 is needed to read the UI dump" >&2; exit 1; }
|
|
|
|
device_dump=/sdcard/.benchmark-watch.xml
|
|
local_dump=$(mktemp)
|
|
trap 'rm -f "$local_dump"' EXIT
|
|
|
|
layout_file="${XDG_CACHE_HOME:-$HOME/.cache}/plain-files-benchmark-install-layout"
|
|
layout_activity=""
|
|
layout_x=""
|
|
layout_y=""
|
|
layout_label=""
|
|
if [ -f "$layout_file" ]; then
|
|
read -r layout_activity layout_x layout_y layout_label <"$layout_file" || true
|
|
fi
|
|
|
|
answered_token=""
|
|
tapped_token=""
|
|
opened_windows=0
|
|
taps=0
|
|
misses=0
|
|
|
|
focused_window() {
|
|
adb shell dumpsys window 2>/dev/null | grep -m1 'mCurrentFocus' || true
|
|
}
|
|
|
|
# Only the installer's own windows count; anything else is left alone.
|
|
is_installer() {
|
|
case "$1" in
|
|
*securitycenter*install*|*InstallApp*|*packageinstaller*) return 0 ;;
|
|
*) return 1 ;;
|
|
esac
|
|
}
|
|
|
|
# The window token, so the same dialog is not answered twice while it closes.
|
|
window_token() {
|
|
sed -n 's/.*mCurrentFocus=Window{\([0-9a-f]*\).*/\1/p' <<<"$1" | head -1 || true
|
|
}
|
|
|
|
# The activity that owns the focused window. Dialogs each get their own window token, so the token
|
|
# cannot be used to tell "the dialog I read is still up" from "another installer dialog replaced it" —
|
|
# which is exactly what happens when a run installs twice in quick succession. The activity name is
|
|
# stable across both, and it is still specific enough that a tap can only ever land on an installer.
|
|
installer_activity() {
|
|
sed -n 's/.*u0 \([^ }]*\).*/\1/p' <<<"$1" | head -1 || true
|
|
}
|
|
|
|
# Prints "<x> <y> <label>" for the dialog's Install button, or nothing at all.
|
|
install_button() {
|
|
adb shell uiautomator dump "$device_dump" >/dev/null 2>&1 || return 0
|
|
adb pull "$device_dump" "$local_dump" >/dev/null 2>&1 || return 0
|
|
python3 - "$local_dump" <<'PY' || true
|
|
import sys
|
|
import xml.etree.ElementTree as ET
|
|
|
|
DENY = ('verweigern', 'deny', 'ablehnen', 'abbrechen', 'cancel', 'nein', 'no')
|
|
ALLOW = ('installieren', 'install', 'erlauben', 'zulassen', 'allow')
|
|
|
|
def centre(node):
|
|
x1, y1, x2, y2 = (int(v) for v in node.get('bounds').strip('[]').replace('][', ',').split(','))
|
|
return (x1 + x2) // 2, (y1 + y2) // 2
|
|
|
|
for node in ET.parse(sys.argv[1]).getroot().iter('node'):
|
|
if 'Button' not in node.get('class', ''):
|
|
continue
|
|
text = (node.get('text') or '').strip()
|
|
low = text.lower()
|
|
if not text or any(low.startswith(d) for d in DENY):
|
|
continue
|
|
if any(low.startswith(a) for a in ALLOW) or node.get('resource-id', '').endswith('button2'):
|
|
x, y = centre(node)
|
|
print(x, y, text.replace(' ', '_'))
|
|
break
|
|
PY
|
|
}
|
|
|
|
# Reads the button, and taps it only if the dialog is *still* the focused window at that moment. Called
|
|
# only while an installer window is up.
|
|
#
|
|
# The re-check is the whole point of this function. A dump takes three to four seconds, which is longer
|
|
# than the dialog can live, so the position read from a dying dialog used to be tapped after it had
|
|
# closed — and the tap landed on whatever the home screen had at those coordinates (a Telegram icon, in
|
|
# the run that made this obvious). Nothing here is remembered between dialogs either, for the same
|
|
# reason: a remembered position is a blind tap waiting to happen.
|
|
answer_dialog() {
|
|
local focus=$1 token activity label now_focus
|
|
token=$(window_token "$focus")
|
|
activity=$(installer_activity "$focus")
|
|
if [ "$token" != "$answered_token" ]; then
|
|
answered_token=$token
|
|
opened_windows=$((opened_windows + 1))
|
|
fi
|
|
|
|
# One tap per dialog. A second would be a tap on whatever the dialog left behind.
|
|
if [ "$token" = "$tapped_token" ]; then
|
|
return 0
|
|
fi
|
|
|
|
# Fast path: the position of this dialog's Install button was measured earlier and saved, and reading
|
|
# the screen again takes longer than the dialog lives. The saved position is used only while an
|
|
# installer window is focused — checked again immediately before the tap, 0.1 s earlier — so it can
|
|
# never land on whatever is behind a dialog that has closed.
|
|
if [ -n "$layout_x" ] && [ "$layout_activity" = "$activity" ]; then
|
|
now_focus=$(focused_window)
|
|
if is_installer "$now_focus" && [ "$(installer_activity "$now_focus")" = "$activity" ]; then
|
|
tapped_token=$(window_token "$now_focus")
|
|
if [ "$dry_run" -eq 1 ]; then
|
|
echo " $(date +%H:%M:%S) would tap '$layout_label' at $layout_x,$layout_y (measured position, installer focused)"
|
|
else
|
|
adb shell input tap "$layout_x" "$layout_y" || true
|
|
taps=$((taps + 1))
|
|
echo " $(date +%H:%M:%S) tapped '$layout_label' at $layout_x,$layout_y (measured position, installer focused)"
|
|
fi
|
|
return 0
|
|
fi
|
|
fi
|
|
|
|
local attempt
|
|
for attempt in $(seq 1 "$wait"); do
|
|
read -r button_x button_y label <<<"$(install_button)"
|
|
if [ -n "${button_x:-}" ]; then
|
|
now_focus=$(focused_window)
|
|
if ! is_installer "$now_focus" || [ "$(installer_activity "$now_focus")" != "$activity" ]; then
|
|
echo " $(date +%H:%M:%S) read '$label' at $button_x,$button_y, but the dialog closed before the tap; not tapping"
|
|
return 0
|
|
fi
|
|
# Whatever installer dialog is up *now* is the one being answered; it may be a newer window
|
|
# than the one this function was called for, which is exactly the case that used to be
|
|
# skipped.
|
|
tapped_token=$(window_token "$now_focus")
|
|
# Remember where this button was, for the dialogs that cannot be read in time.
|
|
layout_activity=$activity
|
|
layout_x=$button_x
|
|
layout_y=$button_y
|
|
layout_label=$label
|
|
printf '%s %s %s %s\n' "$activity" "$button_x" "$button_y" "$label" >"$layout_file" 2>/dev/null || true
|
|
if [ "$dry_run" -eq 1 ]; then
|
|
echo " $(date +%H:%M:%S) would tap '$label' at $button_x,$button_y"
|
|
else
|
|
adb shell input tap "$button_x" "$button_y" || true
|
|
taps=$((taps + 1))
|
|
echo " $(date +%H:%M:%S) tapped '$label' at $button_x,$button_y (read from the screen, installer still up)"
|
|
fi
|
|
return 0
|
|
fi
|
|
sleep 0.5
|
|
done
|
|
|
|
misses=$((misses + 1))
|
|
echo " $(date +%H:%M:%S) installer window focused, but no Install button appeared within ${wait}s"
|
|
}
|
|
|
|
deadline=$((SECONDS + seconds))
|
|
burst_until=0
|
|
last_beat=$SECONDS
|
|
echo "benchmark-watch-install: watching for $seconds s (idle poll ${interval}s, burst after a dialog)"
|
|
|
|
while [ $SECONDS -lt $deadline ]; do
|
|
# A heartbeat, so "is it still running?" can be answered by reading the log instead of guessing.
|
|
if [ $((SECONDS - last_beat)) -ge 30 ]; then
|
|
echo " $(date +%H:%M:%S) still watching: $taps answered, $opened_windows window(s) seen"
|
|
last_beat=$SECONDS
|
|
fi
|
|
|
|
focus=$(focused_window)
|
|
if is_installer "$focus"; then
|
|
answer_dialog "$focus"
|
|
burst_until=$((SECONDS + burst))
|
|
sleep 0.7
|
|
continue
|
|
fi
|
|
|
|
if [ $SECONDS -lt $burst_until ]; then
|
|
sleep 0.4
|
|
else
|
|
sleep "$interval"
|
|
fi
|
|
done
|
|
|
|
echo "benchmark-watch-install: done, $taps tap(s), $opened_windows installer window(s) seen, $misses not answerable"
|