@@ -425,10 +425,7 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {
425
425
426
426
private fun openDocs () {
427
427
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" )
432
429
}
433
430
434
431
private fun chooseApps (){
@@ -455,31 +452,30 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {
455
452
getTestBrowserPackage(this )
456
453
}
457
454
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
+ }
470
459
460
+ private fun launchBrowser (uri : String , canUseHttps : Boolean = true, browserPackage : String? = null) {
471
461
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
+ )
473
472
} catch (e: ActivityNotFoundException ) {
474
- if (browserIntent.`package` != null ) {
473
+ if (browserPackage != null ) {
475
474
// If we tried a specific package, and it failed, try again with the simplest
476
475
// 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 )
481
477
} else {
482
- throw e
478
+ showNoBrowserAlert(uri)
483
479
}
484
480
}
485
481
}
@@ -722,6 +718,20 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {
722
718
.show()
723
719
}
724
720
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
+
725
735
private fun tryStartActivity (intent : Intent ): Boolean {
726
736
return try {
727
737
startActivity(intent)
0 commit comments