fix(core/e2ee): key exchange ui

This commit is contained in:
rhunk 2024-03-16 17:09:12 +01:00
parent a339e1190d
commit 52addbe780
2 changed files with 39 additions and 30 deletions

View File

@ -7,12 +7,16 @@ import android.graphics.drawable.ShapeDrawable
import android.graphics.drawable.shapes.Shape import android.graphics.drawable.shapes.Shape
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.Button import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import me.rhunk.snapenhance.common.data.ContentType import me.rhunk.snapenhance.common.data.ContentType
@ -20,6 +24,7 @@ import me.rhunk.snapenhance.common.data.MessageState
import me.rhunk.snapenhance.common.data.MessagingRuleType import me.rhunk.snapenhance.common.data.MessagingRuleType
import me.rhunk.snapenhance.common.data.RuleState import me.rhunk.snapenhance.common.data.RuleState
import me.rhunk.snapenhance.common.database.impl.ConversationMessage import me.rhunk.snapenhance.common.database.impl.ConversationMessage
import me.rhunk.snapenhance.common.ui.createComposeView
import me.rhunk.snapenhance.common.util.protobuf.ProtoEditor import me.rhunk.snapenhance.common.util.protobuf.ProtoEditor
import me.rhunk.snapenhance.common.util.protobuf.ProtoReader import me.rhunk.snapenhance.common.util.protobuf.ProtoReader
import me.rhunk.snapenhance.common.util.protobuf.ProtoWriter import me.rhunk.snapenhance.common.util.protobuf.ProtoWriter
@ -197,19 +202,13 @@ class EndToEndEncryption : MessagingRuleFeature(
val encryptedMessageIndicator by context.config.experimental.e2eEncryption.encryptedMessageIndicator val encryptedMessageIndicator by context.config.experimental.e2eEncryption.encryptedMessageIndicator
// hook view binder to add special buttons val specialCard = Random.nextLong().toString(16)
val receivePublicKeyTag = Random.nextLong().toString(16)
val receiveSecretTag = Random.nextLong().toString(16)
context.event.subscribe(BindViewEvent::class) { event -> context.event.subscribe(BindViewEvent::class) { event ->
event.chatMessage { conversationId, messageId -> event.chatMessage { conversationId, messageId ->
val viewGroup = event.view as ViewGroup val viewGroup = event.view.parent as? ViewGroup ?: return@subscribe
viewGroup.findViewWithTag<View>(receiveSecretTag)?.also { viewGroup.findViewWithTag<View>(specialCard)?.also {
viewGroup.removeView(it)
}
viewGroup.findViewWithTag<View>(receivePublicKeyTag)?.also {
viewGroup.removeView(it) viewGroup.removeView(it)
} }
@ -226,31 +225,40 @@ class EndToEndEncryption : MessagingRuleFeature(
} }
} }
secretResponses[messageId.toLong()]?.also { secret -> val secret = secretResponses[messageId.toLong()]
viewGroup.addView(Button(context.mainActivity!!).apply { val publicKey = pkRequests[messageId.toLong()]
text = "Accept secret"
tag = receiveSecretTag
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
)
setOnClickListener {
handleSecretResponse(conversationId, secret)
}
})
}
pkRequests[messageId.toLong()]?.also { publicKey -> if (publicKey != null || secret != null) {
viewGroup.addView(Button(context.mainActivity!!).apply { viewGroup.addView(createComposeView(context.mainActivity!!) {
text = "Receive public key" Card(
tag = receivePublicKeyTag modifier = Modifier.fillMaxWidth().padding(8.dp),
onClick = {
if (publicKey != null) {
handlePublicKeyRequest(conversationId, publicKey)
}
if (secret != null) {
handleSecretResponse(conversationId, secret)
}
}
) {
Box(
modifier = Modifier.fillMaxWidth().padding(5.dp),
contentAlignment = Alignment.Center
) {
if (publicKey != null) {
Text("Receive public key")
}
if (secret != null) {
Text("Accept secret")
}
}
}
}.apply {
tag = specialCard
layoutParams = ViewGroup.LayoutParams( layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,
) )
setOnClickListener {
handlePublicKeyRequest(conversationId, publicKey)
}
}) })
} }
} }

View File

@ -12,6 +12,7 @@ fun debugEditText(context: Context, initialText: String): View {
addView(EditText(context).apply { addView(EditText(context).apply {
inputType = InputType.TYPE_NULL inputType = InputType.TYPE_NULL
isSingleLine = false isSingleLine = false
setTextColor(resources.getColor(android.R.color.white, context.theme))
setTextIsSelectable(true) setTextIsSelectable(true)
textSize = 12f textSize = 12f
setPadding(20, 20, 20, 20) setPadding(20, 20, 20, 20)