#!/usr/bin/env bash # One-shot Crkl testing environment — must leave you with a working blue C overlay. set -euo pipefail ROOT="$(cd "$(dirname "$0")/.." && pwd)" cd "$ROOT" UNAME_S="$(uname -s)" if [[ "$UNAME_S" == "Darwin" ]]; then BREW_PREFIX="$(brew --prefix 2>/dev/null || echo /opt/homebrew)" export JAVA_HOME="${JAVA_HOME:-$BREW_PREFIX/opt/openjdk@17}" export ANDROID_HOME="${ANDROID_HOME:-$BREW_PREFIX/share/android-commandlinetools}" else export ANDROID_HOME="${ANDROID_HOME:-$HOME/android-sdk}" fi export ANDROID_SDK_ROOT="$ANDROID_HOME" export PATH="$JAVA_HOME/bin:$ANDROID_HOME/platform-tools:$ANDROID_HOME/emulator:$PATH" export ANDROID_EMULATOR_WAIT_TIME_BEFORE_KILL="${ANDROID_EMULATOR_WAIT_TIME_BEFORE_KILL:-5}" ADB="$ANDROID_HOME/platform-tools/adb" EMU="$ANDROID_HOME/emulator/emulator" AVD_NAME="${AVD_NAME:-CrklEmulator}" GPU="${EMULATOR_GPU:-host}" COMP="com.example.crkl/com.example.crkl.accessibility.CrklAccessibilityService" LOG="${CRKL_EMU_LOG:-/tmp/crkl-emulator.log}" echo "== Crkl test-env ==" if [[ ! -x "$ADB" || ! -x "$EMU" ]]; then echo "Missing Android SDK tools. Run: make setup-mac" exit 1 fi ensure_emulator() { if "$ADB" devices 2>/dev/null | awk '/emulator-.*device/{exit 0} END{exit 1}'; then echo "Emulator already up" return 0 fi echo "Starting emulator (open gRPC)..." "$ADB" emu kill >/dev/null 2>&1 || true pkill -f 'qemu-system' >/dev/null 2>&1 || true sleep 2 rm -f "$HOME/.android/avd/${AVD_NAME}.avd"/*.lock 2>/dev/null || true if [[ "$UNAME_S" == "Darwin" ]]; then chmod +x "$ROOT/scripts/start-emulator-launchd.sh" bash "$ROOT/scripts/start-emulator-launchd.sh" else nohup "$EMU" -avd "$AVD_NAME" -memory 3072 -cores 4 -no-audio \ -gpu "$GPU" -accel on -no-snapshot-load -no-metrics \ -grpc 8554 \ >"$LOG" 2>&1 & echo $! > /tmp/crkl-emulator.pid fi bash "$ROOT/scripts/wait-emulator.sh" "$ADB" } enable_on_screen_nav() { # Prefer nav bar ON THE PHONE (rim chrome is often dead on macOS QT). "$ADB" root >/dev/null 2>&1 || true sleep 1 "$ADB" wait-for-device "$ADB" shell settings put secure navigation_mode 0 || true "$ADB" shell cmd overlay enable com.android.internal.systemui.navbar.threebutton >/dev/null 2>&1 || true "$ADB" shell settings put global force_fsg_nav_bar 0 >/dev/null 2>&1 || true } enable_crkl() { "$ADB" shell pm grant com.example.crkl android.permission.RECORD_AUDIO || true # Disable then enable — avoid empty-string settings (causes "Bad arguments" on some images). "$ADB" shell settings put secure accessibility_enabled 0 || true sleep 1 "$ADB" shell settings put secure enabled_accessibility_services "$COMP" "$ADB" shell settings put secure accessibility_enabled 1 sleep 2 } verify_overlay() { local ok=0 for _ in $(seq 1 15); do if "$ADB" logcat -d -s CrklAccessibilityService:D 2>/dev/null | tail -20 | grep -q "Floating button created"; then ok=1 break fi sleep 1 done if [[ "$ok" -ne 1 ]]; then echo "WARNING: did not see 'Floating button created' in logcat yet — check Accessibility in Settings." else echo "✓ Crkl overlay service connected (blue C should be visible)" fi echo "a11y=$("$ADB" shell settings get secure enabled_accessibility_services | tr -d '\r')" } ensure_emulator echo "== build + install ==" ./gradlew assembleDebug --console=plain -q "$ADB" install -r app/build/outputs/apk/debug/app-debug.apk echo "== on-screen nav + Crkl permissions ==" enable_on_screen_nav enable_crkl echo "== open fixtures ==" "$ADB" shell am force-stop com.example.crkl >/dev/null 2>&1 || true "$ADB" shell am start -n com.example.crkl/.fixtures.TestFixturesActivity >/dev/null sleep 1 # Re-assert a11y after force-stop (some builds drop it) enable_crkl verify_overlay "$ADB" shell am start -n com.example.crkl/.fixtures.TestFixturesActivity >/dev/null echo "" echo "✓ Ready. Look at the emulator phone screen:" echo " • Blue floating C (bottom-right) = Crkl" echo " • Bottom on-screen ◀ ○ □ = Android nav (use these; rim chrome often dead)" echo "" echo "Try now:" echo " 1. Tap blue C" echo " 2. Circle the email card" echo " 3. Tap red EXIT" echo " 4. For OCR: circle the OCR-HELLO-42 block" echo " 5. For STT: after a result, tap Speak" echo ""