Skip to content

Commit 161559b

Browse files
committed
Show a friendly message if no browser is available
1 parent a167212 commit 161559b

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

app/src/main/java/tech/httptoolkit/android/MainActivity.kt

+33-23
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,7 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {
425425

426426
private fun openDocs() {
427427
app.trackEvent("Button", "open-docs")
428-
startActivity(Intent(
429-
Intent.ACTION_VIEW,
430-
Uri.parse("https://httptoolkit.tech/docs/guides/android")
431-
))
428+
launchBrowser("httptoolkit.tech/docs/guides/android")
432429
}
433430

434431
private fun chooseApps(){
@@ -455,31 +452,30 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {
455452
getTestBrowserPackage(this)
456453
}
457454

458-
val browserIntent = Intent(
459-
Intent.ACTION_VIEW,
460-
Uri.parse(
461-
(
462-
// We test with just plain HTTP if we don't have system injection setup and
463-
// there's no user-cert-trusting browser, to reduce confusing failures.
464-
if (testBrowser == null && !certIsSystemTrusted) "http" else "https"
465-
) + "://amiusing.httptoolkit.tech"
466-
)
467-
).apply {
468-
if (testBrowser != null) setPackage(testBrowser)
469-
}
455+
val canUseHttps = testBrowser != null || certIsSystemTrusted
456+
457+
launchBrowser("amiusing.httptoolkit.tech", canUseHttps, testBrowser)
458+
}
470459

460+
private fun launchBrowser(uri: String, canUseHttps: Boolean = true, browserPackage: String? = null) {
471461
try {
472-
startActivity(browserIntent)
462+
startActivity(
463+
Intent(
464+
Intent.ACTION_VIEW,
465+
Uri.parse((
466+
if (canUseHttps) "https" else "http"
467+
) + "://" + uri)
468+
).apply {
469+
if (browserPackage != null) setPackage(browserPackage)
470+
}
471+
)
473472
} catch (e: ActivityNotFoundException) {
474-
if (browserIntent.`package` != null) {
473+
if (browserPackage != null) {
475474
// If we tried a specific package, and it failed, try again with the simplest
476475
// plain HTTP catch-all VIEW intent, and hope something somewhere can handle it.
477-
startActivity(Intent(
478-
Intent.ACTION_VIEW,
479-
Uri.parse("http://amiusing.httptoolkit.tech")
480-
))
476+
launchBrowser(uri, false)
481477
} else {
482-
throw e
478+
showNoBrowserAlert(uri)
483479
}
484480
}
485481
}
@@ -722,6 +718,20 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {
722718
.show()
723719
}
724720

721+
private fun showNoBrowserAlert(uri: String) {
722+
MaterialAlertDialogBuilder(this)
723+
.setTitle("No browser available")
724+
.setIcon(R.drawable.ic_exclamation_triangle)
725+
.setMessage(
726+
"HTTP Toolkit could not open a browser on this device. " +
727+
"This usually means you don't have any browser installed. To visit " +
728+
uri +
729+
" please install a browser app."
730+
)
731+
.setNeutralButton("OK") { _, _ -> }
732+
.show()
733+
}
734+
725735
private fun tryStartActivity(intent: Intent): Boolean {
726736
return try {
727737
startActivity(intent)

0 commit comments

Comments
 (0)