-
Notifications
You must be signed in to change notification settings - Fork 77
v6 - DropInService memory leak #2638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -8,15 +8,10 @@ | |||||
|
|
||||||
| package com.adyen.checkout.dropin.internal.service | ||||||
|
|
||||||
| import android.content.ComponentName | ||||||
| import android.content.Context | ||||||
| import android.content.Intent | ||||||
| import android.content.ServiceConnection | ||||||
| import android.os.IBinder | ||||||
| import com.adyen.checkout.core.action.data.ActionComponentData | ||||||
| import com.adyen.checkout.core.common.AdyenLogLevel | ||||||
| import com.adyen.checkout.core.common.PaymentResult | ||||||
| import com.adyen.checkout.core.common.internal.helper.adyenLog | ||||||
| import com.adyen.checkout.core.components.CheckoutResult | ||||||
| import com.adyen.checkout.core.components.paymentmethod.PaymentComponentState | ||||||
| import com.adyen.checkout.core.error.CheckoutError | ||||||
|
|
@@ -29,55 +24,28 @@ internal class DropInServiceManager( | |||||
| private val serviceClass: Class<out DropInService>, | ||||||
| ) { | ||||||
|
|
||||||
| private var binder: DropInBinder? = null | ||||||
|
|
||||||
| private val _paymentResultFlow = MutableSharedFlow<PaymentResult>() | ||||||
| val paymentResultFlow: SharedFlow<PaymentResult> = _paymentResultFlow.asSharedFlow() | ||||||
|
|
||||||
| private val _errorFlow = MutableSharedFlow<CheckoutError>() | ||||||
| val errorFlow: SharedFlow<CheckoutError> = _errorFlow.asSharedFlow() | ||||||
|
|
||||||
| private val connection = object : ServiceConnection { | ||||||
| override fun onServiceConnected(name: ComponentName?, service: IBinder?) { | ||||||
| val localBinder = service as? DropInBinder | ||||||
| if (localBinder != null) { | ||||||
| binder = localBinder | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| override fun onServiceDisconnected(name: ComponentName?) { | ||||||
| binder = null | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| fun startAndBind(context: Context) { | ||||||
| fun start(context: Context) { | ||||||
| val intent = Intent(context, serviceClass) | ||||||
| context.startService(intent) | ||||||
| context.bindService(intent, connection, Context.BIND_AUTO_CREATE) | ||||||
| } | ||||||
|
|
||||||
| fun unbind(context: Context) { | ||||||
| try { | ||||||
| context.unbindService(connection) | ||||||
| } catch (e: IllegalArgumentException) { | ||||||
| adyenLog(AdyenLogLevel.WARN, e) { "Failed to unbind service" } | ||||||
| } | ||||||
| binder = null | ||||||
| } | ||||||
|
|
||||||
| fun stop(context: Context) { | ||||||
| val intent = Intent(context, serviceClass) | ||||||
| context.stopService(intent) | ||||||
| } | ||||||
|
|
||||||
| // TODO - Binder could be null when service is not connected yet. | ||||||
| // Implement a queue similar in functionality to one in v5 DropInActivity. | ||||||
| suspend fun requestOnSubmit(state: PaymentComponentState<*>): CheckoutResult { | ||||||
| return binder?.requestOnSubmit(state) ?: error("Service is not bound") | ||||||
| return DropInServiceRegistry.get()?.onSubmit(state) ?: error("Service is not available") | ||||||
| } | ||||||
|
|
||||||
| suspend fun requestOnAdditionalDetails(data: ActionComponentData): CheckoutResult { | ||||||
| return binder?.requestOnAdditionalDetails(data) ?: error("Service is not bound") | ||||||
| return DropInServiceRegistry.get()?.onAdditionalDetails(data) ?: error("Service is not available") | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calling
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| suspend fun onPaymentFinished(paymentResult: PaymentResult) { | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,25 @@ | ||||||
| /* | ||||||
| * Copyright (c) 2026 Adyen N.V. | ||||||
| * | ||||||
| * This file is open source and available under the MIT license. See the LICENSE file for more info. | ||||||
| * | ||||||
| * Created by oscars on 16/3/2026. | ||||||
| */ | ||||||
|
|
||||||
| package com.adyen.checkout.dropin.internal.service | ||||||
|
|
||||||
| import com.adyen.checkout.dropin.DropInService | ||||||
|
|
||||||
| internal object DropInServiceRegistry { | ||||||
| private var service: DropInService? = null | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||
|
|
||||||
| fun register(service: DropInService) { | ||||||
| this.service = service | ||||||
| } | ||||||
|
|
||||||
| fun unregister() { | ||||||
| service = null | ||||||
| } | ||||||
|
|
||||||
| fun get(): DropInService? = service | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling
error()will crash the application if the service is not available. This could happen if this method is called before the service is fully initialized. A more robust approach is to return aCheckoutResult.Error. This prevents a crash and allows the UI to handle the error gracefully.