Rabbit-R1/switch port/java/sources/androidx/customview/widget/FocusStrategy.java

273 lines
9.5 KiB
Java
Raw Permalink Normal View History

2024-05-21 16:08:36 -05:00
package androidx.customview.widget;
import android.graphics.Rect;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
/* loaded from: classes.dex */
class FocusStrategy {
/* loaded from: classes.dex */
public interface BoundsAdapter<T> {
void obtainBounds(T t, Rect rect);
}
/* loaded from: classes.dex */
public interface CollectionAdapter<T, V> {
V get(T t, int i);
int size(T t);
}
private static int getWeightedDistanceFor(int i, int i2) {
return (i * 13 * i) + (i2 * i2);
}
public static <L, T> T findNextFocusInRelativeDirection(L l, CollectionAdapter<L, T> collectionAdapter, BoundsAdapter<T> boundsAdapter, T t, int i, boolean z, boolean z2) {
int size = collectionAdapter.size(l);
ArrayList arrayList = new ArrayList(size);
for (int i2 = 0; i2 < size; i2++) {
arrayList.add(collectionAdapter.get(l, i2));
}
Collections.sort(arrayList, new SequentialComparator(z, boundsAdapter));
if (i == 1) {
return (T) getPreviousFocusable(t, arrayList, z2);
}
if (i == 2) {
return (T) getNextFocusable(t, arrayList, z2);
}
throw new IllegalArgumentException("direction must be one of {FOCUS_FORWARD, FOCUS_BACKWARD}.");
}
private static <T> T getNextFocusable(T t, ArrayList<T> arrayList, boolean z) {
int size = arrayList.size();
int lastIndexOf = (t == null ? -1 : arrayList.lastIndexOf(t)) + 1;
if (lastIndexOf < size) {
return arrayList.get(lastIndexOf);
}
if (!z || size <= 0) {
return null;
}
return arrayList.get(0);
}
private static <T> T getPreviousFocusable(T t, ArrayList<T> arrayList, boolean z) {
int size = arrayList.size();
int indexOf = (t == null ? size : arrayList.indexOf(t)) - 1;
if (indexOf >= 0) {
return arrayList.get(indexOf);
}
if (!z || size <= 0) {
return null;
}
return arrayList.get(size - 1);
}
/* loaded from: classes.dex */
private static class SequentialComparator<T> implements Comparator<T> {
private final BoundsAdapter<T> mAdapter;
private final boolean mIsLayoutRtl;
private final Rect mTemp1 = new Rect();
private final Rect mTemp2 = new Rect();
SequentialComparator(boolean z, BoundsAdapter<T> boundsAdapter) {
this.mIsLayoutRtl = z;
this.mAdapter = boundsAdapter;
}
@Override // java.util.Comparator
public int compare(T t, T t2) {
Rect rect = this.mTemp1;
Rect rect2 = this.mTemp2;
this.mAdapter.obtainBounds(t, rect);
this.mAdapter.obtainBounds(t2, rect2);
if (rect.top < rect2.top) {
return -1;
}
if (rect.top > rect2.top) {
return 1;
}
if (rect.left < rect2.left) {
return this.mIsLayoutRtl ? 1 : -1;
}
if (rect.left > rect2.left) {
return this.mIsLayoutRtl ? -1 : 1;
}
if (rect.bottom < rect2.bottom) {
return -1;
}
if (rect.bottom > rect2.bottom) {
return 1;
}
if (rect.right < rect2.right) {
return this.mIsLayoutRtl ? 1 : -1;
}
if (rect.right > rect2.right) {
return this.mIsLayoutRtl ? -1 : 1;
}
return 0;
}
}
public static <L, T> T findNextFocusInAbsoluteDirection(L l, CollectionAdapter<L, T> collectionAdapter, BoundsAdapter<T> boundsAdapter, T t, Rect rect, int i) {
Rect rect2 = new Rect(rect);
if (i == 17) {
rect2.offset(rect.width() + 1, 0);
} else if (i == 33) {
rect2.offset(0, rect.height() + 1);
} else if (i == 66) {
rect2.offset(-(rect.width() + 1), 0);
} else if (i == 130) {
rect2.offset(0, -(rect.height() + 1));
} else {
throw new IllegalArgumentException("direction must be one of {FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT}.");
}
int size = collectionAdapter.size(l);
Rect rect3 = new Rect();
T t2 = null;
for (int i2 = 0; i2 < size; i2++) {
T t3 = collectionAdapter.get(l, i2);
if (t3 != t) {
boundsAdapter.obtainBounds(t3, rect3);
if (isBetterCandidate(i, rect, rect3, rect2)) {
rect2.set(rect3);
t2 = t3;
}
}
}
return t2;
}
private static boolean isBetterCandidate(int i, Rect rect, Rect rect2, Rect rect3) {
if (!isCandidate(rect, rect2, i)) {
return false;
}
if (isCandidate(rect, rect3, i) && !beamBeats(i, rect, rect2, rect3)) {
return !beamBeats(i, rect, rect3, rect2) && getWeightedDistanceFor(majorAxisDistance(i, rect, rect2), minorAxisDistance(i, rect, rect2)) < getWeightedDistanceFor(majorAxisDistance(i, rect, rect3), minorAxisDistance(i, rect, rect3));
}
return true;
}
private static boolean beamBeats(int i, Rect rect, Rect rect2, Rect rect3) {
boolean beamsOverlap = beamsOverlap(i, rect, rect2);
if (beamsOverlap(i, rect, rect3) || !beamsOverlap) {
return false;
}
return !isToDirectionOf(i, rect, rect3) || i == 17 || i == 66 || majorAxisDistance(i, rect, rect2) < majorAxisDistanceToFarEdge(i, rect, rect3);
}
private static boolean isCandidate(Rect rect, Rect rect2, int i) {
if (i == 17) {
return (rect.right > rect2.right || rect.left >= rect2.right) && rect.left > rect2.left;
}
if (i == 33) {
return (rect.bottom > rect2.bottom || rect.top >= rect2.bottom) && rect.top > rect2.top;
}
if (i == 66) {
return (rect.left < rect2.left || rect.right <= rect2.left) && rect.right < rect2.right;
}
if (i == 130) {
return (rect.top < rect2.top || rect.bottom <= rect2.top) && rect.bottom < rect2.bottom;
}
throw new IllegalArgumentException("direction must be one of {FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT}.");
}
private static boolean beamsOverlap(int i, Rect rect, Rect rect2) {
if (i != 17) {
if (i != 33) {
if (i != 66) {
if (i != 130) {
throw new IllegalArgumentException("direction must be one of {FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT}.");
}
}
}
return rect2.right >= rect.left && rect2.left <= rect.right;
}
return rect2.bottom >= rect.top && rect2.top <= rect.bottom;
}
private static boolean isToDirectionOf(int i, Rect rect, Rect rect2) {
if (i == 17) {
return rect.left >= rect2.right;
}
if (i == 33) {
return rect.top >= rect2.bottom;
}
if (i == 66) {
return rect.right <= rect2.left;
}
if (i == 130) {
return rect.bottom <= rect2.top;
}
throw new IllegalArgumentException("direction must be one of {FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT}.");
}
private static int majorAxisDistance(int i, Rect rect, Rect rect2) {
return Math.max(0, majorAxisDistanceRaw(i, rect, rect2));
}
private static int majorAxisDistanceRaw(int i, Rect rect, Rect rect2) {
int i2;
int i3;
if (i == 17) {
i2 = rect.left;
i3 = rect2.right;
} else if (i == 33) {
i2 = rect.top;
i3 = rect2.bottom;
} else if (i == 66) {
i2 = rect2.left;
i3 = rect.right;
} else if (i == 130) {
i2 = rect2.top;
i3 = rect.bottom;
} else {
throw new IllegalArgumentException("direction must be one of {FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT}.");
}
return i2 - i3;
}
private static int majorAxisDistanceToFarEdge(int i, Rect rect, Rect rect2) {
return Math.max(1, majorAxisDistanceToFarEdgeRaw(i, rect, rect2));
}
private static int majorAxisDistanceToFarEdgeRaw(int i, Rect rect, Rect rect2) {
int i2;
int i3;
if (i == 17) {
i2 = rect.left;
i3 = rect2.left;
} else if (i == 33) {
i2 = rect.top;
i3 = rect2.top;
} else if (i == 66) {
i2 = rect2.right;
i3 = rect.right;
} else if (i == 130) {
i2 = rect2.bottom;
i3 = rect.bottom;
} else {
throw new IllegalArgumentException("direction must be one of {FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT}.");
}
return i2 - i3;
}
private static int minorAxisDistance(int i, Rect rect, Rect rect2) {
if (i != 17) {
if (i != 33) {
if (i != 66) {
if (i != 130) {
throw new IllegalArgumentException("direction must be one of {FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT}.");
}
}
}
return Math.abs((rect.left + (rect.width() / 2)) - (rect2.left + (rect2.width() / 2)));
}
return Math.abs((rect.top + (rect.height() / 2)) - (rect2.top + (rect2.height() / 2)));
}
private FocusStrategy() {
}
}