package androidx.core.graphics.drawable;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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.Drawable;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.text.TextUtils;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;
import androidx.core.app.a0;
import androidx.core.app.c0;
import androidx.versionedparcelable.CustomVersionedParcelable;
import g0.d;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import k0.b;
import p0.c;
import p0.g;

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

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

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public Object f1703b;

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public PorterDuff.Mode f1706e;

    @Nullable
    @RestrictTo({RestrictTo.Scope.f555a})
    public byte[] mData;

    @Nullable
    @RestrictTo({RestrictTo.Scope.f555a})
    public Parcelable mParcelable;

    @Nullable
    @RestrictTo({RestrictTo.Scope.f555a})
    public String mString1;

    @Nullable
    @RestrictTo({RestrictTo.Scope.f555a})
    public ColorStateList mTintList;

    @Nullable
    @RestrictTo({RestrictTo.Scope.f555a})
    public String mTintModeStr;

    @Retention(RetentionPolicy.SOURCE)
    @RestrictTo({RestrictTo.Scope.f555a})
    public @interface IconType {
    }

    @RestrictTo({RestrictTo.Scope.f555a})
    public IconCompat() {
        this.f1702a = -1;
        this.mData = null;
        this.mParcelable = null;
        this.f1704c = 0;
        this.f1705d = 0;
        this.mTintList = null;
        this.f1706e = f1701f;
        this.mTintModeStr = null;
    }

    public static Bitmap b(Bitmap bitmap, boolean z10) {
        int iMin = (int) (Math.min(bitmap.getWidth(), bitmap.getHeight()) * 0.6666667f);
        Bitmap bitmapCreateBitmap = Bitmap.createBitmap(iMin, iMin, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmapCreateBitmap);
        Paint paint = new Paint(3);
        float f10 = iMin;
        float f11 = 0.5f * f10;
        float f12 = 0.9166667f * f11;
        if (z10) {
            float f13 = 0.010416667f * f10;
            paint.setColor(0);
            paint.setShadowLayer(f13, 0.0f, f10 * 0.020833334f, 1023410176);
            canvas.drawCircle(f11, f11, f12, paint);
            paint.setShadowLayer(f13, 0.0f, 0.0f, 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() - iMin)) / 2.0f, (-(bitmap.getHeight() - iMin)) / 2.0f);
        bitmapShader.setLocalMatrix(matrix);
        paint.setShader(bitmapShader);
        canvas.drawCircle(f11, f11, f12, paint);
        canvas.setBitmap(null);
        return bitmapCreateBitmap;
    }

    public static IconCompat c(Bitmap bitmap) {
        c.requireNonNull(bitmap);
        IconCompat iconCompat = new IconCompat(1);
        iconCompat.f1703b = bitmap;
        return iconCompat;
    }

    @Nullable
    public static IconCompat createFromBundle(@NonNull Bundle bundle) {
        int i10 = bundle.getInt("type");
        IconCompat iconCompat = new IconCompat(i10);
        iconCompat.f1704c = bundle.getInt("int1");
        iconCompat.f1705d = bundle.getInt("int2");
        iconCompat.mString1 = bundle.getString("string1");
        if (bundle.containsKey("tint_list")) {
            iconCompat.mTintList = (ColorStateList) bundle.getParcelable("tint_list");
        }
        if (bundle.containsKey("tint_mode")) {
            iconCompat.f1706e = PorterDuff.Mode.valueOf(bundle.getString("tint_mode"));
        }
        switch (i10) {
            case -1:
            case 1:
            case 5:
                iconCompat.f1703b = bundle.getParcelable("obj");
                return iconCompat;
            case 0:
            default:
                return null;
            case 2:
            case 4:
            case 6:
                iconCompat.f1703b = bundle.getString("obj");
                return iconCompat;
            case 3:
                iconCompat.f1703b = bundle.getByteArray("obj");
                return iconCompat;
        }
    }

    @Nullable
    @RequiresApi(23)
    public static IconCompat createFromIcon(@NonNull Context context, @NonNull Icon icon) {
        g.checkNotNull(icon);
        return b.createFromIcon(context, icon);
    }

    @Nullable
    @RequiresApi(23)
    @RestrictTo({RestrictTo.Scope.f557c})
    public static IconCompat createFromIconOrNullIfZeroResId(@NonNull Icon icon) {
        if (b.c(icon) == 2 && b.b(icon) == 0) {
            return null;
        }
        return b.a(icon);
    }

    @NonNull
    @RestrictTo({RestrictTo.Scope.f557c})
    public static IconCompat createWithResource(@Nullable Resources resources, @NonNull String str, @DrawableRes int i10) {
        c.requireNonNull(str);
        if (i10 == 0) {
            throw new IllegalArgumentException("Drawable resource ID must not be 0");
        }
        IconCompat iconCompat = new IconCompat(2);
        iconCompat.f1704c = i10;
        if (resources != null) {
            try {
                iconCompat.f1703b = resources.getResourceName(i10);
            } catch (Resources.NotFoundException unused) {
                throw new IllegalArgumentException("Icon resource cannot be found");
            }
        } else {
            iconCompat.f1703b = str;
        }
        iconCompat.mString1 = str;
        return iconCompat;
    }

    public static Resources f(Context context, String str) {
        if ("android".equals(str)) {
            return Resources.getSystem();
        }
        PackageManager packageManager = context.getPackageManager();
        try {
            ApplicationInfo applicationInfo = packageManager.getApplicationInfo(str, 8192);
            if (applicationInfo != null) {
                return packageManager.getResourcesForApplication(applicationInfo);
            }
            return null;
        } catch (PackageManager.NameNotFoundException unused) {
            String.format("Unable to find pkg=%s for icon", str);
            return null;
        }
    }

    public final void a(Context context) {
        Object obj;
        int identifier;
        if (this.f1702a != 2 || (obj = this.f1703b) == null) {
            return;
        }
        String str = (String) obj;
        if (str.contains(":")) {
            String str2 = str.split(":", -1)[1];
            String str3 = str2.split("/", -1)[0];
            String str4 = str2.split("/", -1)[1];
            String str5 = str.split(":", -1)[0];
            if ("0_resource_name_obfuscated".equals(str4) || this.f1704c == (identifier = f(context, e()).getIdentifier(str4, str3, str5))) {
                return;
            }
            this.f1704c = identifier;
        }
    }

    @RestrictTo({RestrictTo.Scope.f557c})
    public void addToShortcutIntent(@NonNull Intent intent, @Nullable Drawable drawable, @NonNull Context context) {
        Bitmap bitmapCopy;
        a(context);
        int i10 = this.f1702a;
        if (i10 == 1) {
            bitmapCopy = (Bitmap) this.f1703b;
            if (drawable != null) {
                bitmapCopy = bitmapCopy.copy(bitmapCopy.getConfig(), true);
            }
        } else if (i10 == 2) {
            try {
                Context contextCreatePackageContext = context.createPackageContext(e(), 0);
                if (drawable == null) {
                    intent.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", Intent.ShortcutIconResource.fromContext(contextCreatePackageContext, this.f1704c));
                    return;
                }
                Drawable drawable2 = d.getDrawable(contextCreatePackageContext, this.f1704c);
                if (drawable2.getIntrinsicWidth() <= 0 || drawable2.getIntrinsicHeight() <= 0) {
                    int launcherLargeIconSize = ((ActivityManager) contextCreatePackageContext.getSystemService("activity")).getLauncherLargeIconSize();
                    bitmapCopy = Bitmap.createBitmap(launcherLargeIconSize, launcherLargeIconSize, Bitmap.Config.ARGB_8888);
                } else {
                    bitmapCopy = Bitmap.createBitmap(drawable2.getIntrinsicWidth(), drawable2.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
                }
                drawable2.setBounds(0, 0, bitmapCopy.getWidth(), bitmapCopy.getHeight());
                drawable2.draw(new Canvas(bitmapCopy));
            } catch (PackageManager.NameNotFoundException e10) {
                throw new IllegalArgumentException("Can't find package " + this.f1703b, e10);
            }
        } else {
            if (i10 != 5) {
                throw new IllegalArgumentException("Icon type not supported for intent shortcuts");
            }
            bitmapCopy = b((Bitmap) this.f1703b, true);
        }
        if (drawable != null) {
            int width = bitmapCopy.getWidth();
            int height = bitmapCopy.getHeight();
            drawable.setBounds(width / 2, height / 2, width, height);
            drawable.draw(new Canvas(bitmapCopy));
        }
        intent.putExtra("android.intent.extra.shortcut.ICON", bitmapCopy);
    }

    public final int d() {
        int i10 = this.f1702a;
        if (i10 == -1) {
            return b.b(this.f1703b);
        }
        if (i10 == 2) {
            return this.f1704c;
        }
        throw new IllegalStateException("called getResId() on " + this);
    }

    public final String e() {
        int i10 = this.f1702a;
        if (i10 == -1) {
            return b.getResPackage(this.f1703b);
        }
        if (i10 == 2) {
            String str = this.mString1;
            return (str == null || TextUtils.isEmpty(str)) ? ((String) this.f1703b).split(":", -1)[0] : this.mString1;
        }
        throw new IllegalStateException("called getResPackage() on " + this);
    }

    public final Uri g() {
        int i10 = this.f1702a;
        if (i10 == -1) {
            return b.getUri(this.f1703b);
        }
        if (i10 == 4 || i10 == 6) {
            return Uri.parse((String) this.f1703b);
        }
        throw new IllegalStateException("called getUri() on " + this);
    }

    @Nullable
    @RestrictTo({RestrictTo.Scope.f557c})
    public Bitmap getBitmap() {
        int i10 = this.f1702a;
        if (i10 == -1) {
            Object obj = this.f1703b;
            if (obj instanceof Bitmap) {
                return (Bitmap) obj;
            }
            return null;
        }
        if (i10 == 1) {
            return (Bitmap) this.f1703b;
        }
        if (i10 == 5) {
            return b((Bitmap) this.f1703b, true);
        }
        throw new IllegalStateException("called getBitmap() on " + this);
    }

    @Nullable
    @RestrictTo({RestrictTo.Scope.f556b})
    public InputStream getUriInputStream(@NonNull Context context) {
        Uri uriG = g();
        String scheme = uriG.getScheme();
        if ("content".equals(scheme) || "file".equals(scheme)) {
            try {
                return context.getContentResolver().openInputStream(uriG);
            } catch (Exception unused) {
                uriG.toString();
                return null;
            }
        }
        try {
            return new FileInputStream(new File((String) this.f1703b));
        } catch (FileNotFoundException unused2) {
            uriG.toString();
            return null;
        }
    }

    @Nullable
    public Drawable loadDrawable(@NonNull Context context) {
        a(context);
        return toIcon(context).loadDrawable(context);
    }

    @NonNull
    public IconCompat setTintList(@Nullable ColorStateList colorStateList) {
        this.mTintList = colorStateList;
        return this;
    }

    @NonNull
    public IconCompat setTintMode(@Nullable PorterDuff.Mode mode) {
        this.f1706e = mode;
        return this;
    }

    @NonNull
    @RequiresApi(23)
    public Icon toIcon(@Nullable Context context) {
        Icon iconCreateWithBitmap;
        int i10 = Build.VERSION.SDK_INT;
        switch (this.f1702a) {
            case -1:
                return (Icon) this.f1703b;
            case 0:
            default:
                throw new IllegalArgumentException("Unknown type");
            case 1:
                iconCreateWithBitmap = Icon.createWithBitmap((Bitmap) this.f1703b);
                break;
            case 2:
                iconCreateWithBitmap = Icon.createWithResource(e(), this.f1704c);
                break;
            case 3:
                iconCreateWithBitmap = Icon.createWithData((byte[]) this.f1703b, this.f1704c, this.f1705d);
                break;
            case 4:
                iconCreateWithBitmap = Icon.createWithContentUri((String) this.f1703b);
                break;
            case 5:
                iconCreateWithBitmap = i10 < 26 ? Icon.createWithBitmap(b((Bitmap) this.f1703b, false)) : a0.d((Bitmap) this.f1703b);
                break;
            case 6:
                if (i10 >= 30) {
                    iconCreateWithBitmap = c0.b(g());
                } else {
                    if (context == null) {
                        throw new IllegalArgumentException("Context is required to resolve the file uri of the icon: " + g());
                    }
                    InputStream uriInputStream = getUriInputStream(context);
                    if (uriInputStream == null) {
                        throw new IllegalStateException("Cannot load adaptive icon from uri: " + g());
                    }
                    if (i10 < 26) {
                        iconCreateWithBitmap = Icon.createWithBitmap(b(BitmapFactory.decodeStream(uriInputStream), false));
                    } else {
                        iconCreateWithBitmap = a0.d(BitmapFactory.decodeStream(uriInputStream));
                    }
                }
                break;
        }
        ColorStateList colorStateList = this.mTintList;
        if (colorStateList != null) {
            iconCreateWithBitmap.setTintList(colorStateList);
        }
        PorterDuff.Mode mode = this.f1706e;
        if (mode == f1701f) {
            return iconCreateWithBitmap;
        }
        iconCreateWithBitmap.setTintMode(mode);
        return iconCreateWithBitmap;
    }

    public final String toString() {
        String str;
        if (this.f1702a == -1) {
            return String.valueOf(this.f1703b);
        }
        StringBuilder sb = new StringBuilder("Icon(typ=");
        switch (this.f1702a) {
            case 1:
                str = "BITMAP";
                break;
            case 2:
                str = "RESOURCE";
                break;
            case 3:
                str = "DATA";
                break;
            case 4:
                str = "URI";
                break;
            case 5:
                str = "BITMAP_MASKABLE";
                break;
            case 6:
                str = "URI_MASKABLE";
                break;
            default:
                str = "UNKNOWN";
                break;
        }
        sb.append(str);
        switch (this.f1702a) {
            case 1:
            case 5:
                sb.append(" size=");
                sb.append(((Bitmap) this.f1703b).getWidth());
                sb.append("x");
                sb.append(((Bitmap) this.f1703b).getHeight());
                break;
            case 2:
                sb.append(" pkg=");
                sb.append(this.mString1);
                sb.append(" id=");
                sb.append(String.format("0x%08x", Integer.valueOf(d())));
                break;
            case 3:
                sb.append(" len=");
                sb.append(this.f1704c);
                if (this.f1705d != 0) {
                    sb.append(" off=");
                    sb.append(this.f1705d);
                }
                break;
            case 4:
            case 6:
                sb.append(" uri=");
                sb.append(this.f1703b);
                break;
        }
        if (this.mTintList != null) {
            sb.append(" tint=");
            sb.append(this.mTintList);
        }
        if (this.f1706e != f1701f) {
            sb.append(" mode=");
            sb.append(this.f1706e);
        }
        sb.append(")");
        return sb.toString();
    }

    public IconCompat(int i10) {
        this.mData = null;
        this.mParcelable = null;
        this.f1704c = 0;
        this.f1705d = 0;
        this.mTintList = null;
        this.f1706e = f1701f;
        this.mTintModeStr = null;
        this.f1702a = i10;
    }

    @Nullable
    @RequiresApi(23)
    @RestrictTo({RestrictTo.Scope.f557c})
    public static IconCompat createFromIcon(@NonNull Icon icon) {
        return b.a(icon);
    }
}
