Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 43 additions & 6 deletions atox/src/main/kotlin/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// SPDX-FileCopyrightText: 2019-2024 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2019-2025 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: GPL-3.0-only

package ltd.evilcorp.atox

import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.util.Log
Expand All @@ -18,10 +19,41 @@ import javax.inject.Inject
import ltd.evilcorp.atox.di.ViewModelFactory
import ltd.evilcorp.atox.settings.Settings
import ltd.evilcorp.atox.ui.contactlist.ARG_SHARE
import ltd.evilcorp.domain.tox.ToxID

private const val TAG = "MainActivity"
private const val SCHEME = "tox:"
private const val TOX_ID_LENGTH = 76

// TODO(robinlinden): Move to common place, use for QR-code-provided uris as well.
data class ToxUri(val toxId: ToxID, val nameSuggestion: String? = null, val messageSuggestion: String? = null) {
companion object {
private const val TOX_ID_LENGTH = 76

fun parseFrom(uri: Uri?): ToxUri? {
if (uri == null) {
return null
}

if (uri.scheme != "tox") {
return null
}

if (uri.isOpaque) {
if (uri.schemeSpecificPart.length != TOX_ID_LENGTH) {
return null
}

return ToxUri(ToxID(uri.schemeSpecificPart))
}

val maybeToxId = uri.host
if (maybeToxId?.length != TOX_ID_LENGTH) {
return null
}

return ToxUri(ToxID(maybeToxId), uri.getQueryParameter("name"), uri.getQueryParameter("message"))
}
}
}

class MainActivity : AppCompatActivity() {
@Inject
Expand Down Expand Up @@ -81,16 +113,21 @@ class MainActivity : AppCompatActivity() {
}

private fun handleToxLinkIntent(intent: Intent) {
val data = intent.dataString ?: ""
val data = intent.data
Log.i(TAG, "Got uri with data: $data")
if (!data.startsWith(SCHEME) || data.length != SCHEME.length + TOX_ID_LENGTH) {
val toxUri = ToxUri.parseFrom(data)
if (toxUri == null) {
Log.e(TAG, "Got malformed uri: $data")
return
}

supportFragmentManager.findFragmentById(R.id.nav_host_fragment)?.findNavController()?.navigate(
R.id.addContactFragment,
bundleOf("toxId" to data.drop(SCHEME.length)),
bundleOf(
"toxId" to toxUri.toxId.string(),
"name" to toxUri.nameSuggestion,
"message" to toxUri.messageSuggestion,
),
)
}

Expand Down
8 changes: 7 additions & 1 deletion atox/src/main/kotlin/ui/addcontact/AddContactFragment.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019-2024 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2019-2025 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2020 aTox contributors
//
// SPDX-License-Identifier: GPL-3.0-only
Expand Down Expand Up @@ -134,5 +134,11 @@ class AddContactFragment : BaseFragment<FragmentAddContactBinding>(FragmentAddCo
add.isEnabled = false

toxId.setText(arguments?.getString("toxId"), TextView.BufferType.EDITABLE)
val messageSuggestion = arguments?.getString("message")
if (messageSuggestion != null) {
message.setText(messageSuggestion)
}

// TODO(robinlinden): Hook up the nickname suggestion once we have nicknames.
}
}
Loading