package com.google.android.gms.internal.pal;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.ConditionVariable;
import androidx.annotation.Nullable;
import org.json.JSONException;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes2.dex */
public final class k5 implements SharedPreferences.OnSharedPreferenceChangeListener {

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final Object f21017a = new Object();

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final ConditionVariable f21018b = new ConditionVariable();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public volatile boolean f21019c = false;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public volatile boolean f21020d = false;

    @Nullable
    private SharedPreferences zze = null;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public Bundle f21021e = new Bundle();

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public JSONObject f21023g = new JSONObject();

    public final Object b(g5 g5Var) {
        Object obj;
        if (!this.f21018b.block(5000L)) {
            synchronized (this.f21017a) {
                try {
                    if (!this.f21020d) {
                        throw new IllegalStateException("Flags.initialize() was not called!");
                    }
                } finally {
                }
            }
        }
        if (!this.f21019c || this.zze == null) {
            synchronized (this.f21017a) {
                if (this.f21019c && this.zze != null) {
                }
                return g5Var.f20948c;
            }
        }
        int i10 = g5Var.f20946a;
        if (i10 != 2) {
            int i11 = 1;
            if (i10 != 1 || !this.f21023g.has(g5Var.f20947b)) {
                return q9.l(new m5(this, g5Var, i11));
            }
            JSONObject jSONObject = this.f21023g;
            int i12 = g5Var.f20933d;
            String str = g5Var.f20947b;
            Object obj2 = g5Var.f20948c;
            switch (i12) {
                case 0:
                    return Boolean.valueOf(jSONObject.optBoolean(str, ((Boolean) obj2).booleanValue()));
                case 1:
                    return Integer.valueOf(jSONObject.optInt(str, ((Integer) obj2).intValue()));
                case 2:
                    return Long.valueOf(jSONObject.optLong(str, ((Long) obj2).longValue()));
                case 3:
                    return Float.valueOf((float) jSONObject.optDouble(str, ((Float) obj2).floatValue()));
                default:
                    return jSONObject.optString(str, (String) obj2);
            }
        }
        Bundle bundle = this.f21021e;
        if (bundle == null) {
            return g5Var.f20948c;
        }
        int i13 = g5Var.f20933d;
        Object obj3 = g5Var.f20948c;
        String str2 = g5Var.f20947b;
        switch (i13) {
            case 0:
                if (bundle.containsKey("com.google.android.gms.ads.flag.".concat(str2))) {
                    return Boolean.valueOf(bundle.getBoolean("com.google.android.gms.ads.flag.".concat(str2)));
                }
                obj = (Boolean) obj3;
                break;
            case 1:
                if (bundle.containsKey("com.google.android.gms.ads.flag.".concat(str2))) {
                    return Integer.valueOf(bundle.getInt("com.google.android.gms.ads.flag.".concat(str2)));
                }
                obj = (Integer) obj3;
                break;
            case 2:
                if (bundle.containsKey("com.google.android.gms.ads.flag.".concat(str2))) {
                    return Long.valueOf(bundle.getLong("com.google.android.gms.ads.flag.".concat(str2)));
                }
                obj = (Long) obj3;
                break;
            case 3:
                if (bundle.containsKey("com.google.android.gms.ads.flag.".concat(str2))) {
                    return Float.valueOf(bundle.getFloat("com.google.android.gms.ads.flag.".concat(str2)));
                }
                obj = (Float) obj3;
                break;
            default:
                return bundle.containsKey("com.google.android.gms.ads.flag.".concat(str2)) ? bundle.getString("com.google.android.gms.ads.flag.".concat(str2)) : (String) obj3;
        }
        return obj;
    }

    public final Object c(h5 h5Var) {
        SharedPreferences sharedPreferences = this.zze;
        g5 g5Var = (g5) h5Var;
        int i10 = g5Var.f20933d;
        String str = g5Var.f20947b;
        Object obj = g5Var.f20948c;
        switch (i10) {
            case 0:
                return Boolean.valueOf(sharedPreferences.getBoolean(str, ((Boolean) obj).booleanValue()));
            case 1:
                return Integer.valueOf(sharedPreferences.getInt(str, ((Integer) obj).intValue()));
            case 2:
                return Long.valueOf(sharedPreferences.getLong(str, ((Long) obj).longValue()));
            case 3:
                return Float.valueOf(sharedPreferences.getFloat(str, ((Float) obj).floatValue()));
            default:
                return sharedPreferences.getString(str, (String) obj);
        }
    }

    public final /* synthetic */ String d() {
        return this.zze.getString("flag_configuration", "{}");
    }

    public final void e(Context context) {
        if (this.f21019c) {
            return;
        }
        synchronized (this.f21017a) {
            try {
                if (this.f21019c) {
                    return;
                }
                if (!this.f21020d) {
                    this.f21020d = true;
                }
                Context applicationContext = context.getApplicationContext() == null ? context : context.getApplicationContext();
                this.f21022f = applicationContext;
                try {
                    this.f21021e = j8.e.a(applicationContext).a(128, this.f21022f.getPackageName()).metaData;
                } catch (PackageManager.NameNotFoundException | NullPointerException unused) {
                }
                try {
                    Context remoteContext = e8.g.getRemoteContext(context);
                    if (remoteContext != null || (remoteContext = context.getApplicationContext()) != null) {
                        context = remoteContext;
                    }
                    c cVar = f5.f20904d.f20906b;
                    SharedPreferences sharedPreferences = context.getSharedPreferences("google_ads_flags", 0);
                    this.zze = sharedPreferences;
                    if (sharedPreferences != null) {
                        sharedPreferences.registerOnSharedPreferenceChangeListener(this);
                    }
                    o5.f21113a.set(new j5(this));
                    f();
                    this.f21019c = true;
                    this.f21020d = false;
                    this.f21018b.open();
                } catch (Throwable th) {
                    this.f21020d = false;
                    this.f21018b.open();
                    throw th;
                }
            } catch (Throwable th2) {
                throw th2;
            }
        }
    }

    public final void f() {
        if (this.zze == null) {
            return;
        }
        try {
            this.f21023g = new JSONObject((String) q9.l(new o3(this, 2)));
        } catch (JSONException unused) {
        }
    }

    @Override // android.content.SharedPreferences.OnSharedPreferenceChangeListener
    public final void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String str) {
        if ("flag_configuration".equals(str)) {
            f();
        }
    }
}
