Skip to content

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

ServiceRepair: check the list before each write of the cycle

Nathanfa7279e9b657
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/ServiceRepair.java b/launcher/app/src/main/java/com/gathin/tvhome/ServiceRepair.java
index 3fb726946..8ee8afd84 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/ServiceRepair.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/ServiceRepair.java
@@ -10,6 +10,7 @@ import android.provider.Settings;
import java.util.ArrayList;
import java.util.List;
+import java.util.Objects;
/**
* Android takes an app's accessibility services off the enabled list when the app is force-stopped (Settings, a
@@ -71,16 +72,18 @@ final class ServiceRepair {
// switch falls back to 0 (Hisense songshan, Android 11, whose standby kills third-party apps). Android
// drops the crashed mark when it sees the helper leave the enabled list, so add, remove, add: after a
// force stop the list is already empty, and a removal that changes nothing is not seen. Off the main
- // thread, so Home does not wait for the pauses.
+ // thread, so Home does not wait for the pauses. Each write first checks the list still reads what the
+ // previous step left, and the cycle stops otherwise: a service turned on or off during a pause stays
+ // that way instead of being overwritten by a snapshot.
final String without = others.toString();
final String with = updated.toString();
new Thread(() -> {
try {
- Settings.Secure.putString(cr, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, with);
+ if (!replaceList(cr, list, with)) return;
Thread.sleep(1000);
- Settings.Secure.putString(cr, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, without);
+ if (!replaceList(cr, with, without)) return;
Thread.sleep(1000);
- Settings.Secure.putString(cr, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, with);
+ if (!replaceList(cr, without, with)) return;
Settings.Secure.putInt(cr, Settings.Secure.ACCESSIBILITY_ENABLED, 1);
} catch (RuntimeException | InterruptedException ignored) {
// The setting is the system's; a refusal leaves the helper off, as before.
@@ -91,6 +94,13 @@ final class ServiceRepair {
}
}
+ /** Writes the enabled list only while it still reads {@code expected}; an absent list counts as empty. */
+ private static boolean replaceList(ContentResolver cr, String expected, String updated) {
+ String now = Settings.Secure.getString(cr, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
+ return Objects.equals(now == null ? "" : now, expected == null ? "" : expected)
+ && Settings.Secure.putString(cr, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, updated);
+ }
+
private static SharedPreferences prefs(Context context) {
return context.getSharedPreferences(PREFS, Context.MODE_PRIVATE);
}