Skip to content

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

Launcher: stock_home_redirect covers a stock home already on screen at boot, not an HDMI input (#1116)

William Huang54263b6f06b5
diff --git a/launcher/app/src/main/AndroidManifest.xml b/launcher/app/src/main/AndroidManifest.xml
index 4cee7a6b2..4fab3b2c9 100644
--- a/launcher/app/src/main/AndroidManifest.xml
+++ b/launcher/app/src/main/AndroidManifest.xml
@@ -156,6 +156,16 @@
android:noHistory="true"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" />
+ <!-- stock_home_redirect at boot: HomeKeyService opens this invisible activity once, so the screen behind
+ it sends a window event when it closes (HomeKeyService.probeAtBoot). -->
+ <activity
+ android:name=".ProbeActivity"
+ android:exported="false"
+ android:excludeFromRecents="true"
+ android:noHistory="true"
+ android:taskAffinity=""
+ android:theme="@android:style/Theme.Translucent.NoTitleBar" />
+
<!-- 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
index 41c74a99e..33ce5caf2 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/HomeKeyService.java
@@ -7,13 +7,16 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
+import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
+import android.os.SystemClock;
import android.os.UserManager;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.provider.Settings;
import android.view.KeyEvent;
+import android.view.ViewConfiguration;
import android.view.accessibility.AccessibilityEvent;
import android.widget.Toast;
@@ -118,6 +121,7 @@ public class HomeKeyService extends AccessibilityService {
info.eventTypes |= AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
setServiceInfo(info);
}
+ if (!stockHome.isEmpty()) probeAtBoot();
holdAppKeys = RemoteKeys.parseHoldAppKeys(config.appKeys);
holdAppScancodes = RemoteKeys.parseHoldAppScancodes(config.appKeys);
}
@@ -179,23 +183,49 @@ public class HomeKeyService extends AccessibilityService {
if (cls != null && stockHome.contains(cls.toString())) openHome();
}
+ /** How long after boot the first bind still counts as "at boot" for probeAtBoot. */
+ static final long BOOT_WINDOW_MS = 3 * 60 * 1000L;
+
+ /** stock_home_redirect at boot: the stock home is often on screen before Android binds this service, so no
+ * window event for it arrives (#1116). Once per boot, open ProbeActivity: when it closes, whatever was in
+ * front comes back and sends a window event, and onAccessibilityEvent covers it only if it is the stock home.
+ * An HDMI input or an app that the TV resumed at boot is left alone. */
+ private void probeAtBoot() {
+ int boot = Settings.Global.getInt(getContentResolver(), Settings.Global.BOOT_COUNT, -1);
+ SharedPreferences prefs = getSharedPreferences("boot", MODE_PRIVATE);
+ if (!firstBindAfterBoot(boot, prefs.getInt("probe_boot", -1), SystemClock.elapsedRealtime())) return;
+ prefs.edit().putInt("probe_boot", boot).apply();
+ try {
+ startActivity(new Intent(this, ProbeActivity.class)
+ .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION));
+ } catch (ActivityNotFoundException | SecurityException ignored) {
+ }
+ }
+
+ static boolean firstBindAfterBoot(int boot, int lastBoot, long uptimeMs) {
+ return boot >= 0 && boot != lastBoot && uptimeMs < BOOT_WINDOW_MS;
+ }
+
/** Lets the system see the key, so a long press still does what the firmware gives it. */
private static final long HOME_KEY_DELAY_MS = 250;
private final Handler handler = new Handler(Looper.getMainLooper());
- private boolean homeLongPress;
/** stock_home_redirect on the Home key: when the stock home is already on screen (it came up at boot, before
* Android bound this service), Home brings it up again with no window change, so the redirect above never
* fires and the TV stays on the stock home until another app opens (#1116). A short press opens this
- * launcher shortly after the system has handled the key; a long press is left alone. */
+ * launcher shortly after the system has handled the key; a long press (Google TV's quick settings) is left
+ * alone. Held time, not repeats: key repeats are made after accessibility filtering, so none arrive here. */
private void redirectHomeKey(KeyEvent event) {
- if (event.getAction() == KeyEvent.ACTION_DOWN) {
- homeLongPress = event.getRepeatCount() > 0;
- } else if (event.getAction() == KeyEvent.ACTION_UP && !event.isCanceled() && !homeLongPress) {
+ if (event.getAction() == KeyEvent.ACTION_UP && !event.isCanceled()
+ && isShortPress(event.getDownTime(), event.getEventTime(), ViewConfiguration.getLongPressTimeout())) {
handler.postDelayed(this::openHome, HOME_KEY_DELAY_MS);
}
}
+ static boolean isShortPress(long downTime, long upTime, long longPressTimeout) {
+ return upTime - downTime < longPressTimeout;
+ }
+
private void openHome() {
startActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME).setPackage(getPackageName())
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED));
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/ProbeActivity.java b/launcher/app/src/main/java/com/gathin/tvhome/ProbeActivity.java
new file mode 100644
index 000000000..af79bd4cb
--- /dev/null
+++ b/launcher/app/src/main/java/com/gathin/tvhome/ProbeActivity.java
@@ -0,0 +1,22 @@
+package com.gathin.tvhome;
+
+import android.app.Activity;
+import android.os.Bundle;
+
+/**
+ * An invisible activity that closes right away. HomeKeyService opens it once at boot (stock_home_redirect):
+ * whatever was on screen comes back to the front when it closes and sends a window event, so the service can
+ * tell the stock home from an HDMI input without reading the screen (#1116).
+ */
+public class ProbeActivity extends Activity {
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ getWindow().getDecorView().post(this::close);
+ }
+
+ private void close() {
+ finish();
+ overridePendingTransition(0, 0);
+ }
+}
diff --git a/launcher/app/src/test/java/com/gathin/tvhome/HomeKeyServiceTest.java b/launcher/app/src/test/java/com/gathin/tvhome/HomeKeyServiceTest.java
new file mode 100644
index 000000000..3b5700da5
--- /dev/null
+++ b/launcher/app/src/test/java/com/gathin/tvhome/HomeKeyServiceTest.java
@@ -0,0 +1,35 @@
+package com.gathin.tvhome;
+
+import org.junit.Test;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class HomeKeyServiceTest {
+ private static final long SOON = 45_000L;
+
+ @Test public void aTapIsAShortPress() {
+ assertTrue(HomeKeyService.isShortPress(1_000, 1_120, 400));
+ }
+
+ @Test public void holdingPastTheTimeoutIsALongPress() {
+ assertFalse(HomeKeyService.isShortPress(1_000, 1_400, 400));
+ assertFalse(HomeKeyService.isShortPress(1_000, 3_000, 400));
+ }
+
+ @Test public void firstBindOfANewBootProbes() {
+ assertTrue(HomeKeyService.firstBindAfterBoot(12, 11, SOON));
+ assertTrue(HomeKeyService.firstBindAfterBoot(1, -1, SOON));
+ }
+
+ @Test public void aRebindInTheSameBootDoesNot() {
+ assertFalse(HomeKeyService.firstBindAfterBoot(12, 12, SOON));
+ }
+
+ @Test public void aBindLongAfterBootDoesNot() {
+ assertFalse(HomeKeyService.firstBindAfterBoot(12, 11, HomeKeyService.BOOT_WINDOW_MS));
+ }
+
+ @Test public void noBootCountMeansNoProbe() {
+ assertFalse(HomeKeyService.firstBindAfterBoot(-1, -1, SOON));
+ }
+}