Skip to content

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

launcher: back to Home from an app without rebuilding the tiles unless apps changed

sim752df31197bd
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 e89977e15..1f3d55953 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
@@ -12,6 +12,7 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.ResolveInfo;
+import android.content.pm.ChangedPackages;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
@@ -203,12 +204,14 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
private Dialog cardPrompt;
/** Set when a decision could not be saved: no more prompts until the next resume, instead of asking in a loop. */
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 onStop and onStart: Home was fully covered by an app, not just by the translucent quick panel. */
private boolean stopped;
/** Between onStart and onStop: Home is (at least partly) on screen. */
private boolean started;
+ /** Rebuild the tiles on the way back to Home only when apps changed meanwhile (TileRefresh). */
+ private final TileRefresh tileRefresh = new TileRefresh();
+ /** PackageManager's change counter at the last rebuild, to catch a package broadcast that never arrived. */
+ private int packageSequence;
/** 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). */
@@ -274,6 +277,8 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
// Updates send REMOVED followed by ADDED; keep the tile until the new APK is ready.
if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())
&& intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) return;
+ // In the background this only marks the tiles stale; onResume rebuilds them once.
+ if (!tileRefresh.onPackagesChanged()) return;
reloadApps(false);
if (appLibrary != null) appLibrary.refresh();
}
@@ -581,6 +586,13 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
packageFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
packageFilter.addDataScheme("package");
timeFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
+ // Registered for the activity's whole life, not only while it is started: an app installed or updated while
+ // another app is in front must still mark the tiles stale, or Home would have to rebuild on every return.
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
+ registerReceiver(packageReceiver, packageFilter, Context.RECEIVER_NOT_EXPORTED);
+ } else {
+ registerReceiver(packageReceiver, packageFilter);
+ }
ConnectivityManager cm = getSystemService(ConnectivityManager.class);
TvInputManager tim = (TvInputManager) getSystemService(TV_INPUT_SERVICE);
@@ -617,7 +629,7 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
lastFocused = 0;
lastFocusedKey = adapter.getItemCount() > 0 ? adapter.keyAt(0) : null;
if (stopped) {
- rebuildOnResume = true;
+ tileRefresh.onHomeAgain();
} else {
// Still on screen (the quick panel was over it, or Home was already in front): the tiles are current,
// so only go back to the first one; rebuilding would replay the entrance animation for nothing.
@@ -631,13 +643,12 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
stopped = false;
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);
} else {
- registerReceiver(packageReceiver, packageFilter);
registerReceiver(timeReceiver, timeFilter);
}
updateClockVisibility();
+ tileRefresh.onStart();
}
@Override
@@ -651,11 +662,19 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
restartIdleClock();
promptsPaused = false;
if (grid.getAdapter() == null) grid.setAdapter(adapter); // dropped by onTrimMemory
- if (rebuildOnResume) {
- tilesReady = false;
- reloadApps(true);
+ switch (tileRefresh.onResume(packagesChangedSinceRebuild())) {
+ case REBUILD:
+ tilesReady = false;
+ reloadApps(true);
+ break;
+ case REFOCUS:
+ // The same tiles: only the focus goes back, as after a rebuild, without replaying the entrance.
+ tilesReady = false;
+ reloadApps(true, false, false);
+ break;
+ default:
+ break;
}
- rebuildOnResume = false;
updateGreeting();
updateRace();
// The network and Inputs pills keep their state while Home is in the background, and callbacks update them
@@ -691,6 +710,19 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
/** Rebuilds the tiles (and the module cards), then puts focus back where it was. */
private void reloadApps(boolean animate) {
+ reloadApps(animate, true);
+ }
+
+ /** @param rebuild false keeps the tiles as they are and only puts focus back (TileRefresh.Action.REFOCUS). */
+ private void reloadApps(boolean animate, boolean rebuild) {
+ reloadApps(animate, rebuild, true);
+ }
+
+ /**
+ * @param entrance false puts focus back as an animated reload would, without replaying the tiles' entrance:
+ * on a return to the same tiles it would only mark a change that didn't happen.
+ */
+ private void reloadApps(boolean animate, boolean rebuild, boolean entrance) {
cancelPendingFocusScroll();
RecyclerView.ViewHolder footer = grid.findViewHolderForAdapterPosition(adapter.appCount());
final boolean restoreLibrary = adapter.isLibraryAt(adapter.appCount()) && footer != null
@@ -709,8 +741,12 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
heroArt.animate().cancel();
heroArt.setAlpha(0f);
}
- reloadTiles();
- if (animate && animations && !reducedMotion) grid.scheduleLayoutAnimation();
+ if (rebuild) reloadTiles();
+ if (animate && entrance && animations && !reducedMotion) {
+ grid.scheduleLayoutAnimation();
+ // A rebuild redraws the grid anyway; without one the entrance needs a draw to start.
+ if (!rebuild) grid.invalidate();
+ }
if (restoreFocus) {
// Keep a focused footer at the end even when the app count changes.
@@ -728,6 +764,17 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
}
}
+ /** True when PackageManager reports apps changed since the last rebuild (one binder call). */
+ private boolean packagesChangedSinceRebuild() {
+ ChangedPackages changed = getPackageManager().getChangedPackages(packageSequence);
+ return changed != null && !changed.getPackageNames().isEmpty();
+ }
+
+ private int currentPackageSequence(int since) {
+ ChangedPackages changed = getPackageManager().getChangedPackages(since);
+ return changed != null ? changed.getSequenceNumber() : since;
+ }
+
/**
* Rebuilds the tiles; the module cards the owner chose to show stand in for their app's tile.
* Every step into the card code runs under CardFeature: if it fails, the tiles are the ordinary
@@ -750,6 +797,7 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
adapter.reload(cards.isEmpty() ? none : CardFeature.run(() -> Cards.shown(cards), none), leanback);
lastFocusedKey = returnKey;
lastFocused = returnSlot;
+ packageSequence = currentPackageSequence(packageSequence);
}
/**
@@ -805,11 +853,10 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
protected void onStop() {
super.onStop();
started = false;
- unregisterReceiver(packageReceiver);
unregisterReceiver(timeReceiver);
handler.removeCallbacks(clockCheck);
stopped = true;
- rebuildOnResume = true;
+ tileRefresh.onStop();
}
/**
@@ -832,7 +879,7 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
grid.getRecycledViewPool().clear();
heroArtCache.clear();
heroArtKey = null;
- rebuildOnResume = true;
+ tileRefresh.invalidate();
}
@Override
@@ -847,6 +894,7 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
@Override
protected void onDestroy() {
super.onDestroy();
+ unregisterReceiver(packageReceiver);
wallpaperPlayer.close();
ConnectivityManager cm = getSystemService(ConnectivityManager.class);
TvInputManager tim = (TvInputManager) getSystemService(TV_INPUT_SERVICE);
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/TileRefresh.java b/launcher/app/src/main/java/com/gathin/tvhome/TileRefresh.java
new file mode 100644
index 000000000..78860940d
--- /dev/null
+++ b/launcher/app/src/main/java/com/gathin/tvhome/TileRefresh.java
@@ -0,0 +1,55 @@
+package com.gathin.tvhome;
+
+/**
+ * Whether Home rebuilds its tiles when it comes back to the front. A rebuild asks PackageManager for every app again
+ * and reloads every banner, icon and label (about 30 ms on a TCL C755, on every Home press), so it happens only when
+ * an app was installed, removed, updated, enabled or disabled in the meantime. Otherwise Home only puts the focus
+ * back, as before.
+ */
+final class TileRefresh {
+
+ enum Action { NOTHING, REFOCUS, REBUILD }
+
+ private boolean started;
+ /** The tiles may not match the installed apps: true until the first build. */
+ private boolean stale = true;
+ private boolean refocus;
+
+ void onStart() {
+ started = true;
+ }
+
+ /** Another app took the screen: its tiles stay, and focus goes back to them on the way in. */
+ void onStop() {
+ started = false;
+ refocus = true;
+ }
+
+ /** Home pressed while Home is in front: back to the first tile. */
+ void onHomeAgain() {
+ refocus = true;
+ }
+
+ /** The tiles were dropped (onTrimMemory): build them again on the way back. */
+ void invalidate() {
+ stale = true;
+ }
+
+ /**
+ * A package broadcast. True when Home is on screen and should rebuild now; while it is in the background the
+ * rebuild waits for {@link #onResume}, so a run of app updates costs one rebuild instead of one each.
+ */
+ boolean onPackagesChanged() {
+ if (started) return true;
+ stale = true;
+ return false;
+ }
+
+ /** @param changedMeanwhile PackageManager reports changed packages since the last build (a missed broadcast). */
+ Action onResume(boolean changedMeanwhile) {
+ Action action = stale || changedMeanwhile ? Action.REBUILD : refocus ? Action.REFOCUS : Action.NOTHING;
+ stale = false;
+ refocus = false;
+ return action;
+ }
+}
diff --git a/launcher/app/src/test/java/com/gathin/tvhome/TileRefreshTest.java b/launcher/app/src/test/java/com/gathin/tvhome/TileRefreshTest.java
new file mode 100644
index 000000000..2daa27f21
--- /dev/null
+++ b/launcher/app/src/test/java/com/gathin/tvhome/TileRefreshTest.java
@@ -0,0 +1,93 @@
+package com.gathin.tvhome;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+/** When Home rebuilds its tiles and when it only puts the focus back. */
+public class TileRefreshTest {
+
+ @Test
+ public void firstResumeBuildsTheTiles() {
+ TileRefresh r = new TileRefresh();
+ r.onStart();
+ assertEquals(TileRefresh.Action.REBUILD, r.onResume(false));
+ }
+
+ @Test
+ public void backFromAnAppWithNothingChangedOnlyRefocuses() {
+ TileRefresh r = shown();
+ r.onStop();
+ r.onStart();
+ assertEquals(TileRefresh.Action.REFOCUS, r.onResume(false));
+ }
+
+ @Test
+ public void anAppChangedInTheBackgroundRebuildsOnceOnReturn() {
+ TileRefresh r = shown();
+ r.onStop();
+ assertFalse(r.onPackagesChanged());
+ assertFalse(r.onPackagesChanged());
+ r.onStart();
+ assertEquals(TileRefresh.Action.REBUILD, r.onResume(false));
+ r.onStop();
+ r.onStart();
+ assertEquals(TileRefresh.Action.REFOCUS, r.onResume(false));
+ }
+
+ @Test
+ public void aMissedBroadcastStillRebuilds() {
+ TileRefresh r = shown();
+ r.onStop();
+ r.onStart();
+ assertEquals(TileRefresh.Action.REBUILD, r.onResume(true));
+ }
+
+ @Test
+ public void anAppChangedWhileHomeIsShownRebuildsAtOnce() {
+ TileRefresh r = shown();
+ assertTrue(r.onPackagesChanged());
+ assertEquals(TileRefresh.Action.NOTHING, r.onResume(false));
+ }
+
+ @Test
+ public void homeOnHomeGoesBackToTheFirstTileWithoutRebuilding() {
+ TileRefresh r = shown();
+ r.onHomeAgain();
+ assertEquals(TileRefresh.Action.REFOCUS, r.onResume(false));
+ }
+
+ @Test
+ public void homePressedInAnAppRefocusesWithoutRebuilding() {
+ // onNewIntent calls onHomeAgain only while Home is stopped; on screen (#552) it just focuses the first tile.
+ TileRefresh r = shown();
+ r.onStop();
+ r.onHomeAgain();
+ r.onStart();
+ assertEquals(TileRefresh.Action.REFOCUS, r.onResume(false));
+ }
+
+ @Test
+ public void tilesDroppedForMemoryAreRebuiltOnReturn() {
+ TileRefresh r = shown();
+ r.onStop();
+ r.invalidate();
+ r.onStart();
+ assertEquals(TileRefresh.Action.REBUILD, r.onResume(false));
+ }
+
+ @Test
+ public void pauseWithoutStopChangesNothing() {
+ TileRefresh r = shown();
+ assertEquals(TileRefresh.Action.NOTHING, r.onResume(false));
+ }
+
+ private static TileRefresh shown() {
+ TileRefresh r = new TileRefresh();
+ r.onStart();
+ r.onResume(false);
+ return r;
+ }
+}