-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathComposeGestureTargetLocator.java
More file actions
164 lines (146 loc) · 6.23 KB
/
ComposeGestureTargetLocator.java
File metadata and controls
164 lines (146 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package io.sentry.compose.gestures;
import androidx.compose.ui.Modifier;
import androidx.compose.ui.geometry.Rect;
import androidx.compose.ui.layout.ModifierInfo;
import androidx.compose.ui.node.LayoutNode;
import androidx.compose.ui.node.Owner;
import androidx.compose.ui.semantics.SemanticsConfiguration;
import androidx.compose.ui.semantics.SemanticsModifier;
import androidx.compose.ui.semantics.SemanticsPropertyKey;
import io.sentry.ILogger;
import io.sentry.ISentryLifecycleToken;
import io.sentry.SentryIntegrationPackageStorage;
import io.sentry.compose.SentryComposeHelper;
import io.sentry.compose.helper.BuildConfig;
import io.sentry.internal.gestures.GestureTargetLocator;
import io.sentry.internal.gestures.UiElement;
import io.sentry.util.AutoClosableReentrantLock;
import java.lang.reflect.Field;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@SuppressWarnings("KotlinInternalInJava")
public final class ComposeGestureTargetLocator implements GestureTargetLocator {
private static final String ORIGIN = "jetpack_compose";
private final @NotNull ILogger logger;
private volatile @Nullable SentryComposeHelper composeHelper;
private final @NotNull AutoClosableReentrantLock lock = new AutoClosableReentrantLock();
static {
SentryIntegrationPackageStorage.getInstance()
.addPackage("maven:io.sentry:sentry-compose", BuildConfig.VERSION_NAME);
}
public ComposeGestureTargetLocator(final @NotNull ILogger logger) {
this.logger = logger;
SentryIntegrationPackageStorage.getInstance().addIntegration("ComposeUserInteraction");
SentryIntegrationPackageStorage.getInstance()
.addPackage("maven:io.sentry:sentry-compose", BuildConfig.VERSION_NAME);
}
@Override
public @Nullable UiElement locate(
@Nullable Object root, float x, float y, UiElement.Type targetType) {
// lazy init composeHelper as it's using some reflection under the hood
if (composeHelper == null) {
try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) {
if (composeHelper == null) {
composeHelper = new SentryComposeHelper(logger);
}
}
}
if (!(root instanceof Owner)) {
return null;
}
final @NotNull Queue<LayoutNode> queue = new LinkedList<>();
queue.add(((Owner) root).getRoot());
// the final tag to return
@Nullable String targetTag = null;
// the last known tag when iterating the node tree
@Nullable String lastKnownTag = null;
while (!queue.isEmpty()) {
final @Nullable LayoutNode node = queue.poll();
if (node == null) {
continue;
}
if (node.isPlaced() && layoutNodeBoundsContain(composeHelper, node, x, y)) {
boolean isClickable = false;
boolean isScrollable = false;
final List<ModifierInfo> modifiers = node.getModifierInfo();
for (ModifierInfo modifierInfo : modifiers) {
if (modifierInfo.getModifier() instanceof SemanticsModifier) {
final SemanticsModifier semanticsModifierCore =
(SemanticsModifier) modifierInfo.getModifier();
final SemanticsConfiguration semanticsConfiguration =
semanticsModifierCore.getSemanticsConfiguration();
for (Map.Entry<? extends SemanticsPropertyKey<?>, ?> entry : semanticsConfiguration) {
final @Nullable String key = entry.getKey().getName();
if ("ScrollBy".equals(key)) {
isScrollable = true;
} else if ("OnClick".equals(key)) {
isClickable = true;
} else if ("SentryTag".equals(key) || "TestTag".equals(key)) {
if (entry.getValue() instanceof String) {
lastKnownTag = (String) entry.getValue();
}
}
}
} else {
final @NotNull Modifier modifier = modifierInfo.getModifier();
// Newer Jetpack Compose 1.5 uses Node modifiers for clicks/scrolls
final @Nullable String type = modifier.getClass().getCanonicalName();
if ("androidx.compose.foundation.ClickableElement".equals(type)
|| "androidx.compose.foundation.CombinedClickableElement".equals(type)) {
isClickable = true;
} else if ("androidx.compose.foundation.ScrollingLayoutElement".equals(type)) {
isScrollable = true;
} else if ("androidx.compose.ui.platform.TestTagElement".equals(type)) {
// Newer Jetpack Compose uses TestTagElement as node elements
// See
// https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/ui/ui/src/commonMain/kotlin/androidx/compose/ui/platform/TestTag.kt;l=34;drc=dcaa116fbfda77e64a319e1668056ce3b032469f
try {
final Field tagField = modifier.getClass().getDeclaredField("tag");
tagField.setAccessible(true);
final @Nullable Object value = tagField.get(modifier);
if (value instanceof String) {
lastKnownTag = (String) value;
}
} catch (Throwable e) {
// ignored
}
}
}
}
if (isClickable && targetType == UiElement.Type.CLICKABLE) {
targetTag = lastKnownTag;
}
if (isScrollable && targetType == UiElement.Type.SCROLLABLE) {
targetTag = lastKnownTag;
// skip any children for scrollable targets
break;
}
}
queue.addAll(node.getZSortedChildren().asMutableList());
}
if (targetTag == null) {
return null;
} else {
return new UiElement(null, null, null, targetTag, ORIGIN);
}
}
private static boolean layoutNodeBoundsContain(
@NotNull SentryComposeHelper composeHelper,
@NotNull LayoutNode node,
final float x,
final float y) {
final @Nullable Rect bounds = composeHelper.getLayoutNodeBoundsInWindow(node);
if (bounds == null) {
return false;
} else {
return x >= bounds.getLeft()
&& x <= bounds.getRight()
&& y >= bounds.getTop()
&& y <= bounds.getBottom();
}
}
}