Skip to content

Commit

Permalink
Fix 'lateinit property currentTrigger has not been initialized' on WI…
Browse files Browse the repository at this point in the history
…KO devices
  • Loading branch information
pyricau committed Oct 25, 2024
1 parent d7335e8 commit 1a740db
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
4 changes: 2 additions & 2 deletions papa-main-trace/src/main/java/papa/MainThreadMessageSpy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,8 @@ object MainThreadMessageSpy {
// Looper can log to a printer before and after each message. We leverage this to surface the
// beginning and end of every main thread message in system traces. This costs a few extra string
// concatenations for each message handling.
// The printer is called before ('>>' prefix) and after ('<<' prefix) every message.
var before = false
Looper.getMainLooper().setMessageLogging { messageAsString ->
val before = messageAsString.startsWith('>')
if (before) {
currentMessageAsString = messageAsString
}
Expand All @@ -71,6 +70,7 @@ object MainThreadMessageSpy {
if (!before) {
currentMessageAsString = null
}
before = !before
}
}

Expand Down
14 changes: 9 additions & 5 deletions papa/src/main/java/papa/internal/MainThreadTriggerTracer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,27 @@ internal object MainThreadTriggerTracer {
private const val ASYNC_SECTION_LABEL = "Main Message Interaction"

fun install() {
lateinit var currentTrigger: InteractionTrigger
var currentTrigger: InteractionTrigger? = null
MainThreadMessageSpy.addTracer { _, before ->
if (before) {
val dispatchUptimeNanos = System.nanoTime()
val asyncTraceCookie = dispatchUptimeNanos.toInt()
SafeTrace.beginAsyncSection(ASYNC_SECTION_LABEL, asyncTraceCookie)
currentTrigger = SimpleInteractionTrigger(
val trigger = SimpleInteractionTrigger(
triggerUptime = dispatchUptimeNanos.nanoseconds,
name = "main-message",
interactionTrace = {
SafeTrace.endAsyncSection(ASYNC_SECTION_LABEL, asyncTraceCookie)
}
)
MainThreadTriggerStack.pushTriggeredBy(currentTrigger)
MainThreadTriggerStack.pushTriggeredBy(trigger)
currentTrigger = trigger
} else {
MainThreadTriggerStack.popTriggeredBy(currentTrigger)
currentTrigger.takeOverInteractionTrace()?.endTrace()
currentTrigger?.let { trigger ->
MainThreadTriggerStack.popTriggeredBy(trigger)
trigger.takeOverInteractionTrace()?.endTrace()
}
currentTrigger = null
}
}
}
Expand Down

0 comments on commit 1a740db

Please sign in to comment.