mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-16 01:07:02 +05:30
common: catch errors when opening URLs
This commit is contained in:
parent
944b220905
commit
7604b776b3
2 changed files with 16 additions and 2 deletions
|
@ -1,7 +1,10 @@
|
||||||
package dev.msfjarvis.claw.common.urllauncher
|
package dev.msfjarvis.claw.common.urllauncher
|
||||||
|
|
||||||
|
import android.content.ActivityNotFoundException
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
|
import android.util.Log
|
||||||
|
import android.widget.Toast
|
||||||
import androidx.browser.customtabs.CustomTabsIntent
|
import androidx.browser.customtabs.CustomTabsIntent
|
||||||
import androidx.compose.ui.platform.UriHandler
|
import androidx.compose.ui.platform.UriHandler
|
||||||
|
|
||||||
|
@ -13,6 +16,12 @@ class UrlLauncher(private val context: Context) : UriHandler {
|
||||||
.setShowTitle(true)
|
.setShowTitle(true)
|
||||||
.setColorScheme(CustomTabsIntent.COLOR_SCHEME_DARK)
|
.setColorScheme(CustomTabsIntent.COLOR_SCHEME_DARK)
|
||||||
.build()
|
.build()
|
||||||
customTabsIntent.launchUrl(context, Uri.parse(uri))
|
try {
|
||||||
|
customTabsIntent.launchUrl(context, Uri.parse(uri))
|
||||||
|
} catch (e: ActivityNotFoundException) {
|
||||||
|
val error = "Failed to open URL: $uri"
|
||||||
|
Log.d("UrlLauncher", error)
|
||||||
|
Toast.makeText(context, error, Toast.LENGTH_SHORT).show()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package dev.msfjarvis.claw.common.urllauncher
|
||||||
|
|
||||||
import androidx.compose.ui.platform.UriHandler
|
import androidx.compose.ui.platform.UriHandler
|
||||||
import java.awt.Desktop
|
import java.awt.Desktop
|
||||||
|
import java.io.IOException
|
||||||
import java.net.URI
|
import java.net.URI
|
||||||
|
|
||||||
class UrlLauncher : UriHandler {
|
class UrlLauncher : UriHandler {
|
||||||
|
@ -9,7 +10,11 @@ class UrlLauncher : UriHandler {
|
||||||
if (Desktop.isDesktopSupported()) {
|
if (Desktop.isDesktopSupported()) {
|
||||||
val desktop = Desktop.getDesktop()
|
val desktop = Desktop.getDesktop()
|
||||||
if (desktop.isSupported(Desktop.Action.BROWSE)) {
|
if (desktop.isSupported(Desktop.Action.BROWSE)) {
|
||||||
desktop.browse(URI(uri))
|
try {
|
||||||
|
desktop.browse(URI(uri))
|
||||||
|
} catch (e: IOException) {
|
||||||
|
println("Failed to open URL: $uri")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue