Skip to content

Commit

Permalink
screen lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto committed Sep 25, 2023
1 parent a2d4d9e commit d2577b1
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 6 deletions.
15 changes: 13 additions & 2 deletions posthog-android/api/posthog-android.api
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,18 @@ public final class com/posthog/android/PostHogAndroid {
}

public final class com/posthog/android/PostHogAndroid$Companion {
public final fun setup (Landroid/content/Context;Lcom/posthog/PostHogConfig;)V
public final fun with (Landroid/content/Context;Lcom/posthog/PostHogConfig;)Lcom/posthog/PostHog;
public final fun setup (Landroid/content/Context;Lcom/posthog/android/PostHogAndroidConfig;)V
public final fun with (Landroid/content/Context;Lcom/posthog/android/PostHogAndroidConfig;)Lcom/posthog/PostHog;
}

public final class com/posthog/android/PostHogAndroidConfig : com/posthog/PostHogConfig {
public fun <init> (Ljava/lang/String;ZZZ)V
public synthetic fun <init> (Ljava/lang/String;ZZZILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun getCaptureApplicationLifecycleEvents ()Z
public final fun getCaptureDeepLinks ()Z
public final fun getCaptureRecordScreenViews ()Z
public final fun setCaptureApplicationLifecycleEvents (Z)V
public final fun setCaptureDeepLinks (Z)V
public final fun setCaptureRecordScreenViews (Z)V
}

2 changes: 2 additions & 0 deletions posthog-android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ dependencies {
implementation(kotlin("stdlib-jdk8", KotlinCompilerVersion.VERSION))

api(project(mapOf("path" to ":posthog")))
implementation("androidx.lifecycle:lifecycle-process:2.6.2")
implementation("androidx.lifecycle:lifecycle-common-java8:2.6.2")

// testImplementation("junit:junit:4.13.2")
// androidTestImplementation("androidx.test.ext:junit:1.1.5")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.posthog.android.internal.PostHogAndroidContext
import com.posthog.android.internal.PostHogAndroidLogger
import com.posthog.android.internal.PostHogAndroidNetworkStatus
import com.posthog.android.internal.PostHogAppInstallIntegration
import com.posthog.android.internal.PostHogLifecycleObserverIntegration
import com.posthog.android.internal.PostHogSharedPreferences
import com.posthog.android.internal.appContext
import java.io.File
Expand Down Expand Up @@ -51,7 +52,10 @@ public class PostHogAndroid private constructor() {
if (context is Application) {
config.addIntegration(PostHogActivityLifecycleCallback(context, config))
}
config.addIntegration(PostHogAppInstallIntegration(context, config))
if (config.captureApplicationLifecycleEvents) {
config.addIntegration(PostHogAppInstallIntegration(context, config))
config.addIntegration(PostHogLifecycleObserverIntegration(context, config))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.content.pm.PackageManager
import android.content.pm.PackageManager.GET_META_DATA
import android.net.ConnectivityManager
import android.os.Build
import android.os.Looper
import android.os.Process
import android.telephony.TelephonyManager
import android.util.DisplayMetrics
Expand Down Expand Up @@ -84,3 +85,7 @@ internal fun Activity.activityLabel(config: PostHogAndroidConfig): String? {
null
}
}

internal fun isMainThread(): Boolean {
return Thread.currentThread().id == Looper.getMainLooper().thread.id
}
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")
}
}
}
8 changes: 5 additions & 3 deletions posthog/api/posthog.api
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ public final class com/posthog/PostHog$Companion {
public final fun with (Lcom/posthog/PostHogConfig;)Lcom/posthog/PostHog;
}

public final class com/posthog/PostHogConfig {
public fun <init> (Ljava/lang/String;Ljava/lang/String;ZZZZIIIILcom/posthog/PostHogEncryption;Ljava/util/List;)V
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;ZZZZIIIILcom/posthog/PostHogEncryption;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public class com/posthog/PostHogConfig {
public fun <init> (Ljava/lang/String;Ljava/lang/String;ZZZZIIIILcom/posthog/PostHogEncryption;)V
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;ZZZZIIIILcom/posthog/PostHogEncryption;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun addIntegration (Lcom/posthog/PostHogIntegration;)V
public final fun getApiKey ()Ljava/lang/String;
public final fun getCachePreferences ()Lcom/posthog/PostHogPreferences;
public final fun getContext ()Lcom/posthog/PostHogContext;
Expand All @@ -76,6 +77,7 @@ public final class com/posthog/PostHogConfig {
public final fun getSdkVersion ()Ljava/lang/String;
public final fun getSendFeatureFlagEvent ()Z
public final fun getStoragePrefix ()Ljava/lang/String;
public final fun removeIntegration (Lcom/posthog/PostHogIntegration;)V
public final fun setCachePreferences (Lcom/posthog/PostHogPreferences;)V
public final fun setContext (Lcom/posthog/PostHogContext;)V
public final fun setDebug (Z)V
Expand Down

0 comments on commit d2577b1

Please sign in to comment.