Skip to content

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

launcher: drop HomeKeyService while the screen is off

VincentYe1237ace89310497
diff --git a/launcher/app/src/main/AndroidManifest.xml b/launcher/app/src/main/AndroidManifest.xml
index 5da0f46a5..701b5feb2 100644
--- a/launcher/app/src/main/AndroidManifest.xml
+++ b/launcher/app/src/main/AndroidManifest.xml
@@ -211,6 +211,28 @@
<!-- The remote's Input button (KEYCODE_TV or KEYCODE_TV_INPUT) opens the input switcher, and input_key_keycodes and
app_keys (config.xml or a theme) bring back other remote buttons; see InputKeyService. -->
+ <!-- Bound again by the system after a force stop, which restarts Home on firmware that kills
+ third-party apps (TCL tc8000_ay30a2). It reads no notification.
+ cmd notification allow_listener com.gathin.tvhome/.GuardRevival -->
+ <service
+ android:name=".GuardRevival"
+ android:directBootAware="true"
+ android:exported="true"
+ android:label="@string/home_key_name"
+ android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
+ <intent-filter>
+ <action android:name="android.service.notification.NotificationListenerService" />
+ </intent-filter>
+ </service>
+ <receiver
+ android:name=".GuardConfig"
+ android:exported="true"
+ android:permission="android.permission.WRITE_SECURE_SETTINGS">
+ <intent-filter>
+ <action android:name="com.gathin.tvhome.GUARD" />
+ </intent-filter>
+ </receiver>
+
<service
android:name=".InputKeyService"
android:exported="false"
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/GuardConfig.java b/launcher/app/src/main/java/com/gathin/tvhome/GuardConfig.java
new file mode 100644
index 000000000..779d6fe86
--- /dev/null
+++ b/launcher/app/src/main/java/com/gathin/tvhome/GuardConfig.java
@@ -0,0 +1,20 @@
+package com.gathin.tvhome;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+
+/**
+ * Opt in to leaving the home helper while the screen is off.
+ * {@code adb shell am broadcast -a com.gathin.tvhome.GUARD -n com.gathin.tvhome/.GuardConfig --ez guard_off_in_standby true}
+ */
+public class GuardConfig extends BroadcastReceiver {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ if (intent.hasExtra("guard_off_in_standby")) {
+ GuardKeep.setOffInStandby(context, intent.getBooleanExtra("guard_off_in_standby", false));
+ GuardKeep.ensureHomeKey(context);
+ }
+ setResultData("guard_off_in_standby=" + GuardKeep.offInStandby(context));
+ }
+}
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/GuardKeep.java b/launcher/app/src/main/java/com/gathin/tvhome/GuardKeep.java
new file mode 100644
index 000000000..cb3d8bf8c
--- /dev/null
+++ b/launcher/app/src/main/java/com/gathin/tvhome/GuardKeep.java
@@ -0,0 +1,106 @@
+package com.gathin.tvhome;
+
+import android.Manifest;
+import android.accessibilityservice.AccessibilityService;
+import android.content.BroadcastReceiver;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.pm.PackageManager;
+import android.os.PowerManager;
+import android.os.UserManager;
+import android.provider.Settings;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Keeps {@link HomeKeyService} on the accessibility list, the way a China-market TV needs it.
+ *
+ * TCL and Xiaomi start their own launcher at boot and force-stop a third-party one, which also
+ * drops its accessibility service. {@link GuardRevival} is bound again by the system afterwards
+ * and calls {@link #ensureHomeKey}. With {@code guard_off_in_standby} the service removes itself
+ * while the screen is off: on a TCL tc8000 the power key never woke the set while an accessibility
+ * service of this app stayed bound.
+ */
+final class GuardKeep {
+ static final String ACTION = "com.gathin.tvhome.GUARD";
+ private static final String PREFS = "guard";
+ private static final String KEY_OFF = "off_in_standby";
+
+ private static BroadcastReceiver screenOn;
+
+ private GuardKeep() {
+ }
+
+ static boolean offInStandby(Context context) {
+ UserManager users = context.getSystemService(UserManager.class);
+ if (users != null && !users.isUserUnlocked()) return false;
+ return context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).getBoolean(KEY_OFF, false);
+ }
+
+ static void setOffInStandby(Context context, boolean off) {
+ context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).edit().putBoolean(KEY_OFF, off).commit();
+ }
+
+ /** Puts HomeKeyService back on the enabled list when standby exit is opted in. Other services stay. */
+ static void ensureHomeKey(Context context) {
+ if (!offInStandby(context)) return;
+ if (context.checkSelfPermission(Manifest.permission.WRITE_SECURE_SETTINGS)
+ != PackageManager.PERMISSION_GRANTED) return;
+ PowerManager power = context.getSystemService(PowerManager.class);
+ // Still off: binding the helper again would sit on the power key. Wait for the screen.
+ if (power != null && !power.isInteractive()) {
+ watchScreenOn(context.getApplicationContext());
+ return;
+ }
+ try {
+ ComponentName mine = new ComponentName(context, HomeKeyService.class);
+ String list = Settings.Secure.getString(context.getContentResolver(),
+ Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
+ List<String> entries = new ArrayList<>();
+ boolean found = false;
+ if (list != null && !list.equals("null")) {
+ for (String entry : list.split(":")) {
+ if (entry.isEmpty()) continue;
+ ComponentName name = ComponentName.unflattenFromString(entry);
+ if (mine.equals(name)) found = true;
+ entries.add(entry);
+ }
+ }
+ if (!found) {
+ entries.add(mine.flattenToShortString());
+ Settings.Secure.putString(context.getContentResolver(),
+ Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, String.join(":", entries));
+ }
+ Settings.Secure.putInt(context.getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, 1);
+ } catch (RuntimeException ignored) {
+ // The settings are the system's; a refusal leaves the helper as it was.
+ }
+ }
+
+ /** Drops the service from the list and puts it back on the next SCREEN_ON. The process has to stay up. */
+ static void stepOut(AccessibilityService service) {
+ watchScreenOn(service.getApplicationContext());
+ service.disableSelf();
+ }
+
+ private static void watchScreenOn(Context app) {
+ if (screenOn != null) return;
+ BroadcastReceiver receiver = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ if (screenOn != this) return;
+ screenOn = null;
+ try {
+ app.unregisterReceiver(this);
+ } catch (RuntimeException ignored) {
+ }
+ ensureHomeKey(app);
+ }
+ };
+ screenOn = receiver;
+ app.registerReceiver(receiver, new IntentFilter(Intent.ACTION_SCREEN_ON));
+ }
+}
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/GuardRevival.java b/launcher/app/src/main/java/com/gathin/tvhome/GuardRevival.java
new file mode 100644
index 000000000..e80797611
--- /dev/null
+++ b/launcher/app/src/main/java/com/gathin/tvhome/GuardRevival.java
@@ -0,0 +1,56 @@
+package com.gathin.tvhome;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.os.UserManager;
+import android.service.notification.NotificationListenerService;
+
+/**
+ * Starts this launcher's process after the firmware has killed it.
+ *
+ * A notification listener keeps its approval across a force stop, and the system binds it again
+ * once the process is gone. That rebind is what brings Home back on TCL and Xiaomi. No notification
+ * is read. Enable with {@code cmd notification allow_listener com.gathin.tvhome/.GuardRevival}.
+ */
+public class GuardRevival extends NotificationListenerService {
+ private BroadcastReceiver unlocked;
+
+ @Override
+ public void onListenerConnected() {
+ UserManager users = getSystemService(UserManager.class);
+ if (users != null && !users.isUserUnlocked()) {
+ if (unlocked != null) return;
+ unlocked = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ stopWaiting();
+ GuardKeep.ensureHomeKey(GuardRevival.this);
+ }
+ };
+ registerReceiver(unlocked, new IntentFilter(Intent.ACTION_USER_UNLOCKED));
+ if (users.isUserUnlocked()) {
+ stopWaiting();
+ GuardKeep.ensureHomeKey(this);
+ }
+ return;
+ }
+ GuardKeep.ensureHomeKey(this);
+ }
+
+ private void stopWaiting() {
+ if (unlocked == null) return;
+ try {
+ unregisterReceiver(unlocked);
+ } catch (RuntimeException ignored) {
+ }
+ unlocked = null;
+ }
+
+ @Override
+ public void onDestroy() {
+ stopWaiting();
+ super.onDestroy();
+ }
+}
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java b/launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java
index f4f1e21cf..af4229119 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java
@@ -10,6 +10,7 @@ import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
+import android.os.PowerManager;
import android.os.SystemClock;
import android.os.UserManager;
import android.content.pm.PackageManager;
@@ -60,6 +61,10 @@ public class HomeKeyService extends AccessibilityService {
private final Set<String> stockHome = new HashSet<>();
/** Stops key filtering while a game controller is connected; null unless gamepad_passthrough and key_filter, or before the unlock. */
private GamepadPassthrough gamepads;
+ /** Screen-off receiver while guard_off_in_standby is set; null otherwise. */
+ private BroadcastReceiver standbyExit;
+ /** disableSelf is how this service leaves for standby, not the user turning it off. */
+ private boolean steppingOut;
@Override
protected void onServiceConnected() {
@@ -97,11 +102,38 @@ public class HomeKeyService extends AccessibilityService {
@Override
public void onDestroy() {
stopWaitingForUnlock();
+ if (standbyExit != null) {
+ try {
+ unregisterReceiver(standbyExit);
+ } catch (RuntimeException ignored) {
+ }
+ standbyExit = null;
+ }
if (gamepads != null) gamepads.stop();
if (consumeVolumeKeys) VolumeKeys.stop();
super.onDestroy();
}
+ /** Some TCL boards never wake from the remote while this helper stays bound with the screen off. */
+ private void armStandbyExit() {
+ if (standbyExit != null || !GuardKeep.offInStandby(this)) return;
+ standbyExit = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ if (!Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) return;
+ leaveForStandby();
+ }
+ };
+ registerReceiver(standbyExit, new IntentFilter(Intent.ACTION_SCREEN_OFF));
+ PowerManager power = getSystemService(PowerManager.class);
+ if (power != null && !power.isInteractive()) leaveForStandby();
+ }
+
+ private void leaveForStandby() {
+ steppingOut = true;
+ GuardKeep.stepOut(this);
+ }
+
private void loadConfig() {
// Keycodes and the panel choice can also come from a theme; theme.sh restarts this service.
Config config = Config.load(this);
@@ -129,6 +161,7 @@ public class HomeKeyService extends AccessibilityService {
setServiceInfo(info);
}
if (!stockHome.isEmpty()) probeAtBoot();
+ armStandbyExit();
holdAppKeys = RemoteKeys.parseHoldAppKeys(config.appKeys);
holdAppScancodes = RemoteKeys.parseHoldAppScancodes(config.appKeys);
// gamepad_passthrough, off by default. Only with key_filter on: off, the service never filters keys.
@@ -184,7 +217,7 @@ public class HomeKeyService extends AccessibilityService {
/** Unbound by Android: the user turned the helper off, so Home doesn't put it back (ServiceRepair). */
@Override
public boolean onUnbind(Intent intent) {
- if (repairRecord) ServiceRepair.disconnected(this, getClass());
+ if (repairRecord && !steppingOut) ServiceRepair.disconnected(this, getClass());
return super.onUnbind(intent);
}