feat: add BLE OTA update button to SettingsActivity

This commit is contained in:
eddy 2026-07-30 17:14:28 +02:00
commit 1988ae3898
2 changed files with 78 additions and 0 deletions

View file

@ -74,6 +74,8 @@ class SettingsActivity : AppCompatActivity() {
private var progressDialog: AlertDialog? = null
private var dialog: AlertDialog? = null
private var flasherJob: Job? = null
private var otaManager: OtaManager? = null
private var otaJob: Job? = null
private var lastTheme: String? = null
private val usbAttachedReceiver = object : BroadcastReceiver() {
@ -140,6 +142,11 @@ class SettingsActivity : AppCompatActivity() {
startFlashing()
}
val otaButton = findViewById<Button>(R.id.otaUpdateButton)
otaButton.setOnClickListener {
startOtaUpdate()
}
registerReceiver(usbAttachedReceiver, IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED), Context.RECEIVER_EXPORTED)
registerReceiver(usbPermissionReceiver, IntentFilter(ACTION_USB_PERMISSION), Context.RECEIVER_NOT_EXPORTED)
registerReceiver(usbDetachedReceiver, IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED), Context.RECEIVER_EXPORTED)
@ -202,11 +209,65 @@ class SettingsActivity : AppCompatActivity() {
}
}
private fun startOtaUpdate() {
val manager = BleRelayManager.instance
if (manager == null) {
Toast.makeText(this, "Niet verbonden met SylDa_Server", Toast.LENGTH_SHORT).show()
return
}
otaManager = OtaManager(this, manager)
if (otaManager?.loadFirmware() != true) {
Toast.makeText(this, getString(R.string.ota_load_failed), Toast.LENGTH_SHORT).show()
return
}
showOtaProgressDialog()
otaJob = lifecycleScope.launch {
val result = otaManager!!.performOta(object : OtaManager.OtaCallback {
override fun onProgress(percent: Int, message: String) {
lifecycleScope.launch(Dispatchers.Main) {
val d = progressDialog ?: return@launch
val pb = d.findViewById<ProgressBar>(R.id.progressBar)
val msg = d.findViewById<TextView>(R.id.message)
pb?.progress = percent
msg?.text = message
}
}
override fun onComplete() {
lifecycleScope.launch(Dispatchers.Main) {
progressDialog?.dismiss()
progressDialog = null
Toast.makeText(this@SettingsActivity, getString(R.string.ota_complete), Toast.LENGTH_LONG).show()
}
}
override fun onError(message: String) {
lifecycleScope.launch(Dispatchers.Main) {
progressDialog?.dismiss()
progressDialog = null
Toast.makeText(this@SettingsActivity, getString(R.string.ota_failed, message), Toast.LENGTH_LONG).show()
}
}
})
}
}
private fun showOtaProgressDialog() {
showProgressDialog(
getString(R.string.title_ota_update),
getString(R.string.msg_ota_starting),
STYLE_HORIZONTAL
)
}
override fun onDestroy() {
super.onDestroy()
currentAction = UsbAction.NONE
flasherJob?.cancel()
flasherJob = null
otaJob?.cancel()
otaJob = null
try { unregisterReceiver(usbAttachedReceiver) } catch (_: Exception) {}
try { unregisterReceiver(usbPermissionReceiver) } catch (_: Exception) {}
try { unregisterReceiver(usbDetachedReceiver) } catch (_: Exception) {}

View file

@ -18,6 +18,23 @@
android:layout_height="wrap_content"
android:text="@string/button_firmware_flash" />
<Button
android:id="@+id/otaUpdateButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button_ota_update"
android:layout_marginTop="8dp" />
<TextView
android:id="@+id/otaStatusText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:gravity="center"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:visibility="gone" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"