Skip to content

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

Merge PR #116: module cards

Gathin23a94b5e1981de
diff --cc launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
index 6df756b11,f11b31b8c..9ffccf2fb
--- a/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
@@@ -1,8 -1,8 +1,9 @@@
package com.gathin.tvhome;
import android.app.Activity;
+ import android.app.Dialog;
import android.content.ActivityNotFoundException;
+import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
@@@ -327,53 -326,66 +349,96 @@@ public class MainActivity extends Activ
@Override
protected void onResume() {
super.onResume();
- glassShown = false;
- glass.setAlpha(0f);
- heroShown = false;
- heroName.setAlpha(0f);
- ambient.setAlpha(0f);
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
+ registerReceiver(packageReceiver, packageFilter, Context.RECEIVER_NOT_EXPORTED);
+ } else {
+ registerReceiver(packageReceiver, packageFilter);
+ }
+ tilesReady = false;
+ promptsPaused = false;
- reloadTiles();
- grid.scheduleLayoutAnimation();
+ reloadApps(true);
updateGreeting();
updateWifiLabel();
updateInputsButton();
handler.postDelayed(minuteTick, MINUTE_MS);
- restoreFocus();
+ }
+
++ /** Rebuilds the tiles (and the module cards), then puts focus back where it was. */
+ private void reloadApps(boolean animate) {
+ // Capture the app before rebinding can move focus and overwrite its key.
+ final String returnKey = lastFocusedKey;
+ final int returnSlot = lastFocused;
+ final boolean restoreFocus = animate || grid.hasFocus();
+ if (restoreFocus) {
+ glassShown = false;
+ glass.setAlpha(0f);
+ heroShown = false;
+ heroName.setAlpha(0f);
+ ambient.setAlpha(0f);
+ }
- adapter.reload();
++ reloadTiles();
+ if (animate) grid.scheduleLayoutAnimation();
+
+ if (restoreFocus) {
+ // Follow the same app, or the nearest remaining slot when it was removed.
- grid.post(() -> {
- int pos = adapter.positionOf(returnKey);
- focusTile(pos >= 0 ? pos : returnSlot);
- });
++ restoreFocus();
+ } else {
+ // A package change must not steal focus from a top button or the Inputs popup.
+ int pos = adapter.positionOf(returnKey);
+ lastFocused = Math.max(0, Math.min(pos >= 0 ? pos : returnSlot, adapter.getItemCount() - 1));
+ lastFocusedKey = adapter.keyAt(lastFocused);
+ if (pos < 0) hideHero();
++ // A newly installed app may have brought a card with it.
++ tilesReady = true;
++ showCardPrompt();
+ }
}
+ /**
+ * 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
+ * ones and cards stay off for this process.
+ */
+ private void reloadTiles() {
+ // Read these before reload(): rebinding can move focus to the first tile and overwrite them.
+ final String returnKey = lastFocusedKey;
+ final int returnSlot = lastFocused;
+ final List<Cards.Card> none = Collections.emptyList();
+ cards = moduleCards ? CardFeature.run(() -> Cards.discover(this, hiddenApps, Cards.load(Cards.storeFile(this))), none) : none;
+ adapter.reload(cards.isEmpty() ? none : CardFeature.run(() -> Cards.shown(cards), none));
+ lastFocusedKey = returnKey;
+ lastFocused = returnSlot;
+ }
+
+ /**
+ * 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.
+ */
+ private void restoreFocus() {
+ final String returnKey = lastFocusedKey;
+ final int returnSlot = lastFocused;
+ grid.post(() -> {
+ int pos = adapter.positionOf(returnKey);
+ focusTile(pos >= 0 ? pos : returnSlot);
+ tilesReady = true;
+ showCardPrompt();
+ });
+ }
+
@Override
protected void onPause() {
super.onPause();
+ unregisterReceiver(packageReceiver);
handler.removeCallbacks(minuteTick);
+ // Nothing of ours stays on screen over another app; an undecided prompt simply returns next time.
+ if (cardPrompt != null) cardPrompt.dismiss();
+ if (openPopup != null) openPopup.dismiss();
+ }
+
+ @Override
+ public void onWindowFocusChanged(boolean hasFocus) {
+ super.onWindowFocusChanged(hasFocus);
+ if (hasFocus) showCardPrompt();
}
@Override