Skip to content

Commit

Permalink
Merge pull request #537 from mindbox-cloud/feature/WMRUSTORE-34
Browse files Browse the repository at this point in the history
WMRUSTORE-34: Update Lifecycle lib
  • Loading branch information
enotniy authored Feb 3, 2025
2 parents ead1fdf + 3a9a2e4 commit a89485f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 36 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ kotlinx_coroutines_bom = "1.6.4"
volley = "1.2.1"
gson = "2.8.9"
work_manager = "2.7.1"
androidx_lifecycle = "2.3.1"
androidx_lifecycle = "2.8.7"
androidx_core_ktx = "1.7.0"
androidx_annotations = "1.3.0"
constraint_layout = "2.1.4"
Expand Down
3 changes: 3 additions & 0 deletions proguard/proguard-lifecycle.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Androidx Lifecycle
-keep !interface * implements androidx.lifecycle.LifecycleObserver {
}
3 changes: 2 additions & 1 deletion sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ android {
"../proguard/proguard-kotlin.pro",
"../proguard/proguard-room.pro",
"../proguard/proguard-volley.pro",
"../proguard/proguard-workmanager.pro"
"../proguard/proguard-workmanager.pro",
"../proguard/proguard-lifecycle.pro"
}

testOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import android.app.Application
import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import cloud.mindbox.mobile_sdk.Mindbox.IS_OPENED_FROM_PUSH_BUNDLE_KEY
import cloud.mindbox.mobile_sdk.logger.MindboxLoggerImpl
import cloud.mindbox.mobile_sdk.logger.mindboxLogI
import cloud.mindbox.mobile_sdk.models.DIRECT
import cloud.mindbox.mobile_sdk.models.LINK
import cloud.mindbox.mobile_sdk.models.PUSH
import cloud.mindbox.mobile_sdk.utils.LoggingExceptionHandler
import cloud.mindbox.mobile_sdk.utils.loggingRunCatching
import java.util.Timer
import kotlin.concurrent.timer

Expand All @@ -25,7 +25,7 @@ internal class LifecycleManager(
private var onActivityStarted: (activity: Activity) -> Unit,
private var onActivityStopped: (activity: Activity) -> Unit,
private var onTrackVisitReady: (source: String?, requestUrl: String?) -> Unit,
) : Application.ActivityLifecycleCallbacks, LifecycleObserver {
) : Application.ActivityLifecycleCallbacks, LifecycleEventObserver {

companion object {

Expand All @@ -51,8 +51,8 @@ internal class LifecycleManager(
override fun onActivityCreated(activity: Activity, p1: Bundle?) {
}

override fun onActivityStarted(activity: Activity) = LoggingExceptionHandler.runCatching {
MindboxLoggerImpl.d(this, "onActivityStarted. activity: ${activity.javaClass.simpleName}")
override fun onActivityStarted(activity: Activity): Unit = loggingRunCatching {
mindboxLogI("onActivityStarted. activity: ${activity.javaClass.simpleName}")
onActivityStarted.invoke(activity)
val areActivitiesEqual = currentActivityName == activity.javaClass.name
val intent = activity.intent
Expand All @@ -65,28 +65,28 @@ internal class LifecycleManager(

if (isAppInBackground || !isIntentChanged) {
isAppInBackground = false
return@runCatching
return@loggingRunCatching
}

sendTrackVisit(activity.intent, areActivitiesEqual)
}

override fun onActivityResumed(activity: Activity) {
MindboxLoggerImpl.d(this, "onActivityResumed. activity: ${activity.javaClass.simpleName}")
mindboxLogI("onActivityResumed. activity: ${activity.javaClass.simpleName}")
isCurrentActivityResumed = true
onActivityResumed.invoke(activity)
isCurrentActivityResumed = true
}

override fun onActivityPaused(activity: Activity) {
MindboxLoggerImpl.d(this, "onActivityPaused. activity: ${activity.javaClass.simpleName}")
mindboxLogI("onActivityPaused. activity: ${activity.javaClass.simpleName}")
isCurrentActivityResumed = false
onActivityPaused.invoke(activity)
isCurrentActivityResumed = false
}

override fun onActivityStopped(activity: Activity) {
MindboxLoggerImpl.d(this, "onActivityStopped. activity: ${activity.javaClass.simpleName}")
mindboxLogI("onActivityStopped. activity: ${activity.javaClass.simpleName}")
if (currentIntent == null || currentActivityName == null) {
updateActivityParameters(activity)
}
Expand All @@ -112,73 +112,70 @@ internal class LifecycleManager(
skipSendingTrackVisit = true
}

fun onNewIntent(newIntent: Intent?) = newIntent?.let { intent ->
fun onNewIntent(newIntent: Intent?): Unit? = newIntent?.let { intent ->
if (intent.data != null || intent.extras?.getBoolean(IS_OPENED_FROM_PUSH_BUNDLE_KEY) == true) {
isIntentChanged = updateHashesList(intent.hashCode())
sendTrackVisit(intent)
skipSendingTrackVisit = isAppInBackground
}
}

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
private fun onAppMovedToBackground() = LoggingExceptionHandler.runCatching {
MindboxLoggerImpl.d(this, "onAppMovedToBackground")
private fun onAppMovedToBackground(): Unit = loggingRunCatching {
mindboxLogI("onAppMovedToBackground")
isAppInBackground = true
cancelKeepAliveTimer()
}

@OnLifecycleEvent(Lifecycle.Event.ON_START)
private fun onAppMovedToForeground() = LoggingExceptionHandler.runCatching {
MindboxLoggerImpl.d(this, "onAppMovedToForeground")
private fun onAppMovedToForeground(): Unit = loggingRunCatching {
mindboxLogI("onAppMovedToForeground")
if (!skipSendingTrackVisit) {
currentIntent?.let(::sendTrackVisit)
} else {
skipSendingTrackVisit = false
}
}

private fun updateActivityParameters(activity: Activity) = LoggingExceptionHandler.runCatching {
private fun updateActivityParameters(activity: Activity): Unit = loggingRunCatching {
currentActivityName = activity.javaClass.name
currentIntent = activity.intent
}

private fun sendTrackVisit(
intent: Intent,
areActivitiesEqual: Boolean = true,
) = LoggingExceptionHandler.runCatching {
): Unit = loggingRunCatching {
val source = if (isIntentChanged) source(intent) else DIRECT

if (areActivitiesEqual || source != DIRECT) {
val requestUrl = if (source == LINK) intent.data?.toString() else null
onTrackVisitReady.invoke(source, requestUrl)
startKeepAliveTimer()

MindboxLoggerImpl.d(this, "Track visit event with source $source and url $requestUrl")
mindboxLogI("Track visit event with source $source and url $requestUrl")
}
}

private fun source(intent: Intent?) = LoggingExceptionHandler.runCatching(defaultValue = null) {
private fun source(intent: Intent?): String? = loggingRunCatching(defaultValue = null) {
when {
intent?.scheme == SCHEMA_HTTP || intent?.scheme == SCHEMA_HTTPS -> LINK
intent?.extras?.getBoolean(IS_OPENED_FROM_PUSH_BUNDLE_KEY) == true -> PUSH
else -> DIRECT
}
}

private fun updateHashesList(code: Int) =
LoggingExceptionHandler.runCatching(defaultValue = true) {
if (!intentHashes.contains(code)) {
if (intentHashes.size >= MAX_INTENT_HASHES_SIZE) {
intentHashes.removeAt(0)
}
intentHashes.add(code)
true
} else {
false
private fun updateHashesList(code: Int): Boolean = loggingRunCatching(defaultValue = true) {
if (!intentHashes.contains(code)) {
if (intentHashes.size >= MAX_INTENT_HASHES_SIZE) {
intentHashes.removeAt(0)
}
intentHashes.add(code)
true
} else {
false
}
}

private fun startKeepAliveTimer() = LoggingExceptionHandler.runCatching {
private fun startKeepAliveTimer(): Unit = loggingRunCatching {
cancelKeepAliveTimer()
timer = timer(
initialDelay = TIMER_PERIOD,
Expand All @@ -187,8 +184,18 @@ internal class LifecycleManager(
)
}

private fun cancelKeepAliveTimer() = LoggingExceptionHandler.runCatching {
private fun cancelKeepAliveTimer(): Unit = loggingRunCatching {
timer?.cancel()
timer = null
}

override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
when (event) {
Lifecycle.Event.ON_STOP -> onAppMovedToBackground()
Lifecycle.Event.ON_START -> onAppMovedToForeground()
else -> {
// do nothing
}
}
}
}

0 comments on commit a89485f

Please sign in to comment.