Skip to content

Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.

theme.sh: write the theme through a ThemeProvider where the shell can't reach Android/data

Mateus Zanesc05c4f49eb02
diff --git a/docs/TROUBLESHOOTING.md b/docs/TROUBLESHOOTING.md
index 9c50fbe8d..c60074daa 100644
--- a/docs/TROUBLESHOOTING.md
+++ b/docs/TROUBLESHOOTING.md
@@ -93,6 +93,11 @@ Things that bit us or contributors, so they don't bite you. Commands use `tools/
android.display`, and the main log shows `setupwraith/.RecoveryActivity` being started every
200 ms or so. If you're already in the loop:
`tools/tv.py 'pm enable --user 0 com.google.android.apps.tv.launcherx'`.
+- **`theme.sh` fails with "couldn't create file: Permission denied"**, seen on TCL G07 (Android 11): the
+ shell is not in the group that may write `Android/data`. `theme.sh` stages the files in
+ `/data/local/tmp` and, when copying them in is refused, writes them through the launcher's
+ `ThemeProvider` instead. A launcher installed before that provider existed doesn't have it: run
+ `launcher/build-and-install.sh` once, then push the theme again.
- **No on-screen keyboard** after switching launchers, seen on Xiaomi: the default input method
pointed at a Baidu keyboard that isn't installed. Fix:
`tools/tv.py 'ime set com.google.android.inputmethod.latin/com.android.inputmethod.latin.LatinIME'`
diff --git a/launcher/app/src/main/AndroidManifest.xml b/launcher/app/src/main/AndroidManifest.xml
index f56e008ea..b92398fa1 100644
--- a/launcher/app/src/main/AndroidManifest.xml
+++ b/launcher/app/src/main/AndroidManifest.xml
@@ -112,6 +112,14 @@
</intent-filter>
</service>
+ <!-- tools/theme.sh writes the theme through this where the shell can't reach Android/data;
+ see ThemeProvider. WRITE_SECURE_SETTINGS: the adb shell has it, ordinary apps don't. -->
+ <provider
+ android:name=".ThemeProvider"
+ android:authorities="com.gathin.tvhome.theme"
+ android:exported="true"
+ android:permission="android.permission.WRITE_SECURE_SETTINGS" />
+
<!-- The remote's Input button (KEYCODE_TV or KEYCODE_TV_INPUT) opens the input switcher, and input_key_keycodes and
app_keys (config.xml or a theme) bring back other remote buttons; see InputKeyService. -->
<service
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/ThemeProvider.java b/launcher/app/src/main/java/com/gathin/tvhome/ThemeProvider.java
new file mode 100644
index 000000000..c3f00c229
--- /dev/null
+++ b/launcher/app/src/main/java/com/gathin/tvhome/ThemeProvider.java
@@ -0,0 +1,81 @@
+package com.gathin.tvhome;
+
+import android.content.ContentProvider;
+import android.content.ContentValues;
+import android.database.Cursor;
+import android.net.Uri;
+import android.os.ParcelFileDescriptor;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.util.List;
+import java.util.regex.Pattern;
+
+/**
+ * Lets tools/theme.sh write the theme files into {@link Config#dir} on firmware where the adb shell is
+ * locked out of Android/data (TCL G07, Android 11). Only config.json, wallpaper.jpg, theme_revision and
+ * images in wallpapers/, and only for callers holding WRITE_SECURE_SETTINGS, which the adb shell has and
+ * ordinary apps cannot get.
+ *
+ * content write --uri content://com.gathin.tvhome.theme/config.json < /data/local/tmp/config.json
+ * content write --uri content://com.gathin.tvhome.theme/wallpapers/dusk.jpg < /data/local/tmp/dusk.jpg
+ * content delete --uri content://com.gathin.tvhome.theme/config.json
+ * content delete --uri content://com.gathin.tvhome.theme/wallpapers (every image in the collection)
+ */
+public class ThemeProvider extends ContentProvider {
+
+ private static final String WALLPAPERS = "wallpapers";
+ /** The names tools/theme.sh accepts for a collection image, and the types WallpaperRotation reads. */
+ private static final Pattern IMAGE = Pattern.compile("[A-Za-z0-9_.-]+\\.(jpg|jpeg|png|webp)",
+ Pattern.CASE_INSENSITIVE);
+
+ private File file(Uri uri) throws FileNotFoundException {
+ List<String> path = uri.getPathSegments();
+ File dir = Config.dir(getContext());
+ if (dir != null && path.size() == 1) {
+ String name = path.get(0);
+ if (Config.FILE_NAME.equals(name) || "wallpaper.jpg".equals(name) || "theme_revision".equals(name)) {
+ return new File(dir, name);
+ }
+ }
+ if (dir != null && path.size() == 2 && WALLPAPERS.equals(path.get(0)) && IMAGE.matcher(path.get(1)).matches()) {
+ File collection = new File(dir, WALLPAPERS);
+ if (!collection.isDirectory() && !collection.mkdirs()) throw new FileNotFoundException(String.valueOf(uri));
+ return new File(collection, path.get(1));
+ }
+ throw new FileNotFoundException(String.valueOf(uri));
+ }
+
+ @Override
+ public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
+ File f = file(uri);
+ if (mode.contains("w")) {
+ return ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_WRITE_ONLY
+ | ParcelFileDescriptor.MODE_CREATE | ParcelFileDescriptor.MODE_TRUNCATE);
+ }
+ return ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
+ }
+
+ @Override
+ public int delete(Uri uri, String selection, String[] selectionArgs) {
+ List<String> path = uri.getPathSegments();
+ File dir = Config.dir(getContext());
+ if (dir != null && path.size() == 1 && WALLPAPERS.equals(path.get(0))) {
+ File[] images = new File(dir, WALLPAPERS).listFiles((d, name) -> IMAGE.matcher(name).matches());
+ int n = 0;
+ if (images != null) for (File f : images) if (f.delete()) n++;
+ return n;
+ }
+ try {
+ return file(uri).delete() ? 1 : 0;
+ } catch (FileNotFoundException e) {
+ return 0;
+ }
+ }
+
+ @Override public boolean onCreate() { return true; }
+ @Override public String getType(Uri uri) { return null; }
+ @Override public Cursor query(Uri uri, String[] p, String s, String[] a, String o) { return null; }
+ @Override public Uri insert(Uri uri, ContentValues values) { return null; }
+ @Override public int update(Uri uri, ContentValues v, String s, String[] a) { return 0; }
+}
diff --git a/tools/theme.ps1 b/tools/theme.ps1
index b4ad1443a..3d0cc732c 100644
--- a/tools/theme.ps1
+++ b/tools/theme.ps1
@@ -27,8 +27,17 @@ $revision = [guid]::NewGuid().ToString('N')
# clear the list, write it back and switch accessibility on again.
$restart = "a=`$(settings get secure enabled_accessibility_services); am force-stop $pkg; am start -n $pkg/.MainActivity >/dev/null; case `"`$a`" in *$pkg*) sleep 1; settings put secure enabled_accessibility_services ''; settings put secure enabled_accessibility_services `"`$a`"; settings put secure accessibility_enabled 1 ;; esac"
+# Some firmware (TCL G07, Android 11) locks the shell out of Android/data. Files are staged in
+# /data/local/tmp and copied in, or written through the launcher's ThemeProvider when that is refused.
+$uri = "content://$pkg.theme"
+$tmp = '/data/local/tmp/cyt-theme'
+# A file's staging path; wallpapers/a.jpg is staged as $tmp-wallpapers-a.jpg.
+function Stage($name) { "$tmp-$($name -replace '/', '-')" }
+function Put($name) { $t = Stage $name; "cp $t $dir/$name 2>/dev/null || content write --uri $uri/$name < $t; rm -f $t" }
+function Del($name) { "rm -f $dir/$name 2>/dev/null; content delete --uri $uri/$name 2>/dev/null" }
+
if ($Reset) {
- & $py -I tv.py "mkdir -p $dir; rm -f $dir/config.json $dir/wallpaper.jpg; printf '%s' '$revision' > $dir/theme_revision; $restart; echo reset"
+ & $py -I tv.py "mkdir -p $dir 2>/dev/null; $(Del 'config.json'); $(Del 'wallpaper.jpg'); printf '%s' '$revision' > $tmp-theme_revision; $(Put 'theme_revision'); $restart; echo reset"
exit 0
}
if (-not $Theme) { throw 'usage: theme.ps1 <theme folder> | -Reset' }
@@ -36,18 +45,20 @@ $config = Join-Path $Theme 'config.json'
if (-not (Test-Path $config)) { throw "no config.json in $Theme" }
try { Get-Content $config -Raw | ConvertFrom-Json | Out-Null } catch { throw "$config is not valid JSON" }
-& $py -I tv.py "mkdir -p $dir" | Out-Null
-& $py -I tv.py --push $config "$dir/config.json"
+& $py -I tv.py "mkdir -p $dir 2>/dev/null" | Out-Null
+& $py -I tv.py --push $config "$tmp-config.json" | Out-Null
+& $py -I tv.py (Put 'config.json')
$wall = Join-Path $Theme 'wallpaper.jpg'
-if (Test-Path $wall) { & $py -I b64push.py $wall "$dir/wallpaper.jpg" } else { & $py -I tv.py "rm -f $dir/wallpaper.jpg" | Out-Null }
-& $py -I tv.py "mkdir -p $dir/wallpapers; rm -f $dir/wallpapers/*.jpg $dir/wallpapers/*.jpeg $dir/wallpapers/*.png $dir/wallpapers/*.webp $dir/wallpapers/*.JPG $dir/wallpapers/*.JPEG $dir/wallpapers/*.PNG $dir/wallpapers/*.WEBP" | Out-Null
+if (Test-Path $wall) { & $py -I b64push.py $wall "$tmp-wallpaper.jpg"; & $py -I tv.py (Put 'wallpaper.jpg') } else { & $py -I tv.py (Del 'wallpaper.jpg') | Out-Null }
+& $py -I tv.py "mkdir -p $dir/wallpapers 2>/dev/null; rm -f $dir/wallpapers/*.jpg $dir/wallpapers/*.jpeg $dir/wallpapers/*.png $dir/wallpapers/*.webp $dir/wallpapers/*.JPG $dir/wallpapers/*.JPEG $dir/wallpapers/*.PNG $dir/wallpapers/*.WEBP 2>/dev/null; content delete --uri $uri/wallpapers 2>/dev/null" | Out-Null
$wallpapers = Join-Path $Theme 'wallpapers'
if (Test-Path $wallpapers -PathType Container) {
Get-ChildItem $wallpapers -File | Where-Object { $_.Extension -match '^\.(jpg|jpeg|png|webp)$' } | ForEach-Object {
if ($_.Name -notmatch '^[A-Za-z0-9_.-]+$') { throw "unsupported wallpaper filename: $($_.Name)" }
- & $py -I b64push.py $_.FullName "$dir/wallpapers/$($_.Name)"
+ & $py -I b64push.py $_.FullName (Stage "wallpapers/$($_.Name)")
+ & $py -I tv.py (Put "wallpapers/$($_.Name)") | Out-Null
}
}
-& $py -I tv.py "printf '%s' '$revision' > $dir/theme_revision" | Out-Null
+& $py -I tv.py "printf '%s' '$revision' > $tmp-theme_revision; $(Put 'theme_revision')" | Out-Null
& $py -I tv.py "$restart; echo applied"
Write-Host "Theme $(Split-Path $Theme -Leaf) is on the TV. Undo with: tools\theme.ps1 -TvHost $TvHost -Reset"
diff --git a/tools/theme.sh b/tools/theme.sh
index 44db03f0b..2c260a9a0 100755
--- a/tools/theme.sh
+++ b/tools/theme.sh
@@ -30,8 +30,17 @@ tv() { "$PY" -I tv.py "$@"; }
# back and switch accessibility on again as build-and-install does.
RESTART="a=\$(settings get secure enabled_accessibility_services); am force-stop $PKG; am start -n $PKG/.MainActivity >/dev/null; case \"\$a\" in *$PKG*) sleep 1; settings put secure enabled_accessibility_services ''; settings put secure enabled_accessibility_services \"\$a\"; settings put secure accessibility_enabled 1 ;; esac"
+# Some firmware (TCL G07, Android 11) locks the shell out of Android/data. Files are staged in
+# /data/local/tmp and copied in, or written through the launcher's ThemeProvider when that is refused.
+URI=content://$PKG.theme
+TMP=/data/local/tmp/cyt-theme
+# A file's staging path; wallpapers/a.jpg is staged as $TMP-wallpapers-a.jpg.
+stage() { echo "$TMP-${1//\//-}"; }
+put() { echo "cp $(stage "$1") $DIR/$1 2>/dev/null || content write --uri $URI/$1 < $(stage "$1"); rm -f $(stage "$1")"; }
+del() { echo "rm -f $DIR/$1 2>/dev/null; content delete --uri $URI/$1 2>/dev/null"; }
+
if [ "${1:-}" = --reset ]; then
- tv "mkdir -p $DIR; rm -f $DIR/config.json $DIR/wallpaper.jpg; printf '%s' '$REVISION' > $DIR/theme_revision; $RESTART; echo reset" | tail -n 1
+ tv "mkdir -p $DIR 2>/dev/null; $(del config.json); $(del wallpaper.jpg); printf '%s' '$REVISION' > $TMP-theme_revision; $(put theme_revision); $RESTART; echo reset" | tail -n 1
exit 0
fi
THEME="${THEME_ARG:?usage: theme.sh <theme folder> | --reset}"
@@ -39,14 +48,16 @@ THEME="${THEME_ARG:?usage: theme.sh <theme folder> | --reset}"
"$PY" -I -c 'import json,sys; json.load(open(sys.argv[1]))' "$THEME/config.json" 2>/dev/null \
|| { echo "$THEME/config.json is not valid JSON" >&2; exit 1; }
-tv "mkdir -p $DIR" >/dev/null
-tv --push "$THEME/config.json" "$DIR/config.json"
+tv "mkdir -p $DIR" >/dev/null 2>&1 || true
+tv --push "$THEME/config.json" "$TMP-config.json" >/dev/null
+tv "$(put config.json)"
if [ -f "$THEME/wallpaper.jpg" ]; then
- "$PY" -I b64push.py "$THEME/wallpaper.jpg" "$DIR/wallpaper.jpg"
+ "$PY" -I b64push.py "$THEME/wallpaper.jpg" "$TMP-wallpaper.jpg"
+ tv "$(put wallpaper.jpg)"
else
- tv "rm -f $DIR/wallpaper.jpg" >/dev/null
+ tv "$(del wallpaper.jpg)" >/dev/null
fi
-tv "mkdir -p $DIR/wallpapers; rm -f $DIR/wallpapers/*.jpg $DIR/wallpapers/*.jpeg $DIR/wallpapers/*.png $DIR/wallpapers/*.webp $DIR/wallpapers/*.JPG $DIR/wallpapers/*.JPEG $DIR/wallpapers/*.PNG $DIR/wallpapers/*.WEBP" >/dev/null
+tv "mkdir -p $DIR/wallpapers 2>/dev/null; rm -f $DIR/wallpapers/*.jpg $DIR/wallpapers/*.jpeg $DIR/wallpapers/*.png $DIR/wallpapers/*.webp $DIR/wallpapers/*.JPG $DIR/wallpapers/*.JPEG $DIR/wallpapers/*.PNG $DIR/wallpapers/*.WEBP 2>/dev/null; content delete --uri $URI/wallpapers 2>/dev/null" >/dev/null
if [ -d "$THEME/wallpapers" ]; then
for IMAGE in "$THEME"/wallpapers/*; do
[ -f "$IMAGE" ] || continue
@@ -54,10 +65,11 @@ if [ -d "$THEME/wallpapers" ]; then
*.jpg|*.jpeg|*.png|*.webp|*.JPG|*.JPEG|*.PNG|*.WEBP)
NAME=$(basename "$IMAGE")
[[ "$NAME" =~ ^[A-Za-z0-9_.-]+$ ]] || { echo "unsupported wallpaper filename: $NAME" >&2; exit 1; }
- "$PY" -I b64push.py "$IMAGE" "$DIR/wallpapers/$NAME" ;;
+ "$PY" -I b64push.py "$IMAGE" "$(stage "wallpapers/$NAME")"
+ tv "$(put "wallpapers/$NAME")" >/dev/null ;;
esac
done
fi
-tv "printf '%s' '$REVISION' > $DIR/theme_revision" >/dev/null
+tv "printf '%s' '$REVISION' > $TMP-theme_revision; $(put theme_revision)" >/dev/null
tv "$RESTART; echo applied" | tail -n 1
echo "Theme $(basename "$THEME") is on the TV. Undo with: tools/theme.sh --reset"