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:
parent
d2faaf8fbc
commit
aa13359125
3 changed files with 72 additions and 24 deletions
|
|
@ -2,7 +2,6 @@ package com.eddy.atomrelay
|
|||
|
||||
import android.app.AlertDialog
|
||||
import android.app.PendingIntent
|
||||
import android.app.ProgressDialog
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
|
|
@ -18,6 +17,7 @@ import android.os.Handler
|
|||
import android.os.Looper
|
||||
import android.util.Log
|
||||
import android.widget.Button
|
||||
import android.widget.ProgressBar
|
||||
import android.widget.TextView
|
||||
import android.widget.Toast
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
|
|
@ -34,12 +34,14 @@ class SettingsActivity : AppCompatActivity() {
|
|||
private const val 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 STYLE_SPINNER = 0
|
||||
private const val STYLE_HORIZONTAL = 1
|
||||
}
|
||||
|
||||
private enum class UsbAction { NONE, EXCHANGE_KEY, FLASH }
|
||||
|
||||
private var currentAction = UsbAction.NONE
|
||||
private var progressDialog: ProgressDialog? = null
|
||||
private var progressDialog: AlertDialog? = null
|
||||
private var dialog: AlertDialog? = null
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
|
||||
|
|
@ -148,7 +150,7 @@ class SettingsActivity : AppCompatActivity() {
|
|||
}
|
||||
|
||||
private fun doKeyExchange(usbManager: UsbManager, device: UsbDevice) {
|
||||
showProgressDialog("Sleutel Uitwisselen", "Sleutel uitlezen...", ProgressDialog.STYLE_SPINNER)
|
||||
showProgressDialog("Sleutel Uitwisselen", "Sleutel uitlezen...", STYLE_SPINNER)
|
||||
|
||||
Thread {
|
||||
val result = readKeyFromEsp(usbManager, device)
|
||||
|
|
@ -199,14 +201,15 @@ class SettingsActivity : AppCompatActivity() {
|
|||
}
|
||||
|
||||
private fun doFlashing(usbManager: UsbManager, device: UsbDevice) {
|
||||
showProgressDialog("Firmware Flashen", "Initialiseren...", ProgressDialog.STYLE_HORIZONTAL)
|
||||
showProgressDialog("Firmware Flashen", "Initialiseren...", STYLE_HORIZONTAL)
|
||||
|
||||
val flasher = EspFlasher(this)
|
||||
flasher.flashDevice(usbManager, device, object : EspFlasher.FlashCallback {
|
||||
override fun onProgress(percent: Int, message: String) {
|
||||
runOnUiThread {
|
||||
progressDialog?.progress = percent
|
||||
progressDialog?.setMessage(message)
|
||||
val d = progressDialog ?: return@runOnUiThread
|
||||
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) {
|
||||
dialog?.dismiss()
|
||||
dialog = ProgressDialog(this).apply {
|
||||
setTitle(title)
|
||||
setMessage(message)
|
||||
setProgressStyle(ProgressDialog.STYLE_SPINNER)
|
||||
setCancelable(true)
|
||||
setButton(ProgressDialog.BUTTON_NEGATIVE, "Annuleren") { _, _ ->
|
||||
val view = layoutInflater.inflate(R.layout.dialog_waiting, null)
|
||||
view.findViewById<TextView>(R.id.message).text = message
|
||||
dialog = AlertDialog.Builder(this)
|
||||
.setTitle(title)
|
||||
.setView(view)
|
||||
.setCancelable(true)
|
||||
.setNegativeButton("Annuleren") { _, _ ->
|
||||
cleanupAction()
|
||||
}
|
||||
show()
|
||||
}
|
||||
.setOnCancelListener { cleanupAction() }
|
||||
.show()
|
||||
}
|
||||
|
||||
private fun showProgressDialog(title: String, message: String, style: Int) {
|
||||
progressDialog?.dismiss()
|
||||
progressDialog = ProgressDialog(this).apply {
|
||||
setTitle(title)
|
||||
setMessage(message)
|
||||
setProgressStyle(style)
|
||||
if (style == ProgressDialog.STYLE_HORIZONTAL) {
|
||||
max = 100
|
||||
progress = 0
|
||||
}
|
||||
setCancelable(false)
|
||||
show()
|
||||
val view = layoutInflater.inflate(R.layout.dialog_progress, null)
|
||||
view.findViewById<TextView>(R.id.message).text = message
|
||||
val progressBar = view.findViewById<ProgressBar>(R.id.progressBar)
|
||||
if (style == STYLE_HORIZONTAL) {
|
||||
progressBar.isIndeterminate = false
|
||||
} else {
|
||||
progressBar.isIndeterminate = true
|
||||
}
|
||||
progressDialog = AlertDialog.Builder(this)
|
||||
.setTitle(title)
|
||||
.setView(view)
|
||||
.setCancelable(false)
|
||||
.show()
|
||||
}
|
||||
|
||||
private fun cleanupAction() {
|
||||
|
|
|
|||
22
android/app/src/main/res/layout/dialog_progress.xml
Normal file
22
android/app/src/main/res/layout/dialog_progress.xml
Normal 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>
|
||||
20
android/app/src/main/res/layout/dialog_waiting.xml
Normal file
20
android/app/src/main/res/layout/dialog_waiting.xml
Normal 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>
|
||||
Loading…
Reference in a new issue