package xyz.stream.view.tvrecyclerview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.FocusFinder;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Interpolator;
import androidx.core.view.s1;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.b2;
import androidx.recyclerview.widget.c2;
import androidx.recyclerview.widget.d1;
import androidx.recyclerview.widget.f1;
import androidx.recyclerview.widget.f2;
import androidx.recyclerview.widget.j2;
import androidx.recyclerview.widget.m2;
import androidx.recyclerview.widget.q1;
import androidx.recyclerview.widget.r1;
import androidx.recyclerview.widget.t0;
import com.google.firebase.messaging.v;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Objects;
import java.util.WeakHashMap;
import xyz.stream.view.R;

/* JADX INFO: loaded from: classes3.dex */
public class TvRecyclerView extends RecyclerView implements View.OnClickListener, View.OnFocusChangeListener {
    private static final int DEFAULT_LOAD_MORE_BEFOREHAND_COUNT = 4;
    private static final Class<?>[] LAYOUT_MANAGER_CONSTRUCTOR_SIGNATURE = {Context.class, AttributeSet.class, Integer.TYPE};
    private final IRecyclerViewDataObserver mDataObserver;
    private boolean mHasFocusWithPrevious;
    private boolean mHasMoreData;
    public int mHorizontalSpacingWithMargins;
    private boolean mIsMemoryFocus;
    private boolean mIsMenu;
    private int mLoadMoreBeforehandCount;
    private boolean mLoadingMore;
    private int mOldHorizontalSpacingWithMargins;
    private int mOldVerticalSpacingWithMargins;
    private OnInBorderKeyEventListener mOnInBorderKeyEventListener;
    private OnTvItemListener mOnItemListener;
    private OnLoadMoreListener mOnLoadMoreListener;
    private boolean mOptimizeLayout;
    private Point mScrollPoint;
    protected int mScrollX;
    protected int mScrollY;
    private boolean mSelectedItemCentered;
    private int mSelectedItemOffsetEnd;
    private int mSelectedItemOffsetStart;
    private int mSelectedPosition;
    private boolean mShouldReverseLayout;
    private final Rect mTempRect;
    public int mVerticalSpacingWithMargins;

    public class IRecyclerViewDataObserver extends f1 {
        public /* synthetic */ IRecyclerViewDataObserver(TvRecyclerView tvRecyclerView, int i10) {
            this();
        }

        @Override // androidx.recyclerview.widget.f1
        public void onChanged() {
            TvRecyclerView.this.mShouldReverseLayout = true;
        }

        private IRecyclerViewDataObserver() {
        }
    }

    public static class ISavedState extends View.BaseSavedState {
        public static final Parcelable.Creator<ISavedState> CREATOR = new Parcelable.Creator<ISavedState>() { // from class: xyz.stream.view.tvrecyclerview.TvRecyclerView.ISavedState.1
            @Override // android.os.Parcelable.Creator
            public ISavedState createFromParcel(Parcel parcel) {
                return new ISavedState(parcel);
            }

            @Override // android.os.Parcelable.Creator
            public ISavedState[] newArray(int i10) {
                return new ISavedState[i10];
            }
        };
        private boolean mHasMoreData;
        private int mHorizontalSpacingWithMargins;
        private Parcelable mISuperState;
        private boolean mIsMenu;
        private boolean mIsSelectFirstVisiblePosition;
        private int mOldHorizontalSpacingWithMargins;
        private int mOldVerticalSpacingWithMargins;
        private boolean mSelectedItemCentered;
        private int mSelectedItemOffsetEnd;
        private int mSelectedItemOffsetStart;
        private int mSelectedPosition;
        private int mVerticalSpacingWithMargins;

        public ISavedState(Parcelable parcelable) {
            super(parcelable);
        }

        @Override // android.view.View.BaseSavedState, android.view.AbsSavedState, android.os.Parcelable
        public void writeToParcel(Parcel parcel, int i10) {
            super.writeToParcel(parcel, i10);
            parcel.writeParcelable(this.mISuperState, 0);
            parcel.writeInt(this.mSelectedPosition);
            parcel.writeInt(this.mVerticalSpacingWithMargins);
            parcel.writeInt(this.mHorizontalSpacingWithMargins);
            parcel.writeInt(this.mOldVerticalSpacingWithMargins);
            parcel.writeInt(this.mOldHorizontalSpacingWithMargins);
            parcel.writeInt(this.mSelectedItemOffsetStart);
            parcel.writeInt(this.mSelectedItemOffsetEnd);
            parcel.writeBooleanArray(new boolean[]{this.mSelectedItemCentered, this.mIsMenu, this.mHasMoreData, this.mIsSelectFirstVisiblePosition});
        }

        public ISavedState(Parcel parcel) {
            super(parcel);
            this.mISuperState = parcel.readParcelable(RecyclerView.class.getClassLoader());
            this.mSelectedPosition = parcel.readInt();
            this.mVerticalSpacingWithMargins = parcel.readInt();
            this.mHorizontalSpacingWithMargins = parcel.readInt();
            this.mOldVerticalSpacingWithMargins = parcel.readInt();
            this.mOldHorizontalSpacingWithMargins = parcel.readInt();
            this.mSelectedItemOffsetStart = parcel.readInt();
            this.mSelectedItemOffsetEnd = parcel.readInt();
            boolean[] zArr = new boolean[4];
            parcel.readBooleanArray(zArr);
            this.mSelectedItemCentered = zArr[0];
            this.mIsMenu = zArr[1];
            this.mHasMoreData = zArr[2];
            this.mIsSelectFirstVisiblePosition = zArr[3];
        }
    }

    public interface OnInBorderKeyEventListener {
        boolean onInBorderKeyEvent(int i10, View view);
    }

    public interface OnLoadMoreListener {
        void onLoadMore();
    }

    public interface OnTvItemListener {
        void onItemClick(TvRecyclerView tvRecyclerView, View view, int i10);

        void onItemPreSelected(TvRecyclerView tvRecyclerView, View view, int i10);

        void onItemSelected(TvRecyclerView tvRecyclerView, View view, int i10);
    }

    public class TvSmoothScroller extends t0 {
        private boolean mIsSmooth;
        private int mOffset;
        private boolean mRequestFocus;

        public TvSmoothScroller(Context context, boolean z10, boolean z11, int i10) {
            super(context);
            this.mRequestFocus = z10;
            this.mIsSmooth = z11;
            this.mOffset = i10;
        }

        /* JADX INFO: Access modifiers changed from: private */
        public /* synthetic */ void lambda$onStop$0(int i10) {
            View viewFindViewByPosition = TvRecyclerView.this.getLayoutManager().findViewByPosition(i10);
            if (viewFindViewByPosition != null) {
                if (!TvRecyclerView.this.hasFocus()) {
                    TvRecyclerView.this.onFocusChanged(true, 130, null);
                }
                viewFindViewByPosition.requestFocus();
            }
        }

        @Override // androidx.recyclerview.widget.t0
        public int calculateDtToFit(int i10, int i11, int i12, int i13, int i14) {
            return (i12 - i10) + this.mOffset;
        }

        @Override // androidx.recyclerview.widget.t0
        public int calculateTimeForScrolling(int i10) {
            if (this.mIsSmooth) {
                return super.calculateTimeForScrolling(i10);
            }
            return (int) Math.ceil((4.0f / TvRecyclerView.this.getContext().getResources().getDisplayMetrics().densityDpi) * Math.abs(i10));
        }

        @Override // androidx.recyclerview.widget.t0, androidx.recyclerview.widget.e2
        public void onStop() {
            if (this.mRequestFocus) {
                TvRecyclerView.this.postDelayed(new a(getTargetPosition(), 0, this), this.mIsSmooth ? 400L : 100L);
            }
            super.onStop();
        }

        @Override // androidx.recyclerview.widget.t0, androidx.recyclerview.widget.e2
        public void onTargetFound(View view, f2 f2Var, c2 c2Var) {
            int freeHeight;
            int iHeight;
            if (TvRecyclerView.this.mSelectedItemCentered && getLayoutManager() != null) {
                TvRecyclerView tvRecyclerView = TvRecyclerView.this;
                tvRecyclerView.getDecoratedBoundsWithMargins(view, tvRecyclerView.mTempRect);
                if (getLayoutManager().canScrollHorizontally()) {
                    freeHeight = TvRecyclerView.this.getFreeWidth();
                    iHeight = TvRecyclerView.this.mTempRect.width();
                } else {
                    freeHeight = TvRecyclerView.this.getFreeHeight();
                    iHeight = TvRecyclerView.this.mTempRect.height();
                }
                this.mOffset = (freeHeight - iHeight) / 2;
            }
            super.onTargetFound(view, f2Var, c2Var);
        }
    }

    public TvRecyclerView(Context context) {
        this(context, null);
    }

    private void adjustPadding() {
        int i10 = this.mVerticalSpacingWithMargins;
        if (i10 >= 0 || this.mHorizontalSpacingWithMargins >= 0) {
            int i11 = i10 / 2;
            int i12 = this.mHorizontalSpacingWithMargins / 2;
            int i13 = this.mOldVerticalSpacingWithMargins / 2;
            int i14 = this.mOldHorizontalSpacingWithMargins / 2;
            setPadding((getPaddingLeft() + i14) - i12, (getPaddingTop() + i13) - i11, (getPaddingRight() + i14) - i12, (getPaddingBottom() + i13) - i11);
        }
    }

    private int computeScrollOffset(int i10, int i11, int i12, int i13) {
        if (i11 > 0) {
            return getLastVisiblePosition() != getItemCount() - 1 ? i11 + i13 : i11;
        }
        if (i10 < 0) {
            return getFirstVisiblePosition() != 0 ? i10 - i12 : i10;
        }
        if (i10 > 0 && i10 < i12 && (canScrollHorizontally(-1) || canScrollVertically(-1))) {
            return i10 - i12;
        }
        if (Math.abs(i11) <= 0 || Math.abs(i11) >= i13) {
            return 0;
        }
        if (canScrollHorizontally(1) || canScrollVertically(1)) {
            return i13 - Math.abs(i11);
        }
        return 0;
    }

    private void createLayoutManager(Context context, String str, AttributeSet attributeSet, int i10) {
        Constructor constructor;
        Object[] objArr;
        if (str != null) {
            String strTrim = str.trim();
            if (strTrim.isEmpty()) {
                return;
            }
            String fullClassName = getFullClassName(context, strTrim);
            try {
                Class<? extends U> clsAsSubclass = (isInEditMode() ? getClass().getClassLoader() : context.getClassLoader()).loadClass(fullClassName).asSubclass(q1.class);
                try {
                    constructor = clsAsSubclass.getConstructor(LAYOUT_MANAGER_CONSTRUCTOR_SIGNATURE);
                    objArr = new Object[]{context, attributeSet, Integer.valueOf(i10)};
                } catch (NoSuchMethodException e10) {
                    try {
                        constructor = clsAsSubclass.getConstructor(new Class[0]);
                        objArr = null;
                    } catch (NoSuchMethodException e11) {
                        e11.initCause(e10);
                        throw new IllegalStateException(attributeSet.getPositionDescription() + ": Error creating LayoutManager " + fullClassName, e11);
                    }
                }
                constructor.setAccessible(true);
                setLayoutManager((q1) constructor.newInstance(objArr));
            } catch (ClassCastException e12) {
                throw new IllegalStateException(attributeSet.getPositionDescription() + ": Class is not a LayoutManager " + fullClassName, e12);
            } catch (ClassNotFoundException e13) {
                throw new IllegalStateException(attributeSet.getPositionDescription() + ": Unable to find LayoutManager " + fullClassName, e13);
            } catch (IllegalAccessException e14) {
                throw new IllegalStateException(attributeSet.getPositionDescription() + ": Cannot access non-public constructor " + fullClassName, e14);
            } catch (InstantiationException e15) {
                throw new IllegalStateException(attributeSet.getPositionDescription() + ": Could not instantiate the LayoutManager: " + fullClassName, e15);
            } catch (InvocationTargetException e16) {
                throw new IllegalStateException(attributeSet.getPositionDescription() + ": Could not instantiate the LayoutManager: " + fullClassName, e16);
            }
        }
    }

    private View findNextFocus(int i10) {
        return FocusFinder.getInstance().findNextFocus(this, getFocusedChild(), i10);
    }

    private int[] getChildRectangleOnScreenScrollAmount2(View view, Rect rect, int i10, int i11) {
        int i12;
        int iComputeScrollOffset;
        int iComputeScrollOffset2 = 0;
        if (getLayoutManager() != null) {
            getDecoratedBoundsWithMargins(view, this.mTempRect);
            if (getLayoutManager().canScrollHorizontally()) {
                iComputeScrollOffset = computeScrollOffset(this.mTempRect.left - getPaddingLeft(), (getPaddingRight() + this.mTempRect.right) - getWidth(), i10, i11);
            } else {
                iComputeScrollOffset = 0;
            }
            if (getLayoutManager().canScrollVertically()) {
                iComputeScrollOffset2 = computeScrollOffset(this.mTempRect.top - getPaddingTop(), (getPaddingBottom() + this.mTempRect.bottom) - getHeight(), i10, i11);
            }
            int i13 = iComputeScrollOffset2;
            iComputeScrollOffset2 = iComputeScrollOffset;
            i12 = i13;
        } else {
            i12 = 0;
        }
        return new int[]{iComputeScrollOffset2, i12};
    }

    /* JADX INFO: Access modifiers changed from: private */
    public int getFreeHeight() {
        return (getHeight() - getPaddingTop()) - getPaddingBottom();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public int getFreeWidth() {
        return (getWidth() - getPaddingLeft()) - getPaddingRight();
    }

    private String getFullClassName(Context context, String str) {
        if (str.charAt(0) == '.') {
            return context.getPackageName() + str;
        }
        if (str.contains(".")) {
            return str;
        }
        return TvRecyclerView.class.getPackage().getName() + '.' + str;
    }

    private boolean hasInBorder(int i10, View view, View view2) {
        if (i10 == 17) {
            if (view2 != null) {
                return false;
            }
            return !canScrollHorizontally(-1);
        }
        if (i10 == 33) {
            if (view2 != null) {
                return false;
            }
            return !canScrollVertically(-1);
        }
        if (i10 == 66) {
            boolean zCanScrollHorizontally = canScrollHorizontally(1);
            return (view2 == null || zCanScrollHorizontally) ? !zCanScrollHorizontally : view != null && Math.abs(((getWidth() - view.getRight()) - ((ViewGroup.MarginLayoutParams) view.getLayoutParams()).rightMargin) - getPaddingRight()) <= 2;
        }
        if (i10 != 130) {
            return false;
        }
        boolean zCanScrollVertically = canScrollVertically(1);
        return (view2 == null || zCanScrollVertically) ? !zCanScrollVertically : view != null && Math.abs(((getHeight() - view.getBottom()) - ((ViewGroup.MarginLayoutParams) view.getLayoutParams()).bottomMargin) - getPaddingBottom()) <= 2;
    }

    private void init(Context context) {
        setChildrenDrawingOrderEnabled(true);
        setWillNotDraw(true);
        setHasFixedSize(true);
        setOverScrollMode(2);
        setClipChildren(false);
        setClipToPadding(false);
        setFocusable(true);
        setFocusableInTouchMode(true);
        if (getItemAnimator() != null) {
            ((m2) getItemAnimator()).f3783g = false;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$onFocusChange$0(boolean z10, View view) {
        if (hasFocus()) {
            return;
        }
        if (this.mIsMenu && !z10) {
            view.setActivated(true);
        }
        if (getOnFocusChangeListener() != null) {
            getOnFocusChangeListener().onFocusChange(this, false);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$setSelection$1(int i10) {
        if (getAdapter() == null || i10 < 0 || i10 >= getItemCount()) {
            return;
        }
        this.mSelectedPosition = i10;
        View viewFindViewByPosition = getLayoutManager() != null ? getLayoutManager().findViewByPosition(i10) : null;
        if (viewFindViewByPosition != null) {
            if (!hasFocus()) {
                onFocusChanged(true, 130, null);
            }
            viewFindViewByPosition.requestFocus();
        } else {
            TvSmoothScroller tvSmoothScroller = new TvSmoothScroller(getContext(), true, false, this.mSelectedItemOffsetStart);
            tvSmoothScroller.setTargetPosition(i10);
            q1 layoutManager = getLayoutManager();
            Objects.requireNonNull(layoutManager);
            layoutManager.startSmoothScroll(tvSmoothScroller);
        }
    }

    private void resetAdapter(d1 d1Var, boolean z10) {
        d1 adapter = getAdapter();
        if (adapter != null) {
            adapter.unregisterAdapterDataObserver(this.mDataObserver);
            this.mShouldReverseLayout = true;
        }
        d1Var.registerAdapterDataObserver(this.mDataObserver);
        View childAt = getChildAt(0);
        if (childAt == null || adapter == null || !(getLayoutManager() instanceof BaseLayoutManager)) {
            this.mSelectedPosition = getFirstVisibleAndFocusablePosition();
        } else {
            this.mHasFocusWithPrevious = hasFocus();
            int decoratedTop = (getLayoutManager().canScrollVertically() ? getLayoutManager().getDecoratedTop(childAt) : getLayoutManager().getDecoratedLeft(childAt)) - (getLayoutManager().canScrollVertically() ? getPaddingTop() : getPaddingLeft());
            scrollBy(decoratedTop, decoratedTop);
        }
        if (z10) {
            this.mSelectedPosition = -1;
        }
    }

    @Override // androidx.recyclerview.widget.RecyclerView, android.view.ViewGroup, android.view.View
    public void addFocusables(ArrayList<View> arrayList, int i10, int i11) {
        if (hasFocus() || !isFocusable()) {
            super.addFocusables(arrayList, i10, i11);
        } else {
            arrayList.add(this);
        }
    }

    @Override // android.view.View
    public boolean canScrollHorizontally(int i10) {
        if (getLayoutManager() == null || !(getLayoutManager() instanceof TwoWayLayoutManager)) {
            return super.canScrollHorizontally(i10);
        }
        TwoWayLayoutManager twoWayLayoutManager = (TwoWayLayoutManager) getLayoutManager();
        return twoWayLayoutManager.canScrollHorizontally() && (i10 <= 0 ? !twoWayLayoutManager.cannotScrollForward(i10) : !twoWayLayoutManager.cannotScrollBackward(i10));
    }

    @Override // android.view.View
    public boolean canScrollVertically(int i10) {
        if (getLayoutManager() == null || !(getLayoutManager() instanceof TwoWayLayoutManager)) {
            return super.canScrollVertically(i10);
        }
        TwoWayLayoutManager twoWayLayoutManager = (TwoWayLayoutManager) getLayoutManager();
        return twoWayLayoutManager.canScrollVertically() && (i10 <= 0 ? !twoWayLayoutManager.cannotScrollForward(i10) : !twoWayLayoutManager.cannotScrollBackward(i10));
    }

    @Override // androidx.recyclerview.widget.RecyclerView, android.view.ViewGroup
    public boolean checkLayoutParams(ViewGroup.LayoutParams layoutParams) {
        int i10;
        boolean zCheckLayoutParams = super.checkLayoutParams(layoutParams);
        if (zCheckLayoutParams && ((i10 = this.mVerticalSpacingWithMargins) >= 0 || this.mHorizontalSpacingWithMargins >= 0)) {
            int i11 = i10 / 2;
            int i12 = this.mHorizontalSpacingWithMargins / 2;
            ((r1) layoutParams).setMargins(i12, i11, i12, i11);
        }
        return zCheckLayoutParams;
    }

    @Override // android.view.ViewGroup, android.view.View
    public boolean dispatchUnhandledMove(View view, int i10) {
        return super.dispatchUnhandledMove(view, i10);
    }

    public void finishLoadMore() {
        this.mLoadingMore = false;
    }

    public void finishLoadMoreWithNoMore() {
        this.mLoadingMore = false;
        setHasMoreData(false);
    }

    @Override // androidx.recyclerview.widget.RecyclerView, android.view.ViewGroup, android.view.ViewParent
    public View focusSearch(View view, int i10) {
        View viewFindNextFocus = findNextFocus(i10);
        if (!hasInBorder(i10, view, viewFindNextFocus)) {
            return viewFindNextFocus;
        }
        OnInBorderKeyEventListener onInBorderKeyEventListener = this.mOnInBorderKeyEventListener;
        if (onInBorderKeyEventListener == null || !onInBorderKeyEventListener.onInBorderKeyEvent(i10, view)) {
            return super.focusSearch(view, i10);
        }
        return null;
    }

    @Override // androidx.recyclerview.widget.RecyclerView, android.view.ViewGroup
    public int getChildDrawingOrder(int i10, int i11) {
        View focusedChild = getFocusedChild();
        if (focusedChild != null) {
            int iIndexOfChild = indexOfChild(focusedChild);
            int i12 = i10 - 1;
            if (i11 == i12) {
                return iIndexOfChild > i11 ? i11 : iIndexOfChild;
            }
            if (iIndexOfChild == i11) {
                return i12;
            }
        }
        return i11;
    }

    public int getFirstVisibleAndFocusablePosition() {
        for (int firstVisiblePosition = getFirstVisiblePosition(); firstVisiblePosition < getChildCount(); firstVisiblePosition++) {
            View viewFindViewByPosition = getLayoutManager().findViewByPosition(firstVisiblePosition);
            if (viewFindViewByPosition != null && viewFindViewByPosition.isFocusable()) {
                return firstVisiblePosition;
            }
        }
        return -1;
    }

    public int getFirstVisiblePosition() {
        if (getChildCount() == 0) {
            return 0;
        }
        return getLayoutManager() instanceof LinearLayoutManager ? ((LinearLayoutManager) getLayoutManager()).findFirstVisibleItemPosition() : getChildAdapterPosition(getChildAt(0));
    }

    public int getItemCount() {
        if (getAdapter() != null) {
            return getAdapter().getItemCount();
        }
        return 0;
    }

    public int getLastVisibleAndFocusablePosition() {
        int firstVisiblePosition = getFirstVisiblePosition();
        for (int lastVisiblePosition = getLastVisiblePosition(); lastVisiblePosition >= firstVisiblePosition; lastVisiblePosition--) {
            View viewFindViewByPosition = getLayoutManager().findViewByPosition(lastVisiblePosition);
            if (viewFindViewByPosition != null && viewFindViewByPosition.isFocusable()) {
                return lastVisiblePosition;
            }
        }
        return -1;
    }

    public int getLastVisiblePosition() {
        int childCount = getChildCount();
        if (childCount == 0) {
            return 0;
        }
        return getLayoutManager() instanceof LinearLayoutManager ? ((LinearLayoutManager) getLayoutManager()).findLastVisibleItemPosition() : getChildAdapterPosition(getChildAt(childCount - 1));
    }

    public int getLoadMoreBeforehandCount() {
        return this.mLoadMoreBeforehandCount;
    }

    public int getSelectedItemOffsetEnd() {
        return this.mSelectedItemOffsetEnd;
    }

    public int getSelectedItemOffsetStart() {
        return this.mSelectedItemOffsetStart;
    }

    public int getSelectedPosition() {
        return this.mSelectedPosition;
    }

    public boolean hasMoreData() {
        return this.mHasMoreData;
    }

    @Override // android.view.View
    public boolean isInEditMode() {
        return true;
    }

    public boolean isLoadingMore() {
        return this.mLoadingMore;
    }

    public boolean isMemoryFocus() {
        return this.mIsMemoryFocus;
    }

    public boolean isMenu() {
        return this.mIsMenu;
    }

    public boolean isScrolling() {
        return getScrollState() != 0;
    }

    public boolean isSelectedItemCentered() {
        return this.mSelectedItemCentered;
    }

    @Override // androidx.recyclerview.widget.RecyclerView
    public void onChildAttachedToWindow(View view) {
        if (view.isClickable()) {
            WeakHashMap weakHashMap = s1.f1815a;
            if (!view.hasOnClickListeners()) {
                view.setOnClickListener(this);
            }
        }
        if (view.isFocusable() && view.getOnFocusChangeListener() == null) {
            view.setOnFocusChangeListener(this);
        }
    }

    @Override // android.view.View.OnClickListener
    public void onClick(View view) {
        OnTvItemListener onTvItemListener = this.mOnItemListener;
        if (onTvItemListener == null || this == view) {
            return;
        }
        onTvItemListener.onItemClick(this, view, getChildAdapterPosition(view));
    }

    @Override // android.view.View.OnFocusChangeListener
    public void onFocusChange(View view, boolean z10) {
        if (view != null) {
            int childAdapterPosition = getChildAdapterPosition(view);
            boolean z11 = view instanceof RecyclerView;
            if (!z11) {
                view.setSelected(z10);
            }
            if (!z10) {
                view.postDelayed(new v(this, z11, view, 5), 6L);
                OnTvItemListener onTvItemListener = this.mOnItemListener;
                if (onTvItemListener == null || z11) {
                    return;
                }
                onTvItemListener.onItemPreSelected(this, view, childAdapterPosition);
                return;
            }
            this.mSelectedPosition = childAdapterPosition;
            if (z11) {
                return;
            }
            if (this.mIsMenu && view.isActivated()) {
                view.setActivated(false);
            }
            OnTvItemListener onTvItemListener2 = this.mOnItemListener;
            if (onTvItemListener2 != null) {
                onTvItemListener2.onItemSelected(this, view, childAdapterPosition);
            }
        }
    }

    @Override // androidx.recyclerview.widget.RecyclerView, android.view.ViewGroup, android.view.View
    public void onLayout(boolean z10, int i10, int i11, int i12, int i13) {
        System.currentTimeMillis();
        this.mHasFocusWithPrevious = this.mHasFocusWithPrevious || hasFocus();
        if (!this.mOptimizeLayout || z10 || this.mShouldReverseLayout) {
            super.onLayout(z10, i10, i11, i12, i13);
            this.mShouldReverseLayout = false;
            if (!hasFocus()) {
                int i14 = this.mSelectedPosition;
                if (i14 < 0) {
                    this.mSelectedPosition = getFirstVisibleAndFocusablePosition();
                } else if (i14 >= getItemCount()) {
                    this.mSelectedPosition = getLastVisibleAndFocusablePosition();
                }
                if (this.mHasFocusWithPrevious && getPreserveFocusAfterLayout()) {
                    requestDefaultFocus();
                } else if (this.mIsMenu) {
                    setItemActivated(this.mSelectedPosition);
                }
            }
        } else {
            hasFocus();
        }
        this.mHasFocusWithPrevious = false;
    }

    @Override // androidx.recyclerview.widget.RecyclerView, android.view.View
    public void onMeasure(int i10, int i11) {
        super.onMeasure(i10, i11);
    }

    @Override // androidx.recyclerview.widget.RecyclerView, android.view.ViewGroup
    public boolean onRequestFocusInDescendants(int i10, Rect rect) {
        int firstVisibleAndFocusablePosition = this.mSelectedPosition;
        if (firstVisibleAndFocusablePosition == -1 || !this.mIsMemoryFocus) {
            firstVisibleAndFocusablePosition = getFirstVisibleAndFocusablePosition();
        }
        View viewFindViewByPosition = getLayoutManager() != null ? getLayoutManager().findViewByPosition(firstVisibleAndFocusablePosition) : null;
        if (viewFindViewByPosition == null) {
            return super.onRequestFocusInDescendants(i10, rect);
        }
        if (getOnFocusChangeListener() != null) {
            getOnFocusChangeListener().onFocusChange(this, true);
        }
        return viewFindViewByPosition.requestFocus(i10, rect);
    }

    @Override // androidx.recyclerview.widget.RecyclerView, android.view.View
    public void onRestoreInstanceState(Parcelable parcelable) {
        if (parcelable != null) {
            if (!(parcelable instanceof ISavedState)) {
                super.onRestoreInstanceState(parcelable);
                return;
            }
            ISavedState iSavedState = (ISavedState) parcelable;
            this.mSelectedPosition = iSavedState.mSelectedPosition;
            this.mVerticalSpacingWithMargins = iSavedState.mVerticalSpacingWithMargins;
            this.mHorizontalSpacingWithMargins = iSavedState.mHorizontalSpacingWithMargins;
            this.mOldVerticalSpacingWithMargins = iSavedState.mOldVerticalSpacingWithMargins;
            this.mOldHorizontalSpacingWithMargins = iSavedState.mOldHorizontalSpacingWithMargins;
            this.mSelectedItemOffsetStart = iSavedState.mSelectedItemOffsetStart;
            this.mSelectedItemOffsetEnd = iSavedState.mSelectedItemOffsetEnd;
            this.mSelectedItemCentered = iSavedState.mSelectedItemCentered;
            this.mIsMenu = iSavedState.mIsMenu;
            this.mHasMoreData = iSavedState.mHasMoreData;
            this.mIsMemoryFocus = iSavedState.mIsSelectFirstVisiblePosition;
            try {
                super.onRestoreInstanceState(iSavedState.mISuperState);
            } catch (Exception e10) {
                e10.printStackTrace();
            }
        }
    }

    @Override // androidx.recyclerview.widget.RecyclerView, android.view.View
    public Parcelable onSaveInstanceState() {
        b2 b2Var = (b2) super.onSaveInstanceState();
        ISavedState iSavedState = new ISavedState(b2Var.getSuperState());
        iSavedState.mISuperState = b2Var;
        iSavedState.mSelectedPosition = this.mSelectedPosition;
        iSavedState.mVerticalSpacingWithMargins = this.mVerticalSpacingWithMargins;
        iSavedState.mHorizontalSpacingWithMargins = this.mHorizontalSpacingWithMargins;
        iSavedState.mOldVerticalSpacingWithMargins = this.mOldVerticalSpacingWithMargins;
        iSavedState.mOldHorizontalSpacingWithMargins = this.mOldHorizontalSpacingWithMargins;
        iSavedState.mSelectedItemOffsetStart = this.mSelectedItemOffsetStart;
        iSavedState.mSelectedItemOffsetEnd = this.mSelectedItemOffsetEnd;
        iSavedState.mSelectedItemCentered = this.mSelectedItemCentered;
        iSavedState.mIsMenu = this.mIsMenu;
        iSavedState.mHasMoreData = this.mHasMoreData;
        iSavedState.mIsSelectFirstVisiblePosition = this.mIsMemoryFocus;
        return iSavedState;
    }

    @Override // androidx.recyclerview.widget.RecyclerView
    public void onScrollStateChanged(int i10) {
        if (i10 == 0) {
            setScrollValue(0, 0);
            if (this.mOnLoadMoreListener == null || this.mLoadingMore || !this.mHasMoreData || getLastVisiblePosition() < getAdapter().getItemCount() - (this.mLoadMoreBeforehandCount + 1)) {
                return;
            }
            this.mLoadingMore = true;
            this.mOnLoadMoreListener.onLoadMore();
        }
    }

    @Override // androidx.recyclerview.widget.RecyclerView, android.view.ViewGroup, android.view.ViewParent
    public boolean requestChildRectangleOnScreen(View view, Rect rect, boolean z10) {
        int freeHeight;
        int iHeight;
        if (view == null || getLayoutManager() == null || !this.mSelectedItemCentered) {
            return false;
        }
        getDecoratedBoundsWithMargins(view, this.mTempRect);
        if (getLayoutManager().canScrollHorizontally()) {
            freeHeight = getFreeWidth();
            iHeight = this.mTempRect.width();
        } else {
            freeHeight = getFreeHeight();
            iHeight = this.mTempRect.height();
        }
        int i10 = (freeHeight - iHeight) / 2;
        this.mSelectedItemOffsetStart = i10;
        this.mSelectedItemOffsetEnd = i10;
        int[] childRectangleOnScreenScrollAmount2 = getChildRectangleOnScreenScrollAmount2(view, rect, i10, i10);
        int i11 = childRectangleOnScreenScrollAmount2[0];
        int i12 = childRectangleOnScreenScrollAmount2[1];
        smoothScrollBy(i11, i12);
        if (i11 != 0 || i12 != 0) {
            return true;
        }
        postInvalidate();
        return false;
    }

    public void requestDefaultFocus() {
        if (!this.mIsMenu && !this.mIsMemoryFocus) {
            setSelection(getFirstVisibleAndFocusablePosition());
            return;
        }
        if (this.mSelectedPosition < 0) {
            this.mSelectedPosition = getFirstVisibleAndFocusablePosition();
        }
        setSelection(this.mSelectedPosition);
    }

    @Override // androidx.recyclerview.widget.RecyclerView
    public void scrollToPosition(int i10) {
        scrollToPositionWithOffset(i10, this.mSelectedItemOffsetStart);
    }

    public void scrollToPositionWithOffset(int i10, int i11) {
        scrollToPositionWithOffset(i10, i11, false);
    }

    @Override // androidx.recyclerview.widget.RecyclerView
    public void setAdapter(d1 d1Var) {
        if (d1Var == null) {
            return;
        }
        resetAdapter(d1Var, false);
        super.setAdapter(d1Var);
    }

    public void setHasMoreData(boolean z10) {
        this.mHasMoreData = z10;
    }

    public void setItemActivated(int i10) {
        int i11 = this.mSelectedPosition;
        if (i10 != i11) {
            j2 j2VarFindViewHolderForLayoutPosition = findViewHolderForLayoutPosition(i11);
            if (j2VarFindViewHolderForLayoutPosition != null && j2VarFindViewHolderForLayoutPosition.itemView.isActivated()) {
                j2VarFindViewHolderForLayoutPosition.itemView.setActivated(false);
            }
            this.mSelectedPosition = i10;
        }
        j2 j2VarFindViewHolderForLayoutPosition2 = findViewHolderForLayoutPosition(i10);
        if (j2VarFindViewHolderForLayoutPosition2 == null || j2VarFindViewHolderForLayoutPosition2.itemView.isActivated()) {
            return;
        }
        j2VarFindViewHolderForLayoutPosition2.itemView.setActivated(true);
    }

    public void setLoadMoreBeforehandCount(int i10) {
        this.mLoadMoreBeforehandCount = i10;
    }

    public void setMemoryFocus(boolean z10) {
        this.mIsMemoryFocus = z10;
    }

    public void setMenu(boolean z10) {
        this.mIsMenu = z10;
    }

    public void setOnInBorderKeyEventListener(OnInBorderKeyEventListener onInBorderKeyEventListener) {
        this.mOnInBorderKeyEventListener = onInBorderKeyEventListener;
    }

    public void setOnItemListener(OnTvItemListener onTvItemListener) {
        this.mOnItemListener = onTvItemListener;
    }

    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
        this.mOnLoadMoreListener = onLoadMoreListener;
    }

    public void setScrollValue(int i10, int i11) {
        if (i10 == 0 && i11 == 0) {
            setTag(null);
        } else {
            this.mScrollPoint.set(i10, i11);
            setTag(this.mScrollPoint);
        }
    }

    public void setSelectedItemAtCentered(boolean z10) {
        this.mSelectedItemCentered = z10;
    }

    public void setSelectedItemOffset(int i10, int i11) {
        this.mSelectedItemOffsetStart = i10;
        this.mSelectedItemOffsetEnd = i11;
    }

    public void setSelectedPosition(int i10) {
        this.mSelectedPosition = i10;
    }

    public void setSelection(int i10) {
        post(new a(i10, 1, this));
    }

    public void setSelectionWithSmooth(int i10) {
        if (getAdapter() == null || i10 < 0 || i10 >= getItemCount()) {
            return;
        }
        this.mSelectedPosition = i10;
        TvSmoothScroller tvSmoothScroller = new TvSmoothScroller(getContext(), true, true, this.mSelectedItemOffsetStart);
        tvSmoothScroller.setTargetPosition(i10);
        getLayoutManager().startSmoothScroll(tvSmoothScroller);
    }

    public void setSpacingWithMargins(int i10, int i11) {
        int i12 = this.mVerticalSpacingWithMargins;
        if (i12 == i10 && this.mHorizontalSpacingWithMargins == i11) {
            return;
        }
        this.mOldVerticalSpacingWithMargins = i12;
        this.mOldHorizontalSpacingWithMargins = this.mHorizontalSpacingWithMargins;
        this.mVerticalSpacingWithMargins = i10;
        this.mHorizontalSpacingWithMargins = i11;
        adjustPadding();
    }

    @Override // androidx.recyclerview.widget.RecyclerView
    public void smoothScrollBy(int i10, int i11, Interpolator interpolator) {
        setScrollValue(i10, i11);
        super.smoothScrollBy(i10, i11, interpolator);
    }

    @Override // androidx.recyclerview.widget.RecyclerView
    public void smoothScrollToPosition(int i10) {
        smoothScrollToPositionWithOffset(i10, this.mSelectedItemOffsetStart);
    }

    public void smoothScrollToPositionWithOffset(int i10, int i11) {
        smoothScrollToPositionWithOffset(i10, i11, false);
    }

    @Override // androidx.recyclerview.widget.RecyclerView
    public void swapAdapter(d1 d1Var, boolean z10) {
        if (d1Var == null) {
            return;
        }
        resetAdapter(d1Var, false);
        super.swapAdapter(d1Var, z10);
    }

    public TvRecyclerView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    private void scrollToPosition(int i10, boolean z10, boolean z11, int i11) {
        this.mSelectedPosition = i10;
        TvSmoothScroller tvSmoothScroller = new TvSmoothScroller(getContext(), z10, z11, i11);
        tvSmoothScroller.setTargetPosition(i10);
        getLayoutManager().startSmoothScroll(tvSmoothScroller);
    }

    public void scrollToPositionWithOffset(int i10, int i11, boolean z10) {
        scrollToPosition(i10, z10, false, i11);
    }

    public void smoothScrollToPositionWithOffset(int i10, int i11, boolean z10) {
        scrollToPosition(i10, z10, true, i11);
    }

    public TvRecyclerView(Context context, AttributeSet attributeSet, int i10) {
        super(context, attributeSet, i10);
        this.mVerticalSpacingWithMargins = 0;
        this.mHorizontalSpacingWithMargins = 0;
        this.mOldVerticalSpacingWithMargins = 0;
        this.mOldHorizontalSpacingWithMargins = 0;
        this.mHasMoreData = true;
        this.mLoadingMore = false;
        this.mSelectedPosition = -1;
        this.mHasFocusWithPrevious = false;
        this.mTempRect = new Rect();
        this.mDataObserver = new IRecyclerViewDataObserver(this, 0);
        this.mShouldReverseLayout = true;
        this.mScrollPoint = new Point();
        init(context);
        TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, R.styleable.TvRecyclerView, i10, 0);
        String string = typedArrayObtainStyledAttributes.getString(R.styleable.TvRecyclerView_tv_layoutManager);
        if (!TextUtils.isEmpty(string)) {
            createLayoutManager(context, string, attributeSet, i10);
        }
        this.mSelectedItemCentered = typedArrayObtainStyledAttributes.getBoolean(R.styleable.TvRecyclerView_tv_selectedItemIsCentered, false);
        this.mIsMenu = typedArrayObtainStyledAttributes.getBoolean(R.styleable.TvRecyclerView_tv_isMenu, false);
        this.mIsMemoryFocus = typedArrayObtainStyledAttributes.getBoolean(R.styleable.TvRecyclerView_tv_isMemoryFocus, true);
        this.mLoadMoreBeforehandCount = typedArrayObtainStyledAttributes.getInt(R.styleable.TvRecyclerView_tv_loadMoreBeforehandCount, 4);
        this.mSelectedItemOffsetStart = typedArrayObtainStyledAttributes.getDimensionPixelOffset(R.styleable.TvRecyclerView_tv_selectedItemOffsetStart, 0);
        this.mSelectedItemOffsetEnd = typedArrayObtainStyledAttributes.getDimensionPixelOffset(R.styleable.TvRecyclerView_tv_selectedItemOffsetEnd, 0);
        this.mOptimizeLayout = typedArrayObtainStyledAttributes.getBoolean(R.styleable.TvRecyclerView_tv_optimizeLayout, false);
        setSpacingWithMargins(typedArrayObtainStyledAttributes.getDimensionPixelOffset(R.styleable.TvRecyclerView_tv_verticalSpacingWithMargins, 0), typedArrayObtainStyledAttributes.getDimensionPixelOffset(R.styleable.TvRecyclerView_tv_horizontalSpacingWithMargins, 0));
        typedArrayObtainStyledAttributes.recycle();
    }

    public void setAdapter(d1 d1Var, boolean z10) {
        if (d1Var == null) {
            return;
        }
        resetAdapter(d1Var, z10);
        super.setAdapter(d1Var);
    }
}
