package k4;

import D0.y;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Looper;
import android.util.Log;
import g4.C0988c;
import java.util.Locale;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final Pattern f13934g = Pattern.compile("[^\\p{Alnum}]");

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final String f13935h = Pattern.quote("/");

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final B3.f f13936a;

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

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final E4.e f13939d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final y f13940e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public C1100c f13941f;

    public w(Context context, String str, E4.e eVar, y yVar) {
        if (context == null) {
            throw new IllegalArgumentException("appContext must not be null");
        }
        if (str == null) {
            throw new IllegalArgumentException("appIdentifier must not be null");
        }
        this.f13937b = context;
        this.f13938c = str;
        this.f13939d = eVar;
        this.f13940e = yVar;
        this.f13936a = new B3.f();
    }

    public final synchronized String a(SharedPreferences sharedPreferences, String str) {
        String lowerCase;
        lowerCase = f13934g.matcher(UUID.randomUUID().toString()).replaceAll("").toLowerCase(Locale.US);
        String str2 = "Created new Crashlytics installation ID: " + lowerCase + " for FID: " + str;
        if (Log.isLoggable("FirebaseCrashlytics", 2)) {
            Log.v("FirebaseCrashlytics", str2, null);
        }
        sharedPreferences.edit().putString("crashlytics.installation.id", lowerCase).putString("firebase.installation.id", str).apply();
        return lowerCase;
    }

    public final v b(boolean z8) {
        String str;
        String str2 = null;
        if (Looper.getMainLooper().isCurrentThread()) {
            String str3 = "Must not be called on a main thread, was called on " + Thread.currentThread().getName() + '.';
            if (Log.isLoggable("FirebaseCrashlytics", 3)) {
                Log.d("FirebaseCrashlytics", str3, null);
            }
        }
        E4.e eVar = this.f13939d;
        if (z8) {
            try {
                g3.o oVarF = ((E4.d) eVar).f();
                TimeUnit timeUnit = TimeUnit.MILLISECONDS;
                str = ((E4.a) W2.g.e(oVarF, 10000L)).f2181a;
            } catch (Exception e8) {
                Log.w("FirebaseCrashlytics", "Error getting Firebase authentication token.", e8);
                str = null;
            }
        } else {
            str = null;
        }
        try {
            g3.o oVarD = ((E4.d) eVar).d();
            TimeUnit timeUnit2 = TimeUnit.MILLISECONDS;
            str2 = (String) W2.g.e(oVarD, 10000L);
        } catch (Exception e9) {
            Log.w("FirebaseCrashlytics", "Error getting Firebase installation id.", e9);
        }
        return new v(str2, str);
    }

    public final synchronized C1100c c() {
        String str;
        C1100c c1100c = this.f13941f;
        if (c1100c != null && (c1100c.f13849b != null || !this.f13940e.a())) {
            return this.f13941f;
        }
        C0988c c0988c = C0988c.f12956a;
        c0988c.f("Determining Crashlytics installation ID...");
        SharedPreferences sharedPreferences = this.f13937b.getSharedPreferences("com.google.firebase.crashlytics", 0);
        String string = sharedPreferences.getString("firebase.installation.id", null);
        c0988c.f("Cached Firebase Installation ID: " + string);
        if (this.f13940e.a()) {
            v vVarB = b(false);
            c0988c.f("Fetched Firebase Installation ID: " + vVarB.f13932a);
            if (vVarB.f13932a == null) {
                if (string == null) {
                    str = "SYN_" + UUID.randomUUID().toString();
                } else {
                    str = string;
                }
                vVarB = new v(str, null);
            }
            if (Objects.equals(vVarB.f13932a, string)) {
                this.f13941f = new C1100c(sharedPreferences.getString("crashlytics.installation.id", null), vVarB.f13932a, vVarB.f13933b);
            } else {
                this.f13941f = new C1100c(a(sharedPreferences, vVarB.f13932a), vVarB.f13932a, vVarB.f13933b);
            }
        } else if (string == null || !string.startsWith("SYN_")) {
            this.f13941f = new C1100c(a(sharedPreferences, "SYN_" + UUID.randomUUID().toString()), null, null);
        } else {
            this.f13941f = new C1100c(sharedPreferences.getString("crashlytics.installation.id", null), null, null);
        }
        c0988c.f("Install IDs: " + this.f13941f);
        return this.f13941f;
    }

    public final String d() {
        String str;
        B3.f fVar = this.f13936a;
        Context context = this.f13937b;
        synchronized (fVar) {
            try {
                if (fVar.f975b == null) {
                    String installerPackageName = context.getPackageManager().getInstallerPackageName(context.getPackageName());
                    if (installerPackageName == null) {
                        installerPackageName = "";
                    }
                    fVar.f975b = installerPackageName;
                }
                str = "".equals(fVar.f975b) ? null : fVar.f975b;
            } finally {
            }
        }
        return str;
    }
}
