package androidx.viewpager2.adapter; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Parcelable; import android.view.View; import android.view.ViewGroup; import android.view.ViewParent; import android.widget.FrameLayout; import androidx.collection.ArraySet; import androidx.collection.LongSparseArray; import androidx.core.util.Preconditions; import androidx.core.view.ViewCompat; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import androidx.lifecycle.Lifecycle; import androidx.lifecycle.LifecycleEventObserver; import androidx.lifecycle.LifecycleOwner; import androidx.recyclerview.widget.RecyclerView; import androidx.viewpager2.widget.ViewPager2; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; /* loaded from: classes2.dex */ public abstract class FragmentStateAdapter extends RecyclerView.Adapter implements StatefulAdapter { private static final long GRACE_WINDOW_TIME_MS = 10000; private static final String KEY_PREFIX_FRAGMENT = "f#"; private static final String KEY_PREFIX_STATE = "s#"; FragmentEventDispatcher mFragmentEventDispatcher; final FragmentManager mFragmentManager; private FragmentMaxLifecycleEnforcer mFragmentMaxLifecycleEnforcer; final LongSparseArray mFragments; private boolean mHasStaleFragments; boolean mIsInGracePeriod; private final LongSparseArray mItemIdToViewHolder; final Lifecycle mLifecycle; private final LongSparseArray mSavedStates; /* loaded from: classes2.dex */ public @interface ExperimentalFragmentStateAdapterApi { } /* loaded from: classes2.dex */ public static abstract class FragmentTransactionCallback { private static final OnPostEventListener NO_OP = new OnPostEventListener() { // from class: androidx.viewpager2.adapter.FragmentStateAdapter.FragmentTransactionCallback.1 @Override // androidx.viewpager2.adapter.FragmentStateAdapter.FragmentTransactionCallback.OnPostEventListener public void onPost() { } }; /* loaded from: classes2.dex */ public interface OnPostEventListener { void onPost(); } public OnPostEventListener onFragmentMaxLifecyclePreUpdated(Fragment fragment, Lifecycle.State state) { return NO_OP; } public OnPostEventListener onFragmentPreAdded(Fragment fragment) { return NO_OP; } public OnPostEventListener onFragmentPreRemoved(Fragment fragment) { return NO_OP; } public OnPostEventListener onFragmentPreSavedInstanceState(Fragment fragment) { return NO_OP; } } public abstract Fragment createFragment(int i); @Override // androidx.recyclerview.widget.RecyclerView.Adapter public long getItemId(int i) { return i; } @Override // androidx.recyclerview.widget.RecyclerView.Adapter public final boolean onFailedToRecycleView(FragmentViewHolder fragmentViewHolder) { return true; } public FragmentStateAdapter(FragmentActivity fragmentActivity) { this(fragmentActivity.getSupportFragmentManager(), fragmentActivity.getLifecycle()); } public FragmentStateAdapter(Fragment fragment) { this(fragment.getChildFragmentManager(), fragment.getLifecycle()); } public FragmentStateAdapter(FragmentManager fragmentManager, Lifecycle lifecycle) { this.mFragments = new LongSparseArray<>(); this.mSavedStates = new LongSparseArray<>(); this.mItemIdToViewHolder = new LongSparseArray<>(); this.mFragmentEventDispatcher = new FragmentEventDispatcher(); this.mIsInGracePeriod = false; this.mHasStaleFragments = false; this.mFragmentManager = fragmentManager; this.mLifecycle = lifecycle; super.setHasStableIds(true); } @Override // androidx.recyclerview.widget.RecyclerView.Adapter public void onAttachedToRecyclerView(RecyclerView recyclerView) { Preconditions.checkArgument(this.mFragmentMaxLifecycleEnforcer == null); FragmentMaxLifecycleEnforcer fragmentMaxLifecycleEnforcer = new FragmentMaxLifecycleEnforcer(); this.mFragmentMaxLifecycleEnforcer = fragmentMaxLifecycleEnforcer; fragmentMaxLifecycleEnforcer.register(recyclerView); } @Override // androidx.recyclerview.widget.RecyclerView.Adapter public void onDetachedFromRecyclerView(RecyclerView recyclerView) { this.mFragmentMaxLifecycleEnforcer.unregister(recyclerView); this.mFragmentMaxLifecycleEnforcer = null; } @Override // androidx.recyclerview.widget.RecyclerView.Adapter public final FragmentViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { return FragmentViewHolder.create(viewGroup); } @Override // androidx.recyclerview.widget.RecyclerView.Adapter public final void onBindViewHolder(FragmentViewHolder fragmentViewHolder, int i) { long itemId = fragmentViewHolder.getItemId(); int id = fragmentViewHolder.getContainer().getId(); Long itemForViewHolder = itemForViewHolder(id); if (itemForViewHolder != null && itemForViewHolder.longValue() != itemId) { removeFragment(itemForViewHolder.longValue()); this.mItemIdToViewHolder.remove(itemForViewHolder.longValue()); } this.mItemIdToViewHolder.put(itemId, Integer.valueOf(id)); ensureFragment(i); if (ViewCompat.isAttachedToWindow(fragmentViewHolder.getContainer())) { placeFragmentInViewHolder(fragmentViewHolder); } gcFragments(); } void gcFragments() { if (!this.mHasStaleFragments || shouldDelayFragmentTransactions()) { return; } ArraySet arraySet = new ArraySet(); for (int i = 0; i < this.mFragments.size(); i++) { long keyAt = this.mFragments.keyAt(i); if (!containsItem(keyAt)) { arraySet.add(Long.valueOf(keyAt)); this.mItemIdToViewHolder.remove(keyAt); } } if (!this.mIsInGracePeriod) { this.mHasStaleFragments = false; for (int i2 = 0; i2 < this.mFragments.size(); i2++) { long keyAt2 = this.mFragments.keyAt(i2); if (!isFragmentViewBound(keyAt2)) { arraySet.add(Long.valueOf(keyAt2)); } } } Iterator it = arraySet.iterator(); while (it.hasNext()) { removeFragment(((Long) it.next()).longValue()); } } private boolean isFragmentViewBound(long j) { View view; if (this.mItemIdToViewHolder.containsKey(j)) { return true; } Fragment fragment = this.mFragments.get(j); return (fragment == null || (view = fragment.getView()) == null || view.getParent() == null) ? false : true; } private Long itemForViewHolder(int i) { Long l = null; for (int i2 = 0; i2 < this.mItemIdToViewHolder.size(); i2++) { if (this.mItemIdToViewHolder.valueAt(i2).intValue() == i) { if (l != null) { throw new IllegalStateException("Design assumption violated: a ViewHolder can only be bound to one item at a time."); } l = Long.valueOf(this.mItemIdToViewHolder.keyAt(i2)); } } return l; } private void ensureFragment(int i) { long itemId = getItemId(i); if (this.mFragments.containsKey(itemId)) { return; } Fragment createFragment = createFragment(i); createFragment.setInitialSavedState(this.mSavedStates.get(itemId)); this.mFragments.put(itemId, createFragment); } @Override // androidx.recyclerview.widget.RecyclerView.Adapter public final void onViewAttachedToWindow(FragmentViewHolder fragmentViewHolder) { placeFragmentInViewHolder(fragmentViewHolder); gcFragments(); } void placeFragmentInViewHolder(final FragmentViewHolder fragmentViewHolder) { Fragment fragment = this.mFragments.get(fragmentViewHolder.getItemId()); if (fragment == null) { throw new IllegalStateException("Design assumption violated."); } FrameLayout container = fragmentViewHolder.getContainer(); View view = fragment.getView(); if (!fragment.isAdded() && view != null) { throw new IllegalStateException("Design assumption violated."); } if (fragment.isAdded() && view == null) { scheduleViewAttach(fragment, container); return; } if (fragment.isAdded() && view.getParent() != null) { if (view.getParent() != container) { addViewToContainer(view, container); return; } return; } if (fragment.isAdded()) { addViewToContainer(view, container); return; } if (!shouldDelayFragmentTransactions()) { scheduleViewAttach(fragment, container); List dispatchPreAdded = this.mFragmentEventDispatcher.dispatchPreAdded(fragment); try { fragment.setMenuVisibility(false); this.mFragmentManager.beginTransaction().add(fragment, "f" + fragmentViewHolder.getItemId()).setMaxLifecycle(fragment, Lifecycle.State.STARTED).commitNow(); this.mFragmentMaxLifecycleEnforcer.updateFragmentMaxLifecycle(false); return; } finally { this.mFragmentEventDispatcher.dispatchPostEvents(dispatchPreAdded); } } if (this.mFragmentManager.isDestroyed()) { return; } this.mLifecycle.addObserver(new LifecycleEventObserver() { // from class: androidx.viewpager2.adapter.FragmentStateAdapter.1 @Override // androidx.lifecycle.LifecycleEventObserver public void onStateChanged(LifecycleOwner lifecycleOwner, Lifecycle.Event event) { if (FragmentStateAdapter.this.shouldDelayFragmentTransactions()) { return; } lifecycleOwner.getLifecycle().removeObserver(this); if (ViewCompat.isAttachedToWindow(fragmentViewHolder.getContainer())) { FragmentStateAdapter.this.placeFragmentInViewHolder(fragmentViewHolder); } } }); } private void scheduleViewAttach(final Fragment fragment, final FrameLayout frameLayout) { this.mFragmentManager.registerFragmentLifecycleCallbacks(new FragmentManager.FragmentLifecycleCallbacks() { // from class: androidx.viewpager2.adapter.FragmentStateAdapter.2 @Override // androidx.fragment.app.FragmentManager.FragmentLifecycleCallbacks public void onFragmentViewCreated(FragmentManager fragmentManager, Fragment fragment2, View view, Bundle bundle) { if (fragment2 == fragment) { fragmentManager.unregisterFragmentLifecycleCallbacks(this); FragmentStateAdapter.this.addViewToContainer(view, frameLayout); } } }, false); } void addViewToContainer(View view, FrameLayout frameLayout) { if (frameLayout.getChildCount() > 1) { throw new IllegalStateException("Design assumption violated."); } if (view.getParent() == frameLayout) { return; } if (frameLayout.getChildCount() > 0) { frameLayout.removeAllViews(); } if (view.getParent() != null) { ((ViewGroup) view.getParent()).removeView(view); } frameLayout.addView(view); } @Override // androidx.recyclerview.widget.RecyclerView.Adapter public final void onViewRecycled(FragmentViewHolder fragmentViewHolder) { Long itemForViewHolder = itemForViewHolder(fragmentViewHolder.getContainer().getId()); if (itemForViewHolder != null) { removeFragment(itemForViewHolder.longValue()); this.mItemIdToViewHolder.remove(itemForViewHolder.longValue()); } } private void removeFragment(long j) { ViewParent parent; Fragment fragment = this.mFragments.get(j); if (fragment == null) { return; } if (fragment.getView() != null && (parent = fragment.getView().getParent()) != null) { ((FrameLayout) parent).removeAllViews(); } if (!containsItem(j)) { this.mSavedStates.remove(j); } if (!fragment.isAdded()) { this.mFragments.remove(j); return; } if (shouldDelayFragmentTransactions()) { this.mHasStaleFragments = true; return; } if (fragment.isAdded() && containsItem(j)) { List dispatchPreSavedInstanceState = this.mFragmentEventDispatcher.dispatchPreSavedInstanceState(fragment); Fragment.SavedState saveFragmentInstanceState = this.mFragmentManager.saveFragmentInstanceState(fragment); this.mFragmentEventDispatcher.dispatchPostEvents(dispatchPreSavedInstanceState); this.mSavedStates.put(j, saveFragmentInstanceState); } List dispatchPreRemoved = this.mFragmentEventDispatcher.dispatchPreRemoved(fragment); try { this.mFragmentManager.beginTransaction().remove(fragment).commitNow(); this.mFragments.remove(j); } finally { this.mFragmentEventDispatcher.dispatchPostEvents(dispatchPreRemoved); } } boolean shouldDelayFragmentTransactions() { return this.mFragmentManager.isStateSaved(); } public boolean containsItem(long j) { return j >= 0 && j < ((long) getItemCount()); } @Override // androidx.recyclerview.widget.RecyclerView.Adapter public final void setHasStableIds(boolean z) { throw new UnsupportedOperationException("Stable Ids are required for the adapter to function properly, and the adapter takes care of setting the flag."); } @Override // androidx.viewpager2.adapter.StatefulAdapter public final Parcelable saveState() { Bundle bundle = new Bundle(this.mFragments.size() + this.mSavedStates.size()); for (int i = 0; i < this.mFragments.size(); i++) { long keyAt = this.mFragments.keyAt(i); Fragment fragment = this.mFragments.get(keyAt); if (fragment != null && fragment.isAdded()) { this.mFragmentManager.putFragment(bundle, createKey(KEY_PREFIX_FRAGMENT, keyAt), fragment); } } for (int i2 = 0; i2 < this.mSavedStates.size(); i2++) { long keyAt2 = this.mSavedStates.keyAt(i2); if (containsItem(keyAt2)) { bundle.putParcelable(createKey(KEY_PREFIX_STATE, keyAt2), this.mSavedStates.get(keyAt2)); } } return bundle; } @Override // androidx.viewpager2.adapter.StatefulAdapter public final void restoreState(Parcelable parcelable) { if (!this.mSavedStates.isEmpty() || !this.mFragments.isEmpty()) { throw new IllegalStateException("Expected the adapter to be 'fresh' while restoring state."); } Bundle bundle = (Bundle) parcelable; if (bundle.getClassLoader() == null) { bundle.setClassLoader(getClass().getClassLoader()); } for (String str : bundle.keySet()) { if (isValidKey(str, KEY_PREFIX_FRAGMENT)) { this.mFragments.put(parseIdFromKey(str, KEY_PREFIX_FRAGMENT), this.mFragmentManager.getFragment(bundle, str)); } else if (isValidKey(str, KEY_PREFIX_STATE)) { long parseIdFromKey = parseIdFromKey(str, KEY_PREFIX_STATE); Fragment.SavedState savedState = (Fragment.SavedState) bundle.getParcelable(str); if (containsItem(parseIdFromKey)) { this.mSavedStates.put(parseIdFromKey, savedState); } } else { throw new IllegalArgumentException("Unexpected key in savedState: " + str); } } if (this.mFragments.isEmpty()) { return; } this.mHasStaleFragments = true; this.mIsInGracePeriod = true; gcFragments(); scheduleGracePeriodEnd(); } private void scheduleGracePeriodEnd() { final Handler handler = new Handler(Looper.getMainLooper()); final Runnable runnable = new Runnable() { // from class: androidx.viewpager2.adapter.FragmentStateAdapter.3 @Override // java.lang.Runnable public void run() { FragmentStateAdapter.this.mIsInGracePeriod = false; FragmentStateAdapter.this.gcFragments(); } }; this.mLifecycle.addObserver(new LifecycleEventObserver() { // from class: androidx.viewpager2.adapter.FragmentStateAdapter.4 @Override // androidx.lifecycle.LifecycleEventObserver public void onStateChanged(LifecycleOwner lifecycleOwner, Lifecycle.Event event) { if (event == Lifecycle.Event.ON_DESTROY) { handler.removeCallbacks(runnable); lifecycleOwner.getLifecycle().removeObserver(this); } } }); handler.postDelayed(runnable, 10000L); } private static String createKey(String str, long j) { return str + j; } private static boolean isValidKey(String str, String str2) { return str.startsWith(str2) && str.length() > str2.length(); } private static long parseIdFromKey(String str, String str2) { return Long.parseLong(str.substring(str2.length())); } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public class FragmentMaxLifecycleEnforcer { private RecyclerView.AdapterDataObserver mDataObserver; private LifecycleEventObserver mLifecycleObserver; private ViewPager2.OnPageChangeCallback mPageChangeCallback; private long mPrimaryItemId = -1; private ViewPager2 mViewPager; FragmentMaxLifecycleEnforcer() { } void register(RecyclerView recyclerView) { this.mViewPager = inferViewPager(recyclerView); ViewPager2.OnPageChangeCallback onPageChangeCallback = new ViewPager2.OnPageChangeCallback() { // from class: androidx.viewpager2.adapter.FragmentStateAdapter.FragmentMaxLifecycleEnforcer.1 @Override // androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback public void onPageScrollStateChanged(int i) { FragmentMaxLifecycleEnforcer.this.updateFragmentMaxLifecycle(false); } @Override // androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback public void onPageSelected(int i) { FragmentMaxLifecycleEnforcer.this.updateFragmentMaxLifecycle(false); } }; this.mPageChangeCallback = onPageChangeCallback; this.mViewPager.registerOnPageChangeCallback(onPageChangeCallback); DataSetChangeObserver dataSetChangeObserver = new DataSetChangeObserver() { // from class: androidx.viewpager2.adapter.FragmentStateAdapter.FragmentMaxLifecycleEnforcer.2 @Override // androidx.viewpager2.adapter.FragmentStateAdapter.DataSetChangeObserver, androidx.recyclerview.widget.RecyclerView.AdapterDataObserver public void onChanged() { FragmentMaxLifecycleEnforcer.this.updateFragmentMaxLifecycle(true); } }; this.mDataObserver = dataSetChangeObserver; FragmentStateAdapter.this.registerAdapterDataObserver(dataSetChangeObserver); this.mLifecycleObserver = new LifecycleEventObserver() { // from class: androidx.viewpager2.adapter.FragmentStateAdapter.FragmentMaxLifecycleEnforcer.3 @Override // androidx.lifecycle.LifecycleEventObserver public void onStateChanged(LifecycleOwner lifecycleOwner, Lifecycle.Event event) { FragmentMaxLifecycleEnforcer.this.updateFragmentMaxLifecycle(false); } }; FragmentStateAdapter.this.mLifecycle.addObserver(this.mLifecycleObserver); } void unregister(RecyclerView recyclerView) { inferViewPager(recyclerView).unregisterOnPageChangeCallback(this.mPageChangeCallback); FragmentStateAdapter.this.unregisterAdapterDataObserver(this.mDataObserver); FragmentStateAdapter.this.mLifecycle.removeObserver(this.mLifecycleObserver); this.mViewPager = null; } void updateFragmentMaxLifecycle(boolean z) { int currentItem; Fragment fragment; if (FragmentStateAdapter.this.shouldDelayFragmentTransactions() || this.mViewPager.getScrollState() != 0 || FragmentStateAdapter.this.mFragments.isEmpty() || FragmentStateAdapter.this.getItemCount() == 0 || (currentItem = this.mViewPager.getCurrentItem()) >= FragmentStateAdapter.this.getItemCount()) { return; } long itemId = FragmentStateAdapter.this.getItemId(currentItem); if ((itemId != this.mPrimaryItemId || z) && (fragment = FragmentStateAdapter.this.mFragments.get(itemId)) != null && fragment.isAdded()) { this.mPrimaryItemId = itemId; FragmentTransaction beginTransaction = FragmentStateAdapter.this.mFragmentManager.beginTransaction(); ArrayList arrayList = new ArrayList(); Fragment fragment2 = null; for (int i = 0; i < FragmentStateAdapter.this.mFragments.size(); i++) { long keyAt = FragmentStateAdapter.this.mFragments.keyAt(i); Fragment valueAt = FragmentStateAdapter.this.mFragments.valueAt(i); if (valueAt.isAdded()) { if (keyAt != this.mPrimaryItemId) { beginTransaction.setMaxLifecycle(valueAt, Lifecycle.State.STARTED); arrayList.add(FragmentStateAdapter.this.mFragmentEventDispatcher.dispatchMaxLifecyclePreUpdated(valueAt, Lifecycle.State.STARTED)); } else { fragment2 = valueAt; } valueAt.setMenuVisibility(keyAt == this.mPrimaryItemId); } } if (fragment2 != null) { beginTransaction.setMaxLifecycle(fragment2, Lifecycle.State.RESUMED); arrayList.add(FragmentStateAdapter.this.mFragmentEventDispatcher.dispatchMaxLifecyclePreUpdated(fragment2, Lifecycle.State.RESUMED)); } if (beginTransaction.isEmpty()) { return; } beginTransaction.commitNow(); Collections.reverse(arrayList); Iterator it = arrayList.iterator(); while (it.hasNext()) { FragmentStateAdapter.this.mFragmentEventDispatcher.dispatchPostEvents((List) it.next()); } } } private ViewPager2 inferViewPager(RecyclerView recyclerView) { ViewParent parent = recyclerView.getParent(); if (parent instanceof ViewPager2) { return (ViewPager2) parent; } throw new IllegalStateException("Expected ViewPager2 instance. Got: " + parent); } } /* loaded from: classes2.dex */ private static abstract class DataSetChangeObserver extends RecyclerView.AdapterDataObserver { @Override // androidx.recyclerview.widget.RecyclerView.AdapterDataObserver public abstract void onChanged(); private DataSetChangeObserver() { } @Override // androidx.recyclerview.widget.RecyclerView.AdapterDataObserver public final void onItemRangeChanged(int i, int i2) { onChanged(); } @Override // androidx.recyclerview.widget.RecyclerView.AdapterDataObserver public final void onItemRangeChanged(int i, int i2, Object obj) { onChanged(); } @Override // androidx.recyclerview.widget.RecyclerView.AdapterDataObserver public final void onItemRangeInserted(int i, int i2) { onChanged(); } @Override // androidx.recyclerview.widget.RecyclerView.AdapterDataObserver public final void onItemRangeRemoved(int i, int i2) { onChanged(); } @Override // androidx.recyclerview.widget.RecyclerView.AdapterDataObserver public final void onItemRangeMoved(int i, int i2, int i3) { onChanged(); } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public static class FragmentEventDispatcher { private List mCallbacks = new CopyOnWriteArrayList(); FragmentEventDispatcher() { } public void registerCallback(FragmentTransactionCallback fragmentTransactionCallback) { this.mCallbacks.add(fragmentTransactionCallback); } public void unregisterCallback(FragmentTransactionCallback fragmentTransactionCallback) { this.mCallbacks.remove(fragmentTransactionCallback); } public List dispatchMaxLifecyclePreUpdated(Fragment fragment, Lifecycle.State state) { ArrayList arrayList = new ArrayList(); Iterator it = this.mCallbacks.iterator(); while (it.hasNext()) { arrayList.add(it.next().onFragmentMaxLifecyclePreUpdated(fragment, state)); } return arrayList; } public void dispatchPostEvents(List list) { Iterator it = list.iterator(); while (it.hasNext()) { it.next().onPost(); } } public List dispatchPreAdded(Fragment fragment) { ArrayList arrayList = new ArrayList(); Iterator it = this.mCallbacks.iterator(); while (it.hasNext()) { arrayList.add(it.next().onFragmentPreAdded(fragment)); } return arrayList; } public List dispatchPreSavedInstanceState(Fragment fragment) { ArrayList arrayList = new ArrayList(); Iterator it = this.mCallbacks.iterator(); while (it.hasNext()) { arrayList.add(it.next().onFragmentPreSavedInstanceState(fragment)); } return arrayList; } public List dispatchPreRemoved(Fragment fragment) { ArrayList arrayList = new ArrayList(); Iterator it = this.mCallbacks.iterator(); while (it.hasNext()) { arrayList.add(it.next().onFragmentPreRemoved(fragment)); } return arrayList; } } public void registerFragmentTransactionCallback(FragmentTransactionCallback fragmentTransactionCallback) { this.mFragmentEventDispatcher.registerCallback(fragmentTransactionCallback); } public void unregisterFragmentTransactionCallback(FragmentTransactionCallback fragmentTransactionCallback) { this.mFragmentEventDispatcher.unregisterCallback(fragmentTransactionCallback); } }