package androidx.leanback.widget;

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 AbstractC1074f {

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

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

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

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

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

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

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

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

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

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

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

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

    public HorizontalGridView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.f11813S0 = new Paint();
        this.f11822b1 = new Rect();
        this.K0.o1(0);
        g0(context, attributeSet);
        TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, Z1.a.f9145e);
        setRowHeight(typedArrayObtainStyledAttributes);
        setNumRows(typedArrayObtainStyledAttributes.getInt(0, 1));
        typedArrayObtainStyledAttributes.recycle();
        h0();
        Paint paint = new Paint();
        this.f11813S0 = paint;
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    }

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

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

    @Override // androidx.recyclerview.widget.RecyclerView, android.view.View
    public final void draw(Canvas canvas) {
        boolean z6;
        boolean z7 = this.f11811Q0;
        C1084p c1084p = this.K0;
        boolean z8 = true;
        if (z7) {
            int childCount = getChildCount();
            for (int i6 = 0; i6 < childCount; i6++) {
                View childAt = getChildAt(i6);
                c1084p.getClass();
                C1081m c1081m = (C1081m) childAt.getLayoutParams();
                c1081m.getClass();
                if (childAt.getLeft() + c1081m.f12008e < getPaddingLeft() - this.f11817W0) {
                    z6 = true;
                    break;
                }
            }
            z6 = false;
        } else {
            z6 = false;
        }
        if (this.f11812R0) {
            for (int childCount2 = getChildCount() - 1; childCount2 >= 0; childCount2--) {
                View childAt2 = getChildAt(childCount2);
                c1084p.getClass();
                C1081m c1081m2 = (C1081m) childAt2.getLayoutParams();
                c1081m2.getClass();
                if (childAt2.getRight() - c1081m2.f12010g > (getWidth() - getPaddingRight()) + this.f11821a1) {
                    break;
                }
            }
            z8 = false;
        } else {
            z8 = false;
        }
        if (!z6) {
            this.f11814T0 = null;
        }
        if (!z8) {
            this.f11818X0 = null;
        }
        if (!z6 && !z8) {
            super.draw(canvas);
            return;
        }
        int paddingLeft = this.f11811Q0 ? (getPaddingLeft() - this.f11817W0) - this.f11816V0 : 0;
        int width = this.f11812R0 ? (getWidth() - getPaddingRight()) + this.f11821a1 + this.f11820Z0 : getWidth();
        int iSave = canvas.save();
        canvas.clipRect((this.f11811Q0 ? this.f11816V0 : 0) + paddingLeft, 0, width - (this.f11812R0 ? this.f11820Z0 : 0), getHeight());
        super.draw(canvas);
        canvas.restoreToCount(iSave);
        Canvas canvas2 = new Canvas();
        Rect rect = this.f11822b1;
        rect.top = 0;
        rect.bottom = getHeight();
        if (z6 && this.f11816V0 > 0) {
            Bitmap tempBitmapLow = getTempBitmapLow();
            tempBitmapLow.eraseColor(0);
            canvas2.setBitmap(tempBitmapLow);
            int iSave2 = canvas2.save();
            canvas2.clipRect(0, 0, this.f11816V0, getHeight());
            float f6 = -paddingLeft;
            canvas2.translate(f6, 0.0f);
            super.draw(canvas2);
            canvas2.restoreToCount(iSave2);
            this.f11813S0.setShader(this.f11815U0);
            canvas2.drawRect(0.0f, 0.0f, this.f11816V0, getHeight(), this.f11813S0);
            rect.left = 0;
            rect.right = this.f11816V0;
            canvas.translate(paddingLeft, 0.0f);
            canvas.drawBitmap(tempBitmapLow, rect, rect, (Paint) null);
            canvas.translate(f6, 0.0f);
        }
        if (!z8 || this.f11820Z0 <= 0) {
            return;
        }
        Bitmap tempBitmapHigh = getTempBitmapHigh();
        tempBitmapHigh.eraseColor(0);
        canvas2.setBitmap(tempBitmapHigh);
        int iSave3 = canvas2.save();
        canvas2.clipRect(0, 0, this.f11820Z0, getHeight());
        canvas2.translate(-(width - this.f11820Z0), 0.0f);
        super.draw(canvas2);
        canvas2.restoreToCount(iSave3);
        this.f11813S0.setShader(this.f11819Y0);
        canvas2.drawRect(0.0f, 0.0f, this.f11820Z0, getHeight(), this.f11813S0);
        rect.left = 0;
        rect.right = this.f11820Z0;
        canvas.translate(width - r4, 0.0f);
        canvas.drawBitmap(tempBitmapHigh, rect, rect, (Paint) null);
        canvas.translate(-(width - this.f11820Z0), 0.0f);
    }

    public final boolean getFadingLeftEdge() {
        return this.f11811Q0;
    }

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

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

    public final boolean getFadingRightEdge() {
        return this.f11812R0;
    }

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

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

    public final void h0() {
        if (this.f11811Q0 || this.f11812R0) {
            setLayerType(2, null);
            setWillNotDraw(false);
        } else {
            setLayerType(0, null);
            setWillNotDraw(true);
        }
    }

    public final void setFadingLeftEdge(boolean z6) {
        if (this.f11811Q0 != z6) {
            this.f11811Q0 = z6;
            if (!z6) {
                this.f11814T0 = null;
            }
            invalidate();
            h0();
        }
    }

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

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

    public final void setFadingRightEdge(boolean z6) {
        if (this.f11812R0 != z6) {
            this.f11812R0 = z6;
            if (!z6) {
                this.f11818X0 = null;
            }
            invalidate();
            h0();
        }
    }

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

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

    public void setNumRows(int i6) {
        C1084p c1084p = this.K0;
        if (i6 < 0) {
            c1084p.getClass();
            throw new IllegalArgumentException();
        }
        c1084p.f12038R = i6;
        requestLayout();
    }

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

    public void setRowHeight(int i6) {
        this.K0.p1(i6);
        requestLayout();
    }
}
