Skip to content

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

perf(launcher): release wallpaper on background trim

maxsonferovanteb32236486237
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 dca4f8424..7b2856c6f 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
@@ -1059,9 +1059,7 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
super.onTrimMemory(level);
// RUNNING_LOW and CRITICAL also arrive while Home is visible when an accessibility service keeps the
// process in the foreground; never blank the screen the owner is looking at.
- boolean due = level >= TRIM_MEMORY_BACKGROUND
- || level == TRIM_MEMORY_RUNNING_LOW || level == TRIM_MEMORY_RUNNING_CRITICAL;
- if (!due || started) return;
+ if (!MemoryTrimPolicy.shouldRelease(level, started)) return;
adapter.clear();
// Detach the adapter too: with Home stopped no layout pass runs, so the tile views (and the bitmaps
// they hold) would stay attached until Home comes back. setAdapter(null) recycles them now.
@@ -1069,6 +1067,7 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
grid.getRecycledViewPool().clear();
heroArtCache.clear();
heroArtKey = null;
+ wallpaperPlayer.releaseForMemoryPressure();
tileRefresh.invalidate();
}
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/MemoryTrimPolicy.java b/launcher/app/src/main/java/com/gathin/tvhome/MemoryTrimPolicy.java
new file mode 100644
index 000000000..25c808bcd
--- /dev/null
+++ b/launcher/app/src/main/java/com/gathin/tvhome/MemoryTrimPolicy.java
@@ -0,0 +1,16 @@
+package com.gathin.tvhome;
+
+import android.content.ComponentCallbacks2;
+
+/** Memory that affects the picture is released only when Home is not on screen. */
+final class MemoryTrimPolicy {
+ private MemoryTrimPolicy() {
+ }
+
+ static boolean shouldRelease(int level, boolean homeStarted) {
+ if (homeStarted) return false;
+ return level >= ComponentCallbacks2.TRIM_MEMORY_BACKGROUND
+ || level == ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW
+ || level == ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL;
+ }
+}
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/WallpaperPlayer.java b/launcher/app/src/main/java/com/gathin/tvhome/WallpaperPlayer.java
index b0c1592a1..8da8dfa64 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/WallpaperPlayer.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/WallpaperPlayer.java
@@ -33,6 +33,7 @@ import java.util.concurrent.Executors;
final class WallpaperPlayer {
private final FrameLayout root;
private final ImageView image;
+ private final int backgroundColor;
private final List<File> files;
private final long interval;
private final String order;
@@ -63,6 +64,7 @@ final class WallpaperPlayer {
WallpaperPlayer(Context context, FrameLayout root, Config config) {
this.root = root;
+ backgroundColor = config.backgroundColor;
files = new ArrayList<>(config.wallpapers);
interval = config.wallpaperRotationMinutes * 60_000L;
bitmapConfig = config.wallpaperTrueColor ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
@@ -135,6 +137,17 @@ final class WallpaperPlayer {
active = false;
}
+ /** Drops the wallpaper texture when Home is hidden and Android asks the process to free memory. */
+ void releaseForMemoryPressure() {
+ if (current == null) return;
+ image.setImageDrawable(null);
+ // The renderer may still have a queued frame that references this hardware bitmap. Drop our references and
+ // let Android reclaim its native buffer after rendering releases it instead of recycling it mid-frame.
+ current = null;
+ displayed = "";
+ root.setBackgroundColor(backgroundColor);
+ }
+
/** The pictures the rotation picks from now: wallpaper_moods' group for the time of day, or the whole collection. */
private List<File> playable() {
if (moods.isEmpty()) return files;
@@ -250,6 +263,8 @@ final class WallpaperPlayer {
private void show(Bitmap bitmap) {
Bitmap old = current;
current = bitmap;
+ // The wallpaper covers the whole root; avoid drawing the fallback colour under it.
+ root.setBackground(null);
image.setImageBitmap(bitmap);
if (old != null && old != bitmap) image.postDelayed(old::recycle, 500);
}
diff --git a/launcher/app/src/test/java/com/gathin/tvhome/MemoryTrimPolicyTest.java b/launcher/app/src/test/java/com/gathin/tvhome/MemoryTrimPolicyTest.java
new file mode 100644
index 000000000..f0a0ebb68
--- /dev/null
+++ b/launcher/app/src/test/java/com/gathin/tvhome/MemoryTrimPolicyTest.java
@@ -0,0 +1,30 @@
+package com.gathin.tvhome;
+
+import android.content.ComponentCallbacks2;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class MemoryTrimPolicyTest {
+ @Test
+ public void ordinaryBackgroundTransitionKeepsWarmResources() {
+ assertFalse(MemoryTrimPolicy.shouldRelease(ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN, false));
+ assertFalse(MemoryTrimPolicy.shouldRelease(ComponentCallbacks2.TRIM_MEMORY_BACKGROUND, true));
+ }
+
+ @Test
+ public void backgroundPressureReleasesResourcesOnlyWhenHomeIsStopped() {
+ assertTrue(MemoryTrimPolicy.shouldRelease(ComponentCallbacks2.TRIM_MEMORY_BACKGROUND, false));
+ assertTrue(MemoryTrimPolicy.shouldRelease(ComponentCallbacks2.TRIM_MEMORY_COMPLETE, false));
+ assertFalse(MemoryTrimPolicy.shouldRelease(ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW, true));
+ assertFalse(MemoryTrimPolicy.shouldRelease(ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL, true));
+ }
+
+ @Test
+ public void lowMemorySignalsReleaseResourcesWhenHomeIsStopped() {
+ assertTrue(MemoryTrimPolicy.shouldRelease(ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW, false));
+ assertTrue(MemoryTrimPolicy.shouldRelease(ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL, false));
+ }
+}