Skip to content

Commit

Permalink
Add support for custom startInteraction start input events
Browse files Browse the repository at this point in the history
  • Loading branch information
pyricau committed Jul 27, 2024
1 parent 2cfaf47 commit c143526
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
2 changes: 1 addition & 1 deletion papa-dev/src/main/java/papa/InteractionOverlayView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class InteractionOverlayView<EventType : Any>(
val interactionLines =
trackedInteractionsWithFrameCount.map { (trackedInteraction, frameCount) ->
val input = trackedInteraction.interactionInput?.let { deliveredInput ->
when (val inputEvent = deliveredInput.event) {
when (val inputEvent = (deliveredInput as DeliveredInput<*>).event) {
is MotionEvent -> MotionEvent.actionToString(inputEvent.action)
is KeyEvent -> KeyEvent.keyCodeToString(inputEvent.keyCode)
else -> error("Unknown input event class ${inputEvent::class.java.name}")
Expand Down
21 changes: 13 additions & 8 deletions papa/api/papa.api
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,13 @@ public final class papa/AppVisibilityState : java/lang/Enum {
public static fun values ()[Lpapa/AppVisibilityState;
}

public final class papa/DeliveredInput {
public final class papa/DeliveredInput : papa/InteractionStartInput {
public synthetic fun <init> (Landroid/view/InputEvent;JJILkotlin/jvm/functions/Function0;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun getDeliveryUptime-UwyO8pc ()J
public final fun getEvent ()Landroid/view/InputEvent;
public final fun getEventUptime-UwyO8pc ()J
public final fun getFramesSinceDelivery ()I
public fun getInputUptime-UwyO8pc ()J
public final fun takeOverTraceEnd ()Lkotlin/jvm/functions/Function0;
public fun toString ()Ljava/lang/String;
}
Expand Down Expand Up @@ -237,7 +238,7 @@ public abstract interface class papa/InteractionEventSink {

public abstract class papa/InteractionResult : papa/InteractionResultData {
public synthetic fun <init> (Lpapa/InteractionResultData;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun getInteractionInput ()Lpapa/DeliveredInput;
public fun getInteractionInput ()Lpapa/InteractionStartInput;
public fun getRunningFrameCount ()I
public fun getSentEvents ()Ljava/util/List;
public fun toString ()Ljava/lang/String;
Expand All @@ -257,14 +258,14 @@ public final class papa/InteractionResult$Finished : papa/InteractionResult {
}

public abstract interface class papa/InteractionResultData {
public abstract fun getInteractionInput ()Lpapa/DeliveredInput;
public abstract fun getInteractionInput ()Lpapa/InteractionStartInput;
public abstract fun getRunningFrameCount ()I
public abstract fun getSentEvents ()Ljava/util/List;
}

public final class papa/InteractionResultDataPayload : papa/InteractionResultData {
public fun <init> (Lpapa/DeliveredInput;ILjava/util/List;)V
public fun getInteractionInput ()Lpapa/DeliveredInput;
public fun <init> (Lpapa/InteractionStartInput;ILjava/util/List;)V
public fun getInteractionInput ()Lpapa/InteractionStartInput;
public fun getRunningFrameCount ()I
public fun getSentEvents ()Ljava/util/List;
}
Expand All @@ -289,6 +290,10 @@ public final class papa/InteractionScope {
public final fun getOnEventCallbacks ()Ljava/util/List;
}

public abstract interface class papa/InteractionStartInput {
public abstract fun getInputUptime-UwyO8pc ()J
}

public abstract interface class papa/InteractionTrace {
public static final field Companion Lpapa/InteractionTrace$Companion;
public abstract fun endTrace ()V
Expand All @@ -307,7 +312,7 @@ public abstract interface class papa/OnEventScope {
public abstract fun getInteractionInput ()Lpapa/DeliveredInput;
public abstract fun recordSingleFrameInteraction (Lpapa/InteractionTrace;)Lpapa/FinishingInteraction;
public abstract fun runningInteractions ()Ljava/util/List;
public abstract fun startInteraction-HG0u8IE (Lpapa/InteractionTrace;J)Lpapa/RunningInteraction;
public abstract fun startInteraction-SxA4cEA (Lpapa/InteractionStartInput;Lpapa/InteractionTrace;J)Lpapa/RunningInteraction;
}

public final class papa/OnEventScope$DefaultImpls {
Expand All @@ -316,7 +321,7 @@ public final class papa/OnEventScope$DefaultImpls {
public static synthetic fun cancelRunningInteractions$default (Lpapa/OnEventScope;Ljava/lang/String;ILjava/lang/Object;)V
public static fun recordSingleFrameInteraction (Lpapa/OnEventScope;Lpapa/InteractionTrace;)Lpapa/FinishingInteraction;
public static synthetic fun recordSingleFrameInteraction$default (Lpapa/OnEventScope;Lpapa/InteractionTrace;ILjava/lang/Object;)Lpapa/FinishingInteraction;
public static synthetic fun startInteraction-HG0u8IE$default (Lpapa/OnEventScope;Lpapa/InteractionTrace;JILjava/lang/Object;)Lpapa/RunningInteraction;
public static synthetic fun startInteraction-SxA4cEA$default (Lpapa/OnEventScope;Lpapa/InteractionStartInput;Lpapa/InteractionTrace;JILjava/lang/Object;)Lpapa/RunningInteraction;
}

public abstract class papa/PapaEvent {
Expand Down Expand Up @@ -431,7 +436,7 @@ public final class papa/SentEvent {
}

public abstract interface class papa/TrackedInteraction {
public abstract fun getInteractionInput ()Lpapa/DeliveredInput;
public abstract fun getInteractionInput ()Lpapa/InteractionStartInput;
public abstract fun getSentEvents ()Ljava/util/List;
public abstract fun recordEvent ()V
}
Expand Down
5 changes: 4 additions & 1 deletion papa/src/main/java/papa/InputTracker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class DeliveredInput<InputEventType : InputEvent>(
val eventUptime: Duration,
val framesSinceDelivery: Int,
private var endTrace: (() -> Unit)?
) {
) : InteractionStartInput {

fun takeOverTraceEnd(): (() -> Unit)? {
val transferedEndTrace = endTrace
Expand All @@ -65,6 +65,9 @@ class DeliveredInput<InputEventType : InputEvent>(
return copy
}

override val inputUptime: Duration
get() = eventUptime

override fun toString(): String {
return "DeliveredInput(" +
"deliveryUptime=${deliveryUptime.toString(MILLISECONDS)}, " +
Expand Down
20 changes: 13 additions & 7 deletions papa/src/main/java/papa/InteractionRuleClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private class InteractionEngine<ParentEventType : Any>(
private var eventInScope: SentEvent<ParentEventType>? = null

inner class RealRunningInteraction(
override val interactionInput: DeliveredInput<out InputEvent>?,
override val interactionInput: InteractionStartInput?,
private val trace: InteractionTrace,
cancelTimeout: Duration
) : RunningInteraction<ParentEventType>, FinishingInteraction<ParentEventType>, FrameCallback {
Expand Down Expand Up @@ -224,13 +224,17 @@ private class InteractionEngine<ParentEventType : Any>(
override val interactionInput = interactionInput

override fun startInteraction(
interactionStartInput: InteractionStartInput?,
trace: InteractionTrace,
cancelTimeout: Duration,
): RunningInteraction<ParentEventType> {
// If the interaction input trace end isn't taken over yet, end it.
interactionInput?.takeOverTraceEnd()?.invoke()

val runningInteraction = RealRunningInteraction(
interactionInput = interactionInput,
// Default to custom interactionStartInput if passed in, otherwise default to delivered
// input (key or tap)
interactionInput = interactionStartInput ?: interactionInput,
trace = trace,
cancelTimeout
)
Expand Down Expand Up @@ -265,6 +269,7 @@ interface OnEventScope<ParentEventType : Any, EventType : ParentEventType> {
val event: EventType

fun startInteraction(
interactionStartInput: InteractionStartInput? = null,
trace: InteractionTrace = InteractionTrace.fromInputDelivered(event, interactionInput),
cancelTimeout: Duration = 1.minutes,
): RunningInteraction<ParentEventType>
Expand All @@ -277,7 +282,7 @@ interface OnEventScope<ParentEventType : Any, EventType : ParentEventType> {
fun recordSingleFrameInteraction(
trace: InteractionTrace = InteractionTrace.fromInputDelivered(event, interactionInput),
): FinishingInteraction<ParentEventType> {
return startInteraction(trace).finish()
return startInteraction(trace = trace).finish()
}

/**
Expand Down Expand Up @@ -334,7 +339,7 @@ fun interface InteractionTrace {

interface TrackedInteraction<EventType : Any> {
val sentEvents: List<SentEvent<EventType>>
val interactionInput: DeliveredInput<out InputEvent>?
val interactionInput: InteractionStartInput?

/**
* Adds the current event instance to the list of events (if not already added).
Expand All @@ -356,7 +361,7 @@ interface InteractionResultData<EventType : Any> {
* Interaction input that was automatically detected when the interaction started to be tracked,
* if any.
*/
val interactionInput: DeliveredInput<out InputEvent>?
val interactionInput: InteractionStartInput?

/**
* The number of frames that were rendered between the first and the last event in [sentEvents]
Expand All @@ -372,7 +377,7 @@ class SentEvent<EventType : Any>(
)

class InteractionResultDataPayload<EventType : Any>(
override val interactionInput: DeliveredInput<out InputEvent>?,
override val interactionInput: InteractionStartInput?,
override val runningFrameCount: Int,
override val sentEvents: List<SentEvent<EventType>>,
) : InteractionResultData<EventType>
Expand Down Expand Up @@ -414,6 +419,7 @@ sealed class InteractionResult<EventType : Any>(
is Canceled<*> -> "cancelReason=\"$cancelReason\", startToCancel=${
startToCancel.toString(MILLISECONDS)
}, "

is Finished<*> -> "startToEndFrameRendered=${
startToEndFrameRendered.toString(MILLISECONDS)
}, "
Expand All @@ -424,7 +430,7 @@ sealed class InteractionResult<EventType : Any>(
interactionInput?.let {
append(
"inputToStart=${
(sentEvents.first().uptime - it.eventUptime).toString(
(sentEvents.first().uptime - it.inputUptime).toString(
MILLISECONDS
)
}, "
Expand Down
7 changes: 7 additions & 0 deletions papa/src/main/java/papa/InteractionStartInput.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package papa

import kotlin.time.Duration

interface InteractionStartInput {
val inputUptime: Duration
}

0 comments on commit c143526

Please sign in to comment.