Skip to content

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

launcher: put the remote's accessibility helpers back after a force stop

Cristian Rojasc806f04beb74
diff --git a/docs/TROUBLESHOOTING.md b/docs/TROUBLESHOOTING.md
index 523e3a37b..3352ef113 100644
--- a/docs/TROUBLESHOOTING.md
+++ b/docs/TROUBLESHOOTING.md
@@ -294,6 +294,11 @@ tools/tv.py 'dumpsys accessibility'
| Home button | `com.gathin.tvhome/.HomeKeyService` | `KEYCODE_ALL_APPS` (not an ordinary `KEYCODE_HOME`) |
| Input button | `com.gathin.tvhome/.InputKeyService` | `KEYCODE_TV`, `KEYCODE_TV_INPUT`, plus the keycodes in `input_key_keycodes` and `app_keys` (`config.xml` or a theme) |
+A force stop of the launcher (Settings > Apps, a cleaner app, `am force-stop`) takes both helpers off
+that list. When the installer enabled a helper it also granted `WRITE_SECURE_SETTINGS`, and Home puts
+the helpers that were running back on the list the next time it starts; a helper turned off in
+Settings stays off.
+
- **Enabled** means the component is in that user's colon-separated list (the class may be written in
full, `com.gathin.tvhome/com.gathin.tvhome.HomeKeyService`). Match the whole component.
- **Bound** means it appears under that user's `Bound services` section of the dump (`services` on older
diff --git a/launcher/app/src/main/AndroidManifest.xml b/launcher/app/src/main/AndroidManifest.xml
index 7dc9ee681..48c919343 100644
--- a/launcher/app/src/main/AndroidManifest.xml
+++ b/launcher/app/src/main/AndroidManifest.xml
@@ -14,6 +14,10 @@
FINE" rule does not apply here. -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
tools:ignore="CoarseFineLocation" />
+ <!-- Puts the remote's accessibility helpers back after a force stop of this app took them off the enabled
+ list (ServiceRepair). Granted with pm grant by build-and-install when a helper is enabled. -->
+ <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"
+ tools:ignore="ProtectedPermissions" />
<!-- Android 11+ package visibility: lets the launcher list the TV apps it shows as tiles, and the
apps that declare a module card (docs/specs/card.md). -->
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 538ae432a..bba78012d 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java
@@ -31,6 +31,7 @@ public class HomeKeyService extends AccessibilityService {
@Override
protected void onServiceConnected() {
super.onServiceConnected();
+ ServiceRepair.connected(this, getClass());
settingsScancodes = getResources().getIntArray(R.array.settings_key_scancodes);
// Keycodes and the panel choice can also come from a theme; theme.sh restarts this service.
Config config = Config.load(this);
@@ -80,6 +81,13 @@ public class HomeKeyService extends AccessibilityService {
return true;
}
+ /** Unbound by Android: the user turned the helper off, so Home doesn't put it back (ServiceRepair). */
+ @Override
+ public boolean onUnbind(Intent intent) {
+ ServiceRepair.disconnected(this, getClass());
+ return super.onUnbind(intent);
+ }
+
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
}
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/InputKeyService.java b/launcher/app/src/main/java/com/gathin/tvhome/InputKeyService.java
index cdfc24d01..de691f0d9 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/InputKeyService.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/InputKeyService.java
@@ -27,6 +27,7 @@ public class InputKeyService extends AccessibilityService {
@Override
protected void onServiceConnected() {
super.onServiceConnected();
+ ServiceRepair.connected(this, getClass());
// From config.xml or a theme's config.json; theme.sh rebinds this service after a push.
Config config = Config.load(this);
inputKeycodes = config.inputKeycodes;
@@ -63,6 +64,13 @@ public class InputKeyService extends AccessibilityService {
return true;
}
+ /** Unbound by Android: the user turned the helper off, so Home doesn't put it back (ServiceRepair). */
+ @Override
+ public boolean onUnbind(Intent intent) {
+ ServiceRepair.disconnected(this, getClass());
+ return super.onUnbind(intent);
+ }
+
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
}
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 e89977e15..e1ddd2e9e 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/MainActivity.java
@@ -305,6 +305,8 @@ public class MainActivity extends Activity implements AppAdapter.TileListener {
density = getResources().getDisplayMetrics().density;
// config.xml, overlaid by config.json from a pushed theme (see Config and tools/theme.sh).
Config cfg = Config.load(this);
+ // After a force stop, put back the remote's accessibility helpers that Android took off the list.
+ ServiceRepair.restore(this);
settingsKeycodes = cfg.settingsKeycodes;
settingsKeyPanel = cfg.settingsKeyPanel;
COLUMNS = cfg.columns;
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/ServiceRepair.java b/launcher/app/src/main/java/com/gathin/tvhome/ServiceRepair.java
new file mode 100644
index 000000000..c04f6123d
--- /dev/null
+++ b/launcher/app/src/main/java/com/gathin/tvhome/ServiceRepair.java
@@ -0,0 +1,68 @@
+package com.gathin.tvhome;
+
+import android.Manifest;
+import android.content.ComponentName;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.content.pm.PackageManager;
+import android.provider.Settings;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Android takes an app's accessibility services off the enabled list when the app is force-stopped (Settings, a
+ * cleaner app, or am force-stop), and the remote's Home, Input and app buttons then stop working until the helper
+ * is turned on again over ADB. A running helper records itself here and drops the record when Android unbinds it
+ * (the user turned it off); a force stop kills the process before that, so the record stays. Home starting again
+ * puts recorded helpers back on the list, when the installer granted WRITE_SECURE_SETTINGS.
+ */
+final class ServiceRepair {
+ private static final String PREFS = "accessibility_helpers";
+
+ private ServiceRepair() {
+ }
+
+ static void connected(Context context, Class<?> service) {
+ prefs(context).edit().putBoolean(service.getName(), true).apply();
+ }
+
+ static void disconnected(Context context, Class<?> service) {
+ prefs(context).edit().remove(service.getName()).apply();
+ }
+
+ static void restore(Context context) {
+ if (context.checkSelfPermission(Manifest.permission.WRITE_SECURE_SETTINGS)
+ != PackageManager.PERMISSION_GRANTED) return;
+ try {
+ ContentResolver cr = context.getContentResolver();
+ String list = Settings.Secure.getString(cr, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
+ List<ComponentName> enabled = new ArrayList<>();
+ if (list != null) {
+ for (String entry : list.split(":")) {
+ ComponentName name = ComponentName.unflattenFromString(entry);
+ if (name != null) enabled.add(name);
+ }
+ }
+ StringBuilder updated = new StringBuilder(list == null || list.equals("null") ? "" : list);
+ boolean missing = false;
+ for (String service : prefs(context).getAll().keySet()) {
+ ComponentName name = new ComponentName(context.getPackageName(), service);
+ if (enabled.contains(name)) continue;
+ if (updated.length() > 0) updated.append(':');
+ updated.append(name.flattenToShortString());
+ missing = true;
+ }
+ if (!missing) return;
+ Settings.Secure.putString(cr, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, updated.toString());
+ Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_ENABLED, 1);
+ } catch (RuntimeException ignored) {
+ // The setting is the system's; a refusal leaves the helper off, as before.
+ }
+ }
+
+ private static SharedPreferences prefs(Context context) {
+ return context.getSharedPreferences(PREFS, Context.MODE_PRIVATE);
+ }
+}
diff --git a/launcher/build-and-install.ps1 b/launcher/build-and-install.ps1
index 46d578b37..08ae3019e 100644
--- a/launcher/build-and-install.ps1
+++ b/launcher/build-and-install.ps1
@@ -95,7 +95,7 @@ function Enable-Service([string]$Name) {
$svc = "com.gathin.tvhome/.$Name"
# Through stdin: Windows PowerShell 5.1 drops the inner double quotes from a native argument, and the
# TV's shell then fails with "no closing quote" while the service stays off.
- ('s=$(settings get secure enabled_accessibility_services); case "$s" in *' + $Name + '*) ;; null|"") settings put secure enabled_accessibility_services ' + $svc + ' ;; *) settings put secure enabled_accessibility_services "$s:' + $svc + '" ;; esac; settings put secure accessibility_enabled 1; echo ' + $Name + ' enabled') |
+ ('s=$(settings get secure enabled_accessibility_services); case "$s" in *' + $Name + '*) ;; null|"") settings put secure enabled_accessibility_services ' + $svc + ' ;; *) settings put secure enabled_accessibility_services "$s:' + $svc + '" ;; esac; settings put secure accessibility_enabled 1; pm grant com.gathin.tvhome android.permission.WRITE_SECURE_SETTINGS 2>/dev/null; echo ' + $Name + ' enabled') |
& $py -I ..\tools\tv.py --batch
}
if ($InputKeyService) { Enable-Service 'InputKeyService' }
diff --git a/launcher/build-and-install.sh b/launcher/build-and-install.sh
index 3ace95d0a..e5e6f3844 100755
--- a/launcher/build-and-install.sh
+++ b/launcher/build-and-install.sh
@@ -187,7 +187,7 @@ esac
# sticks: the key is routed to the launcher (HomeKeyService)
enable_service() { # appends to enabled_accessibility_services without dropping other services
svc=com.gathin.tvhome/.$1
- "$PY" -I ../tools/tv.py "s=\$(settings get secure enabled_accessibility_services); case \"\$s\" in *$1*) ;; null|'') settings put secure enabled_accessibility_services $svc ;; *) settings put secure enabled_accessibility_services \"\$s:$svc\" ;; esac; settings put secure accessibility_enabled 1; echo $1 enabled"
+ "$PY" -I ../tools/tv.py "s=\$(settings get secure enabled_accessibility_services); case \"\$s\" in *$1*) ;; null|'') settings put secure enabled_accessibility_services $svc ;; *) settings put secure enabled_accessibility_services \"\$s:$svc\" ;; esac; settings put secure accessibility_enabled 1; pm grant com.gathin.tvhome android.permission.WRITE_SECURE_SETTINGS 2>/dev/null; echo $1 enabled"
}
if [ "${INPUT_KEY_SERVICE:-0}" = 1 ]; then enable_service InputKeyService; fi
if [ "${HOME_KEY_SERVICE:-0}" = 1 ]; then enable_service HomeKeyService; fi