Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / launcher / undo-tv.sh
undo-tv.sh63 lines · 2.5 KiBRaw
| 1 | #!/bin/sh |
| 2 | # Removes TV Launcher and turns your TV's own home screen back on. |
| 3 | # Usage: sh undo-tv.sh <TV_IP> |
| 4 | set -e |
| 5 | [ -n "$1" ] || { echo "Usage: sh undo-tv.sh <TV_IP>" >&2; exit 1; } |
| 6 | case "$1" in |
| 7 | *.*.*.*:*) TV=$1 ;; |
| 8 | *.*.*.*) TV=$1:5555 ;; |
| 9 | *) TV=$1 ;; |
| 10 | esac |
| 11 | case "$TV" in *:*) adb connect "$TV" >/dev/null || true ;; esac |
| 12 | [ "$(adb -s "$TV" get-state 2>/dev/null)" = device ] || { echo "Can't reach $TV." >&2; exit 1; } |
| 13 | |
| 14 | failed= |
| 15 | # 0: the app is off. 1: it's on or not installed. 2: the TV couldn't be asked. |
| 16 | is_off() { |
| 17 | out=$(adb -s "$TV" shell pm list packages -d "$1") || return 2 |
| 18 | out=$(printf '%s\n' "$out" | tr -d '\r' | grep -v '^$') || return 1 |
| 19 | # Anything but package lines is an error message, even if pm said it worked. |
| 20 | printf '%s\n' "$out" | grep -qv '^package:' && return 2 |
| 21 | printf '%s\n' "$out" | grep -Fqx "package:$1" && return 0 |
| 22 | return 1 |
| 23 | } |
| 24 | # Turns an app back on and checks it worked. An app that's no longer installed counts as done. |
| 25 | turn_on() { |
| 26 | adb -s "$TV" shell pm enable "$1" >/dev/null 2>&1 || true |
| 27 | rc=0; is_off "$1" || rc=$? |
| 28 | case $rc in |
| 29 | 1) echo "Turned $1 back on" ;; |
| 30 | 0) echo "Couldn't turn $1 back on." >&2; failed=1 ;; |
| 31 | *) echo "Couldn't check that $1 is back on." >&2; failed=1 ;; |
| 32 | esac |
| 33 | } |
| 34 | |
| 35 | # Turn back on whatever install-tv.sh turned off, before removing TV Launcher. |
| 36 | # END marks a full read; no list at all is fine. |
| 37 | LIST=/data/local/tmp/launcher-tv-disabled.txt |
| 38 | list=$(adb -s "$TV" shell "if [ -e $LIST ]; then cat $LIST && echo END; else echo END; fi" 2>/dev/null) || list= |
| 39 | list=$(printf '%s\n' "$list" | tr -d '\r') |
| 40 | case "$list" in |
| 41 | *END) for p in $(printf '%s\n' "$list" | grep -vx END | sort -u); do turn_on "$p"; done ;; |
| 42 | *) echo "Couldn't read the list of apps install-tv.sh turned off." >&2; failed=1 ;; |
| 43 | esac |
| 44 | # Google TV's setup app may have been turned off by hand (see install-tv.sh), so turn it |
| 45 | # back on too. |
| 46 | W=com.google.android.tungsten.setupwraith |
| 47 | rc=0; is_off "$W" || rc=$? |
| 48 | case $rc in |
| 49 | 0) turn_on "$W" ;; |
| 50 | 1) ;; |
| 51 | *) echo "Couldn't check $W." >&2; failed=1 ;; |
| 52 | esac |
| 53 | |
| 54 | # If something stayed off, keep TV Launcher and the list, so Home still works and |
| 55 | # undo-tv.sh can try again. |
| 56 | if [ -n "$failed" ]; then |
| 57 | echo "Kept TV Launcher so your TV still has a home screen. Run undo-tv.sh again, or turn the app on with: adb shell pm enable <package>" >&2 |
| 58 | exit 1 |
| 59 | fi |
| 60 | adb -s "$TV" shell rm -f "$LIST" |
| 61 | adb -s "$TV" uninstall com.vayunmathur.launcher >/dev/null 2>&1 || true |
| 62 | adb -s "$TV" shell input keyevent KEYCODE_HOME |
| 63 | echo "Removed. Your TV is back to its own home screen." |