Skip to content

Commit

Permalink
update: library internals
Browse files Browse the repository at this point in the history
- Add more exception handling
  • Loading branch information
Hemant-Mann committed Dec 9, 2021
1 parent ef27a4e commit 3e80146
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 51 deletions.
4 changes: 2 additions & 2 deletions TrackierSDK/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ POM_DEVELOPER_ID=trackier
POM_DEVELOPER_NAME=Trackier
PUBLISH_DEVELOPER_EMAIL=[email protected]
POM_LICENCE_DIST=repo
VERSION_CODE=18
VERSION_NAME=1.6.15
VERSION_CODE=19
VERSION_NAME=1.6.16
GROUP=com.trackier
POM_SCM_DEV_CONNECTION=scm\:[email protected]\:trackier/android_sdk.git
POM_LICENCE_NAME=The Apache Software License, Version 2.0
Expand Down
4 changes: 2 additions & 2 deletions TrackierSDK/sdk-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ android {
defaultConfig {
minSdkVersion 16
targetSdkVersion 30
versionCode 18
versionName "1.6.15"
versionCode 19
versionName "1.6.16"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,19 @@ object APIRepository {

private suspend fun sendInstall(body: MutableMap<String, Any>): ResponseData {
val logger = Factory.logger
logger.info("Install body is: ${body}")
val response = trackierApi.sendInstallData(body)
return response
logger.info("Install body is: $body")
return trackierApi.sendInstallData(body)
}

private suspend fun sendEvent(body: MutableMap<String, Any>): ResponseData {
val logger = Factory.logger
logger.info("Event body is: ${body}")
logger.info("Event body is: $body")
return trackierApi.sendEventData(body)
}

private suspend fun sendSession(body: MutableMap<String, Any>): ResponseData {
val logger = Factory.logger
logger.info("Session body is: ${body}")
logger.info("Session body is: $body")
return trackierApi.sendSessionData(body)
}

Expand All @@ -53,4 +52,18 @@ object APIRepository {
}
}

suspend fun processWork(workRequest: TrackierWorkRequest): ResponseData? {
return try {
when(workRequest.kind) {
TrackierWorkRequest.KIND_INSTALL -> sendInstall(workRequest.getData())
TrackierWorkRequest.KIND_EVENT -> sendEvent(workRequest.getEventData())
TrackierWorkRequest.KIND_UNKNOWN -> null
TrackierWorkRequest.KIND_SESSION_TRACK -> sendSession(workRequest.getSessionData())
else -> null
}
} catch (ex: Exception) {
null
}
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.trackier.sdk

object Constants {
const val SDK_VERSION = "1.6.15"
const val SDK_VERSION = "1.6.16"
const val USER_AGENT = "com.cloudstuff.trackiersdk:trackier-android:" + SDK_VERSION
const val API_VERSION = "v1"
const val BASE_URL = "https://events.trackier.io/" + API_VERSION + "/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ data class DeviceInfo(
deviceInfo.appInstallTime = appInstallTime(context)
deviceInfo.appUpdateTime = getAppUpdateTime(context)
val config = context.resources.configuration
var locale = Util.getLocale(config)
val locale = Util.getLocale(config)
deviceInfo.language = locale?.language
deviceInfo.country = locale?.country

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,17 @@ class TrackierSDKInstance {

private fun setReferrerDetails(refererDetails: RefererDetails) {
refDetails = refererDetails
val prefs = Util.getSharedPref(this.config.context)
prefs.edit().putString(Constants.SHARED_PREF_INSTALL_URL, refererDetails.url)
.putString(Constants.SHARED_PREF_CLICK_TIME, refererDetails.clickTime)
.putString(Constants.SHARED_PREF_INSTALL_TIME, refererDetails.installTime)
.apply()
try {
val prefs = Util.getSharedPref(this.config.context)
prefs.edit().putString(Constants.SHARED_PREF_INSTALL_URL, refererDetails.url)
.putString(Constants.SHARED_PREF_CLICK_TIME, refererDetails.clickTime)
.putString(Constants.SHARED_PREF_INSTALL_TIME, refererDetails.installTime)
.apply()
} catch (ex: Exception) {}
}

private fun setInstallID(installID: String) {
val prefs = Util.getSharedPref(this.config.context)
prefs.edit().putString(Constants.SHARED_PREF_INSTALL_ID, installID)
.apply()
Util.setSharedPrefString(this.config.context, Constants.SHARED_PREF_INSTALL_ID, installID)
}

private fun getInstallID(): String {
Expand All @@ -132,9 +132,7 @@ class TrackierSDKInstance {
}

private fun setFirstInstallTS(firstInstall: String) {
val prefs = Util.getSharedPref(this.config.context)
prefs.edit().putString(Constants.SHARED_PREF_FIRST_INSTALL, firstInstall)
.apply()
Util.setSharedPrefString(this.config.context, Constants.SHARED_PREF_FIRST_INSTALL, firstInstall)
}

private fun getFirstInstallTS(): String {
Expand Down Expand Up @@ -175,8 +173,10 @@ class TrackierSDKInstance {
}

private fun setInstallTracked() {
val prefs = Util.getSharedPref(this.config.context)
prefs.edit().putBoolean(Constants.SHARED_PREF_IS_INSTALL_TRACKED, true).apply()
try {
val prefs = Util.getSharedPref(this.config.context)
prefs.edit().putBoolean(Constants.SHARED_PREF_IS_INSTALL_TRACKED, true).apply()
} catch (ex: Exception) {}
}

private suspend fun trackInstall() {
Expand All @@ -186,27 +186,42 @@ class TrackierSDKInstance {
if (isInstallTracked()) {
return
}
if (!isReferrerStored()) {
if (isLocalRefEnabled) {
val installRef = LocalInstallReferrer(this.config.context, this.localRefDelimeter)
val refDetails = installRef.getRefDetails()
this.setReferrerDetails(refDetails)
} else {
val installRef = InstallReferrer(this.config.context)
val refDetails = installRef.getRefDetails()
this.setReferrerDetails(refDetails)
try {
if (!isReferrerStored()) {
if (isLocalRefEnabled) {
val installRef = LocalInstallReferrer(this.config.context, this.localRefDelimeter)
val refDetails = installRef.getRefDetails()
this.setReferrerDetails(refDetails)
} else {
val installRef = InstallReferrer(this.config.context)
val refDetails = installRef.getRefDetails()
this.setReferrerDetails(refDetails)
}
}
} catch (ex: Exception) {
Factory.logger.warning("Unable to get referrer data on install")
}


val wrkRequest = makeWorkRequest(TrackierWorkRequest.KIND_INSTALL)
TrackierWorkRequest.enqueue(wrkRequest)
try {
TrackierWorkRequest.enqueue(wrkRequest)
} catch (ex: Exception) {
APIRepository.processWork(wrkRequest)
}

setInstallTracked()
}

private fun _trackEvent(event: TrackierEvent) {
private suspend fun _trackEvent(event: TrackierEvent) {
val wrkRequest = makeWorkRequest(TrackierWorkRequest.KIND_EVENT)
wrkRequest.event = event
TrackierWorkRequest.enqueue(wrkRequest)

try {
TrackierWorkRequest.enqueue(wrkRequest)
} catch (ex: Exception) {
APIRepository.processWork(wrkRequest)
}
}

fun trackEvent(event: TrackierEvent) {
Expand All @@ -227,9 +242,10 @@ class TrackierSDKInstance {
}
}
} else {
_trackEvent(event)
CoroutineScope(Dispatchers.IO).launch {
_trackEvent(event)
}
}

}

private fun getLastSessionTime(): String {
Expand Down Expand Up @@ -264,7 +280,7 @@ class TrackierSDKInstance {
if (sessionDiff > this.config.getMinSessionDuration()) {
val wrkRequest = makeWorkRequest(TrackierWorkRequest.KIND_SESSION_TRACK)
wrkRequest.sessionTime = lastSessionTime
APIRepository.doWork(wrkRequest)
APIRepository.processWork(wrkRequest)
}
} catch (e: Exception) {}
setLastSessionTime(currentTime)
Expand All @@ -275,7 +291,6 @@ class TrackierSDKInstance {
val isDeeplinkCalled = Util.getSharedPrefString(this.config.context, Constants.SHARED_PREF_DEEP_LINK_CALLED)
if (isDeeplinkCalled == "true") return

val prefs = Util.getSharedPref(this.config.context)
val dlstr = Util.getSharedPrefString(this.config.context, Constants.SHARED_PREF_DEEP_LINK)
val dlResult: DeepLink
if (dlstr.isBlank()) {
Expand All @@ -284,12 +299,11 @@ class TrackierSDKInstance {
return
}
dlResult = DeepLink(ref.url, true)
prefs.edit().putString(Constants.SHARED_PREF_DEEP_LINK_CALLED, "true").apply()
} else {
Util.delSharedPrefKey(this.config.context, Constants.SHARED_PREF_DEEP_LINK)
dlResult = DeepLink(dlstr, false)
prefs.edit().putString(Constants.SHARED_PREF_DEEP_LINK_CALLED, "true")
.remove(Constants.SHARED_PREF_DEEP_LINK).apply()
}
Util.setSharedPrefString(this.config.context, Constants.SHARED_PREF_DEEP_LINK_CALLED, "true")
dlt.onDeepLinking(dlResult)
}
}
20 changes: 12 additions & 8 deletions TrackierSDK/sdk-core/src/main/java/com/trackier/sdk/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ object Util {
try {
val sdf = SimpleDateFormat(Constants.DATE_TIME_FORMAT, Locale.US)
val dateObj: Date = sdf.parse(date)
var time = dateObj.time.toDouble()
var inUnix: Double = (time / 1000)
val time = dateObj.time.toDouble()
val inUnix: Double = (time / 1000)

return String.format("%.6f", BigDecimal(inUnix))
} catch (e: Exception) {
Expand All @@ -43,7 +43,7 @@ object Util {
try {
val dateProvided = dateFormatter.parse(date)
val cal = Calendar.getInstance()
cal.setTime(dateProvided)
cal.time = dateProvided
return cal[Calendar.YEAR]
} catch (e: Exception) {
return 0
Expand Down Expand Up @@ -96,13 +96,17 @@ object Util {
}

fun setSharedPrefString(context: Context, key: String, value: String) {
val prefs = getSharedPref(context)
prefs.edit().putString(key, value).apply()
try {
val prefs = getSharedPref(context)
prefs.edit().putString(key, value).apply()
} catch (ex: Exception) {}
}

fun delSharedPrefKey(context: Context, key: String) {
val prefs = getSharedPref(context)
prefs.edit().remove(key).apply()
try {
val prefs = getSharedPref(context)
prefs.edit().remove(key).apply()
} catch (ex: Exception) {}
}

fun sha1(input: String) = hashString("SHA-1", input)
Expand All @@ -126,7 +130,7 @@ object Util {
fun loadAddress(interfaceName: String): String? {
try {
val filePath = "/sys/class/net/$interfaceName/address"
val fileData = StringBuilder(1000);
val fileData = StringBuilder(1000)
val reader = BufferedReader(FileReader(filePath), 1024)
val buf = CharArray(1024)
var numRead: Int
Expand Down

0 comments on commit 3e80146

Please sign in to comment.