-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a2d4d9e
commit d2577b1
Showing
6 changed files
with
104 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...android/src/main/java/com/posthog/android/internal/PostHogLifecycleObserverIntegration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.posthog.android.internal | ||
|
||
import android.content.Context | ||
import android.os.Handler | ||
import android.os.Looper | ||
import androidx.lifecycle.DefaultLifecycleObserver | ||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.lifecycle.ProcessLifecycleOwner | ||
import com.posthog.PostHog | ||
import com.posthog.PostHogIntegration | ||
import com.posthog.android.PostHogAndroidConfig | ||
|
||
internal class PostHogLifecycleObserverIntegration(private val context: Context, private val config: PostHogAndroidConfig) : DefaultLifecycleObserver, PostHogIntegration { | ||
private val handler = Handler(Looper.getMainLooper()) | ||
|
||
@Volatile | ||
private var fromBackground = false | ||
|
||
override fun onStart(owner: LifecycleOwner) { | ||
val props = mutableMapOf<String, Any>() | ||
props["from_background"] = fromBackground | ||
|
||
if (!fromBackground) { | ||
getPackageInfo(context, config)?.let { packageInfo -> | ||
props["version"] = packageInfo.versionName | ||
props["build"] = packageInfo.versionCodeCompat() | ||
} | ||
|
||
fromBackground = true | ||
} | ||
|
||
PostHog.capture("Application Opened", properties = props) | ||
} | ||
|
||
override fun onStop(owner: LifecycleOwner) { | ||
PostHog.capture("Application Backgrounded") | ||
} | ||
|
||
private fun add() { | ||
ProcessLifecycleOwner.get().lifecycle.addObserver(this) | ||
} | ||
|
||
override fun install() { | ||
try { | ||
if (isMainThread()) { | ||
add() | ||
} else { | ||
handler.post { | ||
add() | ||
} | ||
} | ||
} catch (e: Throwable) { | ||
config.logger.log("Failed to install PostHogLifecycleObserver: $e") | ||
} | ||
} | ||
|
||
private fun remove() { | ||
ProcessLifecycleOwner.get().lifecycle.removeObserver(this) | ||
} | ||
|
||
override fun uninstall() { | ||
try { | ||
if (isMainThread()) { | ||
remove() | ||
} else { | ||
handler.post { | ||
remove() | ||
} | ||
} | ||
} catch (e: Throwable) { | ||
config.logger.log("Failed to uninstall PostHogLifecycleObserver: $e") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters