package com.bumptech.glide;

import android.content.ComponentCallbacks2;
import android.content.Context;
import android.content.res.Configuration;
import android.util.Log;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import yh.r;

/* loaded from: classes4.dex */
public class b implements ComponentCallbacks2 {
    private static volatile boolean A;

    /* renamed from: z, reason: collision with root package name */
    private static volatile b f12484z;

    /* renamed from: p, reason: collision with root package name */
    private final lh.k f12485p;

    /* renamed from: q, reason: collision with root package name */
    private final mh.d f12486q;

    /* renamed from: r, reason: collision with root package name */
    private final nh.h f12487r;

    /* renamed from: s, reason: collision with root package name */
    private final d f12488s;

    /* renamed from: t, reason: collision with root package name */
    private final mh.b f12489t;

    /* renamed from: u, reason: collision with root package name */
    private final r f12490u;

    /* renamed from: v, reason: collision with root package name */
    private final yh.d f12491v;

    /* renamed from: x, reason: collision with root package name */
    private final a f12493x;

    /* renamed from: w, reason: collision with root package name */
    private final List f12492w = new ArrayList();

    /* renamed from: y, reason: collision with root package name */
    private f f12494y = f.NORMAL;

    /* loaded from: classes4.dex */
    public interface a {
        com.bumptech.glide.request.f build();
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public b(Context context, lh.k kVar, nh.h hVar, mh.d dVar, mh.b bVar, r rVar, yh.d dVar2, int i10, a aVar, Map map, List list, List list2, zh.a aVar2, e eVar) {
        this.f12485p = kVar;
        this.f12486q = dVar;
        this.f12489t = bVar;
        this.f12487r = hVar;
        this.f12490u = rVar;
        this.f12491v = dVar2;
        this.f12493x = aVar;
        this.f12488s = new d(context, bVar, j.d(this, list2, aVar2), new bi.f(), aVar, map, list, kVar, eVar, i10);
    }

    static void a(Context context, GeneratedAppGlideModule generatedAppGlideModule) {
        if (A) {
            throw new IllegalStateException("Glide has been called recursively, this is probably an internal library error!");
        }
        A = true;
        try {
            m(context, generatedAppGlideModule);
        } finally {
            A = false;
        }
    }

    public static b c(Context context) {
        if (f12484z == null) {
            GeneratedAppGlideModule d10 = d(context.getApplicationContext());
            synchronized (b.class) {
                if (f12484z == null) {
                    a(context, d10);
                }
            }
        }
        return f12484z;
    }

    private static GeneratedAppGlideModule d(Context context) {
        try {
            return (GeneratedAppGlideModule) Class.forName("com.bumptech.glide.GeneratedAppGlideModuleImpl").getDeclaredConstructor(Context.class).newInstance(context.getApplicationContext());
        } catch (ClassNotFoundException unused) {
            if (Log.isLoggable("Glide", 5)) {
                Log.w("Glide", "Failed to find GeneratedAppGlideModule. You should include an annotationProcessor compile dependency on com.github.bumptech.glide:compiler in your application and a @GlideModule annotated AppGlideModule implementation or LibraryGlideModules will be silently ignored");
            }
            return null;
        } catch (IllegalAccessException e10) {
            q(e10);
            return null;
        } catch (InstantiationException e11) {
            q(e11);
            return null;
        } catch (NoSuchMethodException e12) {
            q(e12);
            return null;
        } catch (InvocationTargetException e13) {
            q(e13);
            return null;
        }
    }

    private static r l(Context context) {
        ei.k.e(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 c(context).k();
    }

    private static void m(Context context, GeneratedAppGlideModule generatedAppGlideModule) {
        n(context, new c(), generatedAppGlideModule);
    }

    private static void n(Context context, c cVar, GeneratedAppGlideModule generatedAppGlideModule) {
        Context applicationContext = context.getApplicationContext();
        List emptyList = Collections.emptyList();
        if (generatedAppGlideModule == null || generatedAppGlideModule.c()) {
            emptyList = new zh.d(applicationContext).b();
        }
        if (generatedAppGlideModule != null && !generatedAppGlideModule.d().isEmpty()) {
            Set d10 = generatedAppGlideModule.d();
            Iterator it = emptyList.iterator();
            while (it.hasNext()) {
                zh.b bVar = (zh.b) it.next();
                if (d10.contains(bVar.getClass())) {
                    if (Log.isLoggable("Glide", 3)) {
                        Log.d("Glide", "AppGlideModule excludes manifest GlideModule: " + bVar);
                    }
                    it.remove();
                }
            }
        }
        if (Log.isLoggable("Glide", 3)) {
            Iterator it2 = emptyList.iterator();
            while (it2.hasNext()) {
                Log.d("Glide", "Discovered GlideModule from manifest: " + ((zh.b) it2.next()).getClass());
            }
        }
        cVar.b(generatedAppGlideModule != null ? generatedAppGlideModule.e() : null);
        Iterator it3 = emptyList.iterator();
        while (it3.hasNext()) {
            ((zh.b) it3.next()).a(applicationContext, cVar);
        }
        if (generatedAppGlideModule != null) {
            generatedAppGlideModule.b(applicationContext, cVar);
        }
        b a10 = cVar.a(applicationContext, emptyList, generatedAppGlideModule);
        applicationContext.registerComponentCallbacks(a10);
        f12484z = a10;
    }

    private static void q(Exception exc) {
        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.", exc);
    }

    public static l t(Context context) {
        return l(context).f(context);
    }

    public void b() {
        ei.l.a();
        this.f12487r.b();
        this.f12486q.b();
        this.f12489t.b();
    }

    public mh.b e() {
        return this.f12489t;
    }

    public mh.d f() {
        return this.f12486q;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public yh.d g() {
        return this.f12491v;
    }

    public Context h() {
        return this.f12488s.getBaseContext();
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public d i() {
        return this.f12488s;
    }

    public i j() {
        return this.f12488s.i();
    }

    public r k() {
        return this.f12490u;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void o(l lVar) {
        synchronized (this.f12492w) {
            if (this.f12492w.contains(lVar)) {
                throw new IllegalStateException("Cannot register already registered manager");
            }
            this.f12492w.add(lVar);
        }
    }

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

    @Override // android.content.ComponentCallbacks
    public void onLowMemory() {
        b();
    }

    @Override // android.content.ComponentCallbacks2
    public void onTrimMemory(int i10) {
        r(i10);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public boolean p(bi.h hVar) {
        synchronized (this.f12492w) {
            Iterator it = this.f12492w.iterator();
            while (it.hasNext()) {
                if (((l) it.next()).w(hVar)) {
                    return true;
                }
            }
            return false;
        }
    }

    public void r(int i10) {
        ei.l.a();
        synchronized (this.f12492w) {
            Iterator it = this.f12492w.iterator();
            while (it.hasNext()) {
                ((l) it.next()).onTrimMemory(i10);
            }
        }
        this.f12487r.a(i10);
        this.f12486q.a(i10);
        this.f12489t.a(i10);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void s(l lVar) {
        synchronized (this.f12492w) {
            if (!this.f12492w.contains(lVar)) {
                throw new IllegalStateException("Cannot unregister not yet registered manager");
            }
            this.f12492w.remove(lVar);
        }
    }
}
