From ec44cb96841c9eb17bb2e7f58dba341c1f1bff27 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 20 Oct 2026 20:14:49 +0530 Subject: [PATCH 1/3] feat(macos): test opening inputs by app link on Android TVs The Android TV remote service has no input-source key, but it can ask the TV to open links (RemoteAppLinkLaunchRequest), and the MOOR2's Live TV app switches inputs on content://android.media.tv/passthrough links (confirmed with adb, #6) and opens its input menu on the standard com.android.tv.action.VIEW_INPUTS action. On those TVs Input source now opens a panel to test: the input menu via an intent: link (by action, or Xiaomi's activity), HDMI 1-3 directly, and Settings as before. Adds the app-link message, checked against the reference protobuf, and the end-to-end test sends a link to the fake TV. Refs #6 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01YS6HGULeQFiqj9SZfZ7Gvv --- apps/macos/Sources/MiRemote/ATVProtocol.swift | 6 + .../macos/Sources/MiRemote/RemoteClient.swift | 6 + apps/macos/Sources/MiRemote/RemoteView.swift | 113 +++++++++++++++++- .../MiRemoteTests/ATVProtocolTests.swift | 1 + .../Tests/MiRemoteTests/FakeTVTests.swift | 7 ++ apps/macos/Tests/fake_tv.py | 5 + 6 files changed, 134 insertions(+), 4 deletions(-) diff --git a/apps/macos/Sources/MiRemote/ATVProtocol.swift b/apps/macos/Sources/MiRemote/ATVProtocol.swift index f01e3ba..8d03676 100644 --- a/apps/macos/Sources/MiRemote/ATVProtocol.swift +++ b/apps/macos/Sources/MiRemote/ATVProtocol.swift @@ -212,6 +212,12 @@ enum RemoteMessage { Proto.bytes(10, Proto.int(1, Int64(code)) + Proto.int(2, direction.rawValue)) } + /// Asks the TV to open a link (RemoteAppLinkLaunchRequest): a deep link, a TV-input link such + /// as content://android.media.tv/passthrough/, or possibly an intent: URI. + static func appLink(_ link: String) -> [UInt8] { + Proto.bytes(90, Proto.string(1, link)) + } + /// Puts `text` into the focused text field on the TV, cursor after it. The counters echo the /// TV's latest (0 before it has sent any), as the Google TV app and other clients do. static func imeBatchEdit(imeCounter: Int32, fieldCounter: Int32, text: String) -> [UInt8] { diff --git a/apps/macos/Sources/MiRemote/RemoteClient.swift b/apps/macos/Sources/MiRemote/RemoteClient.swift index d56213a..cde0405 100644 --- a/apps/macos/Sources/MiRemote/RemoteClient.swift +++ b/apps/macos/Sources/MiRemote/RemoteClient.swift @@ -610,6 +610,12 @@ final class RemoteClient: ObservableObject { /// Single press: DOWN followed by UP. func press(_ key: AndroidKey) { press(code: key.rawValue) } + /// Android TV Remote: asks the TV to open `link` (see `RemoteMessage.appLink`). + func openLink(_ link: String) { + guard state == .connected, phase == .atv else { return } + sendDelimited(RemoteMessage.appLink(link)) + } + func press(code: Int32, meta: Int32 = 0) { if phase == .atv { if let code = Self.atvKey(for: code) { sendAtvKey(code) } diff --git a/apps/macos/Sources/MiRemote/RemoteView.swift b/apps/macos/Sources/MiRemote/RemoteView.swift index 0de2cc1..252ad12 100644 --- a/apps/macos/Sources/MiRemote/RemoteView.swift +++ b/apps/macos/Sources/MiRemote/RemoteView.swift @@ -20,6 +20,9 @@ struct RemoteView: View { @StateObject private var discovery = TVDiscovery() @AppStorage("tvHost") private var savedHost = "" @State private var showConnect = false + /// Over Android TV Remote there's no input-source key; this panel offers the ways being + /// tested in #6 (opening the input menu by link, or switching HDMI inputs directly). + @State private var showInputs = false @State private var text = "" @FocusState private var remoteFocused: Bool @FocusState private var textFocused: Bool @@ -89,6 +92,15 @@ struct RemoteView: View { .overlay(RoundedRectangle(cornerRadius: 14, style: .continuous).stroke(Palette.bodyEdge)) .padding(14) } + } else if showInputs { + ZStack { + Color.black.opacity(0.55) + .onTapGesture { showInputs = false } + InputSourceSheet(client: client) { showInputs = false } + .background(RoundedRectangle(cornerRadius: 14, style: .continuous).fill(Palette.body)) + .overlay(RoundedRectangle(cornerRadius: 14, style: .continuous).stroke(Palette.bodyEdge)) + .padding(14) + } } else if client.state == .pairing { // Android TVs without MiLink need a one-time code shown on the TV. ZStack { @@ -146,9 +158,8 @@ struct RemoteView: View { HStack { RoundKey(symbol: "power", tint: Palette.power, help: "Power (P)") { client.press(.power) } Spacer() - RoundKey(symbol: "rectangle.on.rectangle", - help: client.tvProtocol == .atv ? "Input source (opens Settings: this TV has no input key over Wi-Fi)" : "Input source") { - client.press(.tvInput) + RoundKey(symbol: "rectangle.on.rectangle", help: "Input source") { + if client.tvProtocol == .atv { showInputs = true } else { client.press(.tvInput) } } Spacer() RoundKey(symbol: "square.stack", @@ -249,7 +260,7 @@ struct RemoteView: View { // MARK: Keyboard private func handleKey(_ press: KeyPress) -> KeyPress.Result { - if textFocused || press.modifiers.contains(.command) || client.state == .pairing { return .ignored } + if textFocused || press.modifiers.contains(.command) || client.state == .pairing || showInputs { return .ignored } switch press.key { case .upArrow: client.press(.dpadUp) case .downArrow: client.press(.dpadDown) @@ -637,3 +648,97 @@ struct PairSheet: View { } } + +// MARK: - Input source (Android TV Remote) + +/// Ways to reach the TV's inputs over the Android TV remote service, which has no input key +/// (#6). Being tested on a MiTV-MOOR2: its Live TV app opens the input menu on the standard +/// `com.android.tv.action.VIEW_INPUTS` action, and switches inputs on TV-input passthrough links. +enum InputSourceOption: CaseIterable, Identifiable { + case inputMenu + case xiaomiInputMenu + case hdmi1 + case hdmi2 + case hdmi3 + case settings + + var id: Self { self } + + var title: String { + switch self { + case .inputMenu: return "Input menu" + case .xiaomiInputMenu: return "Input menu (Xiaomi)" + case .hdmi1: return "HDMI 1" + case .hdmi2: return "HDMI 2" + case .hdmi3: return "HDMI 3" + case .settings: return "Settings" + } + } + + /// The app link sent to the TV; nil for Settings, which is a key. + var link: String { + switch self { + case .inputMenu: return "intent:#Intent;action=com.android.tv.action.VIEW_INPUTS;end" + case .xiaomiInputMenu: return "intent:#Intent;component=com.mitv.livetv/.input.SelectInputActivity;end" + // MediaTek-based Mi TVs: HDMI port n is hardware input HW(n+3). + case .hdmi1: return Self.passthrough("HW4") + case .hdmi2: return Self.passthrough("HW5") + case .hdmi3: return Self.passthrough("HW6") + case .settings: return "" + } + } + + private static func passthrough(_ hardwareId: String) -> String { + "content://android.media.tv/passthrough/com.mediatek.tvinput%2F.hdmi.HDMIInputService%2F\(hardwareId)" + } +} + +struct InputSourceSheet: View { + @ObservedObject var client: RemoteClient + var onClose: () -> Void + @State private var lastSent: String? + + var body: some View { + VStack(alignment: .leading, spacing: 12) { + Text("Input source") + .font(.headline) + Text("This TV has no input key over Wi-Fi. Try opening the input menu, or switch straight to an HDMI input. (Testing: please tell us in issue #6 which of these work.)") + .font(.callout) + .foregroundStyle(.secondary) + .fixedSize(horizontal: false, vertical: true) + LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: 6), count: 2), spacing: 6) { + ForEach(InputSourceOption.allCases) { option in + Button { + if option == .settings { + client.press(.settings) + lastSent = "Settings key" + } else { + client.openLink(option.link) + lastSent = option.link + } + } label: { + Text(option.title) + .font(.system(size: 12, weight: .semibold)) + .lineLimit(1) + .minimumScaleFactor(0.8) + .frame(maxWidth: .infinity, minHeight: 32) + } + .buttonStyle(KeyStyle(shape: RoundedRectangle(cornerRadius: 8, style: .continuous), fill: Palette.key)) + } + } + if let lastSent { + Text("Sent: \(lastSent)") + .font(.system(size: 9, design: .monospaced)) + .foregroundStyle(.secondary) + .textSelection(.enabled) + .fixedSize(horizontal: false, vertical: true) + } + HStack { + Spacer() + Button("Done", action: onClose) + .keyboardShortcut(.cancelAction) + } + } + .padding(18) + } +} diff --git a/apps/macos/Tests/MiRemoteTests/ATVProtocolTests.swift b/apps/macos/Tests/MiRemoteTests/ATVProtocolTests.swift index a00c87f..b9bcb92 100644 --- a/apps/macos/Tests/MiRemoteTests/ATVProtocolTests.swift +++ b/apps/macos/Tests/MiRemoteTests/ATVProtocolTests.swift @@ -63,6 +63,7 @@ final class ATVProtocolTests: XCTestCase { XCTAssertEqual(RemoteMessage.imeBatchEdit(imeCounter: 3, fieldCounter: 7, text: "héllo"), [170, 1, 22, 8, 3, 16, 7, 26, 16, 8, 1, 18, 12, 8, 5, 16, 5, 26, 6, 104, 195, 169, 108, 108, 111]) XCTAssertEqual(RemoteMessage.configure(features: 615), [10, 50, 8, 231, 4, 18, 45, 10, 9, 77, 105, 32, 82, 101, 109, 111, 116, 101, 18, 9, 77, 105, 32, 82, 101, 109, 111, 116, 101, 24, 1, 34, 1, 49, 42, 9, 97, 116, 118, 114, 101, 109, 111, 116, 101, 50, 5, 49, 46, 48, 46, 48]) XCTAssertEqual(RemoteMessage.wantedFeatures, 615) + XCTAssertEqual(RemoteMessage.appLink("content://x"), [210, 5, 13, 10, 11, 99, 111, 110, 116, 101, 110, 116, 58, 47, 47, 120]) } func testParsesRemoteMessages() throws { diff --git a/apps/macos/Tests/MiRemoteTests/FakeTVTests.swift b/apps/macos/Tests/MiRemoteTests/FakeTVTests.swift index aac2d49..7fe7cfe 100644 --- a/apps/macos/Tests/MiRemoteTests/FakeTVTests.swift +++ b/apps/macos/Tests/MiRemoteTests/FakeTVTests.swift @@ -68,6 +68,13 @@ final class FakeTVTests: XCTestCase { ((try? String(contentsOfFile: keysFile, encoding: .utf8)) ?? "").contains("text 0 0 6 Hello!") } + // Links reach the TV as app-link launch requests. + client.openLink(InputSourceOption.inputMenu.link) + try await waitUntil("the TV to receive the link", client: client) { + ((try? String(contentsOfFile: keysFile, encoding: .utf8)) ?? "") + .contains("link \(InputSourceOption.inputMenu.link)") + } + // Reconnecting reuses the pairing: straight to connected, no new code. client.disconnect() try? FileManager.default.removeItem(atPath: codeFile) diff --git a/apps/macos/Tests/fake_tv.py b/apps/macos/Tests/fake_tv.py index 9870465..8c304d6 100644 --- a/apps/macos/Tests/fake_tv.py +++ b/apps/macos/Tests/fake_tv.py @@ -198,6 +198,11 @@ def handle_remote(conn: SSL.Connection, args: argparse.Namespace) -> None: session.send(ping) elif msg.HasField("remote_ping_response"): log("remote: ping answered", msg.remote_ping_response.val1) + elif msg.HasField("remote_app_link_launch_request"): + link = msg.remote_app_link_launch_request.app_link + log("remote: open link", link) + with open(args.keys_file, "a") as f: + f.write(f"link {link}\n") elif msg.HasField("remote_ime_batch_edit"): edit = msg.remote_ime_batch_edit for info in edit.edit_info: From 41f34ba16ce1d96c09842ddc5181d5868f3d70ea Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 20 Oct 2026 21:32:54 +0530 Subject: [PATCH 2/3] feat: input picker on Android TVs (HDMI 1-3 and Settings) Testing on the MiTV-MOOR2 (#6) showed HDMI passthrough links switch inputs over the Android TV remote service, while intent: links for the input menu make the service drop the connection. So on those TVs Input source is now a picker: HDMI 1, 2, 3 (switch straight to the input) and Settings (full list of inputs). It closes as soon as one is chosen. Same picker in the mobile apps, with the app-link message and HDMI links unit tested; the macOS end-to-end test sends an HDMI link to the fake TV. Refs #6 Co-Authored-By: Claude Opus 5.5 --- README.md | 4 +- apps/macos/Sources/MiRemote/RemoteView.swift | 81 +++++++-------- .../Tests/MiRemoteTests/FakeTVTests.swift | 4 +- apps/mobile/src/app/index.tsx | 99 ++++++++++++++++++- apps/mobile/src/atv/__tests__/atv.test.ts | 8 ++ apps/mobile/src/atv/messages.ts | 17 ++++ .../src/components/remote/RemoteKey.tsx | 14 ++- .../src/localization/translations/en.json | 6 ++ .../src/remote/__tests__/client.test.ts | 7 ++ apps/mobile/src/remote/client.ts | 7 ++ 10 files changed, 193 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index a15f7ed..2b6daf9 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ All three implement the same protocols and features: | TV | Status | |---|---| | **Xiaomi Mi TV `MiTV-AXSO0`** (Android TV 9, PatchWall) | ✅ Tested with the macOS app. The mobile apps use the same protocol and have so far been tested against a mock TV | -| **Xiaomi Mi TV `MiTV-MOOR2`** (Android TV 11, no MiLink) | ✅ Tested with the macOS app, through the Android TV remote service ([#4](https://github.com/EndLess728/MiTV-Remote/issues/4)): pairing, navigation, volume, media keys and typing work. Input source opens Settings; recent apps isn't available | +| **Xiaomi Mi TV `MiTV-MOOR2`** (Android TV 11, no MiLink) | ✅ Tested with the macOS app, through the Android TV remote service ([#4](https://github.com/EndLess728/MiTV-Remote/issues/4)): pairing, navigation, volume, media keys, typing and switching HDMI inputs work. Recent apps isn't available | | Other Mi TVs on Android TV with PatchWall | Expected to work, not tested | | Xiaomi TVs on Google TV | Expected to work through the Android TV remote service, not tested | @@ -51,7 +51,7 @@ The apps try MiLink first and fall back to the Android TV remote service, and re **Over the Android TV remote service**, the TV only acts on a limited set of keys, the same ones the Google TV app can send: -- **Input source**: there's no input key, so the button opens the TV's **Settings**, where the inputs are. (Tried on the MOOR2: TV, HDMI 1–4, Guide and F1–F12 keys don't open the input menu.) +- **Input source**: there's no input key, so the button opens a picker that switches straight to **HDMI 1, 2 or 3**, or opens **Settings** for the full list of inputs. The switch is a TV-input link (`content://android.media.tv/passthrough/…`) sent as an app-link request, with the input IDs MediaTek-based Mi TVs use. The input menu itself can't be opened this way: the service only passes plain links, and drops the connection on `intent:` links. - **Recent apps**: not available; the button is greyed out. - **macOS**: the pairing key is kept in your login keychain. After installing a new version, macOS may ask once for your password to let Mi Remote use it; choose **Always Allow** to keep the pairing. diff --git a/apps/macos/Sources/MiRemote/RemoteView.swift b/apps/macos/Sources/MiRemote/RemoteView.swift index 252ad12..087a077 100644 --- a/apps/macos/Sources/MiRemote/RemoteView.swift +++ b/apps/macos/Sources/MiRemote/RemoteView.swift @@ -20,8 +20,7 @@ struct RemoteView: View { @StateObject private var discovery = TVDiscovery() @AppStorage("tvHost") private var savedHost = "" @State private var showConnect = false - /// Over Android TV Remote there's no input-source key; this panel offers the ways being - /// tested in #6 (opening the input menu by link, or switching HDMI inputs directly). + /// Over Android TV Remote there's no input-source key; this picker switches inputs by link. @State private var showInputs = false @State private var text = "" @FocusState private var remoteFocused: Bool @@ -651,12 +650,11 @@ struct PairSheet: View { // MARK: - Input source (Android TV Remote) -/// Ways to reach the TV's inputs over the Android TV remote service, which has no input key -/// (#6). Being tested on a MiTV-MOOR2: its Live TV app opens the input menu on the standard -/// `com.android.tv.action.VIEW_INPUTS` action, and switches inputs on TV-input passthrough links. +/// Inputs reachable over the Android TV remote service, which has no input-source key (#6). The +/// TV's Live TV app switches inputs on TV-input passthrough links, sent as app-link requests. +/// Opening the input menu itself by link doesn't work: the service drops the connection on +/// intent: links (tested on a MiTV-MOOR2). enum InputSourceOption: CaseIterable, Identifiable { - case inputMenu - case xiaomiInputMenu case hdmi1 case hdmi2 case hdmi3 @@ -666,8 +664,6 @@ enum InputSourceOption: CaseIterable, Identifiable { var title: String { switch self { - case .inputMenu: return "Input menu" - case .xiaomiInputMenu: return "Input menu (Xiaomi)" case .hdmi1: return "HDMI 1" case .hdmi2: return "HDMI 2" case .hdmi3: return "HDMI 3" @@ -675,16 +671,14 @@ enum InputSourceOption: CaseIterable, Identifiable { } } - /// The app link sent to the TV; nil for Settings, which is a key. - var link: String { + /// The passthrough link for an HDMI input. MediaTek-based Mi TVs name HDMI port n's input + /// HW(n+3) in their TV input service. Nil for Settings, which is a key. + var link: String? { switch self { - case .inputMenu: return "intent:#Intent;action=com.android.tv.action.VIEW_INPUTS;end" - case .xiaomiInputMenu: return "intent:#Intent;component=com.mitv.livetv/.input.SelectInputActivity;end" - // MediaTek-based Mi TVs: HDMI port n is hardware input HW(n+3). case .hdmi1: return Self.passthrough("HW4") case .hdmi2: return Self.passthrough("HW5") case .hdmi3: return Self.passthrough("HW6") - case .settings: return "" + case .settings: return nil } } @@ -693,52 +687,47 @@ enum InputSourceOption: CaseIterable, Identifiable { } } +/// Input picker for TVs on the Android TV remote service. Closes once an input is chosen. struct InputSourceSheet: View { @ObservedObject var client: RemoteClient var onClose: () -> Void - @State private var lastSent: String? var body: some View { VStack(alignment: .leading, spacing: 12) { Text("Input source") .font(.headline) - Text("This TV has no input key over Wi-Fi. Try opening the input menu, or switch straight to an HDMI input. (Testing: please tell us in issue #6 which of these work.)") - .font(.callout) - .foregroundStyle(.secondary) - .fixedSize(horizontal: false, vertical: true) - LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: 6), count: 2), spacing: 6) { - ForEach(InputSourceOption.allCases) { option in - Button { - if option == .settings { - client.press(.settings) - lastSent = "Settings key" - } else { - client.openLink(option.link) - lastSent = option.link - } - } label: { - Text(option.title) - .font(.system(size: 12, weight: .semibold)) - .lineLimit(1) - .minimumScaleFactor(0.8) - .frame(maxWidth: .infinity, minHeight: 32) - } - .buttonStyle(KeyStyle(shape: RoundedRectangle(cornerRadius: 8, style: .continuous), fill: Palette.key)) + LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: 6), count: 3), spacing: 6) { + ForEach(InputSourceOption.allCases.filter { $0 != .settings }) { option in + choice(option) } } - if let lastSent { - Text("Sent: \(lastSent)") - .font(.system(size: 9, design: .monospaced)) - .foregroundStyle(.secondary) - .textSelection(.enabled) - .fixedSize(horizontal: false, vertical: true) - } + choice(.settings) + Text("Settings has the full list of inputs.") + .font(.caption) + .foregroundStyle(.secondary) HStack { Spacer() - Button("Done", action: onClose) + Button("Cancel", action: onClose) .keyboardShortcut(.cancelAction) } } .padding(18) } + + private func choice(_ option: InputSourceOption) -> some View { + Button { + if let link = option.link { + client.openLink(link) + } else { + client.press(.settings) + } + onClose() + } label: { + Text(option.title) + .font(.system(size: 12, weight: .semibold)) + .lineLimit(1) + .frame(maxWidth: .infinity, minHeight: 32) + } + .buttonStyle(KeyStyle(shape: RoundedRectangle(cornerRadius: 8, style: .continuous), fill: Palette.key)) + } } diff --git a/apps/macos/Tests/MiRemoteTests/FakeTVTests.swift b/apps/macos/Tests/MiRemoteTests/FakeTVTests.swift index 7fe7cfe..8a36b1d 100644 --- a/apps/macos/Tests/MiRemoteTests/FakeTVTests.swift +++ b/apps/macos/Tests/MiRemoteTests/FakeTVTests.swift @@ -69,10 +69,10 @@ final class FakeTVTests: XCTestCase { } // Links reach the TV as app-link launch requests. - client.openLink(InputSourceOption.inputMenu.link) + client.openLink(InputSourceOption.hdmi1.link!) try await waitUntil("the TV to receive the link", client: client) { ((try? String(contentsOfFile: keysFile, encoding: .utf8)) ?? "") - .contains("link \(InputSourceOption.inputMenu.link)") + .contains("link \(InputSourceOption.hdmi1.link!)") } // Reconnecting reuses the pairing: straight to connected, no new code. diff --git a/apps/mobile/src/app/index.tsx b/apps/mobile/src/app/index.tsx index 39c32a6..1c297bc 100644 --- a/apps/mobile/src/app/index.tsx +++ b/apps/mobile/src/app/index.tsx @@ -1,11 +1,20 @@ import React, { useEffect, useState } from 'react'; -import { AppState, Pressable, Text, TextInput, useWindowDimensions, View } from 'react-native'; +import { + AppState, + Modal, + Pressable, + Text, + TextInput, + useWindowDimensions, + View, +} from 'react-native'; import { KeyboardStickyView } from 'react-native-keyboard-controller'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; import { StyleSheet, useUnistyles } from 'react-native-unistyles'; import { Ionicons } from '@react-native-vector-icons/ionicons'; import { router, usePathname } from 'expo-router'; +import { hdmiInputLink } from '@/atv/messages'; import { DPad } from '@/components/remote/DPad'; import { RemoteKey } from '@/components/remote/RemoteKey'; import { Rocker } from '@/components/remote/Rocker'; @@ -63,6 +72,52 @@ function StatusHeader() { ); } +/** + * Input picker for TVs on the Android TV remote service, which has no input-source key (#6): + * HDMI inputs switch by passthrough link; Settings lists every input. Closes once one is chosen. + */ +function InputPicker({ visible, onClose }: { visible: boolean; onClose: () => void }) { + const t = useTranslate(); + const choose = (action: () => void) => { + tapFeedback(); + action(); + onClose(); + }; + + return ( + + + + {t('inputs.title')} + + {[1, 2, 3].map((port) => ( + styles.pickerChoice(pressed)} + onPress={() => choose(() => remote.openLink(hdmiInputLink(port)))} + > + HDMI {port} + + ))} + + styles.pickerChoice(pressed)} + onPress={() => choose(() => remote.press('settings'))} + > + {t('inputs.settings')} + + {t('inputs.settings_hint')} + + {t('inputs.cancel')} + + + + + ); +} + function TypeBar() { const { theme } = useUnistyles(); const t = useTranslate(); @@ -111,6 +166,7 @@ export default function RemoteScreen(): React.JSX.Element { const status = useConnectionStore((s) => s.status); // The Android TV Remote service has no recent-apps key (see atvKey in the client). const atv = useConnectionStore((s) => s.protocol === 'atv'); + const [showInputs, setShowInputs] = useState(false); const pathname = usePathname(); // Android TVs without MiLink need a one-time pairing code; ask for it when the client does. @@ -151,7 +207,12 @@ export default function RemoteScreen(): React.JSX.Element { keyName="power" variant="power" /> - + setShowInputs(true) : undefined} + /> setShowInputs(false)} /> + @@ -255,6 +318,38 @@ export default function RemoteScreen(): React.JSX.Element { } const styles = StyleSheet.create((theme, rt) => ({ + pickerBackdrop: { + flex: 1, + justifyContent: 'center', + padding: ms(24), + backgroundColor: 'rgba(0,0,0,0.55)', + }, + picker: { + width: '100%', + maxWidth: 420, + alignSelf: 'center', + gap: ms(10), + padding: ms(18), + borderRadius: ms(18), + backgroundColor: theme.colors.remoteBody, + }, + pickerRow: { + flexDirection: 'row', + gap: ms(8), + }, + pickerChoice: (pressed: boolean) => ({ + flex: 1, + height: ms(46), + borderRadius: ms(12), + alignItems: 'center', + justifyContent: 'center', + backgroundColor: pressed ? theme.colors.remoteKeyPressed : theme.colors.remoteKey, + }), + pickerCancel: { + alignSelf: 'flex-end', + paddingVertical: ms(6), + paddingHorizontal: ms(8), + }, screen: { flex: 1, backgroundColor: theme.colors.remoteScreen, diff --git a/apps/mobile/src/atv/__tests__/atv.test.ts b/apps/mobile/src/atv/__tests__/atv.test.ts index a05407d..df733b9 100644 --- a/apps/mobile/src/atv/__tests__/atv.test.ts +++ b/apps/mobile/src/atv/__tests__/atv.test.ts @@ -9,7 +9,9 @@ import { } from '../__fixtures__/vectors'; import { certificate, rsaPublicKey, subjectPublicKeyInfo, tbsCertificate } from '../der'; import { + appLink, Direction, + hdmiInputLink, imeBatchEdit, keyInject, normalizeCode, @@ -143,6 +145,12 @@ describe('remote messages', () => { expect(remoteSetActive(615)).toEqual(PB.setActive615); expect(imeBatchEdit(3, 7, 'héllo')).toEqual(PB.imeHello); expect(remoteConfigure(615)).toEqual(PB.configure615); + expect(appLink('content://x')).toEqual([ + 210, 5, 13, 10, 11, 99, 111, 110, 116, 101, 110, 116, 58, 47, 47, 120, + ]); + expect(hdmiInputLink(1)).toBe( + 'content://android.media.tv/passthrough/com.mediatek.tvinput%2F.hdmi.HDMIInputService%2FHW4', + ); }); it('parses incoming messages', () => { diff --git a/apps/mobile/src/atv/messages.ts b/apps/mobile/src/atv/messages.ts index d0d2fee..d62753c 100644 --- a/apps/mobile/src/atv/messages.ts +++ b/apps/mobile/src/atv/messages.ts @@ -153,6 +153,23 @@ export function keyInject(code: number, direction: number): Bytes { return fieldMessage(10, [...fieldVarint(1, code), ...fieldVarint(2, direction)]); } +/** + * Asks the TV to open a link (RemoteAppLinkLaunchRequest): a deep link, or a TV-input + * passthrough link (see `inputLink`). The service drops the connection on intent: links. + */ +export function appLink(link: string): Bytes { + return fieldMessage(90, fieldString(1, link)); +} + +/** + * Passthrough link that switches the TV to HDMI port `port`. The Android TV remote service has + * no input-source key (#6), but the TV's Live TV app switches inputs on these links. MediaTek-based + * Mi TVs name HDMI port n's input HW(n+3). + */ +export function hdmiInputLink(port: number): string { + return `content://android.media.tv/passthrough/com.mediatek.tvinput%2F.hdmi.HDMIInputService%2FHW${port + 3}`; +} + /** * Puts `text` into the focused text field on the TV, cursor after it. The counters echo the * TV's latest (0 before it has sent any), as the Google TV app and other clients do. diff --git a/apps/mobile/src/components/remote/RemoteKey.tsx b/apps/mobile/src/components/remote/RemoteKey.tsx index 92f769d..2b9e643 100644 --- a/apps/mobile/src/components/remote/RemoteKey.tsx +++ b/apps/mobile/src/components/remote/RemoteKey.tsx @@ -6,7 +6,7 @@ import { Ionicons, type IoniconsIconName } from '@react-native-vector-icons/ioni import type { AndroidKeyName } from '@/milink/keys'; import { ms } from '@/utils'; -import { useRemoteKey } from './useRemoteKey'; +import { tapFeedback, useRemoteKey } from './useRemoteKey'; type Variant = 'default' | 'accent' | 'power'; @@ -20,6 +20,8 @@ interface RemoteKeyProps { style?: ViewStyle; /** Dimmed and inert, for keys the connected TV doesn't support. */ disabled?: boolean; + /** Runs instead of sending the key (e.g. to open a picker). */ + onPress?: () => void; } /** A round remote button that sends one Android key. */ @@ -32,6 +34,7 @@ export function RemoteKey({ variant = 'default', style, disabled = false, + onPress, }: RemoteKeyProps): React.JSX.Element { const { theme } = useUnistyles(); const handlers = useRemoteKey(keyName, repeat); @@ -49,7 +52,14 @@ export function RemoteKey({ disabled && styles.disabled, style, ]} - {...handlers} + {...(onPress + ? { + onPress: () => { + tapFeedback(); + onPress(); + }, + } + : handlers)} > diff --git a/apps/mobile/src/localization/translations/en.json b/apps/mobile/src/localization/translations/en.json index b409f4f..915e5aa 100644 --- a/apps/mobile/src/localization/translations/en.json +++ b/apps/mobile/src/localization/translations/en.json @@ -74,5 +74,11 @@ "error_connection": "Lost the connection to the TV while pairing.", "error_certificate": "Couldn't create the pairing key on this phone.", "hint": "You only need to do this once per TV. If it keeps failing, on the TV open Settings → Apps → See all apps → Show system apps → Android TV Remote Service, and clear its storage." + }, + "inputs": { + "title": "Input source", + "settings": "Settings", + "settings_hint": "Settings has the full list of inputs.", + "cancel": "Cancel" } } diff --git a/apps/mobile/src/remote/__tests__/client.test.ts b/apps/mobile/src/remote/__tests__/client.test.ts index de6b774..345f7b6 100644 --- a/apps/mobile/src/remote/__tests__/client.test.ts +++ b/apps/mobile/src/remote/__tests__/client.test.ts @@ -3,8 +3,10 @@ import { X509Certificate } from 'crypto'; import { CLIENT_CERT, PAIRING_CODE, PAIRING_SECRET_HEX, PB, SERVER_CERT } from '@/atv/__fixtures__/vectors'; import { ATV_PAIRING_PORT, + appLink, ATV_REMOTE_PORT, Direction, + hdmiInputLink, imeBatchEdit, keyInject, pairingConfiguration, @@ -146,6 +148,11 @@ describe('RemoteClient', () => { remote.press('appSwitch'); expect(sentMessages()).toEqual([delimited(keyInject(176, Direction.short))]); + // Inputs switch by app link. + mockNative.send.mockClear(); + remote.openLink(hdmiInputLink(2)); + expect(sentMessages()).toEqual([delimited(appLink(hdmiInputLink(2)))]); + // Text goes in as an IME edit even before the TV has sent IME counters. mockNative.send.mockClear(); remote.type('Hi'); diff --git a/apps/mobile/src/remote/client.ts b/apps/mobile/src/remote/client.ts index 8b95913..4cdacb0 100644 --- a/apps/mobile/src/remote/client.ts +++ b/apps/mobile/src/remote/client.ts @@ -3,6 +3,7 @@ import { create } from 'zustand'; import { certificate, type RsaPublicKey, rsaPublicKey, tbsCertificate } from '@/atv/der'; import { + appLink, ATV_PAIRING_PORT, ATV_REMOTE_PORT, Direction, @@ -525,6 +526,12 @@ class RemoteClient { // ── Keys ─────────────────────────────────────────────────── + /** Android TV Remote: asks the TV to open `link` (see `appLink`). */ + openLink(link: string): void { + if (!this.isConnected || this.phase !== 'atv') return; + this.sendDelimited(appLink(link)); + } + /** Single press: DOWN followed by UP. */ press(key: AndroidKeyName): void { this.pressCode(AndroidKey[key]); From 633533964aa33a3ab076221f481c3c429de1ad93 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 20 Oct 2026 21:52:55 +0530 Subject: [PATCH 3/3] docs: explain why recent apps isn't available on Android TV Remote Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2b6daf9..2cf82b0 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ The apps try MiLink first and fall back to the Android TV remote service, and re **Over the Android TV remote service**, the TV only acts on a limited set of keys, the same ones the Google TV app can send: - **Input source**: there's no input key, so the button opens a picker that switches straight to **HDMI 1, 2 or 3**, or opens **Settings** for the full list of inputs. The switch is a TV-input link (`content://android.media.tv/passthrough/…`) sent as an app-link request, with the input IDs MediaTek-based Mi TVs use. The input menu itself can't be opened this way: the service only passes plain links, and drops the connection on `intent:` links. -- **Recent apps**: not available; the button is greyed out. +- **Recent apps**: not available, so the button is greyed out. The service can't deliver the recent-apps key (the TV's `virtual-remote.kl`, which the service types through, has no entry for it), and recents is part of the system UI, so there's no link to open it either. Hold or double-press Home, All apps, Menu and hold OK were all tried on the MOOR2. - **macOS**: the pairing key is kept in your login keychain. After installing a new version, macOS may ask once for your password to let Mi Remote use it; choose **Always Allow** to keep the pairing. **Check your TV** (it must be on the same Wi-Fi as your computer):