content: package androidx.customview.widget;
  
  import android.graphics.Rect;
  import android.os.Bundle;
  import android.view.KeyEvent;
  import android.view.MotionEvent;
  import android.view.View;
  import android.view.ViewParent;
  import android.view.accessibility.AccessibilityEvent;
  import android.view.accessibility.AccessibilityManager;
  import androidx.collection.SparseArrayCompat;
  import androidx.constraintlayout.widget.ConstraintLayout;
  import androidx.core.view.AccessibilityDelegateCompat;
  import androidx.core.view.ViewCompat;
  import androidx.core.view.accessibility.AccessibilityEventCompat;
  import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
  import androidx.core.view.accessibility.AccessibilityNodeProviderCompat;
  import androidx.core.view.accessibility.AccessibilityRecordCompat;
  import androidx.customview.widget.FocusStrategy;
  import java.util.ArrayList;
  import java.util.List;
  import kotlinx.coroutines.internal.LockFreeTaskQueueCore;
  
  public abstract class ExploreByTouchHelper extends AccessibilityDelegateCompat {
      private static final String DEFAULT_CLASS_NAME = "android.view.View";
      public static final int HOST_ID = -1;
      public static final int INVALID_ID = Integer.MIN_VALUE;
      private static final Rect INVALID_PARENT_BOUNDS = new Rect(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE);
      private static final FocusStrategy.BoundsAdapter<AccessibilityNodeInfoCompat> NODE_ADAPTER = new FocusStrategy.BoundsAdapter<AccessibilityNodeInfoCompat>() {
          public void obtainBounds(AccessibilityNodeInfoCompat node, Rect outBounds) {
              node.getBoundsInParent(outBounds);
          }
      };
      private static final FocusStrategy.CollectionAdapter<SparseArrayCompat<AccessibilityNodeInfoCompat>, AccessibilityNodeInfoCompat> SPARSE_VALUES_ADAPTER = new FocusStrategy.CollectionAdapter<SparseArrayCompat<AccessibilityNodeInfoCompat>, AccessibilityNodeInfoCompat>() {
          public AccessibilityNodeInfoCompat get(SparseArrayCompat<AccessibilityNodeInfoCompat> sparseArrayCompat, int index) {
              return sparseArrayCompat.valueAt(index);
          }
  
          public int size(SparseArrayCompat<AccessibilityNodeInfoCompat> sparseArrayCompat) {
              return sparseArrayCompat.size();
          }
      };
      int mAccessibilityFocusedVirtualViewId = Integer.MIN_VALUE;
      private final View mHost;
      private int mHoveredVirtualViewId = Integer.MIN_VALUE;
      int mKeyboardFocusedVirtualViewId = Integer.MIN_VALUE;
      private final AccessibilityManager mManager;
      private MyNodeProvider mNodeProvider;
      private final int[] mTempGlobalRect = new int[2];
      private final Rect mTempParentRect = new Rect();
      private final Rect mTempScreenRect = new Rect();
      private final Rect mTempVisibleRect = new Rect();
  
      private class MyNodeProvider extends AccessibilityNodeProviderCompat {
          MyNodeProvider() {
          }
  
          public AccessibilityNodeInfoCompat createAccessibilityNodeInfo(int virtualViewId) {
              return AccessibilityNodeInfoCompat.obtain(ExploreByTouchHelper.this.obtainAccessibilityNodeInfo(virtualViewId));
          }
  
          public AccessibilityNodeInfoCompat findFocus(int focusType) {
              int i = focusType == 2 ? ExploreByTouchHelper.this.mAccessibilityFocusedVirtualViewId : ExploreByTouchHelper.this.mKeyboardFocusedVirtualViewId;
              if (i == Integer.MIN_VALUE) {
                  return null;
              }
              return createAccessibilityNodeInfo(i);
          }
  
          public boolean performAction(int virtualViewId, int action, Bundle arguments) {
              return ExploreByTouchHelper.this.performAction(virtualViewId, action, arguments);
          }
      }
  
      public ExploreByTouchHelper(View host) {
          if (host != null) {
              this.mHost = host;
              this.mManager = (AccessibilityManager) host.getContext().getSystemService("accessibility");
              host.setFocusable(true);
              if (ViewCompat.getImportantForAccessibility(host) == 0) {
                  ViewCompat.setImportantForAccessibility(host, 1);
                  return;
              }
              return;
          }
          throw new IllegalArgumentException("View may not be null");
      }
  
      private boolean clearAccessibilityFocus(int virtualViewId) {
          if (this.mAccessibilityFocusedVirtualViewId != virtualViewId) {
              return false;
          }
          this.mAccessibilityFocusedVirtualViewId = Integer.MIN_VALUE;
          this.mHost.invalidate();
          sendEventForVirtualView(virtualViewId, 65536);
          return true;
      }
  
      private boolean clickKeyboardFocusedVirtualView() {
          int i = this.mKeyboardFocusedVirtualViewId;
          return i != Integer.MIN_VALUE && onPerformActionForVirtualView(i, 16, (Bundle) null);
      }
  
      private AccessibilityEvent createEvent(int virtualViewId, int eventType) {
          switch (virtualViewId) {
              case -1:
                  return createEventForHost(eventType);
              default:
                  return createEventForChild(virtualViewId, eventType);
          }
      }
  
      private AccessibilityEvent createEventForChild(int virtualViewId, int eventType) {
          AccessibilityEvent obtain = AccessibilityEvent.obtain(eventType);
          AccessibilityNodeInfoCompat obtainAccessibilityNodeInfo = obtainAccessibilityNodeInfo(virtualViewId);
          obtain.getText().add(obtainAccessibilityNodeInfo.getText());
          obtain.setContentDescription(obtainAccessibilityNodeInfo.getContentDescription());
          obtain.setScrollable(obtainAccessibilityNodeInfo.isScrollable());
          obtain.setPassword(obtainAccessibilityNodeInfo.isPassword());
          obtain.setEnabled(obtainAccessibilityNodeInfo.isEnabled());
          obtain.setChecked(obtainAccessibilityNodeInfo.isChecked());
          onPopulateEventForVirtualView(virtualViewId, obtain);
          if (!obtain.getText().isEmpty() || obtain.getContentDescription() != null) {
              obtain.setClassName(obtainAccessibilityNodeInfo.getClassName());
              AccessibilityRecordCompat.setSource(obtain, this.mHost, virtualViewId);
              obtain.setPackageName(this.mHost.getContext().getPackageName());
              return obtain;
          }
          throw new RuntimeException("Callbacks must add text or a content description in populateEventForVirtualViewId()");
      }
  
      private AccessibilityEvent createEventForHost(int eventType) {
          AccessibilityEvent obtain = AccessibilityEvent.obtain(eventType);
          this.mHost.onInitializeAccessibilityEvent(obtain);
          return obtain;
      }
  
      private AccessibilityNodeInfoCompat createNodeForChild(int virtualViewId) {
          AccessibilityNodeInfoCompat obtain = AccessibilityNodeInfoCompat.obtain();
          obtain.setEnabled(true);
          obtain.setFocusable(true);
          obtain.setClassName(DEFAULT_CLASS_NAME);
          Rect rect = INVALID_PARENT_BOUNDS;
          obtain.setBoundsInParent(rect);
          obtain.setBoundsInScreen(rect);
          obtain.setParent(this.mHost);
          onPopulateNodeForVirtualView(virtualViewId, obtain);
          if (obtain.getText() == null && obtain.getContentDescription() == null) {
              throw new RuntimeException("Callbacks must add text or a content description in populateNodeForVirtualViewId()");
          }
          obtain.getBoundsInParent(this.mTempParentRect);
          if (!this.mTempParentRect.equals(rect)) {
              int actions = obtain.getActions();
              if ((actions & 64) != 0) {
                  throw new RuntimeException("Callbacks must not add ACTION_ACCESSIBILITY_FOCUS in populateNodeForVirtualViewId()");
              } else if ((actions & 128) == 0) {
                  obtain.setPackageName(this.mHost.getContext().getPackageName());
                  obtain.setSource(this.mHost, virtualViewId);
                  if (this.mAccessibilityFocusedVirtualViewId == virtualViewId) {
                      obtain.setAccessibilityFocused(true);
                      obtain.addAction(128);
                  } else {
                      obtain.setAccessibilityFocused(false);
                      obtain.addAction(64);
                  }
                  boolean z = this.mKeyboardFocusedVirtualViewId == virtualViewId;
                  if (z) {
                      obtain.addAction(2);
                  } else if (obtain.isFocusable()) {
                      obtain.addAction(1);
                  }
                  obtain.setFocused(z);
                  this.mHost.getLocationOnScreen(this.mTempGlobalRect);
                  obtain.getBoundsInScreen(this.mTempScreenRect);
                  if (this.mTempScreenRect.equals(rect)) {
                      obtain.getBoundsInParent(this.mTempScreenRect);
                      if (obtain.mParentVirtualDescendantId != -1) {
                          AccessibilityNodeInfoCompat obtain2 = AccessibilityNodeInfoCompat.obtain();
                          for (int i = obtain.mParentVirtualDescendantId; i != -1; i = obtain2.mParentVirtualDescendantId) {
                              obtain2.setParent(this.mHost, -1);
                              obtain2.setBoundsInParent(INVALID_PARENT_BOUNDS);
                              onPopulateNodeForVirtualView(i, obtain2);
                              obtain2.getBoundsInParent(this.mTempParentRect);
                              this.mTempScreenRect.offset(this.mTempParentRect.left, this.mTempParentRect.top);
                          }
                          obtain2.recycle();
                      }
                      this.mTempScreenRect.offset(this.mTempGlobalRect[0] - this.mHost.getScrollX(), this.mTempGlobalRect[1] - this.mHost.getScrollY());
                  }
                  if (this.mHost.getLocalVisibleRect(this.mTempVisibleRect)) {
                      this.mTempVisibleRect.offset(this.mTempGlobalRect[0] - this.mHost.getScrollX(), this.mTempGlobalRect[1] - this.mHost.getScrollY());
                      if (this.mTempScreenRect.intersect(this.mTempVisibleRect)) {
                          obtain.setBoundsInScreen(this.mTempScreenRect);
                          if (isVisibleToUser(this.mTempScreenRect)) {
                              obtain.setVisibleToUser(true);
                          }
                      }
                  }
                  return obtain;
              } else {
                  throw new RuntimeException("Callbacks must not add ACTION_CLEAR_ACCESSIBILITY_FOCUS in populateNodeForVirtualViewId()");
              }
          } else {
              throw new RuntimeException("Callbacks must set parent bounds in populateNodeForVirtualViewId()");
          }
      }
  
      private AccessibilityNodeInfoCompat createNodeForHost() {
          AccessibilityNodeInfoCompat obtain = AccessibilityNodeInfoCompat.obtain(this.mHost);
          ViewCompat.onInitializeAccessibilityNodeInfo(this.mHost, obtain);
          ArrayList arrayList = new ArrayList();
          getVisibleVirtualViews(arrayList);
          if (obtain.getChildCount() <= 0 || arrayList.size() <= 0) {
              int size = arrayList.size();
              for (int i = 0; i < size; i++) {
                  obtain.addChild(this.mHost, ((Integer) arrayList.get(i)).intValue());
              }
              return obtain;
          }
          throw new RuntimeException("Views cannot have both real and virtual children");
      }
  
      private SparseArrayCompat<AccessibilityNodeInfoCompat> getAllNodes() {
          ArrayList arrayList = new ArrayList();
          getVisibleVirtualViews(arrayList);
          SparseArrayCompat<AccessibilityNodeInfoCompat> sparseArrayCompat = new SparseArrayCompat<>();
          for (int i = 0; i < arrayList.size(); i++) {
              sparseArrayCompat.put(((Integer) arrayList.get(i)).intValue(), createNodeForChild(((Integer) arrayList.get(i)).intValue()));
          }
          return sparseArrayCompat;
      }
  
      private void getBoundsInParent(int virtualViewId, Rect outBounds) {
          obtainAccessibilityNodeInfo(virtualViewId).getBoundsInParent(outBounds);
      }
  
      private static Rect guessPreviouslyFocusedRect(View host, int direction, Rect outBounds) {
          int width = host.getWidth();
          int height = host.getHeight();
          switch (direction) {
              case 17:
                  outBounds.set(width, 0, width, height);
                  break;
              case 33:
                  outBounds.set(0, height, width, height);
                  break;
              case ConstraintLayout.LayoutParams.Table.LAYOUT_WRAP_BEHAVIOR_IN_PARENT:
                  outBounds.set(-1, 0, -1, height);
                  break;
              case 130:
                  outBounds.set(0, -1, width, -1);
                  break;
              default:
                  throw new IllegalArgumentException("direction must be one of {FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT}.");
          }
          return outBounds;
      }
  
      private boolean isVisibleToUser(Rect localRect) {
          if (localRect == null || localRect.isEmpty() || this.mHost.getWindowVisibility() != 0) {
              return false;
          }
          ViewParent parent = this.mHost.getParent();
          while (parent instanceof View) {
              View view = (View) parent;
              if (view.getAlpha() <= 0.0f || view.getVisibility() != 0) {
                  return false;
              }
              parent = view.getParent();
          }
          return parent != null;
      }
  
      private static int keyToDirection(int keyCode) {
          switch (keyCode) {
              case 19:
                  return 33;
              case 21:
                  return 17;
              case 22:
                  return 66;
              default:
                  return 130;
          }
      }
  
      private boolean moveFocus(int direction, Rect previouslyFocusedRect) {
          AccessibilityNodeInfoCompat accessibilityNodeInfoCompat;
          SparseArrayCompat<AccessibilityNodeInfoCompat> allNodes = getAllNodes();
          int i = this.mKeyboardFocusedVirtualViewId;
          AccessibilityNodeInfoCompat accessibilityNodeInfoCompat2 = i == Integer.MIN_VALUE ? null : allNodes.get(i);
          switch (direction) {
              case 1:
              case 2:
                  accessibilityNodeInfoCompat = (AccessibilityNodeInfoCompat) FocusStrategy.findNextFocusInRelativeDirection(allNodes, SPARSE_VALUES_ADAPTER, NODE_ADAPTER, accessibilityNodeInfoCompat2, direction, ViewCompat.getLayoutDirection(this.mHost) == 1, false);
                  break;
              case 17:
              case 33:
              case ConstraintLayout.LayoutParams.Table.LAYOUT_WRAP_BEHAVIOR_IN_PARENT:
              case 130:
                  Rect rect = new Rect();
                  int i2 = this.mKeyboardFocusedVirtualViewId;
                  if (i2 != Integer.MIN_VALUE) {
                      getBoundsInParent(i2, rect);
                  } else if (previouslyFocusedRect != null) {
                      rect.set(previouslyFocusedRect);
                  } else {
                      guessPreviouslyFocusedRect(this.mHost, direction, rect);
                  }
                  accessibilityNodeInfoCompat = (AccessibilityNodeInfoCompat) FocusStrategy.findNextFocusInAbsoluteDirection(allNodes, SPARSE_VALUES_ADAPTER, NODE_ADAPTER, accessibilityNodeInfoCompat2, rect, direction);
                  break;
              default:
                  throw new IllegalArgumentException("direction must be one of {FOCUS_FORWARD, FOCUS_BACKWARD, FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT}.");
          }
          return requestKeyboardFocusForVirtualView(accessibilityNodeInfoCompat == null ? Integer.MIN_VALUE : allNodes.keyAt(allNodes.indexOfValue(accessibilityNodeInfoCompat)));
      }
  
      private boolean performActionForChild(int virtualViewId, int action, Bundle arguments) {
          switch (action) {
              case 1:
                  return requestKeyboardFocusForVirtualView(virtualViewId);
              case 2:
                  return clearKeyboardFocusForVirtualView(virtualViewId);
              case 64:
                  return requestAccessibilityFocus(virtualViewId);
              case 128:
                  return clearAccessibilityFocus(virtualViewId);
              default:
                  return onPerformActionForVirtualView(virtualViewId, action, arguments);
          }
      }
  
      private boolean performActionForHost(int action, Bundle arguments) {
          return ViewCompat.performAccessibilityAction(this.mHost, action, arguments);
      }
  
      private boolean requestAccessibilityFocus(int virtualViewId) {
          int i;
          if (!this.mManager.isEnabled() || !this.mManager.isTouchExplorationEnabled() || (i = this.mAccessibilityFocusedVirtualViewId) == virtualViewId) {
              return false;
          }
          if (i != Integer.MIN_VALUE) {
              clearAccessibilityFocus(i);
          }
          this.mAccessibilityFocusedVirtualViewId = virtualViewId;
          this.mHost.invalidate();
          sendEventForVirtualView(virtualViewId, 32768);
          return true;
      }
  
      private void updateHoveredVirtualView(int virtualViewId) {
          if (this.mHoveredVirtualViewId != virtualViewId) {
              int i = this.mHoveredVirtualViewId;
              this.mHoveredVirtualViewId = virtualViewId;
              sendEventForVirtualView(virtualViewId, 128);
              sendEventForVirtualView(i, 256);
          }
      }
  
      public final boolean clearKeyboardFocusForVirtualView(int virtualViewId) {
          if (this.mKeyboardFocusedVirtualViewId != virtualViewId) {
              return false;
          }
          this.mKeyboardFocusedVirtualViewId = Integer.MIN_VALUE;
          onVirtualViewKeyboardFocusChanged(virtualViewId, false);
          sendEventForVirtualView(virtualViewId, 8);
          return true;
      }
  
      public final boolean dispatchHoverEvent(MotionEvent event) {
          if (!this.mManager.isEnabled() || !this.mManager.isTouchExplorationEnabled()) {
              return false;
          }
          switch (event.getAction()) {
              case 7:
              case 9:
                  int virtualViewAt = getVirtualViewAt(event.getX(), event.getY());
                  updateHoveredVirtualView(virtualViewAt);
                  return virtualViewAt != Integer.MIN_VALUE;
              case 10:
                  if (this.mHoveredVirtualViewId == Integer.MIN_VALUE) {
                      return false;
                  }
                  updateHoveredVirtualView(Integer.MIN_VALUE);
                  return true;
              default:
                  return false;
          }
      }
  
      public final boolean dispatchKeyEvent(KeyEvent event) {
          boolean z = false;
          if (event.getAction() == 1) {
              return false;
          }
          int keyCode = event.getKeyCode();
          switch (keyCode) {
              case 19:
              case 20:
              case 21:
              case 22:
                  if (!event.hasNoModifiers()) {
                      return false;
                  }
                  int keyToDirection = keyToDirection(keyCode);
                  int repeatCount = event.getRepeatCount() + 1;
                  for (int i = 0; i < repeatCount && moveFocus(keyToDirection, (Rect) null); i++) {
                      z = true;
                  }
                  return z;
              case 23:
              case ConstraintLayout.LayoutParams.Table.LAYOUT_WRAP_BEHAVIOR_IN_PARENT:
                  if (!event.hasNoModifiers() || event.getRepeatCount() != 0) {
                      return false;
                  }
                  clickKeyboardFocusedVirtualView();
                  return true;
              case LockFreeTaskQueueCore.CLOSED_SHIFT /*61*/:
                  if (event.hasNoModifiers()) {
                      return moveFocus(2, (Rect) null);
                  }
                  if (event.hasModifiers(1)) {
                      return moveFocus(1, (Rect) null);
                  }
                  return false;
              default:
                  return false;
          }
      }
  
      public final int getAccessibilityFocusedVirtualViewId() {
          return this.mAccessibilityFocusedVirtualViewId;
      }
  
      public AccessibilityNodeProviderCompat getAccessibilityNodeProvider(View host) {
          if (this.mNodeProvider == null) {
              this.mNodeProvider = new MyNodeProvider();
          }
          return this.mNodeProvider;
      }
  
      @Deprecated
      public int getFocusedVirtualView() {
          return getAccessibilityFocusedVirtualViewId();
      }
  
      public final int getKeyboardFocusedVirtualViewId() {
          return this.mKeyboardFocusedVirtualViewId;
      }
  
      /* access modifiers changed from: protected */
      public abstract int getVirtualViewAt(float f, float f2);
  
      /* access modifiers changed from: protected */
      public abstract void getVisibleVirtualViews(List<Integer> list);
  
      public final void invalidateRoot() {
          invalidateVirtualView(-1, 1);
      }
  
      public final void invalidateVirtualView(int virtualViewId) {
          invalidateVirtualView(virtualViewId, 0);
      }
  
      public final void invalidateVirtualView(int virtualViewId, int changeTypes) {
          ViewParent parent;
          if (virtualViewId != Integer.MIN_VALUE && this.mManager.isEnabled() && (parent = this.mHost.getParent()) != null) {
              AccessibilityEvent createEvent = createEvent(virtualViewId, 2048);
              AccessibilityEventCompat.setContentChangeTypes(createEvent, changeTypes);
              parent.requestSendAccessibilityEvent(this.mHost, createEvent);
          }
      }
  
      /* access modifiers changed from: package-private */
      public AccessibilityNodeInfoCompat obtainAccessibilityNodeInfo(int virtualViewId) {
          return virtualViewId == -1 ? createNodeForHost() : createNodeForChild(virtualViewId);
      }
  
      public final void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
          int i = this.mKeyboardFocusedVirtualViewId;
          if (i != Integer.MIN_VALUE) {
              clearKeyboardFocusForVirtualView(i);
          }
          if (gainFocus) {
              moveFocus(direction, previouslyFocusedRect);
          }
      }
  
      public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
          super.onInitializeAccessibilityEvent(host, event);
          onPopulateEventForHost(event);
      }
  
      public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) {
          super.onInitializeAccessibilityNodeInfo(host, info);
          onPopulateNodeForHost(info);
      }
  
      /* access modifiers changed from: protected */
      public abstract boolean onPerformActionForVirtualView(int i, int i2, Bundle bundle);
  
      /* access modifiers changed from: protected */
      public void onPopulateEventForHost(AccessibilityEvent event) {
      }
  
      /* access modifiers changed from: protected */
      public void onPopulateEventForVirtualView(int virtualViewId, AccessibilityEvent event) {
      }
  
      /* access modifiers changed from: protected */
      public void onPopulateNodeForHost(AccessibilityNodeInfoCompat node) {
      }
  
      /* access modifiers changed from: protected */
      public abstract void onPopulateNodeForVirtualView(int i, AccessibilityNodeInfoCompat accessibilityNodeInfoCompat);
  
      /* access modifiers changed from: protected */
      public void onVirtualViewKeyboardFocusChanged(int virtualViewId, boolean hasFocus) {
      }
  
      /* access modifiers changed from: package-private */
      public boolean performAction(int virtualViewId, int action, Bundle arguments) {
          switch (virtualViewId) {
              case -1:
                  return performActionForHost(action, arguments);
              default:
                  return performActionForChild(virtualViewId, action, arguments);
          }
      }
  
      public final boolean requestKeyboardFocusForVirtualView(int virtualViewId) {
          int i;
          if ((!this.mHost.isFocused() && !this.mHost.requestFocus()) || (i = this.mKeyboardFocusedVirtualViewId) == virtualViewId) {
              return false;
          }
          if (i != Integer.MIN_VALUE) {
              clearKeyboardFocusForVirtualView(i);
          }
          if (virtualViewId == Integer.MIN_VALUE) {
              return false;
          }
          this.mKeyboardFocusedVirtualViewId = virtualViewId;
          onVirtualViewKeyboardFocusChanged(virtualViewId, true);
          sendEventForVirtualView(virtualViewId, 8);
          return true;
      }
  
      public final boolean sendEventForVirtualView(int virtualViewId, int eventType) {
          ViewParent parent;
          if (virtualViewId == Integer.MIN_VALUE || !this.mManager.isEnabled() || (parent = this.mHost.getParent()) == null) {
              return false;
          }
          return parent.requestSendAccessibilityEvent(this.mHost, createEvent(virtualViewId, eventType));
      }
  }
  