Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
buildscript {
ext.kotlin_version = '2.0.20'
if (!project.hasProperty('version') || project.version.equals('unspecified') || project.version.toString().contains('beta')) {
project.version = '+'
}

repositories {
google()
mavenLocal()
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:7.4.1'
classpath 'com.android.tools.build:gradle:8.1.4'
classpath 'com.mparticle:android-kit-plugin:' + project.version
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.10"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

plugins {
id "org.sonarqube" version "3.5.0.2730"
id "org.jlleitschuh.gradle.ktlint" version "11.2.0"
id "org.jlleitschuh.gradle.ktlint" version "13.0.0"
}

sonarqube {
Expand All @@ -28,13 +31,29 @@ sonarqube {
}

apply plugin: 'org.jlleitschuh.gradle.ktlint'
apply plugin: 'com.mparticle.kit'
apply plugin: 'kotlin-android'
apply plugin: 'com.mparticle.kit'

android {
namespace 'com.mparticle.kits.iterable'
buildFeatures {
buildConfig = true
}
defaultConfig {
minSdkVersion 21
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = '17'
}
testOptions {
unitTests.all {
jvmArgs += ['--add-opens', 'java.base/java.lang=ALL-UNNAMED']
}
}
}

dependencies {
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
3 changes: 1 addition & 2 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<manifest package="com.mparticle.kits.iterable"
xmlns:android="http://schemas.android.com/apk/res/android"
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application>
<service
Expand Down
26 changes: 15 additions & 11 deletions src/main/kotlin/com/mparticle/kits/IterableDeviceIdHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@ internal object IterableDeviceIdHelper {
@WorkerThread
fun getGoogleAdId(context: Context?): String? {
try {
val advertisingIdClient = Class
.forName("com.google.android.gms.ads.identifier.AdvertisingIdClient")
val getAdvertisingInfo = advertisingIdClient.getMethod(
"getAdvertisingIdInfo",
Context::class.java
)
val advertisingIdClient =
Class
.forName("com.google.android.gms.ads.identifier.AdvertisingIdClient")
val getAdvertisingInfo =
advertisingIdClient.getMethod(
"getAdvertisingIdInfo",
Context::class.java,
)
val advertisingInfo = getAdvertisingInfo.invoke(null, context)
val isLimitAdTrackingEnabled = advertisingInfo.javaClass.getMethod(
"isLimitAdTrackingEnabled"
)
val limitAdTrackingEnabled = isLimitAdTrackingEnabled
.invoke(advertisingInfo) as Boolean
val isLimitAdTrackingEnabled =
advertisingInfo.javaClass.getMethod(
"isLimitAdTrackingEnabled",
)
val limitAdTrackingEnabled =
isLimitAdTrackingEnabled
.invoke(advertisingInfo) as Boolean
val getId = advertisingInfo.javaClass.getMethod("getId")
val advertisingId = getId.invoke(advertisingInfo) as String
if (!limitAdTrackingEnabled) {
Expand Down
113 changes: 66 additions & 47 deletions src/main/kotlin/com/mparticle/kits/IterableKit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,26 @@ import com.mparticle.kits.KitIntegration.IdentityListener
import com.mparticle.kits.KitIntegration.PushListener
import com.mparticle.kits.iterable.Future

class IterableKit : KitIntegration(), ActivityListener, ApplicationStateListener, IdentityListener,
class IterableKit :
KitIntegration(),
ActivityListener,
ApplicationStateListener,
IdentityListener,
PushListener {
private val previousLinks: MutableSet<String> = HashSet()
private var mpidEnabled = false

override fun onKitCreate(
settings: Map<String, String>,
context: Context
context: Context,
): List<ReportingMessage> {
checkForAttribution()
val userIdField = settings[SETTING_USER_ID_FIELD]
mpidEnabled = userIdField != null && userIdField == IDENTITY_MPID
val configBuilder = IterableConfigHelper.createConfigBuilderFromIterableConfig(
customConfig
)
val configBuilder =
IterableConfigHelper.createConfigBuilderFromIterableConfig(
customConfig,
)
settings[SETTING_GCM_INTEGRATION_NAME]?.let { configBuilder.setPushIntegrationName(it) }
settings[SETTING_API_KEY]?.let {
IterableApi.initialize(context, it, configBuilder.build())
Expand All @@ -50,7 +55,6 @@ class IterableKit : KitIntegration(), ActivityListener, ApplicationStateListener

override fun setOptOut(optedOut: Boolean): List<ReportingMessage> = emptyList()


private fun initIntegrationAttributes() {
val integrationAttributes = HashMap<String, String>()
integrationAttributes[INTEGRATION_ATTRIBUTE_SDK_VERSION] =
Expand All @@ -67,21 +71,24 @@ class IterableKit : KitIntegration(), ActivityListener, ApplicationStateListener
if (!currentLink.isNullOrEmpty() && !previousLinks.contains(currentLink)
) {
previousLinks.add(currentLink)
val clickCallback = IterableActionHandler { result ->
if (!KitUtils.isEmpty(result)) {
val attributionResult = AttributionResult().setLink(result)
attributionResult.serviceProviderId = configuration.kitId
kitManager.onResult(attributionResult)
val clickCallback =
IterableActionHandler { result ->
if (!KitUtils.isEmpty(result)) {
val attributionResult = AttributionResult().setLink(result)
attributionResult.serviceProviderId = configuration.kitId
kitManager.onResult(attributionResult)
}
}
}
IterableApi.getInstance().getAndTrackDeepLink(currentLink, clickCallback)
}
}
}
}

override fun onActivityCreated(activity: Activity, bundle: Bundle?): List<ReportingMessage> =
emptyList()
override fun onActivityCreated(
activity: Activity,
bundle: Bundle?,
): List<ReportingMessage> = emptyList()

override fun onActivityStarted(activity: Activity): List<ReportingMessage> = emptyList()

Expand All @@ -93,55 +100,52 @@ class IterableKit : KitIntegration(), ActivityListener, ApplicationStateListener

override fun onActivitySaveInstanceState(
activity: Activity,
bundle: Bundle?
bundle: Bundle?,
): List<ReportingMessage> = emptyList()


override fun onActivityDestroyed(activity: Activity): List<ReportingMessage> = emptyList()


override fun onApplicationForeground() {
checkForAttribution()
}

override fun onApplicationBackground() {}

override fun onIdentifyCompleted(
mParticleUser: MParticleUser,
filteredIdentityApiRequest: FilteredIdentityApiRequest
filteredIdentityApiRequest: FilteredIdentityApiRequest,
) {
updateIdentity(mParticleUser)
}

override fun onLoginCompleted(
mParticleUser: MParticleUser,
filteredIdentityApiRequest: FilteredIdentityApiRequest
filteredIdentityApiRequest: FilteredIdentityApiRequest,
) {
updateIdentity(mParticleUser)
}

override fun onLogoutCompleted(
mParticleUser: MParticleUser,
filteredIdentityApiRequest: FilteredIdentityApiRequest
filteredIdentityApiRequest: FilteredIdentityApiRequest,
) {
updateIdentity(mParticleUser)
}

override fun onModifyCompleted(
mParticleUser: MParticleUser,
filteredIdentityApiRequest: FilteredIdentityApiRequest
filteredIdentityApiRequest: FilteredIdentityApiRequest,
) {
}

override fun onUserIdentified(mParticleUser: MParticleUser) {
updateIdentity(mParticleUser)
}

private fun isEmpty(string: String?): Boolean {
return string == null || "" == string
}
private fun isEmpty(string: String?): Boolean = string == null || "" == string

private fun getUserId(mParticleUser: MParticleUser): Future<String?> {
return Future.runAsync {
private fun getUserId(mParticleUser: MParticleUser): Future<String?> =
Future.runAsync {
var id: String? = null
if (mpidEnabled) {
if (mParticleUser.id != 0L) {
Expand All @@ -162,23 +166,28 @@ class IterableKit : KitIntegration(), ActivityListener, ApplicationStateListener
}
id
}
}

private fun String?.getPlaceholderEmail(): String? = this?.let { "$it@placeholder.email" }

private fun handleOnSuccess(userId: String?, mParticleUser: MParticleUser) {
private fun handleOnSuccess(
userId: String?,
mParticleUser: MParticleUser,
) {
if (prefersUserId) {
IterableApi.getInstance().setUserId(userId)
} else {
val userIdentities = mParticleUser.userIdentities
val mpEmail = userIdentities[IdentityType.Email]
val placeholderEmail = userId.getPlaceholderEmail()

val email = if (!mpEmail.isNullOrEmpty()) {
mpEmail
} else if (!isEmpty(placeholderEmail)) {
placeholderEmail
} else null
val email =
if (!mpEmail.isNullOrEmpty()) {
mpEmail
} else if (!isEmpty(placeholderEmail)) {
placeholderEmail
} else {
null
}

IterableApi.getInstance().setEmail(email)
}
Expand All @@ -187,31 +196,42 @@ class IterableKit : KitIntegration(), ActivityListener, ApplicationStateListener
private fun updateIdentity(mParticleUser: MParticleUser) {
val userId = getUserId(mParticleUser)

userId.onSuccess(object : Future.SuccessCallback<String?> {
override fun onSuccess(userId: String?) {
handleOnSuccess(userId, mParticleUser)
return
}
}).onFailure(object : Future.FailureCallback {
override fun onFailure(throwable: Throwable?) {
Log.e(ITERABLE_KIT_ERROR_TAG, ITERABLE_KIT_ERROR_MESSAGE, throwable)
}
})
userId
.onSuccess(
object : Future.SuccessCallback<String?> {
override fun onSuccess(userId: String?) {
handleOnSuccess(userId, mParticleUser)
return
}
},
).onFailure(
object : Future.FailureCallback {
override fun onFailure(throwable: Throwable?) {
Log.e(ITERABLE_KIT_ERROR_TAG, ITERABLE_KIT_ERROR_MESSAGE, throwable)
}
},
)
}

override fun willHandlePushMessage(intent: Intent): Boolean {
val extras = intent.extras
return extras != null && extras.containsKey(IterableConstants.ITERABLE_DATA_KEY)
}

override fun onPushMessageReceived(context: Context, intent: Intent) {
override fun onPushMessageReceived(
context: Context,
intent: Intent,
) {
IterableFirebaseMessagingService.handleMessageReceived(
context,
RemoteMessage(intent.extras)
RemoteMessage(intent.extras),
)
}

override fun onPushRegistration(instanceId: String, senderId: String): Boolean {
override fun onPushRegistration(
instanceId: String,
senderId: String,
): Boolean {
IterableApi.getInstance().registerForPush()
return true
}
Expand All @@ -230,7 +250,6 @@ class IterableKit : KitIntegration(), ActivityListener, ApplicationStateListener
private const val ITERABLE_KIT_ERROR_TAG = "IterableKit"
private const val ITERABLE_KIT_ERROR_MESSAGE = "Error while getting the placeholder email"


/**
* Set a custom config to be used when initializing Iterable SDK
* @param config `IterableConfig` instance with configuration data for Iterable SDK
Expand Down
11 changes: 6 additions & 5 deletions src/main/kotlin/com/mparticle/kits/iterable/Future.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import android.os.Looper
import java.util.concurrent.Callable
import java.util.concurrent.Executors

class Future<T> private constructor(callable: Callable<T>) {
class Future<T> private constructor(
callable: Callable<T>,
) {
private lateinit var callbackHandler: Handler
private val successCallbacks: MutableList<SuccessCallback<T>> = ArrayList()
private val failureCallbacks: MutableList<FailureCallback> = ArrayList()

private fun handleSuccess(result: T) {
callbackHandler.post {
var callbacks: List<SuccessCallback<T>>
Expand Down Expand Up @@ -49,9 +52,8 @@ class Future<T> private constructor(callable: Callable<T>) {

companion object {
private val EXECUTOR = Executors.newCachedThreadPool()
fun <T> runAsync(callable: Callable<T>): Future<T> {
return Future(callable)
}

fun <T> runAsync(callable: Callable<T>): Future<T> = Future(callable)
}

init {
Expand All @@ -67,7 +69,6 @@ class Future<T> private constructor(callable: Callable<T>) {
handleFailure(e)
}
} ?: { looper = Looper.getMainLooper() }

}
}
}
2 changes: 1 addition & 1 deletion src/test/kotlin/com/mparticle/kits/IterableKitTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.mockito.Mockito

class IterableKitTests {
private val kit: KitIntegration
get() = IterableKit()
get() = IterableKit()

@Test
@Throws(Exception::class)
Expand Down