From 5ccd042d9ac7af57b5ed9072ed4349e11ab7956d Mon Sep 17 00:00:00 2001 From: Pierre-Yves Ricau Date: Wed, 11 Sep 2024 15:58:13 -0700 Subject: [PATCH] Not sure what's up with these compiled lambdas crashing at runtime.. --- papa/src/main/java/papa/InputEventTrigger.kt | 20 +++++++---- .../main/java/papa/InteractionRuleClient.kt | 36 ++++++++++--------- papa/src/main/java/papa/internal/Perfs.kt | 16 ++++++--- 3 files changed, 45 insertions(+), 27 deletions(-) diff --git a/papa/src/main/java/papa/InputEventTrigger.kt b/papa/src/main/java/papa/InputEventTrigger.kt index a32914e..5e8082d 100644 --- a/papa/src/main/java/papa/InputEventTrigger.kt +++ b/papa/src/main/java/papa/InputEventTrigger.kt @@ -15,7 +15,11 @@ class InputEventTrigger private constructor( get() = renderedUptime != null private val inputEventFrameRenderedCallbacks = - mutableListOf(OnFrameRenderedListener { renderedUptime = it }) + mutableListOf(object : OnFrameRenderedListener { + override fun onFrameRendered(frameRenderedUptime: Duration) { + renderedUptime = frameRenderedUptime + } + }) fun onInputEventFrameRendered(listener: OnFrameRenderedListener) { Handlers.checkOnMainThread() @@ -33,12 +37,16 @@ class InputEventTrigger private constructor( deliveryUptime: Duration ): InputEventTrigger { val trigger = InputEventTrigger(inputEvent, deliveryUptime) - inputEventWindow.postOnWindowFrameRendered { - for (callback in trigger.inputEventFrameRenderedCallbacks) { - callback.onFrameRendered(it) + // When compiling with Java11 we get AbstractMethodError at runtime when this is a lambda. + @Suppress("ObjectLiteralToLambda") + inputEventWindow.postOnWindowFrameRendered(object : OnFrameRenderedListener { + override fun onFrameRendered(frameRenderedUptime: Duration) { + for (callback in trigger.inputEventFrameRenderedCallbacks) { + callback.onFrameRendered(frameRenderedUptime) + } + trigger.inputEventFrameRenderedCallbacks.clear() } - trigger.inputEventFrameRenderedCallbacks.clear() - } + }) return trigger } } diff --git a/papa/src/main/java/papa/InteractionRuleClient.kt b/papa/src/main/java/papa/InteractionRuleClient.kt index 16f1d2d..a96de52 100644 --- a/papa/src/main/java/papa/InteractionRuleClient.kt +++ b/papa/src/main/java/papa/InteractionRuleClient.kt @@ -162,22 +162,26 @@ private class InteractionEngine( stopRunning() finishingInteractions += this recordEvent() - Choreographers.postOnFrameRendered { frameRenderedUptime -> - trace.endTrace() - choreographer.removeFrameCallback(this) - finishingInteractions -= this - val eventsCopy = sentEvents.toList() - resultListener.onInteractionResult( - Finished( - data = InteractionResultDataPayload( - interactionTrigger = interactionTrigger, - runningFrameCount = frameCountSinceStart, - sentEvents = eventsCopy, - ), - endFrameRenderedUptime = frameRenderedUptime + // When compiling with Java11 we get AbstractMethodError at runtime when this is a lambda. + @Suppress("ObjectLiteralToLambda") + Choreographers.postOnFrameRendered(object : OnFrameRenderedListener { + override fun onFrameRendered(frameRenderedUptime: Duration) { + trace.endTrace() + choreographer.removeFrameCallback(this@RealRunningInteraction) + finishingInteractions -= this@RealRunningInteraction + val eventsCopy = sentEvents.toList() + resultListener.onInteractionResult( + Finished( + data = InteractionResultDataPayload( + interactionTrigger = interactionTrigger, + runningFrameCount = frameCountSinceStart, + sentEvents = eventsCopy, + ), + endFrameRenderedUptime = frameRenderedUptime + ) ) - ) - } + } + }) return this } @@ -272,7 +276,7 @@ interface OnEventScope { return startInteraction( trigger = trigger, trace = trace - ).finish() + ).finish() } /** diff --git a/papa/src/main/java/papa/internal/Perfs.kt b/papa/src/main/java/papa/internal/Perfs.kt index 6c40bb5..aa83d82 100644 --- a/papa/src/main/java/papa/internal/Perfs.kt +++ b/papa/src/main/java/papa/internal/Perfs.kt @@ -26,6 +26,7 @@ import papa.AppVisibilityState.INVISIBLE import papa.AppVisibilityState.VISIBLE import papa.Choreographers import papa.Handlers +import papa.OnFrameRenderedListener import papa.PapaEvent.AppLaunch import papa.PapaEventListener import papa.PreLaunchState @@ -46,6 +47,7 @@ import papa.internal.LaunchedActivityStartingTransition.STARTED import papa.internal.MyProcess.ErrorRetrievingMyProcessData import papa.internal.MyProcess.MyProcessData import papa.internal.PerfsActivityLifecycleCallbacks.Companion.trackActivityLifecycle +import kotlin.time.Duration /** * Singleton object centralizing state for app start and future other perf metrics. @@ -418,11 +420,15 @@ internal object Perfs { return } reportedFullDrawn = true - Choreographers.postOnFrameRendered { frameRenderedUptime -> - appStartData = appStartData.copy( - firstFrameAfterFullyDrawnElapsedUptimeMillis = frameRenderedUptime.inWholeMilliseconds - appStartData.processStartUptimeMillis - ) - } + // When compiling with Java11 we get AbstractMethodError at runtime when this is a lambda. + @Suppress("ObjectLiteralToLambda") + Choreographers.postOnFrameRendered(object : OnFrameRenderedListener { + override fun onFrameRendered(frameRenderedUptime: Duration) { + appStartData = appStartData.copy( + firstFrameAfterFullyDrawnElapsedUptimeMillis = frameRenderedUptime.inWholeMilliseconds - appStartData.processStartUptimeMillis + ) + } + }) } fun customFirstEvent(