Skip to content

Commit 5fa0c10

Browse files
sahil-roktMansi-mParticle
authored andcommitted
feat: Implement Rokt MVP
1 parent 80c1bcf commit 5fa0c10

File tree

13 files changed

+1273
-42
lines changed

13 files changed

+1273
-42
lines changed

build.gradle

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ apply plugin: 'com.mparticle.kit'
3636

3737
android {
3838
defaultConfig {
39-
minSdkVersion 16
39+
minSdkVersion 21
4040
consumerProguardFiles 'consumer-proguard.pro'
4141
}
42-
42+
lint {
43+
disable "NullSafeMutableLiveData"
44+
}
4345
testOptions {
4446
unitTests.returnDefaultValues = true
4547
unitTests.all {
@@ -49,9 +51,19 @@ android {
4951
}
5052
}
5153

54+
repositories {
55+
google()
56+
mavenCentral()
57+
maven {
58+
url "https://apps.rokt.com/msdk"
59+
}
60+
}
61+
5262
dependencies {
5363
implementation 'androidx.annotation:annotation:1.5.0'
5464
api 'com.rokt:roktsdk:4.8.1'
65+
66+
testImplementation files('libs/java-json.jar')
5567
testImplementation 'com.squareup.assertj:assertj-android:1.2.0'
5668
testImplementation ("io.mockk:mockk:1.13.4")
5769
}

consumer-proguard.pro

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# These are the proguard rules specified by the Button SDK's documentation
22

33
-keep class com.google.android.gms.ads.identifier.** { *; }
4-
-keep class com.mparticle.kits.ButtonKit { *; }
5-
-keep class com.mparticle.kits.button.** { *; }
4+
-keep class com.rokt.** { *; }

libs/java-json.jar

82.7 KB
Binary file not shown.

libs/testutils.aar

46.6 KB
Binary file not shown.

src/main/kotlin/com/mparticle/kits/RoktKit.kt

Lines changed: 95 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
package com.mparticle.kits
22

3-
import android.app.Activity
43
import android.app.Application
54
import android.content.Context
65
import android.content.pm.PackageInfo
76
import android.content.pm.PackageManager
7+
import android.graphics.Typeface
88
import android.os.Build
9-
import android.os.Bundle
109
import com.mparticle.commerce.CommerceEvent
1110
import com.mparticle.identity.MParticleUser
1211
import com.mparticle.internal.Logger
13-
import com.mparticle.kits.KitIntegration.*
12+
import com.mparticle.kits.KitIntegration.CommerceListener
13+
import com.mparticle.kits.KitIntegration.IdentityListener
14+
import com.mparticle.kits.KitIntegration.RoktListener
1415
import com.rokt.roktsdk.Rokt
16+
import com.rokt.roktsdk.Widget
17+
import java.lang.ref.WeakReference
1518
import java.math.BigDecimal
1619

1720

@@ -20,9 +23,12 @@ import java.math.BigDecimal
2023
*
2124
* Learn more at our [Developer Docs](https://docs.rokt.com/developers/integration-guides/android)
2225
*/
23-
class RoktKit : KitIntegration(), ActivityListener, CommerceListener, IdentityListener {
26+
class RoktKit : KitIntegration(), CommerceListener, IdentityListener, RoktListener, Rokt.RoktCallback {
2427
private var applicationContext: Context? = null
25-
28+
private var onUnloadCallback: Runnable? = null
29+
private var onLoadCallback: Runnable? = null
30+
private var onShouldHideLoadingIndicatorCallback: Runnable? = null
31+
private var onShouldShowLoadingIndicatorCallback: Runnable? = null
2632
override fun getName(): String = NAME
2733

2834
override fun getInstance(): RoktKit = this
@@ -32,17 +38,17 @@ class RoktKit : KitIntegration(), ActivityListener, CommerceListener, IdentityLi
3238
ctx: Context
3339
): List<ReportingMessage> {
3440
applicationContext = ctx.applicationContext
35-
val roktTagId = settings[ROKT_TAG_ID]
41+
val roktTagId = settings[ROKT_ACCOUNT_ID]
3642
if (KitUtils.isEmpty(roktTagId)) {
37-
throwOnKitCreateError(NO_ROKT_TAG_ID)
43+
throwOnKitCreateError(NO_ROKT_ACCOUNT_ID)
3844
}
3945
applicationContext?.let {
4046
val manager = context.packageManager
4147
if (roktTagId != null) {
4248
try {
4349
val info = manager.getPackageInfoForApp(context.packageName, 0)
4450
val application = context.applicationContext as Application
45-
Rokt.init(roktTagId, info.versionName , application)
51+
Rokt.init(roktTagId, info.versionName, application)
4652
} catch (e: PackageManager.NameNotFoundException) {
4753
throwOnKitCreateError(NO_APP_VERSION_FOUND)
4854
} catch (e: Exception) {
@@ -59,34 +65,6 @@ class RoktKit : KitIntegration(), ActivityListener, CommerceListener, IdentityLi
5965
super.reset()
6066
}
6167

62-
/*
63-
* Overrides for ActivityListener
64-
*/
65-
override fun onActivityCreated(activity: Activity, bundle: Bundle?): List<ReportingMessage> {
66-
return emptyList()
67-
}
68-
69-
override fun onActivityStarted(activity: Activity): List<ReportingMessage> {
70-
return emptyList()
71-
}
72-
73-
override fun onActivityResumed(activity: Activity): List<ReportingMessage> {
74-
return emptyList()
75-
}
76-
77-
override fun onActivityPaused(activity: Activity): List<ReportingMessage> = emptyList()
78-
79-
override fun onActivityStopped(activity: Activity): List<ReportingMessage> = emptyList()
80-
81-
82-
override fun onActivitySaveInstanceState(
83-
activity: Activity,
84-
bundle: Bundle?
85-
): List<ReportingMessage> = emptyList()
86-
87-
override fun onActivityDestroyed(activity: Activity): List<ReportingMessage> = emptyList()
88-
89-
9068
/*
9169
* Overrides for CommerceListener
9270
*/
@@ -136,14 +114,93 @@ class RoktKit : KitIntegration(), ActivityListener, CommerceListener, IdentityLi
136114
throw IllegalArgumentException(message)
137115
}
138116

117+
/*
118+
For more details, visit the official documentation:
119+
https://docs.rokt.com/developers/integration-guides/android/how-to/adding-a-placement/
120+
*/
121+
override fun execute(
122+
viewName: String,
123+
attributes: Map<String, String>?,
124+
onUnload: Runnable?,
125+
onLoad: Runnable?,
126+
onShouldHideLoadingIndicator: Runnable?,
127+
onShouldShowLoadingIndicator: Runnable?,
128+
placeHolders: MutableMap<String, WeakReference<com.mparticle.rokt.RoktEmbeddedView>>?,
129+
fontTypefaces: MutableMap<String, WeakReference<Typeface>>?,
130+
filterUser: FilteredMParticleUser?
131+
) {
132+
// Converting the placeholders to a Map<String, WeakReference<Widget>> by filtering and casting each entry
133+
val placeholders: Map<String, WeakReference<Widget>>? = placeHolders?.mapNotNull { entry ->
134+
(entry.value as? WeakReference<Widget>)?.let {
135+
entry.key to it
136+
}
137+
}?.toMap()
138+
onUnloadCallback = onUnload
139+
onLoadCallback = onLoad
140+
onShouldHideLoadingIndicatorCallback = onShouldHideLoadingIndicator
141+
onShouldShowLoadingIndicatorCallback = onShouldShowLoadingIndicator
142+
val finalAttributes: HashMap<String, String> = HashMap<String, String>()
143+
filterUser?.userAttributes?.let { attributes ->
144+
for ((key, value) in attributes) {
145+
finalAttributes[key] = value.toString()
146+
}
147+
}
148+
filterAttributes(finalAttributes, configuration).let {
149+
finalAttributes.putAll(it)
150+
}
151+
filterUser?.id?.let { mpid ->
152+
finalAttributes.put(MPID, mpid.toString())
153+
} ?: run {
154+
Logger.warning("RoktKit: No user ID available for placement")
155+
}
156+
157+
Rokt.execute(
158+
viewName,
159+
finalAttributes,
160+
this,
161+
// Pass placeholders and fontTypefaces only if they are not empty or null
162+
placeholders.takeIf { it?.isNotEmpty() == true },
163+
fontTypefaces.takeIf { it?.isNotEmpty() == true }
164+
)
165+
}
166+
167+
private fun filterAttributes(attributes: Map<String, String>, kitConfiguration: KitConfiguration): Map<String, String> {
168+
val userAttributes: MutableMap<String, String> = HashMap<String, String>()
169+
for ((key, value) in attributes) {
170+
val hashKey = KitUtils.hashForFiltering(key)
171+
if (!kitConfiguration.mAttributeFilters.get(hashKey)) {
172+
userAttributes[key] = value
173+
}
174+
}
175+
return userAttributes
176+
}
177+
139178
companion object {
140179
const val NAME = "Rokt"
141-
const val ROKT_TAG_ID = "rokt_tag_id"
142-
const val NO_ROKT_TAG_ID = "No Rokt tag ID provided, can't initialize kit."
180+
const val ROKT_ACCOUNT_ID = "accountId"
181+
const val MPID = "mpid"
182+
const val NO_ROKT_ACCOUNT_ID = "No Rokt account ID provided, can't initialize kit."
143183
const val NO_APP_VERSION_FOUND = "No App version found, can't initialize kit."
144184
}
185+
186+
override fun onLoad() {
187+
onLoadCallback?.run()
188+
}
189+
190+
override fun onShouldHideLoadingIndicator() {
191+
onShouldHideLoadingIndicatorCallback?.run()
192+
}
193+
194+
override fun onShouldShowLoadingIndicator() {
195+
onShouldShowLoadingIndicatorCallback?.run()
196+
}
197+
198+
override fun onUnload(reason: Rokt.UnloadReasons) {
199+
onUnloadCallback?.run()
200+
}
145201
}
146202

203+
147204
fun PackageManager.getPackageInfoForApp(packageName: String, flags: Int = 0): PackageInfo =
148205
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
149206
getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(flags.toLong()))

0 commit comments

Comments
 (0)