Skip to content

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

launcher: app library shows even when a package change replaces its load

Cristian Rojas8a569b8ba6ce
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/AppLibraryDialog.java b/launcher/app/src/main/java/com/gathin/tvhome/AppLibraryDialog.java
index b49a65391..ff2dc67a9 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/AppLibraryDialog.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/AppLibraryDialog.java
@@ -74,8 +74,8 @@ final class AppLibraryDialog {
private final Set<String> iconsLoading = new HashSet<>();
private final Runnable dismissed;
private boolean closed;
- /** Only the latest load fills the catalogue. */
- private int loads;
+ /** Only the latest load fills the catalogue, and it shows the popup if an opening is waiting. */
+ private final LibraryLoads loads = new LibraryLoads();
private final Activity activity;
private final Predicate<String> onHome;
private final Runnable changed;
@@ -184,7 +184,7 @@ final class AppLibraryDialog {
}
void show() {
- load(this::reveal);
+ load(true);
}
private void reveal() {
@@ -205,13 +205,16 @@ final class AppLibraryDialog {
private final java.util.Set<String> tvPackages = new java.util.HashSet<>();
void refresh() {
- load(null);
+ load(false);
}
- /** Reads the catalogue on LOADER, then fills it on the UI thread and runs {@code then}. */
- private void load(Runnable then) {
+ /**
+ * Reads the catalogue on LOADER, then fills it on the UI thread. reveal: from opening the library; the popup
+ * appears once the latest load has filled it, even when a refresh replaced the opening's own load.
+ */
+ private void load(boolean reveal) {
dismissActions();
- int load = ++loads;
+ int load = loads.start(reveal);
PackageManager pm = activity.getPackageManager();
String self = activity.getPackageName();
LOADER.execute(() -> {
@@ -247,7 +250,7 @@ final class AppLibraryDialog {
}
KNOWN.keySet().retainAll(unique.keySet());
main.post(() -> {
- if (closed || load != loads) return;
+ if (closed || !loads.isLatest(load)) return;
tvPackages.clear();
tvPackages.addAll(tv);
installTimes.clear();
@@ -255,7 +258,7 @@ final class AppLibraryDialog {
labels.clear();
labels.putAll(names);
fill(new ArrayList<>(unique.values()));
- if (then != null) then.run();
+ if (loads.takeReveal()) reveal();
});
});
}
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/LibraryLoads.java b/launcher/app/src/main/java/com/gathin/tvhome/LibraryLoads.java
new file mode 100644
index 000000000..2ada3b238
--- /dev/null
+++ b/launcher/app/src/main/java/com/gathin/tvhome/LibraryLoads.java
@@ -0,0 +1,30 @@
+package com.gathin.tvhome;
+
+/**
+ * Which catalogue load of the app library fills it, and when the popup appears. Opening the library starts a load
+ * and so does a package change (install, update, removal) while it is open or still loading; only the latest load
+ * fills the catalogue, so the reveal an opening asked for belongs to whichever load ends up the latest, not to the
+ * one the opening started. Without that a package change in the middle of an opening left the popup unshown.
+ */
+final class LibraryLoads {
+ private int latest;
+ private boolean revealPending;
+
+ /** Starts a load, from opening the library when reveal is true. Returns its number. */
+ int start(boolean reveal) {
+ if (reveal) revealPending = true;
+ return ++latest;
+ }
+
+ /** Whether a finished load is the latest one, the only one that fills the catalogue. */
+ boolean isLatest(int load) {
+ return load == latest;
+ }
+
+ /** Once the latest load has filled the catalogue: whether the popup still has to appear. True once per opening. */
+ boolean takeReveal() {
+ boolean reveal = revealPending;
+ revealPending = false;
+ return reveal;
+ }
+}
diff --git a/launcher/app/src/test/java/com/gathin/tvhome/LibraryLoadsTest.java b/launcher/app/src/test/java/com/gathin/tvhome/LibraryLoadsTest.java
new file mode 100644
index 000000000..0aed5a2af
--- /dev/null
+++ b/launcher/app/src/test/java/com/gathin/tvhome/LibraryLoadsTest.java
@@ -0,0 +1,39 @@
+package com.gathin.tvhome;
+
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class LibraryLoadsTest {
+ @Test public void aRefreshDuringTheOpeningStillRevealsOnce() {
+ LibraryLoads loads = new LibraryLoads();
+ int opening = loads.start(true);
+ int refresh = loads.start(false); // a package changed before the opening's result came back
+ assertFalse(loads.isLatest(opening));
+ assertTrue(loads.isLatest(refresh));
+ assertTrue(loads.takeReveal());
+ assertFalse(loads.takeReveal());
+ }
+
+ @Test public void aRefreshOnItsOwnNeverReveals() {
+ LibraryLoads loads = new LibraryLoads();
+ assertTrue(loads.isLatest(loads.start(false)));
+ assertFalse(loads.takeReveal());
+ }
+
+ @Test public void anOpeningAfterARefreshRevealsWithItsOwnLoad() {
+ LibraryLoads loads = new LibraryLoads();
+ int refresh = loads.start(false);
+ int opening = loads.start(true);
+ assertFalse(loads.isLatest(refresh));
+ assertTrue(loads.isLatest(opening));
+ assertTrue(loads.takeReveal());
+ }
+
+ @Test public void refreshesAfterTheRevealDoNotRevealAgain() {
+ LibraryLoads loads = new LibraryLoads();
+ loads.start(true);
+ assertTrue(loads.takeReveal());
+ loads.start(false);
+ assertFalse(loads.takeReveal());
+ }
+}