Skip to content

Commit

Permalink
fix: moved shared wrapper code (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shahroz16 committed Jan 17, 2023
1 parent 6d6bc67 commit 51af98f
Show file tree
Hide file tree
Showing 19 changed files with 366 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ import androidx.core.content.ContextCompat
import io.customer.sdk.CustomerIOShared

@DrawableRes
internal fun Context.getDrawableByName(name: String?): Int? = if (name.isNullOrBlank()) null
else resources?.getIdentifier(name, "drawable", packageName)?.takeUnless { id ->
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) id == Resources.ID_NULL
else id == 0
internal fun Context.getDrawableByName(name: String?): Int? = if (name.isNullOrBlank()) {
null
} else {
resources?.getIdentifier(name, "drawable", packageName)?.takeUnless { id ->
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
id == Resources.ID_NULL
} else {
id == 0
}
}
}

@ColorInt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ class DeepLinkUtilImpl(
}

val intent: Intent? = queryDeepLinksForHostApp(context, Uri.parse(link))
return if (intent != null) intent
else {
return if (intent != null) {
intent
} else {
logger.info(
"No supporting activity found in host app for link received in" +
" push notification $link"
Expand Down Expand Up @@ -119,8 +120,11 @@ class DeepLinkUtilImpl(
hostAppIntent.setPackage(context.packageName)

hostAppIntent.flags =
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) notificationIntentFlags
else notificationIntentFlags or Intent.FLAG_ACTIVITY_REQUIRE_NON_BROWSER
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
notificationIntentFlags
} else {
notificationIntentFlags or Intent.FLAG_ACTIVITY_REQUIRE_NON_BROWSER
}

return hostAppIntent.takeIfResolvable(context.packageManager)
}
Expand Down
62 changes: 48 additions & 14 deletions sdk/api/sdk.api
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
public final class io/customer/sdk/AnalyticsConstants {
public static final field AUTO_TRACK_DEVICE_ATTRIBUTES Z
public static final field BACKGROUND_QUEUE_MIN_NUMBER_OF_TASKS I
public static final field BACKGROUND_QUEUE_SECONDS_DELAY D
public static final field HTTP_REQUEST_TIMEOUT J
public static final field INSTANCE Lio/customer/sdk/AnalyticsConstants;
public static final field SHOULD_AUTO_RECORD_SCREEN_VIEWS Z
}

public final class io/customer/sdk/CustomerIO : io/customer/sdk/CustomerIOInstance {
public static final field Companion Lio/customer/sdk/CustomerIO$Companion;
public fun clearIdentify ()V
Expand Down Expand Up @@ -35,6 +26,8 @@ public final class io/customer/sdk/CustomerIO$Builder {
public fun <init> (Ljava/lang/String;Ljava/lang/String;Landroid/app/Application;)V
public fun <init> (Ljava/lang/String;Ljava/lang/String;Lio/customer/sdk/data/model/Region;Landroid/app/Application;)V
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;Lio/customer/sdk/data/model/Region;Landroid/app/Application;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Ljava/lang/String;Ljava/lang/String;Lio/customer/sdk/data/model/Region;Landroid/app/Application;Ljava/util/Map;)V
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;Lio/customer/sdk/data/model/Region;Landroid/app/Application;Ljava/util/Map;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun addCustomerIOModule (Lio/customer/sdk/module/CustomerIOModule;)Lio/customer/sdk/CustomerIO$Builder;
public final fun autoTrackDeviceAttributes (Z)Lio/customer/sdk/CustomerIO$Builder;
public final fun autoTrackScreenViews (Z)Lio/customer/sdk/CustomerIO$Builder;
Expand Down Expand Up @@ -64,6 +57,7 @@ public final class io/customer/sdk/CustomerIOActivityLifecycleCallbacks : androi
}

public final class io/customer/sdk/CustomerIOConfig {
public static final field Companion Lio/customer/sdk/CustomerIOConfig$Companion;
public fun <init> (Lio/customer/sdk/data/store/Client;Ljava/lang/String;Ljava/lang/String;Lio/customer/sdk/data/model/Region;JZZIDDLio/customer/sdk/util/CioLogLevel;Ljava/lang/String;Ljava/util/Map;)V
public final fun component1 ()Lio/customer/sdk/data/store/Client;
public final fun component10 ()D
Expand Down Expand Up @@ -99,6 +93,40 @@ public final class io/customer/sdk/CustomerIOConfig {
public fun toString ()Ljava/lang/String;
}

public final class io/customer/sdk/CustomerIOConfig$Companion {
}

public final class io/customer/sdk/CustomerIOConfig$Companion$AnalyticsConstants {
public static final field AUTO_TRACK_DEVICE_ATTRIBUTES Z
public static final field BACKGROUND_QUEUE_MIN_NUMBER_OF_TASKS I
public static final field BACKGROUND_QUEUE_SECONDS_DELAY D
public static final field HTTP_REQUEST_TIMEOUT J
public static final field INSTANCE Lio/customer/sdk/CustomerIOConfig$Companion$AnalyticsConstants;
public static final field SHOULD_AUTO_RECORD_SCREEN_VIEWS Z
}

public final class io/customer/sdk/CustomerIOConfig$Companion$Config {
public static final field AUTO_TRACK_DEVICE_ATTRIBUTES Ljava/lang/String;
public static final field AUTO_TRACK_PUSH_EVENTS Ljava/lang/String;
public static final field BACKGROUND_QUEUE_MIN_NUMBER_OF_TASKS Ljava/lang/String;
public static final field BACKGROUND_QUEUE_SECONDS_DELAY Ljava/lang/String;
public static final field INSTANCE Lio/customer/sdk/CustomerIOConfig$Companion$Config;
public static final field LOG_LEVEL Ljava/lang/String;
public static final field TRACKING_API_URL Ljava/lang/String;
}

public final class io/customer/sdk/CustomerIOConfig$Companion$Environment {
public static final field API_KEY Ljava/lang/String;
public static final field INSTANCE Lio/customer/sdk/CustomerIOConfig$Companion$Environment;
public static final field REGION Ljava/lang/String;
public static final field SITE_ID Ljava/lang/String;
}

public final class io/customer/sdk/CustomerIOConfig$Companion$SDKConstants {
public static final field INSTANCE Lio/customer/sdk/CustomerIOConfig$Companion$SDKConstants;
public final fun getLOG_LEVEL_DEFAULT ()Lio/customer/sdk/util/CioLogLevel;
}

public abstract interface class io/customer/sdk/CustomerIOInstance {
public abstract fun clearIdentify ()V
public abstract fun deleteDeviceToken ()V
Expand Down Expand Up @@ -135,11 +163,6 @@ public final class io/customer/sdk/CustomerIOShared$Companion {
public final fun instance ()Lio/customer/sdk/CustomerIOShared;
}

public final class io/customer/sdk/SDKConstants {
public static final field INSTANCE Lio/customer/sdk/SDKConstants;
public final fun getLOG_LEVEL_DEFAULT ()Lio/customer/sdk/util/CioLogLevel;
}

public final class io/customer/sdk/data/model/EventType : java/lang/Enum {
public static final field event Lio/customer/sdk/data/model/EventType;
public static final field screen Lio/customer/sdk/data/model/EventType;
Expand All @@ -148,10 +171,16 @@ public final class io/customer/sdk/data/model/EventType : java/lang/Enum {
}

public abstract class io/customer/sdk/data/model/Region {
public static final field Companion Lio/customer/sdk/data/model/Region$Companion;
public synthetic fun <init> (Ljava/lang/String;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun getCode ()Ljava/lang/String;
}

public final class io/customer/sdk/data/model/Region$Companion {
public final fun getRegion (Ljava/lang/String;Lio/customer/sdk/data/model/Region;)Lio/customer/sdk/data/model/Region;
public static synthetic fun getRegion$default (Lio/customer/sdk/data/model/Region$Companion;Ljava/lang/String;Lio/customer/sdk/data/model/Region;ILjava/lang/Object;)Lio/customer/sdk/data/model/Region;
}

public final class io/customer/sdk/data/model/Region$EU : io/customer/sdk/data/model/Region {
public static final field INSTANCE Lio/customer/sdk/data/model/Region$EU;
}
Expand Down Expand Up @@ -188,6 +217,7 @@ public final class io/customer/sdk/data/request/DeviceJsonAdapter : com/squareup
}

public final class io/customer/sdk/data/request/MetricEvent : java/lang/Enum {
public static final field Companion Lio/customer/sdk/data/request/MetricEvent$Companion;
public static final field clicked Lio/customer/sdk/data/request/MetricEvent;
public static final field converted Lio/customer/sdk/data/request/MetricEvent;
public static final field delivered Lio/customer/sdk/data/request/MetricEvent;
Expand All @@ -196,6 +226,10 @@ public final class io/customer/sdk/data/request/MetricEvent : java/lang/Enum {
public static fun values ()[Lio/customer/sdk/data/request/MetricEvent;
}

public final class io/customer/sdk/data/request/MetricEvent$Companion {
public final fun getEvent (Ljava/lang/String;)Lio/customer/sdk/data/request/MetricEvent;
}

public abstract interface class io/customer/sdk/device/DeviceTokenProvider {
public abstract fun getCurrentToken (Lkotlin/jvm/functions/Function1;)V
public abstract fun isValidForThisDevice (Landroid/content/Context;)Z
Expand Down
21 changes: 0 additions & 21 deletions sdk/src/main/java/io/customer/sdk/Constants.kt

This file was deleted.

63 changes: 56 additions & 7 deletions sdk/src/main/java/io/customer/sdk/CustomerIO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import io.customer.sdk.data.model.Region
import io.customer.sdk.data.request.MetricEvent
import io.customer.sdk.data.store.Client
import io.customer.sdk.di.CustomerIOComponent
import io.customer.sdk.extensions.getScreenNameFromActivity
import io.customer.sdk.extensions.*
import io.customer.sdk.module.CustomerIOModule
import io.customer.sdk.module.CustomerIOModuleConfig
import io.customer.sdk.repository.CleanupRepository
Expand Down Expand Up @@ -167,20 +167,69 @@ class CustomerIO internal constructor(
) {
private val sharedInstance = CustomerIOShared.instance()
private var client: Client = Client.Android(Version.version)
private var timeout = AnalyticsConstants.HTTP_REQUEST_TIMEOUT
private var timeout = CustomerIOConfig.Companion.AnalyticsConstants.HTTP_REQUEST_TIMEOUT
private var shouldAutoRecordScreenViews: Boolean =
AnalyticsConstants.SHOULD_AUTO_RECORD_SCREEN_VIEWS
CustomerIOConfig.Companion.AnalyticsConstants.SHOULD_AUTO_RECORD_SCREEN_VIEWS
private var autoTrackDeviceAttributes: Boolean =
AnalyticsConstants.AUTO_TRACK_DEVICE_ATTRIBUTES
CustomerIOConfig.Companion.AnalyticsConstants.AUTO_TRACK_DEVICE_ATTRIBUTES
private val modules: MutableMap<String, CustomerIOModule<out CustomerIOModuleConfig>> =
mutableMapOf()
private var logLevel: CioLogLevel = SDKConstants.LOG_LEVEL_DEFAULT
private var logLevel: CioLogLevel =
CustomerIOConfig.Companion.SDKConstants.LOG_LEVEL_DEFAULT
internal var overrideDiGraph: CustomerIOComponent? = null // set for automated tests
private var trackingApiUrl: String? = null
private var backgroundQueueMinNumberOfTasks: Int =
AnalyticsConstants.BACKGROUND_QUEUE_MIN_NUMBER_OF_TASKS
CustomerIOConfig.Companion.AnalyticsConstants.BACKGROUND_QUEUE_MIN_NUMBER_OF_TASKS
private var backgroundQueueSecondsDelay: Double =
AnalyticsConstants.BACKGROUND_QUEUE_SECONDS_DELAY
CustomerIOConfig.Companion.AnalyticsConstants.BACKGROUND_QUEUE_SECONDS_DELAY

// added a `config` in the secondary constructor so users stick to our advised primary constructor
// and this is used internally only.
constructor(
siteId: String,
apiKey: String,
region: Region = Region.US,
appContext: Application,
config: Map<String, Any?>
) : this(siteId, apiKey, region, appContext) {
setupConfig(config)
}

private fun setupConfig(config: Map<String, Any?>?): Builder {
if (config == null) return this
when (val logLevel = config[CustomerIOConfig.Companion.Config.LOG_LEVEL]) {
is String -> {
setLogLevel(level = CioLogLevel.getLogLevel(logLevel))
}
is Double -> {
setLogLevel(level = CioLogLevel.getLogLevel(logLevel))
}
}
config.getProperty<String>(CustomerIOConfig.Companion.Config.TRACKING_API_URL)
?.takeIfNotBlank()?.let { value ->
setTrackingApiURL(value)
}
config.getProperty<Boolean>(CustomerIOConfig.Companion.Config.AUTO_TRACK_DEVICE_ATTRIBUTES)
?.let { value ->
autoTrackDeviceAttributes(shouldTrackDeviceAttributes = value)
}
config.getProperty<Double>(CustomerIOConfig.Companion.Config.BACKGROUND_QUEUE_SECONDS_DELAY)
?.let { value ->
setBackgroundQueueSecondsDelay(backgroundQueueSecondsDelay = value)
}
when (
val minNumberOfTasks =
config[CustomerIOConfig.Companion.Config.BACKGROUND_QUEUE_MIN_NUMBER_OF_TASKS]
) {
is Int -> {
setBackgroundQueueMinNumberOfTasks(backgroundQueueMinNumberOfTasks = minNumberOfTasks)
}
is Double -> {
setBackgroundQueueMinNumberOfTasks(backgroundQueueMinNumberOfTasks = minNumberOfTasks.toInt())
}
}
return this
}

fun setClient(client: Client): Builder {
this.client = client
Expand Down
39 changes: 39 additions & 0 deletions sdk/src/main/java/io/customer/sdk/CustomerIOConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,43 @@ data class CustomerIOConfig(
}
}
}

companion object {

/**
* SDK constants to avoid repetitive configuration values
*/
object SDKConstants {
val LOG_LEVEL_DEFAULT = CioLogLevel.ERROR
}

/**
* Analytics tracking module constants to avoid repetitive configuration values
*/
object AnalyticsConstants {
const val AUTO_TRACK_DEVICE_ATTRIBUTES = true
const val BACKGROUND_QUEUE_MIN_NUMBER_OF_TASKS = 10
const val BACKGROUND_QUEUE_SECONDS_DELAY = 30.0
const val HTTP_REQUEST_TIMEOUT = 6000L
const val SHOULD_AUTO_RECORD_SCREEN_VIEWS = true
}

/**
* Config keys for extra params
*/
object Environment {
const val SITE_ID = "siteId"
const val API_KEY = "apiKey"
const val REGION = "region"
}

object Config {
const val TRACKING_API_URL = "trackingApiUrl"
const val AUTO_TRACK_PUSH_EVENTS = "autoTrackPushEvents"
const val AUTO_TRACK_DEVICE_ATTRIBUTES = "autoTrackDeviceAttributes"
const val LOG_LEVEL = "logLevel"
const val BACKGROUND_QUEUE_MIN_NUMBER_OF_TASKS = "backgroundQueueMinNumberOfTasks"
const val BACKGROUND_QUEUE_SECONDS_DELAY = "backgroundQueueSecondsDelay"
}
}
}
13 changes: 13 additions & 0 deletions sdk/src/main/java/io/customer/sdk/data/model/Region.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,17 @@ sealed class Region(val code: String) {
// to find the correct hostname for what you're trying to do.
object US : Region(code = "us")
object EU : Region(code = "eu")

companion object {
fun getRegion(region: String?, fallback: Region = US): Region {
return if (region.isNullOrBlank()) {
fallback
} else {
listOf(
US,
EU
).find { value -> value.code.equals(region, ignoreCase = true) } ?: fallback
}
}
}
}
10 changes: 10 additions & 0 deletions sdk/src/main/java/io/customer/sdk/data/request/Metric.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ import java.util.*
@JsonClass(generateAdapter = false)
enum class MetricEvent {
delivered, opened, converted, clicked;

companion object {
fun getEvent(event: String?): MetricEvent? {
return if (event.isNullOrBlank()) {
null
} else {
values().find { value -> value.name.equals(event, ignoreCase = true) }
}
}
}
}

@JsonClass(generateAdapter = true)
Expand Down
40 changes: 40 additions & 0 deletions sdk/src/main/java/io/customer/sdk/extensions/MapExtensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.customer.sdk.extensions

import io.customer.base.internal.InternalCustomerIOApi
import io.customer.sdk.CustomerIOShared

@InternalCustomerIOApi
@Throws(IllegalArgumentException::class)
inline fun <reified T> Map<String, Any?>.getPropertyUnsafe(key: String): T {
val property = get(key)

if (property !is T) {
throw IllegalArgumentException(
"Invalid value provided for key: $key, value $property must be of type ${T::class.java.simpleName}"
)
}
return property
}

@InternalCustomerIOApi
inline fun <reified T> Map<String, Any?>.getProperty(key: String): T? = try {
getPropertyUnsafe(key)
} catch (ex: IllegalArgumentException) {
CustomerIOShared.instance().diStaticGraph.logger.error(
ex.message ?: "getProperty($key) -> IllegalArgumentException"
)
null
}

@InternalCustomerIOApi
@Throws(IllegalArgumentException::class)
fun Map<String, Any?>.getString(key: String): String = try {
getPropertyUnsafe<String>(key).takeIfNotBlank() ?: throw IllegalArgumentException(
"Invalid value provided for $key, must not be blank"
)
} catch (ex: IllegalArgumentException) {
CustomerIOShared.instance().diStaticGraph.logger.error(
ex.message ?: "getString($key) -> IllegalArgumentException"
)
throw ex
}
Loading

0 comments on commit 51af98f

Please sign in to comment.