From b6ab4383e3cb656bed5ab37b3e937c9edb307422 Mon Sep 17 00:00:00 2001 From: Mansi-mParticle <159845845+Mansi-mParticle@users.noreply.github.com> Date: Wed, 30 Oct 2024 17:32:04 -0400 Subject: [PATCH] refactor: Migrate Internal CoreCallback class to kotlin (#517) --- .../com/mparticle/internal/CoreCallbacks.java | 79 ------------------- .../com/mparticle/internal/CoreCallbacks.kt | 63 +++++++++++++++ .../internal/KitFrameworkWrapperTest.kt | 22 +++--- 3 files changed, 74 insertions(+), 90 deletions(-) delete mode 100644 android-core/src/main/java/com/mparticle/internal/CoreCallbacks.java create mode 100644 android-core/src/main/kotlin/com/mparticle/internal/CoreCallbacks.kt diff --git a/android-core/src/main/java/com/mparticle/internal/CoreCallbacks.java b/android-core/src/main/java/com/mparticle/internal/CoreCallbacks.java deleted file mode 100644 index 9f0c9a984..000000000 --- a/android-core/src/main/java/com/mparticle/internal/CoreCallbacks.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.mparticle.internal; - -import android.app.Activity; -import android.net.Uri; - -import androidx.annotation.WorkerThread; - -import com.mparticle.MParticleOptions; - -import org.json.JSONArray; - -import java.lang.ref.WeakReference; -import java.util.Map; - -public interface CoreCallbacks { - boolean isBackgrounded(); - - int getUserBucket(); - - boolean isEnabled(); - - void setIntegrationAttributes(int kitId, Map integrationAttributes); - - Map getIntegrationAttributes(int kitId); - - WeakReference getCurrentActivity(); - - @WorkerThread - JSONArray getLatestKitConfiguration(); - - MParticleOptions.DataplanOptions getDataplanOptions(); - - boolean isPushEnabled(); - - String getPushSenderId(); - - String getPushInstanceId(); - - Uri getLaunchUri(); - - String getLaunchAction(); - - KitListener getKitListener(); - - interface KitListener { - - void kitFound(int kitId); - - void kitConfigReceived(int kitId, String configuration); - - void kitExcluded(int kitId, String reason); - - void kitStarted(int kitId); - - void onKitApiCalled(int kitId, Boolean used, Object... objects); - - void onKitApiCalled(String methodName, int kitId, Boolean used, Object... objects); - - KitListener EMPTY = new KitListener() { - public void kitFound(int kitId) { - } - - public void kitConfigReceived(int kitId, String configuration) { - } - - public void kitExcluded(int kitId, String reason) { - } - - public void kitStarted(int kitId) { - } - - public void onKitApiCalled(int kitId, Boolean used, Object... objects) { - } - - public void onKitApiCalled(String methodName, int kitId, Boolean used, Object... objects) { - } - }; - } -} diff --git a/android-core/src/main/kotlin/com/mparticle/internal/CoreCallbacks.kt b/android-core/src/main/kotlin/com/mparticle/internal/CoreCallbacks.kt new file mode 100644 index 000000000..a84fc5328 --- /dev/null +++ b/android-core/src/main/kotlin/com/mparticle/internal/CoreCallbacks.kt @@ -0,0 +1,63 @@ +package com.mparticle.internal + +import android.app.Activity +import android.net.Uri +import androidx.annotation.WorkerThread +import com.mparticle.MParticleOptions.DataplanOptions +import org.json.JSONArray +import java.lang.ref.WeakReference + +interface CoreCallbacks { + fun isBackgrounded(): Boolean + + fun getUserBucket(): Int + + fun isEnabled(): Boolean + + fun setIntegrationAttributes(kitId: Int, integrationAttributes: Map) + + fun getIntegrationAttributes(kitId: Int): Map? + + fun getCurrentActivity(): WeakReference? + + @WorkerThread + fun getLatestKitConfiguration(): JSONArray? + + fun getDataplanOptions(): DataplanOptions? + + fun isPushEnabled(): Boolean + + fun getPushSenderId(): String? + + fun getPushInstanceId(): String? + + fun getLaunchUri(): Uri? + + fun getLaunchAction(): String? + + fun getKitListener(): KitListener? + + interface KitListener { + fun kitFound(kitId: Int) + + fun kitConfigReceived(kitId: Int, configuration: String?) + + fun kitExcluded(kitId: Int, reason: String?) + + fun kitStarted(kitId: Int) + fun onKitApiCalled(kitId: Int, used: Boolean?, vararg objects: Any?) + fun onKitApiCalled(methodName: String?, kitId: Int, used: Boolean?, vararg objects: Any?) + + companion object { + @JvmField + val EMPTY: KitListener = object : KitListener { + override fun kitFound(kitId: Int) {} + override fun kitConfigReceived(kitId: Int, configuration: String?) {} + override fun kitExcluded(kitId: Int, reason: String?) {} + override fun kitStarted(kitId: Int) {} + override fun onKitApiCalled(kitId: Int, used: Boolean?, vararg objects: Any?) {} + override fun onKitApiCalled(methodName: String?, kitId: Int, used: Boolean?, vararg objects: Any?) {} + } + } + } +} \ No newline at end of file diff --git a/android-core/src/test/kotlin/com/mparticle/internal/KitFrameworkWrapperTest.kt b/android-core/src/test/kotlin/com/mparticle/internal/KitFrameworkWrapperTest.kt index 84e969512..9dd10d402 100644 --- a/android-core/src/test/kotlin/com/mparticle/internal/KitFrameworkWrapperTest.kt +++ b/android-core/src/test/kotlin/com/mparticle/internal/KitFrameworkWrapperTest.kt @@ -89,8 +89,8 @@ class KitFrameworkWrapperTest { true, Mockito.mock(MParticleOptions::class.java) ) - Mockito.`when`(wrapper.mCoreCallbacks.pushInstanceId).thenReturn("instanceId") - Mockito.`when`(wrapper.mCoreCallbacks.pushSenderId).thenReturn("1234545") + Mockito.`when`(wrapper.mCoreCallbacks.getPushInstanceId()).thenReturn("instanceId") + Mockito.`when`(wrapper.mCoreCallbacks.getPushSenderId()).thenReturn("1234545") MParticle.setInstance(MockMParticle()) wrapper.replayEvents() val mockKitManager = Mockito.mock(KitManager::class.java) @@ -594,15 +594,15 @@ class KitFrameworkWrapperTest { mockConfigManager, mockAppStateManager ) - Assert.assertEquals(mockActivity, coreCallbacks.currentActivity.get()) - Assert.assertEquals(mockKitConfiguration, coreCallbacks.latestKitConfiguration) - Assert.assertEquals(mockLaunchUri, coreCallbacks.launchUri) - Assert.assertEquals(mockPushInstanceId, coreCallbacks.pushInstanceId) - Assert.assertEquals(mockPushSenderId, coreCallbacks.pushSenderId) - Assert.assertEquals(mockUserBucket.toLong(), coreCallbacks.userBucket.toLong()) - Assert.assertEquals(isBackground, coreCallbacks.isBackgrounded) - Assert.assertEquals(isEnabled, coreCallbacks.isEnabled) - Assert.assertEquals(isPushEnabled, coreCallbacks.isPushEnabled) + Assert.assertEquals(mockActivity, coreCallbacks.getCurrentActivity()?.get()) + Assert.assertEquals(mockKitConfiguration, coreCallbacks.getLatestKitConfiguration()) + Assert.assertEquals(mockLaunchUri, coreCallbacks.getLaunchUri()) + Assert.assertEquals(mockPushInstanceId, coreCallbacks.getPushInstanceId()) + Assert.assertEquals(mockPushSenderId, coreCallbacks.getPushSenderId()) + Assert.assertEquals(mockUserBucket.toLong(), coreCallbacks.getUserBucket().toLong()) + Assert.assertEquals(isBackground, coreCallbacks.isBackgrounded()) + Assert.assertEquals(isEnabled, coreCallbacks.isEnabled()) + Assert.assertEquals(isPushEnabled, coreCallbacks.isPushEnabled()) Assert.assertEquals(mockIntegrationAttributes1, coreCallbacks.getIntegrationAttributes(1)) Assert.assertEquals(mockIntegrationAttributes2, coreCallbacks.getIntegrationAttributes(2)) }