From ebd29f471363023b21ad006af50df165b203b1a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hunyady=20Mih=C3=A1ly?= Date: Wed, 25 Aug 2021 10:13:10 +0200 Subject: [PATCH] fix(lifecycles): remove unnecessary thread schedulations SUITEDEV-28864 Co-authored-by: kovacszsoltizsolt <22084766+kovacszsoltizsolt@users.noreply.github.com> Co-authored-by: davidSchuppa <32750715+davidSchuppa@users.noreply.github.com> Co-authored-by: LasOri <24588073+LasOri@users.noreply.github.com> --- .../core/util/log/entry/CrashLogTest.kt | 16 ++- .../emarsys/core/concurrency/CoreHandler.java | 23 ---- .../emarsys/core/concurrency/CoreHandler.kt | 18 +++ .../emarsys/core/util/log/entry/CrashLog.kt | 5 +- .../src/main/java/com/emarsys/Emarsys.kt | 113 ++++++++++-------- .../com/emarsys/di/DefaultEmarsysComponent.kt | 2 +- 6 files changed, 97 insertions(+), 80 deletions(-) delete mode 100644 core/src/main/java/com/emarsys/core/concurrency/CoreHandler.java create mode 100644 core/src/main/java/com/emarsys/core/concurrency/CoreHandler.kt diff --git a/core/src/androidTest/java/com/emarsys/core/util/log/entry/CrashLogTest.kt b/core/src/androidTest/java/com/emarsys/core/util/log/entry/CrashLogTest.kt index 03cde713c..39801cb11 100644 --- a/core/src/androidTest/java/com/emarsys/core/util/log/entry/CrashLogTest.kt +++ b/core/src/androidTest/java/com/emarsys/core/util/log/entry/CrashLogTest.kt @@ -19,7 +19,7 @@ class CrashLogTest { @Before fun init() { exception = ConcurrentModificationException("cause of the exception") - crashLog = CrashLog(exception) + crashLog = CrashLog(exception, "testInfo") } @Test @@ -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, diff --git a/core/src/main/java/com/emarsys/core/concurrency/CoreHandler.java b/core/src/main/java/com/emarsys/core/concurrency/CoreHandler.java deleted file mode 100644 index 4f138b772..000000000 --- a/core/src/main/java/com/emarsys/core/concurrency/CoreHandler.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.emarsys.core.concurrency; - -import android.os.Handler; -import android.os.HandlerThread; -import android.os.Message; -import com.emarsys.core.util.log.Logger; -import com.emarsys.core.util.log.entry.CrashLog; - -public class CoreHandler extends Handler { - - public CoreHandler(HandlerThread handlerThread) { - super(handlerThread.getLooper()); - } - - @Override - public void dispatchMessage(Message msg) { - try { - super.dispatchMessage(msg); - } catch (Exception e) { - Logger.error(new CrashLog(e)); - } - } -} diff --git a/core/src/main/java/com/emarsys/core/concurrency/CoreHandler.kt b/core/src/main/java/com/emarsys/core/concurrency/CoreHandler.kt new file mode 100644 index 000000000..56b52cc8e --- /dev/null +++ b/core/src/main/java/com/emarsys/core/concurrency/CoreHandler.kt @@ -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)) + } + } +} \ No newline at end of file diff --git a/core/src/main/java/com/emarsys/core/util/log/entry/CrashLog.kt b/core/src/main/java/com/emarsys/core/util/log/entry/CrashLog.kt index 464ef214b..097186caa 100644 --- a/core/src/main/java/com/emarsys/core/util/log/entry/CrashLog.kt +++ b/core/src/main/java/com/emarsys/core/util/log/entry/CrashLog.kt @@ -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 override val topic: String get() = "log_crash" @@ -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() } diff --git a/emarsys-sdk/src/main/java/com/emarsys/Emarsys.kt b/emarsys-sdk/src/main/java/com/emarsys/Emarsys.kt index 22f40d33e..8937c338c 100644 --- a/emarsys-sdk/src/main/java/com/emarsys/Emarsys.kt +++ b/emarsys-sdk/src/main/java/com/emarsys/Emarsys.kt @@ -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 @@ -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)) { @@ -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) @@ -115,22 +122,22 @@ 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) } } @@ -138,47 +145,47 @@ object Emarsys { @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?, - completionListener: CompletionListener? = null + eventName: String, + eventAttributes: Map?, + 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) } @@ -186,21 +193,21 @@ object Emarsys { 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() { @@ -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() } } } \ No newline at end of file diff --git a/emarsys-sdk/src/main/java/com/emarsys/di/DefaultEmarsysComponent.kt b/emarsys-sdk/src/main/java/com/emarsys/di/DefaultEmarsysComponent.kt index 4f50a2663..ea7b6c321 100644 --- a/emarsys-sdk/src/main/java/com/emarsys/di/DefaultEmarsysComponent.kt +++ b/emarsys-sdk/src/main/java/com/emarsys/di/DefaultEmarsysComponent.kt @@ -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)