|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") |
| 7 | + |
| 8 | +package io.opentelemetry.instrumentation.compose.click |
| 9 | + |
| 10 | +import android.view.MotionEvent |
| 11 | +import android.view.Window |
| 12 | +import androidx.compose.ui.node.LayoutNode |
| 13 | +import io.opentelemetry.api.common.Attributes |
| 14 | +import io.opentelemetry.api.incubator.logs.ExtendedLogRecordBuilder |
| 15 | +import io.opentelemetry.api.incubator.logs.ExtendedLogger |
| 16 | +import io.opentelemetry.semconv.incubating.AppIncubatingAttributes.APP_SCREEN_COORDINATE_X |
| 17 | +import io.opentelemetry.semconv.incubating.AppIncubatingAttributes.APP_SCREEN_COORDINATE_Y |
| 18 | +import io.opentelemetry.semconv.incubating.AppIncubatingAttributes.APP_WIDGET_ID |
| 19 | +import io.opentelemetry.semconv.incubating.AppIncubatingAttributes.APP_WIDGET_NAME |
| 20 | +import java.lang.ref.WeakReference |
| 21 | + |
| 22 | +internal class ComposeClickEventGenerator( |
| 23 | + private val eventLogger: ExtendedLogger, |
| 24 | + private val composeLayoutNodeUtil: ComposeLayoutNodeUtil = ComposeLayoutNodeUtil(), |
| 25 | + private val composeTapTargetDetector: ComposeTapTargetDetector = ComposeTapTargetDetector(composeLayoutNodeUtil), |
| 26 | +) { |
| 27 | + private var windowRef: WeakReference<Window>? = null |
| 28 | + |
| 29 | + fun startTracking(window: Window) { |
| 30 | + windowRef = WeakReference(window) |
| 31 | + val currentCallback = window.callback |
| 32 | + window.callback = WindowCallbackWrapper(currentCallback, this) |
| 33 | + } |
| 34 | + |
| 35 | + fun generateClick(motionEvent: MotionEvent?) { |
| 36 | + windowRef?.get()?.let { window -> |
| 37 | + if (motionEvent != null && motionEvent.actionMasked == MotionEvent.ACTION_UP) { |
| 38 | + createEvent(APP_SCREEN_CLICK_EVENT_NAME) |
| 39 | + .setAttribute(APP_SCREEN_COORDINATE_Y, motionEvent.y.toLong()) |
| 40 | + .setAttribute(APP_SCREEN_COORDINATE_X, motionEvent.x.toLong()) |
| 41 | + .emit() |
| 42 | + |
| 43 | + composeTapTargetDetector.findTapTarget(window.decorView, motionEvent.x, motionEvent.y)?.let { layoutNode -> |
| 44 | + createEvent(VIEW_CLICK_EVENT_NAME) |
| 45 | + .setAllAttributes(createNodeAttributes(layoutNode)) |
| 46 | + .emit() |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + fun stopTracking() { |
| 53 | + windowRef?.get()?.run { |
| 54 | + if (callback is WindowCallbackWrapper) { |
| 55 | + callback = (callback as WindowCallbackWrapper).unwrap() |
| 56 | + } |
| 57 | + } |
| 58 | + windowRef = null |
| 59 | + } |
| 60 | + |
| 61 | + private fun createEvent(name: String): ExtendedLogRecordBuilder = |
| 62 | + eventLogger |
| 63 | + .logRecordBuilder() |
| 64 | + .setEventName(name) |
| 65 | + |
| 66 | + private fun createNodeAttributes(node: LayoutNode): Attributes { |
| 67 | + val builder = Attributes.builder() |
| 68 | + builder.put(APP_WIDGET_NAME, composeTapTargetDetector.nodeToName(node)) |
| 69 | + builder.put(APP_WIDGET_ID, node.semanticsId.toString()) |
| 70 | + |
| 71 | + composeLayoutNodeUtil.getLayoutNodePositionInWindow(node)?.let { |
| 72 | + builder.put(APP_SCREEN_COORDINATE_X, it.x.toLong()) |
| 73 | + builder.put(APP_SCREEN_COORDINATE_Y, it.y.toLong()) |
| 74 | + } |
| 75 | + return builder.build() |
| 76 | + } |
| 77 | +} |
0 commit comments