From 881e442ac66be73ae8aefafcda8791c2312fdf99 Mon Sep 17 00:00:00 2001 From: sim Date: Sun, 26 Mar 2023 18:28:09 +0200 Subject: [PATCH] Add debug dialog --- .../nextpush/activities/MainActivity.kt | 38 +++++++++++++++++++ .../distributor/nextpush/api/SSEListener.kt | 9 +++-- .../nextpush/services/FailureHandler.kt | 6 +++ .../nextpush/utils/DebugInformation.kt | 18 +++++++++ 4 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/org/unifiedpush/distributor/nextpush/utils/DebugInformation.kt diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainActivity.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainActivity.kt index e1f98a3..bd19825 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainActivity.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainActivity.kt @@ -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(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( diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/SSEListener.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/SSEListener.kt index 8b4d214..4e40c1d 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/SSEListener.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/SSEListener.kt @@ -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 } } diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/FailureHandler.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/FailureHandler.kt index ae285f4..0fc935a 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/services/FailureHandler.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/services/FailureHandler.kt @@ -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}" + } } diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/utils/DebugInformation.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/utils/DebugInformation.kt new file mode 100644 index 0000000..aa8275f --- /dev/null +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/utils/DebugInformation.kt @@ -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() +}