@@ -4,6 +4,7 @@ import android.annotation.SuppressLint
44import android.app.PendingIntent
55import android.content.Context
66import android.content.Intent
7+ import androidx.activity.result.ActivityResultLauncher
78import androidx.core.app.NotificationCompat
89import androidx.core.app.TaskStackBuilder
910import kotlinx.coroutines.suspendCancellableCoroutine
@@ -48,10 +49,10 @@ class UserDecisionRegistry private constructor(
4849 * @param appInForeground whether the app is currently in foreground = whether it can directly launch an Activity
4950 * @return *true* if the user explicitly trusts the certificate, *false* if unknown or untrusted
5051 */
51- suspend fun check (cert : X509Certificate , appInForeground : Boolean ): Boolean = suspendCancellableCoroutine { cont ->
52+ suspend fun check (cert : X509Certificate , launcher : ActivityResultLauncher < Intent > ? ): Boolean = suspendCancellableCoroutine { cont ->
5253 // check whether we're able to retrieve user feedback (= start an Activity and/or show a notification)
5354 val notificationsPermitted = NotificationUtils .notificationsPermitted(context)
54- val userDecisionPossible = appInForeground || notificationsPermitted
55+ val userDecisionPossible = launcher != null || notificationsPermitted
5556
5657 if (userDecisionPossible) {
5758 // User decision possible → remember request in pendingDecisions so that a later decision will be applied to this request
@@ -86,7 +87,7 @@ class UserDecisionRegistry private constructor(
8687 }
8788
8889 if (requestDecision)
89- requestDecision(cert, launchActivity = appInForeground , showNotification = notificationsPermitted)
90+ requestDecision(cert, launcher , showNotification = notificationsPermitted)
9091
9192 } else {
9293 // We're not able to retrieve user feedback, directly reject request
@@ -108,8 +109,8 @@ class UserDecisionRegistry private constructor(
108109 * @throws IllegalArgumentException when both [launchActivity] and [showNotification] are *false*
109110 */
110111 @SuppressLint(" MissingPermission" )
111- internal fun requestDecision (cert : X509Certificate , launchActivity : Boolean , showNotification : Boolean ) {
112- if (! launchActivity && ! showNotification)
112+ internal fun requestDecision (cert : X509Certificate , launcher : ActivityResultLauncher < Intent > ? , showNotification : Boolean ) {
113+ if (launcher == null && ! showNotification)
113114 throw IllegalArgumentException (" User decision requires certificate Activity and/or notification" )
114115
115116 val rawCert = cert.encoded
@@ -147,9 +148,9 @@ class UserDecisionRegistry private constructor(
147148 nm.notify(CertUtils .getTag(cert), NotificationUtils .ID_CERT_DECISION , notify)
148149 }
149150
150- if (launchActivity ) {
151+ if (launcher != null ) {
151152 decisionIntent.addFlags(Intent .FLAG_ACTIVITY_NEW_TASK )
152- context.startActivity (decisionIntent)
153+ launcher.launch (decisionIntent)
153154 }
154155 }
155156
0 commit comments