Skip to content

Commit 719caff

Browse files
committed
Don't redownload the certificate when repeat the cert install prompt
1 parent 70aa86a commit 719caff

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

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

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {
562562
// required so this is just reshowing the instructions, or it was automated but that's not
563563
// working for some reason, in which case manual setup is a best-effort fallback.
564564
app.trackEvent("Setup", "cert-install-failed")
565-
launch { promptToManuallyInstallCert(currentProxyConfig!!.certificate) }
565+
launch { promptToManuallyInstallCert(currentProxyConfig!!.certificate, repeatPrompt = true) }
566566
} else {
567567
Sentry.capture("Non-OK result $resultCode for requestCode $requestCode")
568568
mainState = MainState.FAILED
@@ -660,30 +660,33 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {
660660
}
661661

662662
@RequiresApi(Build.VERSION_CODES.Q)
663-
private suspend fun promptToManuallyInstallCert(cert: Certificate) {
664-
// Get ready to save the cert to downloads:
665-
val downloadsUri = MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
666-
667-
val contentDetails = ContentValues().apply {
668-
put(MediaStore.Downloads.DISPLAY_NAME, "HTTP Toolkit Certificate.crt")
669-
put(MediaStore.Downloads.MIME_TYPE, "application/x-x509-ca-cert")
670-
put(MediaStore.Downloads.IS_PENDING, 1)
671-
}
663+
private suspend fun promptToManuallyInstallCert(cert: Certificate, repeatPrompt: Boolean = false) {
664+
if (!repeatPrompt) {
665+
// Get ready to save the cert to downloads:
666+
val downloadsUri =
667+
MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
668+
669+
val contentDetails = ContentValues().apply {
670+
put(MediaStore.Downloads.DISPLAY_NAME, "HTTP Toolkit Certificate.crt")
671+
put(MediaStore.Downloads.MIME_TYPE, "application/x-x509-ca-cert")
672+
put(MediaStore.Downloads.IS_PENDING, 1)
673+
}
672674

673-
val certUri = contentResolver.insert(downloadsUri, contentDetails)
674-
?: throw RuntimeException("Could not get download cert URI")
675+
val certUri = contentResolver.insert(downloadsUri, contentDetails)
676+
?: throw RuntimeException("Could not get download cert URI")
675677

676-
// Write cert contents to a file:
677-
withContext(Dispatchers.IO) {
678-
contentResolver.openFileDescriptor(certUri, "w", null).use { f ->
679-
ParcelFileDescriptor.AutoCloseOutputStream(f).write(cert.encoded)
678+
// Write cert contents to a file:
679+
withContext(Dispatchers.IO) {
680+
contentResolver.openFileDescriptor(certUri, "w", null).use { f ->
681+
ParcelFileDescriptor.AutoCloseOutputStream(f).write(cert.encoded)
682+
}
680683
}
681-
}
682684

683-
// All done, mark it as such:
684-
contentDetails.clear()
685-
contentDetails.put(MediaStore.Downloads.IS_PENDING, 0)
686-
contentResolver.update(certUri, contentDetails, null, null)
685+
// All done, mark it as such:
686+
contentDetails.clear()
687+
contentDetails.put(MediaStore.Downloads.IS_PENDING, 0)
688+
contentResolver.update(certUri, contentDetails, null, null)
689+
}
687690

688691
withContext(Dispatchers.Main) {
689692
MaterialAlertDialogBuilder(this@MainActivity)

0 commit comments

Comments
 (0)