From 633200ac44b02907d368f4bfdc4a69b0c1c2409a Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Fri, 8 Jan 2021 14:51:19 +0800 Subject: [PATCH] if we lose device connection at a bad time, don't leave UI confused (autobug) --- .../java/com/geeksville/mesh/MainActivity.kt | 57 ++++++++++++------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/MainActivity.kt b/app/src/main/java/com/geeksville/mesh/MainActivity.kt index 81ad5342d..25c34f612 100644 --- a/app/src/main/java/com/geeksville/mesh/MainActivity.kt +++ b/app/src/main/java/com/geeksville/mesh/MainActivity.kt @@ -606,34 +606,46 @@ class MainActivity : AppCompatActivity(), Logging, /// Called when we gain/lose a connection to our mesh radio private fun onMeshConnectionChanged(connected: MeshService.ConnectionState) { - model.isConnected.value = connected debug("connchange ${model.isConnected.value}") + if (connected == MeshService.ConnectionState.CONNECTED) { - model.meshService?.let { service -> + + val oldConnection = model.isConnected.value + model.isConnected.value = connected + debug("Getting latest radioconfig from service") - model.radioConfig.value = - MeshProtos.RadioConfig.parseFrom(service.radioConfig) + try { + model.radioConfig.value = + MeshProtos.RadioConfig.parseFrom(service.radioConfig) - val info = service.myNodeInfo - model.myNodeInfo.value = info + val info = service.myNodeInfo + model.myNodeInfo.value = info - val isOld = info.minAppVersion > BuildConfig.VERSION_CODE - if (isOld) - MaterialAlertDialogBuilder(this) - .setTitle(getString(R.string.app_too_old)) - .setMessage(getString(R.string.must_update)) - .setPositiveButton("Okay") { _, _ -> - info("User acknowledged app is old") - } - .show() + val isOld = info.minAppVersion > BuildConfig.VERSION_CODE + if (isOld) + MaterialAlertDialogBuilder(this) + .setTitle(getString(R.string.app_too_old)) + .setMessage(getString(R.string.must_update)) + .setPositiveButton("Okay") { _, _ -> + info("User acknowledged app is old") + } + .show() - updateNodesFromDevice() + updateNodesFromDevice() - // we have a connection to our device now, do the channel change - perhapsChangeChannel() + // we have a connection to our device now, do the channel change + perhapsChangeChannel() + } catch (ex: RemoteException) { + warn("Abandoning connect $ex, because we probably just lost device connection") + model.isConnected.value = oldConnection + } } } + else { + // For other connection states, just slam them in + model.isConnected.value = connected + } } private fun perhapsChangeChannel() { @@ -803,7 +815,8 @@ class MainActivity : AppCompatActivity(), Logging, registerMeshReceiver() // Init our messages table with the service's record of past text messages (ignore all other message types) - val msgs = service.oldMessages.filter { p -> p.dataType == Portnums.PortNum.TEXT_MESSAGE_APP_VALUE } + val msgs = + service.oldMessages.filter { p -> p.dataType == Portnums.PortNum.TEXT_MESSAGE_APP_VALUE } debug("Service provided ${msgs.size} messages") model.messagesState.setMessages(msgs) val connectionState = @@ -853,7 +866,11 @@ class MainActivity : AppCompatActivity(), Logging, } // ALSO bind so we can use the api - mesh.connect(this, MeshService.createIntent(), Context.BIND_AUTO_CREATE + Context.BIND_ABOVE_CLIENT) + mesh.connect( + this, + MeshService.createIntent(), + Context.BIND_AUTO_CREATE + Context.BIND_ABOVE_CLIENT + ) } private fun unbindMeshService() {