From 08b48f53fc899351bd3e22f9471c647e616e3af6 Mon Sep 17 00:00:00 2001 From: Hadrian Burkhardt Date: Sat, 17 Oct 2026 13:56:39 +0200 Subject: [PATCH] Watch for MIUI's install dialog and answer it only when its button is on screen macrobenchmark installs its target before measuring it, and on this phone MIUI puts that install behind com.miui.securitycenter/.permcenter.install.AdbInstallActivity. The test cannot answer it: CompilationMode.installPackageFromPaths runs `pm install -t ...` through Shell.executeScriptCaptureStdout, so the call blocks until the child exits and no test code is running while the dialog is up. This script runs beside the Gradle run and taps the Install button instead. It never taps blindly. Every five seconds it asks who has focus; if that is the installer, it reads the screen and taps the bounds of a button whose label is an install word - or, failing that, MIUI's own button2 id - and if no such button appears within ten seconds it taps nothing and says so. The first version guessed at the dialog's layout when the screen could not be read, which is what made it click whatever happened to be at the guessed coordinates; that code is gone. Reading the screen costs three to four seconds per dialog, which is the price of never pressing the wrong thing. Measured limits, in the script's own comment: for an install started from adb the dialog appears after 1.7 s and counts down from about ten, so a five second poll leaves time to read and tap; for the install macrobenchmark starts inside the test, the window is focused for under a second and is FLAG_SECURE (a screencap of the focused dialog shows the home screen behind it), so that dialog cannot be answered by this script or by a human. The durable fix is the MIUI developer option the dialog is named after, Install via USB. --- tools/benchmark-watch-install.sh | 125 +++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100755 tools/benchmark-watch-install.sh diff --git a/tools/benchmark-watch-install.sh b/tools/benchmark-watch-install.sh new file mode 100755 index 0000000..6f83470 --- /dev/null +++ b/tools/benchmark-watch-install.sh @@ -0,0 +1,125 @@ +#!/usr/bin/env bash +# +# Answers MIUI's "Install via USB" dialog while a benchmark run is in flight. +# +# ./tools/benchmark-watch-install.sh # watch for ten minutes, poll every 5 s +# ./tools/benchmark-watch-install.sh --interval 2 # poll more often +# ./tools/benchmark-watch-install.sh --dry-run # report, never tap +# +# Why this exists: macrobenchmark installs its target before measuring it, and on this phone MIUI puts +# that install behind `com.miui.securitycenter/.permcenter.install.AdbInstallActivity`. The test cannot +# answer it itself — `CompilationMode.installPackageFromPaths` builds `pm install -t …` and runs it +# through `Shell.executeScriptCaptureStdout`, so the call blocks until the child exits and no test code +# is running while the dialog is up. This script runs beside the Gradle run instead. +# +# **It never taps blindly.** A dialog is only answered after its Install button has been read off the +# screen and its own bounds are known; if the button is not there, nothing is tapped. That costs a +# `uiautomator dump` per dialog (3-4 s on this device) but it cannot press whatever happens to sit at a +# guessed coordinate. +# +# Measured on the Xiaomi phone (Android 12, MIUI 14), and both numbers matter: +# +# * for an install started from adb, the dialog appears after about 1.7 s and its deny button counts +# down from about ten seconds — so a 5 s poll still leaves time to read the screen and tap; +# * for the install macrobenchmark starts from inside the test, the window is focused for well under +# a second (`13:51:27.70 AdbInstallActivity` → `13:51:28.38 null`), which is shorter than one +# `uiautomator dump`. Those dialogs will not be caught, by this script or by a human. +# +# So this is honest about its limit: it makes adb-driven installs unattended, and it will not rescue a +# reinstall that MIUI refuses instantly. `--interval` and `--wait` are there for tuning per device. +set -euo pipefail + +seconds=600 +interval=5 +wait=10 +dry_run=0 + +while [ $# -gt 0 ]; do + case "$1" in + --seconds) seconds=$2; shift 2 ;; + --interval) interval=$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] [--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 + +# Prints "