package androidx.leanback.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

/* JADX INFO: loaded from: classes.dex */
public class HorizontalGridView extends AbstractC0469i {

    /* JADX INFO: renamed from: k1, reason: collision with root package name */
    public boolean f8436k1;

    /* JADX INFO: renamed from: l1, reason: collision with root package name */
    public boolean f8437l1;

    /* JADX INFO: renamed from: m1, reason: collision with root package name */
    public final Paint f8438m1;

    /* JADX INFO: renamed from: n1, reason: collision with root package name */
    public Bitmap f8439n1;

    /* JADX INFO: renamed from: o1, reason: collision with root package name */
    public LinearGradient f8440o1;
    public int p1;

    /* JADX INFO: renamed from: q1, reason: collision with root package name */
    public int f8441q1;

    /* JADX INFO: renamed from: r1, reason: collision with root package name */
    public Bitmap f8442r1;

    /* JADX INFO: renamed from: s1, reason: collision with root package name */
    public LinearGradient f8443s1;

    /* JADX INFO: renamed from: t1, reason: collision with root package name */
    public int f8444t1;

    /* JADX INFO: renamed from: u1, reason: collision with root package name */
    public int f8445u1;

    /* JADX INFO: renamed from: v1, reason: collision with root package name */
    public final Rect f8446v1;

    public HorizontalGridView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.f8438m1 = new Paint();
        this.f8446v1 = new Rect();
        this.f8656d1.F1(0);
        x0(context, attributeSet);
        int[] iArr = AbstractC0454a0.f8624b;
        TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, iArr);
        M.J.i(this, context, iArr, attributeSet, typedArrayObtainStyledAttributes, 0);
        setRowHeight(typedArrayObtainStyledAttributes);
        setNumRows(typedArrayObtainStyledAttributes.getInt(0, 1));
        typedArrayObtainStyledAttributes.recycle();
        y0();
        Paint paint = new Paint();
        this.f8438m1 = paint;
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    }

    private Bitmap getTempBitmapHigh() {
        Bitmap bitmap = this.f8442r1;
        if (bitmap == null || bitmap.getWidth() != this.f8444t1 || this.f8442r1.getHeight() != getHeight()) {
            this.f8442r1 = Bitmap.createBitmap(this.f8444t1, getHeight(), Bitmap.Config.ARGB_8888);
        }
        return this.f8442r1;
    }

    private Bitmap getTempBitmapLow() {
        Bitmap bitmap = this.f8439n1;
        if (bitmap == null || bitmap.getWidth() != this.p1 || this.f8439n1.getHeight() != getHeight()) {
            this.f8439n1 = Bitmap.createBitmap(this.p1, getHeight(), Bitmap.Config.ARGB_8888);
        }
        return this.f8439n1;
    }

    @Override // androidx.recyclerview.widget.RecyclerView, android.view.View
    public final void draw(Canvas canvas) {
        boolean z8;
        boolean z9 = true;
        if (this.f8436k1) {
            int childCount = getChildCount();
            for (int i = 0; i < childCount; i++) {
                View childAt = getChildAt(i);
                this.f8656d1.getClass();
                C0490u c0490u = (C0490u) childAt.getLayoutParams();
                c0490u.getClass();
                if (childAt.getLeft() + c0490u.f8717u < getPaddingLeft() - this.f8441q1) {
                    z8 = true;
                    break;
                }
            }
            z8 = false;
        } else {
            z8 = false;
        }
        if (this.f8437l1) {
            for (int childCount2 = getChildCount() - 1; childCount2 >= 0; childCount2--) {
                View childAt2 = getChildAt(childCount2);
                this.f8656d1.getClass();
                C0490u c0490u2 = (C0490u) childAt2.getLayoutParams();
                c0490u2.getClass();
                if (childAt2.getRight() - c0490u2.f8719w > (getWidth() - getPaddingRight()) + this.f8445u1) {
                    break;
                }
            }
            z9 = false;
        } else {
            z9 = false;
        }
        if (!z8) {
            this.f8439n1 = null;
        }
        if (!z9) {
            this.f8442r1 = null;
        }
        if (!z8 && !z9) {
            super.draw(canvas);
            return;
        }
        int paddingLeft = this.f8436k1 ? (getPaddingLeft() - this.f8441q1) - this.p1 : 0;
        int width = this.f8437l1 ? (getWidth() - getPaddingRight()) + this.f8445u1 + this.f8444t1 : getWidth();
        int iSave = canvas.save();
        canvas.clipRect((this.f8436k1 ? this.p1 : 0) + paddingLeft, 0, width - (this.f8437l1 ? this.f8444t1 : 0), getHeight());
        super.draw(canvas);
        canvas.restoreToCount(iSave);
        Canvas canvas2 = new Canvas();
        Rect rect = this.f8446v1;
        rect.top = 0;
        rect.bottom = getHeight();
        if (z8 && this.p1 > 0) {
            Bitmap tempBitmapLow = getTempBitmapLow();
            tempBitmapLow.eraseColor(0);
            canvas2.setBitmap(tempBitmapLow);
            int iSave2 = canvas2.save();
            canvas2.clipRect(0, 0, this.p1, getHeight());
            float f3 = -paddingLeft;
            canvas2.translate(f3, 0.0f);
            super.draw(canvas2);
            canvas2.restoreToCount(iSave2);
            this.f8438m1.setShader(this.f8440o1);
            canvas2.drawRect(0.0f, 0.0f, this.p1, getHeight(), this.f8438m1);
            rect.left = 0;
            rect.right = this.p1;
            canvas.translate(paddingLeft, 0.0f);
            canvas.drawBitmap(tempBitmapLow, rect, rect, (Paint) null);
            canvas.translate(f3, 0.0f);
        }
        if (!z9 || this.f8444t1 <= 0) {
            return;
        }
        Bitmap tempBitmapHigh = getTempBitmapHigh();
        tempBitmapHigh.eraseColor(0);
        canvas2.setBitmap(tempBitmapHigh);
        int iSave3 = canvas2.save();
        canvas2.clipRect(0, 0, this.f8444t1, getHeight());
        canvas2.translate(-(width - this.f8444t1), 0.0f);
        super.draw(canvas2);
        canvas2.restoreToCount(iSave3);
        this.f8438m1.setShader(this.f8443s1);
        canvas2.drawRect(0.0f, 0.0f, this.f8444t1, getHeight(), this.f8438m1);
        rect.left = 0;
        rect.right = this.f8444t1;
        canvas.translate(width - r3, 0.0f);
        canvas.drawBitmap(tempBitmapHigh, rect, rect, (Paint) null);
        canvas.translate(-(width - this.f8444t1), 0.0f);
    }

    @SuppressLint({"GetterSetterNames"})
    public final boolean getFadingLeftEdge() {
        return this.f8436k1;
    }

    public final int getFadingLeftEdgeLength() {
        return this.p1;
    }

    public final int getFadingLeftEdgeOffset() {
        return this.f8441q1;
    }

    @SuppressLint({"GetterSetterNames"})
    public final boolean getFadingRightEdge() {
        return this.f8437l1;
    }

    public final int getFadingRightEdgeLength() {
        return this.f8444t1;
    }

    public final int getFadingRightEdgeOffset() {
        return this.f8445u1;
    }

    public final void setFadingLeftEdge(boolean z8) {
        if (this.f8436k1 != z8) {
            this.f8436k1 = z8;
            if (!z8) {
                this.f8439n1 = null;
            }
            invalidate();
            y0();
        }
    }

    public final void setFadingLeftEdgeLength(int i) {
        if (this.p1 != i) {
            this.p1 = i;
            if (i != 0) {
                this.f8440o1 = new LinearGradient(0.0f, 0.0f, this.p1, 0.0f, 0, -16777216, Shader.TileMode.CLAMP);
            } else {
                this.f8440o1 = null;
            }
            invalidate();
        }
    }

    public final void setFadingLeftEdgeOffset(int i) {
        if (this.f8441q1 != i) {
            this.f8441q1 = i;
            invalidate();
        }
    }

    public final void setFadingRightEdge(boolean z8) {
        if (this.f8437l1 != z8) {
            this.f8437l1 = z8;
            if (!z8) {
                this.f8442r1 = null;
            }
            invalidate();
            y0();
        }
    }

    public final void setFadingRightEdgeLength(int i) {
        if (this.f8444t1 != i) {
            this.f8444t1 = i;
            if (i != 0) {
                this.f8443s1 = new LinearGradient(0.0f, 0.0f, this.f8444t1, 0.0f, -16777216, 0, Shader.TileMode.CLAMP);
            } else {
                this.f8443s1 = null;
            }
            invalidate();
        }
    }

    public final void setFadingRightEdgeOffset(int i) {
        if (this.f8445u1 != i) {
            this.f8445u1 = i;
            invalidate();
        }
    }

    public void setNumRows(int i) {
        GridLayoutManager gridLayoutManager = this.f8656d1;
        if (i < 0) {
            gridLayoutManager.getClass();
            throw new IllegalArgumentException();
        }
        gridLayoutManager.f8416m0 = i;
        requestLayout();
    }

    public void setRowHeight(TypedArray typedArray) {
        if (typedArray.peekValue(1) != null) {
            setRowHeight(typedArray.getLayoutDimension(1, 0));
        }
    }

    public final void y0() {
        if (this.f8436k1 || this.f8437l1) {
            setLayerType(2, null);
            setWillNotDraw(false);
        } else {
            setLayerType(0, null);
            setWillNotDraw(true);
        }
    }

    public void setRowHeight(int i) {
        this.f8656d1.G1(i);
        requestLayout();
    }
}
