Add debug dialog

This commit is contained in:
sim 2023-03-26 18:28:09 +02:00
parent 09e79a7c3a
commit 881e442ac6
4 changed files with 68 additions and 3 deletions

View File

@ -10,8 +10,10 @@ import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.* // ktlint-disable no-wildcard-imports
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isGone
import androidx.core.view.setPadding
import com.google.android.material.card.MaterialCardView
import org.unifiedpush.distributor.nextpush.R
import org.unifiedpush.distributor.nextpush.account.Account
@ -26,11 +28,14 @@ import org.unifiedpush.distributor.nextpush.services.FailureHandler
import org.unifiedpush.distributor.nextpush.services.RestartWorker
import org.unifiedpush.distributor.nextpush.services.StartService
import org.unifiedpush.distributor.nextpush.utils.TAG
import org.unifiedpush.distributor.nextpush.utils.getDebugInfo
import java.lang.String.format
class MainActivity : AppCompatActivity() {
private lateinit var listView: ListView
private var lastClickTime = 0L
private var clickCount = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -46,6 +51,7 @@ class MainActivity : AppCompatActivity() {
format(getString(R.string.main_account_desc), getAccount(this)?.name)
invalidateOptionsMenu()
RestartWorker.startPeriodic(this)
setDebugInformationListener()
}
override fun onStart() {
@ -161,6 +167,38 @@ class MainActivity : AppCompatActivity() {
}
)
}
private fun setDebugInformationListener() {
findViewById<TextView>(R.id.main_account_title).setOnClickListener {
val currentTime = System.currentTimeMillis()
if (currentTime - lastClickTime < 500) {
clickCount++
if (clickCount == 5) {
val scale = resources.displayMetrics.density
val dpAsPixels = (16 * scale + 0.5f)
val showText = TextView(this)
.apply {
setTextIsSelectable(true)
text = getDebugInfo()
setPadding(dpAsPixels.toInt())
}
AlertDialog.Builder(this)
.setTitle("Debug information")
.setView(showText)
.setCancelable(false)
.setPositiveButton("Ok") { dialog, _ ->
dialog.dismiss()
}
.show()
clickCount = 0 // Réinitialisez le compteur après l'affichage de la popup
}
} else {
clickCount = 1
}
lastClickTime = currentTime
}
}
companion object {
fun goToMainActivity(context: Context) {
val intent = Intent(

View File

@ -23,9 +23,6 @@ import java.util.Calendar
class SSEListener(val context: Context) : EventSourceListener() {
private var pinged = false
private var started = false
override fun onOpen(eventSource: EventSource, response: Response) {
FailureHandler.newEventSource(context, eventSource)
StartService.wakeLock?.let {
@ -33,6 +30,8 @@ class SSEListener(val context: Context) : EventSourceListener() {
it.release()
}
}
started = false
pinged = false
try {
Log.d(TAG, "onOpen: " + response.code)
} catch (e: Exception) {
@ -138,5 +137,9 @@ class SSEListener(val context: Context) : EventSourceListener() {
var lastEventDate: Calendar? = null
var keepalive = 900
private set
var pinged = false
private set
var started = false
private set
}
}

View File

@ -77,4 +77,10 @@ object FailureHandler {
// nFails > 0 to be sure it is not actually restarting
return if (orNeverStart) { eventSource == null } else { false } || nFails > 0
}
fun getDebugInfo(): String {
return "nFails: $nFails\n" +
"nFailsBeforePing: $nFailsBeforePing\n" +
"eventSource null: ${eventSource == null}"
}
}

View File

@ -0,0 +1,18 @@
package org.unifiedpush.distributor.nextpush.utils
import org.unifiedpush.distributor.nextpush.api.SSEListener
import org.unifiedpush.distributor.nextpush.services.FailureHandler
import org.unifiedpush.distributor.nextpush.services.StartService
import java.text.SimpleDateFormat
fun getDebugInfo(): String {
val date = SSEListener.lastEventDate?.let {
SimpleDateFormat.getDateTimeInstance().format(it.time)
} ?: "None"
return "ServiceStarted: ${StartService.isServiceStarted}\n" +
"Last Event: $date\n" +
"Keepalive: ${SSEListener.keepalive}\n" +
"SSE started: ${SSEListener.started}\n" +
"SSE pinged: ${SSEListener.pinged}\n" +
FailureHandler.getDebugInfo()
}