feat: add encryption status icon and dialog to node list items

This commit is contained in:
andrekir 2024-09-23 10:08:35 -03:00
parent 587c6c91f4
commit 22b1cf0f16
7 changed files with 87 additions and 3 deletions

View file

@ -14,6 +14,7 @@ import com.geeksville.mesh.Position
import com.geeksville.mesh.TelemetryProtos
import com.geeksville.mesh.copy
import com.geeksville.mesh.util.latLongToMeter
import com.google.protobuf.ByteString
@Suppress("MagicNumber")
@Entity(tableName = "nodes")
@ -80,6 +81,8 @@ data class NodeEntity(
}
val hasPKC get() = !user.publicKey.isEmpty
val errorByteString: ByteString get() = ByteString.copyFrom(ByteArray(32) { 0 })
val mismatchKey get() = user.publicKey == errorByteString
val batteryLevel get() = deviceMetrics.batteryLevel
val voltage get() = deviceMetrics.voltage

View file

@ -785,7 +785,11 @@ class MeshService : Service(), Logging {
/// Update our DB of users based on someone sending out a User subpacket
private fun handleReceivedUser(fromNum: Int, p: MeshProtos.User, channel: Int = 0) {
updateNodeInfo(fromNum) {
it.user = p
val keyMatch = !it.hasPKC || it.user.publicKey == p.publicKey
it.user = if (keyMatch) p else p.copy {
warn("Public key mismatch from $longName ($shortName)")
publicKey = it.errorByteString
}
it.longName = p.longName
it.shortName = p.shortName
it.channel = channel

View file

@ -21,6 +21,7 @@ import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.text.selection.DisableSelection
import androidx.compose.foundation.text.selection.SelectionContainer
@ -37,6 +38,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
@ -54,6 +56,8 @@ import com.geeksville.mesh.ConfigProtos.Config.DisplayConfig
import com.geeksville.mesh.MeshProtos
import com.geeksville.mesh.R
import com.geeksville.mesh.database.entity.NodeEntity
import com.geeksville.mesh.ui.components.NodeKeyStatusIcon
import com.geeksville.mesh.ui.components.SimpleAlertDialog
import com.geeksville.mesh.ui.compose.ElevationInfo
import com.geeksville.mesh.ui.compose.SatelliteCountInfo
import com.geeksville.mesh.ui.preview.NodeEntityPreviewParameterProvider
@ -119,6 +123,16 @@ fun NodeItem(
val (detailsShown, showDetails) = remember { mutableStateOf(expanded) }
var showEncryptionDialog by remember { mutableStateOf(false) }
if (showEncryptionDialog) {
val (title, text) = when {
thatNode.mismatchKey -> R.string.encryption_error to R.string.encryption_error_text
thatNode.hasPKC -> R.string.encryption_pkc to R.string.encryption_pkc_text
else -> R.string.encryption_psk to R.string.encryption_psk_text
}
SimpleAlertDialog(title, text) { showEncryptionDialog = false }
}
Card(
modifier = Modifier
.fillMaxWidth()
@ -142,7 +156,6 @@ fun NodeItem(
Chip(
modifier = Modifier
.width(IntrinsicSize.Min)
.padding(end = 8.dp)
.defaultMinSize(minHeight = 32.dp, minWidth = 72.dp),
colors = ChipDefaults.chipColors(
backgroundColor = Color(nodeColor),
@ -160,9 +173,14 @@ fun NodeItem(
)
},
)
NodeKeyStatusIcon(
hasPKC = thatNode.hasPKC,
mismatchKey = thatNode.mismatchKey,
modifier = Modifier.size(32.dp)
) { showEncryptionDialog = true }
Text(
modifier = Modifier.weight(1f),
text = if (thatNode.hasPKC) "🔒 $longName" else longName,
text = longName,
style = style,
textDecoration = TextDecoration.LineThrough.takeIf { isIgnored },
softWrap = true,

View file

@ -0,0 +1,42 @@
package com.geeksville.mesh.ui.components
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.KeyOff
import androidx.compose.material.icons.filled.Lock
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import com.geeksville.mesh.R
@Composable
fun NodeKeyStatusIcon(
hasPKC: Boolean,
mismatchKey: Boolean,
modifier: Modifier = Modifier,
onClick: () -> Unit = {},
) = IconButton(
onClick = onClick,
modifier = modifier,
) {
val (icon, tint) = when {
mismatchKey -> rememberVectorPainter(Icons.Default.KeyOff) to Color.Red
hasPKC -> rememberVectorPainter(Icons.Default.Lock) to Color.Green
else -> painterResource(R.drawable.ic_lock_open_right_24) to Color.Yellow
}
Icon(
painter = icon,
contentDescription = stringResource(
id = when {
mismatchKey -> R.string.encryption_error
hasPKC -> R.string.encryption_pkc
else -> R.string.encryption_psk
}
),
tint = tint,
)
}

View file

@ -10,6 +10,7 @@ import com.geeksville.mesh.paxcount
import com.geeksville.mesh.position
import com.geeksville.mesh.telemetry
import com.geeksville.mesh.user
import com.google.protobuf.ByteString
import kotlin.random.Random
class NodeEntityPreviewParameterProvider : PreviewParameterProvider<NodeEntity> {
@ -94,6 +95,7 @@ class NodeEntityPreviewParameterProvider : PreviewParameterProvider<NodeEntity>
longName = "Donald Duck, the Grand Duck of the Ducks"
shortName = "DoDu"
hwModel = MeshProtos.HardwareModel.HELTEC_V3
publicKey = ByteString.copyFrom(ByteArray(32) { 1 })
},
longName = "Donald Duck, the Grand Duck of the Ducks",
shortName = "DoDu",