Companion source files, build script, and recovery setup for remapping hardcoded remote buttons on an Android TV.
Article walkthrough: https://vishaltelangre.com/articles/debloating-android-tv-and-remapping-hardcoded-buttons-on-the-remote/
Companion source files, build script, and recovery setup for remapping hardcoded remote buttons on an Android TV.
Article walkthrough: https://vishaltelangre.com/articles/debloating-android-tv-and-remapping-hardcoded-buttons-on-the-remote/
| <?xml version="1.0" encoding="utf-8"?> | |
| <accessibility-service xmlns:android="http://schemas.android.com/apk/res/android" | |
| android:description="@string/service_desc" | |
| android:accessibilityEventTypes="typeWindowStateChanged" | |
| android:accessibilityFeedbackType="feedbackGeneric" | |
| android:notificationTimeout="20" | |
| android:canRequestFilterKeyEvents="true" | |
| android:accessibilityFlags="flagRequestFilterKeyEvents" /> |
| <?xml version="1.0" encoding="utf-8"?> | |
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
| package="com.vishal.remotehook"> | |
| <uses-sdk | |
| android:minSdkVersion="26" | |
| android:targetSdkVersion="34" /> | |
| <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" /> | |
| <queries> | |
| <package android:name="org.smarttube.beta" /> | |
| <package android:name="com.stremio.one" /> | |
| <package android:name="com.google.android.youtube.tv" /> | |
| <package android:name="com.netflix.ninja" /> | |
| </queries> | |
| <application | |
| android:label="@string/app_name" | |
| android:allowBackup="true" | |
| android:supportsRtl="true"> | |
| <service | |
| android:name=".RemoteHookService" | |
| android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" | |
| android:exported="true"> | |
| <intent-filter> | |
| <action android:name="android.accessibilityservice.AccessibilityService" /> | |
| </intent-filter> | |
| <meta-data | |
| android:name="android.accessibilityservice" | |
| android:resource="@xml/accessibility_service_config" /> | |
| </service> | |
| </application> | |
| </manifest> |
| #!/usr/bin/env bash | |
| set -e | |
| TV_IP="${1:-100.x.y.z:5555}" | |
| ANDROID_HOME="${ANDROID_HOME:-$HOME/Library/Android/sdk}" | |
| BUILD_TOOLS="${BUILD_TOOLS:-$(find "$ANDROID_HOME/build-tools" -maxdepth 1 -type d | sort -V | tail -n 1)}" | |
| PLATFORM="${PLATFORM:-$(find "$ANDROID_HOME/platforms" -name "android.jar" | sort -V | tail -n 1)}" | |
| JAVAC="${JAVAC:-javac}" | |
| echo "Using Build Tools: $BUILD_TOOLS" | |
| echo "Using Platform: $PLATFORM" | |
| rm -rf build | |
| mkdir -p build/classes build/dex res/values res/xml | |
| cp strings.xml res/values/strings.xml | |
| cp accessibility_service_config.xml res/xml/accessibility_service_config.xml | |
| "$BUILD_TOOLS/aapt2" compile --dir res -o build/resources.zip | |
| "$BUILD_TOOLS/aapt2" link -I "$PLATFORM" --manifest AndroidManifest.xml -o build/unaligned.apk build/resources.zip --java src | |
| "$JAVAC" -cp "$PLATFORM" -d build/classes src/com/vishal/remotehook/RemoteHookService.java | |
| "$BUILD_TOOLS/d8" build/classes/com/vishal/remotehook/*.class --lib "$PLATFORM" --output build/dex | |
| cd build/dex && zip -u ../unaligned.apk classes.dex && cd ../.. | |
| "$BUILD_TOOLS/zipalign" -p -f 4 build/unaligned.apk RemoteHook.apk | |
| # Generate debug keystore if not exists | |
| if [ ! -f "$HOME/.android/debug.keystore" ]; then | |
| mkdir -p "$HOME/.android" | |
| keytool -genkey -v -keystore "$HOME/.android/debug.keystore" -storepass android -alias androiddebugkey -keypass android -keyalg RSA -keysize 2048 -validity 10000 -dname "CN=Android Debug,O=Android,C=US" | |
| fi | |
| "$BUILD_TOOLS/apksigner" sign --ks "$HOME/.android/debug.keystore" --ks-pass pass:android RemoteHook.apk | |
| echo "Built RemoteHook.apk successfully!" | |
| if [ -n "$1" ]; then | |
| echo "Installing on $TV_IP..." | |
| adb -s "$TV_IP" install -r RemoteHook.apk | |
| fi |
| package com.vishal.remotehook; | |
| import android.accessibilityservice.AccessibilityService; | |
| import android.content.Intent; | |
| import android.content.pm.PackageManager; | |
| import android.view.KeyEvent; | |
| import android.view.accessibility.AccessibilityEvent; | |
| import android.util.Log; | |
| public class RemoteHookService extends AccessibilityService { | |
| private static final String TAG = "RemoteHook"; | |
| private boolean isSmartTubeActive = false; | |
| private boolean isStremioActive = false; | |
| private long lastRedirectTime = 0; | |
| @Override | |
| public void onAccessibilityEvent(AccessibilityEvent event) { | |
| if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { | |
| CharSequence pkg = event.getPackageName(); | |
| if (pkg == null) return; | |
| String pkgName = pkg.toString(); | |
| // Track target apps | |
| if ("org.smarttube.beta".equals(pkgName)) { | |
| isSmartTubeActive = true; | |
| isStremioActive = false; | |
| return; | |
| } else if ("com.stremio.one".equals(pkgName)) { | |
| isStremioActive = true; | |
| isSmartTubeActive = false; | |
| return; | |
| } | |
| // If official YouTube appears | |
| if ("com.google.android.youtube.tv".equals(pkgName)) { | |
| long now = System.currentTimeMillis(); | |
| if (now - lastRedirectTime < 1500) { | |
| return; | |
| } | |
| if (isSmartTubeActive) { | |
| Log.d(TAG, "SmartTube active -> Toggling to Home"); | |
| isSmartTubeActive = false; | |
| lastRedirectTime = now; | |
| launchHome(); | |
| return; | |
| } | |
| Log.d(TAG, "YouTube triggered -> Opening SmartTube"); | |
| lastRedirectTime = now; | |
| launchApp("org.smarttube.beta"); | |
| return; | |
| } | |
| // If official Netflix appears | |
| if ("com.netflix.ninja".equals(pkgName)) { | |
| long now = System.currentTimeMillis(); | |
| if (now - lastRedirectTime < 1500) { | |
| return; | |
| } | |
| if (isStremioActive) { | |
| Log.d(TAG, "Stremio active -> Toggling to Home"); | |
| isStremioActive = false; | |
| lastRedirectTime = now; | |
| launchHome(); | |
| return; | |
| } | |
| Log.d(TAG, "Netflix triggered -> Opening Stremio"); | |
| lastRedirectTime = now; | |
| launchApp("com.stremio.one"); | |
| return; | |
| } | |
| // If stock Google TV launcher tries to display, redirect to Projectivy | |
| if ("com.google.android.apps.tv.launcherx".equals(pkgName)) { | |
| Log.d(TAG, "Stock Google TV launcher detected -> Redirecting to Projectivy"); | |
| launchHome(); | |
| return; | |
| } | |
| // Any other app (e.g. Projectivy, Settings) resets state | |
| isSmartTubeActive = false; | |
| isStremioActive = false; | |
| } | |
| } | |
| @Override | |
| protected boolean onKeyEvent(KeyEvent event) { | |
| int keyCode = event.getKeyCode(); | |
| if (event.getAction() == KeyEvent.ACTION_UP) { | |
| long now = System.currentTimeMillis(); | |
| if (keyCode == 2048 || keyCode == KeyEvent.KEYCODE_BUTTON_3) { | |
| if (now - lastRedirectTime < 1500) return true; | |
| lastRedirectTime = now; | |
| if (isSmartTubeActive) { | |
| isSmartTubeActive = false; | |
| launchHome(); | |
| } else { | |
| launchApp("org.smarttube.beta"); | |
| } | |
| return true; | |
| } else if (keyCode == 2030 || keyCode == 191) { | |
| if (now - lastRedirectTime < 1500) return true; | |
| lastRedirectTime = now; | |
| if (isStremioActive) { | |
| isStremioActive = false; | |
| launchHome(); | |
| } else { | |
| launchApp("com.stremio.one"); | |
| } | |
| return true; | |
| } | |
| } | |
| return super.onKeyEvent(event); | |
| } | |
| private void launchHome() { | |
| launchApp("com.spocky.projengmenu"); | |
| } | |
| private void launchApp(String targetPkg) { | |
| try { | |
| PackageManager pm = getPackageManager(); | |
| Intent intent = pm.getLeanbackLaunchIntentForPackage(targetPkg); | |
| if (intent == null) { | |
| intent = pm.getLaunchIntentForPackage(targetPkg); | |
| } | |
| if (intent != null) { | |
| intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); | |
| startActivity(intent); | |
| Log.d(TAG, "Successfully launched " + targetPkg); | |
| } | |
| } catch (Exception e) { | |
| Log.e(TAG, "Error launching " + targetPkg, e); | |
| } | |
| } | |
| @Override | |
| public void onInterrupt() { | |
| Log.d(TAG, "Service interrupted"); | |
| } | |
| } |
| #!/usr/bin/env bash | |
| set -e | |
| TV_IP="${1:-100.x.y.z:5555}" | |
| if [[ "$TV_IP" == *"100.x.y.z"* ]]; then | |
| echo "Usage: ./setup.sh <TV_IP:PORT> (e.g. ./setup.sh 100.1.2.3:5555)" | |
| exit 1 | |
| fi | |
| echo "Connecting to TV at $TV_IP..." | |
| adb connect "$TV_IP" || true | |
| D="-s $TV_IP" | |
| echo "==> Ensuring required system packages are enabled for Realtek button router..." | |
| adb $D shell pm enable com.google.android.youtube.tv || true | |
| adb $D shell pm enable com.google.android.apps.tv.launcherx || true | |
| adb $D shell pm enable com.realtek.android.globalkeyhandler || true | |
| echo "==> Debloating unused Google TV services..." | |
| for p in com.google.android.videos \ | |
| com.google.android.play.games \ | |
| com.google.android.youtube.tvmusic \ | |
| com.google.android.feedback \ | |
| com.google.android.partnersetup \ | |
| com.google.android.tungsten.setupwraith; do | |
| adb $D shell pm disable-user --user 0 "$p" 2>/dev/null || true | |
| done | |
| echo "==> Setting animation speeds to 0.75x..." | |
| for k in window_animation_scale transition_animation_scale animator_duration_scale; do | |
| adb $D shell settings put global "$k" 0.75 | |
| done | |
| echo "==> Setting Projectivy as default Home Activity..." | |
| adb $D shell cmd package set-home-activity com.spocky.projengmenu/.ui.home.MainActivity || true | |
| echo "==> Configuring Remote Hook permissions and Accessibility Services..." | |
| adb $D shell dumpsys deviceidle whitelist +com.vishal.remotehook || true | |
| SERVICES="com.vishal.remotehook/.RemoteHookService:com.spocky.projengmenu/.services.ProjectivyAccessibilityService" | |
| adb $D shell settings put secure enabled_accessibility_services "$SERVICES" | |
| adb $D shell settings put secure accessibility_enabled 1 | |
| echo "==> Done! Remote Hook and Projectivy are active." |
| <?xml version="1.0" encoding="utf-8"?> | |
| <resources> | |
| <string name="app_name">Remote Hook</string> | |
| <string name="service_desc">Remaps YouTube to SmartTube and Netflix to Stremio</string> | |
| </resources> |