Skip to content

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

Launcher: keep focus on the right app across reloads and app switches

pananonee80e323b915
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 2e7abf3ae..f836074bc 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/AppAdapter.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/AppAdapter.java
@@ -81,6 +81,9 @@ public class AppAdapter extends RecyclerView.Adapter<AppAdapter.Holder> {
this.dockSize = dockSize;
this.favourites = Arrays.asList(ctx.getResources().getStringArray(R.array.dock_apps));
this.hidden = new HashSet<>(Arrays.asList(ctx.getResources().getStringArray(R.array.hidden_apps)));
+ // Tiles keep their identity across reload(), so a focused tile stays on its app instead of
+ // its view being rebound to whatever app now sits in that slot.
+ setHasStableIds(true);
}
void reload() {
@@ -124,9 +127,10 @@ public class AppAdapter extends RecyclerView.Adapter<AppAdapter.Holder> {
Entry e = items.get(position);
h.name.setText(e.label);
h.name.setVisibility(position < dockSize ? View.INVISIBLE : View.VISIBLE); // the dock has no labels
- h.name.setAlpha(0f);
- h.card.setScaleX(1f);
- h.card.setScaleY(1f);
+ boolean focused = h.card.isFocused(); // a rebind after reload() can hit the focused tile
+ h.name.setAlpha(focused ? 1f : 0f);
+ h.card.setScaleX(focused ? FOCUS_SCALE : 1f);
+ h.card.setScaleY(focused ? FOCUS_SCALE : 1f);
if (e.banner != null) {
h.banner.setImageDrawable(e.banner);
h.banner.setVisibility(View.VISIBLE);
@@ -169,6 +173,25 @@ public class AppAdapter extends RecyclerView.Adapter<AppAdapter.Holder> {
return items.size();
}
+ @Override
+ public long getItemId(int position) {
+ return items.get(position).component.flattenToShortString().hashCode();
+ }
+
+ /** 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;
+ }
+
+ /** Position of the app with this key, or -1 when it is gone. */
+ int positionOf(String key) {
+ if (key == null) return -1;
+ for (int i = 0; i < items.size(); i++) {
+ if (items.get(i).component.flattenToShortString().equals(key)) return i;
+ }
+ return -1;
+ }
+
String labelAt(int position) {
return position >= 0 && position < items.size() ? items.get(position).label : "";
}
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 0732c3a65..7695bfa54 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
@@ -69,7 +69,11 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
private SpringAnimation springX, springY;
private float glassTargetX, glassTargetY;
private boolean glassShown;
- private int lastFocused = 0;
+ // Static so they survive the activity being destroyed while another app is open (low-memory TVs,
+ // or Developer options → "Don't keep activities"); they only reset when the process dies.
+ private static int lastFocused = 0;
+ /** App of the last focused tile, so focus follows it when the app list changes. */
+ private static String lastFocusedKey;
private float density;
private final Handler handler = new Handler(Looper.getMainLooper());
@@ -174,17 +178,20 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
heroShown = false;
heroName.setAlpha(0f);
ambient.setAlpha(0f);
+ // Read these before reload(): rebinding can move focus to the first tile and overwrite them.
+ final String returnKey = lastFocusedKey;
+ final int returnSlot = lastFocused;
adapter.reload();
grid.scheduleLayoutAnimation();
updateGreeting();
updateWifiLabel();
handler.postDelayed(minuteTick, MINUTE_MS);
- // Return to the tile we left from, or the first one.
+ // Return to the app we left from, or the same slot if that app is gone. Always refocus: after
+ // reload() the view that holds focus may no longer show the app it had.
grid.post(() -> {
- View focused = getCurrentFocus();
- if (focused != null && grid.findContainingViewHolder(focused) != null) return;
- focusTile(lastFocused);
+ int pos = adapter.positionOf(returnKey);
+ focusTile(pos >= 0 ? pos : returnSlot);
});
}
@@ -204,6 +211,7 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
@Override
public void onTileFocused(int position, View card) {
lastFocused = position;
+ lastFocusedKey = adapter.keyAt(position);
// Tile rect in root coordinates, using layout positions (ignores the scale animation).
Rect r = new Rect(0, 0, card.getWidth(), card.getHeight());
root.offsetDescendantRectToMyCoords(card, r);
@@ -324,7 +332,10 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
final int pos = Math.max(0, Math.min(position, adapter.getItemCount() - 1));
RecyclerView.ViewHolder vh = grid.findViewHolderForAdapterPosition(pos);
if (vh != null) {
- vh.itemView.findViewById(R.id.card).requestFocus();
+ View card = vh.itemView.findViewById(R.id.card);
+ // Already focused (e.g. back from an app): no focus change fires, so restore the glass by hand.
+ if (card.isFocused()) onTileFocused(pos, card);
+ else card.requestFocus();
return;
}
// Not laid out yet: scroll there, then focus once the scroll settles.