package defpackage;

import android.content.Context;
import android.os.Build;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigClientException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Random;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/* renamed from: ᵳˉ, reason: contains not printable characters */
/* loaded from: classes3.dex */
public final class C4556 {

    /* renamed from: ˌʾˀ, reason: contains not printable characters */
    public final C4093 f16930;

    /* renamed from: ˌᵜᵹᵩ, reason: contains not printable characters */
    public final Context f16931;

    /* renamed from: ˎˏʜʾ, reason: contains not printable characters */
    public final ScheduledExecutorService f16932;

    /* renamed from: ˏˋٴ, reason: contains not printable characters */
    public HttpURLConnection f16933;

    /* renamed from: ˏᵙ, reason: contains not printable characters */
    public int f16934;

    /* renamed from: ᵝᵵᵺ, reason: contains not printable characters */
    public final C2288 f16935;

    /* renamed from: ᵥᵯˎ, reason: contains not printable characters */
    public final LinkedHashSet f16937;

    /* renamed from: ᵶᵷᵕ, reason: contains not printable characters */
    public final InterfaceC0466 f16942;

    /* renamed from: ᵸˑ, reason: contains not printable characters */
    public final C0382 f16943;

    /* renamed from: ᵹᵝᵶ, reason: contains not printable characters */
    public final C4995 f16944;

    /* renamed from: ᵸʓᵠ, reason: contains not printable characters */
    public static final int[] f16928 = {2, 4, 8, 16, 32, 64, 128, 256};

    /* renamed from: ᵞʡ, reason: contains not printable characters */
    public static final Pattern f16927 = Pattern.compile("^[^:]+:([0-9]+):(android|ios|web):([0-9a-f]+)");

    /* renamed from: ᵴᵳ, reason: contains not printable characters */
    public boolean f16941 = false;

    /* renamed from: ᵪʩ, reason: contains not printable characters */
    public final Random f16938 = new Random();

    /* renamed from: ᵳٴᵶ, reason: contains not printable characters */
    public final ˊᵻ f16940 = ˊᵻ.ᵾᵬ;

    /* renamed from: ᵯٴᵦ, reason: contains not printable characters */
    public final String f16939 = "firebase";

    /* renamed from: ᵻᵢﹳᵰ, reason: contains not printable characters */
    public boolean f16945 = false;

    /* renamed from: ˇʼʤˎ, reason: contains not printable characters */
    public boolean f16929 = false;

    /* renamed from: ᵡʠ, reason: contains not printable characters */
    public final Object f16936 = new Object();

    public C4556(C2288 c2288, InterfaceC0466 interfaceC0466, C4995 c4995, C0382 c0382, Context context, LinkedHashSet linkedHashSet, C4093 c4093, ScheduledExecutorService scheduledExecutorService) {
        this.f16937 = linkedHashSet;
        this.f16932 = scheduledExecutorService;
        this.f16934 = Math.max(8 - c4093.m8972().f15648, 1);
        this.f16935 = c2288;
        this.f16944 = c4995;
        this.f16942 = interfaceC0466;
        this.f16943 = c0382;
        this.f16931 = context;
        this.f16930 = c4093;
    }

    /* renamed from: ˏˋٴ, reason: contains not printable characters */
    public static String m9673(InputStream inputStream) {
        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            while (true) {
                String line = bufferedReader.readLine();
                if (line == null) {
                    break;
                }
                sb.append(line);
            }
        } catch (IOException unused) {
            if (sb.length() == 0) {
                return "Unable to connect to the server, access is forbidden. HTTP status code: 403";
            }
        }
        return sb.toString();
    }

    /* renamed from: ᵻᵢﹳᵰ, reason: contains not printable characters */
    public static boolean m9674(int i) {
        return i == 408 || i == 429 || i == 502 || i == 503 || i == 504;
    }

    /* renamed from: ˇʼʤˎ, reason: contains not printable characters */
    public final synchronized void m9675(long j) {
        try {
            if (m9680()) {
                int i = this.f16934;
                if (i > 0) {
                    this.f16934 = i - 1;
                    this.f16932.schedule(new RunnableC2802(4, this), j, TimeUnit.MILLISECONDS);
                } else if (!this.f16929) {
                    new FirebaseRemoteConfigClientException("Unable to connect to the server. Check your connection and try again.");
                    m9677();
                }
            }
        } catch (Throwable th) {
            throw th;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:12:0x0071  */
    /* renamed from: ˌᵜᵹᵩ, reason: contains not printable characters */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final void m9676(java.net.HttpURLConnection r6, java.lang.String r7, java.lang.String r8) {
        /*
            r5 = this;
            java.lang.String r0 = "POST"
            r6.setRequestMethod(r0)
            java.lang.String r0 = "X-Goog-Firebase-Installations-Auth"
            r6.setRequestProperty(r0, r8)
            ᵕˎˊ r8 = r5.f16935
            r8.m5993()
            ᵮﾞᵛʗ r0 = r8.f8945
            java.lang.String r1 = r0.f15903
            java.lang.String r2 = "X-Goog-Api-Key"
            r6.setRequestProperty(r2, r1)
            android.content.Context r1 = r5.f16931
            java.lang.String r2 = r1.getPackageName()
            java.lang.String r3 = "X-Android-Package"
            r6.setRequestProperty(r3, r2)
            r2 = 0
            java.lang.String r3 = r1.getPackageName()     // Catch: android.content.pm.PackageManager.NameNotFoundException -> L38
            byte[] r3 = ᵳʿˉ.ᵨᵳʝ(r1, r3)     // Catch: android.content.pm.PackageManager.NameNotFoundException -> L38
            if (r3 != 0) goto L33
            r1.getPackageName()     // Catch: android.content.pm.PackageManager.NameNotFoundException -> L38
        L31:
            r1 = r2
            goto L3c
        L33:
            java.lang.String r1 = ᵭʝʜ.ᵻᵢﹳᵰ(r3)     // Catch: android.content.pm.PackageManager.NameNotFoundException -> L38
            goto L3c
        L38:
            r1.getPackageName()
            goto L31
        L3c:
            java.lang.String r3 = "X-Android-Cert"
            r6.setRequestProperty(r3, r1)
            java.lang.String r1 = "X-Google-GFE-Can-Retry"
            java.lang.String r3 = "yes"
            r6.setRequestProperty(r1, r3)
            java.lang.String r1 = "X-Accept-Response-Streaming"
            java.lang.String r3 = "true"
            r6.setRequestProperty(r1, r3)
            java.lang.String r1 = "Content-Type"
            java.lang.String r3 = "application/json"
            r6.setRequestProperty(r1, r3)
            java.lang.String r1 = "Accept"
            r6.setRequestProperty(r1, r3)
            java.util.HashMap r1 = new java.util.HashMap
            r1.<init>()
            r8.m5993()
            java.lang.String r3 = r0.f15904
            java.util.regex.Pattern r4 = defpackage.C4556.f16927
            java.util.regex.Matcher r3 = r4.matcher(r3)
            boolean r4 = r3.matches()
            if (r4 == 0) goto L76
            r2 = 1
            java.lang.String r2 = r3.group(r2)
        L76:
            java.lang.String r3 = "project"
            r1.put(r3, r2)
            java.lang.String r2 = "namespace"
            java.lang.String r3 = r5.f16939
            r1.put(r2, r3)
            ᵸᵮ r5 = r5.f16944
            ᵭʨ r5 = r5.f18537
            android.content.SharedPreferences r5 = r5.f15298
            java.lang.String r2 = "last_template_version"
            r3 = 0
            long r2 = r5.getLong(r2, r3)
            java.lang.String r5 = java.lang.Long.toString(r2)
            java.lang.String r2 = "lastKnownVersionNumber"
            r1.put(r2, r5)
            r8.m5993()
            java.lang.String r5 = r0.f15904
            java.lang.String r8 = "appId"
            r1.put(r8, r5)
            java.lang.String r5 = "sdkVersion"
            java.lang.String r8 = "23.1.0"
            r1.put(r5, r8)
            java.lang.String r5 = "appInstanceId"
            r1.put(r5, r7)
            org.json.JSONObject r5 = new org.json.JSONObject
            r5.<init>(r1)
            java.lang.String r5 = r5.toString()
            java.lang.String r7 = "utf-8"
            byte[] r5 = r5.getBytes(r7)
            java.io.BufferedOutputStream r7 = new java.io.BufferedOutputStream
            java.io.OutputStream r6 = r6.getOutputStream()
            r7.<init>(r6)
            r7.write(r5)
            r7.flush()
            r7.close()
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: defpackage.C4556.m9676(java.net.HttpURLConnection, java.lang.String, java.lang.String):void");
    }

    /* renamed from: ˎˏʜʾ, reason: contains not printable characters */
    public final synchronized void m9677() {
        Iterator it = this.f16937.iterator();
        while (it.hasNext()) {
            ((C3583) it.next()).m8191();
        }
    }

    /* renamed from: ˏᵙ, reason: contains not printable characters */
    public final String m9678(String str) {
        C2288 c2288 = this.f16935;
        c2288.m5993();
        Matcher matcher = f16927.matcher(c2288.f8945.f15904);
        return AbstractC1327.m4105("https://firebaseremoteconfigrealtime.googleapis.com/v1/projects/", matcher.matches() ? matcher.group(1) : null, "/namespaces/", str, ":streamFetchInvalidations");
    }

    /* renamed from: ᵝᵵᵺ, reason: contains not printable characters */
    public final synchronized void m9679() {
        this.f16940.getClass();
        m9675(Math.max(0L, this.f16930.m8972().f15649.getTime() - new Date(System.currentTimeMillis()).getTime()));
    }

    /* JADX WARN: Removed duplicated region for block: B:14:0x0019  */
    /* renamed from: ᵥᵯˎ, reason: contains not printable characters */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public final synchronized boolean m9680() {
        /*
            r1 = this;
            monitor-enter(r1)
            java.util.LinkedHashSet r0 = r1.f16937     // Catch: java.lang.Throwable -> L17
            boolean r0 = r0.isEmpty()     // Catch: java.lang.Throwable -> L17
            if (r0 != 0) goto L19
            boolean r0 = r1.f16941     // Catch: java.lang.Throwable -> L17
            if (r0 != 0) goto L19
            boolean r0 = r1.f16945     // Catch: java.lang.Throwable -> L17
            if (r0 != 0) goto L19
            boolean r0 = r1.f16929     // Catch: java.lang.Throwable -> L17
            if (r0 != 0) goto L19
            r0 = 1
            goto L1a
        L17:
            r0 = move-exception
            goto L1c
        L19:
            r0 = 0
        L1a:
            monitor-exit(r1)
            return r0
        L1c:
            monitor-exit(r1)     // Catch: java.lang.Throwable -> L17
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: defpackage.C4556.m9680():boolean");
    }

    /* renamed from: ᵪʩ, reason: contains not printable characters */
    public final void m9681(Date date) {
        C4093 c4093 = this.f16930;
        int i = c4093.m8972().f15648 + 1;
        c4093.m8970(i, new Date(date.getTime() + (TimeUnit.MINUTES.toMillis(f16928[(i < 8 ? i : 8) - 1]) / 2) + this.f16938.nextInt((int) r2)));
    }

    /* renamed from: ᵯٴᵦ, reason: contains not printable characters */
    public final synchronized C0975 m9682(HttpURLConnection httpURLConnection) {
        C0975 c0975;
        C3583 c3583 = new C3583(this);
        C4995 c4995 = this.f16944;
        C0382 c0382 = this.f16943;
        LinkedHashSet linkedHashSet = this.f16937;
        ScheduledExecutorService scheduledExecutorService = this.f16932;
        C4093 c4093 = this.f16930;
        c0975 = new C0975();
        c0975.f4344 = httpURLConnection;
        c0975.f4341 = c4995;
        c0975.f4346 = c0382;
        c0975.f4343 = linkedHashSet;
        c0975.f4338 = c3583;
        c0975.f4340 = scheduledExecutorService;
        c0975.f4339 = new Random();
        c0975.f4342 = c4093;
        c0975.f4345 = ˊᵻ.ᵾᵬ;
        return c0975;
    }

    /* renamed from: ᵴᵳ, reason: contains not printable characters */
    public final void m9683(InputStream inputStream, InputStream inputStream2) {
        HttpURLConnection httpURLConnection = this.f16933;
        if (httpURLConnection != null && !this.f16929) {
            httpURLConnection.disconnect();
        }
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException unused) {
            }
        }
        if (inputStream2 != null) {
            try {
                inputStream2.close();
            } catch (IOException unused2) {
            }
        }
    }

    /* renamed from: ᵶᵷᵕ, reason: contains not printable characters */
    public final synchronized void m9684(boolean z) {
        this.f16941 = z;
    }

    /* renamed from: ᵸˑ, reason: contains not printable characters */
    public final void m9685(boolean z) {
        HttpURLConnection httpURLConnection;
        synchronized (this.f16936) {
            try {
                this.f16929 = z;
                if (Build.VERSION.SDK_INT >= 26 && z && (httpURLConnection = this.f16933) != null) {
                    httpURLConnection.disconnect();
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    /* renamed from: ᵹᵝᵶ, reason: contains not printable characters */
    public final synchronized void m9686() {
        this.f16934 = 8;
    }
}
