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
48 changes: 40 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

buildscript {
ext.kotlin_version = '1.7.22'
ext.kotlin_version = '2.0.20'
if (!project.hasProperty('version') || project.version.equals('unspecified')) {
project.version = '+'
}
Expand All @@ -10,16 +9,17 @@ buildscript {
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 @@ -31,15 +31,25 @@ sonarqube {
}

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

android {
namespace 'com.mparticle.kits.button'
buildFeatures {
buildConfig = true
}
defaultConfig {
minSdkVersion 16
consumerProguardFiles 'consumer-proguard.pro'
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = '17'
}
testOptions {
unitTests.returnDefaultValues = true
unitTests.all {
Expand All @@ -49,9 +59,31 @@ android {
}
}

repositories {
google()
mavenCentral()
}

configurations.all {
resolutionStrategy {
force "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
force "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
force "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
force "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
// Force Kotlin compiler version to match stdlib
eachDependency { DependencyResolveDetails details ->
if (details.requested.group == 'org.jetbrains.kotlin' && details.requested.name.startsWith('kotlin-')) {
details.useVersion kotlin_version
}
}
}
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testImplementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.annotation:annotation:1.5.0'
api 'com.usebutton.merchant:button-merchant:1.5.0'
testImplementation 'com.squareup.assertj:assertj-android:1.2.0'
testImplementation ("io.mockk:mockk:1.13.4")
testImplementation "io.mockk:mockk-jvm:1.13.8"
}
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
2 changes: 1 addition & 1 deletion src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<manifest package="com.mparticle.kits.button"/>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"/>
102 changes: 63 additions & 39 deletions src/main/kotlin/com/mparticle/kits/ButtonKit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,31 @@ import com.mparticle.commerce.CommerceEvent
import com.mparticle.commerce.Product
import com.mparticle.identity.MParticleUser
import com.mparticle.internal.Logger
import com.mparticle.kits.KitIntegration.*
import com.mparticle.kits.FilteredIdentityApiRequest
import com.mparticle.kits.KitIntegration.ActivityListener
import com.mparticle.kits.KitIntegration.CommerceListener
import com.mparticle.kits.KitIntegration.IdentityListener
import com.mparticle.kits.ReportingMessage
import com.usebutton.merchant.ButtonMerchant.AttributionTokenListener
import com.usebutton.merchant.ButtonProduct
import com.usebutton.merchant.ButtonProductCompatible
import com.usebutton.merchant.PostInstallIntentListener
import java.math.BigDecimal
import java.util.*
import java.util.ArrayList
import java.util.Collections

/**
* MParticle embedded implementation of the [Button Merchant Library](https://github.com/button/button-merchant-android).
*
* Learn more at our [Developer Docs](https://developer.usebutton.com/guides/merchants/android/button-merchant-integration-guide)
*/
class ButtonKit : KitIntegration(), ActivityListener, CommerceListener, IdentityListener,
AttributionTokenListener, PostInstallIntentListener {
class ButtonKit :
KitIntegration(),
ActivityListener,
CommerceListener,
IdentityListener,
AttributionTokenListener,
PostInstallIntentListener {
private var applicationContext: Context? = null

@JvmField
Expand All @@ -39,7 +49,7 @@ class ButtonKit : KitIntegration(), ActivityListener, CommerceListener, Identity

public override fun onKitCreate(
settings: Map<String, String>,
ctx: Context
ctx: Context,
): List<ReportingMessage> {
applicationContext = ctx.applicationContext
val applicationId = settings[APPLICATION_ID]
Expand All @@ -50,7 +60,6 @@ class ButtonKit : KitIntegration(), ActivityListener, CommerceListener, Identity
throwOnKitCreateError(LOWER_THAN_API_15)
}
applicationContext?.let {

if (applicationId != null) {
merchant.configure(it, applicationId)
}
Expand All @@ -69,6 +78,7 @@ class ButtonKit : KitIntegration(), ActivityListener, CommerceListener, Identity
/*
* Public methods to expose important Merchant Library methods
*/

/**
* Get the Button attribution token which should be attached to any orders reported and
* attributed to the Button network.
Expand All @@ -90,7 +100,7 @@ class ButtonKit : KitIntegration(), ActivityListener, CommerceListener, Identity
* // Use token with your order reporting.
* }
* }
</pre> *
</pre> *
*
* @return the last tracked Button attribution token.
*/
Expand All @@ -104,28 +114,36 @@ class ButtonKit : KitIntegration(), ActivityListener, CommerceListener, Identity
logDebug("Refreshed Button Attribution Token: %s", token)
}

override fun onResult(intent: Intent?, throwable: Throwable?) {
override fun onResult(
intent: Intent?,
throwable: Throwable?,
) {
val pm = applicationContext?.packageManager
if (pm?.let { intent?.resolveActivity(it) } != null) {
logDebug("Handling post-install intent for %s", intent.toString())
val result = AttributionResult()
.setLink(intent?.dataString)
.setServiceProviderId(configuration.kitId)
val result =
AttributionResult()
.setLink(intent?.dataString)
.setServiceProviderId(configuration.kitId)
kitManager.onResult(result)
}
if (throwable != null) {
logError("Error checking post install intent", throwable)
val attributionError = AttributionError()
.setMessage(throwable.message)
.setServiceProviderId(configuration.kitId)
val attributionError =
AttributionError()
.setMessage(throwable.message)
.setServiceProviderId(configuration.kitId)
kitManager.onError(attributionError)
}
}

/*
* Overrides for ActivityListener
*/
override fun onActivityCreated(activity: Activity, bundle: Bundle?): List<ReportingMessage> {
override fun onActivityCreated(
activity: Activity,
bundle: Bundle?,
): List<ReportingMessage> {
applicationContext?.let { merchant.trackIncomingIntent(it, activity.intent) }
return emptyList()
}
Expand All @@ -144,21 +162,21 @@ class ButtonKit : KitIntegration(), ActivityListener, CommerceListener, Identity

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


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

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


/*
* Overrides for CommerceListener
*/
override fun logLtvIncrease(
bigDecimal: BigDecimal, bigDecimal1: BigDecimal,
s: String, map: Map<String, String>
bigDecimal: BigDecimal,
bigDecimal1: BigDecimal,
s: String,
map: Map<String, String>,
): List<ReportingMessage> = emptyList()

override fun logEvent(commerceEvent: CommerceEvent): List<ReportingMessage> {
Expand All @@ -182,10 +200,11 @@ class ButtonKit : KitIntegration(), ActivityListener, CommerceListener, Identity
logDebug("Tracking cart viewed with %d products!", products.size)
merchant.trackCartViewed(products)
}
else -> logDebug(
"Product Action [%s] is not yet supported by the Button Merchant Library",
it
)
else ->
logDebug(
"Product Action [%s] is not yet supported by the Button Merchant Library",
it,
)
}
}
return emptyList()
Expand All @@ -196,26 +215,26 @@ class ButtonKit : KitIntegration(), ActivityListener, CommerceListener, Identity
*/
override fun onIdentifyCompleted(
mParticleUser: MParticleUser,
filteredIdentityApiRequest: FilteredIdentityApiRequest
filteredIdentityApiRequest: FilteredIdentityApiRequest,
) {
}

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

override fun onLogoutCompleted(
mParticleUser: MParticleUser,
filteredIdentityApiRequest: FilteredIdentityApiRequest
filteredIdentityApiRequest: FilteredIdentityApiRequest,
) {
merchant.clearAllData(applicationContext!!)
}

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

Expand All @@ -224,17 +243,21 @@ class ButtonKit : KitIntegration(), ActivityListener, CommerceListener, Identity
/*
* Utility methods
*/
private fun logDebug(message: String, vararg args: Any) {
private fun logDebug(
message: String,
vararg args: Any,
) {
Logger.debug(String.format("ButtonKit: $message", *args))
}

private fun logError(message: String, t: Throwable) {
private fun logError(
message: String,
t: Throwable,
) {
Logger.error(t, "ButtonKit: $message")
}

private fun throwOnKitCreateError(message: String) {
throw IllegalArgumentException(message)
}
private fun throwOnKitCreateError(message: String): Unit = throw IllegalArgumentException(message)

private fun parseAsButtonProducts(products: List<Product>?): List<ButtonProductCompatible> {
val buttonProducts: MutableList<ButtonProductCompatible> = ArrayList()
Expand All @@ -247,7 +270,7 @@ class ButtonKit : KitIntegration(), ActivityListener, CommerceListener, Identity

private fun parseAsButtonProduct(
product: Product?,
collectionSize: Int
collectionSize: Int,
): ButtonProductCompatible {
val buttonProduct = ButtonProduct()
if (product == null) return buttonProduct
Expand All @@ -256,10 +279,11 @@ class ButtonKit : KitIntegration(), ActivityListener, CommerceListener, Identity
buttonProduct.value = (product.totalAmount * 100).toInt()
buttonProduct.quantity = product.quantity.toInt()
buttonProduct.categories = listOf(product.category)
buttonProduct.attributes = Collections.singletonMap(
"btn_product_count",
collectionSize.toString()
)
buttonProduct.attributes =
Collections.singletonMap(
"btn_product_count",
collectionSize.toString(),
)
return buttonProduct
}

Expand All @@ -271,4 +295,4 @@ class ButtonKit : KitIntegration(), ActivityListener, CommerceListener, Identity
const val LOWER_THAN_API_15 =
"App running in an < API 15 environment, can't initialize kit."
}
}
}
Loading