mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-03-30 19:54:27 -04:00
Merge pull request #163 from KenVanHoeylandt/cleanup-service-calls
Simplified calls to system services
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package com.geeksville.mesh.android
|
||||
|
||||
import android.bluetooth.BluetoothManager
|
||||
import android.content.Context
|
||||
import android.hardware.usb.UsbManager
|
||||
|
||||
/**
|
||||
* @return null on platforms without a BlueTooth driver (i.e. the emulator)
|
||||
*/
|
||||
val Context.bluetoothManager: BluetoothManager? get() = getSystemService(Context.BLUETOOTH_SERVICE) as? BluetoothManager?
|
||||
|
||||
val Context.usbManager: UsbManager get() = requireNotNull(getSystemService(Context.USB_SERVICE) as? UsbManager?) { "USB_SERVICE is not available"}
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -33,6 +33,8 @@ import com.geeksville.android.isGooglePlayAvailable
|
||||
import com.geeksville.concurrent.handledLaunch
|
||||
import com.geeksville.mesh.MainActivity
|
||||
import com.geeksville.mesh.R
|
||||
import com.geeksville.mesh.android.bluetoothManager
|
||||
import com.geeksville.mesh.android.usbManager
|
||||
import com.geeksville.mesh.model.UIViewModel
|
||||
import com.geeksville.mesh.service.BluetoothInterface
|
||||
import com.geeksville.mesh.service.MeshService
|
||||
@@ -137,11 +139,8 @@ class BTScanModel(app: Application) : AndroidViewModel(app), Logging {
|
||||
debug("BTScanModel cleared")
|
||||
}
|
||||
|
||||
/// Note: may be null on platforms without a bluetooth driver (ie. the emulator)
|
||||
val bluetoothAdapter =
|
||||
(context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?)?.adapter
|
||||
|
||||
private val usbManager = context.getSystemService(Context.USB_SERVICE) as UsbManager
|
||||
val bluetoothAdapter = context.bluetoothManager?.adapter
|
||||
private val usbManager get() = context.usbManager
|
||||
|
||||
var selectedAddress: String? = null
|
||||
val errorText = object : MutableLiveData<String?>(null) {}
|
||||
|
||||
Reference in New Issue
Block a user