Skip to content

Commit bb6a698

Browse files
committed
Use the PublicKey type for public keys in the database
This means a lot of conversions go away.
1 parent a600d44 commit bb6a698

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+289
-258
lines changed
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2022 Robin Lindén <[email protected]>
1+
// SPDX-FileCopyrightText: 2022-2025 Robin Lindén <[email protected]>
22
//
33
// SPDX-License-Identifier: GPL-3.0-only
44

@@ -7,16 +7,19 @@ package ltd.evilcorp.atox.ui
77
import androidx.test.ext.junit.runners.AndroidJUnit4
88
import androidx.test.platform.app.InstrumentationRegistry
99
import kotlin.test.Test
10+
import ltd.evilcorp.core.vo.PublicKey
1011
import org.junit.runner.RunWith
1112

13+
private val pk = PublicKey("123")
14+
1215
@RunWith(AndroidJUnit4::class)
1316
class AvatarFactoryTest {
1417
@Test
1518
fun emptyName() {
1619
AvatarFactory.create(
1720
InstrumentationRegistry.getInstrumentation().targetContext.resources,
1821
name = "",
19-
publicKey = "123",
22+
pk = pk,
2023
)
2124
}
2225

@@ -25,7 +28,7 @@ class AvatarFactoryTest {
2528
AvatarFactory.create(
2629
InstrumentationRegistry.getInstrumentation().targetContext.resources,
2730
name = "a ",
28-
publicKey = "123",
31+
pk = pk,
2932
)
3033
}
3134
}

atox/src/main/kotlin/ActionReceiver.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,18 @@ class ActionReceiver : BroadcastReceiver() {
7676
Log.e(TAG, "Got intent without required key $KEY_CONTACT_PK $intent")
7777
return@launch
7878
}
79-
if (!contactRepository.exists(pk.string())) {
79+
if (!contactRepository.exists(pk)) {
8080
notificationHelper.dismissNotifications(pk)
8181
notificationHelper.dismissCallNotification(pk)
8282
return@launch
8383
}
8484

8585
RemoteInput.getResultsFromIntent(intent)?.let { results ->
8686
results.getCharSequence(KEY_TEXT_REPLY)?.toString()?.let { input ->
87-
contactRepository.setHasUnreadMessages(pk.string(), false)
87+
contactRepository.setHasUnreadMessages(pk, false)
8888
chatManager.sendMessage(pk, input)
8989
notificationHelper.showMessageNotification(
90-
Contact(pk.string(), tox.getName()),
90+
Contact(pk, tox.getName()),
9191
input,
9292
outgoing = true,
9393
)
@@ -103,7 +103,7 @@ class ActionReceiver : BroadcastReceiver() {
103103
}
104104
Action.CallIgnore -> callManager.removePendingCall(pk)
105105
Action.MarkAsRead -> {
106-
contactRepository.setHasUnreadMessages(pk.string(), false)
106+
contactRepository.setHasUnreadMessages(pk, false)
107107
notificationHelper.dismissNotifications(pk)
108108
}
109109
null -> Log.e(TAG, "Missing action in intent $intent")
@@ -117,7 +117,7 @@ class ActionReceiver : BroadcastReceiver() {
117117
it
118118
} else {
119119
Log.e(TAG, "Unable to get contact ${pk.fingerprint()} for call notification")
120-
Contact(publicKey = pk.string(), name = pk.fingerprint())
120+
Contact(publicKey = pk, name = pk.fingerprint())
121121
}
122122
}
123123

atox/src/main/kotlin/ToxService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2019-2024 Robin Lindén <[email protected]>
1+
// SPDX-FileCopyrightText: 2019-2025 Robin Lindén <[email protected]>
22
// SPDX-FileCopyrightText: 2021-2022 aTox contributors
33
//
44
// SPDX-License-Identifier: GPL-3.0-only
@@ -139,7 +139,7 @@ class ToxService : LifecycleService() {
139139
}
140140

141141
lifecycleScope.launch(Dispatchers.Default) {
142-
userRepository.get(tox.publicKey.string())
142+
userRepository.get(tox.publicKey)
143143
.filterNotNull()
144144
.filter { it.connectionStatus != connectionStatus }
145145
.flowWithLifecycle(lifecycle)

atox/src/main/kotlin/tox/EventListenerCallbacks.kt

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ private fun isImage(filename: String) = try {
5252
false
5353
}
5454

55-
private const val FINGERPRINT_LEN = 8
56-
private fun String.fingerprint() = this.take(FINGERPRINT_LEN)
57-
5855
@Singleton
5956
class EventListenerCallbacks @Inject constructor(
6057
private val ctx: Context,
@@ -73,7 +70,7 @@ class EventListenerCallbacks @Inject constructor(
7370
private var audioPlayer: AudioPlayer? = null
7471
private val scope = CoroutineScope(Dispatchers.Default)
7572

76-
private suspend fun tryGetContact(pk: String, tag: String) = contactRepository.get(pk).firstOrNull().let {
73+
private suspend fun tryGetContact(pk: PublicKey, tag: String) = contactRepository.get(pk).firstOrNull().let {
7774
if (it == null) Log.e(TAG, "$tag -> unable to get contact for ${pk.fingerprint()}")
7875
it
7976
}
@@ -147,7 +144,7 @@ class EventListenerCallbacks @Inject constructor(
147144
}
148145

149146
fileRecvHandler = { publicKey, fileNo, kind, fileSize, filename ->
150-
val name = if (kind == FileKind.Avatar.ordinal) publicKey else filename
147+
val name = if (kind == FileKind.Avatar.ordinal) publicKey.string() else filename
151148

152149
val id = fileTransferManager.add(FileTransfer(publicKey, fileNo, kind, fileSize, name, outgoing = false))
153150

@@ -168,16 +165,16 @@ class EventListenerCallbacks @Inject constructor(
168165
}
169166
}
170167

171-
fileRecvControlHandler = { publicKey: String, fileNo: Int, control: ToxFileControl ->
172-
fileTransferManager.setStatus(publicKey, fileNo, control)
168+
fileRecvControlHandler = { pk: PublicKey, fileNo: Int, control: ToxFileControl ->
169+
fileTransferManager.setStatus(pk, fileNo, control)
173170
}
174171

175-
fileChunkRequestHandler = { publicKey: String, fileNo: Int, position: Long, length: Int ->
176-
fileTransferManager.sendChunk(publicKey, fileNo, position, length)
172+
fileChunkRequestHandler = { pk: PublicKey, fileNo: Int, position: Long, length: Int ->
173+
fileTransferManager.sendChunk(pk, fileNo, position, length)
177174
}
178175

179176
selfConnectionStatusHandler = { status ->
180-
userRepository.updateConnection(tox.publicKey.string(), status)
177+
userRepository.updateConnection(tox.publicKey, status)
181178
}
182179

183180
friendTypingHandler = { publicKey, isTyping ->
@@ -201,8 +198,8 @@ class EventListenerCallbacks @Inject constructor(
201198
audioPlayer?.stop()
202199
audioPlayer?.release()
203200
audioPlayer = null
204-
notificationHelper.dismissCallNotification(PublicKey(pk))
205-
callManager.endCall(PublicKey(pk))
201+
notificationHelper.dismissCallNotification(pk)
202+
callManager.endCall(pk)
206203
}
207204
}
208205

atox/src/main/kotlin/ui/AvatarFactory.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// SPDX-FileCopyrightText: 2019-2022 aTox contributors
1+
// SPDX-FileCopyrightText: 2019-2025 Robin Lindén <[email protected]>
2+
// SPDX-FileCopyrightText: 2021-2022 aTox contributors
23
//
34
// SPDX-License-Identifier: GPL-3.0-only
45

@@ -14,6 +15,7 @@ import android.graphics.RectF
1415
import android.graphics.Typeface
1516
import kotlin.math.abs
1617
import ltd.evilcorp.atox.R
18+
import ltd.evilcorp.core.vo.PublicKey
1719

1820
internal object AvatarFactory {
1921

@@ -27,7 +29,7 @@ internal object AvatarFactory {
2729
fun create(
2830
resources: Resources,
2931
name: String,
30-
publicKey: String,
32+
pk: PublicKey,
3133
size: Px = Px(resources.getDimension(R.dimen.default_avatar_size).toInt()),
3234
): Bitmap {
3335
val defaultAvatarSize = resources.getDimension(R.dimen.default_avatar_size)
@@ -37,7 +39,7 @@ internal object AvatarFactory {
3739
val canvas = Canvas(bitmap)
3840
val rect = RectF(0f, 0f, bitmap.width.toFloat(), bitmap.height.toFloat())
3941
val colors = resources.getIntArray(R.array.contactBackgrounds)
40-
val backgroundPaint = Paint().apply { color = colors[abs(publicKey.hashCode()).rem(colors.size)] }
42+
val backgroundPaint = Paint().apply { color = colors[abs(pk.hashCode()).rem(colors.size)] }
4143

4244
val textPaint = Paint().apply {
4345
color = Color.WHITE

atox/src/main/kotlin/ui/AvatarImageView.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-FileCopyrightText: 2022 aTox contributors
2-
// SPDX-FileCopyrightText: 2022-2024 Robin Lindén <[email protected]>
2+
// SPDX-FileCopyrightText: 2022-2025 Robin Lindén <[email protected]>
33
//
44
// SPDX-License-Identifier: GPL-3.0-only
55

@@ -21,6 +21,7 @@ import kotlin.math.sqrt
2121
import ltd.evilcorp.atox.R
2222
import ltd.evilcorp.core.vo.ConnectionStatus
2323
import ltd.evilcorp.core.vo.Contact
24+
import ltd.evilcorp.core.vo.PublicKey
2425
import ltd.evilcorp.core.vo.UserStatus
2526

2627
private const val STATUS_INDICATOR_SIZE_RATIO_WITH_AVATAR = 12f / 50
@@ -50,7 +51,7 @@ class AvatarImageView @JvmOverloads constructor(context: Context, attrs: Attribu
5051
}
5152

5253
private var name = ""
53-
private var publicKey = ""
54+
private var publicKey = PublicKey("")
5455
private var avatarUri = ""
5556

5657
fun setFrom(contact: Contact) {

atox/src/main/kotlin/ui/NotificationHelper.kt

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class NotificationHelper @Inject constructor(private val context: Context) {
138138
.setSmallIcon(android.R.drawable.sym_action_chat)
139139
.setContentTitle(contact.name.ifEmpty { context.getText(R.string.contact_default_name) })
140140
.setContentText(message)
141-
.setContentIntent(deepLinkToChat(PublicKey(contact.publicKey)))
141+
.setContentIntent(deepLinkToChat(contact.publicKey))
142142
.setAutoCancel(true)
143143
.setSilent(silent)
144144

@@ -151,13 +151,15 @@ class NotificationHelper @Inject constructor(private val context: Context) {
151151

152152
val chatPartner = Person.Builder()
153153
.setName(contact.name.ifEmpty { context.getText(R.string.contact_default_name) })
154-
.setKey(if (outgoing) "myself" else contact.publicKey)
154+
.setKey(if (outgoing) "myself" else contact.publicKey.string())
155155
.setIcon(icon)
156156
.setImportant(true)
157157
.build()
158158

159159
val style =
160-
notifierOld.activeNotifications.find { it.notification.group == contact.publicKey }?.notification?.let {
160+
notifierOld.activeNotifications.find {
161+
it.notification.group == contact.publicKey.string()
162+
}?.notification?.let {
161163
NotificationCompat.MessagingStyle.extractMessagingStyleFromNotification(it)
162164
} ?: NotificationCompat.MessagingStyle(chatPartner)
163165

@@ -167,7 +169,7 @@ class NotificationHelper @Inject constructor(private val context: Context) {
167169

168170
notificationBuilder
169171
.setStyle(style)
170-
.setGroup(contact.publicKey)
172+
.setGroup(contact.publicKey.string())
171173
}
172174

173175
// I can't find it in the documentation for RemoteInput or anything, but per
@@ -182,7 +184,10 @@ class NotificationHelper @Inject constructor(private val context: Context) {
182184
PendingIntentCompat.getBroadcast(
183185
context,
184186
contact.publicKey.hashCode(),
185-
Intent(context, ActionReceiver::class.java).putExtra(KEY_CONTACT_PK, contact.publicKey),
187+
Intent(context, ActionReceiver::class.java).putExtra(
188+
KEY_CONTACT_PK,
189+
contact.publicKey.string(),
190+
),
186191
PendingIntent.FLAG_UPDATE_CURRENT,
187192
mutable = true,
188193
),
@@ -202,7 +207,7 @@ class NotificationHelper @Inject constructor(private val context: Context) {
202207
.Builder(
203208
IconCompat.createWithResource(context, R.drawable.ic_send),
204209
context.getString(R.string.reply),
205-
deepLinkToChat(PublicKey(contact.publicKey), focusMessageBox = true),
210+
deepLinkToChat(contact.publicKey, focusMessageBox = true),
206211
)
207212
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_REPLY)
208213
.build(),
@@ -217,7 +222,7 @@ class NotificationHelper @Inject constructor(private val context: Context) {
217222
context,
218223
"${contact.publicKey}_mark_as_read".hashCode(),
219224
Intent(context, ActionReceiver::class.java)
220-
.putExtra(KEY_CONTACT_PK, contact.publicKey)
225+
.putExtra(KEY_CONTACT_PK, contact.publicKey.string())
221226
.putExtra(KEY_ACTION, Action.MarkAsRead),
222227
PendingIntent.FLAG_UPDATE_CURRENT,
223228
),
@@ -266,7 +271,7 @@ class NotificationHelper @Inject constructor(private val context: Context) {
266271
return
267272
}
268273

269-
dismissCallNotification(PublicKey(contact.publicKey))
274+
dismissCallNotification(contact.publicKey)
270275
val notificationBuilder = NotificationCompat.Builder(context, CALL)
271276
.setCategory(NotificationCompat.CATEGORY_CALL)
272277
.setSmallIcon(android.R.drawable.ic_menu_call)
@@ -282,8 +287,8 @@ class NotificationHelper @Inject constructor(private val context: Context) {
282287
.setContentIntent(
283288
NavDeepLinkBuilder(context)
284289
.setGraph(R.navigation.nav_graph)
285-
.addDestination(R.id.chatFragment, bundleOf(CONTACT_PUBLIC_KEY to contact.publicKey))
286-
.addDestination(R.id.callFragment, bundleOf(CONTACT_PUBLIC_KEY to contact.publicKey))
290+
.addDestination(R.id.chatFragment, bundleOf(CONTACT_PUBLIC_KEY to contact.publicKey.string()))
291+
.addDestination(R.id.callFragment, bundleOf(CONTACT_PUBLIC_KEY to contact.publicKey.string()))
287292
.createPendingIntent(),
288293
)
289294
.addAction(
@@ -295,7 +300,7 @@ class NotificationHelper @Inject constructor(private val context: Context) {
295300
context,
296301
"${contact.publicKey}_end_call".hashCode(),
297302
Intent(context, ActionReceiver::class.java)
298-
.putExtra(KEY_CONTACT_PK, contact.publicKey)
303+
.putExtra(KEY_CONTACT_PK, contact.publicKey.string())
299304
.putExtra(KEY_ACTION, Action.CallEnd),
300305
PendingIntent.FLAG_UPDATE_CURRENT,
301306
),
@@ -323,7 +328,7 @@ class NotificationHelper @Inject constructor(private val context: Context) {
323328
.setSmallIcon(android.R.drawable.ic_menu_call)
324329
.setContentTitle(context.getString(R.string.incoming_call))
325330
.setContentText(context.getString(R.string.incoming_call_from, c.name))
326-
.setContentIntent(deepLinkToChat(PublicKey(c.publicKey)))
331+
.setContentIntent(deepLinkToChat(c.publicKey))
327332
.addAction(
328333
NotificationCompat.Action
329334
.Builder(
@@ -333,7 +338,7 @@ class NotificationHelper @Inject constructor(private val context: Context) {
333338
context,
334339
"${c.publicKey}_accept_call".hashCode(),
335340
Intent(context, ActionReceiver::class.java)
336-
.putExtra(KEY_CONTACT_PK, c.publicKey)
341+
.putExtra(KEY_CONTACT_PK, c.publicKey.string())
337342
.putExtra(KEY_ACTION, Action.CallAccept),
338343
PendingIntent.FLAG_UPDATE_CURRENT,
339344
),
@@ -350,7 +355,7 @@ class NotificationHelper @Inject constructor(private val context: Context) {
350355
context,
351356
"${c.publicKey}_reject_call".hashCode(),
352357
Intent(context, ActionReceiver::class.java)
353-
.putExtra(KEY_CONTACT_PK, c.publicKey)
358+
.putExtra(KEY_CONTACT_PK, c.publicKey.string())
354359
.putExtra(KEY_ACTION, Action.CallReject),
355360
PendingIntent.FLAG_UPDATE_CURRENT,
356361
),
@@ -362,7 +367,7 @@ class NotificationHelper @Inject constructor(private val context: Context) {
362367
context,
363368
"${c.publicKey}_ignore_call".hashCode(),
364369
Intent(context, ActionReceiver::class.java)
365-
.putExtra(KEY_CONTACT_PK, c.publicKey)
370+
.putExtra(KEY_CONTACT_PK, c.publicKey.string())
366371
.putExtra(KEY_ACTION, Action.CallIgnore),
367372
PendingIntent.FLAG_UPDATE_CURRENT,
368373
),

atox/src/main/kotlin/ui/addcontact/AddContactFragment.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2019-2024 Robin Lindén <[email protected]>
1+
// SPDX-FileCopyrightText: 2019-2025 Robin Lindén <[email protected]>
22
// SPDX-FileCopyrightText: 2020 aTox contributors
33
//
44
// SPDX-License-Identifier: GPL-3.0-only
@@ -84,7 +84,7 @@ class AddContactFragment : BaseFragment<FragmentAddContactBinding>(FragmentAddCo
8484
}
8585

8686
if (toxId.error == null) {
87-
if (contacts.find { it.publicKey == input.toPublicKey().string() } != null) {
87+
if (contacts.find { it.publicKey == input.toPublicKey() } != null) {
8888
toxId.error = getString(R.string.tox_id_error_already_exists)
8989
}
9090
}

atox/src/main/kotlin/ui/addcontact/AddContactViewModel.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// SPDX-FileCopyrightText: 2019-2022 aTox contributors
1+
// SPDX-FileCopyrightText: 2019-2025 Robin Lindén <[email protected]>
2+
// SPDX-FileCopyrightText: 2022 aTox contributors
23
//
34
// SPDX-License-Identifier: GPL-3.0-only
45

@@ -16,6 +17,7 @@ import ltd.evilcorp.core.repository.MessageRepository
1617
import ltd.evilcorp.core.vo.Contact
1718
import ltd.evilcorp.core.vo.Message
1819
import ltd.evilcorp.core.vo.MessageType
20+
import ltd.evilcorp.core.vo.PublicKey
1921
import ltd.evilcorp.core.vo.Sender
2022
import ltd.evilcorp.domain.feature.ContactManager
2123
import ltd.evilcorp.domain.tox.Tox
@@ -35,7 +37,7 @@ class AddContactViewModel @Inject constructor(
3537
fun isToxRunning() = tox.started
3638
fun tryLoadTox(): Boolean = toxStarter.tryLoadTox(null) == ToxSaveStatus.Ok
3739

38-
private fun addToChatLog(publicKey: String, message: String) = scope.launch {
40+
private fun addToChatLog(publicKey: PublicKey, message: String) = scope.launch {
3941
messageRepository.add(
4042
Message(
4143
publicKey,
@@ -50,6 +52,6 @@ class AddContactViewModel @Inject constructor(
5052

5153
fun addContact(toxId: ToxID, message: String) {
5254
contactManager.add(toxId, message)
53-
addToChatLog(toxId.toPublicKey().string(), message)
55+
addToChatLog(toxId.toPublicKey(), message)
5456
}
5557
}

0 commit comments

Comments
 (0)