-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoktKit.kt
More file actions
332 lines (289 loc) · 12.1 KB
/
Copy pathRoktKit.kt
File metadata and controls
332 lines (289 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package com.mparticle.kits
import android.app.Application
import android.content.Context
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.graphics.Typeface
import android.os.Build
import com.mparticle.BuildConfig
import com.mparticle.MParticle
import com.mparticle.MParticle.IdentityType
import com.mparticle.WrapperSdk
import com.mparticle.WrapperSdkVersion
import com.mparticle.commerce.CommerceEvent
import com.mparticle.identity.MParticleUser
import com.mparticle.internal.Logger
import com.mparticle.kits.KitIntegration.CommerceListener
import com.mparticle.kits.KitIntegration.IdentityListener
import com.mparticle.kits.KitIntegration.RoktListener
import com.mparticle.rokt.RoktConfig
import com.mparticle.rokt.RoktEmbeddedView
import com.rokt.roktsdk.CacheConfig
import com.rokt.roktsdk.Rokt
import com.rokt.roktsdk.Rokt.SdkFrameworkType.*
import com.rokt.roktsdk.RoktWidgetDimensionCallBack
import com.rokt.roktsdk.Widget
import java.lang.ref.WeakReference
import java.math.BigDecimal
/**
* MParticle embedded implementation of the Rokt Library.
*
* Learn more at our [Developer Docs](https://docs.rokt.com/developers/integration-guides/android)
*/
class RoktKit : KitIntegration(), CommerceListener, IdentityListener, RoktListener, Rokt.RoktCallback {
private var applicationContext: Context? = null
private var mpRoktEventCallback: MParticle.MpRoktEventCallback? = null
override fun getName(): String = NAME
override fun getInstance(): RoktKit = this
public override fun onKitCreate(
settings: Map<String, String>,
ctx: Context
): List<ReportingMessage> {
applicationContext = ctx.applicationContext
val roktTagId = settings[ROKT_ACCOUNT_ID]
if (KitUtils.isEmpty(roktTagId)) {
throwOnKitCreateError(NO_ROKT_ACCOUNT_ID)
}
applicationContext?.let {
val manager = context.packageManager
if (roktTagId != null) {
try {
val info = manager.getPackageInfoForApp(context.packageName, 0)
val application = context.applicationContext as Application
val mparticleVersion = BuildConfig.VERSION_NAME
Rokt.init(
roktTagId = roktTagId,
appVersion = info.versionName,
application = application,
fontPostScriptNames = emptySet(),
fontFilePathMap = emptyMap(),
callback = null,
mParticleSdkVersion = mparticleVersion,
mParticleKitVersion = mparticleVersion
)
} catch (e: PackageManager.NameNotFoundException) {
throwOnKitCreateError(NO_APP_VERSION_FOUND)
} catch (e: Exception) {
logError("Error initializing Rokt", e)
}
}
}
return emptyList()
}
override fun setOptOut(optedOut: Boolean): List<ReportingMessage> = emptyList()
public override fun reset() {
super.reset()
}
/*
* Overrides for CommerceListener
*/
override fun logLtvIncrease(
bigDecimal: BigDecimal, bigDecimal1: BigDecimal,
s: String, map: Map<String, String>
): List<ReportingMessage> = emptyList()
override fun logEvent(commerceEvent: CommerceEvent): List<ReportingMessage> {
return emptyList()
}
/*
* Overrides for IdentityListener
*/
override fun onIdentifyCompleted(
mParticleUser: MParticleUser,
filteredIdentityApiRequest: FilteredIdentityApiRequest
) {
}
override fun onLoginCompleted(
mParticleUser: MParticleUser,
filteredIdentityApiRequest: FilteredIdentityApiRequest
) {
}
override fun onLogoutCompleted(
mParticleUser: MParticleUser,
filteredIdentityApiRequest: FilteredIdentityApiRequest
) {
}
override fun onModifyCompleted(
mParticleUser: MParticleUser,
filteredIdentityApiRequest: FilteredIdentityApiRequest
) {
}
override fun onUserIdentified(mParticleUser: MParticleUser) {}
private fun logError(message: String, t: Throwable) {
Logger.error(t, "RoktKit: $message")
}
private fun throwOnKitCreateError(message: String) {
throw IllegalArgumentException(message)
}
/*
For more details, visit the official documentation:
https://docs.rokt.com/developers/integration-guides/android/how-to/adding-a-placement/
*/
override fun execute(
viewName: String,
attributes: Map<String, String>,
mpRoktEventCallback: MParticle.MpRoktEventCallback?,
placeHolders: MutableMap<String, WeakReference<RoktEmbeddedView>>?,
fontTypefaces: MutableMap<String, WeakReference<Typeface>>?,
filterUser: FilteredMParticleUser?,
mpRoktConfig: RoktConfig?
) {
val placeholders: Map<String, WeakReference<Widget>>? = placeHolders?.mapNotNull { entry ->
val widget = Widget(entry.value.get()?.context as Context)
entry.value.get()?.removeAllViews()
entry.value.get()?.addView(widget)
entry.value.get()?.dimensionCallBack?.let {
widget.registerDimensionListener(object: RoktWidgetDimensionCallBack {
override fun onHeightChanged(height: Int) {
it.onHeightChanged(height)
}
override fun onMarginChanged(
start: Int,
top: Int,
end: Int,
bottom: Int
) {
it.onMarginChanged(start, top, end, bottom)
}
})
}
entry.key to WeakReference(widget)
}?.toMap()
this.mpRoktEventCallback = mpRoktEventCallback
val finalAttributes: HashMap<String, String> = HashMap<String, String>()
filterUser?.userAttributes?.let { userAttrs ->
for ((key, value) in userAttrs) {
finalAttributes[key] = value.toString()
}
}
filterUser?.id?.toString()?.let { mpid ->
finalAttributes[MPID] = mpid
} ?: Logger.warning("RoktKit: No user ID available for placement")
addIdentityAttributes(finalAttributes, filterUser)
val SANDBOX_MODE_ROKT: String = "sandbox"
attributes?.get(SANDBOX_MODE_ROKT)?.let { value ->
finalAttributes.put(SANDBOX_MODE_ROKT, value)
}
val roktConfig = mpRoktConfig?.let { mapToRoktConfig(it) }
Rokt.execute(
viewName,
finalAttributes,
this,
// Pass placeholders and fontTypefaces only if they are not empty or null
placeholders.takeIf { it?.isNotEmpty() == true },
fontTypefaces.takeIf { it?.isNotEmpty() == true },
roktConfig
)
}
override fun setWrapperSdkVersion(wrapperSdkVersion: WrapperSdkVersion) {
val sdkFrameworkType = when (wrapperSdkVersion.sdk) {
WrapperSdk.WrapperFlutter -> Flutter
WrapperSdk.WrapperSdkReactNative -> ReactNative
WrapperSdk.WrapperSdkCordova -> Cordova
else -> Android
}
Rokt.setFrameworkType(sdkFrameworkType)
}
override fun purchaseFinalized(placementId: String, catalogItemId: String, status: Boolean) {
Rokt.purchaseFinalized(placementId, catalogItemId, status)
}
private fun mapToRoktConfig(config: RoktConfig): com.rokt.roktsdk.RoktConfig {
val colorMode = when (config.colorMode) {
RoktConfig.ColorMode.LIGHT -> com.rokt.roktsdk.RoktConfig.ColorMode.LIGHT
RoktConfig.ColorMode.DARK -> com.rokt.roktsdk.RoktConfig.ColorMode.DARK
RoktConfig.ColorMode.SYSTEM -> com.rokt.roktsdk.RoktConfig.ColorMode.SYSTEM
else -> com.rokt.roktsdk.RoktConfig.ColorMode.SYSTEM
}
val cacheConfig = config.cacheConfig?.cacheDurationInSeconds?.let {
CacheConfig(
cacheDurationInSeconds = it,
cacheAttributes = config.cacheConfig?.cacheAttributes
)
}
val edgeToEdgeDisplay = config.edgeToEdgeDisplay
val builder = com.rokt.roktsdk.RoktConfig.Builder()
.colorMode(colorMode)
.edgeToEdgeDisplay(edgeToEdgeDisplay)
cacheConfig?.let {
builder.cacheConfig(it)
}
return builder.build()
}
private fun addIdentityAttributes(attributes: MutableMap<String, String>?, filterUser: FilteredMParticleUser?): MutableMap<String, String> {
val identityAttributes = mutableMapOf<String, String>()
if (filterUser != null) {
for ((identityNumberKey, identityValue) in filterUser.userIdentities) {
val identityType = getStringForIdentity(identityNumberKey)
identityAttributes[identityType] = identityValue
}
}
if (attributes != null) {
attributes.putAll(identityAttributes)
return attributes
} else {
return identityAttributes
}
}
private fun getStringForIdentity(identityType: IdentityType): String {
return when (identityType) {
IdentityType.Other -> "other"
IdentityType.CustomerId -> "customerid"
IdentityType.Facebook -> "facebook"
IdentityType.Twitter -> "twitter"
IdentityType.Google -> "google"
IdentityType.Microsoft -> "microsoft"
IdentityType.Yahoo -> "yahoo"
IdentityType.Email -> "email"
IdentityType.Alias -> "alias"
IdentityType.FacebookCustomAudienceId -> "facebookcustomaudienceid"
IdentityType.Other2 -> "other2"
IdentityType.Other3 -> "other3"
IdentityType.Other4 -> "other4"
IdentityType.Other5 -> "other5"
IdentityType.Other6 -> "other6"
IdentityType.Other7 -> "other7"
IdentityType.Other8 -> "other8"
IdentityType.Other9 -> "other9"
IdentityType.Other10 -> "other10"
IdentityType.MobileNumber -> "mobilenumber"
IdentityType.PhoneNumber2 -> "phonenumber2"
IdentityType.PhoneNumber3 -> "phonenumber3"
else -> ""
}
}
companion object {
const val NAME = "Rokt"
const val ROKT_ACCOUNT_ID = "accountId"
const val MPID = "mpid"
const val NO_ROKT_ACCOUNT_ID = "No Rokt account ID provided, can't initialize kit."
const val NO_APP_VERSION_FOUND = "No App version found, can't initialize kit."
}
override fun onLoad() : Unit{
mpRoktEventCallback?.onLoad()
}
override fun onShouldHideLoadingIndicator() : Unit {
mpRoktEventCallback?.onShouldHideLoadingIndicator()
}
override fun onShouldShowLoadingIndicator() : Unit {
mpRoktEventCallback?.onShouldShowLoadingIndicator()
}
override fun onUnload(reason: Rokt.UnloadReasons) : Unit {
mpRoktEventCallback?.onUnload(
when (reason) {
Rokt.UnloadReasons.NO_OFFERS -> MParticle.UnloadReasons.NO_OFFERS
Rokt.UnloadReasons.FINISHED -> MParticle.UnloadReasons.FINISHED
Rokt.UnloadReasons.TIMEOUT -> MParticle.UnloadReasons.TIMEOUT
Rokt.UnloadReasons.NETWORK_ERROR -> MParticle.UnloadReasons.NETWORK_ERROR
Rokt.UnloadReasons.NO_WIDGET -> MParticle.UnloadReasons.NO_WIDGET
Rokt.UnloadReasons.INIT_FAILED -> MParticle.UnloadReasons.INIT_FAILED
Rokt.UnloadReasons.UNKNOWN_PLACEHOLDER -> MParticle.UnloadReasons.UNKNOWN_PLACEHOLDER
Rokt.UnloadReasons.UNKNOWN -> MParticle.UnloadReasons.UNKNOWN
}
)
}
}
fun PackageManager.getPackageInfoForApp(packageName: String, flags: Int = 0): PackageInfo =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(flags.toLong()))
} else {
@Suppress("DEPRECATION") getPackageInfo(packageName, flags)
}