Skip to content

Commit

Permalink
fix(lifecycles): remove unnecessary thread schedulations
Browse files Browse the repository at this point in the history
SUITEDEV-28864

Co-authored-by: kovacszsoltizsolt <[email protected]>
Co-authored-by: davidSchuppa <[email protected]>
Co-authored-by: LasOri <[email protected]>
  • Loading branch information
4 people committed Aug 25, 2021
1 parent 811afc0 commit ebd29f4
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CrashLogTest {
@Before
fun init() {
exception = ConcurrentModificationException("cause of the exception")
crashLog = CrashLog(exception)
crashLog = CrashLog(exception, "testInfo")
}

@Test
Expand All @@ -29,6 +29,20 @@ class CrashLogTest {

@Test
fun testGetData() {
val result = crashLog.data
val expected = mapOf(
"exception" to exception::class.java.name,
"reason" to exception.message,
"additionalInformation" to "testInfo",
"stackTrace" to exception.stackTrace.map(StackTraceElement::toString)
)
result shouldBe expected
}

@Test
fun testGetData_withoutAdditionalInformation() {
crashLog = CrashLog(exception)

val result = crashLog.data
val expected = mapOf(
"exception" to exception::class.java.name,
Expand Down
23 changes: 0 additions & 23 deletions core/src/main/java/com/emarsys/core/concurrency/CoreHandler.java

This file was deleted.

18 changes: 18 additions & 0 deletions core/src/main/java/com/emarsys/core/concurrency/CoreHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.emarsys.core.concurrency

import android.os.Handler
import com.emarsys.core.util.log.Logger.Companion.error
import android.os.HandlerThread
import android.os.Message
import com.emarsys.core.util.log.entry.CrashLog
import java.lang.Exception

class CoreHandler(handlerThread: HandlerThread) : Handler(handlerThread.looper) {
override fun dispatchMessage(msg: Message) {
try {
super.dispatchMessage(msg)
} catch (e: Exception) {
error(CrashLog(e))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.emarsys.core.util.log.entry

import java.util.*

class CrashLog(val throwable: Throwable?) : LogEntry {
class CrashLog(val throwable: Throwable?, additionalInformation: String? = null) : LogEntry {
override val data: Map<String, Any?>
override val topic: String
get() = "log_crash"
Expand All @@ -12,8 +12,9 @@ class CrashLog(val throwable: Throwable?) : LogEntry {
mapOf(
"exception" to throwable.javaClass.name,
"reason" to throwable.message,
"additionalInformation" to additionalInformation,
"stackTrace" to getStackTrace(throwable)
)
).filterValues { it != null }
} else {
emptyMap()
}
Expand Down
113 changes: 60 additions & 53 deletions emarsys-sdk/src/main/java/com/emarsys/Emarsys.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import com.emarsys.core.database.DatabaseContract
import com.emarsys.core.database.trigger.TriggerEvent
import com.emarsys.core.database.trigger.TriggerType
import com.emarsys.core.feature.FeatureRegistry
import com.emarsys.core.util.log.Logger
import com.emarsys.core.util.log.entry.CrashLog
import com.emarsys.di.DefaultEmarsysDependencies
import com.emarsys.di.EmarsysDependencyInjection
import com.emarsys.di.emarsys
Expand Down Expand Up @@ -75,9 +77,16 @@ object Emarsys {
DefaultEmarsysDependencies(emarsysConfig)
}

emarsys().uiHandler.post {
try {
registerLifecycleObservers()
} catch (e: Throwable) {
Logger.error(CrashLog(e))
}
}

emarsys().coreSdkHandler.post {
registerWatchDogs(emarsysConfig)
registerLifecycleObservers()
registerDatabaseTriggers()

if (FeatureRegistry.isFeatureEnabled(MOBILE_ENGAGE)) {
Expand All @@ -88,25 +97,23 @@ object Emarsys {

private fun registerLifecycleObservers() {
val appLifecycleObserver = emarsys().appLifecycleObserver
emarsys().uiHandler.post {
ProcessLifecycleOwner.get().lifecycle.addObserver(appLifecycleObserver)
}
ProcessLifecycleOwner.get().lifecycle.addObserver(appLifecycleObserver)
}

@JvmStatic
@JvmOverloads
fun setAuthenticatedContact(
contactFieldId: Int,
openIdToken: String,
completionListener: CompletionListener? = null
contactFieldId: Int,
openIdToken: String,
completionListener: CompletionListener? = null
) {
if (FeatureRegistry.isFeatureEnabled(MOBILE_ENGAGE)
|| !FeatureRegistry.isFeatureEnabled(MOBILE_ENGAGE)
&& !FeatureRegistry.isFeatureEnabled(PREDICT)
|| !FeatureRegistry.isFeatureEnabled(MOBILE_ENGAGE)
&& !FeatureRegistry.isFeatureEnabled(PREDICT)
) {
EmarsysDependencyInjection.mobileEngageApi()
.proxyApi(mobileEngage().coreSdkHandler)
.setAuthenticatedContact(contactFieldId, openIdToken, completionListener)
.proxyApi(mobileEngage().coreSdkHandler)
.setAuthenticatedContact(contactFieldId, openIdToken, completionListener)
}

FeatureRegistry.disableFeature(PREDICT)
Expand All @@ -115,92 +122,92 @@ object Emarsys {
@JvmStatic
@JvmOverloads
fun setContact(
contactFieldId: Int,
contactFieldValue: String,
completionListener: CompletionListener? = null
contactFieldId: Int,
contactFieldValue: String,
completionListener: CompletionListener? = null
) {
if (FeatureRegistry.isFeatureEnabled(MOBILE_ENGAGE)
|| !FeatureRegistry.isFeatureEnabled(MOBILE_ENGAGE)
&& !FeatureRegistry.isFeatureEnabled(PREDICT)
|| !FeatureRegistry.isFeatureEnabled(MOBILE_ENGAGE)
&& !FeatureRegistry.isFeatureEnabled(PREDICT)
) {
EmarsysDependencyInjection.mobileEngageApi()
.proxyApi(mobileEngage().coreSdkHandler)
.setContact(contactFieldId, contactFieldValue, completionListener)
.proxyApi(mobileEngage().coreSdkHandler)
.setContact(contactFieldId, contactFieldValue, completionListener)
}
if (FeatureRegistry.isFeatureEnabled(PREDICT)) {
EmarsysDependencyInjection.predictRestrictedApi()
.proxyApi(mobileEngage().coreSdkHandler)
.setContact(contactFieldId, contactFieldValue)
.proxyApi(mobileEngage().coreSdkHandler)
.setContact(contactFieldId, contactFieldValue)
}
}

@JvmStatic
@JvmOverloads
fun clearContact(completionListener: CompletionListener? = null) {
if (FeatureRegistry.isFeatureEnabled(MOBILE_ENGAGE)
|| !FeatureRegistry.isFeatureEnabled(MOBILE_ENGAGE)
&& !FeatureRegistry.isFeatureEnabled(PREDICT)
|| !FeatureRegistry.isFeatureEnabled(MOBILE_ENGAGE)
&& !FeatureRegistry.isFeatureEnabled(PREDICT)
) {
EmarsysDependencyInjection.mobileEngageApi()
.proxyApi(mobileEngage().coreSdkHandler)
.clearContact(completionListener)
.proxyApi(mobileEngage().coreSdkHandler)
.clearContact(completionListener)
}
if (FeatureRegistry.isFeatureEnabled(PREDICT)) {
EmarsysDependencyInjection.predictRestrictedApi()
.proxyApi(mobileEngage().coreSdkHandler)
.clearContact()
.proxyApi(mobileEngage().coreSdkHandler)
.clearContact()
}
}

@JvmStatic
@JvmOverloads
fun trackDeepLink(
activity: Activity,
intent: Intent,
completionListener: CompletionListener? = null
activity: Activity,
intent: Intent,
completionListener: CompletionListener? = null
) {
EmarsysDependencyInjection.deepLinkApi()
.proxyApi(mobileEngage().coreSdkHandler)
.trackDeepLinkOpen(activity, intent, completionListener)
.proxyApi(mobileEngage().coreSdkHandler)
.trackDeepLinkOpen(activity, intent, completionListener)
}

@JvmStatic
@JvmOverloads
fun trackCustomEvent(
eventName: String,
eventAttributes: Map<String, String>?,
completionListener: CompletionListener? = null
eventName: String,
eventAttributes: Map<String, String>?,
completionListener: CompletionListener? = null
) {
EmarsysDependencyInjection.eventServiceApi()
.proxyApi(mobileEngage().coreSdkHandler)
.trackCustomEventAsync(eventName, eventAttributes, completionListener)
.proxyApi(mobileEngage().coreSdkHandler)
.trackCustomEventAsync(eventName, eventAttributes, completionListener)
}

private fun registerWatchDogs(config: EmarsysConfig) {
config.application.registerActivityLifecycleCallbacks(
emarsys().activityLifecycleWatchdog
emarsys().activityLifecycleWatchdog
)
config.application.registerActivityLifecycleCallbacks(emarsys().currentActivityWatchdog)
}

private fun registerDatabaseTriggers() {
if (FeatureRegistry.isFeatureEnabled(PREDICT)) {
emarsys().coreSQLiteDatabase
.registerTrigger(
DatabaseContract.SHARD_TABLE_NAME,
TriggerType.AFTER,
TriggerEvent.INSERT,
emarsys().predictShardTrigger
)
.registerTrigger(
DatabaseContract.SHARD_TABLE_NAME,
TriggerType.AFTER,
TriggerEvent.INSERT,
emarsys().predictShardTrigger
)
}

emarsys().coreSQLiteDatabase
.registerTrigger(
DatabaseContract.SHARD_TABLE_NAME,
TriggerType.AFTER,
TriggerEvent.INSERT,
emarsys().logShardTrigger
)
.registerTrigger(
DatabaseContract.SHARD_TABLE_NAME,
TriggerType.AFTER,
TriggerEvent.INSERT,
emarsys().logShardTrigger
)
}

private fun initializeMobileEngageContact() {
Expand All @@ -213,12 +220,12 @@ object Emarsys {
if (contactToken == null && !requestContext.hasContactIdentification()) {
if (clientState == null || deviceInfoPayload != null && deviceInfoPayload != deviceInfo.deviceInfoPayload) {
EmarsysDependencyInjection.clientServiceApi()
.proxyWithLogExceptions()
.trackDeviceInfo(null)
.proxyWithLogExceptions()
.trackDeviceInfo(null)
}
EmarsysDependencyInjection.mobileEngageApi()
.proxyWithLogExceptions()
.setContact()
.proxyWithLogExceptions()
.setContact()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ open class DefaultEmarsysComponent(config: EmarsysConfig) : EmarsysComponent {
GoogleApiAvailabilityLight.getInstance()
.isGooglePlayServicesAvailable(config.application) == ConnectionResult.SUCCESS

override val coreSdkHandler: CoreSdkHandler = CoreSdkHandlerProvider().provideHandler()
final override val coreSdkHandler: CoreSdkHandler = CoreSdkHandlerProvider().provideHandler()

override val uiHandler: Handler = Handler(config.application.mainLooper)

Expand Down

0 comments on commit ebd29f4

Please sign in to comment.