Merge branch 'master' into bluetooth-state-receiver-readability

This commit is contained in:
Kevin Hester 2020-09-19 07:42:50 -07:00 committed by GitHub
commit c1a62097f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 205 additions and 29 deletions

View file

@ -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"}

View file

@ -17,12 +17,23 @@ import com.geeksville.mesh.MyNodeInfo
import com.geeksville.mesh.service.MeshService
/// Given a human name, strip out the first letter of the first three words and return that as the initials for
/// that user.
fun getInitials(name: String): String {
val words = name.split(Regex("\\s+")).filter { it.isNotEmpty() }.take(3).map { it.first() }
.joinToString("")
/// that user. If the original name is only one word, strip vowels from the original name and if the result is
/// 3 or more characters, use the first three characters. If not, just take the first 3 characters of the
/// original name.
fun getInitials(nameIn: String): String {
val nchars = 3
val minchars = 2
val name = nameIn.trim()
val words = name.split(Regex("\\s+")).filter { it.isNotEmpty() }
return words
val initials = when (words.size) {
in 0..minchars - 1 -> {
val nm = name.filterNot { c -> c.toLowerCase() in "aeiou" }
if (nm.length >= nchars) nm else name
}
else -> words.map { it.first() }.joinToString("")
}
return initials.take(nchars)
}
class UIViewModel(app: Application) : AndroidViewModel(app), Logging {

View file

@ -278,11 +278,18 @@ class MeshService : Service(), Logging {
warnUserAboutLocation()
}
is ApiException ->
if (exception.statusCode == 17) {
// error: cancelled by user
errormsg("User cancelled location access", exception)
} else {
Exceptions.report(exception)
when (exception.statusCode) {
17 ->
// error: cancelled by user
errormsg("User cancelled location access", exception)
8502 ->
// error: settings change unavailable
errormsg(
"Settings-change-unavailable, user disabled location access (globally?)",
exception
)
else ->
Exceptions.report(exception)
}
else ->
Exceptions.report(exception)

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()

View file

@ -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) {}