maauto: reply via app PendingIntent, swallow volume keys
- Commit
c0a48244c1dedd5ff6fa8bac98505b8cd1186997- Author
- vayun-mathur <[email protected]>
- When
- 2026-09-13 18:59 UTC (2 months ago)
- Parents
6552a96
Diff
auto/src/main/java/com/vayunmathur/auto/notifications/MessageMirrorBus.kt
diff --git a/auto/src/main/java/com/vayunmathur/auto/notifications/MessageMirrorBus.kt b/auto/src/main/java/com/vayunmathur/auto/notifications/MessageMirrorBus.ktindex 9b42a05d9e..58d9eda2a1 100644--- a/auto/src/main/java/com/vayunmathur/auto/notifications/MessageMirrorBus.kt+++ b/auto/src/main/java/com/vayunmathur/auto/notifications/MessageMirrorBus.kt@@ -16,7 +16,7 @@ import java.util.concurrent.CopyOnWriteArraySet */ object MessageMirrorBus { fun interface Listener {- fun onMirror(thread: MessagingThread, message: MessagingMessage)+ fun onMirror(thread: MessagingThread, message: MessagingMessage, route: MessageReplyRoute?) } private val listeners = CopyOnWriteArraySet<Listener>()@@ -27,7 +27,7 @@ object MessageMirrorBus { return AutoCloseable { listeners -= listener } } - fun post(thread: MessagingThread, message: MessagingMessage) {- for (listener in listeners) listener.onMirror(thread, message)+ fun post(thread: MessagingThread, message: MessagingMessage, route: MessageReplyRoute? = null) {+ for (listener in listeners) listener.onMirror(thread, message, route) } }
auto/src/main/java/com/vayunmathur/auto/notifications/MessageMirrorService.kt
diff --git a/auto/src/main/java/com/vayunmathur/auto/notifications/MessageMirrorService.kt b/auto/src/main/java/com/vayunmathur/auto/notifications/MessageMirrorService.ktindex 8e13cc654f..96d26ab04b 100644--- a/auto/src/main/java/com/vayunmathur/auto/notifications/MessageMirrorService.kt+++ b/auto/src/main/java/com/vayunmathur/auto/notifications/MessageMirrorService.kt@@ -26,7 +26,7 @@ class MessageMirrorService : NotificationListenerService() { val notification = sbn.notification if (notification.category != Notification.CATEGORY_MESSAGE) return val (thread, message) = extract(sbn) ?: return- MessageMirrorBus.post(thread, message)+ MessageMirrorBus.post(thread, message, replyRoute(sbn)) Log.d(TAG, "mirrored message for thread ${thread.threadId}") } @@ -89,6 +89,33 @@ class MessageMirrorService : NotificationListenerService() { private fun messageKey(sbn: StatusBarNotification, timestamp: Long): String = "${sbn.id}:$timestamp" + /**+ * Pulls the reply/mark-read route out of a message notification, or null+ * when the app offers no reply action.+ *+ * Only the direct-action shape is read: a notification action carrying a+ * `RemoteInput` (what modern message apps post with `MessagingStyle`).+ * The legacy `CarExtender` unread-conversation shape is deliberately not+ * read -- androidx.core removed its accessor, and every current message+ * app uses direct actions. An action without a reply intent is no route.+ */+ private fun replyRoute(sbn: StatusBarNotification): MessageReplyRoute? {+ val notification = sbn.notification+ val threadId = conversationKey(sbn)+ for (action in notification.actions.orEmpty()) {+ val remoteInputs = action.remoteInputs+ if (!remoteInputs.isNullOrEmpty() && action.actionIntent != null) {+ return MessageReplyRoute(+ threadId = threadId,+ replyIntent = action.actionIntent,+ remoteInput = remoteInputs.first(),+ readIntent = null,+ )+ }+ }+ return null+ }+ private companion object { const val TAG = "MaAuto.MsgMirror" }
auto/src/main/java/com/vayunmathur/auto/notifications/MessageReplyRoute.kt
diff --git a/auto/src/main/java/com/vayunmathur/auto/notifications/MessageReplyRoute.kt b/auto/src/main/java/com/vayunmathur/auto/notifications/MessageReplyRoute.ktnew file mode 100644index 0000000000..ceaa500a6b--- /dev/null+++ b/auto/src/main/java/com/vayunmathur/auto/notifications/MessageReplyRoute.kt@@ -0,0 +1,33 @@+package com.vayunmathur.auto.notifications++import android.app.PendingIntent+import android.app.RemoteInput++/**+ * How a head-unit reply leaves the phone for one conversation thread.+ *+ * Gearhead never sends SMS itself: replies go back through the message app+ * that posted the notification, via its own reply `PendingIntent` plus a+ * `RemoteInput` result (`lwg.c()` in the teardown: `REPLY_USING_NOTIFICATION_ACTION`+ * vs `REPLY_USING_CAR_EXTENDER`). The mark-read intent dismisses/handles the+ * thread on the phone side when the car marks it read.+ *+ * Retained per thread by the mirror service and handed to the ch14 owner on+ * the bus; intents are fire-once snapshots (a reposted notification replaces+ * them). Sending is fail-closed: a missing route or a failed `send()` drops+ * with a count, never a crash and never a direct SMS.+ *+ * Only the direct-action shape is retained (an action carrying a `RemoteInput`,+ * what modern `MessagingStyle` notifications post). The legacy `CarExtender`+ * unread-conversation shape is not read -- its androidx accessor is gone and+ * current message apps use direct actions.+ */+data class MessageReplyRoute(+ val threadId: String,+ /** Fires the message app's reply action; null when the app offers none. */+ val replyIntent: PendingIntent?,+ /** Carries the reply text into [replyIntent]; null with the intent. */+ val remoteInput: RemoteInput?,+ /** Marks the thread read on the phone; null when the app offers none. */+ val readIntent: PendingIntent?,+)
auto/src/main/java/com/vayunmathur/auto/platform/AutoSessionState.kt
diff --git a/auto/src/main/java/com/vayunmathur/auto/platform/AutoSessionState.kt b/auto/src/main/java/com/vayunmathur/auto/platform/AutoSessionState.ktindex 221a2c4ce3..c247b991c2 100644--- a/auto/src/main/java/com/vayunmathur/auto/platform/AutoSessionState.kt+++ b/auto/src/main/java/com/vayunmathur/auto/platform/AutoSessionState.kt@@ -448,6 +448,8 @@ object AutoSessionState { is MessagingEvent.ThreadsPosted -> _threadsPosted.value = event.count.toLong() is MessagingEvent.MessagePosted -> _messagesPosted.value++ is MessagingEvent.ReplyReceived -> _repliesReceived.value+++ is MessagingEvent.ReplySent -> _repliesReceived.value+++ is MessagingEvent.ReplyFailed -> Unit is MessagingEvent.MarkedRead -> Unit } }@@ -460,6 +462,7 @@ object AutoSessionState { is InputEvent.Touch -> _touchesInjected.value++ is InputEvent.Key -> _keysInjected.value++ is InputEvent.Scroll -> _scrollsInjected.value+++ is InputEvent.VolumeKey -> _keysInjected.value++ InputEvent.DroppedNoFocus -> _inputDropped.value++ } }
auto/src/main/java/com/vayunmathur/auto/platform/InputChannel.kt
diff --git a/auto/src/main/java/com/vayunmathur/auto/platform/InputChannel.kt b/auto/src/main/java/com/vayunmathur/auto/platform/InputChannel.ktindex 46cbcda183..87b35b20e2 100644--- a/auto/src/main/java/com/vayunmathur/auto/platform/InputChannel.kt+++ b/auto/src/main/java/com/vayunmathur/auto/platform/InputChannel.kt@@ -141,7 +141,15 @@ class InputChannel( if (touchSink(scaled)) onEvent(InputEvent.Touch(scaled.action, scaled.pointers.size)) } events.keys.forEach { key ->- if (keySink(key)) onEvent(InputEvent.Key(key.keycode, key.down))+ // Volume keys are consumed, never injected: gearhead's car home+ // swallows KEYCODE_VOLUME_UP/DOWN (its dispatchKeyEvent returns+ // true for 24/25) because the head unit owns its speaker volume.+ // Injecting them would double-handle volume the HU already manages.+ if (key.keycode == VOLUME_UP || key.keycode == VOLUME_DOWN) {+ onEvent(InputEvent.VolumeKey(key.keycode, key.down))+ } else if (keySink(key)) {+ onEvent(InputEvent.Key(key.keycode, key.down))+ } } // Tap-as-select rides the absolute section: keycode 65541, value 1 // presses DPAD_CENTER and anything else releases it (`jar.java`).@@ -161,6 +169,10 @@ class InputChannel( private companion object { const val TAG = "MaAuto.Input" + /** Consumed, never injected: the head unit owns its speaker volume. */+ const val VOLUME_UP = 24+ const val VOLUME_DOWN = 25+ private fun com.vayunmathur.auto.protocol.InputEvents.describe(): String = "input report (${touches.size} touch, ${keys.size} keys, " + "${scrolls.size} scroll, ${absolutes.size} abs)"
auto/src/main/java/com/vayunmathur/auto/platform/InputEvent.kt
diff --git a/auto/src/main/java/com/vayunmathur/auto/platform/InputEvent.kt b/auto/src/main/java/com/vayunmathur/auto/platform/InputEvent.ktindex 91c11580af..6f62fb1d86 100644--- a/auto/src/main/java/com/vayunmathur/auto/platform/InputEvent.kt+++ b/auto/src/main/java/com/vayunmathur/auto/platform/InputEvent.kt@@ -21,6 +21,13 @@ sealed interface InputEvent { /** One key press or release was injected. */ data class Key(val keycode: Int, val down: Boolean) : InputEvent + /**+ * A head-unit volume key was consumed, not injected: the car owns its+ * speaker volume (gearhead's car home swallows 24/25), so these count+ * separately from injected keys.+ */+ data class VolumeKey(val keycode: Int, val down: Boolean) : InputEvent+ /** One scroll tick was injected. */ data class Scroll(val delta: Int) : InputEvent
auto/src/main/java/com/vayunmathur/auto/platform/Messaging.kt
diff --git a/auto/src/main/java/com/vayunmathur/auto/platform/Messaging.kt b/auto/src/main/java/com/vayunmathur/auto/platform/Messaging.ktindex 484e3bdb9b..bde38a664f 100644--- a/auto/src/main/java/com/vayunmathur/auto/platform/Messaging.kt+++ b/auto/src/main/java/com/vayunmathur/auto/platform/Messaging.kt@@ -16,6 +16,12 @@ sealed interface MessagingEvent { /** The head unit answered with reply text for [threadId]. */ data class ReplyReceived(val threadId: String) : MessagingEvent + /** Reply text for [threadId] was handed to the message app for sending. */+ data class ReplySent(val threadId: String) : MessagingEvent++ /** Reply text for [threadId] could not be handed off; [reason] is short. */+ data class ReplyFailed(val threadId: String, val reason: String) : MessagingEvent+ /** * The head unit marked [threadId] read. Observed only: the phone keeps no * per-thread read state yet, so there is nothing to update. Kept as an
auto/src/main/java/com/vayunmathur/auto/service/MessagingCarAppService.kt
diff --git a/auto/src/main/java/com/vayunmathur/auto/service/MessagingCarAppService.kt b/auto/src/main/java/com/vayunmathur/auto/service/MessagingCarAppService.ktindex 5064b1a95e..361106cc11 100644--- a/auto/src/main/java/com/vayunmathur/auto/service/MessagingCarAppService.kt+++ b/auto/src/main/java/com/vayunmathur/auto/service/MessagingCarAppService.kt@@ -1,7 +1,9 @@ package com.vayunmathur.auto.service import android.util.Log+import android.app.RemoteInput import com.vayunmathur.auto.notifications.MessageMirrorBus+import com.vayunmathur.auto.notifications.MessageReplyRoute import com.vayunmathur.auto.platform.MessagingAudio import com.vayunmathur.auto.platform.MessagingEvent import com.vayunmathur.auto.protocol.GalConnection@@ -31,6 +33,8 @@ class MessagingCarAppService( private val audio: MessagingAudio = MessagingAudio.NoOp, private val onEvent: (MessagingEvent) -> Unit = {}, private val onReply: (threadId: String, text: String) -> Unit = { _, _ -> },+ /** Resolves a context for firing reply intents; read per send, null drops. */+ private val context: () -> android.content.Context? = { null }, ) { val channelId: Int get() = GalService.NOTIFICATION.id @@ -38,11 +42,20 @@ class MessagingCarAppService( private var mirror: AutoCloseable? = null private val threads = LinkedHashMap<String, MessagingThread>() + /**+ * Reply/mark-read routes per thread, replaced on every repost. A route is+ * only as fresh as the last notification; firing a stale intent fails+ * with a count, never a retry -- the app reposts and the route refreshes.+ */+ private val routes = LinkedHashMap<String, MessageReplyRoute>()+ /** The channel grant arrived; start mirroring the phone side. */ fun onChannelOpen() { open = true Log.i(TAG, "messaging channel open; mirroring message threads")- mirror = MessageMirrorBus.register { thread, message -> onPhoneMessage(thread, message) }+ mirror = MessageMirrorBus.register { thread, message, route ->+ onPhoneMessage(thread, message, route)+ } } /** One message for this channel; anything else is ignored, never misparsed. */@@ -61,20 +74,80 @@ class MessagingCarAppService( when (action) { is InboundMessagingAction.Reply -> { onEvent(MessagingEvent.ReplyReceived(action.threadId))- onReply(action.threadId, action.text)+ sendReply(action.threadId, action.text)+ }+ is InboundMessagingAction.MarkRead -> {+ onEvent(MessagingEvent.MarkedRead(action.threadId))+ sendMarkRead(action.threadId) }- is InboundMessagingAction.MarkRead -> onEvent(MessagingEvent.MarkedRead(action.threadId)) is InboundMessagingAction.VoiceReplyBegin -> audio.beginVoiceReply(action.threadId) { text -> onEvent(MessagingEvent.ReplyReceived(action.threadId))- onReply(action.threadId, text)+ sendReply(action.threadId, text)+ }+ }+ }++ /**+ * Hands head-unit reply text to the message app through its own reply+ * intent, exactly like gearhead's `lwg.c()`: the `RemoteInput` result+ * rides an explicit intent into `PendingIntent.send()`. This service still+ * knows GAL, not SMS -- sending stays the message app's job; here we only+ * fire what it advertised. Missing routes and failed sends drop with a+ * count, never a crash.+ */+ private fun sendReply(threadId: String, text: String) {+ val route = routes[threadId]+ val intent = route?.replyIntent+ val remoteInput = route?.remoteInput+ if (intent == null || remoteInput == null) {+ Log.i(TAG, "reply for $threadId with no app route; dropping")+ onEvent(MessagingEvent.ReplyFailed(threadId, "no-route"))+ onReply(threadId, text)+ return+ }+ val appContext = context() ?: run {+ Log.i(TAG, "reply for $threadId with no context; dropping")+ onEvent(MessagingEvent.ReplyFailed(threadId, "no-context"))+ onReply(threadId, text)+ return+ }+ runCatching {+ val fillIn = android.content.Intent()+ val results = android.os.Bundle()+ results.putCharSequence(remoteInput.resultKey, text)+ RemoteInput.addResultsToIntent(arrayOf(remoteInput), fillIn, results)+ intent.send(appContext, 0, fillIn)+ }+ .onSuccess {+ Log.i(TAG, "reply for $threadId handed to message app")+ onEvent(MessagingEvent.ReplySent(threadId)) }+ .onFailure {+ Log.w(TAG, "reply for $threadId failed to send", it)+ onEvent(MessagingEvent.ReplyFailed(threadId, "send-failed"))+ }+ onReply(threadId, text)+ }++ /** Fires the thread's mark-read intent, if the app advertised one. */+ private fun sendMarkRead(threadId: String) {+ val readIntent = routes[threadId]?.readIntent ?: run {+ Log.d(TAG, "mark-read for $threadId with no app route")+ return }+ runCatching { readIntent.send() }+ .onFailure { Log.w(TAG, "mark-read for $threadId failed", it) } } /** A phone notification arrived: snapshot the threads, post the body, read it aloud. */- private fun onPhoneMessage(thread: MessagingThread, message: MessagingMessage) {+ private fun onPhoneMessage(+ thread: MessagingThread,+ message: MessagingMessage,+ route: MessageReplyRoute?,+ ) { if (!open) return threads[thread.threadId] = thread+ if (route != null) routes[route.threadId] = route val (threadsType, threadsPayload) = MessagingCodec.encodeThreads(threads.values.toList()) connection.send(channelId, threadsType, threadsPayload) onEvent(MessagingEvent.ThreadsPosted(threads.size))@@ -88,6 +161,7 @@ class MessagingCarAppService( runCatching { mirror?.close() } mirror = null open = false+ routes.clear() } private companion object {
auto/src/main/java/com/vayunmathur/auto/service/ProjectionService.kt
diff --git a/auto/src/main/java/com/vayunmathur/auto/service/ProjectionService.kt b/auto/src/main/java/com/vayunmathur/auto/service/ProjectionService.ktindex 923abefa82..c8180d8924 100644--- a/auto/src/main/java/com/vayunmathur/auto/service/ProjectionService.kt+++ b/auto/src/main/java/com/vayunmathur/auto/service/ProjectionService.kt@@ -511,6 +511,7 @@ class ProjectionService : Service() { onReply = { threadId, text -> Log.i(TAG, "head-unit reply for $threadId (${text.length} chars)") },+ context = { this }, ) } // ch8 advertises in the same wire-order pass; the owner binds on its