package L1;

import C3.E;
import C3.G;
import C3.W;
import M4.l;
import R0.v;
import android.content.Context;
import android.content.res.AssetManager;
import android.os.Build;
import android.util.Log;
import d5.AbstractC0844a;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Random;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.json.JSONException;
import org.json.JSONObject;
import u0.a0;
import x0.q;

/* JADX INFO: loaded from: classes.dex */
public final class b {

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

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

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public Object f3941d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public Object f3942e;

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

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public Object f3945h;

    public b(HttpURLConnection httpURLConnection, M4.i iVar, M4.d dVar, LinkedHashSet linkedHashSet, l lVar, ScheduledExecutorService scheduledExecutorService) {
        this.f3940c = httpURLConnection;
        this.f3941d = iVar;
        this.f3942e = dVar;
        this.f3939b = linkedHashSet;
        this.f3943f = lVar;
        this.f3944g = scheduledExecutorService;
        this.f3945h = new Random();
        this.f3938a = false;
    }

    public void a(long j8, int i) {
        if (i == 0) {
            new L4.g("Unable to fetch the latest version of the template.");
            e();
            return;
        }
        ((ScheduledExecutorService) this.f3944g).schedule(new M4.b(this, i, j8), ((Random) this.f3945h).nextInt(4), TimeUnit.SECONDS);
    }

    public void b(InputStream inputStream) throws IOException {
        JSONObject jSONObject;
        boolean zIsEmpty;
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
        String strL = "";
        while (true) {
            String line = bufferedReader.readLine();
            if (line == null) {
                break;
            }
            strL = AbstractC0844a.l(strL, line);
            if (line.contains("}")) {
                int iIndexOf = strL.indexOf(123);
                int iLastIndexOf = strL.lastIndexOf(125);
                strL = (iIndexOf < 0 || iLastIndexOf < 0 || iIndexOf >= iLastIndexOf) ? "" : strL.substring(iIndexOf, iLastIndexOf + 1);
                if (!strL.isEmpty()) {
                    try {
                        jSONObject = new JSONObject(strL);
                    } catch (JSONException e8) {
                        new L4.d("Unable to parse config update message.", e8.getCause());
                        e();
                        Log.e("FirebaseRemoteConfig", "Unable to parse latest config update message.", e8);
                    }
                    if (jSONObject.has("featureDisabled") && jSONObject.getBoolean("featureDisabled")) {
                        l lVar = (l) this.f3943f;
                        new L4.g("The server is temporarily unavailable. Try again in a few minutes.");
                        lVar.a();
                        break;
                    }
                    synchronized (this) {
                        zIsEmpty = ((LinkedHashSet) this.f3939b).isEmpty();
                    }
                    if (zIsEmpty) {
                        break;
                    }
                    if (jSONObject.has("latestTemplateVersionNumber")) {
                        long j8 = ((M4.i) this.f3941d).f4253g.f4285a.getLong("last_template_version", 0L);
                        long j9 = jSONObject.getLong("latestTemplateVersionNumber");
                        if (j9 > j8) {
                            a(j9, 3);
                        }
                    }
                    strL = "";
                } else {
                    continue;
                }
            }
        }
        bufferedReader.close();
    }

    public void c() {
        HttpURLConnection httpURLConnection = (HttpURLConnection) this.f3940c;
        if (httpURLConnection == null) {
            return;
        }
        InputStream inputStream = null;
        try {
            try {
                try {
                    inputStream = httpURLConnection.getInputStream();
                    b(inputStream);
                    if (inputStream != null) {
                        inputStream.close();
                    }
                } catch (IOException e8) {
                    Log.d("FirebaseRemoteConfig", "Exception thrown when closing connection stream. Retrying connection...", e8);
                }
            } catch (IOException e9) {
                if (!this.f3938a) {
                    Log.d("FirebaseRemoteConfig", "Real-time connection was closed due to an exception.", e9);
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            }
        } catch (Throwable th) {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e10) {
                    Log.d("FirebaseRemoteConfig", "Exception thrown when closing connection stream. Retrying connection...", e10);
                }
            }
            throw th;
        }
    }

    public FileInputStream d(AssetManager assetManager, String str) {
        try {
            return assetManager.openFd(str).createInputStream();
        } catch (FileNotFoundException e8) {
            String message = e8.getMessage();
            if (message == null || !message.contains("compressed")) {
                return null;
            }
            ((d) this.f3940c).i();
            return null;
        }
    }

    public synchronized void e() {
        Iterator it = ((LinkedHashSet) this.f3939b).iterator();
        while (it.hasNext()) {
            ((l) it.next()).a();
        }
    }

    public void f(int i, Serializable serializable) {
        ((Executor) this.f3939b).execute(new a(i, 0, this, serializable));
    }

    public b(AssetManager assetManager, Executor executor, d dVar, String str, File file) {
        this.f3938a = false;
        this.f3939b = executor;
        this.f3940c = dVar;
        this.f3944g = str;
        this.f3943f = file;
        int i = Build.VERSION.SDK_INT;
        byte[] bArr = null;
        if (i >= 24) {
            if (i >= 31) {
                bArr = e.f3957d;
            } else {
                switch (i) {
                    case 24:
                    case 25:
                        bArr = e.f3961h;
                        break;
                    case 26:
                        bArr = e.f3960g;
                        break;
                    case 27:
                        bArr = e.f3959f;
                        break;
                    case 28:
                    case 29:
                    case 30:
                        bArr = e.f3958e;
                        break;
                }
            }
        }
        this.f3941d = bArr;
    }

    public b(Context context, v vVar) {
        this.f3939b = context.getApplicationContext();
        this.f3940c = vVar;
        E e8 = G.f1197r;
        this.f3943f = W.f1225u;
        this.f3944g = a0.f17260a;
        this.f3945h = q.f18545a;
    }
}
