#!/bin/sh # CILLTV Home setup for Mac/Linux. Run on your computer with Android Platform Tools. # https://updates.tinybadger.party/ — no root, app data deletion or automatic reboot. set -eu umask 077 fail() { printf 'CILLTV: %s\n' "$*" >&2; exit 1; } usage() { cat <<'EOF' Make CILLTV the Android TV Home app (opens CILLTV once per boot). sh cilltv-home.sh Set up the only connected TV sh cilltv-home.sh --serial SERIAL Choose a TV from adb devices sh cilltv-home.sh --restore Restore the saved previous Home app sh cilltv-home.sh --user ID Choose an Android user (default: current) Install CILLTV first. Install Google's Android Platform Tools on this computer, enable USB/network debugging on the TV, and approve this computer on the TV. For network debugging, use adb pair (if required), then adb connect ADDRESS:PORT. ADB must be on PATH, or set ADB to the full path to your adb executable. Undo records stay on this computer in ~/.local/state/cilltv-home (or XDG_STATE_HOME). This changes only the default Home app. Google apps and TV settings are retained. Some manufacturers block replacement launchers; failure is reported and the previous Home app is restored where possible. Reboot manually to test startup. EOF } serial=${ANDROID_SERIAL:-} user_id= mode=setup while [ "$#" -gt 0 ]; do case "$1" in --serial|--user) [ "$#" -ge 2 ] || fail "$1 needs a value." case "$1" in --serial) serial=$2 ;; --user) user_id=$2 ;; esac shift 2 ;; --restore) mode=restore; shift ;; --help|-h) usage; exit 0 ;; *) fail "Unknown option: $1. Use --help." ;; esac done ADB=${ADB:-adb} command -v "$ADB" >/dev/null 2>&1 || fail "Install Android Platform Tools and put adb on PATH (or set ADB)." if [ -z "$serial" ]; then devices=$("$ADB" devices | tr -d '\r' | awk 'NR > 1 && NF >= 2 {print $1}') count=$(printf '%s\n' "$devices" | awk 'NF {n++} END {print n+0}') [ "$count" -eq 1 ] || fail "Connect exactly one TV, or use --serial from adb devices." serial=$devices fi adb_tv() { "$ADB" -s "$serial" "$@"; } [ "$(adb_tv get-state 2>/dev/null | tr -d '\r')" = device ] || fail "TV is offline or unauthorised. Approve debugging on the TV, then retry." if [ -z "$user_id" ]; then user_id=$(adb_tv shell am get-current-user | tr -d '\r'); fi case "$user_id" in ''|*[!0-9]*) fail "Cannot identify Android user; supply --user ID." ;; esac target=com.cillian.cilltv/com.cillian.cilltv.HomeActivity valid_component() { printf '%s\n' "$1" | LC_ALL=C grep -Eq '^[A-Za-z0-9_][A-Za-z0-9_.]*/[A-Za-z0-9_.]+$'; } normalize() { awk -F/ 'NF == 2 {if (substr($2,1,1)==".") print $1 "/" $1 $2; else print $0}'; } resolve_home() { adb_tv shell cmd package resolve-activity --brief --user "$user_id" -a android.intent.action.MAIN -c android.intent.category.HOME | tr -d '\r' | tail -n 1 | normalize } available() { adb_tv shell cmd package query-activities --brief --user "$user_id" -a android.intent.action.MAIN -c android.intent.category.HOME | tr -d '\r' | normalize | grep -Fxq "$1" } set_home() { adb_tv shell cmd package set-home-activity --user "$user_id" "$1"; } device_id=$(adb_tv shell getprop ro.serialno | tr -d '\r') case "$device_id" in ''|unknown) device_id=$serial ;; esac state_root=${XDG_STATE_HOME:-"$HOME/.local/state"}/cilltv-home state_key=$(printf '%s\n%s\n' "$device_id" "$user_id" | cksum | awk '{print $1 "-" $2}') state_file=$state_root/$state_key.home current=$(resolve_home) if [ "$mode" = restore ]; then [ -f "$state_file" ] || fail "No saved Home app for this TV/user on this computer. Choose Home in the TV's Settings instead." previous=$(cat "$state_file") valid_component "$previous" || fail "Invalid undo record." available "$previous" || fail "Saved Home app is unavailable. Enable/install it in TV Settings first." set_home "$previous" || fail "Android refused to restore Home." [ "$(resolve_home)" = "$previous" ] || fail "Android did not restore the saved launcher. Choose Home in TV Settings." rm "$state_file" printf 'Restored Home: %s. Press Home on the TV remote.\n' "$previous" exit 0 fi available "$target" || fail "Install/enable CILLTV for this Android user first. Its HomeActivity must be available." if [ "$current" != "$target" ]; then valid_component "$current" && available "$current" || fail "Cannot identify a usable previous Home app. Choose a Home app in TV Settings first, then retry." mkdir -p "$state_root" # A repeat run must retain the original undo record. Never execute its contents. if [ ! -f "$state_file" ]; then (set -C; printf '%s\n' "$current" > "$state_file") || fail "Could not save previous Home app." fi if ! set_home "$target" || [ "$(resolve_home)" != "$target" ]; then if set_home "$current" && [ "$(resolve_home)" = "$current" ]; then fail "TV refused CILLTV as Home; previous launcher restored. Choose Home in TV Settings or check manufacturer restrictions." fi fail "TV refused CILLTV and rollback could not be verified. Choose the original Home app in TV Settings. Undo record: $state_file" fi fi printf 'CILLTV is the default Home app for Android user %s.\n' "$user_id" printf 'Press Home now. After a reboot CILLTV Home opens CILLTV automatically.\n' printf 'Undo on this computer: sh cilltv-home.sh --restore --serial "%s" --user %s\n' "$serial" "$user_id" if [ ! -f "$state_file" ]; then printf 'CILLTV was already Home; no earlier launcher was available to save.\n'; fi