package com.bumptech.glide.request;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.SystemClock;
import android.util.Log;
import androidx.annotation.GuardedBy;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.bumptech.glide.Priority;
import com.bumptech.glide.h;
import com.bumptech.glide.k;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.engine.e0;
import com.bumptech.glide.load.engine.q;
import com.bumptech.glide.load.engine.u;
import com.bumptech.glide.request.target.i;
import com.bumptech.glide.request.target.j;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Executor;
import k5.b;
import q5.c;
import q5.d;
import q5.g;
import t5.p;
import u5.e;

/* JADX INFO: loaded from: classes.dex */
public final class a implements c, i, g {

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public static final boolean f5862u = Log.isLoggable("GlideRequest", 2);

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

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

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

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

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

    @Nullable
    @GuardedBy("requestLock")
    private Drawable errorDrawable;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final Class f5868f;

    @Nullable
    @GuardedBy("requestLock")
    private Drawable fallbackDrawable;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final q5.a f5869g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final int f5870h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final int f5871i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final Priority f5872j;

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

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public final r5.d f5874l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final Executor f5875m;

    @Nullable
    private final Object model;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public e0 f5876n;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public com.bumptech.glide.load.engine.k f5877o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public volatile q f5878p;

    @Nullable
    @GuardedBy("requestLock")
    private Drawable placeholderDrawable;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public SingleRequest$Status f5879q;

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

    @Nullable
    private final List<q5.e> requestListeners;

    @Nullable
    private RuntimeException requestOrigin;

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

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public boolean f5882t;

    @Nullable
    private final String tag;

    @Nullable
    private final q5.e targetListener;

    private a(Context context, k kVar, @NonNull Object obj, @Nullable Object obj2, Class<Object> cls, q5.a aVar, int i10, int i11, Priority priority, j jVar, @Nullable q5.e eVar, @Nullable List<q5.e> list, d dVar, q qVar, r5.d dVar2, Executor executor) {
        this.tag = f5862u ? String.valueOf(hashCode()) : null;
        this.f5863a = new e();
        this.f5864b = obj;
        this.f5866d = context;
        this.f5867e = kVar;
        this.model = obj2;
        this.f5868f = cls;
        this.f5869g = aVar;
        this.f5870h = i10;
        this.f5871i = i11;
        this.f5872j = priority;
        this.f5873k = jVar;
        this.targetListener = eVar;
        this.requestListeners = list;
        this.f5865c = dVar;
        this.f5878p = qVar;
        this.f5874l = dVar2;
        this.f5875m = executor;
        this.f5879q = SingleRequest$Status.f5855a;
        if (this.requestOrigin == null && kVar.f5587h.f5826a.containsKey(h.class)) {
            this.requestOrigin = new RuntimeException("Glide request origin trace");
        }
    }

    public static <R> a obtain(Context context, k kVar, Object obj, Object obj2, Class<R> cls, q5.a aVar, int i10, int i11, Priority priority, j jVar, q5.e eVar, @Nullable List<q5.e> list, d dVar, q qVar, r5.d dVar2, Executor executor) {
        return new a(context, kVar, obj, obj2, cls, aVar, i10, i11, priority, jVar, eVar, list, dVar, qVar, dVar2, executor);
    }

    @Override // q5.c
    public final boolean a() {
        boolean z10;
        synchronized (this.f5864b) {
            z10 = this.f5879q == SingleRequest$Status.f5858d;
        }
        return z10;
    }

    public final void b() {
        if (this.f5882t) {
            throw new IllegalStateException("You can't start or clear loads in RequestListener or Target callbacks. If you're trying to start a fallback request when a load fails, use RequestBuilder#error(RequestBuilder). Otherwise consider posting your into() or clear() calls to the main thread using a Handler instead.");
        }
        this.f5863a.a();
        this.f5873k.a(this);
        com.bumptech.glide.load.engine.k kVar = this.f5877o;
        if (kVar != null) {
            synchronized (((q) kVar.f5723c)) {
                ((u) kVar.f5721a).h((g) kVar.f5722b);
            }
            this.f5877o = null;
        }
    }

    public final Drawable c() {
        int i10;
        if (this.placeholderDrawable == null) {
            q5.a aVar = this.f5869g;
            Drawable placeholderDrawable = aVar.getPlaceholderDrawable();
            this.placeholderDrawable = placeholderDrawable;
            if (placeholderDrawable == null && (i10 = aVar.f32193f) > 0) {
                this.placeholderDrawable = e(i10);
            }
        }
        return this.placeholderDrawable;
    }

    @Override // q5.c
    public final void clear() {
        synchronized (this.f5864b) {
            try {
                if (this.f5882t) {
                    throw new IllegalStateException("You can't start or clear loads in RequestListener or Target callbacks. If you're trying to start a fallback request when a load fails, use RequestBuilder#error(RequestBuilder). Otherwise consider posting your into() or clear() calls to the main thread using a Handler instead.");
                }
                this.f5863a.a();
                SingleRequest$Status singleRequest$Status = this.f5879q;
                SingleRequest$Status singleRequest$Status2 = SingleRequest$Status.f5860f;
                if (singleRequest$Status == singleRequest$Status2) {
                    return;
                }
                b();
                e0 e0Var = this.f5876n;
                if (e0Var != null) {
                    this.f5876n = null;
                } else {
                    e0Var = null;
                }
                d dVar = this.f5865c;
                if (dVar == null || dVar.d(this)) {
                    this.f5873k.onLoadCleared(c());
                }
                this.f5879q = singleRequest$Status2;
                if (e0Var != null) {
                    this.f5878p.getClass();
                    q.d(e0Var);
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final boolean d() {
        d dVar = this.f5865c;
        return dVar == null || !dVar.getRoot().a();
    }

    public final Drawable e(int i10) {
        q5.a aVar = this.f5869g;
        return b.getDrawable(this.f5867e, i10, aVar.getTheme() != null ? aVar.getTheme() : this.f5866d.getTheme());
    }

    @Override // q5.c
    public final boolean f(c cVar) {
        int i10;
        int i11;
        Object obj;
        Class cls;
        q5.a aVar;
        Priority priority;
        int size;
        int i12;
        int i13;
        Object obj2;
        Class cls2;
        q5.a aVar2;
        Priority priority2;
        int size2;
        if (!(cVar instanceof a)) {
            return false;
        }
        synchronized (this.f5864b) {
            try {
                i10 = this.f5870h;
                i11 = this.f5871i;
                obj = this.model;
                cls = this.f5868f;
                aVar = this.f5869g;
                priority = this.f5872j;
                List<q5.e> list = this.requestListeners;
                size = list != null ? list.size() : 0;
            } finally {
            }
        }
        a aVar3 = (a) cVar;
        synchronized (aVar3.f5864b) {
            try {
                i12 = aVar3.f5870h;
                i13 = aVar3.f5871i;
                obj2 = aVar3.model;
                cls2 = aVar3.f5868f;
                aVar2 = aVar3.f5869g;
                priority2 = aVar3.f5872j;
                List<q5.e> list2 = aVar3.requestListeners;
                size2 = list2 != null ? list2.size() : 0;
            } finally {
            }
        }
        return i10 == i12 && i11 == i13 && p.bothModelsNullEquivalentOrEquals(obj, obj2) && cls.equals(cls2) && aVar.equals(aVar2) && priority == priority2 && size == size2;
    }

    @Override // q5.c
    public final boolean g() {
        boolean z10;
        synchronized (this.f5864b) {
            z10 = this.f5879q == SingleRequest$Status.f5860f;
        }
        return z10;
    }

    public final void h(GlideException glideException, int i10) {
        boolean zOnLoadFailed;
        d dVar;
        int i11;
        int i12;
        this.f5863a.a();
        synchronized (this.f5864b) {
            try {
                glideException.setOrigin(this.requestOrigin);
                int i13 = this.f5867e.f5588i;
                if (i13 <= i10) {
                    Objects.toString(this.model);
                    if (i13 <= 4) {
                        ArrayList arrayList = new ArrayList();
                        GlideException.a(glideException, arrayList);
                        int size = arrayList.size();
                        int i14 = 0;
                        while (i14 < size) {
                            int i15 = i14 + 1;
                            i14 = i15;
                        }
                    }
                }
                Drawable drawableC = null;
                this.f5877o = null;
                this.f5879q = SingleRequest$Status.f5859e;
                boolean z10 = true;
                this.f5882t = true;
                try {
                    List<q5.e> list = this.requestListeners;
                    if (list != null) {
                        Iterator<q5.e> it = list.iterator();
                        zOnLoadFailed = false;
                        while (it.hasNext()) {
                            zOnLoadFailed |= it.next().onLoadFailed(glideException, this.model, this.f5873k, d());
                        }
                    } else {
                        zOnLoadFailed = false;
                    }
                    q5.e eVar = this.targetListener;
                    if (eVar == null || !eVar.onLoadFailed(glideException, this.model, this.f5873k, d())) {
                        z10 = false;
                    }
                    if (!(zOnLoadFailed | z10) && ((dVar = this.f5865c) == null || dVar.b(this))) {
                        if (this.model == null) {
                            if (this.fallbackDrawable == null) {
                                q5.a aVar = this.f5869g;
                                Drawable fallbackDrawable = aVar.getFallbackDrawable();
                                this.fallbackDrawable = fallbackDrawable;
                                if (fallbackDrawable == null && (i12 = aVar.f32200m) > 0) {
                                    this.fallbackDrawable = e(i12);
                                }
                            }
                            drawableC = this.fallbackDrawable;
                        }
                        if (drawableC == null) {
                            if (this.errorDrawable == null) {
                                q5.a aVar2 = this.f5869g;
                                Drawable errorPlaceholder = aVar2.getErrorPlaceholder();
                                this.errorDrawable = errorPlaceholder;
                                if (errorPlaceholder == null && (i11 = aVar2.f32192e) > 0) {
                                    this.errorDrawable = e(i11);
                                }
                            }
                            drawableC = this.errorDrawable;
                        }
                        if (drawableC == null) {
                            drawableC = c();
                        }
                        this.f5873k.onLoadFailed(drawableC);
                    }
                    this.f5882t = false;
                    d dVar2 = this.f5865c;
                    if (dVar2 != null) {
                        dVar2.e(this);
                    }
                } finally {
                    this.f5882t = false;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @Override // q5.c
    public final void i() {
        d dVar;
        int i10;
        synchronized (this.f5864b) {
            try {
                if (this.f5882t) {
                    throw new IllegalStateException("You can't start or clear loads in RequestListener or Target callbacks. If you're trying to start a fallback request when a load fails, use RequestBuilder#error(RequestBuilder). Otherwise consider posting your into() or clear() calls to the main thread using a Handler instead.");
                }
                this.f5863a.a();
                int i11 = t5.i.f32966a;
                SystemClock.elapsedRealtimeNanos();
                if (this.model == null) {
                    if (p.g(this.f5870h, this.f5871i)) {
                        this.f5880r = this.f5870h;
                        this.f5881s = this.f5871i;
                    }
                    if (this.fallbackDrawable == null) {
                        q5.a aVar = this.f5869g;
                        Drawable fallbackDrawable = aVar.getFallbackDrawable();
                        this.fallbackDrawable = fallbackDrawable;
                        if (fallbackDrawable == null && (i10 = aVar.f32200m) > 0) {
                            this.fallbackDrawable = e(i10);
                        }
                    }
                    h(new GlideException("Received null model"), this.fallbackDrawable == null ? 5 : 3);
                    return;
                }
                SingleRequest$Status singleRequest$Status = this.f5879q;
                if (singleRequest$Status == SingleRequest$Status.f5856b) {
                    throw new IllegalArgumentException("Cannot restart a running request");
                }
                if (singleRequest$Status == SingleRequest$Status.f5858d) {
                    k(this.f5876n, DataSource.f5593e, false);
                    return;
                }
                List<q5.e> list = this.requestListeners;
                if (list != null) {
                    for (q5.e eVar : list) {
                    }
                }
                SingleRequest$Status singleRequest$Status2 = SingleRequest$Status.f5857c;
                this.f5879q = singleRequest$Status2;
                if (p.g(this.f5870h, this.f5871i)) {
                    m(this.f5870h, this.f5871i);
                } else {
                    this.f5873k.c(this);
                }
                SingleRequest$Status singleRequest$Status3 = this.f5879q;
                if ((singleRequest$Status3 == SingleRequest$Status.f5856b || singleRequest$Status3 == singleRequest$Status2) && ((dVar = this.f5865c) == null || dVar.b(this))) {
                    this.f5873k.onLoadStarted(c());
                }
                if (f5862u) {
                    SystemClock.elapsedRealtimeNanos();
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @Override // q5.c
    public final boolean isRunning() {
        boolean z10;
        synchronized (this.f5864b) {
            try {
                SingleRequest$Status singleRequest$Status = this.f5879q;
                z10 = singleRequest$Status == SingleRequest$Status.f5856b || singleRequest$Status == SingleRequest$Status.f5857c;
            } finally {
            }
        }
        return z10;
    }

    @Override // q5.c
    public final boolean j() {
        boolean z10;
        synchronized (this.f5864b) {
            z10 = this.f5879q == SingleRequest$Status.f5858d;
        }
        return z10;
    }

    public final void k(e0 e0Var, DataSource dataSource, boolean z10) {
        this.f5863a.a();
        e0 e0Var2 = null;
        try {
            synchronized (this.f5864b) {
                try {
                    this.f5877o = null;
                    if (e0Var == null) {
                        h(new GlideException("Expected to receive a Resource<R> with an object of " + this.f5868f + " inside, but instead got null."), 5);
                        return;
                    }
                    Object obj = e0Var.get();
                    try {
                        if (obj != null && this.f5868f.isAssignableFrom(obj.getClass())) {
                            d dVar = this.f5865c;
                            if (dVar == null || dVar.c(this)) {
                                l(e0Var, obj, dataSource);
                                return;
                            }
                            this.f5876n = null;
                            this.f5879q = SingleRequest$Status.f5858d;
                            this.f5878p.getClass();
                            q.d(e0Var);
                            return;
                        }
                        this.f5876n = null;
                        StringBuilder sb = new StringBuilder("Expected to receive an object of ");
                        sb.append(this.f5868f);
                        sb.append(" but instead got ");
                        sb.append(obj != null ? obj.getClass() : "");
                        sb.append("{");
                        sb.append(obj);
                        sb.append("} inside Resource{");
                        sb.append(e0Var);
                        sb.append("}.");
                        sb.append(obj != null ? "" : " To indicate failure return a null Resource object, rather than a Resource object containing null data.");
                        h(new GlideException(sb.toString()), 5);
                        this.f5878p.getClass();
                        q.d(e0Var);
                    } catch (Throwable th) {
                        e0Var2 = e0Var;
                        th = th;
                        throw th;
                    }
                } catch (Throwable th2) {
                    th = th2;
                }
            }
        } catch (Throwable th3) {
            if (e0Var2 != null) {
                this.f5878p.getClass();
                q.d(e0Var2);
            }
            throw th3;
        }
    }

    public final void l(e0 e0Var, Object obj, DataSource dataSource) {
        boolean zOnResourceReady;
        boolean zD = d();
        this.f5879q = SingleRequest$Status.f5858d;
        this.f5876n = e0Var;
        if (this.f5867e.f5588i <= 3) {
            Objects.toString(dataSource);
            Objects.toString(this.model);
            int i10 = t5.i.f32966a;
            SystemClock.elapsedRealtimeNanos();
        }
        boolean z10 = true;
        this.f5882t = true;
        try {
            List<q5.e> list = this.requestListeners;
            if (list != null) {
                Iterator<q5.e> it = list.iterator();
                zOnResourceReady = false;
                while (it.hasNext()) {
                    zOnResourceReady |= it.next().onResourceReady(obj, this.model, this.f5873k, dataSource, zD);
                }
            } else {
                zOnResourceReady = false;
            }
            q5.e eVar = this.targetListener;
            if (eVar == null || !eVar.onResourceReady(obj, this.model, this.f5873k, dataSource, zD)) {
                z10 = false;
            }
            if (!(z10 | zOnResourceReady)) {
                this.f5874l.getClass();
                this.f5873k.onResourceReady(obj, r5.a.f32480a);
            }
            this.f5882t = false;
            d dVar = this.f5865c;
            if (dVar != null) {
                dVar.h(this);
            }
        } catch (Throwable th) {
            this.f5882t = false;
            throw th;
        }
    }

    public final void m(int i10, int i11) {
        Object obj;
        int iRound = i10;
        this.f5863a.a();
        Object obj2 = this.f5864b;
        synchronized (obj2) {
            try {
                try {
                    boolean z10 = f5862u;
                    if (z10) {
                        int i12 = t5.i.f32966a;
                        SystemClock.elapsedRealtimeNanos();
                    }
                    if (this.f5879q == SingleRequest$Status.f5857c) {
                        SingleRequest$Status singleRequest$Status = SingleRequest$Status.f5856b;
                        this.f5879q = singleRequest$Status;
                        float f10 = this.f5869g.f32189b;
                        if (iRound != Integer.MIN_VALUE) {
                            iRound = Math.round(iRound * f10);
                        }
                        this.f5880r = iRound;
                        this.f5881s = i11 == Integer.MIN_VALUE ? i11 : Math.round(f10 * i11);
                        if (z10) {
                            int i13 = t5.i.f32966a;
                            SystemClock.elapsedRealtimeNanos();
                        }
                        q qVar = this.f5878p;
                        k kVar = this.f5867e;
                        Object obj3 = this.model;
                        q5.a aVar = this.f5869g;
                        try {
                            obj = obj2;
                        } catch (Throwable th) {
                            th = th;
                            obj = obj2;
                        }
                        try {
                            this.f5877o = qVar.a(kVar, obj3, aVar.f32197j, this.f5880r, this.f5881s, aVar.S, this.f5868f, this.f5872j, aVar.f32190c, aVar.R, aVar.f32198k, aVar.X, aVar.Q, aVar.f32194g, aVar.V, aVar.Y, aVar.W, this, this.f5875m);
                            if (this.f5879q != singleRequest$Status) {
                                this.f5877o = null;
                            }
                            if (z10) {
                                int i14 = t5.i.f32966a;
                                SystemClock.elapsedRealtimeNanos();
                            }
                        } catch (Throwable th2) {
                            th = th2;
                            throw th;
                        }
                    }
                } catch (Throwable th3) {
                    th = th3;
                }
            } catch (Throwable th4) {
                th = th4;
                obj = obj2;
            }
        }
    }

    @Override // q5.c
    public final void pause() {
        synchronized (this.f5864b) {
            try {
                if (isRunning()) {
                    clear();
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final String toString() {
        Object obj;
        Class cls;
        synchronized (this.f5864b) {
            obj = this.model;
            cls = this.f5868f;
        }
        return super.toString() + "[model=" + obj + ", transcodeClass=" + cls + "]";
    }
}
