fix: replace BluetoothAdapter with repository methods

pull/784/head
andrekir 2023-11-28 18:17:05 -03:00
rodzic 23966b173b
commit 79b98c84b2
3 zmienionych plików z 6 dodań i 12 usunięć

Wyświetl plik

@ -8,7 +8,6 @@ import android.bluetooth.BluetoothManager
import android.companion.CompanionDeviceManager import android.companion.CompanionDeviceManager
import android.content.Context import android.content.Context
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.hardware.usb.UsbManager
import android.location.LocationManager import android.location.LocationManager
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
@ -26,8 +25,6 @@ val Context.companionDeviceManager: CompanionDeviceManager?
@SuppressLint("NewApi") @SuppressLint("NewApi")
get() = getSystemService(Context.COMPANION_DEVICE_SERVICE).takeIf { hasCompanionDeviceApi() } as? CompanionDeviceManager? get() = getSystemService(Context.COMPANION_DEVICE_SERVICE).takeIf { hasCompanionDeviceApi() } as? CompanionDeviceManager?
val Context.usbManager: UsbManager get() = requireNotNull(getSystemService(Context.USB_SERVICE) as? UsbManager?) { "USB_SERVICE is not available"}
val Context.notificationManager: NotificationManager get() = requireNotNull(getSystemService(Context.NOTIFICATION_SERVICE) as? NotificationManager?) val Context.notificationManager: NotificationManager get() = requireNotNull(getSystemService(Context.NOTIFICATION_SERVICE) as? NotificationManager?)
val Context.locationManager: LocationManager get() = requireNotNull(getSystemService(Context.LOCATION_SERVICE) as? LocationManager?) val Context.locationManager: LocationManager get() = requireNotNull(getSystemService(Context.LOCATION_SERVICE) as? LocationManager?)

Wyświetl plik

@ -1,11 +1,11 @@
package com.geeksville.mesh.repository.radio package com.geeksville.mesh.repository.radio
import android.app.Application import android.app.Application
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothGattCharacteristic import android.bluetooth.BluetoothGattCharacteristic
import android.bluetooth.BluetoothGattService import android.bluetooth.BluetoothGattService
import com.geeksville.mesh.android.Logging import com.geeksville.mesh.android.Logging
import com.geeksville.mesh.concurrent.handledLaunch import com.geeksville.mesh.concurrent.handledLaunch
import com.geeksville.mesh.repository.bluetooth.BluetoothRepository
import com.geeksville.mesh.service.* import com.geeksville.mesh.service.*
import com.geeksville.mesh.util.anonymize import com.geeksville.mesh.util.anonymize
import com.geeksville.mesh.util.exceptionReporter import com.geeksville.mesh.util.exceptionReporter
@ -79,7 +79,7 @@ A variable keepAllPackets, if set to true will suppress this behavior and instea
*/ */
class BluetoothInterface @AssistedInject constructor( class BluetoothInterface @AssistedInject constructor(
context: Application, context: Application,
bluetoothAdapter: dagger.Lazy<BluetoothAdapter?>, bluetoothRepository: BluetoothRepository,
private val service: RadioInterfaceService, private val service: RadioInterfaceService,
@Assisted val address: String, @Assisted val address: String,
) : IRadioInterface, Logging { ) : IRadioInterface, Logging {
@ -136,7 +136,7 @@ class BluetoothInterface @AssistedInject constructor(
init { init {
// Note: this call does no comms, it just creates the device object (even if the // Note: this call does no comms, it just creates the device object (even if the
// device is off/not connected) // device is off/not connected)
val device = bluetoothAdapter.get()?.getRemoteDevice(address) val device = bluetoothRepository.getRemoteDevice(address)
if (device != null) { if (device != null) {
info("Creating radio interface service. device=${address.anonymize}") info("Creating radio interface service. device=${address.anonymize}")

Wyświetl plik

@ -1,8 +1,7 @@
package com.geeksville.mesh.repository.radio package com.geeksville.mesh.repository.radio
import android.annotation.SuppressLint
import android.bluetooth.BluetoothAdapter
import com.geeksville.mesh.android.Logging import com.geeksville.mesh.android.Logging
import com.geeksville.mesh.repository.bluetooth.BluetoothRepository
import com.geeksville.mesh.util.anonymize import com.geeksville.mesh.util.anonymize
import javax.inject.Inject import javax.inject.Inject
@ -11,17 +10,15 @@ import javax.inject.Inject
*/ */
class BluetoothInterfaceSpec @Inject constructor( class BluetoothInterfaceSpec @Inject constructor(
private val factory: BluetoothInterfaceFactory, private val factory: BluetoothInterfaceFactory,
private val bluetoothAdapter: dagger.Lazy<BluetoothAdapter?> private val bluetoothRepository: BluetoothRepository,
): InterfaceSpec<BluetoothInterface>, Logging { ): InterfaceSpec<BluetoothInterface>, Logging {
override fun createInterface(rest: String): BluetoothInterface { override fun createInterface(rest: String): BluetoothInterface {
return factory.create(rest) return factory.create(rest)
} }
/** Return true if this address is still acceptable. For BLE that means, still bonded */ /** Return true if this address is still acceptable. For BLE that means, still bonded */
@SuppressLint("MissingPermission")
override fun addressValid(rest: String): Boolean { override fun addressValid(rest: String): Boolean {
val allPaired = bluetoothAdapter.get()?.bondedDevices.orEmpty() val allPaired = bluetoothRepository.state.value.bondedDevices
.map { it.address }.toSet() .map { it.address }.toSet()
return if (!allPaired.contains(rest)) { return if (!allPaired.contains(rest)) {
warn("Ignoring stale bond to ${rest.anonymize}") warn("Ignoring stale bond to ${rest.anonymize}")