diff --git a/dd-sdk-android-core/api/apiSurface b/dd-sdk-android-core/api/apiSurface index 8ad9b71ea8..39ebef8483 100644 --- a/dd-sdk-android-core/api/apiSurface +++ b/dd-sdk-android-core/api/apiSurface @@ -51,6 +51,7 @@ interface com.datadog.android.api.SdkCore val name: String val time: com.datadog.android.api.context.TimeInfo val service: String + fun isCoreActive(): Boolean fun setTrackingConsent(com.datadog.android.privacy.TrackingConsent) fun setUserInfo(String? = null, String? = null, String? = null, Map = emptyMap()) fun addUserProperties(Map) diff --git a/dd-sdk-android-core/api/dd-sdk-android-core.api b/dd-sdk-android-core/api/dd-sdk-android-core.api index c4f3172efc..2b46c391bc 100644 --- a/dd-sdk-android-core/api/dd-sdk-android-core.api +++ b/dd-sdk-android-core/api/dd-sdk-android-core.api @@ -116,6 +116,7 @@ public abstract interface class com/datadog/android/api/SdkCore { public abstract fun getName ()Ljava/lang/String; public abstract fun getService ()Ljava/lang/String; public abstract fun getTime ()Lcom/datadog/android/api/context/TimeInfo; + public abstract fun isCoreActive ()Z public abstract fun setTrackingConsent (Lcom/datadog/android/privacy/TrackingConsent;)V public abstract fun setUserInfo (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/util/Map;)V } diff --git a/dd-sdk-android-core/src/main/kotlin/com/datadog/android/api/SdkCore.kt b/dd-sdk-android-core/src/main/kotlin/com/datadog/android/api/SdkCore.kt index dd70f39fe1..fd6914d5bc 100644 --- a/dd-sdk-android-core/src/main/kotlin/com/datadog/android/api/SdkCore.kt +++ b/dd-sdk-android-core/src/main/kotlin/com/datadog/android/api/SdkCore.kt @@ -32,6 +32,12 @@ interface SdkCore { */ val service: String + /** + * Returns true if the core is active. + */ + @AnyThread + fun isCoreActive(): Boolean + /** * Sets the tracking consent regarding the data collection for this instance of the Datadog SDK. * diff --git a/dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/internal/DatadogCore.kt b/dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/internal/DatadogCore.kt index e056805021..e9b65d99bc 100644 --- a/dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/internal/DatadogCore.kt +++ b/dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/internal/DatadogCore.kt @@ -266,6 +266,8 @@ internal class DatadogCore( return coreFeature.createScheduledExecutorService(executorContext) } + override fun isCoreActive(): Boolean = isActive + // endregion // region InternalSdkCore diff --git a/dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/internal/NoOpInternalSdkCore.kt b/dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/internal/NoOpInternalSdkCore.kt index 4b1b1d40f8..e57d2a3554 100644 --- a/dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/internal/NoOpInternalSdkCore.kt +++ b/dd-sdk-android-core/src/main/kotlin/com/datadog/android/core/internal/NoOpInternalSdkCore.kt @@ -89,6 +89,8 @@ internal object NoOpInternalSdkCore : InternalSdkCore { override fun clearAllData() = Unit + override fun isCoreActive(): Boolean = false + // endregion // region FeatureSdkCore diff --git a/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/SessionReplay.kt b/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/SessionReplay.kt index 02c3bc7a49..077f0b5b47 100644 --- a/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/SessionReplay.kt +++ b/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/SessionReplay.kt @@ -100,7 +100,7 @@ object SessionReplay { } private fun isAlreadyRegistered() = - currentRegisteredCore?.get() != null + currentRegisteredCore?.get()?.isCoreActive() == true private fun logAlreadyRegisteredWarning(internalLogger: InternalLogger) = internalLogger.log( diff --git a/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/SessionReplayTest.kt b/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/SessionReplayTest.kt index bfa39220a6..9425609026 100644 --- a/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/SessionReplayTest.kt +++ b/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/SessionReplayTest.kt @@ -132,6 +132,7 @@ internal class SessionReplayTest { @Mock mockInternalLogger: InternalLogger ) { // Given + whenever(mockCore1.isCoreActive()).thenReturn(true) whenever(mockCore1.internalLogger).thenReturn(mockInternalLogger) whenever(mockCore2.internalLogger).thenReturn(mockInternalLogger) val fakeSessionReplayConfigurationWithMockRequirement = fakeSessionReplayConfiguration.copy( @@ -159,5 +160,42 @@ internal class SessionReplayTest { targets = listOf(InternalLogger.Target.MAINTAINER, InternalLogger.Target.TELEMETRY), message = IS_ALREADY_REGISTERED_WARNING ) + assertThat(SessionReplay.currentRegisteredCore?.get()).isEqualTo(mockCore1) + } + + @Test + fun `M allow changing cores W enable { Session Replay already enabled but old core inactive }`( + @Forgery fakeSessionReplayConfiguration: SessionReplayConfiguration, + @Mock mockCore1: FeatureSdkCore, + @Mock mockCore2: FeatureSdkCore, + @Mock mockInternalLogger: InternalLogger + ) { + // Given + whenever(mockCore1.internalLogger).thenReturn(mockInternalLogger) + whenever(mockCore2.internalLogger).thenReturn(mockInternalLogger) + val fakeSessionReplayConfigurationWithMockRequirement = fakeSessionReplayConfiguration.copy( + systemRequirementsConfiguration = mockSystemRequirementsConfiguration + ) + whenever( + mockSystemRequirementsConfiguration.runIfRequirementsMet(any(), any()) + ) doAnswer { + it.getArgument<() -> Unit>(1).invoke() + } + whenever(mockCore1.isCoreActive()).thenReturn(true) + SessionReplay.enable( + sessionReplayConfiguration = fakeSessionReplayConfigurationWithMockRequirement, + sdkCore = mockCore1 + ) + assertThat(SessionReplay.currentRegisteredCore?.get()).isEqualTo(mockCore1) + + // When + whenever(mockCore1.isCoreActive()).thenReturn(false) + SessionReplay.enable( + sessionReplayConfiguration = fakeSessionReplayConfigurationWithMockRequirement, + sdkCore = mockCore2 + ) + + // Then + assertThat(SessionReplay.currentRegisteredCore?.get()).isEqualTo(mockCore2) } }