fix: CompanionDeviceManager context

master
andrekir 2023-04-01 07:58:37 -03:00
rodzic ca7e459dfe
commit d4c125b962
2 zmienionych plików z 5 dodań i 11 usunięć

Wyświetl plik

@ -1,6 +1,7 @@
package com.geeksville.mesh.android package com.geeksville.mesh.android
import android.Manifest import android.Manifest
import android.annotation.SuppressLint
import android.app.NotificationManager import android.app.NotificationManager
import android.bluetooth.BluetoothManager import android.bluetooth.BluetoothManager
import android.location.LocationManager import android.location.LocationManager
@ -9,7 +10,6 @@ import android.content.Context
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.hardware.usb.UsbManager import android.hardware.usb.UsbManager
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import com.geeksville.mesh.MainActivity
import com.geeksville.mesh.R import com.geeksville.mesh.R
/** /**
@ -18,14 +18,9 @@ import com.geeksville.mesh.R
val Context.bluetoothManager: BluetoothManager? val Context.bluetoothManager: BluetoothManager?
get() = getSystemService(Context.BLUETOOTH_SERVICE).takeIf { hasBluetoothPermission() } as? BluetoothManager? get() = getSystemService(Context.BLUETOOTH_SERVICE).takeIf { hasBluetoothPermission() } as? BluetoothManager?
val Context.deviceManager: CompanionDeviceManager? val Context.companionDeviceManager: CompanionDeviceManager?
get() { @SuppressLint("NewApi")
if (GeeksvilleApplication.currentActivity is MainActivity) { get() = getSystemService(Context.COMPANION_DEVICE_SERVICE).takeIf { hasCompanionDeviceApi() } as? CompanionDeviceManager?
val activity = GeeksvilleApplication.currentActivity
if (hasCompanionDeviceApi()) return activity?.getSystemService(Context.COMPANION_DEVICE_SERVICE) as? CompanionDeviceManager?
}
return null
}
val Context.usbManager: UsbManager get() = requireNotNull(getSystemService(Context.USB_SERVICE) as? UsbManager?) { "USB_SERVICE is not available"} val Context.usbManager: UsbManager get() = requireNotNull(getSystemService(Context.USB_SERVICE) as? UsbManager?) { "USB_SERVICE is not available"}

Wyświetl plik

@ -131,7 +131,6 @@ class BTScanModel @Inject constructor(
debug("BTScanModel cleared") debug("BTScanModel cleared")
} }
private val deviceManager get() = context.deviceManager
val hasCompanionDeviceApi get() = application.hasCompanionDeviceApi() val hasCompanionDeviceApi get() = application.hasCompanionDeviceApi()
val hasBluetoothPermission get() = application.hasBluetoothPermission() val hasBluetoothPermission get() = application.hasBluetoothPermission()
private val usbManager get() = context.usbManager private val usbManager get() = context.usbManager
@ -373,7 +372,7 @@ class BTScanModel @Inject constructor(
@SuppressLint("NewApi") @SuppressLint("NewApi")
private fun startCompanionScan() { private fun startCompanionScan() {
debug("starting companion scan") debug("starting companion scan")
deviceManager?.associate( context.companionDeviceManager?.associate(
associationRequest(), associationRequest(),
@SuppressLint("NewApi") @SuppressLint("NewApi")
object : CompanionDeviceManager.Callback() { object : CompanionDeviceManager.Callback() {