From c1388d6badd158561ec92f5271999ea95eece59c Mon Sep 17 00:00:00 2001 From: geeksville Date: Wed, 13 May 2020 17:00:23 -0700 Subject: [PATCH] firmware update button is kinda in --- .../java/com/geeksville/mesh/MainActivity.kt | 2 + .../java/com/geeksville/mesh/MyNodeInfo.kt | 3 ++ .../java/com/geeksville/mesh/model/UIState.kt | 4 ++ .../geeksville/mesh/service/MeshService.kt | 41 ++++++++++--------- .../geeksville/mesh/ui/SettingsFragment.kt | 26 +++++++++++- app/src/main/res/layout/settings_fragment.xml | 13 +++++- app/src/main/res/values/strings.xml | 4 ++ 7 files changed, 71 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/MainActivity.kt b/app/src/main/java/com/geeksville/mesh/MainActivity.kt index 0b8a355d2..f34113c20 100644 --- a/app/src/main/java/com/geeksville/mesh/MainActivity.kt +++ b/app/src/main/java/com/geeksville/mesh/MainActivity.kt @@ -544,6 +544,8 @@ class MainActivity : AppCompatActivity(), Logging, model.radioConfig.value = MeshProtos.RadioConfig.parseFrom(service.radioConfig) + model.myNodeInfo.value = service.myNodeInfo + updateNodesFromDevice() // we have a connection to our device now, do the channel change diff --git a/app/src/main/java/com/geeksville/mesh/MyNodeInfo.kt b/app/src/main/java/com/geeksville/mesh/MyNodeInfo.kt index 8c9c5596d..91dd6f9c2 100644 --- a/app/src/main/java/com/geeksville/mesh/MyNodeInfo.kt +++ b/app/src/main/java/com/geeksville/mesh/MyNodeInfo.kt @@ -15,6 +15,9 @@ data class MyNodeInfo( val couldUpdate: Boolean, // this application contains a software load we _could_ install if you want val shouldUpdate: Boolean // this device has old firmware ) : Parcelable { + /** A human readable description of the software/hardware version */ + val firmwareString: String get() = "$model $region/$firmwareVersion" + constructor(parcel: Parcel) : this( parcel.readInt(), parcel.readByte() != 0.toByte(), diff --git a/app/src/main/java/com/geeksville/mesh/model/UIState.kt b/app/src/main/java/com/geeksville/mesh/model/UIState.kt index e9ea3b90d..d18e10ed6 100644 --- a/app/src/main/java/com/geeksville/mesh/model/UIState.kt +++ b/app/src/main/java/com/geeksville/mesh/model/UIState.kt @@ -12,6 +12,7 @@ import com.geeksville.android.BuildUtils.isEmulator import com.geeksville.android.Logging import com.geeksville.mesh.IMeshService import com.geeksville.mesh.MeshProtos +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 @@ -64,6 +65,9 @@ class UIViewModel(app: Application) : AndroidViewModel(app), Logging { val radioConfig = object : MutableLiveData(null) { } + /// hardware info about our local device + val myNodeInfo = object : MutableLiveData(null) {} + override fun onCleared() { super.onCleared() debug("ViewModel cleared") diff --git a/app/src/main/java/com/geeksville/mesh/service/MeshService.kt b/app/src/main/java/com/geeksville/mesh/service/MeshService.kt index b43820cda..35e10fff2 100644 --- a/app/src/main/java/com/geeksville/mesh/service/MeshService.kt +++ b/app/src/main/java/com/geeksville/mesh/service/MeshService.kt @@ -853,6 +853,7 @@ class MeshService : Service(), Logging { ) handleMyInfo(myInfo) + myNodeInfo = newMyNodeInfo // Apply the changes from handleMyInfo right now radioConfig = MeshProtos.RadioConfig.parseFrom(connectedRadio.readRadioConfig()) @@ -1132,6 +1133,8 @@ class MeshService : Service(), Logging { private fun handleMyInfo(myInfo: MeshProtos.MyNodeInfo) { + setFirmwareUpdateFilename(myInfo) + val mi = with(myInfo) { MyNodeInfo( myNodeNum, @@ -1139,8 +1142,8 @@ class MeshService : Service(), Logging { region, hwModel, firmwareVersion, - false, - false + firmwareUpdateFilename != null, + SoftwareUpdateService.shouldUpdate(this@MeshService, firmwareVersion) ) } @@ -1284,27 +1287,27 @@ class MeshService : Service(), Logging { } + var firmwareUpdateFilename: String? = null + /*** * Return the filename we will install on the device */ - val firmwareUpdateFilename: String? - get() = - try { - myNodeInfo?.let { - if (it.region != null && it.firmwareVersion != null && it.model != null) - SoftwareUpdateService.getUpdateFilename( - this, - it.region, - it.firmwareVersion, - it.model - ) - else - null - } - } catch (ex: Exception) { - errormsg("Unable to update", ex) + fun setFirmwareUpdateFilename(info: MeshProtos.MyNodeInfo) { + firmwareUpdateFilename = try { + if (info.region != null && info.firmwareVersion != null && info.hwModel != null) + SoftwareUpdateService.getUpdateFilename( + this, + info.region, + info.firmwareVersion, + info.hwModel + ) + else null - } + } catch (ex: Exception) { + errormsg("Unable to update", ex) + null + } + } private fun doFirmwareUpdate() { // Run in the IO thread diff --git a/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt b/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt index c5841ac28..cd4052f49 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt @@ -311,11 +311,35 @@ class SettingsFragment : ScreenFragment("Settings"), Logging { usernameEditText.setText(name) }) - // Only let user edit their name while connected to a radio + // Only let user edit their name or set software update while connected to a radio model.isConnected.observe(viewLifecycleOwner, Observer { connected -> usernameView.isEnabled = connected == MeshService.ConnectionState.CONNECTED + + // If actively connected possibly let the user update firmware + val info = model.myNodeInfo.value + if (connected == MeshService.ConnectionState.CONNECTED && info != null && info.couldUpdate) { + updateFirmwareButton.visibility = View.VISIBLE + } else { + updateFirmwareButton.visibility = View.GONE + } + + when (connected) { + MeshService.ConnectionState.CONNECTED -> { + val fwStr = info?.firmwareString ?: "" + scanStatusText.text = getString(R.string.connected_to).format(fwStr) + } + MeshService.ConnectionState.DISCONNECTED -> + scanStatusText.text = getString(R.string.not_connected) + MeshService.ConnectionState.DEVICE_SLEEP -> + scanStatusText.text = getString(R.string.connected_sleeping) + } + }) + updateFirmwareButton.setOnClickListener { + debug("User started firmware update") + } + usernameEditText.on(EditorInfo.IME_ACTION_DONE) { debug("did IME action") val n = usernameEditText.text.toString().trim() diff --git a/app/src/main/res/layout/settings_fragment.xml b/app/src/main/res/layout/settings_fragment.xml index 82927d756..32dd30270 100644 --- a/app/src/main/res/layout/settings_fragment.xml +++ b/app/src/main/res/layout/settings_fragment.xml @@ -45,9 +45,9 @@ android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="16dp" - android:layout_marginEnd="16dp" + android:layout_marginEnd="8dp" android:text="@string/looking_for_meshtastic_devices" - app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintEnd_toStartOf="@+id/updateFirmwareButton" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/usernameView" /> @@ -117,5 +117,14 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/scanStatusText" /> +