package androidx.cardview.widget; import android.content.res.ColorStateList; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Outline; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffColorFilter; import android.graphics.Rect; import android.graphics.RectF; import android.graphics.drawable.Drawable; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; @RequiresApi(21) public class RoundRectDrawable extends Drawable { private ColorStateList mBackground; private final RectF mBoundsF; private final Rect mBoundsI; private boolean mInsetForPadding = false; private boolean mInsetForRadius = true; private float mPadding; private final Paint mPaint; private float mRadius; private ColorStateList mTint; private PorterDuffColorFilter mTintFilter; private PorterDuff.Mode mTintMode = PorterDuff.Mode.SRC_IN; public RoundRectDrawable(ColorStateList colorStateList, float f) { this.mRadius = f; this.mPaint = new Paint(5); setBackground(colorStateList); this.mBoundsF = new RectF(); this.mBoundsI = new Rect(); } private PorterDuffColorFilter createTintFilter(ColorStateList colorStateList, PorterDuff.Mode mode) { if (colorStateList == null || mode == null) { return null; } return new PorterDuffColorFilter(colorStateList.getColorForState(getState(), 0), mode); } private void setBackground(ColorStateList colorStateList) { if (colorStateList == null) { colorStateList = ColorStateList.valueOf(0); } this.mBackground = colorStateList; this.mPaint.setColor(colorStateList.getColorForState(getState(), this.mBackground.getDefaultColor())); } private void updateBounds(Rect rect) { if (rect == null) { rect = getBounds(); } this.mBoundsF.set((float) rect.left, (float) rect.top, (float) rect.right, (float) rect.bottom); this.mBoundsI.set(rect); if (this.mInsetForPadding) { float calculateVerticalPadding = RoundRectDrawableWithShadow.calculateVerticalPadding(this.mPadding, this.mRadius, this.mInsetForRadius); this.mBoundsI.inset((int) Math.ceil((double) RoundRectDrawableWithShadow.calculateHorizontalPadding(this.mPadding, this.mRadius, this.mInsetForRadius)), (int) Math.ceil((double) calculateVerticalPadding)); this.mBoundsF.set(this.mBoundsI); } } @Override // android.graphics.drawable.Drawable public void draw(Canvas canvas) { boolean z2; Paint paint = this.mPaint; if (this.mTintFilter == null || paint.getColorFilter() != null) { z2 = false; } else { paint.setColorFilter(this.mTintFilter); z2 = true; } RectF rectF = this.mBoundsF; float f = this.mRadius; canvas.drawRoundRect(rectF, f, f, paint); if (z2) { paint.setColorFilter(null); } } public ColorStateList getColor() { return this.mBackground; } @Override // android.graphics.drawable.Drawable public int getOpacity() { return -3; } @Override // android.graphics.drawable.Drawable public void getOutline(Outline outline) { outline.setRoundRect(this.mBoundsI, this.mRadius); } public float getPadding() { return this.mPadding; } public float getRadius() { return this.mRadius; } @Override // android.graphics.drawable.Drawable public boolean isStateful() { ColorStateList colorStateList; ColorStateList colorStateList2 = this.mTint; return (colorStateList2 != null && colorStateList2.isStateful()) || ((colorStateList = this.mBackground) != null && colorStateList.isStateful()) || super.isStateful(); } @Override // android.graphics.drawable.Drawable public void onBoundsChange(Rect rect) { super.onBoundsChange(rect); updateBounds(rect); } @Override // android.graphics.drawable.Drawable public boolean onStateChange(int[] iArr) { PorterDuff.Mode mode; ColorStateList colorStateList = this.mBackground; int colorForState = colorStateList.getColorForState(iArr, colorStateList.getDefaultColor()); boolean z2 = colorForState != this.mPaint.getColor(); if (z2) { this.mPaint.setColor(colorForState); } ColorStateList colorStateList2 = this.mTint; if (colorStateList2 == null || (mode = this.mTintMode) == null) { return z2; } this.mTintFilter = createTintFilter(colorStateList2, mode); return true; } @Override // android.graphics.drawable.Drawable public void setAlpha(int i) { this.mPaint.setAlpha(i); } public void setColor(@Nullable ColorStateList colorStateList) { setBackground(colorStateList); invalidateSelf(); } @Override // android.graphics.drawable.Drawable public void setColorFilter(ColorFilter colorFilter) { this.mPaint.setColorFilter(colorFilter); } public void setPadding(float f, boolean z2, boolean z3) { if (f != this.mPadding || this.mInsetForPadding != z2 || this.mInsetForRadius != z3) { this.mPadding = f; this.mInsetForPadding = z2; this.mInsetForRadius = z3; updateBounds(null); invalidateSelf(); } } public void setRadius(float f) { if (f != this.mRadius) { this.mRadius = f; updateBounds(null); invalidateSelf(); } } @Override // android.graphics.drawable.Drawable public void setTintList(ColorStateList colorStateList) { this.mTint = colorStateList; this.mTintFilter = createTintFilter(colorStateList, this.mTintMode); invalidateSelf(); } @Override // android.graphics.drawable.Drawable public void setTintMode(PorterDuff.Mode mode) { this.mTintMode = mode; this.mTintFilter = createTintFilter(this.mTint, mode); invalidateSelf(); } }