Skip to content

Open-source Android PDF and document toolkit with PDF tools, Office viewing, XLSX editing, scanning, and image editing. Built with Kotlin and Jetpack Compose.

Let users open web addresses from QR scan results

Kzyube Napoles76582bc57e1d
diff --git a/android/app/src/main/java/com/chethan616/clearpdf/ui/home/CreateDock.kt b/android/app/src/main/java/com/chethan616/clearpdf/ui/home/CreateDock.kt
index 218c3f7..f16fc40 100644
--- a/android/app/src/main/java/com/chethan616/clearpdf/ui/home/CreateDock.kt
+++ b/android/app/src/main/java/com/chethan616/clearpdf/ui/home/CreateDock.kt
@@ -1,10 +1,13 @@
package com.chethan616.clearpdf.ui.home
+import android.content.Intent
+import android.net.Uri
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.runtime.saveable.rememberSaveable
+import com.chethan616.clearpdf.ui.glass.GlassPill
import com.chethan616.clearpdf.ui.glass.GlassDialog
import com.journeyapps.barcodescanner.ScanContract
import com.journeyapps.barcodescanner.ScanOptions
@@ -262,6 +265,14 @@ fun BoxScope.CreateDock(
SelectionContainer {
Text(result, color = c.ink, style = ClearType.Body)
}
+ remember(result) { scannedWebLink(result) }?.let { address ->
+ Spacer(Modifier.height(12.dp))
+ GlassPill(onClick = {
+ vm.intents.trySend(Intent(Intent.ACTION_VIEW, Uri.parse(address).normalizeScheme()))
+ }) {
+ Text(stringResource(R.string.sheet_open_link), color = c.ink, style = ClearType.BodyStrong)
+ }
+ }
}
}
diff --git a/android/app/src/main/java/com/chethan616/clearpdf/ui/home/ScannedWebLink.kt b/android/app/src/main/java/com/chethan616/clearpdf/ui/home/ScannedWebLink.kt
new file mode 100644
index 0000000..576e1e8
--- /dev/null
+++ b/android/app/src/main/java/com/chethan616/clearpdf/ui/home/ScannedWebLink.kt
@@ -0,0 +1,26 @@
+package com.chethan616.clearpdf.ui.home
+
+import java.net.IDN
+import java.net.URI
+import java.net.URISyntaxException
+
+/** Return a web address only when the entire QR payload is one absolute HTTP(S) URL. */
+internal fun scannedWebLink(payload: String): String? {
+ return try {
+ val uri = URI(payload)
+ if (!uri.scheme.equals("https", true) && !uri.scheme.equals("http", true)) return null
+ val authority = uri.rawAuthority ?: return null
+ // URI accepts Unicode paths, but expects international domain names in ASCII form.
+ val address = if (uri.host == null && authority.any { it.code > 127 }) {
+ val host = authority.substringBefore(':')
+ val ascii = IDN.toASCII(host, IDN.USE_STD3_ASCII_RULES)
+ payload.replaceFirst("://$host", "://$ascii")
+ } else payload
+ val server = URI(address).parseServerAuthority()
+ address.takeIf { server.host != null && server.port <= 65535 }
+ } catch (_: URISyntaxException) {
+ null
+ } catch (_: IllegalArgumentException) {
+ null
+ }
+}
diff --git a/android/app/src/test/java/com/chethan616/clearpdf/ui/home/ScannedWebLinkTest.kt b/android/app/src/test/java/com/chethan616/clearpdf/ui/home/ScannedWebLinkTest.kt
new file mode 100644
index 0000000..b129e43
--- /dev/null
+++ b/android/app/src/test/java/com/chethan616/clearpdf/ui/home/ScannedWebLinkTest.kt
@@ -0,0 +1,21 @@
+package com.chethan616.clearpdf.ui.home
+
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertNull
+import org.junit.Test
+
+class ScannedWebLinkTest {
+ @Test fun acceptsWholeWebAddresses() {
+ for (url in listOf("https://example.org/file.pdf?q=a%20b#page=2", "HTTP://localhost:8080/", "https://[::1]/")) {
+ assertEquals(url, scannedWebLink(url))
+ }
+ assertEquals("https://xn--bcher-kva.example:8080/č", scannedWebLink("https://bücher.example:8080/č"))
+ }
+
+ @Test fun leavesOtherPayloadsAsText() {
+ for (value in listOf("Meeting: https://example.org", "WIFI:T:WPA;S:Office;;", "intent://scan", "javascript:alert(1)",
+ "mailto:[email protected]", "https:///path", "example.org", "https://example.org:99999", "https://example.org\nextra")) {
+ assertNull(value, scannedWebLink(value))
+ }
+ }
+}