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;
import androidx.leanback.widget.GridLayoutManager;
import androidx.leanback.widget.ck;
import defpackage.sia;

/* JADX INFO: loaded from: classes.dex */
public class HorizontalGridView extends m {
    public boolean n;
    public boolean o;
    public final Paint p;
    public Bitmap q;
    public LinearGradient r;
    public int s;
    public int t;
    public Bitmap u;
    public LinearGradient v;
    public int w;
    public int x;
    public final Rect y;

    public HorizontalGridView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.p = new Paint();
        this.y = new Rect();
        this.a.setOrientation(0);
        s(context, attributeSet);
        int[] iArr = ck.c.b;
        TypedArray typedArrayObtainStyledAttributes = context.obtainStyledAttributes(attributeSet, iArr);
        sia.y(this, context, iArr, attributeSet, typedArrayObtainStyledAttributes, 0, 0);
        setRowHeight(typedArrayObtainStyledAttributes);
        setNumRows(typedArrayObtainStyledAttributes.getInt(0, 1));
        typedArrayObtainStyledAttributes.recycle();
        t();
        Paint paint = new Paint();
        this.p = paint;
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    }

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

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

    @Override // androidx.recyclerview.widget.RecyclerView, android.view.View
    public final void draw(Canvas canvas) {
        boolean z;
        boolean z2 = true;
        if (this.n) {
            int childCount = getChildCount();
            for (int i = 0; i < childCount; i++) {
                View childAt = getChildAt(i);
                this.a.getClass();
                GridLayoutManager.d dVar = (GridLayoutManager.d) childAt.getLayoutParams();
                dVar.getClass();
                if (childAt.getLeft() + dVar.e < getPaddingLeft() - this.t) {
                    z = true;
                    break;
                }
            }
            z = false;
        } else {
            z = false;
        }
        if (this.o) {
            for (int childCount2 = getChildCount() - 1; childCount2 >= 0; childCount2--) {
                View childAt2 = getChildAt(childCount2);
                this.a.getClass();
                GridLayoutManager.d dVar2 = (GridLayoutManager.d) childAt2.getLayoutParams();
                dVar2.getClass();
                if (childAt2.getRight() - dVar2.i > (getWidth() - getPaddingRight()) + this.x) {
                    break;
                }
            }
            z2 = false;
        } else {
            z2 = false;
        }
        if (!z) {
            this.q = null;
        }
        if (!z2) {
            this.u = null;
        }
        if (!z && !z2) {
            super.draw(canvas);
            return;
        }
        int paddingLeft = this.n ? (getPaddingLeft() - this.t) - this.s : 0;
        int width = this.o ? (getWidth() - getPaddingRight()) + this.x + this.w : getWidth();
        int iSave = canvas.save();
        canvas.clipRect((this.n ? this.s : 0) + paddingLeft, 0, width - (this.o ? this.w : 0), getHeight());
        super.draw(canvas);
        canvas.restoreToCount(iSave);
        Canvas canvas2 = new Canvas();
        Rect rect = this.y;
        rect.top = 0;
        rect.bottom = getHeight();
        if (z && this.s > 0) {
            Bitmap tempBitmapLow = getTempBitmapLow();
            tempBitmapLow.eraseColor(0);
            canvas2.setBitmap(tempBitmapLow);
            int iSave2 = canvas2.save();
            canvas2.clipRect(0, 0, this.s, getHeight());
            float f = -paddingLeft;
            canvas2.translate(f, 0.0f);
            super.draw(canvas2);
            canvas2.restoreToCount(iSave2);
            this.p.setShader(this.r);
            canvas2.drawRect(0.0f, 0.0f, this.s, getHeight(), this.p);
            rect.left = 0;
            rect.right = this.s;
            canvas.translate(paddingLeft, 0.0f);
            canvas.drawBitmap(tempBitmapLow, rect, rect, (Paint) null);
            canvas.translate(f, 0.0f);
        }
        if (!z2 || this.w <= 0) {
            return;
        }
        Bitmap tempBitmapHigh = getTempBitmapHigh();
        tempBitmapHigh.eraseColor(0);
        canvas2.setBitmap(tempBitmapHigh);
        int iSave3 = canvas2.save();
        canvas2.clipRect(0, 0, this.w, getHeight());
        canvas2.translate(-(width - this.w), 0.0f);
        super.draw(canvas2);
        canvas2.restoreToCount(iSave3);
        this.p.setShader(this.v);
        canvas2.drawRect(0.0f, 0.0f, this.w, getHeight(), this.p);
        rect.left = 0;
        rect.right = this.w;
        canvas.translate(width - r3, 0.0f);
        canvas.drawBitmap(tempBitmapHigh, rect, rect, (Paint) null);
        canvas.translate(-(width - this.w), 0.0f);
    }

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

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

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

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

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

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

    public final void setFadingLeftEdge(boolean z) {
        if (this.n != z) {
            this.n = z;
            if (!z) {
                this.q = null;
            }
            invalidate();
            t();
        }
    }

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

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

    public final void setFadingRightEdge(boolean z) {
        if (this.o != z) {
            this.o = z;
            if (!z) {
                this.u = null;
            }
            invalidate();
            t();
        }
    }

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

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

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

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

    public final void t() {
        if (this.n || this.o) {
            setLayerType(2, null);
            setWillNotDraw(false);
        } else {
            setLayerType(0, null);
            setWillNotDraw(true);
        }
    }

    public void setRowHeight(int i) {
        this.a.as(i);
        requestLayout();
    }
}
