Skip to content

Commit 9e15892

Browse files
committed
[Android] Add window.open UCP delegation
1 parent 1e68121 commit 9e15892

15 files changed

Lines changed: 471 additions & 407 deletions

File tree

platforms/android/lib/api/lib.api

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,6 @@ public final class com/shopify/checkoutkit/Checkout$Companion {
630630
}
631631

632632
public abstract interface class com/shopify/checkoutkit/CheckoutCommunicationClient {
633-
public abstract fun openExternalUrl (Landroid/net/Uri;)Z
634633
public abstract fun process (Ljava/lang/String;)Ljava/lang/String;
635634
}
636635

@@ -669,7 +668,6 @@ public abstract interface class com/shopify/checkoutkit/CheckoutEventProcessor {
669668
public abstract fun onCheckoutCanceled ()V
670669
public abstract fun onCheckoutCompleted (Lcom/shopify/checkoutkit/lifecycleevents/CheckoutCompletedEvent;)V
671670
public abstract fun onCheckoutFailed (Lcom/shopify/checkoutkit/CheckoutException;)V
672-
public abstract fun onCheckoutLinkClicked (Landroid/net/Uri;)V
673671
public abstract fun onGeolocationPermissionsHidePrompt ()V
674672
public abstract fun onGeolocationPermissionsShowPrompt (Ljava/lang/String;Landroid/webkit/GeolocationPermissions$Callback;)V
675673
public abstract fun onPermissionRequest (Landroid/webkit/PermissionRequest;)V
@@ -773,8 +771,6 @@ public final class com/shopify/checkoutkit/CheckoutProtocol {
773771
public final class com/shopify/checkoutkit/CheckoutProtocol$Client : com/shopify/checkoutkit/CheckoutCommunicationClient {
774772
public fun <init> ()V
775773
public final fun on (Lcom/shopify/checkoutkit/NotificationDescriptor;Lkotlin/jvm/functions/Function1;)Lcom/shopify/checkoutkit/CheckoutProtocol$Client;
776-
public final fun onOpenExternalUrl (Lkotlin/jvm/functions/Function1;)Lcom/shopify/checkoutkit/CheckoutProtocol$Client;
777-
public fun openExternalUrl (Landroid/net/Uri;)Z
778774
public fun process (Ljava/lang/String;)Ljava/lang/String;
779775
}
780776

@@ -1368,10 +1364,7 @@ public final class com/shopify/checkoutkit/CredentialResult$Companion {
13681364
}
13691365

13701366
public abstract class com/shopify/checkoutkit/DefaultCheckoutEventProcessor : com/shopify/checkoutkit/CheckoutEventProcessor {
1371-
public fun <init> (Landroid/content/Context;)V
1372-
public fun <init> (Landroid/content/Context;Lcom/shopify/checkoutkit/LogWrapper;)V
1373-
public synthetic fun <init> (Landroid/content/Context;Lcom/shopify/checkoutkit/LogWrapper;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
1374-
public fun onCheckoutLinkClicked (Landroid/net/Uri;)V
1367+
public fun <init> ()V
13751368
public fun onGeolocationPermissionsHidePrompt ()V
13761369
public fun onGeolocationPermissionsShowPrompt (Ljava/lang/String;Landroid/webkit/GeolocationPermissions$Callback;)V
13771370
public fun onPermissionRequest (Landroid/webkit/PermissionRequest;)V

platforms/android/lib/src/main/java/com/shopify/checkoutkit/CheckoutCommunicationClient.kt

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
*/
2323
package com.shopify.checkoutkit
2424

25-
import android.net.Uri
26-
2725
/**
2826
* Implement this interface to handle Embedded Checkout Protocol (ECP) messages beyond
2927
* the built-in methods handled natively by the SDK.
@@ -34,20 +32,14 @@ public interface CheckoutCommunicationClient {
3432
/**
3533
* Process a JSON-RPC 2.0 ECP message from the checkout web page.
3634
*
37-
* Called for all EC notifications (ec.start, ec.error, ec.complete, ec.*.change)
38-
* and any unknown methods. For requests, return a JSON-RPC 2.0 response string;
39-
* for notifications, return null (no response is sent).
35+
* Called for EC notifications (ec.start, ec.error, ec.complete, ec.*.change) and
36+
* any unknown methods the kit doesn't handle natively. Delegations such as
37+
* `ec.window.open_request` are handled internally by the kit and are not forwarded
38+
* here. For requests, return a JSON-RPC 2.0 response string; for notifications,
39+
* return null (no response is sent).
4040
*
4141
* @param message JSON-RPC 2.0 encoded message string
4242
* @return JSON-RPC 2.0 encoded response string, or null to send no response
4343
*/
4444
public fun process(message: String): String?
45-
46-
/**
47-
* Called when checkout requests that a URL be opened externally (ec.window.open_request).
48-
*
49-
* @param url the URL checkout wants opened in an external browser or app
50-
* @return true if the URL was handled and displayed externally, false otherwise
51-
*/
52-
public fun openExternalUrl(url: Uri): Boolean
5345
}

platforms/android/lib/src/main/java/com/shopify/checkoutkit/CheckoutEventProcessor.kt

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
*/
2323
package com.shopify.checkoutkit
2424

25-
import android.annotation.SuppressLint
26-
import android.content.Context
27-
import android.content.Intent
2825
import android.net.Uri
2926
import android.webkit.GeolocationPermissions
3027
import android.webkit.PermissionRequest
@@ -57,12 +54,6 @@ public interface CheckoutEventProcessor {
5754
*/
5855
public fun onCheckoutCanceled()
5956

60-
/**
61-
* Event indicating that a link has been clicked within checkout that should be opened outside
62-
* of the WebView, e.g. in a system browser or email client. Protocols can be http/https/mailto/tel
63-
*/
64-
public fun onCheckoutLinkClicked(uri: Uri)
65-
6657
/**
6758
* A permission has been requested by the web chrome client, e.g. to access the camera
6859
*/
@@ -103,10 +94,6 @@ internal class NoopEventProcessor : CheckoutEventProcessor {
10394
/* noop */
10495
}
10596

106-
override fun onCheckoutLinkClicked(uri: Uri) {
107-
/* noop */
108-
}
109-
11097
override fun onShowFileChooser(
11198
webView: WebView,
11299
filePathCallback: ValueCallback<Array<Uri>>,
@@ -130,22 +117,9 @@ internal class NoopEventProcessor : CheckoutEventProcessor {
130117

131118
/**
132119
* An abstract class that provides a default implementation of the CheckoutEventProcessor interface
133-
* for handling checkout events and interacting with the Android operating system.
134-
* @param context from which we will launch intents.
120+
* for the optional permission and file-chooser callbacks. Override in subclasses as needed.
135121
*/
136-
public abstract class DefaultCheckoutEventProcessor @JvmOverloads constructor(
137-
private val context: Context,
138-
private val log: LogWrapper = LogWrapper(),
139-
) : CheckoutEventProcessor {
140-
141-
override fun onCheckoutLinkClicked(uri: Uri) {
142-
when (uri.scheme) {
143-
"tel" -> context.launchPhoneApp(uri.schemeSpecificPart)
144-
"mailto" -> context.launchEmailApp(uri.schemeSpecificPart)
145-
"https", "http" -> context.launchBrowser(uri)
146-
else -> context.tryLaunchDeepLink(uri)
147-
}
148-
}
122+
public abstract class DefaultCheckoutEventProcessor : CheckoutEventProcessor {
149123

150124
override fun onPermissionRequest(permissionRequest: PermissionRequest) {
151125
// no-op override to implement
@@ -166,42 +140,4 @@ public abstract class DefaultCheckoutEventProcessor @JvmOverloads constructor(
166140
override fun onGeolocationPermissionsHidePrompt() {
167141
// no-op override to implement
168142
}
169-
170-
private fun Context.launchEmailApp(to: String) {
171-
log.d(LOG_TAG, "Attempting to launch email app.")
172-
val intent = Intent(Intent.ACTION_SEND)
173-
intent.type = "vnd.android.cursor.item/email"
174-
intent.putExtra(Intent.EXTRA_EMAIL, arrayOf(to))
175-
startActivity(intent)
176-
}
177-
178-
private fun Context.launchBrowser(uri: Uri) {
179-
log.d(LOG_TAG, "Attempting to launch browser for $uri.")
180-
val intent = Intent(Intent.ACTION_VIEW)
181-
intent.data = uri
182-
startActivity(intent)
183-
}
184-
185-
private fun Context.launchPhoneApp(phone: String) {
186-
log.d(LOG_TAG, "Attempting to launch phone app.")
187-
val intent = Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phone, null))
188-
startActivity(intent)
189-
}
190-
191-
@SuppressLint("QueryPermissionsNeeded")
192-
private fun Context.tryLaunchDeepLink(uri: Uri) {
193-
log.d(LOG_TAG, "Attempting to launch deep link for uri $uri.")
194-
val intent = Intent(Intent.ACTION_VIEW)
195-
intent.data = uri
196-
if (context.packageManager.queryIntentActivities(intent, 0).isNotEmpty()) {
197-
startActivity(intent)
198-
} else {
199-
log.w(TAG, "Unrecognized scheme for link clicked in checkout '$uri'")
200-
}
201-
}
202-
203-
private companion object {
204-
private const val LOG_TAG = "DefaultCheckoutEventProcessor"
205-
private const val TAG = "DefaultCheckoutEventProcessor"
206-
}
207143
}

0 commit comments

Comments
 (0)