Skip to content

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

ThemeProvider: the allowed file names live in ThemeFiles, with unit tests

Mateus Zanes7be1f65e8412
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/ThemeFiles.java b/launcher/app/src/main/java/com/gathin/tvhome/ThemeFiles.java
new file mode 100644
index 000000000..bf7852597
--- /dev/null
+++ b/launcher/app/src/main/java/com/gathin/tvhome/ThemeFiles.java
@@ -0,0 +1,37 @@
+package com.gathin.tvhome;
+
+import java.util.List;
+import java.util.regex.Pattern;
+
+/** The files ThemeProvider may write or delete under Config.dir, by the URI's path segments. */
+final class ThemeFiles {
+ 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 ThemeFiles() {}
+
+ /** The path relative to the theme directory, e.g. "config.json" or "wallpapers/dusk.jpg", or null if refused. */
+ static String path(List<String> segments) {
+ if (segments == null) return null;
+ if (segments.size() == 1) {
+ String name = segments.get(0);
+ return Config.FILE_NAME.equals(name) || "wallpaper.jpg".equals(name) || "theme_revision".equals(name)
+ ? name : null;
+ }
+ if (segments.size() == 2 && WALLPAPERS.equals(segments.get(0)) && collectionImage(segments.get(1))) {
+ return WALLPAPERS + "/" + segments.get(1);
+ }
+ return null;
+ }
+
+ /** True for "wallpapers" alone: a delete of it clears the collection's images. */
+ static boolean isCollection(List<String> segments) {
+ return segments != null && segments.size() == 1 && WALLPAPERS.equals(segments.get(0));
+ }
+
+ static boolean collectionImage(String name) {
+ return name != null && !name.startsWith(".") && IMAGE.matcher(name).matches();
+ }
+}
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/ThemeProvider.java b/launcher/app/src/main/java/com/gathin/tvhome/ThemeProvider.java
index c3f00c229..183f9c18b 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/ThemeProvider.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/ThemeProvider.java
@@ -8,14 +8,11 @@ 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.
+ * locked out of Android/data (TCL G07, Android 11). Only the files {@link ThemeFiles} allows, 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
@@ -24,26 +21,14 @@ import java.util.regex.Pattern;
*/
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();
+ String path = ThemeFiles.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));
+ if (dir == null || path == null) throw new FileNotFoundException(String.valueOf(uri));
+ File f = new File(dir, path);
+ File parent = f.getParentFile();
+ if (parent != null && !parent.isDirectory() && !parent.mkdirs()) throw new FileNotFoundException(String.valueOf(uri));
+ return f;
}
@Override
@@ -58,10 +43,9 @@ public class ThemeProvider extends ContentProvider {
@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());
+ if (dir != null && ThemeFiles.isCollection(uri.getPathSegments())) {
+ File[] images = new File(dir, ThemeFiles.WALLPAPERS).listFiles((d, name) -> ThemeFiles.collectionImage(name));
int n = 0;
if (images != null) for (File f : images) if (f.delete()) n++;
return n;
diff --git a/launcher/app/src/test/java/com/gathin/tvhome/ThemeFilesTest.java b/launcher/app/src/test/java/com/gathin/tvhome/ThemeFilesTest.java
new file mode 100644
index 000000000..7e4e2c92c
--- /dev/null
+++ b/launcher/app/src/test/java/com/gathin/tvhome/ThemeFilesTest.java
@@ -0,0 +1,45 @@
+package com.gathin.tvhome;
+
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.Collections;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+public class ThemeFilesTest {
+ @Test public void themeFilesAreAllowed() {
+ assertEquals("config.json", ThemeFiles.path(Collections.singletonList("config.json")));
+ assertEquals("wallpaper.jpg", ThemeFiles.path(Collections.singletonList("wallpaper.jpg")));
+ assertEquals("theme_revision", ThemeFiles.path(Collections.singletonList("theme_revision")));
+ }
+
+ @Test public void collectionImagesAreAllowed() {
+ assertEquals("wallpapers/dusk.jpg", ThemeFiles.path(Arrays.asList("wallpapers", "dusk.jpg")));
+ assertEquals("wallpapers/Night_2.WEBP", ThemeFiles.path(Arrays.asList("wallpapers", "Night_2.WEBP")));
+ }
+
+ @Test public void anythingElseIsRefused() {
+ assertNull(ThemeFiles.path(null));
+ assertNull(ThemeFiles.path(Collections.emptyList()));
+ assertNull(ThemeFiles.path(Collections.singletonList("cards.json")));
+ assertNull(ThemeFiles.path(Collections.singletonList("..")));
+ assertNull(ThemeFiles.path(Arrays.asList("..", "config.json")));
+ assertNull(ThemeFiles.path(Arrays.asList("wallpapers", "..")));
+ assertNull(ThemeFiles.path(Arrays.asList("wallpapers", "run.sh")));
+ assertNull(ThemeFiles.path(Arrays.asList("wallpapers", "a b.jpg")));
+ assertNull(ThemeFiles.path(Arrays.asList("wallpapers", "../config.jpg")));
+ assertNull(ThemeFiles.path(Arrays.asList("wallpapers", ".jpg")));
+ assertNull(ThemeFiles.path(Arrays.asList("other", "dusk.jpg")));
+ assertNull(ThemeFiles.path(Arrays.asList("wallpapers", "sub", "dusk.jpg")));
+ }
+
+ @Test public void onlyTheBareFolderIsTheCollection() {
+ assertTrue(ThemeFiles.isCollection(Collections.singletonList("wallpapers")));
+ assertFalse(ThemeFiles.isCollection(Arrays.asList("wallpapers", "dusk.jpg")));
+ assertFalse(ThemeFiles.isCollection(Collections.singletonList("config.json")));
+ }
+}