Handle updated intents for Matter commissioning (#3535)

- It looks like fast pair/shared Matter devices now re-use the commissioning activity if it is still open in the background. Set the launch mode to single top to be able to use onNewIntent and get data for new devices.
This commit is contained in:
Joris Pelgröm 2023-05-20 20:58:23 +02:00 committed by GitHub
parent f811c09175
commit cb59ce1208
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 6 deletions

View file

@ -14,7 +14,8 @@
android:name=".matter.MatterCommissioningActivity"
android:configChanges="orientation|screenSize"
android:exported="true"
android:theme="@style/Theme.HomeAssistant.Config">
android:theme="@style/Theme.HomeAssistant.Config"
android:launchMode="singleTop">
<intent-filter>
<action android:name="com.google.android.gms.home.matter.ACTION_COMMISSION_DEVICE"/>
<category android:name="android.intent.category.DEFAULT"/>

View file

@ -1,5 +1,6 @@
package io.homeassistant.companion.android.matter
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.activity.compose.setContent
@ -36,6 +37,7 @@ class MatterCommissioningActivity : AppCompatActivity() {
private var deviceCode: String? = null
private var deviceName by mutableStateOf<String?>(null)
private var servers by mutableStateOf<List<Server>>(emptyList())
private var newMatterDevice = false
private val threadPermissionLauncher = registerForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result ->
deviceCode?.let { viewModel.onThreadPermissionResult(result, it) }
@ -77,17 +79,24 @@ class MatterCommissioningActivity : AppCompatActivity() {
deviceName = data.deviceName
deviceCode = data.manualPairingCode
viewModel.checkSetup()
viewModel.checkSetup(newMatterDevice)
newMatterDevice = false
} catch (e: SharedDeviceData.InvalidSharedDeviceDataException) {
Log.e(TAG, "Received incomplete Matter commissioning data, launching webview")
continueToApp(true)
if (!newMatterDevice) continueToApp(true)
}
} else {
Log.d(TAG, "No Matter commissioning data, launching webview")
continueToApp(true)
if (!newMatterDevice) continueToApp(true)
}
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
this.intent = intent // Data is handled by check in onResume()
newMatterDevice = true
}
private fun startCommissioning() {
lifecycleScope.launch {
val threadIntent = viewModel.syncThreadIfNecessary()

View file

@ -45,9 +45,13 @@ class MatterCommissioningViewModel @Inject constructor(
var serverId by mutableStateOf(0)
private set
fun checkSetup() {
fun checkSetup(isNewDevice: Boolean) {
viewModelScope.launch {
if (step != CommissioningFlowStep.NotStarted) return@launch
if (!isNewDevice && step != CommissioningFlowStep.NotStarted) {
return@launch
} else {
step = CommissioningFlowStep.NotStarted
}
if (!serverManager.isRegistered()) {
step = CommissioningFlowStep.NotRegistered