Skip to content

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

Merge PR #356: feat(themes): rotate local wallpaper collections

Gathin231306b7acf809
diff --cc launcher/app/src/main/java/com/gathin/tvhome/Config.java
index 95540b6e6,c36df63aa..96f18aae5
--- a/launcher/app/src/main/java/com/gathin/tvhome/Config.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/Config.java
@@@ -72,14 -72,16 +72,20 @@@ final class Config
final int skyIntensity;
/** Remote keycodes that open Settings through HomeKeyService (config.xml, settings_key_keycodes). */
final int[] settingsKeycodes;
+ /** Extra keycodes that open the input switcher in InputKeyService (config.xml input_key_keycodes). */
+ final int[] inputKeycodes;
+ /** Remote buttons that open an app, "keycode:package" or "KEYCODE_NAME:package" (config.xml app_keys). */
+ final List<String> appKeys;
/** The Settings button opens the quick settings panel instead of the TV's Settings (settings_key_panel). */
final boolean settingsKeyPanel;
+ /** Whether themes may show a wallpaper (config.json, wallpaper). */
+ final boolean wallpaperEnabled;
/** Full-screen picture behind the launcher, or null for the plain colour. */
final File wallpaper;
+ /** Rotating theme images, or the single wallpaper when no collection is configured. */
+ final List<File> wallpapers;
+ final int wallpaperRotationMinutes;
+ final int wallpaperDim;
/** Whether the overlay file was present and parsed. */
final boolean overlayApplied;
/** Festive skin from a theme (festival, festival_until, greeting, accent_color); {@link Festival#NONE} if none. */
@@@ -90,10 -92,9 +96,11 @@@
int columns, int rowsBelowDock, boolean animations, boolean advancedEffects,
boolean shamsiDate, String greetingName, boolean moduleCards, boolean reducedMotion, boolean dockTitle, boolean dockRow, boolean reorderButton, String vpnApp,
boolean showHomeSettingsShortcut, boolean showStats, boolean appLibrary, int backgroundColor, int skyIntensity,
- int[] settingsKeycodes, boolean settingsKeyPanel, File wallpaper, boolean overlayApplied,
+ int[] settingsKeycodes, boolean settingsKeyPanel, boolean wallpaperEnabled, File wallpaper, List<File> wallpapers,
+ int wallpaperRotationMinutes, int wallpaperDim, boolean overlayApplied,
- Festival festival) {
+ Festival festival, int[] inputKeycodes, List<String> appKeys) {
+ this.inputKeycodes = inputKeycodes;
+ this.appKeys = Collections.unmodifiableList(appKeys);
this.dockApps = Collections.unmodifiableList(dockApps);
this.fillDock = fillDock;
this.hiddenApps = Collections.unmodifiableList(hiddenApps);
@@@ -218,8 -227,9 +237,9 @@@
return new Config(dock, fillDock, hidden, liveTv, liveTvAction, panelService, panelAction, inputAction,
Math.max(1, columns), Math.max(0, rowsBelow), animations, advancedEffects,
shamsi, greetingName, moduleCards, reducedMotion, dockTitle, dockRow, reorderButton, vpn, homeSettings, showStats, library, background,
- Math.max(0, Math.min(100, skyIntensity)), settingsKeycodes, settingsKeyPanel, wallpaper, applied,
+ Math.max(0, Math.min(100, skyIntensity)), settingsKeycodes, settingsKeyPanel, wallpaperEnabled, wallpaper,
+ wallpapers, rotationMinutes, wallpaperDim, applied,
- festival);
+ festival, inputKeycodes, appKeys);
}
private static void syncThemeChoices(SharedPreferences choices, File dir, File config) {
diff --cc launcher/app/src/main/java/com/gathin/tvhome/WallpaperPlayer.java
index 000000000,a00558902..7292276c6
mode 000000,100644..100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/WallpaperPlayer.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/WallpaperPlayer.java
@@@ -1,0 -1,132 +1,161 @@@
+ package com.gathin.tvhome;
+
+ import android.content.Context;
+ import android.content.SharedPreferences;
+ import android.graphics.Bitmap;
+ import android.graphics.BitmapFactory;
+ import android.util.DisplayMetrics;
+ import android.util.Log;
+ import android.view.View;
+ import android.view.ViewGroup;
+ import android.widget.FrameLayout;
+ import android.widget.ImageView;
+
+ import java.io.File;
+ import java.util.ArrayList;
+ import java.util.List;
+ import java.util.Random;
+ import java.util.concurrent.ExecutorService;
+ import java.util.concurrent.Executors;
+
+ /** Displays and rotates a theme's local wallpaper images while Home is visible. */
+ final class WallpaperPlayer {
+ private final FrameLayout root;
+ private final ImageView image;
+ private final List<File> files;
+ private final long interval;
+ private final SharedPreferences preferences;
+ private final ExecutorService decoder = Executors.newSingleThreadExecutor();
+ private final Random random = new Random();
+ private final int width;
+ private final int height;
+ private boolean active;
+ private boolean closed;
+ private boolean loading;
+ private String displayed = "";
+
+ WallpaperPlayer(Context context, FrameLayout root, Config config) {
+ this.root = root;
+ files = new ArrayList<>(config.wallpapers);
+ interval = config.wallpaperRotationMinutes * 60_000L;
+ preferences = context.getSharedPreferences("wallpaper_rotation", Context.MODE_PRIVATE);
+ DisplayMetrics metrics = context.getResources().getDisplayMetrics();
+ width = metrics.widthPixels;
+ height = metrics.heightPixels;
+ image = new ImageView(context);
+ image.setScaleType(ImageView.ScaleType.CENTER_CROP);
+ image.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);
+ if (config.wallpaperEnabled && !files.isEmpty()) {
+ root.addView(image, 0, new FrameLayout.LayoutParams(
+ ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
- View shade = new View(context);
- shade.setBackgroundColor((config.wallpaperDim * 255 / 100) << 24);
- shade.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);
- root.addView(shade, 1, new FrameLayout.LayoutParams(
- ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
++ // No shade layer at all when the theme doesn't dim: a full-screen view is overdraw on 1 GB TVs.
++ if (config.wallpaperDim > 0) {
++ View shade = new View(context);
++ shade.setBackgroundColor((config.wallpaperDim * 255 / 100) << 24);
++ shade.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);
++ root.addView(shade, 1, new FrameLayout.LayoutParams(
++ ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
++ }
++ showFirst();
++ }
++ }
++
++ /**
++ * The picture Home opens with is decoded right here, on the main thread, as the single wallpaper always was:
++ * Home's first frame already has it instead of drawing the plain colour and the picture popping in later.
++ * Only the later changes of a rotation decode in the background.
++ */
++ private void showFirst() {
++ String previous = preferences.getString("image", "");
++ File first = WallpaperRotation.find(files, previous);
++ if (first == null) first = WallpaperRotation.pick(files, previous, random);
++ while (first != null) {
++ Bitmap bitmap = decode(first);
++ if (bitmap != null) {
++ image.setImageBitmap(bitmap);
++ displayed = first.getName();
++ if (!displayed.equals(previous)) {
++ preferences.edit().putString("image", displayed)
++ .putLong("changed", System.currentTimeMillis()).apply();
++ }
++ return;
++ }
++ files.remove(first); // unreadable or over 4K: try another, or keep the plain colour
++ first = WallpaperRotation.pick(files, previous, random);
+ }
+ }
+
+ void start() {
+ active = true;
+ update();
+ }
+
+ void stop() {
+ active = false;
+ }
+
+ void close() {
+ active = false;
+ closed = true;
+ decoder.shutdownNow();
+ }
+
+ void update() {
+ if (!active || closed || loading || files.isEmpty()) return;
+ String previous = preferences.getString("image", "");
+ File selected = WallpaperRotation.find(files, previous);
+ long changed = preferences.getLong("changed", 0);
+ if (selected == null || WallpaperRotation.due(changed, System.currentTimeMillis(), interval)) {
+ selected = WallpaperRotation.pick(files, previous, random);
+ }
+ if (selected == null || selected.getName().equals(displayed)) return;
+ File next = selected;
+ loading = true;
+ decoder.execute(() -> {
+ Bitmap bitmap = decode(next);
+ root.post(() -> {
+ loading = false;
+ if (closed || !active) {
+ if (bitmap != null) bitmap.recycle();
+ return;
+ }
+ if (bitmap == null) {
+ files.remove(next);
+ Log.w("TvHome.Wallpaper", "Skipping unreadable image: " + next.getName());
+ update();
+ return;
+ }
+ image.setImageBitmap(bitmap);
+ displayed = next.getName();
+ if (!displayed.equals(previous) || changed == 0) {
+ preferences.edit().putString("image", displayed)
+ .putLong("changed", System.currentTimeMillis()).apply();
+ }
+ });
+ });
+ }
+
+ private Bitmap decode(File file) {
+ try {
+ BitmapFactory.Options options = new BitmapFactory.Options();
+ options.inJustDecodeBounds = true;
+ BitmapFactory.decodeFile(file.getPath(), options);
+ int width = options.outWidth, height = options.outHeight;
+ if (width <= 0 || height <= 0) return null;
+ int sample = WallpaperSize.sampleSize(width, height, this.width, this.height);
+ if (!WallpaperSize.accepted(width / sample, height / sample)) return null;
+ options.inJustDecodeBounds = false;
+ options.inSampleSize = sample;
+ int[] scale = WallpaperSize.coverScale(width / sample, height / sample, this.width, this.height);
+ if (scale != null) {
+ options.inScaled = true;
+ options.inDensity = scale[0];
+ options.inTargetDensity = scale[1];
+ }
+ options.inPreferredConfig = Bitmap.Config.RGB_565;
+ return BitmapFactory.decodeFile(file.getPath(), options);
+ } catch (Exception | OutOfMemoryError failure) {
+ return null;
+ }
+ }
+ }
diff --cc launcher/app/src/test/java/com/gathin/tvhome/WallpaperRotationTest.java
index 000000000,000000000..b75d8d294
new file mode 100644
--- /dev/null
+++ b/launcher/app/src/test/java/com/gathin/tvhome/WallpaperRotationTest.java
@@@ -1,0 -1,0 +1,77 @@@
++package com.gathin.tvhome;
++
++import static org.junit.Assert.assertEquals;
++import static org.junit.Assert.assertFalse;
++import static org.junit.Assert.assertNotEquals;
++import static org.junit.Assert.assertNull;
++import static org.junit.Assert.assertTrue;
++
++import java.io.File;
++import java.io.FileOutputStream;
++import java.io.IOException;
++import java.nio.file.Files;
++import java.util.Arrays;
++import java.util.Collections;
++import java.util.List;
++import java.util.Random;
++
++import org.junit.Test;
++
++public class WallpaperRotationTest {
++
++ private static File touch(File dir, String name, int bytes) throws IOException {
++ File f = new File(dir, name);
++ try (FileOutputStream out = new FileOutputStream(f)) {
++ out.write(new byte[bytes]);
++ }
++ return f;
++ }
++
++ @Test
++ public void imagesKeepsPicturesOnlySortedByName() throws IOException {
++ File dir = Files.createTempDirectory("walls").toFile();
++ touch(dir, "b.JPG", 10);
++ touch(dir, "a.png", 10);
++ touch(dir, "notes.txt", 10);
++ touch(dir, "empty.jpg", 0);
++ new File(dir, "folder.jpg").mkdir();
++ List<File> images = WallpaperRotation.images(dir);
++ assertEquals(Arrays.asList("a.png", "b.JPG"), names(images));
++ assertTrue(WallpaperRotation.images(null).isEmpty());
++ assertTrue(WallpaperRotation.images(new File(dir, "missing")).isEmpty());
++ }
++
++ @Test
++ public void pickAvoidsThePreviousPictureWhenThereIsAnother() {
++ List<File> images = Arrays.asList(new File("a.jpg"), new File("b.jpg"));
++ Random random = new Random(1);
++ for (int i = 0; i < 20; i++) {
++ assertNotEquals("a.jpg", WallpaperRotation.pick(images, "a.jpg", random).getName());
++ }
++ assertEquals("a.jpg", WallpaperRotation.pick(Collections.singletonList(new File("a.jpg")), "a.jpg", random).getName());
++ assertNull(WallpaperRotation.pick(Collections.emptyList(), "", random));
++ }
++
++ @Test
++ public void findMatchesByName() {
++ List<File> images = Arrays.asList(new File("a.jpg"), new File("b.jpg"));
++ assertEquals("b.jpg", WallpaperRotation.find(images, "b.jpg").getName());
++ assertNull(WallpaperRotation.find(images, "c.jpg"));
++ }
++
++ @Test
++ public void dueOnlyWithRotationOnAndTheIntervalPassed() {
++ long minute = 60_000L;
++ assertFalse(WallpaperRotation.due(0, 10 * minute, 0)); // rotation off
++ assertTrue(WallpaperRotation.due(0, 10 * minute, 5 * minute)); // never changed yet
++ assertFalse(WallpaperRotation.due(8 * minute, 10 * minute, 5 * minute));
++ assertTrue(WallpaperRotation.due(5 * minute, 10 * minute, 5 * minute));
++ assertTrue(WallpaperRotation.due(20 * minute, 10 * minute, 5 * minute)); // clock went back
++ }
++
++ private static List<String> names(List<File> files) {
++ String[] out = new String[files.size()];
++ for (int i = 0; i < out.length; i++) out[i] = files.get(i).getName();
++ return Arrays.asList(out);
++ }
++}