Skip to content

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

Launcher: let go of the tiles when the system is short of memory and Home is hidden

Riku Pirttinokkad56520fb011c
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/AppAdapter.java b/launcher/app/src/main/java/com/gathin/tvhome/AppAdapter.java
index 11693851d..0fc662f32 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/AppAdapter.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/AppAdapter.java
@@ -338,6 +338,12 @@ public class AppAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
return width <= 0 || height <= 0 || width >= height * 1.4f;
}
+ /** Lets go of every tile and its artwork (Home is in the background and memory is short). */
+ void clear() {
+ items.clear();
+ notifyDataSetChanged();
+ }
+
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
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 7597e0e60..27c44fe76 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
@@ -193,6 +193,8 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
private boolean promptsPaused;
/** Rebuild the tiles on resume: Home was fully hidden by an app, or Home was pressed. Not after a side panel such as Wi-Fi settings. */
private boolean rebuildOnResume = true;
+ /** Between onStart and onStop: Home is (at least partly) on screen. */
+ private boolean started;
/** The Inputs or Module cards dropdown while one is open, so onPause can close it. */
private PopupWindow openPopup;
/** Reorder mode is on (top-bar button). */
@@ -588,6 +590,7 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
@Override
protected void onStart() {
super.onStart();
+ started = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
registerReceiver(packageReceiver, packageFilter, Context.RECEIVER_NOT_EXPORTED);
registerReceiver(timeReceiver, timeFilter, Context.RECEIVER_NOT_EXPORTED);
@@ -607,6 +610,7 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
return;
}
promptsPaused = false;
+ if (grid.getAdapter() == null) grid.setAdapter(adapter); // dropped by onTrimMemory
if (rebuildOnResume) {
tilesReady = false;
reloadApps(true);
@@ -734,12 +738,36 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
@Override
protected void onStop() {
super.onStop();
+ started = false;
unregisterReceiver(packageReceiver);
unregisterReceiver(timeReceiver);
handler.removeCallbacks(clockCheck);
rebuildOnResume = true;
}
+ /**
+ * Another app needs the memory while Home is in the background: let go of every tile's artwork, and
+ * rebuild it when Home comes back. Not at UI_HIDDEN, the ordinary trip to an app, where keeping the
+ * tiles is what makes the return instant.
+ */
+ @Override
+ public void onTrimMemory(int level) {
+ 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;
+ 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.
+ grid.setAdapter(null);
+ grid.getRecycledViewPool().clear();
+ heroArtCache.clear();
+ heroArtKey = null;
+ rebuildOnResume = true;
+ }
+
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);