Skip to content

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

Launcher: decode the wallpaper at screen size, refuse one over 4K

abuhasanrumiea929baa6293
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java b/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
index 653182d8e..97ccf4f44 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
@@ -372,8 +372,9 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
}
/**
- * A theme's full-screen picture behind everything. Decoded once, downsampled to the screen so a 4K
- * JPEG costs a 1080p bitmap at most (about 8 MB), which is the price of a wallpaper on a 1 GB TV.
+ * A theme's full-screen picture behind everything. Decoded once at the size CENTER_CROP shows, so it
+ * costs a screen-sized bitmap (about 4 MB at 1080p), which is the price of a wallpaper on a 1 GB TV.
+ * A picture over 4K is refused rather than decoded (#166); the plain colour stays.
*/
private void showWallpaper(File file) {
try {
@@ -381,10 +382,17 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
android.graphics.BitmapFactory.Options opts = new android.graphics.BitmapFactory.Options();
opts.inJustDecodeBounds = true;
android.graphics.BitmapFactory.decodeFile(file.getPath(), opts);
- int sample = 1;
- while (opts.outWidth / (sample * 2) >= dm.widthPixels && opts.outHeight / (sample * 2) >= dm.heightPixels) sample *= 2;
+ int w = opts.outWidth, h = opts.outHeight;
+ if (!WallpaperSize.accepted(w, h)) return;
+ int sample = WallpaperSize.sampleSize(w, h, dm.widthPixels, dm.heightPixels);
opts.inJustDecodeBounds = false;
opts.inSampleSize = sample;
+ int[] scale = WallpaperSize.coverScale(w / sample, h / sample, dm.widthPixels, dm.heightPixels);
+ if (scale != null) {
+ opts.inScaled = true;
+ opts.inDensity = scale[0];
+ opts.inTargetDensity = scale[1];
+ }
opts.inPreferredConfig = android.graphics.Bitmap.Config.RGB_565; // no alpha needed, half the memory
android.graphics.Bitmap bmp = android.graphics.BitmapFactory.decodeFile(file.getPath(), opts);
if (bmp == null) return;
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/WallpaperSize.java b/launcher/app/src/main/java/com/gathin/tvhome/WallpaperSize.java
new file mode 100644
index 000000000..e685ce15f
--- /dev/null
+++ b/launcher/app/src/main/java/com/gathin/tvhome/WallpaperSize.java
@@ -0,0 +1,34 @@
+package com.gathin.tvhome;
+
+/**
+ * How big a theme's wallpaper may be and what size it is decoded at (#166). A 1 GB TV cannot afford a
+ * picture decoded at full size: a 65535x2159 image is about 283 MB even in RGB_565.
+ */
+final class WallpaperSize {
+ private WallpaperSize() { }
+
+ /** 4K UHD (3840x2160, 8.3 M pixels) and a little more; anything bigger is refused. */
+ static final long MAX_PIXELS = 8_400_000L;
+
+ static boolean accepted(int width, int height) {
+ return width > 0 && height > 0 && (long) width * height <= MAX_PIXELS;
+ }
+
+ /** The largest power-of-two sample size that still covers the screen in both directions. */
+ static int sampleSize(int width, int height, int screenW, int screenH) {
+ int sample = 1;
+ while (width / (sample * 2) >= screenW && height / (sample * 2) >= screenH) sample *= 2;
+ return sample;
+ }
+
+ /**
+ * {inDensity, inTargetDensity} that scale a (sampled) picture down to what CENTER_CROP shows: the
+ * side that limits the cover lands on the screen size. Null when it is already that small.
+ */
+ static int[] coverScale(int width, int height, int screenW, int screenH) {
+ if ((long) screenW * height >= (long) screenH * width) { // width limits the cover
+ return width > screenW ? new int[]{width, screenW} : null;
+ }
+ return height > screenH ? new int[]{height, screenH} : null;
+ }
+}
diff --git a/launcher/app/src/test/java/com/gathin/tvhome/WallpaperSizeTest.java b/launcher/app/src/test/java/com/gathin/tvhome/WallpaperSizeTest.java
new file mode 100644
index 000000000..c2a20b12e
--- /dev/null
+++ b/launcher/app/src/test/java/com/gathin/tvhome/WallpaperSizeTest.java
@@ -0,0 +1,44 @@
+package com.gathin.tvhome;
+
+import org.junit.Test;
+import static org.junit.Assert.assertArrayEquals;
+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 WallpaperSizeTest {
+ @Test public void veryWidePictureIsRefused() {
+ assertFalse(WallpaperSize.accepted(65535, 2159));
+ }
+
+ @Test public void fourKPictureIsAccepted() {
+ assertTrue(WallpaperSize.accepted(3840, 2160));
+ assertTrue(WallpaperSize.accepted(1920, 1080));
+ }
+
+ @Test public void unreadablePictureIsRefused() {
+ assertFalse(WallpaperSize.accepted(-1, -1));
+ assertFalse(WallpaperSize.accepted(0, 1080));
+ }
+
+ @Test public void fourKOn1080pSamplesByTwo() {
+ assertEquals(2, WallpaperSize.sampleSize(3840, 2160, 1920, 1080));
+ assertNull(WallpaperSize.coverScale(1920, 1080, 1920, 1080));
+ }
+
+ @Test public void photoBetweenSampleSizesIsScaledToTheScreen() {
+ // 3000x2000 keeps sample size 1; the cover is limited by the width, so it decodes 1920 wide.
+ assertEquals(1, WallpaperSize.sampleSize(3000, 2000, 1920, 1080));
+ assertArrayEquals(new int[]{3000, 1920}, WallpaperSize.coverScale(3000, 2000, 1920, 1080));
+ }
+
+ @Test public void widePictureIsScaledByItsHeight() {
+ assertArrayEquals(new int[]{2000, 1080}, WallpaperSize.coverScale(4000, 2000, 1920, 1080));
+ }
+
+ @Test public void smallPictureIsNotScaled() {
+ assertEquals(1, WallpaperSize.sampleSize(1280, 720, 1920, 1080));
+ assertNull(WallpaperSize.coverScale(1280, 720, 1920, 1080));
+ }
+}
diff --git a/themes/README.md b/themes/README.md
index c1e25037f..f10eacdce 100644
--- a/themes/README.md
+++ b/themes/README.md
@@ -48,7 +48,8 @@ is ignored, and a file that doesn't parse is ignored as a whole, so a theme can
`wallpaper.jpg` is drawn full screen behind the launcher, centre-cropped. Keep it dark, 1920×1080 and
under about 300 KB: the text, the hero name and the dock band are designed for black, and a 1 GB TV
-decodes the picture into memory on every Home start. Pictures larger than the screen are downsampled.
+decodes the picture into memory on every Home start. Pictures larger than the screen are scaled down to
+it; a picture over 4K (about 8.4 million pixels) is not shown at all and the plain colour stays.
## Reduced motion