library: fix AssistantIntent receiver crashing caller task
- Commit
f73f958d0823fab6bc479f32941951382af260b7- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-25 00:10 UTC (1 month ago)
- Parents
1ee0407
Diff
library/src/main/java/com/vayunmathur/library/util/AssistantIntent.kt
diff --git a/library/src/main/java/com/vayunmathur/library/util/AssistantIntent.kt b/library/src/main/java/com/vayunmathur/library/util/AssistantIntent.ktindex 87ee9ba896..e3c552a7b3 100644--- a/library/src/main/java/com/vayunmathur/library/util/AssistantIntent.kt+++ b/library/src/main/java/com/vayunmathur/library/util/AssistantIntent.kt@@ -6,9 +6,11 @@ import com.vayunmathur.library.log.Log import android.content.ActivityNotFoundException import android.content.Context import android.content.Intent+import android.os.BadParcelableException import android.os.Bundle import android.os.Handler import android.os.Looper+import android.os.Parcel import android.os.ResultReceiver import androidx.activity.ComponentActivity import androidx.core.content.IntentCompat@@ -38,7 +40,6 @@ abstract class AssistantIntent<Input : Any, Output : Any>( // 1. Get the incoming data + the channel to answer on. val inputString = intent.getStringExtra("DATA")- val receiver = IntentCompat.getParcelableExtra(intent, "RECEIVER", ResultReceiver::class.java) // 2. These activities are themed @android:style/Theme.NoDisplay, which // REQUIRES finish() to be called before onResume() completes. If we finish@@ -51,6 +52,13 @@ abstract class AssistantIntent<Input : Any, Output : Any>( // task/lifecycle independent). finish() + val receiver = try {+ IntentCompat.getParcelableExtra(intent, "RECEIVER", ResultReceiver::class.java)+ } catch (expected: BadParcelableException) {+ Log.error(TAG, "Unreadable RECEIVER extra for ${javaClass.name}", expected)+ return+ }+ // Decode defensively: this activity runs in the caller's task, so an // exception escaping onCreate would crash and tear that task down. val input = try {@@ -142,7 +150,7 @@ class IntentLauncher(private val activity: ComponentActivity) { val intent = Intent().apply { setClassName(packageName, className) putExtra("DATA", Json.encodeToString(serializer, input))- putExtra("RECEIVER", receiver)+ putExtra("RECEIVER", receiver.forIpc()) } // Not installed / not resolvable: report back so callers can raise a@@ -180,6 +188,19 @@ class IntentLauncher(private val activity: ComponentActivity) { result ?: throw TimeoutException("Timed out waiting for a response from $packageName") } + // Strips the subclass name from the parcel so the target app (which lacks+ // SecureResultReceiver, or has it R8-stripped) can unmarshal it.+ private fun ResultReceiver.forIpc(): ResultReceiver {+ val parcel = Parcel.obtain()+ try {+ writeToParcel(parcel, 0)+ parcel.setDataPosition(0)+ return ResultReceiver.CREATOR.createFromParcel(parcel)+ } finally {+ parcel.recycle()+ }+ }+ companion object { private const val INTENT_TIMEOUT_MS = 30_000L private const val INTENT_TAG = "IntentLauncher"