Skip to content

fix: compose autocapture properties update #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,24 @@ internal object ViewHierarchyScanner {
if (view is ViewGroup) {
queue.addAll(view.children)
}

// Applies the locators until a target is found. If the target type is clickable, check
// the children in case the target is a child which is also clickable.
viewTargetLocators.any { locator ->
with(locator) {
view.locate(targetPosition, targetType)?.let { newTarget ->
if (targetType == ViewTarget.Type.Clickable) {
target = newTarget
return@any true
} else {
return newTarget
try {
// Applies the locators until a target is found. If the target type is clickable, check
// the children in case the target is a child which is also clickable.
viewTargetLocators.any { locator ->
with(locator) {
view.locate(targetPosition, targetType)?.let { newTarget ->
if (targetType == ViewTarget.Type.Clickable) {
target = newTarget
return@any true
} else {
return newTarget
}
}
false
}
false
}
} catch (e: ClassCastException) {
logger.error("Error while locating target in view hierarchy: $e")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import androidx.annotation.OptIn;
import androidx.compose.ui.InternalComposeUiApi;
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 androidx.compose.ui.platform.InspectableValue;
import androidx.compose.ui.platform.ValueElement;

import com.amplitude.android.internal.ViewTarget;
import com.amplitude.common.Logger;
Expand All @@ -17,6 +17,8 @@
import org.jetbrains.annotations.Nullable;

import java.util.ArrayDeque;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
Expand Down Expand Up @@ -72,27 +74,44 @@ public ViewTarget locate(
boolean isClickable = 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 ("OnClick".equals(key)) {
isClickable = true;
} else if ("TestTag".equals(key)) {
if (entry.getValue() instanceof String) {
lastKnownTag = (String) entry.getValue();
final Modifier modifier = modifierInfo.getModifier();
if (modifier instanceof InspectableValue) {
final InspectableValue inspectableValue = (InspectableValue) modifier;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this might be a good time to convert this to Kotlin too, to get inferred types make it more succinct

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, bcos this is a java hack for reflection

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand, it seems we don't use reflection here specifically?

if ("testTag".equals(inspectableValue.getNameFallback())) {
Iterator<ValueElement> iterator = inspectableValue.getInspectableElements().iterator();
while (iterator.hasNext()) {
final ValueElement element = iterator.next();
if ("tag".equals(element.getName())) {
lastKnownTag = (String) element.getValue();
break;
}
}
} else if ("semantics".equals(inspectableValue.getNameFallback())) {
Iterator<ValueElement> iterator = inspectableValue.getInspectableElements().iterator();
while (iterator.hasNext()) {
final ValueElement element = iterator.next();
if ("properties".equals(element.getName())) {
final Object elementValue = element.getValue();
if (elementValue instanceof LinkedHashMap) {
final LinkedHashMap<Object, Object> properties = (LinkedHashMap<Object, Object>) elementValue;
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
if ("TestTag".equals(entry.getKey())) {
lastKnownTag = (String) entry.getValue();
break;
}
}
}
}
}
}
} else {
// Newer Jetpack Compose 1.5 uses Node modifiers for clicks/scrolls
final @Nullable String type = modifierInfo.getModifier().getClass().getCanonicalName();
if ("androidx.compose.foundation.ClickableElement".equals(type)
|| "androidx.compose.foundation.CombinedClickableElement".equals(type)) {
if ("clickable".equals(inspectableValue.getNameFallback())) {
isClickable = true;
} else {
final @Nullable String type = modifier.getClass().getCanonicalName();
if ("androidx.compose.foundation.ClickableElement".equals(type)
|| "androidx.compose.foundation.CombinedClickableElement".equals(type)) {
isClickable = true;
}
}
}
}
Expand Down