Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recording: fix missing windowAttachCount method after minification #96

Merged
merged 3 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Next

- recording: fix missing windowAttachCount method after minification ([#96](https://github.com/PostHog/posthog-android/pull/96))

## 3.1.6 - 2024-02-01

- recording: clone drawable before using ([#91](https://github.com/PostHog/posthog-android/pull/91))
Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: clean compile stop checkFormat format api dryRelease release testReport test testJava
.PHONY: clean compile stop checkFormat format api dryRelease release testReport test testJava generateLintBaseLine

clean:
./gradlew clean
Expand Down Expand Up @@ -36,3 +36,7 @@ test:
# compile already runs the tests (tests only java)
testJava:
./gradlew :posthog:test

generateLintBaseLine:
rm -f posthog-android/lint-baseline.xml
./gradlew lintDebug -Dlint.baselines.continue=true
4 changes: 2 additions & 2 deletions buildSrc/src/main/java/PosthogBuildConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ object PosthogBuildConfig {

object Dependencies {
// runtime
val LIFECYCLE = "2.6.2"
val LIFECYCLE = "2.7.0"
val GSON = "2.10.1"
val OKHTTP = "4.12.0"
val CURTAINS = "1.2.4"
val CURTAINS = "1.2.5"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

val ANDROIDX_CORE = "1.5.0"

// tests
Expand Down
39 changes: 3 additions & 36 deletions posthog-android/lint-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,28 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<issues format="6" by="lint 8.1.3" type="baseline" client="gradle" dependencies="true" name="AGP (8.1.3)" variant="all" version="8.1.3">

<issue
id="GradleDependency"
message="A newer version of androidx.lifecycle:lifecycle-process than 2.6.2 is available: 2.7.0"
errorLine1=" implementation(&quot;androidx.lifecycle:lifecycle-process:${PosthogBuildConfig.Dependencies.LIFECYCLE}&quot;)"
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="build.gradle.kts"
line="83"
column="20"/>
</issue>

<issue
id="GradleDependency"
message="A newer version of androidx.lifecycle:lifecycle-common-java8 than 2.6.2 is available: 2.7.0"
errorLine1=" implementation(&quot;androidx.lifecycle:lifecycle-common-java8:${PosthogBuildConfig.Dependencies.LIFECYCLE}&quot;)"
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="build.gradle.kts"
line="84"
column="20"/>
</issue>

<issue
id="GradleDependency"
message="A newer version of androidx.core:core than 1.5.0 is available: 1.12.0"
Expand All @@ -36,23 +14,12 @@

<issue
id="GradleDependency"
message="A newer version of org.mockito:mockito-inline than 4.11.0 is available: 5.0.0"
errorLine1=" testImplementation(&quot;org.mockito:mockito-inline:${PosthogBuildConfig.Dependencies.MOCKITO_INLINE}&quot;)"
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="build.gradle.kts"
line="101"
column="24"/>
</issue>

<issue
id="GradleDependency"
message="A newer version of com.squareup.okhttp3:mockwebserver than 4.11.0 is available: 4.12.0"
errorLine1=" testImplementation(&quot;com.squareup.okhttp3:mockwebserver:${PosthogBuildConfig.Dependencies.OKHTTP}&quot;)"
message="A newer version of org.mockito.kotlin:mockito-kotlin than 4.1.0 is available: 5.0.0"
errorLine1=" testImplementation(&quot;org.mockito.kotlin:mockito-kotlin:${PosthogBuildConfig.Dependencies.MOCKITO}&quot;)"
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="build.gradle.kts"
line="103"
line="100"
column="24"/>
</issue>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,38 +93,50 @@ public class PostHogReplayIntegration(
get() = config.sessionReplay && PostHog.isSessionActive()

private val onRootViewsChangedListener = OnRootViewsChangedListener { view, added ->
if (added) {
view.phoneWindow?.let { window ->
if (view.windowAttachCount == 0) {
window.onDecorViewReady { decorView ->
val listener = decorView.onNextDraw(mainHandler, config.dateProvider) {
if (!isSessionReplayEnabled) {
return@onNextDraw
}
val timestamp = config.dateProvider.currentTimeMillis()

executor.submit {
generateSnapshot(WeakReference(decorView), timestamp)
try {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a few try/catch to be more defensive and degrade gracefully

if (added) {
view.phoneWindow?.let { window ->
if (view.windowAttachCount == 0) {
window.onDecorViewReady { decorView ->
try {
val listener = decorView.onNextDraw(mainHandler, config.dateProvider) {
if (!isSessionReplayEnabled) {
return@onNextDraw
}
val timestamp = config.dateProvider.currentTimeMillis()

executor.submit {
try {
generateSnapshot(WeakReference(decorView), timestamp)
} catch (e: Throwable) {
config.logger.log("Session Replay generateSnapshot failed: $e.")
}
}
}

val status = ViewTreeSnapshotStatus(listener)
decorViews[decorView] = status
} catch (e: Throwable) {
config.logger.log("Session Replay onDecorViewReady failed: $e.")
}
}

val status = ViewTreeSnapshotStatus(listener)
decorViews[decorView] = status
window.touchEventInterceptors += onTouchEventListener
// TODO: can check if user pressed hardware back button (KEYCODE_BACK)
// window.keyEventInterceptors
}

window.touchEventInterceptors += onTouchEventListener
// TODO: can check if user pressed hardware back button (KEYCODE_BACK)
// window.keyEventInterceptors
}
}
} else {
view.phoneWindow?.let { window ->
window.peekDecorView()?.let { decorView ->
decorViews[decorView]?.let { status ->
cleanSessionState(decorView, status)
} else {
view.phoneWindow?.let { window ->
window.peekDecorView()?.let { decorView ->
decorViews[decorView]?.let { status ->
cleanSessionState(decorView, status)
}
}
}
}
} catch (e: Throwable) {
config.logger.log("Session Replay OnRootViewsChangedListener failed: $e.")
}
}

Expand Down Expand Up @@ -220,14 +232,22 @@ public class PostHogReplayIntegration(
return
}

Curtains.onRootViewsChangedListeners += onRootViewsChangedListener
try {
Curtains.onRootViewsChangedListeners += onRootViewsChangedListener
} catch (e: Throwable) {
config.logger.log("Session Replay setup failed: $e.")
}
}

override fun uninstall() {
Curtains.onRootViewsChangedListeners -= onRootViewsChangedListener
try {
Curtains.onRootViewsChangedListeners -= onRootViewsChangedListener

decorViews.entries.forEach {
cleanSessionState(it.key, it.value)
decorViews.entries.forEach {
cleanSessionState(it.key, it.value)
}
} catch (e: Throwable) {
config.logger.log("Session Replay uninstall failed: $e.")
}
}

Expand Down