@@ -5,8 +5,8 @@ import androidx.lifecycle.DefaultLifecycleObserver
5
5
import androidx.lifecycle.Lifecycle
6
6
import androidx.lifecycle.LifecycleOwner
7
7
import androidx.lifecycle.ProcessLifecycleOwner
8
- import com.posthog.PostHog
9
8
import com.posthog.PostHogIntegration
9
+ import com.posthog.PostHogInterface
10
10
import com.posthog.android.PostHogAndroidConfig
11
11
import java.util.Timer
12
12
import java.util.TimerTask
@@ -30,6 +30,8 @@ internal class PostHogLifecycleObserverIntegration(
30
30
private val lastUpdatedSession = AtomicLong (0L )
31
31
private val sessionMaxInterval = (1000 * 60 * 30 ).toLong() // 30 minutes
32
32
33
+ private var postHog: PostHogInterface ? = null
34
+
33
35
private companion object {
34
36
// in case there are multiple instances or the SDK is closed/setup again
35
37
// the value is still cached
@@ -54,7 +56,7 @@ internal class PostHogLifecycleObserverIntegration(
54
56
fromBackground = true
55
57
}
56
58
57
- PostHog .capture(" Application Opened" , properties = props)
59
+ postHog? .capture(" Application Opened" , properties = props)
58
60
}
59
61
}
60
62
@@ -67,7 +69,7 @@ internal class PostHogLifecycleObserverIntegration(
67
69
if (lastUpdatedSession == 0L ||
68
70
(lastUpdatedSession + sessionMaxInterval) <= currentTimeMillis
69
71
) {
70
- PostHog .startSession()
72
+ postHog? .startSession()
71
73
}
72
74
this .lastUpdatedSession.set(currentTimeMillis)
73
75
}
@@ -85,7 +87,7 @@ internal class PostHogLifecycleObserverIntegration(
85
87
timerTask =
86
88
object : TimerTask () {
87
89
override fun run () {
88
- PostHog .endSession()
90
+ postHog? .endSession()
89
91
}
90
92
}
91
93
timer.schedule(timerTask, sessionMaxInterval)
@@ -94,7 +96,7 @@ internal class PostHogLifecycleObserverIntegration(
94
96
95
97
override fun onStop (owner : LifecycleOwner ) {
96
98
if (config.captureApplicationLifecycleEvents) {
97
- PostHog .capture(" Application Backgrounded" )
99
+ postHog? .capture(" Application Backgrounded" )
98
100
}
99
101
100
102
val currentTimeMillis = config.dateProvider.currentTimeMillis()
@@ -106,8 +108,9 @@ internal class PostHogLifecycleObserverIntegration(
106
108
lifecycle.addObserver(this )
107
109
}
108
110
109
- override fun install () {
111
+ override fun install (postHog : PostHogInterface ) {
110
112
try {
113
+ this .postHog = postHog
111
114
if (isMainThread(mainHandler)) {
112
115
add()
113
116
} else {
@@ -126,6 +129,7 @@ internal class PostHogLifecycleObserverIntegration(
126
129
127
130
override fun uninstall () {
128
131
try {
132
+ this .postHog = null
129
133
if (isMainThread(mainHandler)) {
130
134
remove()
131
135
} else {
0 commit comments