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 removing the ViewTreeObserver when its not alive #134

Merged
merged 3 commits into from
May 24, 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Next

- allow anonymous uuid generation to be customizable ([#132](https://github.com/PostHog/posthog-android/pull/132))
- feat: allow anonymous uuid generation to be customizable ([#132](https://github.com/PostHog/posthog-android/pull/132))
- recording: fix removing the ViewTreeObserver when its not alive ([#134](https://github.com/PostHog/posthog-android/pull/134))

## 3.2.2 - 2024-05-21

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import com.posthog.android.internal.displayMetrics
import com.posthog.android.internal.screenSize
import com.posthog.android.replay.internal.NextDrawListener.Companion.onNextDraw
import com.posthog.android.replay.internal.ViewTreeSnapshotStatus
import com.posthog.android.replay.internal.isAliveAndAttachedToWindow
import com.posthog.internal.PostHogThreadFactory
import com.posthog.internal.replay.RRCustomEvent
import com.posthog.internal.replay.RREvent
Expand Down Expand Up @@ -247,13 +248,22 @@ public class PostHogReplayIntegration(
view: View,
status: ViewTreeSnapshotStatus,
) {
view.viewTreeObserver?.let { viewTreeObserver ->
if (viewTreeObserver.isAlive) {
mainHandler.handler.post {
viewTreeObserver.removeOnDrawListener(status.listener)
if (view.isAliveAndAttachedToWindow()) {
mainHandler.handler.post {
// 2nd check to avoid:
// Exception java.lang.IllegalStateException: This ViewTreeObserver is not alive
// Since the post might be executed a bit later if the thread is busy
if (view.isAliveAndAttachedToWindow()) {
try {
// swallow the exception because we still wanna remove it from the decorViews
view.viewTreeObserver?.removeOnDrawListener(status.listener)
} catch (e: Throwable) {
config.logger.log("Removing the viewTreeObserver failed: $e.")
}
}
}
}

view.phoneWindow?.let { window ->
window.touchEventInterceptors -= onTouchEventListener
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

package com.posthog.android.replay.internal

import android.os.Build
import android.view.View
import android.view.ViewTreeObserver
import com.posthog.android.internal.MainHandler
Expand All @@ -24,13 +23,8 @@ internal class NextDrawListener(
}

private fun safelyRegisterForNextDraw() {
// Prior to API 26, OnDrawListener wasn't merged back from the floating ViewTreeObserver into
// the real ViewTreeObserver.
// https://android.googlesource.com/platform/frameworks/base/+/9f8ec54244a5e0343b9748db3329733f259604f3
view.viewTreeObserver?.let { viewTreeObserver ->
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O || (viewTreeObserver.isAlive && view.isAttachedToWindow)) {
viewTreeObserver.addOnDrawListener(this)
}
if (view.isAliveAndAttachedToWindow()) {
view.viewTreeObserver?.addOnDrawListener(this)
}
}

Expand All @@ -48,3 +42,10 @@ internal class NextDrawListener(
}
}
}

internal fun View.isAliveAndAttachedToWindow(): Boolean {
// Prior to API 26, OnDrawListener wasn't merged back from the floating ViewTreeObserver into
// the real ViewTreeObserver.
// https://android.googlesource.com/platform/frameworks/base/+/9f8ec54244a5e0343b9748db3329733f259604f3
return viewTreeObserver?.isAlive == true && isAttachedToWindow
}
Loading