From 34e240d7fab530d103467f6b968ab6ea4133e41a Mon Sep 17 00:00:00 2001 From: andrekir Date: Thu, 28 Apr 2022 11:53:32 -0300 Subject: [PATCH] update deprecated intent method --- .../geeksville/mesh/ui/SettingsFragment.kt | 86 ++++++++----------- 1 file changed, 36 insertions(+), 50 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt b/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt index 1cd50fa5e..02a969316 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt @@ -1,7 +1,6 @@ package com.geeksville.mesh.ui import android.annotation.SuppressLint -import android.app.Activity import android.app.Application import android.app.PendingIntent import android.bluetooth.BluetoothDevice @@ -21,6 +20,8 @@ import android.view.View import android.view.ViewGroup import android.view.inputmethod.EditorInfo import android.widget.* +import androidx.activity.result.IntentSenderRequest +import androidx.activity.result.contract.ActivityResultContracts import androidx.fragment.app.activityViewModels import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.MutableLiveData @@ -677,6 +678,10 @@ class SettingsFragment : ScreenFragment("Settings"), Logging { updateNodeInfo() } + scanModel.devices.observe(viewLifecycleOwner) { devices -> + updateDevicesButtons(devices) + } + scanModel.errorText.observe(viewLifecycleOwner) { errMsg -> if (errMsg != null) { binding.scanStatusText.text = errMsg @@ -845,10 +850,6 @@ class SettingsFragment : ScreenFragment("Settings"), Logging { private fun initClassicScan() { - scanModel.devices.observe(viewLifecycleOwner) { devices -> - updateDevicesButtons(devices) - } - binding.changeRadioButton.setOnClickListener { debug("User clicked changeRadioButton") if (!myActivity.hasScanPermission()) { @@ -881,10 +882,7 @@ class SettingsFragment : ScreenFragment("Settings"), Logging { } } - private fun startCompanionScan() { - // Disable the change button until our scan has some results - binding.changeRadioButton.isEnabled = false - + private fun associationRequest(): AssociationRequest { // To skip filtering based on name and supported feature flags (UUIDs), // don't include calls to setNamePattern() and addServiceUuid(), // respectively. This example uses Bluetooth. @@ -897,28 +895,46 @@ class SettingsFragment : ScreenFragment("Settings"), Logging { // The argument provided in setSingleDevice() determines whether a single // device name or a list of device names is presented to the user as // pairing options. - val pairingRequest: AssociationRequest = AssociationRequest.Builder() + return AssociationRequest.Builder() .addDeviceFilter(deviceFilter) .setSingleDevice(false) .build() + } + + @SuppressLint("MissingPermission") + val associationResultLauncher = registerForActivityResult( + ActivityResultContracts.StartIntentSenderForResult() + ) { + it.data + ?.getParcelableExtra(CompanionDeviceManager.EXTRA_DEVICE) + ?.let { device -> + scanModel.onSelected( + myActivity, + BTScanModel.DeviceListEntry( + device.name, + "x${device.address}", + device.bondState == BOND_BONDED + ) + ) + } + } + + private fun startCompanionScan() { + // Disable the change button until our scan has some results + binding.changeRadioButton.isEnabled = false // When the app tries to pair with the Bluetooth device, show the // appropriate pairing request dialog to the user. deviceManager.associate( - pairingRequest, + associationRequest(), object : CompanionDeviceManager.Callback() { override fun onDeviceFound(chooserLauncher: IntentSender) { debug("Found one device - enabling changeRadioButton") binding.changeRadioButton.isEnabled = true binding.changeRadioButton.setOnClickListener { - debug("User clicked changeRadioButton") - try { - startIntentSenderForResult( - chooserLauncher, - MainActivity.SELECT_DEVICE_REQUEST_CODE, null, 0, 0, 0, null - ) - } catch (ex: Throwable) { - errormsg("CompanionDevice startIntentSenderForResult error") + chooserLauncher.let { + val request = IntentSenderRequest.Builder(it).build() + associationResultLauncher.launch(request) } } } @@ -933,8 +949,7 @@ class SettingsFragment : ScreenFragment("Settings"), Logging { private fun initModernScan() { - scanModel.devices.observe(viewLifecycleOwner) { devices -> - updateDevicesButtons(devices) + scanModel.devices.observe(viewLifecycleOwner) { startCompanionScan() } } @@ -1061,33 +1076,4 @@ class SettingsFragment : ScreenFragment("Settings"), Logging { } } } - - @SuppressLint("MissingPermission") - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - if (hasCompanionDeviceApi && myActivity.hasConnectPermission() - && requestCode == MainActivity.SELECT_DEVICE_REQUEST_CODE - && resultCode == Activity.RESULT_OK - ) { - val deviceToPair: BluetoothDevice = - data?.getParcelableExtra(CompanionDeviceManager.EXTRA_DEVICE)!! - - // We only keep an association to one device at a time... - deviceManager.associations.forEach { old -> - if (deviceToPair.address != old) { - debug("Forgetting old BLE association ${old.anonymize}") - deviceManager.disassociate(old) - } - } - scanModel.onSelected( - myActivity, - BTScanModel.DeviceListEntry( - deviceToPair.name, - "x${deviceToPair.address}", - deviceToPair.bondState == BOND_BONDED - ) - ) - } else { - super.onActivityResult(requestCode, resultCode, data) - } - } }