|
| 1 | +package com.instabug.reactlibrary.utils; |
| 2 | + |
| 3 | +import android.text.TextUtils; |
| 4 | +import android.view.View; |
| 5 | +import android.view.ViewParent; |
| 6 | +import androidx.annotation.NonNull; |
| 7 | +import androidx.annotation.Nullable; |
| 8 | +import com.facebook.react.views.text.ReactTextView; |
| 9 | +import com.facebook.react.views.view.ReactViewGroup; |
| 10 | +import com.instabug.library.core.InstabugCore; |
| 11 | +import com.instabug.library.visualusersteps.TouchedView; |
| 12 | +import com.instabug.library.visualusersteps.TouchedViewExtractor; |
| 13 | +import com.instabug.library.visualusersteps.VisualUserStepsHelper; |
| 14 | + |
| 15 | +public class RNTouchedViewExtractor implements TouchedViewExtractor { |
| 16 | + |
| 17 | + private final int depthTraversalLimit = 3; |
| 18 | + |
| 19 | + /** |
| 20 | + * Determines whether the native Android SDK should depend on native extraction |
| 21 | + * when a label is not found by the RNTouchedViewExtractor. |
| 22 | + * |
| 23 | + * <p> |
| 24 | + * - {@code RNTouchedViewExtractor} tries to find a label. |
| 25 | + * <br> |
| 26 | + * - If it returns a label, the view is labeled with the one returned. |
| 27 | + * <br> |
| 28 | + * - If it returns {@code null}: |
| 29 | + * <br> |
| 30 | + * - If {@code shouldDependOnNative} is {@code true}, the native Android SDK |
| 31 | + * will try to extract the label from the view. |
| 32 | + * <br> |
| 33 | + * - If it's {@code false}, the Android SDK will label it {@code null} as returned |
| 34 | + * from {@code RNTouchedViewExtractor} without trying to label it. |
| 35 | + * </p> |
| 36 | + * |
| 37 | + * @return {@code true} if the native Android SDK should depend on native extraction, |
| 38 | + * {@code false} otherwise. |
| 39 | + */ |
| 40 | + @Override |
| 41 | + public boolean getShouldDependOnNative() { |
| 42 | + return true; |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + @Nullable |
| 47 | + @Override |
| 48 | + public TouchedView extract(@NonNull View view, @NonNull TouchedView touchedView) { |
| 49 | + ReactViewGroup reactViewGroup = findReactButtonViewGroup(view); |
| 50 | + // If no button is found return `null` to leave the extraction of the touched view to the native Android SDK. |
| 51 | + if (reactViewGroup == null) return null; |
| 52 | + return getExtractionStrategy(reactViewGroup).extract(reactViewGroup, touchedView); |
| 53 | + } |
| 54 | + |
| 55 | + @Nullable |
| 56 | + private ReactViewGroup findReactButtonViewGroup(@NonNull View startView) { |
| 57 | + if (isReactButtonViewGroup(startView)) return (ReactViewGroup) startView; |
| 58 | + ViewParent currentParent = startView.getParent(); |
| 59 | + int depth = 1; |
| 60 | + do { |
| 61 | + if (currentParent == null || isReactButtonViewGroup(currentParent)) |
| 62 | + return (ReactViewGroup) currentParent; |
| 63 | + currentParent = currentParent.getParent(); |
| 64 | + depth++; |
| 65 | + } while (depth < depthTraversalLimit); |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + private boolean isReactButtonViewGroup(@NonNull View view) { |
| 70 | + return (view instanceof ReactViewGroup) && view.isFocusable() && view.isClickable(); |
| 71 | + } |
| 72 | + |
| 73 | + private boolean isReactButtonViewGroup(@NonNull ViewParent viewParent) { |
| 74 | + if (!(viewParent instanceof ReactViewGroup)) return false; |
| 75 | + ReactViewGroup group = (ReactViewGroup) viewParent; |
| 76 | + return group.isFocusable() && group.isClickable(); |
| 77 | + } |
| 78 | + |
| 79 | + private ReactButtonExtractionStrategy getExtractionStrategy(ReactViewGroup reactButton) { |
| 80 | + boolean isPrivateView = VisualUserStepsHelper.isPrivateView(reactButton); |
| 81 | + if (isPrivateView) return new PrivateViewLabelExtractionStrategy(); |
| 82 | + |
| 83 | + int labelsCount = 0; |
| 84 | + int groupsCount = 0; |
| 85 | + for (int index = 0; index < reactButton.getChildCount(); index++) { |
| 86 | + View currentView = reactButton.getChildAt(index); |
| 87 | + if (currentView instanceof ReactTextView) { |
| 88 | + |
| 89 | + labelsCount++; |
| 90 | + continue; |
| 91 | + } |
| 92 | + if (currentView instanceof ReactViewGroup) { |
| 93 | + groupsCount++; |
| 94 | + } |
| 95 | + } |
| 96 | + if (labelsCount > 1 || groupsCount > 0) return new MultiLabelsExtractionStrategy(); |
| 97 | + if (labelsCount == 1) return new SingleLabelExtractionStrategy(); |
| 98 | + return new NoLabelsExtractionStrategy(); |
| 99 | + } |
| 100 | + |
| 101 | + interface ReactButtonExtractionStrategy { |
| 102 | + @Nullable |
| 103 | + TouchedView extract(ReactViewGroup reactButton, TouchedView touchedView); |
| 104 | + } |
| 105 | + |
| 106 | + class MultiLabelsExtractionStrategy implements ReactButtonExtractionStrategy { |
| 107 | + private final String MULTI_LABEL_BUTTON_PRE_STRING = "a button that contains \"%s\""; |
| 108 | + |
| 109 | + @Override |
| 110 | + @Nullable |
| 111 | + public TouchedView extract(ReactViewGroup reactButton, TouchedView touchedView) { |
| 112 | + |
| 113 | + touchedView.setProminentLabel( |
| 114 | + InstabugCore.composeProminentLabelForViewGroup(reactButton, MULTI_LABEL_BUTTON_PRE_STRING) |
| 115 | + ); |
| 116 | + return touchedView; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + class PrivateViewLabelExtractionStrategy implements ReactButtonExtractionStrategy { |
| 121 | + |
| 122 | + private final String PRIVATE_VIEW_LABEL_BUTTON_PRE_STRING = "a button"; |
| 123 | + |
| 124 | + @Override |
| 125 | + public TouchedView extract(ReactViewGroup reactButton, TouchedView touchedView) { |
| 126 | + touchedView.setProminentLabel(PRIVATE_VIEW_LABEL_BUTTON_PRE_STRING); |
| 127 | + return touchedView; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + class SingleLabelExtractionStrategy implements ReactButtonExtractionStrategy { |
| 132 | + |
| 133 | + @Override |
| 134 | + public TouchedView extract(ReactViewGroup reactButton, TouchedView touchedView) { |
| 135 | + ReactTextView targetLabel = null; |
| 136 | + for (int index = 0; index < reactButton.getChildCount(); index++) { |
| 137 | + View currentView = reactButton.getChildAt(index); |
| 138 | + if (!(currentView instanceof ReactTextView)) continue; |
| 139 | + targetLabel = (ReactTextView) currentView; |
| 140 | + break; |
| 141 | + } |
| 142 | + if (targetLabel == null) return touchedView; |
| 143 | + |
| 144 | + String labelText = getLabelText(targetLabel); |
| 145 | + touchedView.setProminentLabel(InstabugCore.composeProminentLabelFor(labelText, false)); |
| 146 | + return touchedView; |
| 147 | + } |
| 148 | + |
| 149 | + @Nullable |
| 150 | + private String getLabelText(ReactTextView textView) { |
| 151 | + String labelText = null; |
| 152 | + if (!TextUtils.isEmpty(textView.getText())) { |
| 153 | + labelText = textView.getText().toString(); |
| 154 | + } else if (!TextUtils.isEmpty(textView.getContentDescription())) { |
| 155 | + labelText = textView.getContentDescription().toString(); |
| 156 | + } |
| 157 | + return labelText; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + class NoLabelsExtractionStrategy implements ReactButtonExtractionStrategy { |
| 162 | + @Override |
| 163 | + public TouchedView extract(ReactViewGroup reactButton, TouchedView touchedView) { |
| 164 | + return touchedView; |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments