simplified calls to system services

and fixed some code that assumed BluetoothManager to never be NULL
This commit is contained in:
Ken Van Hoeylandt 2020-09-17 19:49:50 +02:00
parent b98a8ea34b
commit f97bc21719
4 changed files with 22 additions and 15 deletions

View file

@ -10,6 +10,7 @@ import com.geeksville.android.Logging
import com.geeksville.concurrent.CallbackContinuation
import com.geeksville.concurrent.Continuation
import com.geeksville.concurrent.SyncContinuation
import com.geeksville.mesh.android.bluetoothManager
import com.geeksville.util.exceptionReporter
import kotlinx.coroutines.*
import java.io.Closeable
@ -101,10 +102,7 @@ class SafeBluetooth(private val context: Context, private val device: BluetoothD
fun restartBle() {
GeeksvilleApplication.analytics.track("ble_restart") // record # of times we needed to use this nasty hack
errormsg("Doing emergency BLE restart")
val mgr =
context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
val adp = mgr.adapter
if (null != adp) {
context.bluetoothManager?.adapter?.let { adp ->
if (adp.isEnabled) {
adp.disable()
// TODO: display some kind of UI about restarting BLE

View file

@ -7,6 +7,7 @@ import android.content.IntentFilter
import android.hardware.usb.UsbDevice
import android.hardware.usb.UsbManager
import com.geeksville.android.Logging
import com.geeksville.mesh.android.usbManager
import com.geeksville.util.exceptionReporter
import com.geeksville.util.ignoreException
import com.hoho.android.usbserial.driver.UsbSerialDriver
@ -32,8 +33,7 @@ class SerialInterface(private val service: RadioInterfaceService, val address: S
fun toInterfaceName(deviceName: String) = "s$deviceName"
fun findDrivers(context: Context): List<UsbSerialDriver> {
val manager = context.getSystemService(Context.USB_SERVICE) as UsbManager
val drivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager)
val drivers = UsbSerialProber.getDefaultProber().findAllDrivers(context.usbManager)
val devices = drivers.map { it.device }
devices.forEach { d ->
debug("Found serial port ${d.deviceName}")
@ -43,8 +43,7 @@ class SerialInterface(private val service: RadioInterfaceService, val address: S
fun addressValid(context: Context, rest: String): Boolean {
findSerial(context, rest)?.let { d ->
val manager = context.getSystemService(Context.USB_SERVICE) as UsbManager
return assumePermission || manager.hasPermission(d.device)
return assumePermission || context.usbManager.hasPermission(d.device)
}
return false
}
@ -75,8 +74,7 @@ class SerialInterface(private val service: RadioInterfaceService, val address: S
if (UsbManager.ACTION_USB_DEVICE_ATTACHED == intent.action) {
debug("attaching USB")
val device: UsbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE)!!
val manager = context.getSystemService(Context.USB_SERVICE) as UsbManager
if (assumePermission || manager.hasPermission(device)) {
if (assumePermission || context.usbManager.hasPermission(device)) {
// reinit the port from scratch and reopen
onDeviceDisconnect(true)
connect()