Meshtastic-Android/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt

987 lines
39 KiB
Kotlin
Raw Normal View History

2020-02-13 09:25:39 -08:00
package com.geeksville.mesh.ui
import android.annotation.SuppressLint
2020-04-08 21:17:23 -07:00
import android.app.Application
2020-06-08 14:04:56 -07:00
import android.app.PendingIntent
2020-04-08 21:17:23 -07:00
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothDevice.BOND_BONDED
import android.bluetooth.BluetoothDevice.BOND_BONDING
2020-04-08 21:17:23 -07:00
import android.bluetooth.le.*
import android.content.*
2021-12-15 12:02:01 -03:00
import android.content.pm.PackageManager
2020-06-08 14:04:56 -07:00
import android.hardware.usb.UsbDevice
import android.hardware.usb.UsbManager
2020-04-08 18:42:17 -07:00
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.RemoteException
2020-04-08 18:42:17 -07:00
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2020-04-09 11:03:17 -07:00
import android.view.inputmethod.EditorInfo
2021-03-04 09:08:29 +08:00
import android.widget.AdapterView
2021-03-03 13:51:33 +08:00
import android.widget.ArrayAdapter
2020-04-08 21:17:23 -07:00
import android.widget.RadioButton
2022-01-24 14:56:17 -03:00
import android.widget.TextView
2020-04-08 18:42:17 -07:00
import androidx.fragment.app.activityViewModels
2020-04-08 21:17:23 -07:00
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.geeksville.android.GeeksvilleApplication
2020-04-08 18:42:17 -07:00
import com.geeksville.android.Logging
2020-04-09 11:03:17 -07:00
import com.geeksville.android.hideKeyboard
2020-07-15 15:58:53 -07:00
import com.geeksville.android.isGooglePlayAvailable
import com.geeksville.mesh.MainActivity
2020-04-08 18:42:17 -07:00
import com.geeksville.mesh.R
import com.geeksville.mesh.RadioConfigProtos
import com.geeksville.mesh.android.*
import com.geeksville.mesh.databinding.SettingsFragmentBinding
2020-04-09 11:03:17 -07:00
import com.geeksville.mesh.model.UIViewModel
2021-03-29 20:45:11 +08:00
import com.geeksville.mesh.service.*
import com.geeksville.mesh.service.SoftwareUpdateService.Companion.ACTION_UPDATE_PROGRESS
import com.geeksville.mesh.service.SoftwareUpdateService.Companion.ProgressNotStarted
import com.geeksville.mesh.service.SoftwareUpdateService.Companion.ProgressSuccess
2020-05-30 14:38:16 -07:00
import com.geeksville.util.anonymize
2020-04-08 21:17:23 -07:00
import com.geeksville.util.exceptionReporter
import com.geeksville.util.exceptionToSnackbar
import com.google.android.gms.location.LocationRequest
import com.google.android.gms.location.LocationServices
import com.google.android.gms.location.LocationSettingsRequest
2020-04-15 14:10:40 -07:00
import com.google.android.material.dialog.MaterialAlertDialogBuilder
2022-01-24 14:56:17 -03:00
import com.google.android.material.snackbar.Snackbar
import com.hoho.android.usbserial.driver.UsbSerialDriver
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
2020-04-08 18:42:17 -07:00
2021-03-29 20:33:06 +08:00
object SLogging : Logging
/// Change to a new macaddr selection, updating GUI and radio
fun changeDeviceSelection(context: MainActivity, newAddr: String?) {
// FIXME, this is a kinda yucky way to find the service
context.model.meshService?.let { service ->
MeshService.changeDeviceAddress(context, service, newAddr)
}
}
/// Show the UI asking the user to bond with a device, call changeSelection() if/when bonding completes
@SuppressLint("MissingPermission")
private fun requestBonding(
activity: MainActivity,
device: BluetoothDevice,
onComplete: (Int) -> Unit
) {
2020-07-29 16:16:29 -07:00
SLogging.info("Starting bonding for ${device.anonymize}")
// We need this receiver to get informed when the bond attempt finished
val bondChangedReceiver = object : BroadcastReceiver() {
override fun onReceive(
context: Context,
intent: Intent
) = exceptionReporter {
val state =
intent.getIntExtra(
BluetoothDevice.EXTRA_BOND_STATE,
-1
)
SLogging.debug("Received bond state changed $state")
if (state != BOND_BONDING) {
context.unregisterReceiver(this) // we stay registered until bonding completes (either with BONDED or NONE)
2020-04-23 09:03:44 -07:00
SLogging.debug("Bonding completed, state=$state")
onComplete(state)
}
}
}
val filter = IntentFilter()
filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED)
activity.registerReceiver(bondChangedReceiver, filter)
// We ignore missing BT adapters, because it lets us run on the emulator
try {
device.createBond()
} catch (ex: Throwable) {
SLogging.warn("Failed creating Bluetooth bond: ${ex.message}")
}
}
2020-04-08 21:17:23 -07:00
class BTScanModel(app: Application) : AndroidViewModel(app), Logging {
2020-04-08 18:42:17 -07:00
2021-02-21 11:34:43 +08:00
private val context: Context get() = getApplication<Application>().applicationContext
2020-04-08 18:42:17 -07:00
2020-04-08 21:17:23 -07:00
init {
debug("BTScanModel created")
2020-04-08 18:42:17 -07:00
}
2020-02-18 08:56:53 -08:00
open class DeviceListEntry(val name: String, val address: String, val bonded: Boolean) {
2020-06-08 14:19:49 -07:00
val bluetoothAddress
get() =
if (isBluetooth)
2020-06-08 14:19:49 -07:00
address.substring(1)
else
null
override fun toString(): String {
return "DeviceListEntry(name=${name.anonymize}, addr=${address.anonymize})"
}
val isBluetooth: Boolean get() = address[0] == 'x'
val isSerial: Boolean get() = address[0] == 's'
2020-02-18 08:56:53 -08:00
}
class USBDeviceListEntry(usbManager: UsbManager, val usb: UsbSerialDriver) : DeviceListEntry(
usb.device.deviceName,
SerialInterface.toInterfaceName(usb.device.deviceName),
SerialInterface.assumePermission || usbManager.hasPermission(usb.device)
)
2020-04-08 21:17:23 -07:00
override fun onCleared() {
super.onCleared()
debug("BTScanModel cleared")
2020-02-18 08:56:53 -08:00
}
2020-02-13 09:25:39 -08:00
val bluetoothAdapter = context.bluetoothManager?.adapter
private val usbManager get() = context.usbManager
2020-06-08 14:04:56 -07:00
var selectedAddress: String? = null
2020-04-08 21:17:23 -07:00
val errorText = object : MutableLiveData<String?>(null) {}
2020-04-09 11:03:17 -07:00
private var scanner: BluetoothLeScanner? = null
2020-04-08 21:17:23 -07:00
/// If this address is for a bluetooth device, return the macaddr portion, else null
val selectedBluetooth: String?
get() = selectedAddress?.let { a ->
if (a[0] == 'x')
a.substring(1)
else
null
}
/// If this address is for a USB device, return the macaddr portion, else null
val selectedUSB: String?
get() = selectedAddress?.let { a ->
if (a[0] == 's')
a.substring(1)
else
null
}
/// Use the string for the NopInterface
val selectedNotNull: String get() = selectedAddress ?: "n"
2020-04-09 11:03:17 -07:00
private val scanCallback = object : ScanCallback() {
override fun onScanFailed(errorCode: Int) {
val msg = "Unexpected bluetooth scan failure: $errorCode"
2020-06-28 16:09:56 -07:00
errormsg(msg)
2020-04-09 11:03:17 -07:00
// error code2 seeems to be indicate hung bluetooth stack
errorText.value = msg
}
2020-02-18 09:09:49 -08:00
2020-04-09 11:03:17 -07:00
// For each device that appears in our scan, ask for its GATT, when the gatt arrives,
// check if it is an eligable device and store it in our list of candidates
// if that device later disconnects remove it as a candidate
@SuppressLint("MissingPermission")
2020-04-09 11:03:17 -07:00
override fun onScanResult(callbackType: Int, result: ScanResult) {
2021-03-29 20:33:06 +08:00
if ((result.device.name?.startsWith("Mesh") == true)) {
val addr = result.device.address
2022-01-07 18:51:20 -03:00
val fullAddr = "x$addr" // full address with the bluetooth prefix added
// prevent logspam because weill get get lots of redundant scan results
val isBonded = result.device.bondState == BOND_BONDED
val oldDevs = devices.value!!
val oldEntry = oldDevs[fullAddr]
if (oldEntry == null || oldEntry.bonded != isBonded) { // Don't spam the GUI with endless updates for non changing nodes
val entry = DeviceListEntry(
result.device.name
?: "unnamed-$addr", // autobug: some devices might not have a name, if someone is running really old device code?
fullAddr,
isBonded
)
// If nothing was selected, by default select the first valid thing we see
val activity: MainActivity? = try {
GeeksvilleApplication.currentActivity as MainActivity? // Can be null if app is shutting down
} catch (_: ClassCastException) {
// Buggy "Z812" phones apparently have the wrong class type for this
errormsg("Unexpected class for main activity")
null
}
if (selectedAddress == null && entry.bonded && activity != null)
changeScanSelection(
activity,
fullAddr
)
addDevice(entry) // Add/replace entry
}
2020-02-18 09:09:49 -08:00
}
}
2020-04-09 11:03:17 -07:00
}
2020-02-13 19:02:40 -08:00
private fun addDevice(entry: DeviceListEntry) {
val oldDevs = devices.value!!
oldDevs[entry.address] = entry // Add/replace entry
devices.value = oldDevs // trigger gui updates
}
@SuppressLint("MissingPermission")
2020-04-09 11:03:17 -07:00
fun stopScan() {
if (scanner != null) {
debug("stopping scan")
try {
scanner?.stopScan(scanCallback)
} catch (ex: Throwable) {
warn("Ignoring error stopping scan, probably BT adapter was disabled suddenly: ${ex.message}")
2020-04-08 21:17:23 -07:00
}
2020-04-09 11:03:17 -07:00
scanner = null
2020-04-08 21:17:23 -07:00
}
2020-04-09 11:03:17 -07:00
}
2020-02-13 19:02:40 -08:00
/**
* returns true if we could start scanning, false otherwise
*/
2022-01-08 16:56:41 -03:00
fun setupScan(): Boolean {
selectedAddress = RadioInterfaceService.getDeviceAddress(context)
2020-02-13 19:02:40 -08:00
2021-03-29 20:45:11 +08:00
return if (bluetoothAdapter == null || MockInterface.addressValid(context, "")) {
2020-04-09 11:03:17 -07:00
warn("No bluetooth adapter. Running under emulation?")
2020-04-08 21:17:23 -07:00
2020-04-09 11:03:17 -07:00
val testnodes = listOf(
DeviceListEntry("Included simulator", "m", true),
DeviceListEntry("Complete simulator", "t10.0.2.2", true),
DeviceListEntry(context.getString(R.string.none), "n", true)
/* Don't populate fake bluetooth devices, because we don't want testlab inside of google
to try and use them.
DeviceListEntry("Meshtastic_ab12", "xaa", false),
DeviceListEntry("Meshtastic_32ac", "xbb", true) */
2020-04-09 11:03:17 -07:00
)
devices.value = (testnodes.map { it.address to it }).toMap().toMutableMap()
2020-04-08 21:17:23 -07:00
2020-04-09 11:03:17 -07:00
// If nothing was selected, by default select the first thing we see
if (selectedAddress == null)
changeScanSelection(
GeeksvilleApplication.currentActivity as MainActivity,
testnodes.first().address
)
true
2020-04-09 11:03:17 -07:00
} else {
val usbDrivers = SerialInterface.findDrivers(context)
/* model.bluetoothEnabled.value */
2022-01-08 16:56:41 -03:00
if (bluetoothAdapter.bluetoothLeScanner == null && usbDrivers.isEmpty()) {
2020-04-09 11:03:17 -07:00
errorText.value =
2020-04-09 11:27:42 -07:00
context.getString(R.string.requires_bluetooth)
false
} else {
if (scanner == null) {
// Clear the old device list
devices.value?.clear()
// Include a placeholder for "None"
addDevice(DeviceListEntry(context.getString(R.string.none), "n", true))
usbDrivers.forEach { d ->
2020-06-08 14:04:56 -07:00
addDevice(
USBDeviceListEntry(usbManager, d)
2020-06-08 14:04:56 -07:00
)
}
} else {
debug("scan already running")
}
true
2020-04-08 21:17:23 -07:00
}
}
2020-04-09 11:03:17 -07:00
}
@SuppressLint("MissingPermission")
2022-01-08 16:56:41 -03:00
fun startScan() {
/// The following call might return null if the user doesn't have bluetooth access permissions
val bluetoothLeScanner: BluetoothLeScanner? = bluetoothAdapter?.bluetoothLeScanner
if (bluetoothLeScanner != null) { // could be null if bluetooth is disabled
debug("starting scan")
// filter and only accept devices that have our service
val filter =
ScanFilter.Builder()
// Samsung doesn't seem to filter properly by service so this can't work
// see https://stackoverflow.com/questions/57981986/altbeacon-android-beacon-library-not-working-after-device-has-screen-off-for-a-s/57995960#57995960
// and https://stackoverflow.com/a/45590493
// .setServiceUuid(ParcelUuid(BluetoothInterface.BTM_SERVICE_UUID))
.build()
val settings =
ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.build()
bluetoothLeScanner.startScan(listOf(filter), settings, scanCallback)
scanner = bluetoothLeScanner
}
}
val devices = object : MutableLiveData<MutableMap<String, DeviceListEntry>>(mutableMapOf()) {
2020-04-09 11:03:17 -07:00
2020-04-08 21:17:23 -07:00
/**
* Called when the number of active observers change from 1 to 0.
*
*
* This does not mean that there are no observers left, there may still be observers but their
* lifecycle states aren't [Lifecycle.State.STARTED] or [Lifecycle.State.RESUMED]
* (like an Activity in the back stack).
*
*
* You can check if there are observers via [.hasObservers].
*/
override fun onInactive() {
super.onInactive()
2022-01-08 17:50:48 -03:00
// stopScan()
2020-04-08 21:17:23 -07:00
}
}
/// Called by the GUI when a new device has been selected by the user
/// Returns true if we were able to change to that item
fun onSelected(activity: MainActivity, it: DeviceListEntry): Boolean {
2020-04-08 21:17:23 -07:00
// If the device is paired, let user select it, otherwise start the pairing flow
if (it.bonded) {
changeScanSelection(activity, it.address)
2020-04-08 21:17:23 -07:00
return true
} else {
2020-06-08 14:04:56 -07:00
// Handle requestng USB or bluetooth permissions for the device
debug("Requesting permissions for the device")
2020-06-08 14:04:56 -07:00
exceptionReporter {
val bleAddress = it.bluetoothAddress
if (bleAddress != null) {
// Request bonding for bluetooth
// We ignore missing BT adapters, because it lets us run on the emulator
bluetoothAdapter
?.getRemoteDevice(bleAddress)?.let { device ->
requestBonding(activity, device) { state ->
if (state == BOND_BONDED) {
errorText.value = activity.getString(R.string.pairing_completed)
changeScanSelection(
activity,
it.address
)
} else {
errorText.value =
activity.getString(R.string.pairing_failed_try_again)
}
2020-06-08 14:04:56 -07:00
// Force the GUI to redraw
devices.value = devices.value
}
}
}
2020-06-08 14:04:56 -07:00
}
2020-04-09 12:22:41 -07:00
2020-06-08 14:04:56 -07:00
if (it.isSerial) {
it as USBDeviceListEntry
2020-06-08 14:04:56 -07:00
val ACTION_USB_PERMISSION = "com.geeksville.mesh.USB_PERMISSION"
val usbReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (ACTION_USB_PERMISSION == intent.action) {
val device: UsbDevice =
intent.getParcelableExtra(UsbManager.EXTRA_DEVICE)!!
2020-06-08 14:04:56 -07:00
if (intent.getBooleanExtra(
UsbManager.EXTRA_PERMISSION_GRANTED,
false
)
) {
info("User approved USB access")
changeScanSelection(activity, it.address)
2020-06-08 14:04:56 -07:00
// Force the GUI to redraw
devices.value = devices.value
2020-06-08 14:04:56 -07:00
} else {
errormsg("USB permission denied for device $device")
}
}
// We don't need to stay registered
activity.unregisterReceiver(this)
2020-04-08 21:17:23 -07:00
}
}
2020-06-08 14:04:56 -07:00
val permissionIntent =
PendingIntent.getBroadcast(activity, 0, Intent(ACTION_USB_PERMISSION), 0)
val filter = IntentFilter(ACTION_USB_PERMISSION)
activity.registerReceiver(usbReceiver, filter)
usbManager.requestPermission(it.usb.device, permissionIntent)
2020-06-08 14:04:56 -07:00
}
2020-04-08 21:17:23 -07:00
return false
}
}
/// Change to a new macaddr selection, updating GUI and radio
fun changeScanSelection(context: MainActivity, newAddr: String) {
try {
info("Changing device to ${newAddr.anonymize}")
changeDeviceSelection(context, newAddr)
selectedAddress =
newAddr // do this after changeDeviceSelection, so if it throws the change will be discarded
devices.value = devices.value // Force a GUI update
} catch (ex: RemoteException) {
errormsg("Failed talking to service, probably it is shutting down $ex.message")
// ignore the failure and the GUI won't be updating anyways
}
2020-04-08 21:17:23 -07:00
}
}
@SuppressLint("NewApi")
2020-04-08 21:17:23 -07:00
class SettingsFragment : ScreenFragment("Settings"), Logging {
private var _binding: SettingsFragmentBinding? = null
// This property is only valid between onCreateView and onDestroyView.
private val binding get() = _binding!!
2020-04-08 21:17:23 -07:00
private val scanModel: BTScanModel by activityViewModels()
2020-04-09 11:03:17 -07:00
private val model: UIViewModel by activityViewModels()
2020-04-08 21:17:23 -07:00
// FIXME - move this into a standard GUI helper class
private val guiJob = Job()
private val mainScope = CoroutineScope(Dispatchers.Main + guiJob)
private val hasCompanionDeviceApi: Boolean by lazy {
BluetoothInterface.hasCompanionDeviceApi(requireContext())
}
2021-06-23 11:40:15 -07:00
private val myActivity get() = requireActivity() as MainActivity
override fun onDestroy() {
guiJob.cancel()
super.onDestroy()
}
private fun doFirmwareUpdate() {
model.meshService?.let { service ->
debug("User started firmware update")
binding.updateFirmwareButton.isEnabled = false // Disable until things complete
binding.updateProgressBar.visibility = View.VISIBLE
binding.updateProgressBar.progress = 0 // start from scratch
exceptionToSnackbar(requireView()) {
// We rely on our broadcast receiver to show progress as this progresses
service.startFirmwareUpdate()
}
}
}
2020-04-08 21:17:23 -07:00
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
2022-01-25 01:34:46 -03:00
): View {
_binding = SettingsFragmentBinding.inflate(inflater, container, false)
return binding.root
2020-04-08 21:17:23 -07:00
}
/// Set the correct update button configuration based on current progress
private fun refreshUpdateButton(enable: Boolean) {
2022-01-03 21:59:30 -03:00
debug("Reiniting the update button")
2020-05-15 10:18:15 -07:00
val info = model.myNodeInfo.value
val service = model.meshService
if (model.isConnected.value == MeshService.ConnectionState.CONNECTED && info != null && info.shouldUpdate && info.couldUpdate && service != null) {
binding.updateFirmwareButton.visibility = View.VISIBLE
binding.updateFirmwareButton.text =
getString(R.string.update_to).format(getString(R.string.short_firmware_version))
val progress = service.updateStatus
binding.updateFirmwareButton.isEnabled = enable &&
(progress < 0) // if currently doing an upgrade disable button
if (progress >= 0) {
binding.updateProgressBar.progress = progress // update partial progress
binding.scanStatusText.setText(R.string.updating_firmware)
binding.updateProgressBar.visibility = View.VISIBLE
} else
when (progress) {
ProgressSuccess -> {
binding.scanStatusText.setText(R.string.update_successful)
binding.updateProgressBar.visibility = View.GONE
}
ProgressNotStarted -> {
// Do nothing - because we don't want to overwrite the status text in this case
binding.updateProgressBar.visibility = View.GONE
}
else -> {
binding.scanStatusText.setText(R.string.update_failed)
binding.updateProgressBar.visibility = View.VISIBLE
}
}
binding.updateProgressBar.isEnabled = false
2020-05-15 10:18:15 -07:00
} else {
binding.updateFirmwareButton.visibility = View.GONE
binding.updateProgressBar.visibility = View.GONE
2020-05-15 10:18:15 -07:00
}
}
/**
* Pull the latest device info from the model and into the GUI
*/
private fun updateNodeInfo() {
val connected = model.isConnected.value
val isConnected = connected == MeshService.ConnectionState.CONNECTED
binding.nodeSettings.visibility = if (isConnected) View.VISIBLE else View.GONE
2022-01-07 18:51:20 -03:00
binding.provideLocationCheckbox.visibility = if (isConnected) View.VISIBLE else View.GONE
if (connected == MeshService.ConnectionState.DISCONNECTED)
model.ownerName.value = ""
// update the region selection from the device
val region = model.region
2021-03-04 11:20:51 +08:00
val spinner = binding.regionSpinner
val unsetIndex = regions.indexOf(RadioConfigProtos.RegionCode.Unset.name)
spinner.onItemSelectedListener = null
debug("current region is $region")
var regionIndex = regions.indexOf(region.name)
if (regionIndex == -1) // Not found, probably because the device has a region our app doesn't yet understand. Punt and say Unset
regionIndex = unsetIndex
// We don't want to be notified of our own changes, so turn off listener while making them
spinner.setSelection(regionIndex, false)
spinner.onItemSelectedListener = regionSpinnerListener
spinner.isEnabled = true
// If actively connected possibly let the user update firmware
refreshUpdateButton(region != RadioConfigProtos.RegionCode.Unset)
2020-05-15 10:18:15 -07:00
2021-03-04 11:20:51 +08:00
// Update the status string (highest priority messages first)
val info = model.myNodeInfo.value
2021-03-04 11:20:51 +08:00
val statusText = binding.scanStatusText
2021-06-23 11:40:15 -07:00
val permissionsWarning = myActivity.getMissingMessage()
2021-03-04 11:20:51 +08:00
when {
2022-01-25 15:59:45 -03:00
(permissionsWarning != null) ->
2021-06-23 11:40:15 -07:00
statusText.text = permissionsWarning
2021-03-04 11:20:51 +08:00
region == RadioConfigProtos.RegionCode.Unset ->
statusText.text = getString(R.string.must_set_region)
connected == MeshService.ConnectionState.CONNECTED -> {
val fwStr = info?.firmwareString ?: "unknown"
2021-03-04 11:20:51 +08:00
statusText.text = getString(R.string.connected_to).format(fwStr)
2020-05-15 10:18:15 -07:00
}
2021-03-04 11:20:51 +08:00
connected == MeshService.ConnectionState.DISCONNECTED ->
statusText.text = getString(R.string.not_connected)
connected == MeshService.ConnectionState.DEVICE_SLEEP ->
statusText.text = getString(R.string.connected_sleeping)
2020-05-15 10:18:15 -07:00
}
}
private val regionSpinnerListener = object : AdapterView.OnItemSelectedListener {
2021-03-04 09:08:29 +08:00
override fun onItemSelected(
parent: AdapterView<*>,
view: View,
position: Int,
id: Long
) {
2021-03-29 19:08:42 +08:00
val item = parent.getItemAtPosition(position) as String?
val asProto = item!!.let { RadioConfigProtos.RegionCode.valueOf(it) }
exceptionToSnackbar(requireView()) {
model.region = asProto
}
2021-03-04 11:20:51 +08:00
updateNodeInfo() // We might have just changed Unset to set
2021-03-04 09:08:29 +08:00
}
override fun onNothingSelected(parent: AdapterView<*>) {
2021-03-04 09:08:29 +08:00
//TODO("Not yet implemented")
}
}
2021-03-04 11:20:51 +08:00
/// the sorted list of region names like arrayOf("US", "CN", "EU488")
private val regions = RadioConfigProtos.RegionCode.values().filter {
it != RadioConfigProtos.RegionCode.UNRECOGNIZED
}.map {
it.name
}.sorted()
private fun initCommonUI() {
2022-01-08 16:56:41 -03:00
scanModel.setupScan()
2021-03-04 11:20:51 +08:00
// init our region spinner
2021-03-03 13:51:33 +08:00
val spinner = binding.regionSpinner
val regionAdapter =
ArrayAdapter(requireContext(), android.R.layout.simple_spinner_item, regions)
2021-03-03 13:51:33 +08:00
regionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinner.adapter = regionAdapter
2021-03-04 09:08:29 +08:00
2022-01-31 21:55:24 -03:00
model.bluetoothEnabled.observe(viewLifecycleOwner) {
2022-01-26 16:27:16 -03:00
if (it) binding.changeRadioButton.show()
else binding.changeRadioButton.hide()
2022-01-31 21:55:24 -03:00
}
2022-01-25 01:34:46 -03:00
2022-01-31 21:55:24 -03:00
model.ownerName.observe(viewLifecycleOwner) { name ->
binding.usernameEditText.setText(name)
2022-01-31 21:55:24 -03:00
}
2020-04-09 11:03:17 -07:00
2020-05-13 17:00:23 -07:00
// Only let user edit their name or set software update while connected to a radio
2022-01-31 21:55:24 -03:00
model.isConnected.observe(viewLifecycleOwner) {
2022-01-26 16:27:16 -03:00
updateNodeInfo()
updateDevicesButtons(scanModel.devices.value)
2022-01-31 21:55:24 -03:00
}
2022-01-26 16:27:16 -03:00
2022-01-31 21:55:24 -03:00
model.radioConfig.observe(viewLifecycleOwner) {
2022-01-26 16:27:16 -03:00
binding.provideLocationCheckbox.isEnabled =
isGooglePlayAvailable(requireContext()) && model.locationShare ?: true
if (model.locationShare == false) {
model.provideLocation.value = false
binding.provideLocationCheckbox.isChecked = false
}
2022-01-31 21:55:24 -03:00
}
2020-05-13 17:00:23 -07:00
2020-05-15 10:18:15 -07:00
// Also watch myNodeInfo because it might change later
2022-01-31 21:55:24 -03:00
model.myNodeInfo.observe(viewLifecycleOwner) {
updateNodeInfo()
2022-01-31 21:55:24 -03:00
}
2022-01-31 21:55:24 -03:00
scanModel.errorText.observe(viewLifecycleOwner) { errMsg ->
2022-01-08 16:30:06 -03:00
if (errMsg != null) {
binding.scanStatusText.text = errMsg
}
2022-01-31 21:55:24 -03:00
}
2022-01-08 16:30:06 -03:00
2022-01-31 21:55:24 -03:00
scanModel.devices.observe(viewLifecycleOwner) { devices ->
2022-01-25 18:14:10 -03:00
updateDevicesButtons(devices)
2022-01-31 21:55:24 -03:00
}
2022-01-08 16:30:06 -03:00
binding.updateFirmwareButton.setOnClickListener {
MaterialAlertDialogBuilder(requireContext())
.setMessage("${getString(R.string.update_firmware)}?")
.setNeutralButton(R.string.cancel) { _, _ ->
}
.setPositiveButton(getString(R.string.okay)) { _, _ ->
doFirmwareUpdate()
}
.show()
2020-05-13 17:00:23 -07:00
}
binding.usernameEditText.on(EditorInfo.IME_ACTION_DONE) {
2020-04-09 11:03:17 -07:00
debug("did IME action")
val n = binding.usernameEditText.text.toString().trim()
2020-04-09 11:03:17 -07:00
if (n.isNotEmpty())
model.setOwner(n)
2020-04-09 11:03:17 -07:00
requireActivity().hideKeyboard()
}
2021-06-23 11:40:15 -07:00
binding.provideLocationCheckbox.setOnCheckedChangeListener { view, isChecked ->
2022-01-25 01:20:31 -03:00
if (view.isPressed && isChecked) { // We want to ignore changes caused by code (as opposed to the user)
// Don't check the box until the system setting changes
view.isChecked = myActivity.hasLocationPermission() && myActivity.hasBackgroundPermission()
if (!myActivity.hasLocationPermission()) // Make sure we have location permission (prerequisite)
myActivity.requestLocationPermission()
else if (!myActivity.hasBackgroundPermission())
MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.background_required)
.setMessage(R.string.why_background_required)
.setNeutralButton(R.string.cancel) { _, _ ->
debug("User denied background permission")
}
.setPositiveButton(getString(R.string.accept)) { _, _ ->
myActivity.requestBackgroundPermission()
}
.show()
if (view.isChecked) {
debug("User changed location tracking to $isChecked")
model.provideLocation.value = isChecked
checkLocationEnabled(getString(R.string.location_disabled))
model.meshService?.setupProvideLocation()
2022-01-03 21:59:30 -03:00
}
2022-01-09 00:25:40 -03:00
} else {
2022-01-25 01:20:31 -03:00
model.provideLocation.value = isChecked
2022-01-04 10:33:09 -03:00
model.meshService?.stopProvideLocation()
2021-06-23 11:40:15 -07:00
}
}
2021-02-05 21:29:28 -08:00
2020-04-15 14:10:40 -07:00
val app = (requireContext().applicationContext as GeeksvilleApplication)
2020-04-11 13:20:30 -07:00
// Set analytics checkbox
binding.analyticsOkayCheckbox.isChecked = app.isAnalyticsAllowed
2020-04-15 14:10:40 -07:00
binding.analyticsOkayCheckbox.setOnCheckedChangeListener { _, isChecked ->
2020-04-11 13:20:30 -07:00
debug("User changed analytics to $isChecked")
2020-04-15 14:10:40 -07:00
app.isAnalyticsAllowed = isChecked
binding.reportBugButton.isEnabled = app.isAnalyticsAllowed
2020-04-15 14:10:40 -07:00
}
// report bug button only enabled if analytics is allowed
binding.reportBugButton.isEnabled = app.isAnalyticsAllowed
binding.reportBugButton.setOnClickListener {
2020-04-15 14:10:40 -07:00
MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.report_a_bug)
.setMessage(getString(R.string.report_bug_text))
.setNeutralButton(R.string.cancel) { _, _ ->
debug("Decided not to report a bug")
}
.setPositiveButton(getString(R.string.report)) { _, _ ->
reportError("Clicked Report A Bug")
}
.show()
2020-04-09 11:03:17 -07:00
}
}
private fun addDeviceButton(device: BTScanModel.DeviceListEntry, enabled: Boolean) {
val b = RadioButton(requireActivity())
b.text = device.name
b.id = View.generateViewId()
b.isEnabled = enabled
b.isChecked =
device.address == scanModel.selectedNotNull && device.bonded // Only show checkbox if device is still paired
binding.deviceRadioGroup.addView(b)
b.setOnClickListener {
if (!device.bonded) // If user just clicked on us, try to bond
binding.scanStatusText.setText(R.string.starting_pairing)
b.isChecked =
2021-06-23 11:40:15 -07:00
scanModel.onSelected(myActivity, device)
2022-01-25 01:34:46 -03:00
if (!b.isSelected) {
2022-01-25 18:14:10 -03:00
binding.scanStatusText.text = getString(R.string.please_pair)
2022-01-25 01:34:46 -03:00
}
}
}
@SuppressLint("MissingPermission")
2021-02-14 15:52:16 +08:00
private fun updateDevicesButtons(devices: MutableMap<String, BTScanModel.DeviceListEntry>?) {
// Remove the old radio buttons and repopulate
binding.deviceRadioGroup.removeAllViews()
2021-02-14 15:52:16 +08:00
if (devices == null) return
val adapter = scanModel.bluetoothAdapter
var hasShownOurDevice = false
devices.values.forEach { device ->
if (device.address == scanModel.selectedNotNull)
hasShownOurDevice = true
addDeviceButton(device, true)
}
2021-02-02 19:01:11 -08:00
// The selected device is not in the scan; it is either offline, or it doesn't advertise
// itself (most BLE devices don't advertise when connected).
// Show it in the list, greyed out based on connection status.
if (!hasShownOurDevice) {
// Note: we pull this into a tempvar, because otherwise some other thread can change selectedAddress after our null check
// and before use
val bleAddr = scanModel.selectedBluetooth
if (bleAddr != null && adapter != null && myActivity.hasConnectPermission()) {
val bDevice =
adapter.getRemoteDevice(bleAddr)
if (bDevice.name != null) { // ignore nodes that node have a name, that means we've lost them since they appeared
val curDevice = BTScanModel.DeviceListEntry(
bDevice.name,
scanModel.selectedAddress!!,
bDevice.bondState == BOND_BONDED
)
2021-02-14 15:52:16 +08:00
addDeviceButton(
curDevice,
model.isConnected.value == MeshService.ConnectionState.CONNECTED
)
}
} else if (scanModel.selectedUSB != null) {
// Must be a USB device, show a placeholder disabled entry
val curDevice = BTScanModel.DeviceListEntry(
scanModel.selectedUSB!!,
scanModel.selectedAddress!!,
false
)
addDeviceButton(curDevice, false)
}
}
// get rid of the warning text once at least one device is paired.
// If we are running on an emulator, always leave this message showing so we can test the worst case layout
2022-01-08 16:30:06 -03:00
val curRadio = RadioInterfaceService.getBondedDeviceAddress(requireContext())
2022-01-08 17:50:48 -03:00
if (curRadio != null && !MockInterface.addressValid(requireContext(), "")) {
binding.warningNotPaired.visibility = View.GONE
2022-01-09 00:25:40 -03:00
// binding.scanStatusText.text = getString(R.string.current_pair).format(curRadio)
2022-01-25 18:14:10 -03:00
} else {
binding.warningNotPaired.visibility = View.VISIBLE
binding.scanStatusText.text = getString(R.string.not_paired_yet)
2022-01-08 16:30:06 -03:00
}
}
private fun initClassicScan() {
binding.changeRadioButton.setOnClickListener {
2022-01-25 18:14:10 -03:00
debug("User clicked changeRadioButton")
if (!myActivity.hasScanPermission()) {
myActivity.requestScanPermission()
} else {
checkLocationEnabled()
scanLeDevice()
}
}
}
// per https://developer.android.com/guide/topics/connectivity/bluetooth/find-ble-devices
private fun scanLeDevice() {
var scanning = false
val SCAN_PERIOD: Long = 5000 // Stops scanning after 5 seconds
if (!scanning) { // Stops scanning after a pre-defined scan period.
Handler(Looper.getMainLooper()).postDelayed({
scanning = false
binding.scanProgressBar.visibility = View.GONE
scanModel.stopScan()
}, SCAN_PERIOD)
scanning = true
binding.scanProgressBar.visibility = View.VISIBLE
scanModel.startScan()
} else {
scanning = false
binding.scanProgressBar.visibility = View.GONE
scanModel.stopScan()
}
2020-04-08 21:17:23 -07:00
}
private fun initModernScan() {
2022-01-07 18:51:20 -03:00
binding.changeRadioButton.setOnClickListener {
2022-01-25 18:14:10 -03:00
debug("User clicked changeRadioButton")
if (!myActivity.hasScanPermission()) {
myActivity.requestScanPermission()
} else {
// checkLocationEnabled() // ? some phones still need location turned on
myActivity.startCompanionScan()
}
2022-01-07 18:51:20 -03:00
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initCommonUI()
if (hasCompanionDeviceApi)
initModernScan()
else
initClassicScan()
}
2022-01-03 21:59:30 -03:00
// If the user has not turned on location access throw up a toast warning
2022-01-24 14:56:17 -03:00
private fun checkLocationEnabled(
warningReason: String = getString(R.string.location_disabled_warning)
) {
2021-12-15 12:02:01 -03:00
2022-01-09 00:25:40 -03:00
val hasGps: Boolean =
2021-12-15 12:02:01 -03:00
myActivity.packageManager.hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS)
2022-01-03 21:59:30 -03:00
// FIXME If they don't have google play for now we don't check for location enabled
2022-01-09 00:25:40 -03:00
if (hasGps && isGooglePlayAvailable(requireContext())) {
2020-07-15 15:58:53 -07:00
// We do this painful process because LocationManager.isEnabled is only SDK28 or latet
val builder = LocationSettingsRequest.Builder()
builder.setNeedBle(true)
val request = LocationRequest.create().apply {
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
}
builder.addLocationRequest(request) // Make sure we are granted high accuracy permission
2020-07-15 15:58:53 -07:00
val locationSettingsResponse = LocationServices.getSettingsClient(requireActivity())
.checkLocationSettings(builder.build())
2022-01-24 14:56:17 -03:00
fun weNeedAccess(warningReason: String) {
2022-01-09 00:25:40 -03:00
warn("Telling user we need need location access")
2022-02-07 21:50:31 -03:00
Snackbar.make(requireView(), warningReason, Snackbar.LENGTH_INDEFINITE)
2022-01-24 14:56:17 -03:00
.apply { view.findViewById<TextView>(R.id.snackbar_text).isSingleLine = false }
.setAction(R.string.okay) {
// dismiss
}
.show()
}
2020-07-15 15:58:53 -07:00
locationSettingsResponse.addOnSuccessListener {
2022-01-04 11:15:59 -03:00
if (!it.locationSettingsStates?.isBleUsable!! || !it.locationSettingsStates?.isLocationUsable!!)
2022-01-24 14:56:17 -03:00
weNeedAccess(warningReason)
else
debug("We have location access")
2020-07-15 15:58:53 -07:00
}
2022-01-26 16:27:16 -03:00
locationSettingsResponse.addOnFailureListener {
2020-07-15 15:58:53 -07:00
errormsg("Failed to get location access")
// We always show the toast regardless of what type of exception we receive. Because even non
// resolvable api exceptions mean user still needs to fix something.
///if (exception is ResolvableApiException) {
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
// exception.startResolutionForResult(this@MainActivity, REQUEST_CHECK_SETTINGS)
2020-07-15 15:58:53 -07:00
// For now just punt and show a dialog
// The context might be gone (if activity is going away) by the time this handler is called
2022-01-24 14:56:17 -03:00
weNeedAccess(warningReason)
2020-07-15 15:58:53 -07:00
//} else
// Exceptions.report(exception)
}
}
}
private val updateProgressFilter = IntentFilter(ACTION_UPDATE_PROGRESS)
private val updateProgressReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
refreshUpdateButton(true)
}
}
override fun onPause() {
super.onPause()
requireActivity().unregisterReceiver(updateProgressReceiver)
}
2020-04-09 11:03:17 -07:00
override fun onResume() {
super.onResume()
2022-01-08 16:56:41 -03:00
scanModel.setupScan()
2021-06-23 11:40:15 -07:00
// system permissions might have changed while we were away
2022-01-04 10:33:09 -03:00
binding.provideLocationCheckbox.isChecked = myActivity.hasLocationPermission() && myActivity.hasBackgroundPermission() && (model.provideLocation.value ?: false) && isGooglePlayAvailable(requireContext())
2021-06-23 11:40:15 -07:00
myActivity.registerReceiver(updateProgressReceiver, updateProgressFilter)
// Keep reminding user BLE is still off
2021-06-23 11:40:15 -07:00
val hasUSB = SerialInterface.findDrivers(myActivity).isNotEmpty()
if (!hasUSB) {
// Warn user if BLE is disabled
if (scanModel.bluetoothAdapter?.isEnabled != true) {
2022-01-31 21:55:24 -03:00
Snackbar.make(
2022-02-07 21:50:31 -03:00
requireView(),
2022-01-31 21:55:24 -03:00
R.string.error_bluetooth,
Snackbar.LENGTH_INDEFINITE
)
2022-01-24 14:56:17 -03:00
.setAction(R.string.okay) {
// dismiss
}
.show()
} else {
2022-01-24 14:56:17 -03:00
if (binding.provideLocationCheckbox.isChecked)
checkLocationEnabled(getString(R.string.location_disabled))
}
}
2020-02-13 09:25:39 -08:00
}
}