Files
faceclaw/scripts/resetOnboarding.sh
T

122 lines
4.2 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# resetOnboarding.sh — reset Faceclaw on an attached phone back to its
# pre-onboarding state, for testing the onboarding flow repeatedly.
#
# By default this only clears the onboarding-related preference keys
# (onboarding.complete, onboarding.previewOnly, the on-glasses onboarding's
# onboarding.glassesFinishedVersion, ...) and leaves everything
# else — notably the saved glasses/ring MAC addresses — intact, so you don't
# have to re-scan devices on every test pass. Pass --all to wipe ALL app data
# (a true fresh-install state, addresses included).
#
# Usage:
# scripts/resetOnboarding.sh # reset onboarding flags only
# scripts/resetOnboarding.sh --all # full `pm clear` (wipes everything)
# scripts/resetOnboarding.sh -s <device-id> # target a specific adb device
#
# The device id defaults to DEVICE_ID from build_paths.sh, the same device
# build_and_run.sh targets. adb must be on PATH, or added to it in build_paths.sh.
# Sourced ahead of strict mode, since the build scripts source it without one.
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
if [ -f "$ROOT/build_paths.sh" ]; then
source "$ROOT/build_paths.sh"
fi
set -euo pipefail
PACKAGE="com.faceclaw.app"
PREF_KEY_PREFIX="onboarding."
WIPE_ALL=0
while [ $# -gt 0 ]; do
case "$1" in
--all) WIPE_ALL=1; shift ;;
-s|--device) DEVICE_ID="$2"; shift 2 ;;
-h|--help)
sed -n '2,20p' "$0" | sed 's/^# \{0,1\}//'
exit 0 ;;
*)
echo "unknown option: $1 (try --help)" >&2
exit 2 ;;
esac
done
command -v adb >/dev/null 2>&1 || { echo "error: adb not found on PATH" >&2; exit 1; }
if [ -z "${DEVICE_ID:-}" ]; then
echo "error: no adb device; set DEVICE_ID in build_paths.sh or pass -s <device-id>." >&2
exit 2
fi
ADB="adb -s $DEVICE_ID"
# Network devices (host:port) need an explicit connect first. A failure here is
# reported by the connected-device check below.
case "$DEVICE_ID" in
*:*) adb connect "$DEVICE_ID" >/dev/null 2>&1 || true ;;
esac
# Confirm the device is actually there before doing anything.
if ! $ADB get-state >/dev/null 2>&1; then
echo "error: adb device '$DEVICE_ID' not connected." >&2
echo " Connected devices:" >&2
adb devices -l | sed '1d' >&2
exit 1
fi
if [ "$WIPE_ALL" -eq 1 ]; then
echo "Wiping ALL app data for $PACKAGE (fresh-install state)..."
$ADB shell pm clear "$PACKAGE"
echo "Done. The next launch starts from a clean onboarding."
exit 0
fi
# Surgical reset: strip only the onboarding.* keys from the app's
# SharedPreferences XML, preserving saved device addresses and other settings.
# This needs run-as, which works for debuggable (sideloaded/dev) builds.
if ! $ADB shell run-as "$PACKAGE" true >/dev/null 2>&1; then
echo "run-as is unavailable for $PACKAGE (release build?)." >&2
echo "Falling back to a full data wipe; re-run with --all to silence this." >&2
$ADB shell pm clear "$PACKAGE"
echo "Done (full wipe)."
exit 0
fi
echo "Clearing onboarding preference keys ($PREF_KEY_PREFIX*) for $PACKAGE..."
# Force-stop first so the running app can't flush its in-memory preferences
# back over the files after we edit them.
$ADB shell am force-stop "$PACKAGE"
# List the shared_prefs XML files, then strip matching <... name="onboarding...">
# entries from each. toybox sed (present on modern Android) supports -i.
# Note: each remote command is passed to `adb shell` as ONE quoted string --
# adb joins multiple args with spaces without re-quoting, so inner quoting
# (e.g. sh -c '...') gets flattened and re-tokenized by the device shell.
FILES="$($ADB shell "run-as $PACKAGE ls shared_prefs 2>/dev/null" | tr -d '\r' || true)"
if [ -z "$FILES" ]; then
echo "No shared_prefs found; the app may not have run yet. Nothing to do."
exit 0
fi
CHANGED=0
for f in $FILES; do
case "$f" in
*.xml) ;;
*) continue ;;
esac
if $ADB shell "run-as $PACKAGE grep -q 'name=\"$PREF_KEY_PREFIX' shared_prefs/$f" >/dev/null 2>&1; then
$ADB shell "run-as $PACKAGE sed -i '/name=\"$PREF_KEY_PREFIX/d' shared_prefs/$f"
echo " cleared onboarding keys in shared_prefs/$f"
CHANGED=1
fi
done
if [ "$CHANGED" -eq 1 ]; then
echo "Done. Relaunch Faceclaw to go through onboarding again."
else
echo "No onboarding keys were set. App is already in pre-onboarding state."
fi