package p138.p176.p291.p296.p297.p310;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import java.io.Closeable;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicReference;
import org.json.JSONObject;
import p138.p139.p143.p144.C2803;
import p138.p176.p177.p242.p246.p251.C4303;
import p138.p176.p177.p242.p270.AbstractC5062;
import p138.p176.p177.p242.p270.C5063;
import p138.p176.p177.p242.p270.InterfaceC5061;
import p138.p176.p291.p296.p297.C5608;
import p138.p176.p291.p296.p297.p299.C5627;
import p138.p176.p291.p296.p297.p299.C5679;
import p138.p176.p291.p296.p297.p299.C5692;
import p138.p176.p291.p296.p297.p299.C5694;
import p138.p176.p291.p296.p297.p303.C5819;
import p138.p176.p291.p296.p297.p303.C5821;
import p138.p176.p291.p296.p297.p304.C5829;
import p138.p176.p291.p296.p297.p310.p311.C5864;
import p138.p176.p291.p296.p297.p310.p311.C5865;
import p138.p176.p291.p296.p297.p310.p311.C5866;
import p138.p176.p291.p296.p297.p310.p311.C5868;
import p138.p176.p291.p296.p297.p310.p311.C5869;
import p138.p176.p291.p296.p297.p310.p311.InterfaceC5867;
import p138.p176.p291.p296.p297.p310.p312.C5872;
import p138.p176.p291.p296.p297.p310.p312.InterfaceC5873;

/* JADX INFO: renamed from: ދ.ހ.ށ.ބ.ށ.ޏ.ށ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes.dex */
public class C5857 implements InterfaceC5859 {

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final Context f16582;

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public final C5869 f16583;

    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    public final C5860 f16584;

    /* JADX INFO: renamed from: ށ, reason: contains not printable characters */
    public final C5692 f16585;

    /* JADX INFO: renamed from: ނ, reason: contains not printable characters */
    public final C5854 f16586;

    /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
    public final InterfaceC5873 f16587;

    /* JADX INFO: renamed from: ބ, reason: contains not printable characters */
    public final C5679 f16588;

    /* JADX INFO: renamed from: ޅ, reason: contains not printable characters */
    public final AtomicReference<InterfaceC5867> f16589 = new AtomicReference<>();

    /* JADX INFO: renamed from: ކ, reason: contains not printable characters */
    public final AtomicReference<C5063<C5864>> f16590 = new AtomicReference<>(new C5063());

    /* JADX INFO: renamed from: ދ.ހ.ށ.ބ.ށ.ޏ.ށ$֏, reason: contains not printable characters */
    public class C5858 implements InterfaceC5061<Void, Void> {
        public C5858() {
        }

        @Override // p138.p176.p177.p242.p270.InterfaceC5061
        /* JADX INFO: renamed from: ֏ */
        public AbstractC5062<Void> mo8542(Void r10) throws Throwable {
            JSONObject jSONObjectM9213;
            FileWriter fileWriter;
            C5857 c5857 = C5857.this;
            InterfaceC5873 interfaceC5873 = c5857.f16587;
            C5869 c5869 = c5857.f16583;
            C5872 c5872 = (C5872) interfaceC5873;
            FileWriter fileWriter2 = null;
            if (c5872 == null) {
                throw null;
            }
            try {
                Map<String, String> mapM9212 = c5872.m9212(c5869);
                C5819 c5819M8984 = c5872.m8984(mapM9212);
                c5872.m9214(c5819M8984, c5869);
                c5872.f16628.m8972("Requesting settings from " + c5872.f16093);
                c5872.f16628.m8972("Settings query params were: " + mapM9212);
                C5821 c5821M9125 = c5819M8984.m9125();
                c5872.f16628.m8972("Settings request ID: " + c5821M9125.f16511.m10067("X-REQUEST-ID"));
                jSONObjectM9213 = c5872.m9213(c5821M9125);
            } catch (IOException e) {
                C5608 c5608 = c5872.f16628;
                if (c5608.m8974(6)) {
                    Log.e(c5608.f16067, "Settings request failed.", e);
                }
                jSONObjectM9213 = null;
            }
            if (jSONObjectM9213 != null) {
                C5868 c5868M9208 = C5857.this.f16584.m9208(jSONObjectM9213);
                C5854 c5854 = C5857.this.f16586;
                long j = c5868M9208.f16617;
                if (c5854 == null) {
                    throw null;
                }
                C5608.f16066.m8972("Writing settings to cache file...");
                try {
                    jSONObjectM9213.put("expires_at", j);
                    fileWriter = new FileWriter(new File(new C5829(c5854.f16577).m9146(), "com.crashlytics.settings.json"));
                    try {
                        fileWriter.write(jSONObjectM9213.toString());
                        fileWriter.flush();
                    } catch (Exception e2) {
                        e = e2;
                        try {
                            C5608 c56082 = C5608.f16066;
                            if (c56082.m8974(6)) {
                                Log.e(c56082.f16067, "Failed to cache settings", e);
                            }
                        } catch (Throwable th) {
                            th = th;
                            fileWriter2 = fileWriter;
                            fileWriter = fileWriter2;
                            C5627.m8998((Closeable) fileWriter, "Failed to close settings writer.");
                            throw th;
                        }
                    } catch (Throwable th2) {
                        th = th2;
                        C5627.m8998((Closeable) fileWriter, "Failed to close settings writer.");
                        throw th;
                    }
                } catch (Exception e3) {
                    e = e3;
                    fileWriter = null;
                } catch (Throwable th3) {
                    th = th3;
                    fileWriter = fileWriter2;
                    C5627.m8998((Closeable) fileWriter, "Failed to close settings writer.");
                    throw th;
                }
                C5627.m8998((Closeable) fileWriter, "Failed to close settings writer.");
                C5857.this.m9206(jSONObjectM9213, "Loaded settings: ");
                C5857 c58572 = C5857.this;
                String str = c58572.f16583.f16623;
                SharedPreferences.Editor editorEdit = C5627.m9008(c58572.f16582).edit();
                editorEdit.putString("existing_instance_identifier", str);
                editorEdit.apply();
                C5857.this.f16589.set(c5868M9208);
                C5857.this.f16590.get().m8557(c5868M9208.f16614);
                C5063<C5864> c5063 = new C5063<>();
                c5063.m8557(c5868M9208.f16614);
                C5857.this.f16590.set(c5063);
            }
            return C4303.m7054((Object) null);
        }
    }

    public C5857(Context context, C5869 c5869, C5692 c5692, C5860 c5860, C5854 c5854, InterfaceC5873 interfaceC5873, C5679 c5679) {
        this.f16582 = context;
        this.f16583 = c5869;
        this.f16585 = c5692;
        this.f16584 = c5860;
        this.f16586 = c5854;
        this.f16587 = interfaceC5873;
        this.f16588 = c5679;
        AtomicReference<InterfaceC5867> atomicReference = this.f16589;
        JSONObject jSONObject = new JSONObject();
        atomicReference.set(new C5868(C5855.m9201(c5692, 3600L, jSONObject), null, new C5866(jSONObject.optInt("max_custom_exception_events", 8), 4), new C5865(jSONObject.optBoolean("collect_reports", true)), 0, 3600));
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public AbstractC5062<C5864> m9203() {
        return this.f16590.get().f15214;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public AbstractC5062<Void> m9204(EnumC5856 enumC5856, Executor executor) throws Throwable {
        C5868 c5868M9205;
        if (!(!C5627.m9008(this.f16582).getString("existing_instance_identifier", "").equals(this.f16583.f16623)) && (c5868M9205 = m9205(enumC5856)) != null) {
            this.f16589.set(c5868M9205);
            this.f16590.get().m8557(c5868M9205.f16614);
            return C4303.m7054((Object) null);
        }
        C5868 c5868M92052 = m9205(EnumC5856.IGNORE_CACHE_EXPIRATION);
        if (c5868M92052 != null) {
            this.f16589.set(c5868M92052);
            this.f16590.get().m8557(c5868M92052.f16614);
        }
        C5679 c5679 = this.f16588;
        return C5694.m9070(c5679.f16258.f15214, c5679.m9053()).mo8550(executor, new C5858());
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final C5868 m9205(EnumC5856 enumC5856) throws Throwable {
        C5868 c5868 = null;
        try {
            if (EnumC5856.SKIP_CACHE_LOOKUP.equals(enumC5856)) {
                return null;
            }
            JSONObject jSONObjectM9200 = this.f16586.m9200();
            if (jSONObjectM9200 == null) {
                C5608.f16066.m8972("No cached settings data found.");
                return null;
            }
            C5868 c5868M9208 = this.f16584.m9208(jSONObjectM9200);
            if (c5868M9208 == null) {
                C5608 c5608 = C5608.f16066;
                if (!c5608.m8974(6)) {
                    return null;
                }
                Log.e(c5608.f16067, "Failed to parse cached settings data.", null);
                return null;
            }
            m9206(jSONObjectM9200, "Loaded cached settings: ");
            if (this.f16585 == null) {
                throw null;
            }
            long jCurrentTimeMillis = System.currentTimeMillis();
            if (!EnumC5856.IGNORE_CACHE_EXPIRATION.equals(enumC5856)) {
                if (c5868M9208.f16617 < jCurrentTimeMillis) {
                    C5608.f16066.m8972("Cached settings have expired.");
                    return null;
                }
            }
            try {
                C5608.f16066.m8972("Returning cached settings.");
                return c5868M9208;
            } catch (Exception e) {
                e = e;
                c5868 = c5868M9208;
                C5608 c56082 = C5608.f16066;
                if (!c56082.m8974(6)) {
                    return c5868;
                }
                Log.e(c56082.f16067, "Failed to get cached settings", e);
                return c5868;
            }
        } catch (Exception e2) {
            e = e2;
        }
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final void m9206(JSONObject jSONObject, String str) {
        C5608 c5608 = C5608.f16066;
        StringBuilder sbM4889 = C2803.m4889(str);
        sbM4889.append(jSONObject.toString());
        c5608.m8972(sbM4889.toString());
    }

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public InterfaceC5867 m9207() {
        return this.f16589.get();
    }
}
