Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / launcher / install-tv.sh
install-tv.sh172 lines · 7.8 KiBRaw
| 1 | #!/bin/sh |
| 2 | # Installs the TV Launcher on your TV and makes it the default home screen. |
| 3 | # Needs adb (and, for the build step, the repo's Gradle wrapper). |
| 4 | # |
| 5 | # sh install-tv.sh <TV_IP> build + install + set as default home |
| 6 | # APK=path/to.apk sh install-tv.sh <TV_IP> |
| 7 | # install a prebuilt APK instead of building |
| 8 | # |
| 9 | # The TV and this machine must be on the same network, with USB/network |
| 10 | # debugging turned on on the TV (Settings > System > Developer options). |
| 11 | # An IP gets :5555 added; an adb serial (like emulator-5554) is used as is. |
| 12 | set -e |
| 13 | cd "$(dirname "$0")/.." |
| 14 | |
| 15 | TARGET="$1" |
| 16 | [ -n "$TARGET" ] || { echo "Usage: sh launcher/install-tv.sh <TV_IP>" >&2; exit 1; } |
| 17 | command -v adb >/dev/null 2>&1 || { |
| 18 | echo "adb is missing. Install it first:" >&2 |
| 19 | echo " Mac: brew install android-platform-tools" >&2 |
| 20 | echo " Windows: https://developer.android.com/tools/releases/platform-tools" >&2 |
| 21 | exit 1; } |
| 22 | |
| 23 | case "$TARGET" in |
| 24 | *.*.*.*:*) TV=$TARGET ;; |
| 25 | *.*.*.*) TV=$TARGET:5555 ;; |
| 26 | *) TV=$TARGET ;; |
| 27 | esac |
| 28 | case "$TV" in *:*) adb connect "$TV" >/dev/null || true ;; esac |
| 29 | |
| 30 | state=$(adb -s "$TV" get-state 2>&1 || true) |
| 31 | case "$state" in |
| 32 | device) ;; |
| 33 | *unauthorized*) echo "Your TV is asking \"Allow debugging?\". Press Allow (tick \"Always allow\"), then run this again." >&2; exit 1 ;; |
| 34 | *) echo "Can't reach $TV. Check the TV is on, on the same network, and has USB/network debugging turned on." >&2; exit 1 ;; |
| 35 | esac |
| 36 | |
| 37 | ME=com.vayunmathur.launcher/.MainActivity |
| 38 | |
| 39 | if [ -n "$APK" ]; then |
| 40 | [ -f "$APK" ] || { echo "No such APK: $APK" >&2; exit 1; } |
| 41 | else |
| 42 | echo "Building…" |
| 43 | ./gradlew :launcher:assembleDev --console=plain -q |
| 44 | APK_DIR=launcher/build/outputs/apk/dev |
| 45 | META=$APK_DIR/output-metadata.json |
| 46 | APK_NAME=launcher-dev.apk |
| 47 | if [ -f "$META" ]; then |
| 48 | FOUND=$(grep -oE '"outputFile"[[:space:]]*:[[:space:]]*"[^"]+"' "$META" | head -1 | sed -E 's/.*"([^"]+)"$/\1/') |
| 49 | [ -n "$FOUND" ] && APK_NAME=$FOUND |
| 50 | fi |
| 51 | APK=$APK_DIR/$APK_NAME |
| 52 | [ -f "$APK" ] || { echo "Build produced no APK where expected ($APK)." >&2; exit 1; } |
| 53 | fi |
| 54 | |
| 55 | echo "Installing… If your TV shows a Google Play Protect message, follow it on the TV to allow the install." |
| 56 | adb -s "$TV" install -r "$APK" |
| 57 | |
| 58 | # Keeps adb's exit status, so a failed command on the TV counts as failed here. |
| 59 | sh_tv() { _rc=0; _out=$(adb -s "$TV" shell "$@") || _rc=$?; [ -z "$_out" ] || printf '%s\n' "$_out" | tr -d '\r'; return $_rc; } |
| 60 | current_home() { sh_tv cmd package resolve-activity --brief -a android.intent.action.MAIN -c android.intent.category.HOME | tail -1; } |
| 61 | |
| 62 | # Make it the default home: the role (Android 10+) and the classic way. |
| 63 | if [ "$(sh_tv getprop ro.build.version.sdk)" -ge 29 ]; then |
| 64 | sh_tv cmd role add-role-holder --user 0 android.app.role.HOME com.vayunmathur.launcher >/dev/null 2>&1 || true |
| 65 | fi |
| 66 | sh_tv cmd package set-home-activity "$ME" >/dev/null 2>&1 || true |
| 67 | |
| 68 | # Known TV home screens that are safe to turn off. |
| 69 | LAUNCHERS="com.google.android.tvlauncher com.google.android.apps.tv.launcherx com.google.android.leanbacklauncher |
| 70 | com.mitv.tvhome com.amazon.tv.launcher" |
| 71 | |
| 72 | # Only known launchers get turned off. Settings, setup and System UI also answer Home, and |
| 73 | # the TV needs them to start, so they, unknown apps and anything always running stay on. |
| 74 | is_launcher() { |
| 75 | case "$1" in |
| 76 | com.vayunmathur.launcher|com.android.settings|com.android.tv.settings|com.android.systemui|com.android.provision) return 1 ;; |
| 77 | *setup*|*wraith*|*provision*|*systemui*|*tvcenter*|*.tv.framework*|*.tvinput*) return 1 ;; |
| 78 | esac |
| 79 | case " $(echo $LAUNCHERS) " in *" $1 "*) ;; *) return 1 ;; esac |
| 80 | # If we can't read its flags, or it always runs, leave it on. |
| 81 | flags=$(sh_tv dumpsys package "$1" 2>/dev/null | sed -n '/pkgFlags=\[/{p;q;}') |
| 82 | [ -n "$flags" ] || return 1 |
| 83 | case "$flags" in *PERSISTENT*) return 1 ;; esac |
| 84 | return 0 |
| 85 | } |
| 86 | |
| 87 | is_off() { sh_tv pm list packages -d "$1" | grep -Fqx "package:$1"; } |
| 88 | |
| 89 | # Turns apps off (not uninstall). Each one is saved on the TV first, so undo-tv.sh can |
| 90 | # always turn it back on. If it can't be saved, it's left on. |
| 91 | turn_off() { |
| 92 | for p in "$@"; do |
| 93 | if ! sh_tv "echo $p >> /data/local/tmp/launcher-tv-disabled.txt"; then |
| 94 | echo "Leaving $p on: couldn't save it for undo-tv.sh." >&2; continue |
| 95 | fi |
| 96 | sh_tv pm disable-user --user 0 "$p" >/dev/null 2>&1 || true |
| 97 | is_off "$p" || echo "Couldn't turn $p off." >&2 |
| 98 | done |
| 99 | # Android saves this a few seconds later. Restarting before then can undo it. |
| 100 | echo "Saving the change on the TV, this takes 15 seconds…" |
| 101 | sleep 15 |
| 102 | sh_tv cmd package set-home-activity "$ME" >/dev/null 2>&1 || true |
| 103 | } |
| 104 | |
| 105 | # Google TV's setup app. It's handled on its own below. |
| 106 | WRAITH=com.google.android.tungsten.setupwraith |
| 107 | home_apps() { sh_tv cmd package query-activities --brief -a android.intent.action.MAIN -c android.intent.category.HOME 2>/dev/null \ |
| 108 | | grep / | sed 's|/.*||; s/^ *//' | sort -u; } |
| 109 | |
| 110 | # Most TVs' own launchers still win when you press Home. The fix is to turn them off. |
| 111 | if [ "$(current_home)" != "$ME" ]; then |
| 112 | others="" |
| 113 | for p in $(home_apps); do |
| 114 | if is_launcher "$p"; then others="$others $p"; continue; fi |
| 115 | case "$p" in com.vayunmathur.launcher|com.android.settings|com.android.tv.settings|$WRAITH) ;; *) echo "Leaving $p on: it may be needed for your TV to start." ;; esac |
| 116 | done |
| 117 | others=$(echo $others | tr ' ' '\n') |
| 118 | if [ -n "$others" ]; then |
| 119 | echo "Your TV's own home screen keeps taking over:" |
| 120 | echo "$others" | sed 's/^/ /' |
| 121 | answer=${YES:+y} |
| 122 | if [ -z "$answer" ]; then |
| 123 | printf "Turn it off so Home opens TV Launcher? You can undo this with undo-tv.sh. [y/N] " |
| 124 | read answer </dev/tty || answer=n |
| 125 | fi |
| 126 | case "$answer" in |
| 127 | y|Y|yes) turn_off $others ;; |
| 128 | *) echo "Left it on. Your TV may keep opening its own home screen." ;; |
| 129 | esac |
| 130 | fi |
| 131 | fi |
| 132 | |
| 133 | # On some Google TVs (like Sony BRAVIA) the setup app wins Home once Google TV's home |
| 134 | # screen is off. It's only offered if Home opens exactly that app, Google TV's home screen |
| 135 | # is already off, the TV has finished its first setup, and the system doesn't keep it |
| 136 | # always running. YES=1 doesn't cover it: it needs its own answer, or YES_SETUP=1. |
| 137 | LAUNCHERX=com.google.android.apps.tv.launcherx |
| 138 | off_list=$(sh_tv pm list packages -d "$LAUNCHERX" 2>/dev/null) || off_list= |
| 139 | if [ "$(current_home | sed 's|/.*||')" = "$WRAITH" ] \ |
| 140 | && printf '%s\n' "$off_list" | grep -Fqx "package:$LAUNCHERX"; then |
| 141 | flags=$(sh_tv dumpsys package "$WRAITH" 2>/dev/null | sed -n '/pkgFlags=\[/{p;q;}') |
| 142 | [ -n "$flags" ] || flags=PERSISTENT |
| 143 | if [ "$(sh_tv settings get global device_provisioned)" != 1 ] || [ "$(sh_tv settings get secure user_setup_complete)" != 1 ]; then |
| 144 | echo "Leaving $WRAITH on: your TV hasn't finished its first setup." |
| 145 | else case "$flags" in |
| 146 | *PERSISTENT*) echo "Leaving $WRAITH on: it may be needed for your TV to start." ;; |
| 147 | *) |
| 148 | echo "Google TV's setup app ($WRAITH) is taking over Home." |
| 149 | echo "Turning it off fixed this on a Sony BRAVIA, but it hasn't been tested on every TV." |
| 150 | echo "If anything goes wrong, undo-tv.sh turns it back on." |
| 151 | if [ "$YES_SETUP" = 1 ]; then answer=y |
| 152 | elif [ -n "$YES" ]; then answer=n |
| 153 | else |
| 154 | printf "Turn off the setup app too? [y/N] " |
| 155 | read answer </dev/tty || answer=n |
| 156 | fi |
| 157 | case "$answer" in |
| 158 | y|Y|yes) turn_off "$WRAITH" ;; |
| 159 | *) echo "Left it on. Your TV may keep opening its setup app when you press Home. To turn it off later, run this again with YES_SETUP=1." ;; |
| 160 | esac ;; |
| 161 | esac; fi |
| 162 | fi |
| 163 | |
| 164 | # The row order lives in the app itself (DataStore): defaults lead, installed leanback apps |
| 165 | # top the row up, and the remote's edit menu (hold OK on a tile) persists changes. Nothing |
| 166 | # to push from here. |
| 167 | adb -s "$TV" shell input keyevent KEYCODE_HOME |
| 168 | if [ "$(current_home)" = "$ME" ]; then |
| 169 | echo "Done. Press Home on your remote to see it." |
| 170 | else |
| 171 | echo "Installed, but your TV still opens its own home screen. If it asks which home screen to use, pick \"TV Launcher\" and choose Always." |
| 172 | fi |