Rabbit-R1/switch port/java/sources/androidx/recyclerview/widget/AdapterHelper.java
2024-05-21 17:08:36 -04:00

608 lines
22 KiB
Java

package androidx.recyclerview.widget;
import androidx.core.util.Pools;
import androidx.recyclerview.widget.OpReorderer;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public final class AdapterHelper implements OpReorderer.Callback {
private static final boolean DEBUG = false;
static final int POSITION_TYPE_INVISIBLE = 0;
static final int POSITION_TYPE_NEW_OR_LAID_OUT = 1;
private static final String TAG = "AHT";
final Callback mCallback;
final boolean mDisableRecycler;
private int mExistingUpdateTypes;
Runnable mOnItemProcessedCallback;
final OpReorderer mOpReorderer;
final ArrayList<UpdateOp> mPendingUpdates;
final ArrayList<UpdateOp> mPostponedList;
private Pools.Pool<UpdateOp> mUpdateOpPool;
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public interface Callback {
RecyclerView.ViewHolder findViewHolder(int i);
void markViewHoldersUpdated(int i, int i2, Object obj);
void offsetPositionsForAdd(int i, int i2);
void offsetPositionsForMove(int i, int i2);
void offsetPositionsForRemovingInvisible(int i, int i2);
void offsetPositionsForRemovingLaidOutOrNewView(int i, int i2);
void onDispatchFirstPass(UpdateOp updateOp);
void onDispatchSecondPass(UpdateOp updateOp);
}
/* JADX INFO: Access modifiers changed from: package-private */
public boolean hasAnyUpdateTypes(int i) {
return (this.mExistingUpdateTypes & i) != 0;
}
/* JADX INFO: Access modifiers changed from: package-private */
public AdapterHelper(Callback callback) {
this(callback, false);
}
AdapterHelper(Callback callback, boolean z) {
this.mUpdateOpPool = new Pools.SimplePool(30);
this.mPendingUpdates = new ArrayList<>();
this.mPostponedList = new ArrayList<>();
this.mExistingUpdateTypes = 0;
this.mCallback = callback;
this.mDisableRecycler = z;
this.mOpReorderer = new OpReorderer(this);
}
AdapterHelper addUpdateOp(UpdateOp... updateOpArr) {
Collections.addAll(this.mPendingUpdates, updateOpArr);
return this;
}
/* JADX INFO: Access modifiers changed from: package-private */
public void reset() {
recycleUpdateOpsAndClearList(this.mPendingUpdates);
recycleUpdateOpsAndClearList(this.mPostponedList);
this.mExistingUpdateTypes = 0;
}
/* JADX INFO: Access modifiers changed from: package-private */
public void preProcess() {
this.mOpReorderer.reorderOps(this.mPendingUpdates);
int size = this.mPendingUpdates.size();
for (int i = 0; i < size; i++) {
UpdateOp updateOp = this.mPendingUpdates.get(i);
int i2 = updateOp.cmd;
if (i2 == 1) {
applyAdd(updateOp);
} else if (i2 == 2) {
applyRemove(updateOp);
} else if (i2 == 4) {
applyUpdate(updateOp);
} else if (i2 == 8) {
applyMove(updateOp);
}
Runnable runnable = this.mOnItemProcessedCallback;
if (runnable != null) {
runnable.run();
}
}
this.mPendingUpdates.clear();
}
/* JADX INFO: Access modifiers changed from: package-private */
public void consumePostponedUpdates() {
int size = this.mPostponedList.size();
for (int i = 0; i < size; i++) {
this.mCallback.onDispatchSecondPass(this.mPostponedList.get(i));
}
recycleUpdateOpsAndClearList(this.mPostponedList);
this.mExistingUpdateTypes = 0;
}
private void applyMove(UpdateOp updateOp) {
postponeAndUpdateViewHolders(updateOp);
}
private void applyRemove(UpdateOp updateOp) {
boolean z;
char c;
int i = updateOp.positionStart;
int i2 = updateOp.positionStart + updateOp.itemCount;
int i3 = updateOp.positionStart;
char c2 = 65535;
int i4 = 0;
while (i3 < i2) {
if (this.mCallback.findViewHolder(i3) != null || canFindInPreLayout(i3)) {
if (c2 == 0) {
dispatchAndUpdateViewHolders(obtainUpdateOp(2, i, i4, null));
z = true;
} else {
z = false;
}
c = 1;
} else {
if (c2 == 1) {
postponeAndUpdateViewHolders(obtainUpdateOp(2, i, i4, null));
z = true;
} else {
z = false;
}
c = 0;
}
if (z) {
i3 -= i4;
i2 -= i4;
i4 = 1;
} else {
i4++;
}
i3++;
c2 = c;
}
if (i4 != updateOp.itemCount) {
recycleUpdateOp(updateOp);
updateOp = obtainUpdateOp(2, i, i4, null);
}
if (c2 == 0) {
dispatchAndUpdateViewHolders(updateOp);
} else {
postponeAndUpdateViewHolders(updateOp);
}
}
private void applyUpdate(UpdateOp updateOp) {
int i = updateOp.positionStart;
int i2 = updateOp.positionStart + updateOp.itemCount;
char c = 65535;
int i3 = 0;
for (int i4 = updateOp.positionStart; i4 < i2; i4++) {
if (this.mCallback.findViewHolder(i4) != null || canFindInPreLayout(i4)) {
if (c == 0) {
dispatchAndUpdateViewHolders(obtainUpdateOp(4, i, i3, updateOp.payload));
i = i4;
i3 = 0;
}
c = 1;
} else {
if (c == 1) {
postponeAndUpdateViewHolders(obtainUpdateOp(4, i, i3, updateOp.payload));
i = i4;
i3 = 0;
}
c = 0;
}
i3++;
}
if (i3 != updateOp.itemCount) {
Object obj = updateOp.payload;
recycleUpdateOp(updateOp);
updateOp = obtainUpdateOp(4, i, i3, obj);
}
if (c == 0) {
dispatchAndUpdateViewHolders(updateOp);
} else {
postponeAndUpdateViewHolders(updateOp);
}
}
private void dispatchAndUpdateViewHolders(UpdateOp updateOp) {
int i;
if (updateOp.cmd == 1 || updateOp.cmd == 8) {
throw new IllegalArgumentException("should not dispatch add or move for pre layout");
}
int updatePositionWithPostponed = updatePositionWithPostponed(updateOp.positionStart, updateOp.cmd);
int i2 = updateOp.positionStart;
int i3 = updateOp.cmd;
if (i3 == 2) {
i = 0;
} else {
if (i3 != 4) {
throw new IllegalArgumentException("op should be remove or update." + updateOp);
}
i = 1;
}
int i4 = 1;
for (int i5 = 1; i5 < updateOp.itemCount; i5++) {
int updatePositionWithPostponed2 = updatePositionWithPostponed(updateOp.positionStart + (i * i5), updateOp.cmd);
int i6 = updateOp.cmd;
if (i6 == 2 ? updatePositionWithPostponed2 != updatePositionWithPostponed : !(i6 == 4 && updatePositionWithPostponed2 == updatePositionWithPostponed + 1)) {
UpdateOp obtainUpdateOp = obtainUpdateOp(updateOp.cmd, updatePositionWithPostponed, i4, updateOp.payload);
dispatchFirstPassAndUpdateViewHolders(obtainUpdateOp, i2);
recycleUpdateOp(obtainUpdateOp);
if (updateOp.cmd == 4) {
i2 += i4;
}
i4 = 1;
updatePositionWithPostponed = updatePositionWithPostponed2;
} else {
i4++;
}
}
Object obj = updateOp.payload;
recycleUpdateOp(updateOp);
if (i4 > 0) {
UpdateOp obtainUpdateOp2 = obtainUpdateOp(updateOp.cmd, updatePositionWithPostponed, i4, obj);
dispatchFirstPassAndUpdateViewHolders(obtainUpdateOp2, i2);
recycleUpdateOp(obtainUpdateOp2);
}
}
void dispatchFirstPassAndUpdateViewHolders(UpdateOp updateOp, int i) {
this.mCallback.onDispatchFirstPass(updateOp);
int i2 = updateOp.cmd;
if (i2 == 2) {
this.mCallback.offsetPositionsForRemovingInvisible(i, updateOp.itemCount);
} else {
if (i2 == 4) {
this.mCallback.markViewHoldersUpdated(i, updateOp.itemCount, updateOp.payload);
return;
}
throw new IllegalArgumentException("only remove and update ops can be dispatched in first pass");
}
}
private int updatePositionWithPostponed(int i, int i2) {
int i3;
int i4;
for (int size = this.mPostponedList.size() - 1; size >= 0; size--) {
UpdateOp updateOp = this.mPostponedList.get(size);
if (updateOp.cmd == 8) {
if (updateOp.positionStart < updateOp.itemCount) {
i3 = updateOp.positionStart;
i4 = updateOp.itemCount;
} else {
i3 = updateOp.itemCount;
i4 = updateOp.positionStart;
}
if (i >= i3 && i <= i4) {
if (i3 == updateOp.positionStart) {
if (i2 == 1) {
updateOp.itemCount++;
} else if (i2 == 2) {
updateOp.itemCount--;
}
i++;
} else {
if (i2 == 1) {
updateOp.positionStart++;
} else if (i2 == 2) {
updateOp.positionStart--;
}
i--;
}
} else if (i < updateOp.positionStart) {
if (i2 == 1) {
updateOp.positionStart++;
updateOp.itemCount++;
} else if (i2 == 2) {
updateOp.positionStart--;
updateOp.itemCount--;
}
}
} else if (updateOp.positionStart <= i) {
if (updateOp.cmd == 1) {
i -= updateOp.itemCount;
} else if (updateOp.cmd == 2) {
i += updateOp.itemCount;
}
} else if (i2 == 1) {
updateOp.positionStart++;
} else if (i2 == 2) {
updateOp.positionStart--;
}
}
for (int size2 = this.mPostponedList.size() - 1; size2 >= 0; size2--) {
UpdateOp updateOp2 = this.mPostponedList.get(size2);
if (updateOp2.cmd == 8) {
if (updateOp2.itemCount == updateOp2.positionStart || updateOp2.itemCount < 0) {
this.mPostponedList.remove(size2);
recycleUpdateOp(updateOp2);
}
} else if (updateOp2.itemCount <= 0) {
this.mPostponedList.remove(size2);
recycleUpdateOp(updateOp2);
}
}
return i;
}
private boolean canFindInPreLayout(int i) {
int size = this.mPostponedList.size();
for (int i2 = 0; i2 < size; i2++) {
UpdateOp updateOp = this.mPostponedList.get(i2);
if (updateOp.cmd == 8) {
if (findPositionOffset(updateOp.itemCount, i2 + 1) == i) {
return true;
}
} else if (updateOp.cmd == 1) {
int i3 = updateOp.positionStart + updateOp.itemCount;
for (int i4 = updateOp.positionStart; i4 < i3; i4++) {
if (findPositionOffset(i4, i2 + 1) == i) {
return true;
}
}
} else {
continue;
}
}
return false;
}
private void applyAdd(UpdateOp updateOp) {
postponeAndUpdateViewHolders(updateOp);
}
private void postponeAndUpdateViewHolders(UpdateOp updateOp) {
this.mPostponedList.add(updateOp);
int i = updateOp.cmd;
if (i == 1) {
this.mCallback.offsetPositionsForAdd(updateOp.positionStart, updateOp.itemCount);
return;
}
if (i == 2) {
this.mCallback.offsetPositionsForRemovingLaidOutOrNewView(updateOp.positionStart, updateOp.itemCount);
} else if (i == 4) {
this.mCallback.markViewHoldersUpdated(updateOp.positionStart, updateOp.itemCount, updateOp.payload);
} else {
if (i == 8) {
this.mCallback.offsetPositionsForMove(updateOp.positionStart, updateOp.itemCount);
return;
}
throw new IllegalArgumentException("Unknown update op type for " + updateOp);
}
}
/* JADX INFO: Access modifiers changed from: package-private */
public boolean hasPendingUpdates() {
return this.mPendingUpdates.size() > 0;
}
/* JADX INFO: Access modifiers changed from: package-private */
public int findPositionOffset(int i) {
return findPositionOffset(i, 0);
}
int findPositionOffset(int i, int i2) {
int size = this.mPostponedList.size();
while (i2 < size) {
UpdateOp updateOp = this.mPostponedList.get(i2);
if (updateOp.cmd == 8) {
if (updateOp.positionStart == i) {
i = updateOp.itemCount;
} else {
if (updateOp.positionStart < i) {
i--;
}
if (updateOp.itemCount <= i) {
i++;
}
}
} else if (updateOp.positionStart > i) {
continue;
} else if (updateOp.cmd == 2) {
if (i < updateOp.positionStart + updateOp.itemCount) {
return -1;
}
i -= updateOp.itemCount;
} else if (updateOp.cmd == 1) {
i += updateOp.itemCount;
}
i2++;
}
return i;
}
/* JADX INFO: Access modifiers changed from: package-private */
public boolean onItemRangeChanged(int i, int i2, Object obj) {
if (i2 < 1) {
return false;
}
this.mPendingUpdates.add(obtainUpdateOp(4, i, i2, obj));
this.mExistingUpdateTypes |= 4;
return this.mPendingUpdates.size() == 1;
}
/* JADX INFO: Access modifiers changed from: package-private */
public boolean onItemRangeInserted(int i, int i2) {
if (i2 < 1) {
return false;
}
this.mPendingUpdates.add(obtainUpdateOp(1, i, i2, null));
this.mExistingUpdateTypes |= 1;
return this.mPendingUpdates.size() == 1;
}
/* JADX INFO: Access modifiers changed from: package-private */
public boolean onItemRangeRemoved(int i, int i2) {
if (i2 < 1) {
return false;
}
this.mPendingUpdates.add(obtainUpdateOp(2, i, i2, null));
this.mExistingUpdateTypes |= 2;
return this.mPendingUpdates.size() == 1;
}
/* JADX INFO: Access modifiers changed from: package-private */
public boolean onItemRangeMoved(int i, int i2, int i3) {
if (i == i2) {
return false;
}
if (i3 != 1) {
throw new IllegalArgumentException("Moving more than 1 item is not supported yet");
}
this.mPendingUpdates.add(obtainUpdateOp(8, i, i2, null));
this.mExistingUpdateTypes |= 8;
return this.mPendingUpdates.size() == 1;
}
/* JADX INFO: Access modifiers changed from: package-private */
public void consumeUpdatesInOnePass() {
consumePostponedUpdates();
int size = this.mPendingUpdates.size();
for (int i = 0; i < size; i++) {
UpdateOp updateOp = this.mPendingUpdates.get(i);
int i2 = updateOp.cmd;
if (i2 == 1) {
this.mCallback.onDispatchSecondPass(updateOp);
this.mCallback.offsetPositionsForAdd(updateOp.positionStart, updateOp.itemCount);
} else if (i2 == 2) {
this.mCallback.onDispatchSecondPass(updateOp);
this.mCallback.offsetPositionsForRemovingInvisible(updateOp.positionStart, updateOp.itemCount);
} else if (i2 == 4) {
this.mCallback.onDispatchSecondPass(updateOp);
this.mCallback.markViewHoldersUpdated(updateOp.positionStart, updateOp.itemCount, updateOp.payload);
} else if (i2 == 8) {
this.mCallback.onDispatchSecondPass(updateOp);
this.mCallback.offsetPositionsForMove(updateOp.positionStart, updateOp.itemCount);
}
Runnable runnable = this.mOnItemProcessedCallback;
if (runnable != null) {
runnable.run();
}
}
recycleUpdateOpsAndClearList(this.mPendingUpdates);
this.mExistingUpdateTypes = 0;
}
public int applyPendingUpdatesToPosition(int i) {
int size = this.mPendingUpdates.size();
for (int i2 = 0; i2 < size; i2++) {
UpdateOp updateOp = this.mPendingUpdates.get(i2);
int i3 = updateOp.cmd;
if (i3 != 1) {
if (i3 != 2) {
if (i3 == 8) {
if (updateOp.positionStart == i) {
i = updateOp.itemCount;
} else {
if (updateOp.positionStart < i) {
i--;
}
if (updateOp.itemCount <= i) {
i++;
}
}
}
} else if (updateOp.positionStart > i) {
continue;
} else {
if (updateOp.positionStart + updateOp.itemCount > i) {
return -1;
}
i -= updateOp.itemCount;
}
} else if (updateOp.positionStart <= i) {
i += updateOp.itemCount;
}
}
return i;
}
/* JADX INFO: Access modifiers changed from: package-private */
public boolean hasUpdates() {
return (this.mPostponedList.isEmpty() || this.mPendingUpdates.isEmpty()) ? false : true;
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public static final class UpdateOp {
static final int ADD = 1;
static final int MOVE = 8;
static final int POOL_SIZE = 30;
static final int REMOVE = 2;
static final int UPDATE = 4;
int cmd;
int itemCount;
Object payload;
int positionStart;
String cmdToString() {
int i = this.cmd;
return i != 1 ? i != 2 ? i != 4 ? i != 8 ? "??" : "mv" : "up" : "rm" : "add";
}
public int hashCode() {
return (((this.cmd * 31) + this.positionStart) * 31) + this.itemCount;
}
UpdateOp(int i, int i2, int i3, Object obj) {
this.cmd = i;
this.positionStart = i2;
this.itemCount = i3;
this.payload = obj;
}
public String toString() {
return Integer.toHexString(System.identityHashCode(this)) + "[" + cmdToString() + ",s:" + this.positionStart + "c:" + this.itemCount + ",p:" + this.payload + "]";
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof UpdateOp)) {
return false;
}
UpdateOp updateOp = (UpdateOp) obj;
int i = this.cmd;
if (i != updateOp.cmd) {
return false;
}
if (i == 8 && Math.abs(this.itemCount - this.positionStart) == 1 && this.itemCount == updateOp.positionStart && this.positionStart == updateOp.itemCount) {
return true;
}
if (this.itemCount != updateOp.itemCount || this.positionStart != updateOp.positionStart) {
return false;
}
Object obj2 = this.payload;
if (obj2 != null) {
if (!obj2.equals(updateOp.payload)) {
return false;
}
} else if (updateOp.payload != null) {
return false;
}
return true;
}
}
@Override // androidx.recyclerview.widget.OpReorderer.Callback
public UpdateOp obtainUpdateOp(int i, int i2, int i3, Object obj) {
UpdateOp acquire = this.mUpdateOpPool.acquire();
if (acquire == null) {
return new UpdateOp(i, i2, i3, obj);
}
acquire.cmd = i;
acquire.positionStart = i2;
acquire.itemCount = i3;
acquire.payload = obj;
return acquire;
}
@Override // androidx.recyclerview.widget.OpReorderer.Callback
public void recycleUpdateOp(UpdateOp updateOp) {
if (this.mDisableRecycler) {
return;
}
updateOp.payload = null;
this.mUpdateOpPool.release(updateOp);
}
void recycleUpdateOpsAndClearList(List<UpdateOp> list) {
int size = list.size();
for (int i = 0; i < size; i++) {
recycleUpdateOp(list.get(i));
}
list.clear();
}
}