From 3e8014609ce3f01715c0767f609e3ac5c8d5180a Mon Sep 17 00:00:00 2001 From: Hemant Mann Date: Thu, 9 Dec 2021 12:37:07 +0530 Subject: [PATCH] update: library internals - Add more exception handling --- TrackierSDK/gradle.properties | 4 +- TrackierSDK/sdk-core/build.gradle | 4 +- .../java/com/trackier/sdk/APIRepository.kt | 23 ++++-- .../main/java/com/trackier/sdk/Constants.kt | 2 +- .../main/java/com/trackier/sdk/DeviceInfo.kt | 2 +- .../com/trackier/sdk/TrackierSDKInstance.kt | 78 +++++++++++-------- .../src/main/java/com/trackier/sdk/Util.kt | 20 +++-- 7 files changed, 82 insertions(+), 51 deletions(-) diff --git a/TrackierSDK/gradle.properties b/TrackierSDK/gradle.properties index f78ff37..dd48a5b 100644 --- a/TrackierSDK/gradle.properties +++ b/TrackierSDK/gradle.properties @@ -20,8 +20,8 @@ POM_DEVELOPER_ID=trackier POM_DEVELOPER_NAME=Trackier PUBLISH_DEVELOPER_EMAIL=dev@trackier.com 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\:git@github.com\:trackier/android_sdk.git POM_LICENCE_NAME=The Apache Software License, Version 2.0 diff --git a/TrackierSDK/sdk-core/build.gradle b/TrackierSDK/sdk-core/build.gradle index 4499d51..4f61acf 100644 --- a/TrackierSDK/sdk-core/build.gradle +++ b/TrackierSDK/sdk-core/build.gradle @@ -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" diff --git a/TrackierSDK/sdk-core/src/main/java/com/trackier/sdk/APIRepository.kt b/TrackierSDK/sdk-core/src/main/java/com/trackier/sdk/APIRepository.kt index 2dd2d48..3b3448a 100644 --- a/TrackierSDK/sdk-core/src/main/java/com/trackier/sdk/APIRepository.kt +++ b/TrackierSDK/sdk-core/src/main/java/com/trackier/sdk/APIRepository.kt @@ -26,20 +26,19 @@ object APIRepository { private suspend fun sendInstall(body: MutableMap): 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): 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): ResponseData { val logger = Factory.logger - logger.info("Session body is: ${body}") + logger.info("Session body is: $body") return trackierApi.sendSessionData(body) } @@ -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 + } + } + } diff --git a/TrackierSDK/sdk-core/src/main/java/com/trackier/sdk/Constants.kt b/TrackierSDK/sdk-core/src/main/java/com/trackier/sdk/Constants.kt index 5bcc646..6b3c124 100644 --- a/TrackierSDK/sdk-core/src/main/java/com/trackier/sdk/Constants.kt +++ b/TrackierSDK/sdk-core/src/main/java/com/trackier/sdk/Constants.kt @@ -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 + "/" diff --git a/TrackierSDK/sdk-core/src/main/java/com/trackier/sdk/DeviceInfo.kt b/TrackierSDK/sdk-core/src/main/java/com/trackier/sdk/DeviceInfo.kt index 0c80140..c668b41 100644 --- a/TrackierSDK/sdk-core/src/main/java/com/trackier/sdk/DeviceInfo.kt +++ b/TrackierSDK/sdk-core/src/main/java/com/trackier/sdk/DeviceInfo.kt @@ -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 diff --git a/TrackierSDK/sdk-core/src/main/java/com/trackier/sdk/TrackierSDKInstance.kt b/TrackierSDK/sdk-core/src/main/java/com/trackier/sdk/TrackierSDKInstance.kt index c0e41b7..f661826 100644 --- a/TrackierSDK/sdk-core/src/main/java/com/trackier/sdk/TrackierSDKInstance.kt +++ b/TrackierSDK/sdk-core/src/main/java/com/trackier/sdk/TrackierSDKInstance.kt @@ -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 { @@ -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 { @@ -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() { @@ -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) { @@ -227,9 +242,10 @@ class TrackierSDKInstance { } } } else { - _trackEvent(event) + CoroutineScope(Dispatchers.IO).launch { + _trackEvent(event) + } } - } private fun getLastSessionTime(): String { @@ -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) @@ -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()) { @@ -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) } } diff --git a/TrackierSDK/sdk-core/src/main/java/com/trackier/sdk/Util.kt b/TrackierSDK/sdk-core/src/main/java/com/trackier/sdk/Util.kt index 5f78d0c..5bbbae7 100644 --- a/TrackierSDK/sdk-core/src/main/java/com/trackier/sdk/Util.kt +++ b/TrackierSDK/sdk-core/src/main/java/com/trackier/sdk/Util.kt @@ -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) { @@ -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 @@ -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) @@ -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