package androidx.recyclerview.widget; import android.os.Handler; import android.os.Looper; import androidx.recyclerview.widget.AsyncDifferConfig; import androidx.recyclerview.widget.DiffUtil; import androidx.recyclerview.widget.RecyclerView; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.Executor; /* loaded from: classes2.dex */ public class AsyncListDiffer { private static final Executor sMainThreadExecutor = new MainThreadExecutor(); final AsyncDifferConfig mConfig; private List mList; private final List> mListeners; Executor mMainThreadExecutor; int mMaxScheduledGeneration; private List mReadOnlyList; private final ListUpdateCallback mUpdateCallback; /* loaded from: classes2.dex */ public interface ListListener { void onCurrentListChanged(List list, List list2); } public List getCurrentList() { return this.mReadOnlyList; } /* loaded from: classes2.dex */ private static class MainThreadExecutor implements Executor { final Handler mHandler = new Handler(Looper.getMainLooper()); MainThreadExecutor() { } @Override // java.util.concurrent.Executor public void execute(Runnable runnable) { this.mHandler.post(runnable); } } public AsyncListDiffer(RecyclerView.Adapter adapter, DiffUtil.ItemCallback itemCallback) { this(new AdapterListUpdateCallback(adapter), new AsyncDifferConfig.Builder(itemCallback).build()); } public AsyncListDiffer(ListUpdateCallback listUpdateCallback, AsyncDifferConfig asyncDifferConfig) { this.mListeners = new CopyOnWriteArrayList(); this.mReadOnlyList = Collections.emptyList(); this.mUpdateCallback = listUpdateCallback; this.mConfig = asyncDifferConfig; if (asyncDifferConfig.getMainThreadExecutor() != null) { this.mMainThreadExecutor = asyncDifferConfig.getMainThreadExecutor(); } else { this.mMainThreadExecutor = sMainThreadExecutor; } } public void submitList(List list) { submitList(list, null); } public void submitList(final List list, final Runnable runnable) { final int i = this.mMaxScheduledGeneration + 1; this.mMaxScheduledGeneration = i; final List list2 = this.mList; if (list == list2) { if (runnable != null) { runnable.run(); return; } return; } List list3 = this.mReadOnlyList; if (list == null) { int size = list2.size(); this.mList = null; this.mReadOnlyList = Collections.emptyList(); this.mUpdateCallback.onRemoved(0, size); onCurrentListChanged(list3, runnable); return; } if (list2 == null) { this.mList = list; this.mReadOnlyList = Collections.unmodifiableList(list); this.mUpdateCallback.onInserted(0, list.size()); onCurrentListChanged(list3, runnable); return; } this.mConfig.getBackgroundThreadExecutor().execute(new Runnable() { // from class: androidx.recyclerview.widget.AsyncListDiffer.1 @Override // java.lang.Runnable public void run() { final DiffUtil.DiffResult calculateDiff = DiffUtil.calculateDiff(new DiffUtil.Callback() { // from class: androidx.recyclerview.widget.AsyncListDiffer.1.1 @Override // androidx.recyclerview.widget.DiffUtil.Callback public int getOldListSize() { return list2.size(); } @Override // androidx.recyclerview.widget.DiffUtil.Callback public int getNewListSize() { return list.size(); } /* JADX WARN: Multi-variable type inference failed */ @Override // androidx.recyclerview.widget.DiffUtil.Callback public boolean areItemsTheSame(int i2, int i3) { Object obj = list2.get(i2); Object obj2 = list.get(i3); if (obj == null || obj2 == null) { return obj == null && obj2 == null; } return AsyncListDiffer.this.mConfig.getDiffCallback().areItemsTheSame(obj, obj2); } /* JADX WARN: Multi-variable type inference failed */ @Override // androidx.recyclerview.widget.DiffUtil.Callback public boolean areContentsTheSame(int i2, int i3) { Object obj = list2.get(i2); Object obj2 = list.get(i3); if (obj != null && obj2 != null) { return AsyncListDiffer.this.mConfig.getDiffCallback().areContentsTheSame(obj, obj2); } if (obj == null && obj2 == null) { return true; } throw new AssertionError(); } /* JADX WARN: Multi-variable type inference failed */ @Override // androidx.recyclerview.widget.DiffUtil.Callback public Object getChangePayload(int i2, int i3) { Object obj = list2.get(i2); Object obj2 = list.get(i3); if (obj != null && obj2 != null) { return AsyncListDiffer.this.mConfig.getDiffCallback().getChangePayload(obj, obj2); } throw new AssertionError(); } }); AsyncListDiffer.this.mMainThreadExecutor.execute(new Runnable() { // from class: androidx.recyclerview.widget.AsyncListDiffer.1.2 @Override // java.lang.Runnable public void run() { if (AsyncListDiffer.this.mMaxScheduledGeneration == i) { AsyncListDiffer.this.latchList(list, calculateDiff, runnable); } } }); } }); } void latchList(List list, DiffUtil.DiffResult diffResult, Runnable runnable) { List list2 = this.mReadOnlyList; this.mList = list; this.mReadOnlyList = Collections.unmodifiableList(list); diffResult.dispatchUpdatesTo(this.mUpdateCallback); onCurrentListChanged(list2, runnable); } private void onCurrentListChanged(List list, Runnable runnable) { Iterator> it = this.mListeners.iterator(); while (it.hasNext()) { it.next().onCurrentListChanged(list, this.mReadOnlyList); } if (runnable != null) { runnable.run(); } } public void addListListener(ListListener listListener) { this.mListeners.add(listListener); } public void removeListListener(ListListener listListener) { this.mListeners.remove(listListener); } }