Skip to content

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

Launcher: an app moved into the dock on the TV stays there with fill_dock false (#499)

John5edc52f27140
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 5b9d07153..ded8863f8 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/AppAdapter.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/AppAdapter.java
@@ -167,6 +167,8 @@ public class AppAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final boolean dockTitle;
/** The owner's saved order (reorder button, Order): key -> rank. Empty when there is none or it no longer applies. */
private Map<String, Integer> savedRanks = Collections.emptyMap();
+ /** Dock size recorded with the saved order (Order.docked), or -1: then dock_apps says which tiles are docked. */
+ private int savedDocked = -1;
/** Reorder mode: no empty dock slots, so every position holds a tile that can move. */
private boolean reordering;
private final TileListener listener;
@@ -293,7 +295,7 @@ public class AppAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
ctx.getDrawable(R.drawable.ic_home_settings), null, 0));
}
docked = 0;
- while (docked < fresh.size() && favourites.contains(fresh.get(docked).pkg)) docked++;
+ while (docked < fresh.size() && isDocked(fresh.get(docked))) docked++;
emptySlots = dockRow && !reordering ? DockSlots.empty(docked, dockSize, fresh.size(), fillDock) : 0;
for (int i = 0; i < emptySlots; i++) {
fresh.add(docked + i, new Entry(Entry.EMPTY, "", new ComponentName(ctx, "empty-dock-slot-" + i),
@@ -481,9 +483,20 @@ public class AppAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
return isLibraryAt(position) ? Long.MIN_VALUE : items.get(position).component.flattenToShortString().hashCode();
}
- /** Uses this saved order from the next reload() (Order.ranks). */
- void setSavedOrder(Map<String, Integer> ranks) {
+ /** Uses this saved order and dock size from the next reload() (Order.ranks, Order.docked). */
+ void setSavedOrder(Map<String, Integer> ranks, int docked) {
savedRanks = ranks;
+ savedDocked = docked;
+ }
+
+ /**
+ * A tile in the dock: one of the first savedDocked tiles of the saved order when one was recorded, so an app
+ * the owner moved into the dock stays there (#499); else a dock_apps entry.
+ */
+ private boolean isDocked(Entry e) {
+ if (savedDocked < 0) return favourites.contains(e.pkg);
+ Integer rank = savedRanks.get(e.component.flattenToShortString());
+ return rank != null && rank < savedDocked;
}
/** Reorder mode on or off; takes effect at the next reload() (empty dock slots are left out while on). */
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/DockSlots.java b/launcher/app/src/main/java/com/gathin/tvhome/DockSlots.java
index a9f9f0ad7..ab92c352f 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/DockSlots.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/DockSlots.java
@@ -1,8 +1,13 @@
package com.gathin.tvhome;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
/**
- * The dock is the first row of the grid. dock_apps decides what is in it: a slot it leaves free stays
- * empty instead of taking the first grid app (#162) when fill_dock is false; by default they fill.
+ * The dock is the first row of the grid. dock_apps decides what is in it, or the saved order once the owner
+ * changed the dock on the TV: a slot left free stays empty instead of taking the first grid app (#162) when
+ * fill_dock is false; by default they fill.
*/
final class DockSlots {
private DockSlots() { }
@@ -16,6 +21,30 @@ final class DockSlots {
return dockSize - docked;
}
+ /**
+ * The order a reorder session saves when fill_dock is false (#499). Reorder mode shows no empty slots, so a
+ * grid app slides into a free dock slot without being put there. Only the keys in {@code dock} (in the dock
+ * when the session started, or carried into it) that end inside the first {@code dockSize} places stay in the
+ * dock: they come first, in their order, and every other key follows in its order. The dock size to save is
+ * the number of keys in {@code dock} found there.
+ */
+ static List<String> dockFirst(List<String> keys, int dockSize, Set<String> dock) {
+ List<String> head = new ArrayList<>(), tail = new ArrayList<>();
+ for (int i = 0; i < keys.size(); i++) {
+ String k = keys.get(i);
+ (i < dockSize && dock.contains(k) ? head : tail).add(k);
+ }
+ head.addAll(tail);
+ return head;
+ }
+
+ /** How many of the first {@code dockSize} keys are in {@code dock}: the dock size dockFirst puts first. */
+ static int dockCount(List<String> keys, int dockSize, Set<String> dock) {
+ int n = 0;
+ for (int i = 0; i < Math.min(dockSize, keys.size()); i++) if (dock.contains(keys.get(i))) n++;
+ return n;
+ }
+
/** A position focus can land on: an empty slot sends it to the last dock app. */
static int focusable(int position, int docked, int empty) {
return empty > 0 && position >= docked && position < docked + empty ? docked - 1 : position;
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 c38133eec..441f48237 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
@@ -195,6 +195,13 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
private String movingKey;
/** Tile order when reorder mode started, to save only when something moved. */
private List<String> orderAtStart;
+ /**
+ * fill_dock false: tiles in the dock when reorder mode started, plus those carried or swapped into it since
+ * (#499). A grid app that only slid into a free slot, which reorder mode does not show, is not one of them.
+ */
+ private Set<String> dockInSession;
+ /** fill_dock false: how many dock slots held an app when reorder mode started. */
+ private int dockedAtStart;
private View btnReorder;
/** dock_apps as loaded (config.xml or a theme): a saved Order only applies while these are unchanged. */
private List<String> dockApps;
@@ -403,7 +410,8 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
adapter = new AppAdapter(this, this, COLUMNS, cfg);
dockApps = cfg.dockApps;
fillDockLeavesSlots = !cfg.fillDock;
- adapter.setSavedOrder(Order.ranks(Order.load(Order.file(this)), dockApps));
+ Order savedOrder = Order.load(Order.file(this));
+ adapter.setSavedOrder(Order.ranks(savedOrder, dockApps), Order.docked(savedOrder, dockApps));
grid.setAdapter(adapter);
// tvOS layout: push the dock row towards the bottom of the screen, leaving room for
@@ -828,6 +836,10 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
/** The start shared by reorder and edit-dock mode. False when there is nothing to move. */
private boolean beginReorder() {
if (adapter.movableCount() < 2) return false;
+ // Before the reload below: once it has dropped the empty slots, the dock row also shows grid apps.
+ dockInSession = new HashSet<>();
+ dockedAtStart = adapter.dockFilled();
+ for (int i = 0; i < Math.min(dockedAtStart, adapter.movableCount()); i++) dockInSession.add(adapter.keyAt(i));
adapter.setReordering(true);
if (adapter.hasEmptySlots()) reloadTiles(); // every position must hold a tile that can move
reordering = true;
@@ -899,6 +911,8 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
adapter.move(pos, slot);
adapter.move(adapter.positionOf(slotKey), pos);
});
+ dockInSession.add(adapter.keyAt(slot));
+ if (pos >= COLUMNS) dockInSession.remove(slotKey);
}
endReorder();
refocusAfterLayout(slot);
@@ -946,10 +960,18 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
List<String> keys = adapter.movableKeys();
List<String> before = orderAtStart;
orderAtStart = null;
- if (!keys.equals(before)) {
- Order order = new Order(Order.dockKey(dockApps), keys);
+ // fill_dock false: the dock is what the owner put there, saved with its size, so the empty slots that
+ // come back after the mode go after it (#499). With fill_dock true every dock slot holds an app anyway.
+ int docked = -1;
+ if (fillDockLeavesSlots) {
+ docked = DockSlots.dockCount(keys, COLUMNS, dockInSession);
+ keys = DockSlots.dockFirst(keys, COLUMNS, dockInSession);
+ }
+ dockInSession = null;
+ if (!keys.equals(before) || (fillDockLeavesSlots && docked != dockedAtStart)) {
+ Order order = new Order(Order.dockKey(dockApps), keys, docked);
if (Order.save(Order.file(this), order)) {
- adapter.setSavedOrder(Order.ranks(order, dockApps));
+ adapter.setSavedOrder(Order.ranks(order, dockApps), Order.docked(order, dockApps));
} else {
Toast.makeText(this, R.string.reorder_not_saved, Toast.LENGTH_SHORT).show();
}
@@ -977,6 +999,9 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
else to = from / COLUMNS < (n - 1) / COLUMNS ? n - 1 : from;
if (to < 0 || to >= n || to == from) return;
keepScroll(() -> adapter.move(from, to));
+ // The owner carries this tile: where it lands decides whether it is in the dock, not dock_apps (#499).
+ if (to < COLUMNS) dockInSession.add(movingKey);
+ else dockInSession.remove(movingKey);
refocusAfterLayout(to);
}
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/Order.java b/launcher/app/src/main/java/com/gathin/tvhome/Order.java
index 639d13736..40819aa3a 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/Order.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/Order.java
@@ -22,8 +22,11 @@ import java.util.Map;
* dock_apps changes (a rebuild or a pushed theme), the saved order is ignored, so the new dock takes effect until
* the owner reorders again.
*
- * Format, UTF-8 text: {@code tvhome-order 1}, then {@code dock <comma-separated dock_apps>}, then one tile key
- * (component, flattened short) per line.
+ * Format, UTF-8 text: {@code tvhome-order 1}, then {@code dock <comma-separated dock_apps>}, then optionally
+ * {@code docked <n>}, then one tile key (component, flattened short) per line. {@code docked} says how many of the
+ * first keys are in the dock, for fill_dock false, where an app the owner moved into the dock is not in dock_apps
+ * (#499). Files without it (older launchers) leave the dock to dock_apps; an older launcher reads the line as a key
+ * that matches no tile.
*/
final class Order {
@@ -31,14 +34,22 @@ final class Order {
static final String FILE_NAME = "order.txt";
private static final String MAGIC = "tvhome-order 1";
private static final String DOCK = "dock ";
+ private static final String DOCKED = "docked ";
/** dock_apps the order was saved under, comma-joined (package names cannot contain commas). */
final String dock;
final List<String> keys;
+ /** How many of the first keys are in the dock, or -1 when not recorded (dock_apps decides). */
+ final int docked;
Order(String dock, List<String> keys) {
+ this(dock, keys, -1);
+ }
+
+ Order(String dock, List<String> keys, int docked) {
this.dock = dock;
this.keys = Collections.unmodifiableList(new ArrayList<>(keys));
+ this.docked = docked;
}
static String dockKey(List<String> dockApps) {
@@ -47,6 +58,7 @@ final class Order {
static String encode(Order order) {
StringBuilder sb = new StringBuilder(MAGIC).append('\n').append(DOCK).append(order.dock).append('\n');
+ if (order.docked >= 0) sb.append(DOCKED).append(order.docked).append('\n');
for (String k : order.keys) sb.append(k).append('\n');
return sb.toString();
}
@@ -56,8 +68,18 @@ final class Order {
if (text == null) return null;
String[] lines = text.split("\n", -1);
if (lines.length < 2 || !lines[0].equals(MAGIC) || !lines[1].startsWith(DOCK)) return null;
+ int docked = -1, first = 2;
+ if (lines.length > 2 && lines[2].startsWith(DOCKED)) {
+ try {
+ docked = Integer.parseInt(lines[2].substring(DOCKED.length()));
+ } catch (NumberFormatException e) {
+ return null;
+ }
+ if (docked < 0) return null;
+ first = 3;
+ }
List<String> keys = new ArrayList<>();
- for (int i = 2; i < lines.length; i++) {
+ for (int i = first; i < lines.length; i++) {
String k = lines[i];
if (k.isEmpty()) {
if (i == lines.length - 1) break; // the trailing newline
@@ -66,7 +88,8 @@ final class Order {
if (keys.contains(k)) return null;
keys.add(k);
}
- return new Order(lines[1].substring(DOCK.length()), keys);
+ if (docked > keys.size()) return null;
+ return new Order(lines[1].substring(DOCK.length()), keys, docked);
}
/**
@@ -80,6 +103,11 @@ final class Order {
return r;
}
+ /** The saved dock size when the order still applies to these dock_apps and recorded one, else -1. */
+ static int docked(Order order, List<String> dockApps) {
+ return order == null || !order.dock.equals(dockKey(dockApps)) ? -1 : order.docked;
+ }
+
/** Sort step for two tiles: saved ones first in saved order, then 0 to fall through to the usual rule. */
static int compare(Map<String, Integer> ranks, String keyA, String keyB) {
Integer a = ranks.get(keyA), b = ranks.get(keyB);
diff --git a/launcher/app/src/test/java/com/gathin/tvhome/DockSlotsTest.java b/launcher/app/src/test/java/com/gathin/tvhome/DockSlotsTest.java
index 411b364bd..8aad7ba4c 100644
--- a/launcher/app/src/test/java/com/gathin/tvhome/DockSlotsTest.java
+++ b/launcher/app/src/test/java/com/gathin/tvhome/DockSlotsTest.java
@@ -1,9 +1,58 @@
package com.gathin.tvhome;
import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
import static org.junit.Assert.assertEquals;
public class DockSlotsTest {
+ private static Set<String> set(String... keys) {
+ return new HashSet<>(Arrays.asList(keys));
+ }
+
+ @Test public void anAppCarriedIntoTheDockStaysThere() {
+ // #499: Apple TV (not in dock_apps) carried into the 4th slot, YouTube carried out into the grid.
+ List<String> keys = Arrays.asList("tivimate", "jellyfin", "iplayer", "appletv", "my5", "youtube");
+ Set<String> dock = set("tivimate", "jellyfin", "iplayer", "appletv");
+ assertEquals(keys, DockSlots.dockFirst(keys, 4, dock));
+ assertEquals(4, DockSlots.dockCount(keys, 4, dock));
+ }
+
+ @Test public void aGridAppThatOnlySlidIntoAFreeSlotGoesBackToTheGrid() {
+ // #162: three dock apps and a free slot. Reorder mode shows no empty slot, so "my5" sits in the 4th place
+ // without the owner putting it there; only a grid tile moved.
+ List<String> keys = Arrays.asList("tivimate", "jellyfin", "iplayer", "my5", "vlc", "appletv");
+ Set<String> dock = set("tivimate", "jellyfin", "iplayer");
+ assertEquals(keys, DockSlots.dockFirst(keys, 4, dock));
+ assertEquals(3, DockSlots.dockCount(keys, 4, dock));
+ }
+
+ @Test public void aSlidInGridAppAheadOfADockAppMovesBehindTheDock() {
+ // "tivimate" carried from the 1st place to the 4th: "my5" slid in at the 3rd and must not split the dock.
+ List<String> keys = Arrays.asList("jellyfin", "iplayer", "my5", "tivimate", "vlc");
+ Set<String> dock = set("tivimate", "jellyfin", "iplayer");
+ assertEquals(Arrays.asList("jellyfin", "iplayer", "tivimate", "my5", "vlc"), DockSlots.dockFirst(keys, 4, dock));
+ assertEquals(3, DockSlots.dockCount(keys, 4, dock));
+ }
+
+ @Test public void aDockAppPushedPastTheDockLeavesIt() {
+ // An app carried into the 1st place pushes the 4th dock app into the grid.
+ List<String> keys = Arrays.asList("appletv", "tivimate", "jellyfin", "iplayer", "youtube");
+ Set<String> dock = set("appletv", "tivimate", "jellyfin", "iplayer", "youtube");
+ assertEquals(keys, DockSlots.dockFirst(keys, 4, dock));
+ assertEquals(4, DockSlots.dockCount(keys, 4, dock));
+ }
+
+ @Test public void fewerTilesThanDockSlotsStillPutTheDockFirst() {
+ List<String> keys = Arrays.asList("a", "b");
+ assertEquals(Arrays.asList("b", "a"), DockSlots.dockFirst(keys, 4, set("b")));
+ assertEquals(1, DockSlots.dockCount(keys, 4, set("b")));
+ }
+
@Test public void threeDockAppsInFourColumnsLeaveOneSlotEmpty() {
assertEquals(1, DockSlots.empty(3, 4, 20, false));
}
diff --git a/launcher/app/src/test/java/com/gathin/tvhome/OrderTest.java b/launcher/app/src/test/java/com/gathin/tvhome/OrderTest.java
index 2f98da6b8..b94eb8ed2 100644
--- a/launcher/app/src/test/java/com/gathin/tvhome/OrderTest.java
+++ b/launcher/app/src/test/java/com/gathin/tvhome/OrderTest.java
@@ -25,6 +25,40 @@ public class OrderTest {
assertEquals(o.keys, back.keys);
}
+ @Test
+ public void dockedRoundTrips() {
+ Order o = new Order(Order.dockKey(DOCK), Arrays.asList("b/.B", "a/.A", "c/.C"), 2);
+ String text = Order.encode(o);
+ assertEquals("tvhome-order 1\ndock com.plexapp.android,com.netflix.ninja\ndocked 2\nb/.B\na/.A\nc/.C\n", text);
+ Order back = Order.decode(text);
+ assertEquals(2, back.docked);
+ assertEquals(o.keys, back.keys);
+ }
+
+ @Test
+ public void anOrderWithoutDockedLeavesTheDockToDockApps() {
+ Order back = Order.decode("tvhome-order 1\ndock x\na/.A\nb/.B\n");
+ assertEquals(-1, back.docked);
+ assertEquals(Arrays.asList("a/.A", "b/.B"), back.keys);
+ assertEquals(-1, Order.docked(back, Collections.singletonList("x")));
+ assertTrue(Order.encode(new Order("x", Arrays.asList("a/.A"))).indexOf("docked") < 0);
+ }
+
+ @Test
+ public void dockedFollowsTheDockAppsCheck() {
+ Order o = new Order(Order.dockKey(DOCK), Arrays.asList("b/.B", "a/.A"), 1);
+ assertEquals(1, Order.docked(o, DOCK));
+ assertEquals(-1, Order.docked(o, Collections.singletonList("com.plexapp.android")));
+ assertEquals(-1, Order.docked(null, DOCK));
+ }
+
+ @Test
+ public void rejectsABadDockedLine() {
+ assertNull(Order.decode("tvhome-order 1\ndock x\ndocked two\na/.A\n"));
+ assertNull(Order.decode("tvhome-order 1\ndock x\ndocked -1\na/.A\n"));
+ assertNull(Order.decode("tvhome-order 1\ndock x\ndocked 3\na/.A\n")); // more docked than keys
+ }
+
@Test
public void emptyOrderRoundTrips() {
Order back = Order.decode(Order.encode(new Order("", Collections.emptyList())));