package j;

import h.s;
import java.io.IOException;
import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.Response;
import okhttp3.ResponseBody;

/* JADX INFO: compiled from: OkHttpCall.java */
/* JADX INFO: loaded from: classes.dex */
final class i<T> implements j.b<T> {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final o<T, ?> f4331b;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    @Nullable
    private final Object[] f4332f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private volatile boolean f4333g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    @GuardedBy("this")
    @Nullable
    private Call f4334h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    @GuardedBy("this")
    @Nullable
    private Throwable f4335i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    @GuardedBy("this")
    private boolean f4336j;

    /* JADX INFO: compiled from: OkHttpCall.java */
    class a implements Callback {
        final /* synthetic */ d a;

        a(d dVar) {
            this.a = dVar;
        }

        private void a(Throwable th) {
            try {
                this.a.b(i.this, th);
            } catch (Throwable th2) {
                th2.printStackTrace();
            }
        }

        private void b(m<T> mVar) {
            try {
                this.a.a(i.this, mVar);
            } catch (Throwable th) {
                th.printStackTrace();
            }
        }

        @Override // okhttp3.Callback
        public void onFailure(Call call, IOException iOException) {
            try {
                this.a.b(i.this, iOException);
            } catch (Throwable th) {
                th.printStackTrace();
            }
        }

        @Override // okhttp3.Callback
        public void onResponse(Call call, Response response) throws IOException {
            try {
                b(i.this.c(response));
            } catch (Throwable th) {
                a(th);
            }
        }
    }

    /* JADX INFO: compiled from: OkHttpCall.java */
    static final class b extends ResponseBody {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private final ResponseBody f4338b;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        IOException f4339f;

        /* JADX INFO: compiled from: OkHttpCall.java */
        class a extends h.h {
            a(s sVar) {
                super(sVar);
            }

            @Override // h.h, h.s
            public long read(h.c cVar, long j2) throws IOException {
                try {
                    return super.read(cVar, j2);
                } catch (IOException e2) {
                    b.this.f4339f = e2;
                    throw e2;
                }
            }
        }

        b(ResponseBody responseBody) {
            this.f4338b = responseBody;
        }

        void a() throws IOException {
            IOException iOException = this.f4339f;
            if (iOException != null) {
                throw iOException;
            }
        }

        @Override // okhttp3.ResponseBody, java.io.Closeable, java.lang.AutoCloseable
        public void close() {
            this.f4338b.close();
        }

        @Override // okhttp3.ResponseBody
        public long contentLength() {
            return this.f4338b.contentLength();
        }

        @Override // okhttp3.ResponseBody
        public MediaType contentType() {
            return this.f4338b.contentType();
        }

        @Override // okhttp3.ResponseBody
        public h.e source() {
            return h.l.d(new a(this.f4338b.source()));
        }
    }

    /* JADX INFO: compiled from: OkHttpCall.java */
    static final class c extends ResponseBody {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private final MediaType f4341b;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        private final long f4342f;

        c(MediaType mediaType, long j2) {
            this.f4341b = mediaType;
            this.f4342f = j2;
        }

        @Override // okhttp3.ResponseBody
        public long contentLength() {
            return this.f4342f;
        }

        @Override // okhttp3.ResponseBody
        public MediaType contentType() {
            return this.f4341b;
        }

        @Override // okhttp3.ResponseBody
        public h.e source() {
            throw new IllegalStateException("Cannot read raw response body of a converted body.");
        }
    }

    i(o<T, ?> oVar, @Nullable Object[] objArr) {
        this.f4331b = oVar;
        this.f4332f = objArr;
    }

    private Call b() throws IOException {
        Call callNewCall = this.f4331b.a.newCall(this.f4331b.c(this.f4332f));
        if (callNewCall != null) {
            return callNewCall;
        }
        throw new NullPointerException("Call.Factory returned null.");
    }

    @Override // j.b
    /* JADX INFO: renamed from: a, reason: merged with bridge method [inline-methods] and merged with bridge method [inline-methods] */
    public i<T> m6clone() {
        return new i<>(this.f4331b, this.f4332f);
    }

    m<T> c(Response response) throws IOException {
        ResponseBody responseBodyBody = response.body();
        Response responseBuild = response.newBuilder().body(new c(responseBodyBody.contentType(), responseBodyBody.contentLength())).build();
        int iCode = responseBuild.code();
        if (iCode < 200 || iCode >= 300) {
            try {
                return m.c(p.a(responseBodyBody), responseBuild);
            } finally {
                responseBodyBody.close();
            }
        }
        if (iCode == 204 || iCode == 205) {
            responseBodyBody.close();
            return m.f(null, responseBuild);
        }
        b bVar = new b(responseBodyBody);
        try {
            return m.f(this.f4331b.d(bVar), responseBuild);
        } catch (RuntimeException e2) {
            bVar.a();
            throw e2;
        }
    }

    @Override // j.b
    public void cancel() {
        Call call;
        this.f4333g = true;
        synchronized (this) {
            call = this.f4334h;
        }
        if (call != null) {
            call.cancel();
        }
    }

    @Override // j.b
    public void d(d<T> dVar) {
        Call call;
        Throwable th;
        p.b(dVar, "callback == null");
        synchronized (this) {
            if (this.f4336j) {
                throw new IllegalStateException("Already executed.");
            }
            this.f4336j = true;
            call = this.f4334h;
            th = this.f4335i;
            if (call == null && th == null) {
                try {
                    Call callB = b();
                    this.f4334h = callB;
                    call = callB;
                } catch (Throwable th2) {
                    th = th2;
                    this.f4335i = th;
                }
            }
        }
        if (th != null) {
            dVar.b(this, th);
            return;
        }
        if (this.f4333g) {
            call.cancel();
        }
        call.enqueue(new a(dVar));
    }

    @Override // j.b
    public m<T> execute() throws IOException {
        Call callB;
        synchronized (this) {
            if (this.f4336j) {
                throw new IllegalStateException("Already executed.");
            }
            this.f4336j = true;
            if (this.f4335i != null) {
                if (this.f4335i instanceof IOException) {
                    throw ((IOException) this.f4335i);
                }
                throw ((RuntimeException) this.f4335i);
            }
            callB = this.f4334h;
            if (callB == null) {
                try {
                    callB = b();
                    this.f4334h = callB;
                } catch (IOException | RuntimeException e2) {
                    this.f4335i = e2;
                    throw e2;
                }
            }
        }
        if (this.f4333g) {
            callB.cancel();
        }
        return c(callB.execute());
    }

    @Override // j.b
    public boolean isCanceled() {
        boolean z = true;
        if (this.f4333g) {
            return true;
        }
        synchronized (this) {
            if (this.f4334h == null || !this.f4334h.isCanceled()) {
                z = false;
            }
        }
        return z;
    }
}
