From 2adf13f1442ab1597dc3b61a17e7af9b7ec7a2e3 Mon Sep 17 00:00:00 2001 From: Rehan Date: Mon, 7 Oct 2024 14:43:10 +0500 Subject: [PATCH 1/2] chore: ensure Android component is initialized from services --- .../java/io/customer/commontest/core/BaseTest.kt | 4 ++-- core/api/core.api | 3 +-- .../main/kotlin/io/customer/sdk/core/di/DiGraph.kt | 14 -------------- .../io/customer/sdk/core/di/SDKComponentExt.kt | 4 +++- .../kotlin/io/customer/sdk/CustomerIOBuilder.kt | 4 ++-- .../CustomerIOCloudMessagingReceiver.kt | 3 ++- .../CustomerIOFirebaseMessagingService.kt | 4 +++- .../activity/NotificationClickReceiverActivity.kt | 2 ++ 8 files changed, 15 insertions(+), 23 deletions(-) diff --git a/common-test/src/main/java/io/customer/commontest/core/BaseTest.kt b/common-test/src/main/java/io/customer/commontest/core/BaseTest.kt index eea2596f0..972bc3ded 100644 --- a/common-test/src/main/java/io/customer/commontest/core/BaseTest.kt +++ b/common-test/src/main/java/io/customer/commontest/core/BaseTest.kt @@ -7,7 +7,7 @@ import io.customer.commontest.config.configureAndroidSDKComponent import io.customer.commontest.config.configureSDKComponent import io.customer.commontest.config.testConfigurationDefault import io.customer.sdk.core.di.SDKComponent -import io.customer.sdk.core.di.registerAndroidSDKComponent +import io.customer.sdk.core.di.ensureAndroidComponent import io.mockk.clearAllMocks /** @@ -43,6 +43,6 @@ abstract class BaseTest { private fun registerAndroidSDKComponent(testConfig: TestConfig) { val application = testConfig.argumentOrNull()?.value ?: return - testConfig.configureAndroidSDKComponent(SDKComponent.registerAndroidSDKComponent(application)) + testConfig.configureAndroidSDKComponent(SDKComponent.ensureAndroidComponent(application)) } } diff --git a/core/api/core.api b/core/api/core.api index ee0836bd4..d79185a56 100644 --- a/core/api/core.api +++ b/core/api/core.api @@ -137,7 +137,6 @@ public abstract class io/customer/sdk/core/di/DiGraph { public fun ()V public final fun getOverrides ()Ljava/util/concurrent/ConcurrentHashMap; public final fun getSingletons ()Ljava/util/concurrent/ConcurrentHashMap; - public final fun overrideDependency (Ljava/lang/Class;Ljava/lang/Object;)V public fun reset ()V } @@ -155,7 +154,7 @@ public final class io/customer/sdk/core/di/SDKComponent : io/customer/sdk/core/d } public final class io/customer/sdk/core/di/SDKComponentExtKt { - public static final fun registerAndroidSDKComponent (Lio/customer/sdk/core/di/SDKComponent;Landroid/content/Context;)Lio/customer/sdk/core/di/AndroidSDKComponent; + public static final fun ensureAndroidComponent (Lio/customer/sdk/core/di/SDKComponent;Landroid/content/Context;)Lio/customer/sdk/core/di/AndroidSDKComponent; } public abstract interface class io/customer/sdk/core/environment/BuildEnvironment { diff --git a/core/src/main/kotlin/io/customer/sdk/core/di/DiGraph.kt b/core/src/main/kotlin/io/customer/sdk/core/di/DiGraph.kt index b16a766ef..7a243dcbb 100644 --- a/core/src/main/kotlin/io/customer/sdk/core/di/DiGraph.kt +++ b/core/src/main/kotlin/io/customer/sdk/core/di/DiGraph.kt @@ -133,18 +133,4 @@ abstract class DiGraph { overrides.clear() singletons.clear() } - - // TODO: Remove deprecated functions after all usages are removed. - @Deprecated("Use overrideDependency instead", ReplaceWith("overrideDependency(value)")) - fun overrideDependency(dependency: Class, value: Dependency) { - overrides[dependency.name] = value as Any - } - - @Deprecated("Use newInstance or singleton instead", ReplaceWith("newInstance()")) - inline fun override(): DEP? = overrides[dependencyKey(identifier = null)] as? DEP - - @Deprecated("Use singleton instead", ReplaceWith("singleton(newInstanceCreator)")) - inline fun getSingletonInstanceCreate(newInstanceCreator: () -> INST): INST { - return getOrCreateSingletonInstance(identifier = null, newInstanceCreator = newInstanceCreator) - } } diff --git a/core/src/main/kotlin/io/customer/sdk/core/di/SDKComponentExt.kt b/core/src/main/kotlin/io/customer/sdk/core/di/SDKComponentExt.kt index 1ff39d618..31ff35af6 100644 --- a/core/src/main/kotlin/io/customer/sdk/core/di/SDKComponentExt.kt +++ b/core/src/main/kotlin/io/customer/sdk/core/di/SDKComponentExt.kt @@ -9,8 +9,10 @@ import android.content.Context /** * Create and register an instance of AndroidSDKComponent with the provided context, * only if it is not already initialized. + * This function should be called from all entry points of the SDK to ensure that + * AndroidSDKComponent is initialized before accessing any of its dependencies. */ -fun SDKComponent.registerAndroidSDKComponent( +fun SDKComponent.ensureAndroidComponent( context: Context ) = registerDependency { AndroidSDKComponentImpl(context) diff --git a/datapipelines/src/main/kotlin/io/customer/sdk/CustomerIOBuilder.kt b/datapipelines/src/main/kotlin/io/customer/sdk/CustomerIOBuilder.kt index 7da48bfd2..6e915105d 100644 --- a/datapipelines/src/main/kotlin/io/customer/sdk/CustomerIOBuilder.kt +++ b/datapipelines/src/main/kotlin/io/customer/sdk/CustomerIOBuilder.kt @@ -4,7 +4,7 @@ import android.app.Application import com.segment.analytics.kotlin.core.platform.policies.FlushPolicy import io.customer.datapipelines.config.DataPipelinesModuleConfig import io.customer.sdk.core.di.SDKComponent -import io.customer.sdk.core.di.registerAndroidSDKComponent +import io.customer.sdk.core.di.ensureAndroidComponent import io.customer.sdk.core.module.CustomerIOModule import io.customer.sdk.core.module.CustomerIOModuleConfig import io.customer.sdk.core.util.CioLogLevel @@ -33,7 +33,7 @@ class CustomerIOBuilder( // Initialize AndroidSDKComponent as soon as the builder is created so that // it can be used by the modules. // Also, it is needed to override test dependencies in the test environment - private val androidSDKComponent = SDKComponent.registerAndroidSDKComponent( + private val androidSDKComponent = SDKComponent.ensureAndroidComponent( context = applicationContext ) diff --git a/messagingpush/src/main/java/io/customer/messagingpush/CustomerIOCloudMessagingReceiver.kt b/messagingpush/src/main/java/io/customer/messagingpush/CustomerIOCloudMessagingReceiver.kt index 8199ad7c8..330ee6c56 100644 --- a/messagingpush/src/main/java/io/customer/messagingpush/CustomerIOCloudMessagingReceiver.kt +++ b/messagingpush/src/main/java/io/customer/messagingpush/CustomerIOCloudMessagingReceiver.kt @@ -7,6 +7,7 @@ import com.google.android.gms.cloudmessaging.CloudMessagingReceiver import io.customer.messagingpush.di.pushMessageProcessor import io.customer.messagingpush.processor.PushMessageProcessor import io.customer.sdk.core.di.SDKComponent +import io.customer.sdk.core.di.ensureAndroidComponent /** * Broadcast receiver for listening to push events from GoogleCloudMessaging (GCM). @@ -26,7 +27,7 @@ class CustomerIOCloudMessagingReceiver : BroadcastReceiver() { val extras = intent.extras // Ignore event if no data was received in extras if (extras == null || extras.isEmpty) return - // TODO: Make sure PushMessageProcessor works as expected even if the SDK is not initialized + SDKComponent.ensureAndroidComponent(context = context) SDKComponent.pushMessageProcessor.processGCMMessageIntent(intent = intent) } } diff --git a/messagingpush/src/main/java/io/customer/messagingpush/CustomerIOFirebaseMessagingService.kt b/messagingpush/src/main/java/io/customer/messagingpush/CustomerIOFirebaseMessagingService.kt index ea9154185..9b1078886 100644 --- a/messagingpush/src/main/java/io/customer/messagingpush/CustomerIOFirebaseMessagingService.kt +++ b/messagingpush/src/main/java/io/customer/messagingpush/CustomerIOFirebaseMessagingService.kt @@ -6,6 +6,7 @@ import com.google.firebase.messaging.RemoteMessage import io.customer.messagingpush.di.pushMessageProcessor import io.customer.sdk.communication.Event import io.customer.sdk.core.di.SDKComponent +import io.customer.sdk.core.di.ensureAndroidComponent open class CustomerIOFirebaseMessagingService : FirebaseMessagingService() { @@ -47,6 +48,7 @@ open class CustomerIOFirebaseMessagingService : FirebaseMessagingService() { } private fun handleNewToken(context: Context, token: String) { + SDKComponent.ensureAndroidComponent(context = context) eventBus.publish( Event.RegisterDeviceTokenEvent(token) ) @@ -57,7 +59,7 @@ open class CustomerIOFirebaseMessagingService : FirebaseMessagingService() { remoteMessage: RemoteMessage, handleNotificationTrigger: Boolean = true ): Boolean { - // TODO: Make sure PushNotificationHandler works as expected even if the SDK is not initialized + SDKComponent.ensureAndroidComponent(context = context) val handler = CustomerIOPushNotificationHandler( pushMessageProcessor = SDKComponent.pushMessageProcessor, remoteMessage = remoteMessage diff --git a/messagingpush/src/main/java/io/customer/messagingpush/activity/NotificationClickReceiverActivity.kt b/messagingpush/src/main/java/io/customer/messagingpush/activity/NotificationClickReceiverActivity.kt index c40e781ff..6a6d2f51c 100644 --- a/messagingpush/src/main/java/io/customer/messagingpush/activity/NotificationClickReceiverActivity.kt +++ b/messagingpush/src/main/java/io/customer/messagingpush/activity/NotificationClickReceiverActivity.kt @@ -5,6 +5,7 @@ import android.content.Intent import android.os.Bundle import io.customer.messagingpush.di.pushMessageProcessor import io.customer.sdk.core.di.SDKComponent +import io.customer.sdk.core.di.ensureAndroidComponent import io.customer.sdk.tracking.TrackableScreen /** @@ -24,6 +25,7 @@ class NotificationClickReceiverActivity : Activity(), TrackableScreen { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + SDKComponent.ensureAndroidComponent(context = this) handleIntent(data = intent) } From a8d7405b44bc974e44514bdfa546126c801c0fe0 Mon Sep 17 00:00:00 2001 From: Rehan Date: Mon, 7 Oct 2024 16:38:53 +0500 Subject: [PATCH 2/2] renamed method --- .../src/main/java/io/customer/commontest/core/BaseTest.kt | 4 ++-- core/api/core.api | 2 +- .../main/kotlin/io/customer/sdk/core/di/SDKComponentExt.kt | 2 +- .../src/main/kotlin/io/customer/sdk/CustomerIOBuilder.kt | 4 ++-- .../messagingpush/CustomerIOCloudMessagingReceiver.kt | 4 ++-- .../messagingpush/CustomerIOFirebaseMessagingService.kt | 6 +++--- .../activity/NotificationClickReceiverActivity.kt | 4 ++-- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/common-test/src/main/java/io/customer/commontest/core/BaseTest.kt b/common-test/src/main/java/io/customer/commontest/core/BaseTest.kt index 972bc3ded..5930283a0 100644 --- a/common-test/src/main/java/io/customer/commontest/core/BaseTest.kt +++ b/common-test/src/main/java/io/customer/commontest/core/BaseTest.kt @@ -7,7 +7,7 @@ import io.customer.commontest.config.configureAndroidSDKComponent import io.customer.commontest.config.configureSDKComponent import io.customer.commontest.config.testConfigurationDefault import io.customer.sdk.core.di.SDKComponent -import io.customer.sdk.core.di.ensureAndroidComponent +import io.customer.sdk.core.di.setupAndroidComponent import io.mockk.clearAllMocks /** @@ -43,6 +43,6 @@ abstract class BaseTest { private fun registerAndroidSDKComponent(testConfig: TestConfig) { val application = testConfig.argumentOrNull()?.value ?: return - testConfig.configureAndroidSDKComponent(SDKComponent.ensureAndroidComponent(application)) + testConfig.configureAndroidSDKComponent(SDKComponent.setupAndroidComponent(application)) } } diff --git a/core/api/core.api b/core/api/core.api index d79185a56..cc28582e6 100644 --- a/core/api/core.api +++ b/core/api/core.api @@ -154,7 +154,7 @@ public final class io/customer/sdk/core/di/SDKComponent : io/customer/sdk/core/d } public final class io/customer/sdk/core/di/SDKComponentExtKt { - public static final fun ensureAndroidComponent (Lio/customer/sdk/core/di/SDKComponent;Landroid/content/Context;)Lio/customer/sdk/core/di/AndroidSDKComponent; + public static final fun setupAndroidComponent (Lio/customer/sdk/core/di/SDKComponent;Landroid/content/Context;)Lio/customer/sdk/core/di/AndroidSDKComponent; } public abstract interface class io/customer/sdk/core/environment/BuildEnvironment { diff --git a/core/src/main/kotlin/io/customer/sdk/core/di/SDKComponentExt.kt b/core/src/main/kotlin/io/customer/sdk/core/di/SDKComponentExt.kt index 31ff35af6..38be83737 100644 --- a/core/src/main/kotlin/io/customer/sdk/core/di/SDKComponentExt.kt +++ b/core/src/main/kotlin/io/customer/sdk/core/di/SDKComponentExt.kt @@ -12,7 +12,7 @@ import android.content.Context * This function should be called from all entry points of the SDK to ensure that * AndroidSDKComponent is initialized before accessing any of its dependencies. */ -fun SDKComponent.ensureAndroidComponent( +fun SDKComponent.setupAndroidComponent( context: Context ) = registerDependency { AndroidSDKComponentImpl(context) diff --git a/datapipelines/src/main/kotlin/io/customer/sdk/CustomerIOBuilder.kt b/datapipelines/src/main/kotlin/io/customer/sdk/CustomerIOBuilder.kt index 6e915105d..e8a3ee86e 100644 --- a/datapipelines/src/main/kotlin/io/customer/sdk/CustomerIOBuilder.kt +++ b/datapipelines/src/main/kotlin/io/customer/sdk/CustomerIOBuilder.kt @@ -4,7 +4,7 @@ import android.app.Application import com.segment.analytics.kotlin.core.platform.policies.FlushPolicy import io.customer.datapipelines.config.DataPipelinesModuleConfig import io.customer.sdk.core.di.SDKComponent -import io.customer.sdk.core.di.ensureAndroidComponent +import io.customer.sdk.core.di.setupAndroidComponent import io.customer.sdk.core.module.CustomerIOModule import io.customer.sdk.core.module.CustomerIOModuleConfig import io.customer.sdk.core.util.CioLogLevel @@ -33,7 +33,7 @@ class CustomerIOBuilder( // Initialize AndroidSDKComponent as soon as the builder is created so that // it can be used by the modules. // Also, it is needed to override test dependencies in the test environment - private val androidSDKComponent = SDKComponent.ensureAndroidComponent( + private val androidSDKComponent = SDKComponent.setupAndroidComponent( context = applicationContext ) diff --git a/messagingpush/src/main/java/io/customer/messagingpush/CustomerIOCloudMessagingReceiver.kt b/messagingpush/src/main/java/io/customer/messagingpush/CustomerIOCloudMessagingReceiver.kt index 330ee6c56..922287b02 100644 --- a/messagingpush/src/main/java/io/customer/messagingpush/CustomerIOCloudMessagingReceiver.kt +++ b/messagingpush/src/main/java/io/customer/messagingpush/CustomerIOCloudMessagingReceiver.kt @@ -7,7 +7,7 @@ import com.google.android.gms.cloudmessaging.CloudMessagingReceiver import io.customer.messagingpush.di.pushMessageProcessor import io.customer.messagingpush.processor.PushMessageProcessor import io.customer.sdk.core.di.SDKComponent -import io.customer.sdk.core.di.ensureAndroidComponent +import io.customer.sdk.core.di.setupAndroidComponent /** * Broadcast receiver for listening to push events from GoogleCloudMessaging (GCM). @@ -27,7 +27,7 @@ class CustomerIOCloudMessagingReceiver : BroadcastReceiver() { val extras = intent.extras // Ignore event if no data was received in extras if (extras == null || extras.isEmpty) return - SDKComponent.ensureAndroidComponent(context = context) + SDKComponent.setupAndroidComponent(context = context) SDKComponent.pushMessageProcessor.processGCMMessageIntent(intent = intent) } } diff --git a/messagingpush/src/main/java/io/customer/messagingpush/CustomerIOFirebaseMessagingService.kt b/messagingpush/src/main/java/io/customer/messagingpush/CustomerIOFirebaseMessagingService.kt index 9b1078886..c819a4bfa 100644 --- a/messagingpush/src/main/java/io/customer/messagingpush/CustomerIOFirebaseMessagingService.kt +++ b/messagingpush/src/main/java/io/customer/messagingpush/CustomerIOFirebaseMessagingService.kt @@ -6,7 +6,7 @@ import com.google.firebase.messaging.RemoteMessage import io.customer.messagingpush.di.pushMessageProcessor import io.customer.sdk.communication.Event import io.customer.sdk.core.di.SDKComponent -import io.customer.sdk.core.di.ensureAndroidComponent +import io.customer.sdk.core.di.setupAndroidComponent open class CustomerIOFirebaseMessagingService : FirebaseMessagingService() { @@ -48,7 +48,7 @@ open class CustomerIOFirebaseMessagingService : FirebaseMessagingService() { } private fun handleNewToken(context: Context, token: String) { - SDKComponent.ensureAndroidComponent(context = context) + SDKComponent.setupAndroidComponent(context = context) eventBus.publish( Event.RegisterDeviceTokenEvent(token) ) @@ -59,7 +59,7 @@ open class CustomerIOFirebaseMessagingService : FirebaseMessagingService() { remoteMessage: RemoteMessage, handleNotificationTrigger: Boolean = true ): Boolean { - SDKComponent.ensureAndroidComponent(context = context) + SDKComponent.setupAndroidComponent(context = context) val handler = CustomerIOPushNotificationHandler( pushMessageProcessor = SDKComponent.pushMessageProcessor, remoteMessage = remoteMessage diff --git a/messagingpush/src/main/java/io/customer/messagingpush/activity/NotificationClickReceiverActivity.kt b/messagingpush/src/main/java/io/customer/messagingpush/activity/NotificationClickReceiverActivity.kt index 6a6d2f51c..377996979 100644 --- a/messagingpush/src/main/java/io/customer/messagingpush/activity/NotificationClickReceiverActivity.kt +++ b/messagingpush/src/main/java/io/customer/messagingpush/activity/NotificationClickReceiverActivity.kt @@ -5,7 +5,7 @@ import android.content.Intent import android.os.Bundle import io.customer.messagingpush.di.pushMessageProcessor import io.customer.sdk.core.di.SDKComponent -import io.customer.sdk.core.di.ensureAndroidComponent +import io.customer.sdk.core.di.setupAndroidComponent import io.customer.sdk.tracking.TrackableScreen /** @@ -25,7 +25,7 @@ class NotificationClickReceiverActivity : Activity(), TrackableScreen { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - SDKComponent.ensureAndroidComponent(context = this) + SDKComponent.setupAndroidComponent(context = this) handleIntent(data = intent) }