#!/usr/bin/env bash set -euo pipefail ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" DEFAULT_SERIAL="adb-A9TU024628000647-B0UNif._adb-tls-connect._tcp" DEFAULT_SDK_DIR="/opt/homebrew/share/android-commandlinetools" PACKAGE_NAME="site.hyzq.bossandroid" ACTIVITY_NAME=".MainActivity" APK_PATH="$ROOT_DIR/android-app/app/build/outputs/apk/debug/app-debug.apk" TARGET_SERIAL="${BOSS_ANDROID_SERIAL:-$DEFAULT_SERIAL}" usage() { cat < Commands: serial Print the locked Honor device serial status Show adb status for the Honor device install Install the current debug APK on the Honor device launch Launch the Boss app on the Honor device build-install Build the debug APK and install it on the Honor device screenshot Save a screenshot to /tmp/boss-honor-screen.png ui-dump Dump the current UI hierarchy to /tmp/boss-honor-ui.xml logcat Tail logcat for the Boss app only Environment: BOSS_ANDROID_SERIAL Override the locked device serial if needed ANDROID_HOME Android SDK path for build-install ANDROID_SDK_ROOT Android SDK path for build-install EOF } ensure_connected() { local state state="$(adb -s "$TARGET_SERIAL" get-state 2>/dev/null || true)" if [[ "$state" != "device" ]]; then echo "Honor debug device is not connected: $TARGET_SERIAL" >&2 exit 1 fi } ensure_apk() { if [[ ! -f "$APK_PATH" ]]; then echo "APK not found: $APK_PATH" >&2 echo "Run './scripts/android_honor_debug.sh build-install' or build the app first." >&2 exit 1 fi } build_debug_apk() { ( cd "$ROOT_DIR/android-app" if [[ -z "${ANDROID_HOME:-}" && -d "$DEFAULT_SDK_DIR" ]]; then export ANDROID_HOME="$DEFAULT_SDK_DIR" fi if [[ -z "${ANDROID_SDK_ROOT:-}" && -d "${ANDROID_HOME:-}" ]]; then export ANDROID_SDK_ROOT="$ANDROID_HOME" fi ./gradlew assembleDebug ) } command="${1:-}" case "$command" in serial) echo "$TARGET_SERIAL" ;; status) ensure_connected echo "Serial: $TARGET_SERIAL" echo "State: $(adb -s "$TARGET_SERIAL" get-state)" adb -s "$TARGET_SERIAL" shell getprop ro.product.brand adb -s "$TARGET_SERIAL" shell getprop ro.product.model ;; install) ensure_connected ensure_apk adb -s "$TARGET_SERIAL" install -r "$APK_PATH" ;; launch) ensure_connected adb -s "$TARGET_SERIAL" shell am start -n "$PACKAGE_NAME/$ACTIVITY_NAME" ;; build-install) ensure_connected build_debug_apk adb -s "$TARGET_SERIAL" install -r "$APK_PATH" adb -s "$TARGET_SERIAL" shell am start -n "$PACKAGE_NAME/$ACTIVITY_NAME" ;; screenshot) ensure_connected adb -s "$TARGET_SERIAL" shell screencap -p /sdcard/boss-honor-screen.png >/dev/null adb -s "$TARGET_SERIAL" pull /sdcard/boss-honor-screen.png /tmp/boss-honor-screen.png >/dev/null adb -s "$TARGET_SERIAL" shell rm -f /sdcard/boss-honor-screen.png >/dev/null echo "/tmp/boss-honor-screen.png" ;; ui-dump) ensure_connected adb -s "$TARGET_SERIAL" shell uiautomator dump /sdcard/boss-honor-ui.xml >/dev/null adb -s "$TARGET_SERIAL" pull /sdcard/boss-honor-ui.xml /tmp/boss-honor-ui.xml >/dev/null adb -s "$TARGET_SERIAL" shell rm -f /sdcard/boss-honor-ui.xml >/dev/null echo "/tmp/boss-honor-ui.xml" ;; logcat) ensure_connected adb -s "$TARGET_SERIAL" logcat --pid="$(adb -s "$TARGET_SERIAL" shell pidof "$PACKAGE_NAME")" ;; ""|-h|--help|help) usage ;; *) echo "Unknown command: $command" >&2 usage >&2 exit 1 ;; esac