android, desktop: catch URI creation errors (#4901)

* android, desktop: catch URI creation errors

* showing alert when pasting an incorrect link

* moved from Uri to String while processing SimpleX links
This commit is contained in:
Stanislav Dmitrenko
2024-09-19 08:36:54 +00:00
committed by GitHub
parent 10ded1530c
commit c5813b3489
9 changed files with 19 additions and 24 deletions
@@ -150,12 +150,9 @@ fun processIntent(intent: Intent?) {
"android.intent.action.VIEW" -> {
val uri = intent.data
if (uri != null) {
val transformedUri = uri.toURIOrNull()
if (transformedUri != null) {
chatModel.appOpenUrl.value = null to transformedUri
} else {
AlertManager.shared.showAlertMsg(generalGetString(MR.strings.error_parsing_uri_title), generalGetString(MR.strings.error_parsing_uri_desc))
}
chatModel.appOpenUrl.value = null to uri.toString()
} else {
AlertManager.shared.showAlertMsg(generalGetString(MR.strings.error_parsing_uri_title), generalGetString(MR.strings.error_parsing_uri_desc))
}
}
}
@@ -82,7 +82,7 @@ object ChatModel {
val desktopOnboardingRandomPassword = mutableStateOf(false)
// set when app is opened via contact or invitation URI (rhId, uri)
val appOpenUrl = mutableStateOf<Pair<Long?, URI>?>(null)
val appOpenUrl = mutableStateOf<Pair<Long?, String>?>(null)
// Needed to check for bottom nav bar and to apply or not navigation bar color on Android
val newChatSheetVisible = mutableStateOf(false)
@@ -665,9 +665,8 @@ private fun updateMemberRoleDialog(
fun connectViaMemberAddressAlert(rhId: Long?, connReqUri: String) {
try {
val uri = URI(connReqUri)
withBGApi {
planAndConnect(rhId, uri, incognito = null, close = { ModalManager.closeAllModalsEverywhere() })
planAndConnect(rhId, connReqUri, incognito = null, close = { ModalManager.closeAllModalsEverywhere() })
}
} catch (e: RuntimeException) {
AlertManager.shared.showAlertMsg(
@@ -478,7 +478,7 @@ private fun ToggleFilterEnabledButton() {
@Composable
expect fun ActiveCallInteractiveArea(call: Call)
fun connectIfOpenedViaUri(rhId: Long?, uri: URI, chatModel: ChatModel) {
fun connectIfOpenedViaUri(rhId: Long?, uri: String, chatModel: ChatModel) {
Log.d(TAG, "connectIfOpenedViaUri: opened via link")
if (chatModel.currentUser.value == null) {
chatModel.appOpenUrl.value = rhId to uri
@@ -566,7 +566,7 @@ private fun connect(link: String, searchChatFilteredBySimplexLink: MutableState<
withBGApi {
planAndConnect(
chatModel.remoteHostId(),
URI.create(link),
link,
incognito = null,
filterKnownContact = { searchChatFilteredBySimplexLink.value = it.id },
filterKnownGroup = { searchChatFilteredBySimplexLink.value = it.id },
@@ -480,12 +480,11 @@ inline fun <reified T> serializableSaver(): Saver<T, *> = Saver(
)
fun UriHandler.openVerifiedSimplexUri(uri: String) {
val URI = try { URI.create(uri) } catch (e: Exception) { null }
if (URI != null) {
connectIfOpenedViaUri(chatModel.remoteHostId(), URI, ChatModel)
}
connectIfOpenedViaUri(chatModel.remoteHostId(), uri, ChatModel)
}
fun uriCreateOrNull(uri: String) = try { URI.create(uri) } catch (e: Exception) { null }
fun UriHandler.openUriCatching(uri: String) {
try {
openUri(uri)
@@ -20,7 +20,7 @@ enum class ConnectionLinkType {
suspend fun planAndConnect(
rhId: Long?,
uri: URI,
uri: String,
incognito: Boolean?,
close: (() -> Unit)?,
cleanup: (() -> Unit)? = null,
@@ -29,7 +29,7 @@ suspend fun planAndConnect(
) {
val connectionPlan = chatModel.controller.apiConnectPlan(rhId, uri.toString())
if (connectionPlan != null) {
val link = strHasSingleSimplexLink(uri.toString().trim())
val link = strHasSingleSimplexLink(uri.trim())
val linkText = if (link?.format is Format.SimplexLink)
"<br><br><u>${link.simplexLinkText(link.format.linkType, link.format.smpHosts)}</u>"
else
@@ -323,13 +323,13 @@ suspend fun planAndConnect(
suspend fun connectViaUri(
chatModel: ChatModel,
rhId: Long?,
uri: URI,
uri: String,
incognito: Boolean,
connectionPlan: ConnectionPlan?,
close: (() -> Unit)?,
cleanup: (() -> Unit)?,
) {
val pcc = chatModel.controller.apiConnect(rhId, incognito, uri.toString())
val pcc = chatModel.controller.apiConnect(rhId, incognito, uri)
val connLinkType = if (connectionPlan != null) planToConnectionLinkType(connectionPlan) else ConnectionLinkType.INVITATION
if (pcc != null) {
withChats {
@@ -361,7 +361,7 @@ fun planToConnectionLinkType(connectionPlan: ConnectionPlan): ConnectionLinkType
fun askCurrentOrIncognitoProfileAlert(
chatModel: ChatModel,
rhId: Long?,
uri: URI,
uri: String,
connectionPlan: ConnectionPlan?,
close: (() -> Unit)?,
title: String,
@@ -417,7 +417,7 @@ fun openKnownContact(chatModel: ChatModel, rhId: Long?, close: (() -> Unit)?, co
fun ownGroupLinkConfirmConnect(
chatModel: ChatModel,
rhId: Long?,
uri: URI,
uri: String,
linkText: String,
incognito: Boolean?,
connectionPlan: ConnectionPlan?,
@@ -482,7 +482,7 @@ private fun connect(link: String, searchChatFilteredBySimplexLink: MutableState<
withBGApi {
planAndConnect(
chatModel.remoteHostId(),
URI.create(link),
link,
incognito = null,
filterKnownContact = { searchChatFilteredBySimplexLink.value = it.id },
close = close,
@@ -655,7 +655,7 @@ private suspend fun verify(rhId: Long?, text: String?, close: () -> Unit): Boole
private suspend fun connect(rhId: Long?, link: String, close: () -> Unit, cleanup: (() -> Unit)? = null) {
planAndConnect(
rhId,
URI.create(link),
link,
close = close,
cleanup = cleanup,
incognito = null
@@ -204,7 +204,7 @@ fun startServer(onResponse: (WVAPIMessage) -> Unit): NanoWSD {
return when {
session.headers["upgrade"] == "websocket" -> super.handle(session)
session.uri.contains("/simplex/call/") -> resourcesToResponse("/desktop/call.html")
else -> resourcesToResponse(URI.create(session.uri).path)
else -> resourcesToResponse(uriCreateOrNull(session.uri)?.path ?: return newFixedLengthResponse("Error parsing URL"))
}
}
}