Skip to content

Commit a9fc062

Browse files
committed
Support tox-uris in the form tox://0123ABCD?name=robin&message=hello
`tox:0123ABCD` is still supported, but it can't have query-parameters as it's an opaque uri. `tox://0123ABCD` is a hierarchical uri and supports all the fancy features.
1 parent 8f8c55e commit a9fc062

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

atox/src/main/kotlin/MainActivity.kt

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,41 @@ import javax.inject.Inject
1919
import ltd.evilcorp.atox.di.ViewModelFactory
2020
import ltd.evilcorp.atox.settings.Settings
2121
import ltd.evilcorp.atox.ui.contactlist.ARG_SHARE
22+
import ltd.evilcorp.domain.tox.ToxID
2223

2324
private const val TAG = "MainActivity"
24-
private const val TOX_ID_LENGTH = 76
2525

26-
private fun isToxUri(uri: Uri) = uri.isOpaque && uri.scheme == "tox" && uri.schemeSpecificPart.length == TOX_ID_LENGTH
26+
// TODO(robinlinden): Move to common place, use for QR-code-provided uris as well.
27+
data class ToxUri(val toxId: ToxID, val nameSuggestion: String? = null, val messageSuggestion: String? = null) {
28+
companion object {
29+
private const val TOX_ID_LENGTH = 76
30+
31+
fun parseFrom(uri: Uri?): ToxUri? {
32+
if (uri == null) {
33+
return null
34+
}
35+
36+
if (uri.scheme != "tox") {
37+
return null
38+
}
39+
40+
if (uri.isOpaque) {
41+
if (uri.schemeSpecificPart.length != TOX_ID_LENGTH) {
42+
return null
43+
}
44+
45+
return ToxUri(ToxID(uri.schemeSpecificPart))
46+
}
47+
48+
val maybeToxId = uri.host
49+
if (maybeToxId?.length != TOX_ID_LENGTH) {
50+
return null
51+
}
52+
53+
return ToxUri(ToxID(maybeToxId), uri.getQueryParameter("name"), uri.getQueryParameter("message"))
54+
}
55+
}
56+
}
2757

2858
class MainActivity : AppCompatActivity() {
2959
@Inject
@@ -85,14 +115,19 @@ class MainActivity : AppCompatActivity() {
85115
private fun handleToxLinkIntent(intent: Intent) {
86116
val data = intent.data
87117
Log.i(TAG, "Got uri with data: $data")
88-
if (data == null || !isToxUri(data)) {
118+
val toxUri = ToxUri.parseFrom(data)
119+
if (toxUri == null) {
89120
Log.e(TAG, "Got malformed uri: $data")
90121
return
91122
}
92123

93124
supportFragmentManager.findFragmentById(R.id.nav_host_fragment)?.findNavController()?.navigate(
94125
R.id.addContactFragment,
95-
bundleOf("toxId" to data.schemeSpecificPart),
126+
bundleOf(
127+
"toxId" to toxUri.toxId.string(),
128+
"name" to toxUri.nameSuggestion,
129+
"message" to toxUri.messageSuggestion,
130+
),
96131
)
97132
}
98133

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2019-2024 Robin Lindén <dev@robinlinden.eu>
1+
// SPDX-FileCopyrightText: 2019-2025 Robin Lindén <dev@robinlinden.eu>
22
// SPDX-FileCopyrightText: 2020 aTox contributors
33
//
44
// SPDX-License-Identifier: GPL-3.0-only
@@ -134,5 +134,11 @@ class AddContactFragment : BaseFragment<FragmentAddContactBinding>(FragmentAddCo
134134
add.isEnabled = false
135135

136136
toxId.setText(arguments?.getString("toxId"), TextView.BufferType.EDITABLE)
137+
val messageSuggestion = arguments?.getString("message")
138+
if (messageSuggestion != null) {
139+
message.setText(messageSuggestion)
140+
}
141+
142+
// TODO(robinlinden): Hook up the nickname suggestion once we have nicknames.
137143
}
138144
}

0 commit comments

Comments
 (0)