Replace deprecated ProgressDialog with AlertDialog + custom layouts

Use AlertDialog with dialog_waiting.xml (spinner + message) and
dialog_progress.xml (horizontal ProgressBar + message) instead of
the deprecated ProgressDialog. Progress updates now use findViewById
on the dialog view.

Reviewed in codereview.md (recommendation 2.4).
This commit is contained in:
eddy 2026-07-26 16:57:07 +02:00
commit aa13359125
3 changed files with 72 additions and 24 deletions

View file

@ -2,7 +2,6 @@ package com.eddy.atomrelay
import android.app.AlertDialog import android.app.AlertDialog
import android.app.PendingIntent import android.app.PendingIntent
import android.app.ProgressDialog
import android.content.BroadcastReceiver import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
@ -18,6 +17,7 @@ import android.os.Handler
import android.os.Looper import android.os.Looper
import android.util.Log import android.util.Log
import android.widget.Button import android.widget.Button
import android.widget.ProgressBar
import android.widget.TextView import android.widget.TextView
import android.widget.Toast import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
@ -34,12 +34,14 @@ class SettingsActivity : AppCompatActivity() {
private const val KEY_TIMEOUT_MS = 5000L private const val KEY_TIMEOUT_MS = 5000L
private const val SET_KEY_TIMEOUT_MS = 5000L private const val SET_KEY_TIMEOUT_MS = 5000L
private const val ACTION_USB_PERMISSION = "com.eddy.atomrelay.USB_PERMISSION" private const val ACTION_USB_PERMISSION = "com.eddy.atomrelay.USB_PERMISSION"
private const val STYLE_SPINNER = 0
private const val STYLE_HORIZONTAL = 1
} }
private enum class UsbAction { NONE, EXCHANGE_KEY, FLASH } private enum class UsbAction { NONE, EXCHANGE_KEY, FLASH }
private var currentAction = UsbAction.NONE private var currentAction = UsbAction.NONE
private var progressDialog: ProgressDialog? = null private var progressDialog: AlertDialog? = null
private var dialog: AlertDialog? = null private var dialog: AlertDialog? = null
private val handler = Handler(Looper.getMainLooper()) private val handler = Handler(Looper.getMainLooper())
@ -148,7 +150,7 @@ class SettingsActivity : AppCompatActivity() {
} }
private fun doKeyExchange(usbManager: UsbManager, device: UsbDevice) { private fun doKeyExchange(usbManager: UsbManager, device: UsbDevice) {
showProgressDialog("Sleutel Uitwisselen", "Sleutel uitlezen...", ProgressDialog.STYLE_SPINNER) showProgressDialog("Sleutel Uitwisselen", "Sleutel uitlezen...", STYLE_SPINNER)
Thread { Thread {
val result = readKeyFromEsp(usbManager, device) val result = readKeyFromEsp(usbManager, device)
@ -199,14 +201,15 @@ class SettingsActivity : AppCompatActivity() {
} }
private fun doFlashing(usbManager: UsbManager, device: UsbDevice) { private fun doFlashing(usbManager: UsbManager, device: UsbDevice) {
showProgressDialog("Firmware Flashen", "Initialiseren...", ProgressDialog.STYLE_HORIZONTAL) showProgressDialog("Firmware Flashen", "Initialiseren...", STYLE_HORIZONTAL)
val flasher = EspFlasher(this) val flasher = EspFlasher(this)
flasher.flashDevice(usbManager, device, object : EspFlasher.FlashCallback { flasher.flashDevice(usbManager, device, object : EspFlasher.FlashCallback {
override fun onProgress(percent: Int, message: String) { override fun onProgress(percent: Int, message: String) {
runOnUiThread { runOnUiThread {
progressDialog?.progress = percent val d = progressDialog ?: return@runOnUiThread
progressDialog?.setMessage(message) d.findViewById<ProgressBar>(R.id.progressBar)?.progress = percent
d.findViewById<TextView>(R.id.message)?.text = message
} }
} }
@ -253,31 +256,34 @@ class SettingsActivity : AppCompatActivity() {
private fun showWaitingDialog(title: String, message: String) { private fun showWaitingDialog(title: String, message: String) {
dialog?.dismiss() dialog?.dismiss()
dialog = ProgressDialog(this).apply { val view = layoutInflater.inflate(R.layout.dialog_waiting, null)
setTitle(title) view.findViewById<TextView>(R.id.message).text = message
setMessage(message) dialog = AlertDialog.Builder(this)
setProgressStyle(ProgressDialog.STYLE_SPINNER) .setTitle(title)
setCancelable(true) .setView(view)
setButton(ProgressDialog.BUTTON_NEGATIVE, "Annuleren") { _, _ -> .setCancelable(true)
.setNegativeButton("Annuleren") { _, _ ->
cleanupAction() cleanupAction()
} }
show() .setOnCancelListener { cleanupAction() }
} .show()
} }
private fun showProgressDialog(title: String, message: String, style: Int) { private fun showProgressDialog(title: String, message: String, style: Int) {
progressDialog?.dismiss() progressDialog?.dismiss()
progressDialog = ProgressDialog(this).apply { val view = layoutInflater.inflate(R.layout.dialog_progress, null)
setTitle(title) view.findViewById<TextView>(R.id.message).text = message
setMessage(message) val progressBar = view.findViewById<ProgressBar>(R.id.progressBar)
setProgressStyle(style) if (style == STYLE_HORIZONTAL) {
if (style == ProgressDialog.STYLE_HORIZONTAL) { progressBar.isIndeterminate = false
max = 100 } else {
progress = 0 progressBar.isIndeterminate = true
}
setCancelable(false)
show()
} }
progressDialog = AlertDialog.Builder(this)
.setTitle(title)
.setView(view)
.setCancelable(false)
.show()
} }
private fun cleanupAction() { private fun cleanupAction() {

View file

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_marginBottom="12dp" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="0" />
</LinearLayout>

View file

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="20dp"
android:gravity="center_vertical">
<ProgressBar
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp" />
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp" />
</LinearLayout>