1
1
package com.mparticle.kits
2
2
3
- import android.app.Activity
4
3
import android.app.Application
5
4
import android.content.Context
6
5
import android.content.pm.PackageInfo
7
6
import android.content.pm.PackageManager
7
+ import android.graphics.Typeface
8
8
import android.os.Build
9
- import android.os.Bundle
10
9
import com.mparticle.commerce.CommerceEvent
11
10
import com.mparticle.identity.MParticleUser
12
11
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
14
15
import com.rokt.roktsdk.Rokt
16
+ import com.rokt.roktsdk.Widget
17
+ import java.lang.ref.WeakReference
15
18
import java.math.BigDecimal
16
19
17
20
@@ -20,9 +23,12 @@ import java.math.BigDecimal
20
23
*
21
24
* Learn more at our [Developer Docs](https://docs.rokt.com/developers/integration-guides/android)
22
25
*/
23
- class RoktKit : KitIntegration (), ActivityListener, CommerceListener, IdentityListener {
26
+ class RoktKit : KitIntegration (), CommerceListener, IdentityListener, RoktListener, Rokt.RoktCallback {
24
27
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
26
32
override fun getName (): String = NAME
27
33
28
34
override fun getInstance (): RoktKit = this
@@ -32,17 +38,17 @@ class RoktKit : KitIntegration(), ActivityListener, CommerceListener, IdentityLi
32
38
ctx : Context
33
39
): List <ReportingMessage > {
34
40
applicationContext = ctx.applicationContext
35
- val roktTagId = settings[ROKT_TAG_ID ]
41
+ val roktTagId = settings[ROKT_ACCOUNT_ID ]
36
42
if (KitUtils .isEmpty(roktTagId)) {
37
- throwOnKitCreateError(NO_ROKT_TAG_ID )
43
+ throwOnKitCreateError(NO_ROKT_ACCOUNT_ID )
38
44
}
39
45
applicationContext?.let {
40
46
val manager = context.packageManager
41
47
if (roktTagId != null ) {
42
48
try {
43
49
val info = manager.getPackageInfoForApp(context.packageName, 0 )
44
50
val application = context.applicationContext as Application
45
- Rokt .init (roktTagId, info.versionName , application)
51
+ Rokt .init (roktTagId, info.versionName, application)
46
52
} catch (e: PackageManager .NameNotFoundException ) {
47
53
throwOnKitCreateError(NO_APP_VERSION_FOUND )
48
54
} catch (e: Exception ) {
@@ -59,34 +65,6 @@ class RoktKit : KitIntegration(), ActivityListener, CommerceListener, IdentityLi
59
65
super .reset()
60
66
}
61
67
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
-
90
68
/*
91
69
* Overrides for CommerceListener
92
70
*/
@@ -136,14 +114,93 @@ class RoktKit : KitIntegration(), ActivityListener, CommerceListener, IdentityLi
136
114
throw IllegalArgumentException (message)
137
115
}
138
116
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
+
139
178
companion object {
140
179
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."
143
183
const val NO_APP_VERSION_FOUND = " No App version found, can't initialize kit."
144
184
}
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
+ }
145
201
}
146
202
203
+
147
204
fun PackageManager.getPackageInfoForApp (packageName : String , flags : Int = 0): PackageInfo =
148
205
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
149
206
getPackageInfo(packageName, PackageManager .PackageInfoFlags .of(flags.toLong()))
0 commit comments