Skip to content

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

launcher: reorder apps on the TV from a top-bar button

Samuel Hinton33ca9770b968
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 1c351396f..703943ef9 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/AppAdapter.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/AppAdapter.java
@@ -110,6 +110,10 @@ public class AppAdapter extends RecyclerView.Adapter<AppAdapter.Holder> {
}
private final Context ctx;
+ /** 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();
+ /** Reorder mode: no empty dock slots, so every position holds a tile that can move. */
+ private boolean reordering;
private final TileListener listener;
private final int dockSize;
/** Dock order (config.xml dock_apps, or a theme's config.json). */
@@ -172,6 +176,9 @@ public class AppAdapter extends RecyclerView.Adapter<AppAdapter.Holder> {
}
}
Collections.sort(fresh, (a, b) -> {
+ // The owner's own order first; tiles it does not know (new installs) follow by the rule below.
+ int saved = Order.compare(savedRanks, a.component.flattenToShortString(), b.component.flattenToShortString());
+ if (saved != 0) return saved;
int fa = favourites.indexOf(a.pkg), fb = favourites.indexOf(b.pkg);
if (fa >= 0 || fb >= 0) {
if (fa < 0) return 1;
@@ -192,7 +199,7 @@ public class AppAdapter extends RecyclerView.Adapter<AppAdapter.Holder> {
}
docked = 0;
while (docked < fresh.size() && favourites.contains(fresh.get(docked).pkg)) docked++;
- emptySlots = DockSlots.empty(docked, dockSize, fresh.size(), fillDock);
+ emptySlots = reordering ? 0 : DockSlots.empty(docked, dockSize, fresh.size(), fillDock);
for (int i = 0; i < emptySlots; i++) {
fresh.add(docked + i, new Entry(Entry.EMPTY, "", new ComponentName(ctx, "empty-dock-slot-" + i),
null, null, null, 0));
@@ -320,6 +327,48 @@ public class AppAdapter extends RecyclerView.Adapter<AppAdapter.Holder> {
return items.get(position).component.flattenToShortString().hashCode();
}
+ /** Uses this saved order from the next reload() (Order.ranks). */
+ void setSavedOrder(Map<String, Integer> ranks) {
+ savedRanks = ranks;
+ }
+
+ /** Reorder mode on or off; takes effect at the next reload() (empty dock slots are left out while on). */
+ void setReordering(boolean on) {
+ reordering = on;
+ }
+
+ boolean hasEmptySlots() {
+ return emptySlots > 0;
+ }
+
+ /** Tiles that can be moved: everything but the "Default home" tile, which always stays last. */
+ int movableCount() {
+ int n = items.size();
+ return n > 0 && items.get(n - 1).kind == Entry.HOME_SETTINGS ? n - 1 : n;
+ }
+
+ /** Keys of the movable tiles in their current order, for Order (empty dock slots are not tiles). */
+ List<String> movableKeys() {
+ List<String> keys = new ArrayList<>();
+ for (int i = 0; i < movableCount(); i++) {
+ Entry e = items.get(i);
+ if (e.kind != Entry.EMPTY) keys.add(e.component.flattenToShortString());
+ }
+ return keys;
+ }
+
+ /** Moves one tile; the others shift by one. Stable ids keep the moved tile's view (and its focus). */
+ void move(int from, int to) {
+ if (from == to || from < 0 || to < 0 || from >= movableCount() || to >= movableCount()) return;
+ items.add(to, items.remove(from));
+ notifyItemMoved(from, to);
+ // The dock row hides names by position: rebind the tiles whose side of the dock edge changed.
+ if (from < dockSize || to < dockSize) {
+ int lo = Math.min(from, to), hi = Math.max(from, to);
+ notifyItemRangeChanged(lo, hi - lo + 1);
+ }
+ }
+
/** Stable key for the app at a position (its component), or null. */
String keyAt(int position) {
return position >= 0 && position < items.size() ? items.get(position).component.flattenToShortString() : null;
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 d7c86c17d..4f4fb93eb 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
@@ -147,6 +147,17 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
private boolean rebuildOnResume = true;
/** 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). */
+ private boolean reordering;
+ /** Key of the tile being carried in reorder mode, or null. */
+ private String movingKey;
+ /** Tile order when reorder mode started, to save only when something moved. */
+ private List<String> orderAtStart;
+ 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;
+ /** fill_dock false: the dock can have empty slots, which reorder mode leaves out and restores afterwards. */
+ private boolean fillDockLeavesSlots;
private final Handler handler = new Handler(Looper.getMainLooper());
/** Refreshes the network pill when the connection changes: at boot Wi-Fi often connects after Home is shown. */
@@ -297,6 +308,9 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
AnimationUtils.loadAnimation(this, enterAnim), enterDelay));
}
adapter = new AppAdapter(this, this, COLUMNS, cfg);
+ dockApps = cfg.dockApps;
+ fillDockLeavesSlots = !cfg.fillDock;
+ adapter.setSavedOrder(Order.ranks(Order.load(Order.file(this)), dockApps));
grid.setAdapter(adapter);
// tvOS layout: push the dock row towards the bottom of the screen, leaving room for
@@ -360,6 +374,8 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
return true;
});
btnSettings.setOnClickListener(v -> open(systemIntent(Settings.ACTION_SETTINGS)));
+ btnReorder = findViewById(R.id.btnReorder);
+ btnReorder.setOnClickListener(v -> toggleReorder());
btnSettings.setOnLongClickListener(v -> {
showSettingsMenu();
return true;
@@ -385,6 +401,7 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
btnInputs.setOnKeyListener(pillKeys);
btnWifi.setOnKeyListener(pillKeys);
btnSettings.setOnKeyListener(pillKeys);
+ btnReorder.setOnKeyListener(pillKeys);
if (advancedEffects) {
for (View button : new View[]{btnInputs, btnWifi, btnSettings}) {
button.setOnFocusChangeListener((view, focused) -> view.animate()
@@ -560,6 +577,7 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
* ones and cards stay off for this process.
*/
private void reloadTiles() {
+ if (reordering) endReorder(); // an app installed or removed while reordering: keep the order so far
// Read these before reload(): rebinding can move focus to the first tile and overwrite them.
final String returnKey = lastFocusedKey;
final int returnSlot = lastFocused;
@@ -588,6 +606,7 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
@Override
protected void onPause() {
super.onPause();
+ endReorder(); // Home or another app while reordering keeps the order so far
stopAmbientAnimation();
heroName.animate().cancel();
ambient.animate().cancel();
@@ -623,6 +642,181 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
}
}
+ // ---- reorder ---------------------------------------------------------------------------------
+
+ /** Blue the glass turns while reorder mode is on. */
+ private static final int REORDER_ACCENT = 0xFF6EC1FF;
+
+ /**
+ * Reorder mode (top-bar button). While it is on, every key comes here first:
+ * - nothing carried: arrows move focus as usual; OK on a tile picks it up instead of opening the app;
+ * Back ends the mode and saves the order.
+ * - a tile carried: arrows move it; Back (or OK) puts it down and the mode stays on for the next one.
+ * Other keys (volume, Home) pass through; leaving the launcher ends the mode and saves.
+ */
+ @Override
+ public boolean dispatchKeyEvent(KeyEvent event) {
+ if (!reordering) return super.dispatchKeyEvent(event);
+ boolean down = event.getAction() == KeyEvent.ACTION_DOWN;
+ int code = event.getKeyCode();
+ boolean ok = code == KeyEvent.KEYCODE_DPAD_CENTER || code == KeyEvent.KEYCODE_ENTER
+ || code == KeyEvent.KEYCODE_NUMPAD_ENTER;
+ boolean back = code == KeyEvent.KEYCODE_BACK || code == KeyEvent.KEYCODE_ESCAPE;
+ if (movingKey != null) {
+ switch (code) {
+ case KeyEvent.KEYCODE_DPAD_LEFT:
+ case KeyEvent.KEYCODE_DPAD_RIGHT:
+ case KeyEvent.KEYCODE_DPAD_UP:
+ case KeyEvent.KEYCODE_DPAD_DOWN:
+ if (down) moveBy(code);
+ return true;
+ default:
+ if (ok || back) {
+ if (!down) drop();
+ return true;
+ }
+ return super.dispatchKeyEvent(event);
+ }
+ }
+ if (back) {
+ if (!down) endReorder();
+ return true;
+ }
+ int pos = focusedTilePosition();
+ if (ok && pos >= 0) {
+ if (!down) pickUp(pos);
+ return true; // never opens the app while reordering
+ }
+ return super.dispatchKeyEvent(event); // arrows navigate; OK on a top-bar pill works as usual
+ }
+
+ /** Adapter position of the focused tile, or -1 when focus is not on a tile. */
+ private int focusedTilePosition() {
+ View f = getCurrentFocus();
+ if (f == null || f.getId() != R.id.card || !(f.getParent() instanceof View)) return -1;
+ return grid.getChildAdapterPosition((View) f.getParent());
+ }
+
+ /** The reorder button: turns the mode on (focus goes back to the last tile), or off again. */
+ private void toggleReorder() {
+ if (reordering) {
+ endReorder();
+ return;
+ }
+ if (adapter.movableCount() < 2) return;
+ adapter.setReordering(true);
+ if (adapter.hasEmptySlots()) reloadTiles(); // every position must hold a tile that can move
+ reordering = true;
+ orderAtStart = adapter.movableKeys();
+ glass.setAccent(REORDER_ACCENT);
+ btnReorder.setActivated(true);
+ focusTile(Math.max(0, Math.min(lastFocused, adapter.movableCount() - 1)));
+ Toast.makeText(this, R.string.reorder_hint, Toast.LENGTH_LONG).show();
+ }
+
+ private void pickUp(int pos) {
+ if (pos >= adapter.movableCount()) return; // the "Default home" tile stays put
+ movingKey = adapter.keyAt(pos);
+ dimOthers();
+ }
+
+ /** Puts the carried tile down where it is; the mode stays on. */
+ private void drop() {
+ movingKey = null;
+ dimOthers();
+ }
+
+ /** While a tile is carried, every other tile is dimmed so the carried one stands out; otherwise all are full. */
+ private void dimOthers() {
+ for (int i = 0; i < grid.getChildCount(); i++) {
+ View item = grid.getChildAt(i);
+ int pos = grid.getChildAdapterPosition(item);
+ boolean carried = movingKey != null && movingKey.equals(adapter.keyAt(pos));
+ item.setAlpha(movingKey == null || carried ? 1f : 0.45f);
+ }
+ }
+
+ /** Ends the mode and saves the order if it changed since the mode started. */
+ private void endReorder() {
+ if (!reordering) return;
+ drop();
+ reordering = false;
+ adapter.setReordering(false);
+ glass.setAccent(festive ? festival.accentColor() : 0);
+ btnReorder.setActivated(false);
+ List<String> keys = adapter.movableKeys();
+ List<String> before = orderAtStart;
+ orderAtStart = null;
+ if (!keys.equals(before)) {
+ Order order = new Order(Order.dockKey(dockApps), keys);
+ if (Order.save(Order.file(this), order)) {
+ adapter.setSavedOrder(Order.ranks(order, dockApps));
+ } else {
+ Toast.makeText(this, R.string.reorder_not_saved, Toast.LENGTH_SHORT).show();
+ }
+ }
+ if (fillDockLeavesSlots) reloadTiles(); // the empty dock slots left out while reordering come back
+ }
+
+ /**
+ * Moves the carried tile one step. Left and Right wrap across rows, like a list; Up and Down move a whole row,
+ * and Down into a short last row lands on its last tile. Nothing moves past either end or up into the top bar.
+ */
+ private void moveBy(int keyCode) {
+ int from = adapter.positionOf(movingKey);
+ int n = adapter.movableCount();
+ if (from < 0 || from >= n) {
+ drop();
+ return;
+ }
+ int to;
+ if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) to = from - 1;
+ else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) to = from + 1;
+ else if (keyCode == KeyEvent.KEYCODE_DPAD_UP) to = from - COLUMNS;
+ else if (from + COLUMNS < n) to = from + COLUMNS;
+ else to = from / COLUMNS < (n - 1) / COLUMNS ? n - 1 : from;
+ if (to < 0 || to >= n || to == from) return;
+ keepScroll(() -> adapter.move(from, to));
+ refocusAfterLayout(to);
+ }
+
+ /**
+ * Applies an adapter change without the grid jumping. After a move the layout manager anchors on the focused
+ * tile, keeping it at its old place on screen and shifting every other row; pinning the first visible row at its
+ * current offset as the next layout's anchor keeps the scroll where it was. onTileFocused still scrolls
+ * afterwards if the moved tile itself ends up off screen.
+ */
+ private void keepScroll(Runnable change) {
+ GridLayoutManager lm = (GridLayoutManager) grid.getLayoutManager();
+ int first = lm.findFirstVisibleItemPosition();
+ View firstView = first != RecyclerView.NO_POSITION ? lm.findViewByPosition(first) : null;
+ int offset = firstView != null ? lm.getDecoratedTop(firstView) - grid.getPaddingTop() : 0;
+ change.run();
+ if (firstView != null) lm.scrollToPositionWithOffset(first, offset);
+ }
+
+ /**
+ * Once the grid has applied the change, put the glass (and focus, if the move dropped it) on the tile at pos.
+ * The grid has a fixed size, so it applies adapter changes on the next animation frame rather than in a layout
+ * pass; until then it cannot find the tile by position. Wait frame by frame (at most 10) for that. No focus
+ * change fires for a moved tile, so focusTile's already-focused path calls onTileFocused itself.
+ */
+ private void refocusAfterLayout(int pos) {
+ grid.postOnAnimation(new Runnable() {
+ int frames;
+
+ @Override
+ public void run() {
+ if ((grid.hasPendingAdapterUpdates() || grid.isLayoutRequested()) && ++frames < 10) {
+ grid.postOnAnimation(this);
+ return;
+ }
+ focusTile(pos);
+ dimOthers(); // views that came into view or were rebound during the move
+ }
+ });
+ }
+
@Override
public void onBackPressed() {
// Home screen: Back goes nowhere.
@@ -1320,7 +1514,8 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
* the next one follows as soon as this one is answered. Nothing is shown until the owner says Show.
*/
private void showCardPrompt() {
- if (!moduleCards || !tilesReady || promptsPaused || cardPrompt != null || !hasWindowFocus() || isFinishing()) return;
+ if (!moduleCards || !tilesReady || promptsPaused || cardPrompt != null || !hasWindowFocus() || isFinishing()
+ || reordering) return;
if (cards.isEmpty() || !CardFeature.working()) return;
CardFeature.run(() -> {
showCardPromptNow();
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/Order.java b/launcher/app/src/main/java/com/gathin/tvhome/Order.java
new file mode 100644
index 000000000..639d13736
--- /dev/null
+++ b/launcher/app/src/main/java/com/gathin/tvhome/Order.java
@@ -0,0 +1,126 @@
+package com.gathin.tvhome;
+
+import android.content.Context;
+import android.util.AtomicFile;
+import android.util.Log;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * The owner's own tile order, set with the reorder button and kept in the launcher's private files (order.txt).
+ *
+ * Saved tiles come first in this order; apps not in it (new installs) follow by the usual rule (dock_apps, cards,
+ * alphabetical) and uninstalled ones simply drop out. The file also records the dock_apps it was made under: when
+ * 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.
+ */
+final class Order {
+
+ private static final String TAG = "TvHome.Order";
+ static final String FILE_NAME = "order.txt";
+ private static final String MAGIC = "tvhome-order 1";
+ private static final String DOCK = "dock ";
+
+ /** dock_apps the order was saved under, comma-joined (package names cannot contain commas). */
+ final String dock;
+ final List<String> keys;
+
+ Order(String dock, List<String> keys) {
+ this.dock = dock;
+ this.keys = Collections.unmodifiableList(new ArrayList<>(keys));
+ }
+
+ static String dockKey(List<String> dockApps) {
+ return String.join(",", dockApps);
+ }
+
+ static String encode(Order order) {
+ StringBuilder sb = new StringBuilder(MAGIC).append('\n').append(DOCK).append(order.dock).append('\n');
+ for (String k : order.keys) sb.append(k).append('\n');
+ return sb.toString();
+ }
+
+ /** The order in this text, or null when it is not one (wrong header, a blank or duplicate key). */
+ static Order decode(String text) {
+ 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;
+ List<String> keys = new ArrayList<>();
+ for (int i = 2; i < lines.length; i++) {
+ String k = lines[i];
+ if (k.isEmpty()) {
+ if (i == lines.length - 1) break; // the trailing newline
+ return null;
+ }
+ if (keys.contains(k)) return null;
+ keys.add(k);
+ }
+ return new Order(lines[1].substring(DOCK.length()), keys);
+ }
+
+ /**
+ * Rank of each saved key (0 = first) when the order still applies to these dock_apps, else an empty map.
+ * Tiles without a rank sort after every ranked one.
+ */
+ static Map<String, Integer> ranks(Order order, List<String> dockApps) {
+ Map<String, Integer> r = new HashMap<>();
+ if (order == null || !order.dock.equals(dockKey(dockApps))) return r;
+ for (int i = 0; i < order.keys.size(); i++) r.put(order.keys.get(i), i);
+ return r;
+ }
+
+ /** 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);
+ if (a == null && b == null) return 0;
+ if (a == null) return 1;
+ if (b == null) return -1;
+ return Integer.compare(a, b);
+ }
+
+ // ---- file ------------------------------------------------------------------------------------
+
+ static File file(Context ctx) {
+ return new File(ctx.getFilesDir(), FILE_NAME);
+ }
+
+ /** The saved order, or null when there is none or it does not decode (then it is ignored, not deleted). */
+ static Order load(File file) {
+ if (!file.exists()) return null;
+ try {
+ Order o = decode(new String(new AtomicFile(file).readFully(), StandardCharsets.UTF_8));
+ if (o == null) Log.w(TAG, file.getName() + " is not valid; ignored");
+ return o;
+ } catch (IOException e) {
+ Log.w(TAG, "cannot read " + file.getName() + ": " + e);
+ return null;
+ }
+ }
+
+ /** Writes the order atomically. False (and a log line) when the write failed. */
+ static boolean save(File file, Order order) {
+ AtomicFile af = new AtomicFile(file);
+ FileOutputStream out = null;
+ try {
+ out = af.startWrite();
+ out.write(encode(order).getBytes(StandardCharsets.UTF_8));
+ af.finishWrite(out);
+ return true;
+ } catch (IOException e) {
+ Log.w(TAG, "cannot write " + file.getName() + ": " + e);
+ if (out != null) af.failWrite(out);
+ return false;
+ }
+ }
+}
diff --git a/launcher/app/src/main/res/drawable/ic_swap_vert.xml b/launcher/app/src/main/res/drawable/ic_swap_vert.xml
new file mode 100644
index 000000000..8faeb80e3
--- /dev/null
+++ b/launcher/app/src/main/res/drawable/ic_swap_vert.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Material "swap_vert": two opposed arrows, for the reorder button. Tinted by the pill's text colour. -->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+ android:width="22dp" android:height="22dp"
+ android:viewportWidth="24" android:viewportHeight="24">
+ <path
+ android:fillColor="#FFFFFFFF"
+ android:pathData="M16,17.01V10h-2v7.01h-3L15,21l4,-3.99h-3zM9,3L5,6.99h3V14h2V6.99h3L9,3z" />
+</vector>
diff --git a/launcher/app/src/main/res/drawable/pill_bg.xml b/launcher/app/src/main/res/drawable/pill_bg.xml
index 79261c256..cb93ccadf 100644
--- a/launcher/app/src/main/res/drawable/pill_bg.xml
+++ b/launcher/app/src/main/res/drawable/pill_bg.xml
@@ -6,6 +6,14 @@
<corners android:radius="21dp" />
</shape>
</item>
+ <!-- Reorder button while reorder mode is on (focus is on the tiles then). -->
+ <item android:state_activated="true">
+ <shape android:shape="rectangle">
+ <solid android:color="#596EC1FF" />
+ <stroke android:width="1.5dp" android:color="#FF6EC1FF" />
+ <corners android:radius="21dp" />
+ </shape>
+ </item>
<item>
<shape android:shape="rectangle">
<gradient android:angle="270" android:startColor="#2EFFFFFF" android:endColor="#14FFFFFF" />
diff --git a/launcher/app/src/main/res/layout/activity_main.xml b/launcher/app/src/main/res/layout/activity_main.xml
index a03eac46c..ee9871178 100644
--- a/launcher/app/src/main/res/layout/activity_main.xml
+++ b/launcher/app/src/main/res/layout/activity_main.xml
@@ -182,6 +182,13 @@
android:drawableStart="@drawable/ic_wifi"
android:nextFocusDown="@+id/grid" />
+ <TextView
+ android:id="@+id/btnReorder"
+ style="@style/TopButtonIcon"
+ android:contentDescription="@string/reorder"
+ android:drawableStart="@drawable/ic_swap_vert"
+ android:nextFocusDown="@+id/grid" />
+
<TextView
android:id="@+id/btnSettings"
style="@style/TopButton"
diff --git a/launcher/app/src/main/res/values-ar/strings.xml b/launcher/app/src/main/res/values-ar/strings.xml
index 78248fc59..7175d5afe 100644
--- a/launcher/app/src/main/res/values-ar/strings.xml
+++ b/launcher/app/src/main/res/values-ar/strings.xml
@@ -58,4 +58,7 @@
<string name="on">تشغيل</string>
<string name="off">إيقاف</string>
<string name="enable">تفعيل</string>
+ <string name="reorder">إعادة ترتيب التطبيقات</string>
+ <string name="reorder_hint">اضغط موافق لالتقاط تطبيق، والأسهم لتحريكه، ورجوع لتثبيته. اضغط رجوع مرة أخرى للإنهاء.</string>
+ <string name="reorder_not_saved">تعذّر حفظ الترتيب الجديد</string>
</resources>
diff --git a/launcher/app/src/main/res/values-b+sr+Latn/strings.xml b/launcher/app/src/main/res/values-b+sr+Latn/strings.xml
index 63b877a6e..c2d770ba2 100644
--- a/launcher/app/src/main/res/values-b+sr+Latn/strings.xml
+++ b/launcher/app/src/main/res/values-b+sr+Latn/strings.xml
@@ -39,4 +39,7 @@
<string name="on">Uklj.</string>
<string name="off">Isklj.</string>
<string name="enable">Uključi</string>
+ <string name="reorder">Promeni redosled aplikacija</string>
+ <string name="reorder_hint">OK podiže aplikaciju, strelice je pomeraju, Nazad je postavlja. Ponovo Nazad za kraj.</string>
+ <string name="reorder_not_saved">Novi redosled nije sačuvan</string>
</resources>
diff --git a/launcher/app/src/main/res/values-ca/strings.xml b/launcher/app/src/main/res/values-ca/strings.xml
index 77d3d13c2..c69f35e1f 100644
--- a/launcher/app/src/main/res/values-ca/strings.xml
+++ b/launcher/app/src/main/res/values-ca/strings.xml
@@ -31,4 +31,7 @@
<string name="on">Activat</string>
<string name="off">Desactivat</string>
<string name="enable">Activa</string>
+ <string name="reorder">Reordena les aplicacions</string>
+ <string name="reorder_hint">D\'acord agafa una aplicació, les fletxes la mouen, Enrere la deixa. Torna a prémer Enrere per acabar.</string>
+ <string name="reorder_not_saved">No s\'ha pogut desar el nou ordre</string>
</resources>
diff --git a/launcher/app/src/main/res/values-de/strings.xml b/launcher/app/src/main/res/values-de/strings.xml
index cd269f5e3..2c2c30398 100644
--- a/launcher/app/src/main/res/values-de/strings.xml
+++ b/launcher/app/src/main/res/values-de/strings.xml
@@ -35,4 +35,7 @@
<string name="on">Ein</string>
<string name="off">Aus</string>
<string name="enable">Aktivieren</string>
+ <string name="reorder">Apps anordnen</string>
+ <string name="reorder_hint">OK hebt eine App an, Pfeiltasten verschieben sie, Zurück legt sie ab. Nochmals Zurück zum Beenden.</string>
+ <string name="reorder_not_saved">Neue Reihenfolge konnte nicht gespeichert werden</string>
</resources>
diff --git a/launcher/app/src/main/res/values-es/strings.xml b/launcher/app/src/main/res/values-es/strings.xml
index 808ceec24..22d74d90f 100644
--- a/launcher/app/src/main/res/values-es/strings.xml
+++ b/launcher/app/src/main/res/values-es/strings.xml
@@ -32,4 +32,7 @@
<string name="on">Sí</string>
<string name="off">No</string>
<string name="enable">Activar</string>
+ <string name="reorder">Reordenar apps</string>
+ <string name="reorder_hint">OK coge una app, las flechas la mueven, Atrás la suelta. Atrás de nuevo para terminar.</string>
+ <string name="reorder_not_saved">No se pudo guardar el nuevo orden</string>
</resources>
diff --git a/launcher/app/src/main/res/values-fr/strings.xml b/launcher/app/src/main/res/values-fr/strings.xml
index f25d6ccb6..ea4dd7e57 100644
--- a/launcher/app/src/main/res/values-fr/strings.xml
+++ b/launcher/app/src/main/res/values-fr/strings.xml
@@ -58,4 +58,7 @@
<string name="on">Activé</string>
<string name="off">Désactivé</string>
<string name="enable">Activer</string>
+ <string name="reorder">Réorganiser les applis</string>
+ <string name="reorder_hint">OK prend une appli, les flèches la déplacent, Retour la pose. Retour à nouveau pour terminer.</string>
+ <string name="reorder_not_saved">Impossible d\'enregistrer le nouvel ordre</string>
</resources>
diff --git a/launcher/app/src/main/res/values-iw/strings.xml b/launcher/app/src/main/res/values-iw/strings.xml
index 5f3d99ea9..125be383b 100644
--- a/launcher/app/src/main/res/values-iw/strings.xml
+++ b/launcher/app/src/main/res/values-iw/strings.xml
@@ -62,4 +62,7 @@
<string name="quick_network">רשת</string>
<string name="quick_apps">אפליקציות</string>
<string name="quick_apps_hint">הפעלה מחדש</string>
+ <string name="reorder">סידור אפליקציות</string>
+ <string name="reorder_hint">אישור מרים אפליקציה, החצים מזיזים אותה, חזרה מניחה אותה. חזרה שוב לסיום.</string>
+ <string name="reorder_not_saved">לא ניתן לשמור את הסדר החדש</string>
</resources>
diff --git a/launcher/app/src/main/res/values-pl/strings.xml b/launcher/app/src/main/res/values-pl/strings.xml
index 1fa45f537..0de54a1db 100644
--- a/launcher/app/src/main/res/values-pl/strings.xml
+++ b/launcher/app/src/main/res/values-pl/strings.xml
@@ -58,4 +58,7 @@
<string name="on">Wł.</string>
<string name="off">Wył.</string>
<string name="enable">Włącz</string>
+ <string name="reorder">Zmień kolejność aplikacji</string>
+ <string name="reorder_hint">OK podnosi aplikację, strzałki ją przesuwają, Wstecz ją upuszcza. Ponownie Wstecz, aby zakończyć.</string>
+ <string name="reorder_not_saved">Nie udało się zapisać nowej kolejności</string>
</resources>
diff --git a/launcher/app/src/main/res/values-pt/strings.xml b/launcher/app/src/main/res/values-pt/strings.xml
index d994915c8..0daa9f9a6 100644
--- a/launcher/app/src/main/res/values-pt/strings.xml
+++ b/launcher/app/src/main/res/values-pt/strings.xml
@@ -46,4 +46,7 @@
<string name="card_state_new">Novo</string>
<string name="card_state_new_key">Nova chave</string>
<string name="card_state_new_label">Novo nome</string>
+ <string name="reorder">Reordenar apps</string>
+ <string name="reorder_hint">OK pega numa app, as setas movem-na, Voltar pousa-a. Voltar outra vez para terminar.</string>
+ <string name="reorder_not_saved">Não foi possível guardar a nova ordem</string>
</resources>
diff --git a/launcher/app/src/main/res/values-ru/strings.xml b/launcher/app/src/main/res/values-ru/strings.xml
index f9060ba25..e03aed15a 100644
--- a/launcher/app/src/main/res/values-ru/strings.xml
+++ b/launcher/app/src/main/res/values-ru/strings.xml
@@ -58,4 +58,7 @@
<string name="on">Вкл.</string>
<string name="off">Выкл.</string>
<string name="enable">Включить</string>
+ <string name="reorder">Изменить порядок приложений</string>
+ <string name="reorder_hint">ОК — взять приложение, стрелки — переместить, Назад — поставить. Ещё раз Назад — готово.</string>
+ <string name="reorder_not_saved">Не удалось сохранить новый порядок</string>
</resources>
diff --git a/launcher/app/src/main/res/values-th/strings.xml b/launcher/app/src/main/res/values-th/strings.xml
index bb66f7f61..9f2338dd3 100644
--- a/launcher/app/src/main/res/values-th/strings.xml
+++ b/launcher/app/src/main/res/values-th/strings.xml
@@ -33,4 +33,7 @@
<string name="on">เปิด</string>
<string name="off">ปิด</string>
<string name="enable">เปิดใช้งาน</string>
+ <string name="reorder">จัดเรียงแอป</string>
+ <string name="reorder_hint">กด OK เพื่อเลือกแอป ใช้ลูกศรเพื่อย้าย กดย้อนกลับเพื่อวาง กดย้อนกลับอีกครั้งเพื่อเสร็จสิ้น</string>
+ <string name="reorder_not_saved">บันทึกลำดับใหม่ไม่ได้</string>
</resources>
diff --git a/launcher/app/src/main/res/values-tr/strings.xml b/launcher/app/src/main/res/values-tr/strings.xml
index 7d71658d6..c2120b84d 100644
--- a/launcher/app/src/main/res/values-tr/strings.xml
+++ b/launcher/app/src/main/res/values-tr/strings.xml
@@ -58,4 +58,7 @@
<string name="on">Açık</string>
<string name="off">Kapalı</string>
<string name="enable">Etkinleştir</string>
+ <string name="reorder">Uygulamaları sırala</string>
+ <string name="reorder_hint">Tamam bir uygulamayı alır, oklar taşır, Geri bırakır. Bitirmek için tekrar Geri.</string>
+ <string name="reorder_not_saved">Yeni sıra kaydedilemedi</string>
</resources>
diff --git a/launcher/app/src/main/res/values-zh-rCN/strings.xml b/launcher/app/src/main/res/values-zh-rCN/strings.xml
index 220e62447..3ce33cfc5 100644
--- a/launcher/app/src/main/res/values-zh-rCN/strings.xml
+++ b/launcher/app/src/main/res/values-zh-rCN/strings.xml
@@ -34,4 +34,7 @@
<string name="cant_open">无法打开 %1$s</string>
<string name="date_format">M月d日 EEEE</string>
<string name="dream_name">时钟</string>
+ <string name="reorder">调整应用顺序</string>
+ <string name="reorder_hint">按确定选中应用,方向键移动,按返回放下。再按一次返回完成。</string>
+ <string name="reorder_not_saved">无法保存新顺序</string>
</resources>
diff --git a/launcher/app/src/main/res/values-zh-rTW/strings.xml b/launcher/app/src/main/res/values-zh-rTW/strings.xml
index 8be29ac81..5880aa7a5 100644
--- a/launcher/app/src/main/res/values-zh-rTW/strings.xml
+++ b/launcher/app/src/main/res/values-zh-rTW/strings.xml
@@ -58,4 +58,7 @@
<string name="card_state_new">新的</string>
<string name="card_state_new_key">新金鑰</string>
<string name="card_state_new_label">新名稱</string>
+ <string name="reorder">調整應用程式順序</string>
+ <string name="reorder_hint">按確定選取應用程式,方向鍵移動,按返回放下。再按一次返回完成。</string>
+ <string name="reorder_not_saved">無法儲存新順序</string>
</resources>
diff --git a/launcher/app/src/main/res/values/strings.xml b/launcher/app/src/main/res/values/strings.xml
index dd579ee75..9b69bdd24 100644
--- a/launcher/app/src/main/res/values/strings.xml
+++ b/launcher/app/src/main/res/values/strings.xml
@@ -64,4 +64,7 @@
<string name="card_state_new">New</string>
<string name="card_state_new_key">New key</string>
<string name="card_state_new_label">New name</string>
+ <string name="reorder">Reorder apps</string>
+ <string name="reorder_hint">OK picks up an app, arrows move it, Back places it. Back again to finish.</string>
+ <string name="reorder_not_saved">Couldn\'t save the new order</string>
</resources>
diff --git a/launcher/app/src/main/res/values/styles.xml b/launcher/app/src/main/res/values/styles.xml
index 4c56dce05..1833cd18a 100644
--- a/launcher/app/src/main/res/values/styles.xml
+++ b/launcher/app/src/main/res/values/styles.xml
@@ -26,6 +26,15 @@
</style>
<style name="TopButton" parent="TopButtonBase" />
+ <!-- Icon-only pill: a 42dp circle (pill_bg's 21dp corners) with the 22dp icon centred by even padding. Give it a
+ contentDescription, since there is no text. -->
+ <style name="TopButtonIcon" parent="TopButtonBase">
+ <item name="android:layout_width">42dp</item>
+ <item name="android:paddingStart">10dp</item>
+ <item name="android:paddingEnd">10dp</item>
+ <item name="android:drawablePadding">0dp</item>
+ </style>
+
<style name="SettingsRow">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">52dp</item>
diff --git a/launcher/app/src/test/java/com/gathin/tvhome/OrderTest.java b/launcher/app/src/test/java/com/gathin/tvhome/OrderTest.java
new file mode 100644
index 000000000..2f98da6b8
--- /dev/null
+++ b/launcher/app/src/test/java/com/gathin/tvhome/OrderTest.java
@@ -0,0 +1,75 @@
+package com.gathin.tvhome;
+
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+/** Plain JVM tests for the saved tile order: file format, dock_apps reset and the sort step. */
+public class OrderTest {
+
+ private static final List<String> DOCK = Arrays.asList("com.plexapp.android", "com.netflix.ninja");
+
+ @Test
+ public void roundTrips() {
+ Order o = new Order(Order.dockKey(DOCK), Arrays.asList("b/.B", "a/.A", "c/.C"));
+ Order back = Order.decode(Order.encode(o));
+ assertEquals(o.dock, back.dock);
+ assertEquals(o.keys, back.keys);
+ }
+
+ @Test
+ public void emptyOrderRoundTrips() {
+ Order back = Order.decode(Order.encode(new Order("", Collections.emptyList())));
+ assertEquals("", back.dock);
+ assertTrue(back.keys.isEmpty());
+ }
+
+ @Test
+ public void rejectsWhatIsNotAnOrder() {
+ assertNull(Order.decode(null));
+ assertNull(Order.decode(""));
+ assertNull(Order.decode("{\"version\":2}"));
+ assertNull(Order.decode("tvhome-order 2\ndock x\na/.A\n"));
+ assertNull(Order.decode("tvhome-order 1\na/.A\n")); // no dock line
+ assertNull(Order.decode("tvhome-order 1\ndock x\na/.A\n\nb/.B\n")); // blank key in the middle
+ assertNull(Order.decode("tvhome-order 1\ndock x\na/.A\na/.A\n")); // duplicate key
+ }
+
+ @Test
+ public void ranksFollowTheSavedOrder() {
+ Order o = new Order(Order.dockKey(DOCK), Arrays.asList("b/.B", "a/.A"));
+ Map<String, Integer> r = Order.ranks(o, DOCK);
+ assertEquals(Integer.valueOf(0), r.get("b/.B"));
+ assertEquals(Integer.valueOf(1), r.get("a/.A"));
+ }
+
+ @Test
+ public void changedDockAppsDropTheSavedOrder() {
+ Order o = new Order(Order.dockKey(DOCK), Arrays.asList("b/.B", "a/.A"));
+ assertTrue(Order.ranks(o, Arrays.asList("com.netflix.ninja", "com.plexapp.android")).isEmpty());
+ assertTrue(Order.ranks(o, Collections.singletonList("com.plexapp.android")).isEmpty());
+ assertTrue(Order.ranks(null, DOCK).isEmpty());
+ }
+
+ @Test
+ public void savedTilesComeFirstAndNewOnesKeepTheirOrderAfter() {
+ Order o = new Order(Order.dockKey(DOCK), Arrays.asList("c/.C", "a/.A", "gone/.Gone"));
+ Map<String, Integer> r = Order.ranks(o, DOCK);
+ // "n" and "m" are new installs: unranked, so the usual rule (here: list order) decides between them.
+ List<String> tiles = new ArrayList<>(Arrays.asList("m/.M", "a/.A", "n/.N", "c/.C"));
+ List<String> usual = new ArrayList<>(tiles);
+ tiles.sort((x, y) -> {
+ int c = Order.compare(r, x, y);
+ return c != 0 ? c : Integer.compare(usual.indexOf(x), usual.indexOf(y));
+ });
+ assertEquals(Arrays.asList("c/.C", "a/.A", "m/.M", "n/.N"), tiles);
+ }
+}