@@ -37,99 +37,111 @@ import com.shopify.checkoutsheetkit.lifecycleevents.CheckoutCompletedEvent
3737import com.shopify.checkoutsheetkit.pixelevents.PixelEvent
3838
3939/* *
40- * Interface to implement to allow responding to lifecycle events in checkout.
41- * We'd strongly recommend extending DefaultCheckoutEventProcessor where possible
40+ * Interface to implement to allow responding to lifecycle events in checkout. We'd strongly
41+ * recommend extending DefaultCheckoutEventProcessor where possible
4242 */
4343public interface CheckoutEventProcessor {
44- /* *
45- * Event representing the successful completion of a checkout.
46- */
44+ /* * Event representing the successful completion of a checkout. */
4745 public fun onCheckoutCompleted (checkoutCompletedEvent : CheckoutCompletedEvent )
4846
4947 /* *
50- * Event representing an error that occurred during checkout. This can be used to display
51- * error messages for example.
48+ * Event representing an error that occurred during checkout. This can be used to display error
49+ * messages for example.
5250 *
53- * @param error - the CheckoutErrorException that occurred
51+ * @param error
52+ * - the CheckoutErrorException that occurred
5453 * @see Exception
5554 */
5655 public fun onCheckoutFailed (error : CheckoutException )
5756
58- /* *
59- * Event representing the cancellation/closing of checkout by the buyer
60- */
57+ /* * Event representing the cancellation/closing of checkout by the buyer */
6158 public fun onCheckoutCanceled ()
6259
6360 /* *
6461 * Event indicating that a link has been clicked within checkout that should be opened outside
65- * of the WebView, e.g. in a system browser or email client. Protocols can be http/https/mailto/tel
62+ * of the WebView, e.g. in a system browser or email client. Protocols can be
63+ * http/https/mailto/tel
6664 */
6765 public fun onCheckoutLinkClicked (uri : Uri )
6866
69- /* *
70- * A permission has been requested by the web chrome client, e.g. to access the camera
71- */
67+ /* * A permission has been requested by the web chrome client, e.g. to access the camera */
7268 public fun onPermissionRequest (permissionRequest : PermissionRequest )
7369
7470 /* *
75- * Web Pixel event emitted from checkout, that can be optionally transformed, enhanced (e.g. with user and session identifiers),
76- * and processed
71+ * Web Pixel event emitted from checkout, that can be optionally transformed, enhanced (e.g.
72+ * with user and session identifiers), and processed
7773 */
7874 public fun onWebPixelEvent (event : PixelEvent )
7975
8076 /* *
81- * Called when the client should show a file chooser. This is called to handle HTML forms with 'file' input type, in response to the
82- * user pressing the "Select File" button. To cancel the request, call filePathCallback.onReceiveValue(null) and return true.
77+ * Called when the client should show a file chooser. This is called to handle HTML forms with
78+ * 'file' input type, in response to the user pressing the "Select File" button. To cancel the
79+ * request, call filePathCallback.onReceiveValue(null) and return true.
8380 */
8481 public fun onShowFileChooser (
85- webView : WebView ,
86- filePathCallback : ValueCallback <Array <Uri >>,
87- fileChooserParams : WebChromeClient .FileChooserParams ,
82+ webView : WebView ,
83+ filePathCallback : ValueCallback <Array <Uri >>,
84+ fileChooserParams : WebChromeClient .FileChooserParams ,
8885 ): Boolean
8986
9087 /* *
91- * Called when the client should show a location permissions prompt. For example when using 'Use my location' for
92- * pickup points in checkout
88+ * Called when the client should show a location permissions prompt. For example when using 'Use
89+ * my location' for pickup points in checkout
9390 */
94- public fun onGeolocationPermissionsShowPrompt (origin : String , callback : GeolocationPermissions .Callback )
91+ public fun onGeolocationPermissionsShowPrompt (
92+ origin : String ,
93+ callback : GeolocationPermissions .Callback
94+ )
9595
9696 /* *
97- * Called when the client should hide the location permissions prompt, e.g. if th request is cancelled
97+ * Called when the client should hide the location permissions prompt, e.g. if th request is
98+ * cancelled
9899 */
99100 public fun onGeolocationPermissionsHidePrompt ()
100101}
101102
102103internal class NoopEventProcessor : CheckoutEventProcessor {
103- override fun onCheckoutCompleted (checkoutCompletedEvent : CheckoutCompletedEvent ) {/* noop */
104+ override fun onCheckoutCompleted (checkoutCompletedEvent : CheckoutCompletedEvent ) {
105+ /* noop */
104106 }
105107
106- override fun onCheckoutFailed (error : CheckoutException ) {/* noop */
108+ override fun onCheckoutFailed (error : CheckoutException ) {
109+ /* noop */
107110 }
108111
109- override fun onCheckoutCanceled () {/* noop */
112+ override fun onCheckoutCanceled () {
113+ /* noop */
110114 }
111115
112- override fun onCheckoutLinkClicked (uri : Uri ) {/* noop */
116+ override fun onCheckoutLinkClicked (uri : Uri ) {
117+ /* noop */
113118 }
114119
115- override fun onWebPixelEvent (event : PixelEvent ) {/* noop */
120+ override fun onWebPixelEvent (event : PixelEvent ) {
121+ /* noop */
116122 }
117123
118124 override fun onShowFileChooser (
119- webView : WebView ,
120- filePathCallback : ValueCallback <Array <Uri >>,
121- fileChooserParams : WebChromeClient .FileChooserParams ,
125+ webView : WebView ,
126+ filePathCallback : ValueCallback <Array <Uri >>,
127+ fileChooserParams : WebChromeClient .FileChooserParams ,
122128 ): Boolean {
123129 return false
124130 }
125131
126- override fun onPermissionRequest (permissionRequest : PermissionRequest ) {/* noop */
132+ override fun onPermissionRequest (permissionRequest : PermissionRequest ) {
133+ /* noop */
127134 }
128135
129- override fun onGeolocationPermissionsShowPrompt (origin : String , callback : GeolocationPermissions .Callback ) {/* noop */
136+ override fun onGeolocationPermissionsShowPrompt (
137+ origin : String ,
138+ callback : GeolocationPermissions .Callback
139+ ) {
140+ /* noop */
130141 }
131142
132- override fun onGeolocationPermissionsHidePrompt () {/* noop */
143+ override fun onGeolocationPermissionsHidePrompt () {
144+ /* noop */
133145 }
134146}
135147
@@ -138,15 +150,18 @@ internal class NoopEventProcessor : CheckoutEventProcessor {
138150 * for handling checkout events and interacting with the Android operating system.
139151 * @param context from which we will launch intents.
140152 */
141- public abstract class DefaultCheckoutEventProcessor @JvmOverloads constructor(
142- private val context : Context ,
143- private val log : LogWrapper = LogWrapper (),
153+ public abstract class DefaultCheckoutEventProcessor
154+ @JvmOverloads
155+ constructor (
156+ private val context: Context ,
157+ private val log: LogWrapper = LogWrapper (),
144158) : CheckoutEventProcessor {
145159
146- private val LOCATION_PERMISSIONS : Array <String > = arrayOf(
147- Manifest .permission.ACCESS_FINE_LOCATION ,
148- Manifest .permission.ACCESS_COARSE_LOCATION
149- )
160+ private val locationPermissions: Array <String > =
161+ arrayOf(
162+ Manifest .permission.ACCESS_FINE_LOCATION ,
163+ Manifest .permission.ACCESS_COARSE_LOCATION
164+ )
150165
151166 override fun onCheckoutLinkClicked (uri : Uri ) {
152167 when (uri.scheme) {
@@ -166,48 +181,53 @@ public abstract class DefaultCheckoutEventProcessor @JvmOverloads constructor(
166181 }
167182
168183 override fun onShowFileChooser (
169- webView : WebView ,
170- filePathCallback : ValueCallback <Array <Uri >>,
171- fileChooserParams : WebChromeClient .FileChooserParams ,
184+ webView : WebView ,
185+ filePathCallback : ValueCallback <Array <Uri >>,
186+ fileChooserParams : WebChromeClient .FileChooserParams ,
172187 ): Boolean {
173188 return false
174189 }
175190
176191 /* *
177- * Called when the webview requests location permissions. For example when using 'Use my location' to locate pickup points.
178- * The default implementation here will check for both manifest and runtime permissions. If both have been granted,
179- * permission will be granted to present the location prompt to the user.
192+ * Called when the webview requests location permissions. For example when using 'Use my
193+ * location' to locate pickup points. The default implementation here will check for both
194+ * manifest and runtime permissions. If both have been granted, permission will be granted to
195+ * present the location prompt to the user.
180196 *
181- * Runtime permissions must be requested by your host app. The Checkout Sheet kit cannot request them on your behalf.
197+ * Runtime permissions must be requested by your host app. The Checkout Sheet kit cannot request
198+ * them on your behalf.
182199 */
183200 override fun onGeolocationPermissionsShowPrompt (
184- origin : String ,
185- callback : GeolocationPermissions .Callback
201+ origin : String ,
202+ callback : GeolocationPermissions .Callback
186203 ) {
187204 // Check manifest permissions
188- val manifestPermissions = try {
189- context.packageManager
190- .getPackageInfo(context.packageName, PackageManager .GET_PERMISSIONS )
191- .requestedPermissions
192- ?.toSet() ? : emptySet()
193- } catch (e: Exception ) {
194- emptySet()
195- }
205+ val manifestPermissions =
206+ try {
207+ context.packageManager
208+ .getPackageInfo(context.packageName, PackageManager .GET_PERMISSIONS )
209+ .requestedPermissions
210+ ?.toSet()
211+ ? : emptySet()
212+ } catch (e: Exception ) {
213+ log.e(TAG , " Failed to get package permissions" , e)
214+ emptySet()
215+ }
196216
197217 // Check if either permission is declared in manifest
198- val hasManifestPermission = LOCATION_PERMISSIONS .any { permission ->
199- manifestPermissions.contains(permission)
200- }
218+ val hasManifestPermission =
219+ locationPermissions.any { permission -> manifestPermissions.contains(permission) }
201220
202221 if (! hasManifestPermission) {
203222 callback.invoke(origin, false , false )
204223 return
205224 }
206225
207226 // Check runtime permissions
208- val hasRuntimePermission = LOCATION_PERMISSIONS .any { permission ->
209- context.checkSelfPermission(permission) == PackageManager .PERMISSION_GRANTED
210- }
227+ val hasRuntimePermission =
228+ locationPermissions.any { permission ->
229+ context.checkSelfPermission(permission) == PackageManager .PERMISSION_GRANTED
230+ }
211231
212232 callback.invoke(origin, hasRuntimePermission, hasRuntimePermission)
213233 }
0 commit comments