package androidx.core.graphics.drawable;

import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.PorterDuff;
import android.graphics.drawable.Icon;
import android.os.Build;
import android.os.Parcelable;
import android.util.Log;
import androidx.versionedparcelable.CustomVersionedParcelable;
import java.io.ByteArrayOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.Charset;

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

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    static final PorterDuff.Mode f614j = PorterDuff.Mode.SRC_IN;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    Object f615b;
    public int a = -1;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public byte[] f616c = null;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public Parcelable f617d = null;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public int f618e = 0;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public int f619f = 0;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public ColorStateList f620g = null;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    PorterDuff.Mode f621h = f614j;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public String f622i = null;

    private static int b(Icon icon) {
        if (Build.VERSION.SDK_INT >= 28) {
            return icon.getResId();
        }
        try {
            return ((Integer) icon.getClass().getMethod("getResId", new Class[0]).invoke(icon, new Object[0])).intValue();
        } catch (IllegalAccessException e2) {
            Log.e("IconCompat", "Unable to get icon resource", e2);
            return 0;
        } catch (NoSuchMethodException e3) {
            Log.e("IconCompat", "Unable to get icon resource", e3);
            return 0;
        } catch (InvocationTargetException e4) {
            Log.e("IconCompat", "Unable to get icon resource", e4);
            return 0;
        }
    }

    private static String d(Icon icon) {
        if (Build.VERSION.SDK_INT >= 28) {
            return icon.getResPackage();
        }
        try {
            return (String) icon.getClass().getMethod("getResPackage", new Class[0]).invoke(icon, new Object[0]);
        } catch (IllegalAccessException e2) {
            Log.e("IconCompat", "Unable to get icon package", e2);
            return null;
        } catch (NoSuchMethodException e3) {
            Log.e("IconCompat", "Unable to get icon package", e3);
            return null;
        } catch (InvocationTargetException e4) {
            Log.e("IconCompat", "Unable to get icon package", e4);
            return null;
        }
    }

    private static String g(int i2) {
        switch (i2) {
            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 int a() {
        if (this.a == -1 && Build.VERSION.SDK_INT >= 23) {
            return b((Icon) this.f615b);
        }
        if (this.a == 2) {
            return this.f618e;
        }
        throw new IllegalStateException("called getResId() on " + this);
    }

    public String c() {
        if (this.a == -1 && Build.VERSION.SDK_INT >= 23) {
            return d((Icon) this.f615b);
        }
        if (this.a == 2) {
            return ((String) this.f615b).split(":", -1)[0];
        }
        throw new IllegalStateException("called getResPackage() on " + this);
    }

    public void e() {
        this.f621h = PorterDuff.Mode.valueOf(this.f622i);
        switch (this.a) {
            case -1:
                Parcelable parcelable = this.f617d;
                if (parcelable == null) {
                    throw new IllegalArgumentException("Invalid icon");
                }
                this.f615b = parcelable;
                return;
            case 0:
            default:
                return;
            case 1:
            case 5:
                Parcelable parcelable2 = this.f617d;
                if (parcelable2 != null) {
                    this.f615b = parcelable2;
                    return;
                }
                byte[] bArr = this.f616c;
                this.f615b = bArr;
                this.a = 3;
                this.f618e = 0;
                this.f619f = bArr.length;
                return;
            case 2:
            case 4:
            case 6:
                this.f615b = new String(this.f616c, Charset.forName("UTF-16"));
                return;
            case 3:
                this.f615b = this.f616c;
                return;
        }
    }

    public void f(boolean z) {
        this.f622i = this.f621h.name();
        switch (this.a) {
            case -1:
                if (z) {
                    throw new IllegalArgumentException("Can't serialize Icon created with IconCompat#createFromIcon");
                }
                this.f617d = (Parcelable) this.f615b;
                return;
            case 0:
            default:
                return;
            case 1:
            case 5:
                if (!z) {
                    this.f617d = (Parcelable) this.f615b;
                    return;
                }
                Bitmap bitmap = (Bitmap) this.f615b;
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 90, byteArrayOutputStream);
                this.f616c = byteArrayOutputStream.toByteArray();
                return;
            case 2:
                this.f616c = ((String) this.f615b).getBytes(Charset.forName("UTF-16"));
                return;
            case 3:
                this.f616c = (byte[]) this.f615b;
                return;
            case 4:
            case 6:
                this.f616c = this.f615b.toString().getBytes(Charset.forName("UTF-16"));
                return;
        }
    }

    public String toString() {
        if (this.a == -1) {
            return String.valueOf(this.f615b);
        }
        StringBuilder sb = new StringBuilder("Icon(typ=");
        sb.append(g(this.a));
        switch (this.a) {
            case 1:
            case 5:
                sb.append(" size=");
                sb.append(((Bitmap) this.f615b).getWidth());
                sb.append("x");
                sb.append(((Bitmap) this.f615b).getHeight());
                break;
            case 2:
                sb.append(" pkg=");
                sb.append(c());
                sb.append(" id=");
                sb.append(String.format("0x%08x", Integer.valueOf(a())));
                break;
            case 3:
                sb.append(" len=");
                sb.append(this.f618e);
                if (this.f619f != 0) {
                    sb.append(" off=");
                    sb.append(this.f619f);
                }
                break;
            case 4:
            case 6:
                sb.append(" uri=");
                sb.append(this.f615b);
                break;
        }
        if (this.f620g != null) {
            sb.append(" tint=");
            sb.append(this.f620g);
        }
        if (this.f621h != f614j) {
            sb.append(" mode=");
            sb.append(this.f621h);
        }
        sb.append(")");
        return sb.toString();
    }
}
