11package com.mparticle.kits
22
3- import android.app.Activity
43import android.app.Application
54import android.content.Context
65import android.content.pm.PackageInfo
76import android.content.pm.PackageManager
7+ import android.graphics.Typeface
88import android.os.Build
9- import android.os.Bundle
109import com.mparticle.commerce.CommerceEvent
1110import com.mparticle.identity.MParticleUser
1211import 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
1415import com.rokt.roktsdk.Rokt
16+ import com.rokt.roktsdk.Widget
17+ import java.lang.ref.WeakReference
1518import 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+
147204fun 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