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

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

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

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

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

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

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

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

    @Nullable
    private SharedPreferences zze = null;

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

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public boolean f18901h = false;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public boolean f18902i = false;

    /* JADX WARN: Removed duplicated region for block: B:72:0x012d A[Catch: all -> 0x000e, DONT_GENERATE, TRY_ENTER, TryCatch #3 {all -> 0x000e, blocks: (B:7:0x0008, B:9:0x000c, B:13:0x0011, B:15:0x0016, B:16:0x0018, B:18:0x002a, B:19:0x002e, B:20:0x0030, B:47:0x00a8, B:48:0x00af, B:57:0x00e0, B:58:0x00e7, B:72:0x012d, B:73:0x0134, B:86:0x0175, B:87:0x017c, B:89:0x017e, B:90:0x0185, B:22:0x0045, B:37:0x0063, B:39:0x006d, B:40:0x0077, B:42:0x007d, B:44:0x008d, B:46:0x00a4, B:50:0x00b1, B:52:0x00b5, B:54:0x00c5, B:56:0x00dc, B:60:0x00e9, B:70:0x0129, B:75:0x0136, B:77:0x0144, B:79:0x0152, B:80:0x0159, B:82:0x0167, B:84:0x016b, B:85:0x016e, B:63:0x00fa, B:65:0x0108, B:67:0x0110, B:68:0x011c, B:26:0x004c, B:32:0x0059), top: B:100:0x0008, inners: #0 }] */
    /* JADX WARN: Removed duplicated region for block: B:75:0x0136 A[Catch: all -> 0x0053, TRY_ENTER, TryCatch #0 {all -> 0x0053, blocks: (B:22:0x0045, B:37:0x0063, B:39:0x006d, B:40:0x0077, B:42:0x007d, B:44:0x008d, B:46:0x00a4, B:50:0x00b1, B:52:0x00b5, B:54:0x00c5, B:56:0x00dc, B:60:0x00e9, B:70:0x0129, B:75:0x0136, B:77:0x0144, B:79:0x0152, B:80:0x0159, B:82:0x0167, B:84:0x016b, B:85:0x016e, B:63:0x00fa, B:65:0x0108, B:67:0x0110, B:68:0x011c, B:26:0x004c, B:32:0x0059), top: B:95:0x0045, outer: #3 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void a(Context context) {
        Context applicationContext;
        SharedPreferences sharedPreferences;
        SharedPreferences sharedPreferences2;
        if (this.f18896c) {
            return;
        }
        synchronized (this.f18894a) {
            try {
                if (this.f18896c) {
                    return;
                }
                if (!this.f18897d) {
                    this.f18897d = true;
                }
                this.f18901h = TextUtils.equals(context.getPackageName(), "com.google.android.gms");
                if (context.getApplicationContext() != null) {
                    context = context.getApplicationContext();
                }
                this.f18899f = context;
                try {
                    this.f18898e = j8.e.a(context).a(128, this.f18899f.getPackageName()).metaData;
                } catch (PackageManager.NameNotFoundException | NullPointerException unused) {
                }
                try {
                    Context context2 = this.f18899f;
                    SharedPreferences sharedPreferencesZzb = null;
                    if (context2 == null) {
                        context2 = null;
                    } else {
                        try {
                            applicationContext = context2.createPackageContext("com.google.android.gms", 0);
                        } catch (PackageManager.NameNotFoundException unused2) {
                            applicationContext = null;
                        }
                        if (applicationContext != null || (applicationContext = context2.getApplicationContext()) != null) {
                            context2 = applicationContext;
                        }
                    }
                    if (context2 != null) {
                        vn vnVar = n7.y.f31093e.f31095b;
                        sharedPreferencesZzb = vn.zzb(context2);
                    }
                    if (sharedPreferencesZzb != null) {
                        cq.f10030a.set(new wn(this, sharedPreferencesZzb));
                    }
                    if (!this.f18901h) {
                        bp bpVar = hp.f12220d;
                        if (((Long) bpVar.j()).longValue() > 0 && ni.G(this.f18899f, "crash_without_write") >= ((Long) bpVar.j()).longValue()) {
                            this.f18902i = true;
                            this.f18896c = true;
                            return;
                        }
                    }
                    if (!this.f18901h) {
                        bp bpVar2 = hp.f12222f;
                        if (((Long) bpVar2.j()).longValue() > 0 && ni.G(this.f18899f, "init_without_write") >= ((Long) bpVar2.j()).longValue()) {
                            this.f18902i = true;
                            this.f18896c = true;
                            return;
                        }
                    }
                    Context context3 = this.f18899f;
                    if (!((Boolean) op.f15144k.j()).booleanValue()) {
                        if (((Boolean) op.f15145l.j()).booleanValue() && (sharedPreferences = context3.getSharedPreferences("admob", 0)) != null) {
                            try {
                                if (new JSONObject((String) ni.b(new rn(sharedPreferences, 2))).optBoolean("local_flags_enabled")) {
                                }
                            } catch (JSONException unused3) {
                            }
                        }
                        if (context2 != null) {
                            return;
                        }
                        n7.y yVar = n7.y.f31093e;
                        vn vnVar2 = yVar.f31095b;
                        this.zze = vn.zzb(context2);
                        if (!this.f18901h && ((Boolean) op.f15138e.j()).booleanValue()) {
                            yVar.f31097d.a(this.f18899f);
                        }
                        if (!((Boolean) op.f15136c.j()).booleanValue() && (sharedPreferences2 = this.zze) != null) {
                            sharedPreferences2.registerOnSharedPreferenceChangeListener(this);
                        }
                        e(this.zze);
                        this.f18896c = true;
                        return;
                    }
                    context2 = this.f18899f;
                    if (context2 != null) {
                    }
                } finally {
                    this.f18897d = false;
                    this.f18895b.open();
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final Object b(final un unVar) {
        if (!this.f18895b.block(5000L)) {
            synchronized (this.f18894a) {
                try {
                    if (!this.f18897d) {
                        throw new IllegalStateException("Flags.initialize() was not called!");
                    }
                } finally {
                }
            }
        }
        if (!this.f18896c || this.zze == null || this.f18902i) {
            synchronized (this.f18894a) {
                if (this.f18896c && this.zze != null && !this.f18902i) {
                }
                return unVar.b();
            }
        }
        int i10 = unVar.f17335a;
        if (i10 != 2) {
            return (i10 == 1 && this.f18900g.has(unVar.f17336b)) ? unVar.a(this.f18900g) : ni.b(new uq1() { // from class: com.google.android.gms.internal.ads.xn
                @Override // com.google.android.gms.internal.ads.uq1
                public final /* synthetic */ Object zza() {
                    return this.f18483a.d(unVar);
                }
            });
        }
        Bundle bundle = this.f18898e;
        if (bundle == null) {
            return unVar.b();
        }
        tn tnVar = (tn) unVar;
        int i11 = tnVar.f16950e;
        String str = tnVar.f17336b;
        switch (i11) {
            case 0:
                return bundle.containsKey("com.google.android.gms.ads.flag.".concat(str)) ? Boolean.valueOf(bundle.getBoolean("com.google.android.gms.ads.flag.".concat(str))) : (Boolean) tnVar.b();
            case 1:
                return bundle.containsKey("com.google.android.gms.ads.flag.".concat(str)) ? Integer.valueOf(bundle.getInt("com.google.android.gms.ads.flag.".concat(str))) : (Integer) tnVar.b();
            case 2:
                return bundle.containsKey("com.google.android.gms.ads.flag.".concat(str)) ? Long.valueOf(bundle.getLong("com.google.android.gms.ads.flag.".concat(str))) : (Long) tnVar.b();
            case 3:
                return bundle.containsKey("com.google.android.gms.ads.flag.".concat(str)) ? Float.valueOf(bundle.getFloat("com.google.android.gms.ads.flag.".concat(str))) : (Float) tnVar.b();
            default:
                return bundle.containsKey("com.google.android.gms.ads.flag.".concat(str)) ? bundle.getString("com.google.android.gms.ads.flag.".concat(str)) : (String) tnVar.b();
        }
    }

    public final Object c(tn tnVar) {
        return (this.f18896c || this.f18897d) ? b(tnVar) : tnVar.b();
    }

    public final Object d(un unVar) {
        SharedPreferences sharedPreferences = this.zze;
        tn tnVar = (tn) unVar;
        int i10 = tnVar.f16950e;
        String str = tnVar.f17336b;
        switch (i10) {
            case 0:
                return Boolean.valueOf(sharedPreferences.getBoolean(str, ((Boolean) tnVar.b()).booleanValue()));
            case 1:
                return Integer.valueOf(sharedPreferences.getInt(str, ((Integer) tnVar.b()).intValue()));
            case 2:
                return Long.valueOf(sharedPreferences.getLong(str, ((Long) tnVar.b()).longValue()));
            case 3:
                return Float.valueOf(sharedPreferences.getFloat(str, ((Float) tnVar.b()).floatValue()));
            default:
                return sharedPreferences.getString(str, (String) tnVar.b());
        }
    }

    public final void e(SharedPreferences sharedPreferences) {
        if (sharedPreferences != null) {
            try {
                this.f18900g = new JSONObject((String) ni.b(new rn(sharedPreferences, 1)));
            } catch (JSONException unused) {
            }
        }
    }

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