Annotated debug panel of to/from fields with hex form (#830)

This commit is contained in:
Mike Cumings 2024-02-02 18:55:41 -08:00 committed by GitHub
parent d75188f03c
commit e32a1dadea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 130 additions and 3 deletions

View file

@ -1,21 +1,25 @@
package com.geeksville.mesh.ui
import android.content.Context
import android.text.SpannedString
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.text.toSpannable
import androidx.recyclerview.widget.RecyclerView
import com.geeksville.mesh.R
import com.geeksville.mesh.database.entity.MeshLog
import java.text.DateFormat
import java.util.*
import java.util.Date
class DebugAdapter internal constructor(
context: Context
) : RecyclerView.Adapter<DebugAdapter.DebugViewHolder>() {
private val inflater: LayoutInflater = LayoutInflater.from(context)
private val colorAnnotation = ContextCompat.getColor(context, R.color.colorAnnotation)
private var logs = emptyList<MeshLog>()
private val timeFormat: DateFormat =
@ -35,11 +39,36 @@ class DebugAdapter internal constructor(
override fun onBindViewHolder(holder: DebugViewHolder, position: Int) {
val current = logs[position]
holder.logTypeView.text = current.message_type
holder.logRawMessage.text = current.raw_message
holder.logRawMessage.text = annotateMessage(current)
val date = Date(current.received_date)
holder.logDateReceivedView.text = timeFormat.format(date)
}
/**
* Enhance the raw message by visually distinguishing the annotations prior to when
* the data was added to the database.
*
* @see com.geeksville.mesh.ui.DebugFragment.annotateMeshLogs
*/
private fun annotateMessage(current: MeshLog): CharSequence {
val spannable = current.raw_message.toSpannable()
REGEX_ANNOTATED_NODE_ID.findAll(spannable).toList().reversed().forEach {
spannable.setSpan(
android.text.style.StyleSpan(android.graphics.Typeface.ITALIC),
it.range.first,
it.range.last + 1,
SpannedString.SPAN_EXCLUSIVE_EXCLUSIVE
)
spannable.setSpan(
android.text.style.ForegroundColorSpan(colorAnnotation),
it.range.first,
it.range.last + 1,
SpannedString.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
return spannable
}
internal fun setLogs(logs: List<MeshLog>) {
this.logs = logs
notifyDataSetChanged()
@ -47,4 +76,10 @@ class DebugAdapter internal constructor(
override fun getItemCount() = logs.size
private companion object {
/**
* Regex to match the node ID annotations in the MeshLog raw message text.
*/
val REGEX_ANNOTATED_NODE_ID = Regex("\\(![0-9a-fA-F]{8}\\)$", RegexOption.MULTILINE)
}
}