package o;

import android.net.TrafficStats;
import com.google.android.gms.common.internal.Preconditions;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
import org.json.JSONException;
import org.json.JSONObject;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* renamed from: o.Iy, reason: case insensitive filesystem */
/* loaded from: classes.dex */
public final class C0232Iy implements InterfaceC0258Jy {
    public static final Object m = new Object();
    public final C0076Cy a;
    public final C0180Gy b;
    public final DH c;
    public final C2358ri0 d;
    public final AK e;
    public final C0964d00 f;
    public final Object g;
    public final ExecutorService h;
    public final K60 i;
    public String j;
    public final HashSet k;
    public final ArrayList l;

    static {
        new AtomicInteger(1);
    }

    public C0232Iy(C0076Cy c0076Cy, DZ dz, ExecutorService executorService, K60 k60) {
        c0076Cy.a();
        C0180Gy c0180Gy = new C0180Gy(c0076Cy.a, dz);
        DH dh = new DH(c0076Cy);
        if (C2616uQ.m == null) {
            C2616uQ.m = new C2616uQ(18);
        }
        C2616uQ c2616uQ = C2616uQ.m;
        if (C2358ri0.d == null) {
            C2358ri0.d = new C2358ri0(c2616uQ);
        }
        C2358ri0 c2358ri0 = C2358ri0.d;
        AK ak = new AK(new C0295Lj(2, c0076Cy));
        C0964d00 c0964d00 = new C0964d00();
        this.g = new Object();
        this.k = new HashSet();
        this.l = new ArrayList();
        this.a = c0076Cy;
        this.b = c0180Gy;
        this.c = dh;
        this.d = c2358ri0;
        this.e = ak;
        this.f = c0964d00;
        this.h = executorService;
        this.i = k60;
    }

    /* JADX WARN: Finally extract failed */
    public final void a() {
        C1108ec c1108ecQ;
        synchronized (m) {
            try {
                C0076Cy c0076Cy = this.a;
                c0076Cy.a();
                C1390hY c1390hYC = C1390hY.c(c0076Cy.a);
                try {
                    c1108ecQ = this.c.q();
                    int i = c1108ecQ.b;
                    boolean z = true;
                    if (i != 2 && i != 1) {
                        z = false;
                    }
                    if (z) {
                        String strF = f(c1108ecQ);
                        DH dh = this.c;
                        C1013dc c1013dcA = c1108ecQ.a();
                        c1013dcA.a = strF;
                        c1013dcA.b = 3;
                        c1108ecQ = c1013dcA.a();
                        dh.n(c1108ecQ);
                    }
                    if (c1390hYC != null) {
                        c1390hYC.A();
                    }
                } catch (Throwable th) {
                    if (c1390hYC != null) {
                        c1390hYC.A();
                    }
                    throw th;
                }
            } catch (Throwable th2) {
                throw th2;
            }
        }
        i(c1108ecQ);
        this.i.execute(new RunnableC0206Hy(this, 2));
    }

    public final C1108ec b(C1108ec c1108ec) throws C0284Ky {
        int responseCode;
        C2061oc c2061ocF;
        C0180Gy c0180Gy = this.b;
        C0076Cy c0076Cy = this.a;
        c0076Cy.a();
        String str = c0076Cy.c.a;
        String str2 = c1108ec.a;
        C0076Cy c0076Cy2 = this.a;
        c0076Cy2.a();
        String str3 = c0076Cy2.c.g;
        String str4 = c1108ec.d;
        C2016o20 c2016o20 = c0180Gy.c;
        if (!c2016o20.a()) {
            throw new C0284Ky("Firebase Installations Service is unavailable. Please try again later.");
        }
        URL urlA = C0180Gy.a("projects/" + str3 + "/installations/" + str2 + "/authTokens:generate");
        for (int i = 0; i <= 1; i++) {
            TrafficStats.setThreadStatsTag(32771);
            HttpURLConnection httpURLConnectionC = c0180Gy.c(urlA, str);
            try {
                try {
                    httpURLConnectionC.setRequestMethod("POST");
                    httpURLConnectionC.addRequestProperty("Authorization", "FIS_v2 " + str4);
                    httpURLConnectionC.setDoOutput(true);
                    C0180Gy.h(httpURLConnectionC);
                    responseCode = httpURLConnectionC.getResponseCode();
                    c2016o20.b(responseCode);
                } finally {
                    httpURLConnectionC.disconnect();
                    TrafficStats.clearThreadStatsTag();
                }
            } catch (IOException | AssertionError unused) {
            }
            if (responseCode >= 200 && responseCode < 300) {
                c2061ocF = C0180Gy.f(httpURLConnectionC);
            } else {
                C0180Gy.b(httpURLConnectionC, null, str, str3);
                if (responseCode == 401 || responseCode == 404) {
                    C0692a8 c0692a8A = C2061oc.a();
                    c0692a8A.b = 3;
                    c2061ocF = c0692a8A.b();
                } else {
                    if (responseCode == 429) {
                        throw new C0284Ky("Firebase servers have received too many requests from this client in a short period of time. Please try again later.");
                    }
                    if (responseCode < 500 || responseCode >= 600) {
                        C0692a8 c0692a8A2 = C2061oc.a();
                        c0692a8A2.b = 2;
                        c2061ocF = c0692a8A2.b();
                    }
                }
            }
            int iW = AbstractC2462sn.w(c2061ocF.c);
            if (iW != 0) {
                if (iW == 1) {
                    C1013dc c1013dcA = c1108ec.a();
                    c1013dcA.g = "BAD CONFIG";
                    c1013dcA.b = 5;
                    return c1013dcA.a();
                }
                if (iW != 2) {
                    throw new C0284Ky("Firebase Installations Service is unavailable. Please try again later.");
                }
                synchronized (this) {
                    this.j = null;
                }
                C1013dc c1013dcA2 = c1108ec.a();
                c1013dcA2.b = 2;
                return c1013dcA2.a();
            }
            String str5 = c2061ocF.a;
            long j = c2061ocF.b;
            C2358ri0 c2358ri0 = this.d;
            c2358ri0.getClass();
            TimeUnit timeUnit = TimeUnit.MILLISECONDS;
            c2358ri0.a.getClass();
            long seconds = timeUnit.toSeconds(System.currentTimeMillis());
            C1013dc c1013dcA3 = c1108ec.a();
            c1013dcA3.c = str5;
            c1013dcA3.e = Long.valueOf(j);
            c1013dcA3.f = Long.valueOf(seconds);
            return c1013dcA3.a();
        }
        throw new C0284Ky("Firebase Installations Service is unavailable. Please try again later.");
    }

    public final Hw0 c() {
        String str;
        e();
        synchronized (this) {
            str = this.j;
        }
        if (str != null) {
            return AbstractC0752ap.K(str);
        }
        Qc0 qc0 = new Qc0();
        LD ld = new LD(qc0);
        synchronized (this.g) {
            this.l.add(ld);
        }
        Hw0 hw0 = qc0.a;
        this.h.execute(new RunnableC0206Hy(this, 0));
        return hw0;
    }

    public final Hw0 d() {
        e();
        Qc0 qc0 = new Qc0();
        KD kd = new KD(this.d, qc0);
        synchronized (this.g) {
            this.l.add(kd);
        }
        Hw0 hw0 = qc0.a;
        this.h.execute(new RunnableC0206Hy(this, 1));
        return hw0;
    }

    public final void e() {
        C0076Cy c0076Cy = this.a;
        c0076Cy.a();
        Preconditions.checkNotEmpty(c0076Cy.c.b, "Please set your Application ID. A valid Firebase App ID is required to communicate with Firebase server APIs: It identifies your application with Firebase.Please refer to https://firebase.google.com/support/privacy/init-options.");
        c0076Cy.a();
        Preconditions.checkNotEmpty(c0076Cy.c.g, "Please set your Project ID. A valid Firebase Project ID is required to communicate with Firebase server APIs: It identifies your application with Firebase.Please refer to https://firebase.google.com/support/privacy/init-options.");
        c0076Cy.a();
        Preconditions.checkNotEmpty(c0076Cy.c.a, "Please set a valid API key. A Firebase API key is required to communicate with Firebase server APIs: It authenticates your project with Google.Please refer to https://firebase.google.com/support/privacy/init-options.");
        c0076Cy.a();
        String str = c0076Cy.c.b;
        Pattern pattern = C2358ri0.c;
        Preconditions.checkArgument(str.contains(":"), "Please set your Application ID. A valid Firebase App ID is required to communicate with Firebase server APIs: It identifies your application with Firebase.Please refer to https://firebase.google.com/support/privacy/init-options.");
        c0076Cy.a();
        Preconditions.checkArgument(C2358ri0.c.matcher(c0076Cy.c.a).matches(), "Please set a valid API key. A Firebase API key is required to communicate with Firebase server APIs: It authenticates your project with Google.Please refer to https://firebase.google.com/support/privacy/init-options.");
    }

    /* JADX WARN: Removed duplicated region for block: B:6:0x001e  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final java.lang.String f(o.C1108ec r6) {
        /*
            r5 = this;
            o.Cy r0 = r5.a
            r0.a()
            java.lang.String r0 = r0.b
            java.lang.String r1 = "CHIME_ANDROID_SDK"
            boolean r0 = r0.equals(r1)
            if (r0 != 0) goto L1e
            o.Cy r0 = r5.a
            java.lang.String r1 = "[DEFAULT]"
            r0.a()
            java.lang.String r0 = r0.b
            boolean r0 = r1.equals(r0)
            if (r0 == 0) goto L5c
        L1e:
            int r6 = r6.b
            r0 = 1
            if (r6 != r0) goto L5c
            o.AK r6 = r5.e
            java.lang.Object r6 = r6.get()
            o.rG r6 = (o.C2321rG) r6
            android.content.SharedPreferences r0 = r6.a
            monitor-enter(r0)
            android.content.SharedPreferences r1 = r6.a     // Catch: java.lang.Throwable -> L3f
            monitor-enter(r1)     // Catch: java.lang.Throwable -> L3f
            android.content.SharedPreferences r2 = r6.a     // Catch: java.lang.Throwable -> L57
            java.lang.String r3 = "|S|id"
            r4 = 0
            java.lang.String r2 = r2.getString(r3, r4)     // Catch: java.lang.Throwable -> L57
            monitor-exit(r1)     // Catch: java.lang.Throwable -> L57
            if (r2 == 0) goto L41
            monitor-exit(r0)     // Catch: java.lang.Throwable -> L3f
            goto L46
        L3f:
            r6 = move-exception
            goto L5a
        L41:
            java.lang.String r2 = r6.a()     // Catch: java.lang.Throwable -> L3f
            monitor-exit(r0)     // Catch: java.lang.Throwable -> L3f
        L46:
            boolean r6 = android.text.TextUtils.isEmpty(r2)
            if (r6 == 0) goto L56
            o.d00 r6 = r5.f
            r6.getClass()
            java.lang.String r6 = o.C0964d00.a()
            return r6
        L56:
            return r2
        L57:
            r6 = move-exception
            monitor-exit(r1)     // Catch: java.lang.Throwable -> L57
            throw r6     // Catch: java.lang.Throwable -> L3f
        L5a:
            monitor-exit(r0)     // Catch: java.lang.Throwable -> L3f
            throw r6
        L5c:
            o.d00 r6 = r5.f
            r6.getClass()
            java.lang.String r6 = o.C0964d00.a()
            return r6
        */
        throw new UnsupportedOperationException("Method not decompiled: o.C0232Iy.f(o.ec):java.lang.String");
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r2v2, types: [o.Gy] */
    /* JADX WARN: Type inference failed for: r2v22 */
    /* JADX WARN: Type inference failed for: r2v23 */
    /* JADX WARN: Type inference failed for: r2v24 */
    /* JADX WARN: Type inference failed for: r2v3 */
    /* JADX WARN: Type inference failed for: r2v4 */
    /* JADX WARN: Type inference failed for: r2v6 */
    /* JADX WARN: Type inference failed for: r2v7, types: [o.Ub] */
    public final C1108ec g(C1108ec c1108ec) throws C0284Ky {
        int responseCode;
        String str = c1108ec.a;
        String string = null;
        if (str != null && str.length() == 11) {
            C2321rG c2321rG = (C2321rG) this.e.get();
            synchronized (c2321rG.a) {
                try {
                    String[] strArr = C2321rG.c;
                    int i = 0;
                    while (true) {
                        if (i < 4) {
                            String str2 = strArr[i];
                            String string2 = c2321rG.a.getString("|T|" + c2321rG.b + "|" + str2, null);
                            if (string2 == null || string2.isEmpty()) {
                                i++;
                            } else if (string2.startsWith("{")) {
                                try {
                                    string = new JSONObject(string2).getString("token");
                                } catch (JSONException unused) {
                                }
                            } else {
                                string = string2;
                            }
                        }
                    }
                } finally {
                }
            }
        }
        C0180Gy c0180Gy = this.b;
        C0076Cy c0076Cy = this.a;
        c0076Cy.a();
        String str3 = c0076Cy.c.a;
        String str4 = c1108ec.a;
        C0076Cy c0076Cy2 = this.a;
        c0076Cy2.a();
        String str5 = c0076Cy2.c.g;
        C0076Cy c0076Cy3 = this.a;
        c0076Cy3.a();
        String str6 = c0076Cy3.c.b;
        C2016o20 c2016o20 = c0180Gy.c;
        if (!c2016o20.a()) {
            throw new C0284Ky("Firebase Installations Service is unavailable. Please try again later.");
        }
        URL urlA = C0180Gy.a("projects/" + str5 + "/installations");
        int i2 = 0;
        C0520Ub c0520Ub = c0180Gy;
        while (i2 <= 1) {
            TrafficStats.setThreadStatsTag(32769);
            HttpURLConnection httpURLConnectionC = c0520Ub.c(urlA, str3);
            try {
                try {
                    httpURLConnectionC.setRequestMethod("POST");
                    httpURLConnectionC.setDoOutput(true);
                    if (string != null) {
                        httpURLConnectionC.addRequestProperty("x-goog-fis-android-iid-migration-auth", string);
                    }
                    C0180Gy.g(httpURLConnectionC, str4, str6);
                    responseCode = httpURLConnectionC.getResponseCode();
                    c2016o20.b(responseCode);
                } catch (IOException | AssertionError unused2) {
                }
                if (responseCode >= 200 && responseCode < 300) {
                    C0520Ub c0520UbE = C0180Gy.e(httpURLConnectionC);
                    httpURLConnectionC.disconnect();
                    TrafficStats.clearThreadStatsTag();
                    c0520Ub = c0520UbE;
                } else {
                    try {
                        C0180Gy.b(httpURLConnectionC, str6, str3, str5);
                    } catch (IOException | AssertionError unused3) {
                        httpURLConnectionC.disconnect();
                        TrafficStats.clearThreadStatsTag();
                        i2++;
                        c0520Ub = c0520Ub;
                    }
                    if (responseCode == 429) {
                        throw new C0284Ky("Firebase servers have received too many requests from this client in a short period of time. Please try again later.");
                    }
                    if (responseCode < 500 || responseCode >= 600) {
                        C0520Ub c0520Ub2 = new C0520Ub(null, null, null, null, 2);
                        httpURLConnectionC.disconnect();
                        TrafficStats.clearThreadStatsTag();
                        c0520Ub = c0520Ub2;
                    } else {
                        httpURLConnectionC.disconnect();
                        TrafficStats.clearThreadStatsTag();
                        i2++;
                        c0520Ub = c0520Ub;
                    }
                }
                int iW = AbstractC2462sn.w(c0520Ub.e);
                if (iW != 0) {
                    if (iW != 1) {
                        throw new C0284Ky("Firebase Installations Service is unavailable. Please try again later.");
                    }
                    C1013dc c1013dcA = c1108ec.a();
                    c1013dcA.g = "BAD CONFIG";
                    c1013dcA.b = 5;
                    return c1013dcA.a();
                }
                String str7 = c0520Ub.b;
                String str8 = c0520Ub.c;
                C2358ri0 c2358ri0 = this.d;
                c2358ri0.getClass();
                TimeUnit timeUnit = TimeUnit.MILLISECONDS;
                c2358ri0.a.getClass();
                long seconds = timeUnit.toSeconds(System.currentTimeMillis());
                C2061oc c2061oc = c0520Ub.d;
                String str9 = c2061oc.a;
                long j = c2061oc.b;
                C1013dc c1013dcA2 = c1108ec.a();
                c1013dcA2.a = str7;
                c1013dcA2.b = 4;
                c1013dcA2.c = str9;
                c1013dcA2.d = str8;
                c1013dcA2.e = Long.valueOf(j);
                c1013dcA2.f = Long.valueOf(seconds);
                return c1013dcA2.a();
            } finally {
                httpURLConnectionC.disconnect();
                TrafficStats.clearThreadStatsTag();
            }
        }
        throw new C0284Ky("Firebase Installations Service is unavailable. Please try again later.");
    }

    public final void h(Exception exc) {
        synchronized (this.g) {
            try {
                Iterator it = this.l.iterator();
                while (it.hasNext()) {
                    if (((Fa0) it.next()).b(exc)) {
                        it.remove();
                    }
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void i(C1108ec c1108ec) {
        synchronized (this.g) {
            try {
                Iterator it = this.l.iterator();
                while (it.hasNext()) {
                    if (((Fa0) it.next()).a(c1108ec)) {
                        it.remove();
                    }
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }
}
