Skip to content

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

feat(launcher): add Home-key service and options for TV sticks

Hoai Nguyen78413845a921
diff --git a/launcher/app/src/main/AndroidManifest.xml b/launcher/app/src/main/AndroidManifest.xml
index 8cea721b9..159e7eeca 100644
--- a/launcher/app/src/main/AndroidManifest.xml
+++ b/launcher/app/src/main/AndroidManifest.xml
@@ -32,6 +32,21 @@
</intent-filter>
</activity>
+ <!-- Home button for remotes that send KEYCODE_ALL_APPS (Xiaomi). Enabled over adb by
+ build-and-install.sh; see HomeKeyService. -->
+ <service
+ android:name=".HomeKeyService"
+ android:label="@string/home_key_name"
+ android:exported="true"
+ android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
+ <intent-filter>
+ <action android:name="android.accessibilityservice.AccessibilityService" />
+ </intent-filter>
+ <meta-data
+ android:name="android.accessibilityservice"
+ android:resource="@xml/home_key_service" />
+ </service>
+
<!-- Screensaver: pick it in Settings, or `settings put secure screensaver_components
com.gathin.tvhome/.ClockDream` over adb. -->
<service
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java b/launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java
new file mode 100644
index 000000000..0c0626b7c
--- /dev/null
+++ b/launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java
@@ -0,0 +1,39 @@
+package com.gathin.tvhome;
+
+import android.accessibilityservice.AccessibilityService;
+import android.content.Intent;
+import android.view.KeyEvent;
+import android.view.accessibility.AccessibilityEvent;
+
+/**
+ * Sends the remote's Home button to this launcher on remotes that emit KEYCODE_ALL_APPS for Home
+ * (Xiaomi sticks and boxes). Android TV 9 routes that key only to the system-image launcher, so once
+ * the stock launcher is disabled the button does nothing. Every other key passes through untouched.
+ *
+ * Enable over adb:
+ * settings put secure enabled_accessibility_services com.gathin.tvhome/.HomeKeyService
+ * settings put secure accessibility_enabled 1
+ */
+public class HomeKeyService extends AccessibilityService {
+
+ @Override
+ protected boolean onKeyEvent(KeyEvent event) {
+ if (event.getKeyCode() != KeyEvent.KEYCODE_ALL_APPS) return false;
+ if (event.getAction() == KeyEvent.ACTION_UP && !event.isCanceled()) {
+ Intent home = new Intent(Intent.ACTION_MAIN)
+ .addCategory(Intent.CATEGORY_HOME)
+ .setPackage(getPackageName())
+ .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
+ startActivity(home);
+ }
+ return true;
+ }
+
+ @Override
+ public void onAccessibilityEvent(AccessibilityEvent event) {
+ }
+
+ @Override
+ public void onInterrupt() {
+ }
+}
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 1983ab73e..ac0cac891 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
@@ -130,11 +130,14 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
adapter = new AppAdapter(this, this, COLUMNS);
grid.setAdapter(adapter);
- // tvOS layout: push the dock row to the bottom of the screen; the grid hides below until you press Down.
+ // tvOS layout: push the dock row towards the bottom of the screen, leaving room for
+ // rows_below_dock grid rows under it; the rest of the grid stays below until you press Down.
+ int rowsBelowDock = Math.max(0, getResources().getInteger(R.integer.rows_below_dock));
grid.addOnLayoutChangeListener((v, l, t, r, b, ol, ot, or, ob) -> {
int h = b - t;
int dockRow = (int) ((145 + 6 + 12) * density); // item height plus DockDecoration offsets
- int want = Math.max(0, h - dockRow - (int) (30 * density));
+ int gridRows = (int) (145 * density * rowsBelowDock);
+ int want = Math.max(0, h - dockRow - gridRows - (int) (30 * density));
if (grid.getPaddingTop() != want) {
grid.setPadding(grid.getPaddingLeft(), want, grid.getPaddingRight(), grid.getPaddingBottom());
}
@@ -160,7 +163,10 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
View btnInputs = findViewById(R.id.btnInputs);
View btnSettings = findViewById(R.id.btnSettings);
+ boolean showInputs = getResources().getBoolean(R.bool.show_inputs);
+ btnInputs.setVisibility(showInputs ? View.VISIBLE : View.GONE);
btnInputs.setOnClickListener(v -> showInputs());
+ int firstPill = showInputs ? R.id.btnInputs : R.id.btnWifi;
btnWifi.setOnClickListener(v -> open(new Intent(Settings.ACTION_WIFI_SETTINGS)));
btnSettings.setOnClickListener(v -> open(new Intent(Settings.ACTION_SETTINGS)));
@@ -169,7 +175,7 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
View.OnKeyListener pillKeys = (v, keyCode, event) -> {
if (event.getAction() != KeyEvent.ACTION_DOWN) return false;
if (keyCode == KeyEvent.KEYCODE_DPAD_UP) return true;
- if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT && v.getId() == R.id.btnInputs) return true;
+ if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT && v.getId() == firstPill) return true;
return keyCode == KeyEvent.KEYCODE_DPAD_RIGHT && v.getId() == R.id.btnSettings;
};
btnInputs.setOnKeyListener(pillKeys);
diff --git a/launcher/app/src/main/res/values/config.xml b/launcher/app/src/main/res/values/config.xml
index c3dd51ca8..1cac4d276 100644
--- a/launcher/app/src/main/res/values/config.xml
+++ b/launcher/app/src/main/res/values/config.xml
@@ -22,6 +22,12 @@
<!-- The TV's Live TV / tuner app, offered at the end of the Inputs menu. Leave empty to omit. -->
<string name="live_tv_package" translatable="false">com.mediatek.wwtv.tvcenter</string>
+ <!-- Show the Inputs pill (HDMI / AV / Live TV). Turn off on sticks and boxes without inputs. -->
+ <bool name="show_inputs">true</bool>
+
+ <!-- Grid rows left visible under the dock on the home screen. 0 = dock sits at the very bottom. -->
+ <integer name="rows_below_dock">0</integer>
+
<!-- Tiles per row in the grid; the dock always has this many slots. -->
<integer name="columns">4</integer>
</resources>
diff --git a/launcher/app/src/main/res/values/strings.xml b/launcher/app/src/main/res/values/strings.xml
index 81b72f54a..7b011b117 100644
--- a/launcher/app/src/main/res/values/strings.xml
+++ b/launcher/app/src/main/res/values/strings.xml
@@ -20,4 +20,6 @@
<string name="cant_open">Can\'t open %1$s</string>
<string name="date_format">EEEE d MMMM</string>
<string name="dream_name">Clock</string>
+ <string name="home_key_name">Home button</string>
+ <string name="home_key_description">Makes the remote\'s Home button open this launcher.</string>
</resources>
diff --git a/launcher/app/src/main/res/xml/home_key_service.xml b/launcher/app/src/main/res/xml/home_key_service.xml
new file mode 100644
index 000000000..2921be72d
--- /dev/null
+++ b/launcher/app/src/main/res/xml/home_key_service.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
+ android:accessibilityEventTypes="typeWindowStateChanged"
+ android:accessibilityFeedbackType="feedbackGeneric"
+ android:accessibilityFlags="flagRequestFilterKeyEvents"
+ android:canRequestFilterKeyEvents="true"
+ android:description="@string/home_key_description" />
diff --git a/launcher/build-and-install.sh b/launcher/build-and-install.sh
index 9c9d2ed19..a22592152 100755
--- a/launcher/build-and-install.sh
+++ b/launcher/build-and-install.sh
@@ -21,4 +21,12 @@ printf '%s\n' \
"cmd package set-home-activity --user 0 com.gathin.tvhome/.MainActivity" \
"pm disable-user --user 0 $STOCK_LAUNCHER" \
| "$PY" -I ../tools/tv.py --batch
+# Remotes whose Home button sends KEYCODE_ALL_APPS (Xiaomi) need HomeKeyService. Android turns
+# accessibility services off when their app is reinstalled, so re-enable it after every install.
+if [ "${HOME_KEY_SERVICE:-0}" = 1 ]; then
+ printf '%s\n' \
+ "settings put secure enabled_accessibility_services com.gathin.tvhome/.HomeKeyService" \
+ "settings put secure accessibility_enabled 1" \
+ | "$PY" -I ../tools/tv.py --batch
+fi
echo "Installed. Press Home on the remote to see it."