-
-
Notifications
You must be signed in to change notification settings - Fork 427
/
ViewUtils.java
97 lines (88 loc) · 3.25 KB
/
ViewUtils.java
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
package io.sentry.android.core.internal.gestures;
import android.content.res.Resources;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import io.sentry.android.core.SentryAndroidOptions;
import io.sentry.internal.gestures.GestureTargetLocator;
import io.sentry.internal.gestures.UiElement;
import io.sentry.util.Objects;
import java.util.LinkedList;
import java.util.Queue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
final class ViewUtils {
/**
* Finds a target view, that has been selected/clicked by the given coordinates x and y and the
* given {@code viewTargetSelector}.
*
* @param decorView - the root view of this window
* @param x - the x coordinate of a {@link MotionEvent}
* @param y - the y coordinate of {@link MotionEvent}
* @param targetType - the type of target to find
* @return the {@link View} that contains the touch coordinates and complements the {@code
* viewTargetSelector}
*/
static @Nullable UiElement findTarget(
final @NotNull SentryAndroidOptions options,
final @NotNull View decorView,
final float x,
final float y,
final UiElement.Type targetType) {
final Queue<View> queue = new LinkedList<>();
queue.add(decorView);
@Nullable UiElement target = null;
while (queue.size() > 0) {
final View view = Objects.requireNonNull(queue.poll(), "view is required");
if (view instanceof ViewGroup) {
final ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
queue.add(viewGroup.getChildAt(i));
}
}
for (GestureTargetLocator locator : options.getGestureTargetLocators()) {
final @Nullable UiElement newTarget = locator.locate(view, x, y, targetType);
if (newTarget != null) {
if (targetType == UiElement.Type.CLICKABLE) {
target = newTarget;
} else {
return newTarget;
}
}
}
}
return target;
}
/**
* Retrieves the human-readable view id based on {@code view.getContext().getResources()}, falls
* back to a hexadecimal id representation in case the view id is not available in the resources.
*
* @param view - the view that the id is being retrieved for.
* @return human-readable view id
*/
static String getResourceIdWithFallback(final @NotNull View view) {
final int viewId = view.getId();
try {
return getResourceId(view);
} catch (Resources.NotFoundException e) {
// fall back to hex representation of the id
return "0x" + Integer.toString(viewId, 16);
}
}
/**
* Retrieves the human-readable view id based on {@code view.getContext().getResources()}.
*
* @param view - the view whose id is being retrieved
* @return human-readable view id
* @throws Resources.NotFoundException in case the view id was not found
*/
static String getResourceId(final @NotNull View view) throws Resources.NotFoundException {
final int viewId = view.getId();
final Resources resources = view.getContext().getResources();
String resourceId = "";
if (resources != null) {
resourceId = resources.getResourceEntryName(viewId);
}
return resourceId;
}
}