package com.bumptech.glide;

import android.app.Activity;
import android.content.ComponentCallbacks2;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetFileDescriptor;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import android.view.View;
import androidx.annotation.GuardedBy;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.internal.atv_ads_framework.d1;
import com.google.android.gms.measurement.internal.a0;
import g5.k0;
import g5.n0;
import g5.p0;
import j5.e0;
import j5.z;
import java.io.File;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/* JADX INFO: loaded from: classes.dex */
public final class c implements ComponentCallbacks2 {

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public static volatile c f5550i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public static volatile boolean f5551j;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final c5.e f5552a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final d5.h f5553b;

    @Nullable
    @GuardedBy("this")
    private f5.a bitmapPreFiller;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final k f5554c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final p f5555d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final c5.b f5556e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final n5.l f5557f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final n5.d f5558g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final ArrayList f5559h = new ArrayList();

    public c(Context context, com.bumptech.glide.load.engine.q qVar, d5.h hVar, c5.e eVar, c5.b bVar, n5.l lVar, n5.d dVar, int i10, b bVar2, androidx.collection.f fVar, List list, m mVar) {
        a5.m cVar;
        a5.m aVar;
        int i11;
        this.f5552a = eVar;
        this.f5556e = bVar;
        this.f5553b = hVar;
        this.f5557f = lVar;
        this.f5558g = dVar;
        Resources resources = context.getResources();
        p pVar = new p();
        this.f5555d = pVar;
        j5.j jVar = new j5.j();
        xyz.stream.api.r rVar = pVar.f5836g;
        synchronized (rVar) {
            rVar.f35933a.add(jVar);
        }
        int i12 = Build.VERSION.SDK_INT;
        if (i12 >= 27) {
            j5.r rVar2 = new j5.r();
            xyz.stream.api.r rVar3 = pVar.f5836g;
            synchronized (rVar3) {
                rVar3.f35933a.add(rVar2);
            }
        }
        ArrayList arrayListE = pVar.e();
        l5.a aVar2 = new l5.a(context, arrayListE, eVar, bVar);
        com.bumptech.glide.load.resource.bitmap.b bVar3 = new com.bumptech.glide.load.resource.bitmap.b(eVar, new a0(15));
        j5.n nVar = new j5.n(pVar.e(), resources.getDisplayMetrics(), eVar, bVar);
        if (i12 < 28 || !mVar.f5826a.containsKey(g.class)) {
            cVar = new j5.c(nVar, 2);
            aVar = new j5.a(nVar, bVar);
        } else {
            aVar = new j5.f(1);
            cVar = new j5.f(0);
        }
        if (i12 < 28 || !mVar.f5826a.containsKey(f.class)) {
            i11 = i12;
        } else {
            i11 = i12;
            pVar.a(new k5.a(1, new d1(arrayListE, bVar, 8)), InputStream.class, Drawable.class, "Animation");
            pVar.a(new k5.a(0, new d1(arrayListE, bVar, 8)), ByteBuffer.class, Drawable.class, "Animation");
        }
        k5.e eVar2 = new k5.e(context);
        k0 k0Var = new k0(resources, 2);
        k0 k0Var2 = new k0(resources, 3);
        k0 k0Var3 = new k0(resources, 1);
        k0 k0Var4 = new k0(resources, 0);
        j5.b bVar4 = new j5.b(bVar);
        m5.a aVar3 = new m5.a();
        m5.d dVar2 = new m5.d();
        ContentResolver contentResolver = context.getContentResolver();
        pVar.b(ByteBuffer.class, new a0(11));
        pVar.b(InputStream.class, new xyz.stream.utils.a0(bVar));
        pVar.a(cVar, ByteBuffer.class, Bitmap.class, "Bitmap");
        pVar.a(aVar, InputStream.class, Bitmap.class, "Bitmap");
        pVar.a(new z(nVar), ParcelFileDescriptor.class, Bitmap.class, "Bitmap");
        pVar.a(bVar3, ParcelFileDescriptor.class, Bitmap.class, "Bitmap");
        pVar.a(new com.bumptech.glide.load.resource.bitmap.b(eVar, new androidx.window.core.h()), AssetFileDescriptor.class, Bitmap.class, "Bitmap");
        n0 n0Var = n0.f26210a;
        pVar.d(Bitmap.class, Bitmap.class, n0Var);
        pVar.a(new e0(0), Bitmap.class, Bitmap.class, "Bitmap");
        pVar.c(Bitmap.class, bVar4);
        pVar.a(new j5.a(resources, cVar), ByteBuffer.class, BitmapDrawable.class, "BitmapDrawable");
        pVar.a(new j5.a(resources, aVar), InputStream.class, BitmapDrawable.class, "BitmapDrawable");
        pVar.a(new j5.a(resources, bVar3), ParcelFileDescriptor.class, BitmapDrawable.class, "BitmapDrawable");
        pVar.c(BitmapDrawable.class, new xyz.stream.utils.f(eVar, bVar4, 8, 0));
        pVar.a(new l5.m(arrayListE, aVar2, bVar), InputStream.class, l5.d.class, "Animation");
        pVar.a(aVar2, ByteBuffer.class, l5.d.class, "Animation");
        pVar.c(l5.d.class, new a0(16));
        pVar.d(y4.a.class, y4.a.class, n0Var);
        pVar.a(new j5.c(eVar, 1), y4.a.class, Bitmap.class, "Bitmap");
        pVar.a(eVar2, Uri.class, Drawable.class, "legacy_append");
        pVar.a(new j5.a0(eVar2, eVar), Uri.class, Bitmap.class, "legacy_append");
        pVar.h(new com.bumptech.glide.load.data.h(2));
        pVar.d(File.class, ByteBuffer.class, new g5.d(2));
        pVar.d(File.class, InputStream.class, new g5.m(1));
        pVar.a(new e0(1), File.class, File.class, "legacy_append");
        pVar.d(File.class, ParcelFileDescriptor.class, new g5.m(0));
        pVar.d(File.class, File.class, n0Var);
        pVar.h(new com.bumptech.glide.load.data.m(bVar));
        pVar.h(new com.bumptech.glide.load.data.h(1));
        Class cls = Integer.TYPE;
        pVar.d(cls, InputStream.class, k0Var);
        pVar.d(cls, ParcelFileDescriptor.class, k0Var3);
        pVar.d(Integer.class, InputStream.class, k0Var);
        pVar.d(Integer.class, ParcelFileDescriptor.class, k0Var3);
        pVar.d(Integer.class, Uri.class, k0Var2);
        pVar.d(cls, AssetFileDescriptor.class, k0Var4);
        pVar.d(Integer.class, AssetFileDescriptor.class, k0Var4);
        pVar.d(cls, Uri.class, k0Var2);
        pVar.d(String.class, InputStream.class, new g5.k(0));
        pVar.d(Uri.class, InputStream.class, new g5.k(0));
        pVar.d(String.class, InputStream.class, new g5.d(5));
        pVar.d(String.class, ParcelFileDescriptor.class, new g5.d(4));
        pVar.d(String.class, AssetFileDescriptor.class, new g5.d(3));
        int i13 = 1;
        pVar.d(Uri.class, InputStream.class, new g5.b(context.getAssets(), i13));
        pVar.d(Uri.class, AssetFileDescriptor.class, new g5.b(context.getAssets(), 0));
        pVar.d(Uri.class, InputStream.class, new g5.u(context, i13));
        pVar.d(Uri.class, InputStream.class, new g5.u(context, 2));
        if (i11 >= 29) {
            pVar.d(Uri.class, InputStream.class, new h5.d(context, 1));
            pVar.d(Uri.class, ParcelFileDescriptor.class, new h5.d(context, 0));
        }
        pVar.d(Uri.class, InputStream.class, new p0(contentResolver, 2));
        pVar.d(Uri.class, ParcelFileDescriptor.class, new p0(contentResolver, 1));
        pVar.d(Uri.class, AssetFileDescriptor.class, new p0(contentResolver, 0));
        pVar.d(Uri.class, InputStream.class, new g5.d(6));
        pVar.d(URL.class, InputStream.class, new g5.d(7));
        int i14 = 0;
        pVar.d(Uri.class, File.class, new g5.u(context, i14));
        pVar.d(g5.o.class, InputStream.class, new g5.k(1));
        pVar.d(byte[].class, ByteBuffer.class, new g5.d(i14));
        pVar.d(byte[].class, InputStream.class, new g5.d(1));
        pVar.d(Uri.class, Uri.class, n0Var);
        pVar.d(Drawable.class, Drawable.class, n0Var);
        pVar.a(new k5.f(), Drawable.class, Drawable.class, "legacy_append");
        pVar.i(Bitmap.class, BitmapDrawable.class, new m5.b(resources));
        pVar.i(Bitmap.class, byte[].class, aVar3);
        pVar.i(Drawable.class, byte[].class, new m5.c(eVar, aVar3, dVar2));
        pVar.i(l5.d.class, byte[].class, dVar2);
        com.bumptech.glide.load.resource.bitmap.b bVar5 = new com.bumptech.glide.load.resource.bitmap.b(eVar, new a0(14));
        pVar.a(bVar5, ByteBuffer.class, Bitmap.class, "legacy_append");
        pVar.a(new j5.a(resources, bVar5), ByteBuffer.class, BitmapDrawable.class, "legacy_append");
        this.f5554c = new k(context, bVar, pVar, new androidx.window.core.h(19), bVar2, fVar, list, qVar, mVar, i10);
    }

    public static c a(Context context) {
        if (f5550i == null) {
            GeneratedAppGlideModule annotationGeneratedGlideModules = getAnnotationGeneratedGlideModules(context.getApplicationContext());
            synchronized (c.class) {
                try {
                    if (f5550i == null) {
                        checkAndInitializeGlide(context, annotationGeneratedGlideModules);
                    }
                } finally {
                }
            }
        }
        return f5550i;
    }

    public static u c(Activity activity) {
        return getRetriever(activity).b(activity);
    }

    @GuardedBy("Glide.class")
    private static void checkAndInitializeGlide(@NonNull Context context, @Nullable GeneratedAppGlideModule generatedAppGlideModule) {
        if (f5551j) {
            throw new IllegalStateException("You cannot call Glide.get() in registerComponents(), use the provided Glide instance instead");
        }
        f5551j = true;
        initializeGlide(context, generatedAppGlideModule);
        f5551j = false;
    }

    public static u d(Context context) {
        return getRetriever(context).c(context);
    }

    public static u e(View view) {
        return getRetriever(view.getContext()).d(view);
    }

    public static u f(AppCompatActivity appCompatActivity) {
        return getRetriever(appCompatActivity).f(appCompatActivity);
    }

    public static u g(androidx.fragment.app.e0 e0Var) {
        return getRetriever(e0Var.getContext()).e(e0Var);
    }

    @Nullable
    private static GeneratedAppGlideModule getAnnotationGeneratedGlideModules(Context context) {
        try {
            return (GeneratedAppGlideModule) Class.forName("com.bumptech.glide.GeneratedAppGlideModuleImpl").getDeclaredConstructor(Context.class).newInstance(context.getApplicationContext());
        } catch (ClassNotFoundException unused) {
            return null;
        } catch (IllegalAccessException e10) {
            throw new IllegalStateException("GeneratedAppGlideModuleImpl is implemented incorrectly. If you've manually implemented this class, remove your implementation. The Annotation processor will generate a correct implementation.", e10);
        } catch (InstantiationException e11) {
            throw new IllegalStateException("GeneratedAppGlideModuleImpl is implemented incorrectly. If you've manually implemented this class, remove your implementation. The Annotation processor will generate a correct implementation.", e11);
        } catch (NoSuchMethodException e12) {
            throw new IllegalStateException("GeneratedAppGlideModuleImpl is implemented incorrectly. If you've manually implemented this class, remove your implementation. The Annotation processor will generate a correct implementation.", e12);
        } catch (InvocationTargetException e13) {
            throw new IllegalStateException("GeneratedAppGlideModuleImpl is implemented incorrectly. If you've manually implemented this class, remove your implementation. The Annotation processor will generate a correct implementation.", e13);
        }
    }

    @Nullable
    public static File getPhotoCacheDir(@NonNull Context context) {
        return getPhotoCacheDir(context, "image_manager_disk_cache");
    }

    @NonNull
    private static n5.l getRetriever(@Nullable Context context) {
        t5.n.checkNotNull(context, "You cannot start a load on a not yet attached View or a Fragment where getActivity() returns null (which usually occurs when getActivity() is called before the Fragment is attached or after the Fragment is destroyed).");
        return a(context).f5557f;
    }

    @GuardedBy("Glide.class")
    private static void initializeGlide(@NonNull Context context, @Nullable GeneratedAppGlideModule generatedAppGlideModule) {
        initializeGlide(context, new j(), generatedAppGlideModule);
    }

    public final void b(u uVar) {
        synchronized (this.f5559h) {
            try {
                if (!this.f5559h.contains(uVar)) {
                    throw new IllegalStateException("Cannot unregister not yet registered manager");
                }
                this.f5559h.remove(uVar);
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @Override // android.content.ComponentCallbacks
    public final void onConfigurationChanged(Configuration configuration) {
    }

    @Override // android.content.ComponentCallbacks
    public final void onLowMemory() {
        t5.p.a();
        ((t5.k) this.f5553b).a(0L);
        this.f5552a.d();
        c5.k kVar = (c5.k) this.f5556e;
        synchronized (kVar) {
            kVar.b(0);
        }
    }

    @Override // android.content.ComponentCallbacks2
    public final void onTrimMemory(int i10) {
        long j10;
        t5.p.a();
        synchronized (this.f5559h) {
            try {
                Iterator it = this.f5559h.iterator();
                while (it.hasNext()) {
                    ((u) it.next()).getClass();
                }
            } finally {
            }
        }
        d5.f fVar = (d5.f) this.f5553b;
        fVar.getClass();
        if (i10 >= 40) {
            fVar.a(0L);
        } else if (i10 >= 20 || i10 == 15) {
            synchronized (fVar) {
                j10 = fVar.f32970b;
            }
            fVar.a(j10 / 2);
        }
        this.f5552a.c(i10);
        c5.k kVar = (c5.k) this.f5556e;
        synchronized (kVar) {
            if (i10 >= 40) {
                synchronized (kVar) {
                    kVar.b(0);
                }
            } else if (i10 >= 20 || i10 == 15) {
                kVar.b(kVar.f4889e / 2);
            }
        }
    }

    @Nullable
    public static File getPhotoCacheDir(@NonNull Context context, @NonNull String str) {
        File cacheDir = context.getCacheDir();
        if (cacheDir == null) {
            return null;
        }
        File file = new File(cacheDir, str);
        if (file.isDirectory() || file.mkdirs()) {
            return file;
        }
        return null;
    }

    @GuardedBy("Glide.class")
    private static void initializeGlide(@NonNull Context context, @NonNull j jVar, @Nullable GeneratedAppGlideModule generatedAppGlideModule) {
        Context applicationContext = context.getApplicationContext();
        Collections.emptyList();
        ArrayList<o5.a> arrayList = new ArrayList();
        try {
            ApplicationInfo applicationInfo = applicationContext.getPackageManager().getApplicationInfo(applicationContext.getPackageName(), 128);
            if (applicationInfo.metaData != null) {
                if (Log.isLoggable("ManifestParser", 2)) {
                    Objects.toString(applicationInfo.metaData);
                }
                for (String str : applicationInfo.metaData.keySet()) {
                    if ("GlideModule".equals(applicationInfo.metaData.get(str))) {
                        arrayList.add(nb.c.n(str));
                    }
                }
            }
            if (generatedAppGlideModule != null && !generatedAppGlideModule.E().isEmpty()) {
                Set setE = generatedAppGlideModule.E();
                Iterator it = arrayList.iterator();
                while (it.hasNext()) {
                    o5.a aVar = (o5.a) it.next();
                    if (setE.contains(aVar.getClass())) {
                        if (Log.isLoggable("Glide", 3)) {
                            aVar.toString();
                        }
                        it.remove();
                    }
                }
            }
            if (Log.isLoggable("Glide", 3)) {
                Iterator it2 = arrayList.iterator();
                while (it2.hasNext()) {
                    ((o5.a) it2.next()).getClass().toString();
                }
            }
            jVar.setRequestManagerFactory(generatedAppGlideModule != null ? generatedAppGlideModule.getRequestManagerFactory() : null);
            Iterator it3 = arrayList.iterator();
            while (it3.hasNext()) {
                ((o5.a) it3.next()).a(applicationContext, jVar);
            }
            c cVarA = jVar.a(applicationContext);
            for (o5.a aVar2 : arrayList) {
                try {
                    aVar2.b(applicationContext, cVarA.f5555d);
                } catch (AbstractMethodError e10) {
                    throw new IllegalStateException("Attempting to register a Glide v3 module. If you see this, you or one of your dependencies may be including Glide v3 even though you're using Glide v4. You'll need to find and remove (or update) the offending dependency. The v3 module name is: ".concat(aVar2.getClass().getName()), e10);
                }
            }
            applicationContext.registerComponentCallbacks(cVarA);
            f5550i = cVarA;
        } catch (PackageManager.NameNotFoundException e11) {
            throw new RuntimeException("Unable to find metadata to parse GlideModules", e11);
        }
    }
}
