package androidx.core.graphics.drawable;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Shader;
import android.graphics.drawable.AdaptiveIconDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.Parcelable;
import android.text.TextUtils;
import androidx.versionedparcelable.CustomVersionedParcelable;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;

/* loaded from: classes.dex */
public class IconCompat extends CustomVersionedParcelable {

    /* renamed from: k, reason: collision with root package name */
    static final PorterDuff.Mode f3733k = PorterDuff.Mode.SRC_IN;

    /* renamed from: a, reason: collision with root package name */
    public int f3734a;

    /* renamed from: b, reason: collision with root package name */
    Object f3735b;

    /* renamed from: c, reason: collision with root package name */
    public byte[] f3736c;

    /* renamed from: d, reason: collision with root package name */
    public Parcelable f3737d;

    /* renamed from: e, reason: collision with root package name */
    public int f3738e;

    /* renamed from: f, reason: collision with root package name */
    public int f3739f;

    /* renamed from: g, reason: collision with root package name */
    public ColorStateList f3740g;

    /* renamed from: h, reason: collision with root package name */
    PorterDuff.Mode f3741h;

    /* renamed from: i, reason: collision with root package name */
    public String f3742i;

    /* renamed from: j, reason: collision with root package name */
    public String f3743j;

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public static class a {
        static int a(Object obj) {
            return c.a(obj);
        }

        static String b(Object obj) {
            return c.b(obj);
        }

        static int c(Object obj) {
            return c.c(obj);
        }

        static Uri d(Object obj) {
            return c.d(obj);
        }

        static Drawable e(Icon icon, Context context) {
            return icon.loadDrawable(context);
        }

        static Icon f(IconCompat iconCompat, Context context) {
            Icon createWithBitmap;
            switch (iconCompat.f3734a) {
                case -1:
                    return (Icon) iconCompat.f3735b;
                case 0:
                default:
                    throw new IllegalArgumentException("Unknown type");
                case 1:
                    createWithBitmap = Icon.createWithBitmap((Bitmap) iconCompat.f3735b);
                    break;
                case 2:
                    createWithBitmap = Icon.createWithResource(iconCompat.g(), iconCompat.f3738e);
                    break;
                case 3:
                    createWithBitmap = Icon.createWithData((byte[]) iconCompat.f3735b, iconCompat.f3738e, iconCompat.f3739f);
                    break;
                case 4:
                    createWithBitmap = Icon.createWithContentUri((String) iconCompat.f3735b);
                    break;
                case 5:
                    createWithBitmap = b.b((Bitmap) iconCompat.f3735b);
                    break;
                case 6:
                    createWithBitmap = d.a(iconCompat.i());
                    break;
            }
            ColorStateList colorStateList = iconCompat.f3740g;
            if (colorStateList != null) {
                createWithBitmap.setTintList(colorStateList);
            }
            PorterDuff.Mode mode = iconCompat.f3741h;
            if (mode != IconCompat.f3733k) {
                createWithBitmap.setTintMode(mode);
            }
            return createWithBitmap;
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public static class b {
        static Drawable a(Drawable drawable, Drawable drawable2) {
            return new AdaptiveIconDrawable(drawable, drawable2);
        }

        static Icon b(Bitmap bitmap) {
            return Icon.createWithAdaptiveBitmap(bitmap);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public static class c {
        static int a(Object obj) {
            return ((Icon) obj).getResId();
        }

        static String b(Object obj) {
            return ((Icon) obj).getResPackage();
        }

        static int c(Object obj) {
            return ((Icon) obj).getType();
        }

        static Uri d(Object obj) {
            return ((Icon) obj).getUri();
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public static class d {
        static Icon a(Uri uri) {
            return Icon.createWithAdaptiveBitmapContentUri(uri);
        }
    }

    public IconCompat() {
        this.f3734a = -1;
        this.f3736c = null;
        this.f3737d = null;
        this.f3738e = 0;
        this.f3739f = 0;
        this.f3740g = null;
        this.f3741h = f3733k;
        this.f3742i = null;
    }

    static Bitmap a(Bitmap bitmap, boolean z10) {
        int min = (int) (Math.min(bitmap.getWidth(), bitmap.getHeight()) * 0.6666667f);
        Bitmap createBitmap = Bitmap.createBitmap(min, min, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(createBitmap);
        Paint paint = new Paint(3);
        float f10 = min;
        float f11 = 0.5f * f10;
        float f12 = 0.9166667f * f11;
        if (z10) {
            float f13 = 0.010416667f * f10;
            paint.setColor(0);
            paint.setShadowLayer(f13, BitmapDescriptorFactory.HUE_RED, f10 * 0.020833334f, 1023410176);
            canvas.drawCircle(f11, f11, f12, paint);
            paint.setShadowLayer(f13, BitmapDescriptorFactory.HUE_RED, BitmapDescriptorFactory.HUE_RED, 503316480);
            canvas.drawCircle(f11, f11, f12, paint);
            paint.clearShadowLayer();
        }
        paint.setColor(-16777216);
        Shader.TileMode tileMode = Shader.TileMode.CLAMP;
        BitmapShader bitmapShader = new BitmapShader(bitmap, tileMode, tileMode);
        Matrix matrix = new Matrix();
        matrix.setTranslate((-(bitmap.getWidth() - min)) / 2.0f, (-(bitmap.getHeight() - min)) / 2.0f);
        bitmapShader.setLocalMatrix(matrix);
        paint.setShader(bitmapShader);
        canvas.drawCircle(f11, f11, f12, paint);
        canvas.setBitmap(null);
        return createBitmap;
    }

    public static IconCompat b(Bitmap bitmap) {
        s3.d.c(bitmap);
        IconCompat iconCompat = new IconCompat(1);
        iconCompat.f3735b = bitmap;
        return iconCompat;
    }

    public static IconCompat c(Context context, int i10) {
        s3.d.c(context);
        return d(context.getResources(), context.getPackageName(), i10);
    }

    public static IconCompat d(Resources resources, String str, int i10) {
        s3.d.c(str);
        if (i10 == 0) {
            throw new IllegalArgumentException("Drawable resource ID must not be 0");
        }
        IconCompat iconCompat = new IconCompat(2);
        iconCompat.f3738e = i10;
        if (resources != null) {
            try {
                iconCompat.f3735b = resources.getResourceName(i10);
            } catch (Resources.NotFoundException unused) {
                throw new IllegalArgumentException("Icon resource cannot be found");
            }
        } else {
            iconCompat.f3735b = str;
        }
        iconCompat.f3743j = str;
        return iconCompat;
    }

    private static String n(int i10) {
        switch (i10) {
            case 1:
                return "BITMAP";
            case 2:
                return "RESOURCE";
            case 3:
                return "DATA";
            case 4:
                return "URI";
            case 5:
                return "BITMAP_MASKABLE";
            case 6:
                return "URI_MASKABLE";
            default:
                return "UNKNOWN";
        }
    }

    public Bitmap e() {
        int i10 = this.f3734a;
        if (i10 == -1) {
            Object obj = this.f3735b;
            if (obj instanceof Bitmap) {
                return (Bitmap) obj;
            }
            return null;
        }
        if (i10 == 1) {
            return (Bitmap) this.f3735b;
        }
        if (i10 == 5) {
            return a((Bitmap) this.f3735b, true);
        }
        throw new IllegalStateException("called getBitmap() on " + this);
    }

    public int f() {
        int i10 = this.f3734a;
        if (i10 == -1) {
            return a.a(this.f3735b);
        }
        if (i10 == 2) {
            return this.f3738e;
        }
        throw new IllegalStateException("called getResId() on " + this);
    }

    public String g() {
        int i10 = this.f3734a;
        if (i10 == -1) {
            return a.b(this.f3735b);
        }
        if (i10 == 2) {
            String str = this.f3743j;
            return (str == null || TextUtils.isEmpty(str)) ? ((String) this.f3735b).split(":", -1)[0] : this.f3743j;
        }
        throw new IllegalStateException("called getResPackage() on " + this);
    }

    public int h() {
        int i10 = this.f3734a;
        return i10 == -1 ? a.c(this.f3735b) : i10;
    }

    public Uri i() {
        int i10 = this.f3734a;
        if (i10 == -1) {
            return a.d(this.f3735b);
        }
        if (i10 == 4 || i10 == 6) {
            return Uri.parse((String) this.f3735b);
        }
        throw new IllegalStateException("called getUri() on " + this);
    }

    public void j() {
        this.f3741h = PorterDuff.Mode.valueOf(this.f3742i);
        switch (this.f3734a) {
            case -1:
                Parcelable parcelable = this.f3737d;
                if (parcelable == null) {
                    throw new IllegalArgumentException("Invalid icon");
                }
                this.f3735b = parcelable;
                return;
            case 0:
            default:
                return;
            case 1:
            case 5:
                Parcelable parcelable2 = this.f3737d;
                if (parcelable2 != null) {
                    this.f3735b = parcelable2;
                    return;
                }
                byte[] bArr = this.f3736c;
                this.f3735b = bArr;
                this.f3734a = 3;
                this.f3738e = 0;
                this.f3739f = bArr.length;
                return;
            case 2:
            case 4:
            case 6:
                String str = new String(this.f3736c, Charset.forName("UTF-16"));
                this.f3735b = str;
                if (this.f3734a == 2 && this.f3743j == null) {
                    this.f3743j = str.split(":", -1)[0];
                    return;
                }
                return;
            case 3:
                this.f3735b = this.f3736c;
                return;
        }
    }

    public void k(boolean z10) {
        this.f3742i = this.f3741h.name();
        switch (this.f3734a) {
            case -1:
                if (z10) {
                    throw new IllegalArgumentException("Can't serialize Icon created with IconCompat#createFromIcon");
                }
                this.f3737d = (Parcelable) this.f3735b;
                return;
            case 0:
            default:
                return;
            case 1:
            case 5:
                if (!z10) {
                    this.f3737d = (Parcelable) this.f3735b;
                    return;
                }
                Bitmap bitmap = (Bitmap) this.f3735b;
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 90, byteArrayOutputStream);
                this.f3736c = byteArrayOutputStream.toByteArray();
                return;
            case 2:
                this.f3736c = ((String) this.f3735b).getBytes(Charset.forName("UTF-16"));
                return;
            case 3:
                this.f3736c = (byte[]) this.f3735b;
                return;
            case 4:
            case 6:
                this.f3736c = this.f3735b.toString().getBytes(Charset.forName("UTF-16"));
                return;
        }
    }

    public Icon l() {
        return m(null);
    }

    public Icon m(Context context) {
        return a.f(this, context);
    }

    public String toString() {
        if (this.f3734a == -1) {
            return String.valueOf(this.f3735b);
        }
        StringBuilder sb2 = new StringBuilder("Icon(typ=");
        sb2.append(n(this.f3734a));
        switch (this.f3734a) {
            case 1:
            case 5:
                sb2.append(" size=");
                sb2.append(((Bitmap) this.f3735b).getWidth());
                sb2.append("x");
                sb2.append(((Bitmap) this.f3735b).getHeight());
                break;
            case 2:
                sb2.append(" pkg=");
                sb2.append(this.f3743j);
                sb2.append(" id=");
                sb2.append(String.format("0x%08x", Integer.valueOf(f())));
                break;
            case 3:
                sb2.append(" len=");
                sb2.append(this.f3738e);
                if (this.f3739f != 0) {
                    sb2.append(" off=");
                    sb2.append(this.f3739f);
                    break;
                }
                break;
            case 4:
            case 6:
                sb2.append(" uri=");
                sb2.append(this.f3735b);
                break;
        }
        if (this.f3740g != null) {
            sb2.append(" tint=");
            sb2.append(this.f3740g);
        }
        if (this.f3741h != f3733k) {
            sb2.append(" mode=");
            sb2.append(this.f3741h);
        }
        sb2.append(")");
        return sb2.toString();
    }

    IconCompat(int i10) {
        this.f3736c = null;
        this.f3737d = null;
        this.f3738e = 0;
        this.f3739f = 0;
        this.f3740g = null;
        this.f3741h = f3733k;
        this.f3742i = null;
        this.f3734a = i10;
    }
}
