move hasCompanionDeviceApi out of BluetoothInterface

This commit is contained in:
andrekir
2022-04-28 21:40:34 -03:00
parent eaff87eed3
commit aaa5c1cf04
3 changed files with 11 additions and 71 deletions

View File

@@ -8,7 +8,6 @@ import android.content.pm.PackageManager
import android.hardware.usb.UsbManager
import android.os.Build
import androidx.core.content.ContextCompat
import com.geeksville.mesh.repository.radio.BluetoothInterface
/**
* @return null on platforms without a BlueTooth driver (i.e. the emulator)
@@ -19,6 +18,14 @@ val Context.usbManager: UsbManager get() = requireNotNull(getSystemService(Conte
val Context.notificationManager: NotificationManager get() = requireNotNull(getSystemService(Context.NOTIFICATION_SERVICE) as? NotificationManager?)
/**
* @return true if CompanionDeviceManager API is present
*/
fun Context.hasCompanionDeviceApi(): Boolean =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
packageManager.hasSystemFeature(PackageManager.FEATURE_COMPANION_DEVICE_SETUP)
else false
/**
* return a list of the permissions we don't have
*/
@@ -62,7 +69,7 @@ fun Context.getScanPermissions(): List<String> {
perms.add(Manifest.permission.BLUETOOTH_ADMIN)
}
*/
if (!BluetoothInterface.hasCompanionDeviceApi(this)) {
if (!hasCompanionDeviceApi()) {
perms.add(Manifest.permission.ACCESS_FINE_LOCATION)
perms.add(Manifest.permission.BLUETOOTH_ADMIN)
}

View File

@@ -118,12 +118,6 @@ class BluetoothInterface(val service: RadioInterfaceService, val address: String
usbRepository: UsbRepository, // Temporary until dependency injection transition is completed
rest: String
): Boolean {
/* val allPaired = if (hasCompanionDeviceApi(context)) {
val deviceManager: CompanionDeviceManager by lazy {
context.getSystemService(Context.COMPANION_DEVICE_SERVICE) as CompanionDeviceManager
}
deviceManager.associations.map { it }.toSet()
} else { */
val allPaired = getBluetoothAdapter(context)?.bondedDevices.orEmpty()
.map { it.address }.toSet()
return if (!allPaired.contains(rest)) {
@@ -133,63 +127,6 @@ class BluetoothInterface(val service: RadioInterfaceService, val address: String
true
}
/// Return the device we are configured to use, or null for none
/*
@SuppressLint("NewApi")
fun getBondedDeviceAddress(context: Context): String? =
if (hasCompanionDeviceApi(context)) {
// Use new companion API
val deviceManager = context.getSystemService(CompanionDeviceManager::class.java)
val associations = deviceManager.associations
val result = associations.firstOrNull()
debug("reading bonded devices: $result")
result
} else {
// Use classic API and a preferences string
val allPaired =
getBluetoothAdapter(context)?.bondedDevices.orEmpty().map { it.address }.toSet()
// If the user has unpaired our device, treat things as if we don't have one
val address = InterfaceService.getPrefs(context).getString(DEVADDR_KEY, null)
if (address != null && !allPaired.contains(address)) {
warn("Ignoring stale bond to ${address.anonymize}")
null
} else
address
}
*/
/// Can we use the modern BLE scan API?
fun hasCompanionDeviceApi(context: Context): Boolean =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val res =
context.packageManager.hasSystemFeature(PackageManager.FEATURE_COMPANION_DEVICE_SETUP)
debug("CompanionDevice API available=$res")
res
} else {
warn("CompanionDevice API not available, falling back to classic scan")
false
}
/** FIXME - when adding companion device support back in, use this code to set companion device from setBondedDevice
* if (BluetoothInterface.hasCompanionDeviceApi(this)) {
// We only keep an association to one device at a time...
if (addr != null) {
val deviceManager = getSystemService(CompanionDeviceManager::class.java)
deviceManager.associations.forEach { old ->
if (addr != old) {
BluetoothInterface.debug("Forgetting old BLE association $old")
deviceManager.disassociate(old)
}
}
}
*/
/**
* this is created in onCreate()
* We do an ugly hack of keeping it in the singleton so we can share it for the rare software update case

View File

@@ -37,7 +37,6 @@ import com.geeksville.mesh.android.*
import com.geeksville.mesh.databinding.SettingsFragmentBinding
import com.geeksville.mesh.model.BluetoothViewModel
import com.geeksville.mesh.model.UIViewModel
import com.geeksville.mesh.repository.radio.BluetoothInterface
import com.geeksville.mesh.repository.radio.MockInterface
import com.geeksville.mesh.repository.radio.RadioInterfaceService
import com.geeksville.mesh.repository.radio.SerialInterface
@@ -154,6 +153,7 @@ class BTScanModel @Inject constructor(
}
val bluetoothAdapter = context.bluetoothManager?.adapter
val hasCompanionDeviceApi get() = context.hasCompanionDeviceApi()
private val usbManager get() = context.usbManager
var selectedAddress: String? = null
@@ -466,10 +466,6 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
@Inject
internal lateinit var usbRepository: UsbRepository
private val hasCompanionDeviceApi: Boolean by lazy {
BluetoothInterface.hasCompanionDeviceApi(requireContext())
}
private val deviceManager: CompanionDeviceManager by lazy {
requireContext().getSystemService(Context.COMPANION_DEVICE_SERVICE) as CompanionDeviceManager
}
@@ -958,7 +954,7 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
super.onViewCreated(view, savedInstanceState)
initCommonUI()
if (hasCompanionDeviceApi)
if (scanModel.hasCompanionDeviceApi)
initModernScan()
else
initClassicScan()