package yh;

import android.app.Activity;
import android.app.Application;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.ContextWrapper;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import androidx.fragment.app.k0;
import androidx.fragment.app.z;
import com.bumptech.glide.c;
import java.util.HashMap;
import java.util.Map;

/* loaded from: classes4.dex */
public class r implements Handler.Callback {

    /* renamed from: k, reason: collision with root package name */
    private static final b f52757k = new a();

    /* renamed from: a, reason: collision with root package name */
    private volatile com.bumptech.glide.l f52758a;

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

    /* renamed from: e, reason: collision with root package name */
    private final b f52762e;

    /* renamed from: i, reason: collision with root package name */
    private final k f52766i;

    /* renamed from: j, reason: collision with root package name */
    private final o f52767j;

    /* renamed from: b, reason: collision with root package name */
    final Map f52759b = new HashMap();

    /* renamed from: c, reason: collision with root package name */
    final Map f52760c = new HashMap();

    /* renamed from: f, reason: collision with root package name */
    private final androidx.collection.a f52763f = new androidx.collection.a();

    /* renamed from: g, reason: collision with root package name */
    private final androidx.collection.a f52764g = new androidx.collection.a();

    /* renamed from: h, reason: collision with root package name */
    private final Bundle f52765h = new Bundle();

    /* loaded from: classes4.dex */
    class a implements b {
        a() {
        }

        @Override // yh.r.b
        public com.bumptech.glide.l a(com.bumptech.glide.b bVar, l lVar, s sVar, Context context) {
            return new com.bumptech.glide.l(bVar, lVar, sVar, context);
        }
    }

    /* loaded from: classes4.dex */
    public interface b {
        com.bumptech.glide.l a(com.bumptech.glide.b bVar, l lVar, s sVar, Context context);
    }

    public r(b bVar, com.bumptech.glide.e eVar) {
        bVar = bVar == null ? f52757k : bVar;
        this.f52762e = bVar;
        this.f52761d = new Handler(Looper.getMainLooper(), this);
        this.f52767j = new o(bVar);
        this.f52766i = b(eVar);
    }

    private static void a(Activity activity) {
        if (activity.isDestroyed()) {
            throw new IllegalArgumentException("You cannot start a load for a destroyed activity");
        }
    }

    private static k b(com.bumptech.glide.e eVar) {
        return (sh.r.f35752h && sh.r.f35751g) ? eVar.a(c.d.class) ? new i() : new j() : new g();
    }

    private static Activity c(Context context) {
        if (context instanceof Activity) {
            return (Activity) context;
        }
        if (context instanceof ContextWrapper) {
            return c(((ContextWrapper) context).getBaseContext());
        }
        return null;
    }

    private com.bumptech.glide.l d(Context context, FragmentManager fragmentManager, Fragment fragment, boolean z10) {
        q j10 = j(fragmentManager, fragment);
        com.bumptech.glide.l e10 = j10.e();
        if (e10 == null) {
            e10 = this.f52762e.a(com.bumptech.glide.b.c(context), j10.c(), j10.f(), context);
            if (z10) {
                e10.onStart();
            }
            j10.k(e10);
        }
        return e10;
    }

    private com.bumptech.glide.l h(Context context) {
        if (this.f52758a == null) {
            synchronized (this) {
                if (this.f52758a == null) {
                    this.f52758a = this.f52762e.a(com.bumptech.glide.b.c(context.getApplicationContext()), new yh.b(), new h(), context.getApplicationContext());
                }
            }
        }
        return this.f52758a;
    }

    private q j(FragmentManager fragmentManager, Fragment fragment) {
        q qVar = (q) this.f52759b.get(fragmentManager);
        if (qVar != null) {
            return qVar;
        }
        q qVar2 = (q) fragmentManager.findFragmentByTag("com.bumptech.glide.manager");
        if (qVar2 == null) {
            qVar2 = new q();
            qVar2.j(fragment);
            this.f52759b.put(fragmentManager, qVar2);
            fragmentManager.beginTransaction().add(qVar2, "com.bumptech.glide.manager").commitAllowingStateLoss();
            this.f52761d.obtainMessage(1, fragmentManager).sendToTarget();
        }
        return qVar2;
    }

    private v l(z zVar, androidx.fragment.app.f fVar) {
        v vVar = (v) this.f52760c.get(zVar);
        if (vVar != null) {
            return vVar;
        }
        v vVar2 = (v) zVar.l0("com.bumptech.glide.manager");
        if (vVar2 == null) {
            vVar2 = new v();
            vVar2.G(fVar);
            this.f52760c.put(zVar, vVar2);
            zVar.p().e(vVar2, "com.bumptech.glide.manager").j();
            this.f52761d.obtainMessage(2, zVar).sendToTarget();
        }
        return vVar2;
    }

    private static boolean m(Context context) {
        Activity c10 = c(context);
        return c10 == null || !c10.isFinishing();
    }

    private boolean n(FragmentManager fragmentManager, boolean z10) {
        q qVar = (q) this.f52759b.get(fragmentManager);
        q qVar2 = (q) fragmentManager.findFragmentByTag("com.bumptech.glide.manager");
        if (qVar2 == qVar) {
            return true;
        }
        if (qVar2 != null && qVar2.e() != null) {
            throw new IllegalStateException("We've added two fragments with requests! Old: " + qVar2 + " New: " + qVar);
        }
        if (z10 || fragmentManager.isDestroyed()) {
            if (Log.isLoggable("RMRetriever", 5)) {
                if (fragmentManager.isDestroyed()) {
                    Log.w("RMRetriever", "Parent was destroyed before our Fragment could be added");
                } else {
                    Log.w("RMRetriever", "Tried adding Fragment twice and failed twice, giving up!");
                }
            }
            qVar.c().c();
            return true;
        }
        FragmentTransaction add = fragmentManager.beginTransaction().add(qVar, "com.bumptech.glide.manager");
        if (qVar2 != null) {
            add.remove(qVar2);
        }
        add.commitAllowingStateLoss();
        this.f52761d.obtainMessage(1, 1, 0, fragmentManager).sendToTarget();
        if (Log.isLoggable("RMRetriever", 3)) {
            Log.d("RMRetriever", "We failed to add our Fragment the first time around, trying again...");
        }
        return false;
    }

    private boolean o(z zVar, boolean z10) {
        v vVar = (v) this.f52760c.get(zVar);
        v vVar2 = (v) zVar.l0("com.bumptech.glide.manager");
        if (vVar2 == vVar) {
            return true;
        }
        if (vVar2 != null && vVar2.B() != null) {
            throw new IllegalStateException("We've added two fragments with requests! Old: " + vVar2 + " New: " + vVar);
        }
        if (z10 || zVar.M0()) {
            if (zVar.M0()) {
                if (Log.isLoggable("RMRetriever", 5)) {
                    Log.w("RMRetriever", "Parent was destroyed before our Fragment could be added, all requests for the destroyed parent are cancelled");
                }
            } else if (Log.isLoggable("RMRetriever", 6)) {
                Log.e("RMRetriever", "ERROR: Tried adding Fragment twice and failed twice, giving up and cancelling all associated requests! This probably means you're starting loads in a unit test with an Activity that you haven't created and never create. If you're using Robolectric, create the Activity as part of your test setup");
            }
            vVar.z().c();
            return true;
        }
        k0 e10 = zVar.p().e(vVar, "com.bumptech.glide.manager");
        if (vVar2 != null) {
            e10.r(vVar2);
        }
        e10.l();
        this.f52761d.obtainMessage(2, 1, 0, zVar).sendToTarget();
        if (Log.isLoggable("RMRetriever", 3)) {
            Log.d("RMRetriever", "We failed to add our Fragment the first time around, trying again...");
        }
        return false;
    }

    public com.bumptech.glide.l e(Activity activity) {
        if (ei.l.p()) {
            return f(activity.getApplicationContext());
        }
        if (activity instanceof androidx.fragment.app.k) {
            return g((androidx.fragment.app.k) activity);
        }
        a(activity);
        this.f52766i.a(activity);
        return d(activity, activity.getFragmentManager(), null, m(activity));
    }

    public com.bumptech.glide.l f(Context context) {
        if (context == null) {
            throw new IllegalArgumentException("You cannot start a load on a null Context");
        }
        if (ei.l.q() && !(context instanceof Application)) {
            if (context instanceof androidx.fragment.app.k) {
                return g((androidx.fragment.app.k) context);
            }
            if (context instanceof Activity) {
                return e((Activity) context);
            }
            if (context instanceof ContextWrapper) {
                ContextWrapper contextWrapper = (ContextWrapper) context;
                if (contextWrapper.getBaseContext().getApplicationContext() != null) {
                    return f(contextWrapper.getBaseContext());
                }
            }
        }
        return h(context);
    }

    public com.bumptech.glide.l g(androidx.fragment.app.k kVar) {
        if (ei.l.p()) {
            return f(kVar.getApplicationContext());
        }
        a(kVar);
        this.f52766i.a(kVar);
        boolean m10 = m(kVar);
        return this.f52767j.b(kVar, com.bumptech.glide.b.c(kVar.getApplicationContext()), kVar.getLifecycle(), kVar.getSupportFragmentManager(), m10);
    }

    @Override // android.os.Handler.Callback
    public boolean handleMessage(Message message) {
        FragmentManager fragmentManager;
        FragmentManager fragmentManager2;
        boolean z10 = true;
        boolean z11 = false;
        boolean z12 = message.arg1 == 1;
        int i10 = message.what;
        Object obj = null;
        if (i10 == 1) {
            FragmentManager fragmentManager3 = (FragmentManager) message.obj;
            if (n(fragmentManager3, z12)) {
                obj = this.f52759b.remove(fragmentManager3);
                fragmentManager2 = fragmentManager3;
                z11 = true;
                fragmentManager = fragmentManager2;
            }
            fragmentManager = null;
            z11 = true;
            z10 = false;
        } else if (i10 != 2) {
            z10 = false;
            fragmentManager = null;
        } else {
            z zVar = (z) message.obj;
            if (o(zVar, z12)) {
                obj = this.f52760c.remove(zVar);
                fragmentManager2 = zVar;
                z11 = true;
                fragmentManager = fragmentManager2;
            }
            fragmentManager = null;
            z11 = true;
            z10 = false;
        }
        if (Log.isLoggable("RMRetriever", 5) && z10 && obj == null) {
            Log.w("RMRetriever", "Failed to remove expected request manager fragment, manager: " + fragmentManager);
        }
        return z11;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public q i(Activity activity) {
        return j(activity.getFragmentManager(), null);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public v k(z zVar) {
        return l(zVar, null);
    }
}
