package p7;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Looper;
import android.security.NetworkSecurityPolicy;
import android.text.TextUtils;
import androidx.annotation.Nullable;
import com.google.android.gms.internal.ads.ao;
import com.google.android.gms.internal.ads.b30;
import com.google.android.gms.internal.ads.gp;
import com.google.android.gms.internal.ads.hp;
import com.google.android.gms.internal.ads.p20;
import com.google.android.gms.internal.ads.wj;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes2.dex */
public final class o0 implements n0 {

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public com.google.common.util.concurrent.w f31729d;

    @Nullable
    private SharedPreferences zzf;

    @Nullable
    private SharedPreferences.Editor zzg;

    @Nullable
    private String zzi;

    @Nullable
    private String zzj;

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final ArrayList f31728c = new ArrayList();

    @Nullable
    private wj zze = null;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public boolean f31730e = true;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public boolean f31731f = true;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public String f31732g = "-1";

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public int f31733h = -1;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public p20 f31734i = new p20("", 0);

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public long f31735j = 0;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public long f31736k = 0;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public int f31737l = -1;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public int f31738m = 0;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public Set f31739n = Collections.emptySet();

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

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public boolean f31741p = true;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public boolean f31742q = true;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public String f31743r = null;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public String f31744s = "";

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

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public String f31746u = "";

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public String f31747v = "{}";

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public int f31748w = -1;

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    public int f31749x = -1;

    /* JADX INFO: renamed from: y, reason: collision with root package name */
    public long f31750y = 0;

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

    public final void A(String str) {
        k();
        synchronized (this.f31726a) {
            try {
                if (TextUtils.equals(this.f31743r, str)) {
                    return;
                }
                this.f31743r = str;
                SharedPreferences.Editor editor = this.zzg;
                if (editor != null) {
                    editor.putString("display_cutout", str);
                    this.zzg.apply();
                }
                l();
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void B(boolean z10) {
        k();
        synchronized (this.f31726a) {
            try {
                if (z10 == this.f31731f) {
                    return;
                }
                this.f31731f = z10;
                SharedPreferences.Editor editor = this.zzg;
                if (editor != null) {
                    editor.putBoolean("gad_idless", z10);
                    this.zzg.apply();
                }
                l();
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final boolean C() {
        boolean z10;
        if (!((Boolean) n7.y.f31093e.f31096c.b(ao.U0)).booleanValue()) {
            return false;
        }
        k();
        synchronized (this.f31726a) {
            z10 = this.f31731f;
        }
        return z10;
    }

    public final void D(boolean z10) {
        k();
        synchronized (this.f31726a) {
            try {
                long jCurrentTimeMillis = System.currentTimeMillis() + ((Long) n7.y.f31093e.f31096c.b(ao.Ib)).longValue();
                SharedPreferences.Editor editor = this.zzg;
                if (editor != null) {
                    editor.putBoolean("is_topics_ad_personalization_allowed", z10);
                    this.zzg.putLong("topics_consent_expiry_time_ms", jCurrentTimeMillis);
                    this.zzg.apply();
                }
                l();
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final boolean E() {
        k();
        synchronized (this.f31726a) {
            try {
                SharedPreferences sharedPreferences = this.zzf;
                boolean z10 = false;
                if (sharedPreferences == null) {
                    return false;
                }
                if (sharedPreferences.getLong("topics_consent_expiry_time_ms", 0L) < System.currentTimeMillis()) {
                    return false;
                }
                if (this.zzf.getBoolean("is_topics_ad_personalization_allowed", false) && !this.f31731f) {
                    z10 = true;
                }
                return z10;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void a(String str) {
        k();
        synchronized (this.f31726a) {
            try {
                this.f31732g = str;
                if (this.zzg != null) {
                    if (str.equals("-1")) {
                        this.zzg.remove("IABTCF_TCString");
                    } else {
                        this.zzg.putString("IABTCF_TCString", str);
                    }
                    this.zzg.apply();
                }
                l();
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void b(int i10) {
        k();
        synchronized (this.f31726a) {
            try {
                this.f31733h = i10;
                SharedPreferences.Editor editor = this.zzg;
                if (editor != null) {
                    if (i10 == -1) {
                        editor.remove("gad_has_consent_for_cookies");
                    } else {
                        editor.putInt("gad_has_consent_for_cookies", i10);
                    }
                    this.zzg.apply();
                }
                l();
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void c(int i10) {
        k();
        synchronized (this.f31726a) {
            try {
                if (this.f31749x == i10) {
                    return;
                }
                this.f31749x = i10;
                SharedPreferences.Editor editor = this.zzg;
                if (editor != null) {
                    editor.putInt("sd_app_measure_npa", i10);
                    this.zzg.apply();
                }
                l();
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void d(long j10) {
        k();
        synchronized (this.f31726a) {
            try {
                if (this.f31750y == j10) {
                    return;
                }
                this.f31750y = j10;
                SharedPreferences.Editor editor = this.zzg;
                if (editor != null) {
                    editor.putLong("sd_app_measure_npa_ts", j10);
                    this.zzg.apply();
                }
                l();
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void e(String str) {
        if (((Boolean) n7.y.f31093e.f31096c.b(ao.f8856fa)).booleanValue()) {
            k();
            synchronized (this.f31726a) {
                try {
                    if (this.f31744s.equals(str)) {
                        return;
                    }
                    this.f31744s = str;
                    SharedPreferences.Editor editor = this.zzg;
                    if (editor != null) {
                        editor.putString("inspector_info", str);
                        this.zzg.apply();
                    }
                    l();
                } catch (Throwable th) {
                    throw th;
                }
            }
        }
    }

    public final void f(boolean z10) {
        if (((Boolean) n7.y.f31093e.f31096c.b(ao.f9104ua)).booleanValue()) {
            k();
            synchronized (this.f31726a) {
                try {
                    if (this.f31745t == z10) {
                        return;
                    }
                    this.f31745t = z10;
                    SharedPreferences.Editor editor = this.zzg;
                    if (editor != null) {
                        editor.putBoolean("linked_device", z10);
                        this.zzg.apply();
                    }
                    l();
                } catch (Throwable th) {
                    throw th;
                }
            }
        }
    }

    public final void g(String str) {
        if (((Boolean) n7.y.f31093e.f31096c.b(ao.f9104ua)).booleanValue()) {
            k();
            synchronized (this.f31726a) {
                try {
                    if (this.f31746u.equals(str)) {
                        return;
                    }
                    this.f31746u = str;
                    SharedPreferences.Editor editor = this.zzg;
                    if (editor != null) {
                        editor.putString("linked_ad_unit", str);
                        this.zzg.apply();
                    }
                    l();
                } catch (Throwable th) {
                    throw th;
                }
            }
        }
    }

    public final void h(String str) {
        if (((Boolean) n7.y.f31093e.f31096c.b(ao.Ia)).booleanValue()) {
            k();
            synchronized (this.f31726a) {
                try {
                    if (this.f31747v.equals(str)) {
                        return;
                    }
                    this.f31747v = str;
                    SharedPreferences.Editor editor = this.zzg;
                    if (editor != null) {
                        editor.putString("inspector_ui_storage", str);
                        this.zzg.apply();
                    }
                    l();
                } catch (Throwable th) {
                    throw th;
                }
            }
        }
    }

    public final void i() {
        k();
        synchronized (this.f31726a) {
            try {
                if (this.f31751z) {
                    return;
                }
                this.f31751z = true;
                SharedPreferences.Editor editor = this.zzg;
                if (editor != null) {
                    editor.putBoolean("is_install_referrer_reported", true);
                    this.zzg.apply();
                }
                l();
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void j(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("admob", 0);
        SharedPreferences.Editor editorEdit = sharedPreferences.edit();
        try {
            synchronized (this.f31726a) {
                try {
                    this.zzf = sharedPreferences;
                    this.zzg = editorEdit;
                    NetworkSecurityPolicy.getInstance().isCleartextTrafficPermitted();
                    this.f31730e = this.zzf.getBoolean("use_https", this.f31730e);
                    this.f31741p = this.zzf.getBoolean("content_url_opted_out", this.f31741p);
                    this.zzi = this.zzf.getString("content_url_hashes", this.zzi);
                    this.f31731f = this.zzf.getBoolean("gad_idless", this.f31731f);
                    this.f31742q = this.zzf.getBoolean("content_vertical_opted_out", this.f31742q);
                    this.zzj = this.zzf.getString("content_vertical_hashes", this.zzj);
                    this.f31738m = this.zzf.getInt("version_code", this.f31738m);
                    if (((Boolean) hp.f12223g.j()).booleanValue() && n7.y.f31093e.f31096c.f18902i) {
                        this.f31734i = new p20("", 0L);
                    } else {
                        this.f31734i = new p20(this.zzf.getString("app_settings_json", this.f31734i.f15240e), this.zzf.getLong("app_settings_last_update_ms", this.f31734i.f15241f));
                    }
                    this.f31735j = this.zzf.getLong("app_last_background_time_ms", this.f31735j);
                    this.f31737l = this.zzf.getInt("request_in_session_count", this.f31737l);
                    this.f31736k = this.zzf.getLong("first_ad_req_time_ms", this.f31736k);
                    this.f31739n = this.zzf.getStringSet("never_pool_slots", this.f31739n);
                    this.f31743r = this.zzf.getString("display_cutout", this.f31743r);
                    this.f31748w = this.zzf.getInt("app_measurement_npa", this.f31748w);
                    this.f31749x = this.zzf.getInt("sd_app_measure_npa", this.f31749x);
                    this.f31750y = this.zzf.getLong("sd_app_measure_npa_ts", this.f31750y);
                    this.f31744s = this.zzf.getString("inspector_info", this.f31744s);
                    this.f31745t = this.zzf.getBoolean("linked_device", this.f31745t);
                    this.f31746u = this.zzf.getString("linked_ad_unit", this.f31746u);
                    this.f31747v = this.zzf.getString("inspector_ui_storage", this.f31747v);
                    this.f31732g = this.zzf.getString("IABTCF_TCString", this.f31732g);
                    this.f31733h = this.zzf.getInt("gad_has_consent_for_cookies", this.f31733h);
                    this.f31751z = this.zzf.getBoolean("is_install_referrer_reported", this.f31751z);
                    try {
                        this.f31740o = new JSONObject(this.zzf.getString("native_advanced_settings", "{}"));
                    } catch (JSONException unused) {
                    }
                    l();
                } catch (Throwable th) {
                    throw th;
                }
            }
        } catch (Throwable th2) {
            m7.n.C.f30469h.d("AdSharedPreferenceManagerImpl.initializeOnBackgroundThread", th2);
            l0.h();
        }
    }

    public final void k() {
        com.google.common.util.concurrent.w wVar = this.f31729d;
        if (wVar == null || wVar.isDone()) {
            return;
        }
        try {
            this.f31729d.get(1L, TimeUnit.SECONDS);
        } catch (InterruptedException unused) {
            Thread.currentThread().interrupt();
        } catch (CancellationException | ExecutionException | TimeoutException unused2) {
        }
    }

    public final void l() {
        b30.f9359a.execute(new e.s0(this, 26));
    }

    public final void m(Context context) {
        synchronized (this.f31726a) {
            try {
                if (this.zzf != null) {
                    return;
                }
                this.f31729d = b30.f9359a.e(new n0.a(this, context));
                this.f31727b = true;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void n(boolean z10) {
        k();
        synchronized (this.f31726a) {
            try {
                if (this.f31741p == z10) {
                    return;
                }
                this.f31741p = z10;
                SharedPreferences.Editor editor = this.zzg;
                if (editor != null) {
                    editor.putBoolean("content_url_opted_out", z10);
                    this.zzg.apply();
                }
                l();
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final boolean o() {
        boolean z10;
        k();
        synchronized (this.f31726a) {
            z10 = this.f31741p;
        }
        return z10;
    }

    public final void p(boolean z10) {
        k();
        synchronized (this.f31726a) {
            try {
                if (this.f31742q == z10) {
                    return;
                }
                this.f31742q = z10;
                SharedPreferences.Editor editor = this.zzg;
                if (editor != null) {
                    editor.putBoolean("content_vertical_opted_out", z10);
                    this.zzg.apply();
                }
                l();
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final boolean q() {
        boolean z10;
        k();
        synchronized (this.f31726a) {
            z10 = this.f31742q;
        }
        return z10;
    }

    public final void r(int i10) {
        k();
        synchronized (this.f31726a) {
            try {
                if (this.f31738m == i10) {
                    return;
                }
                this.f31738m = i10;
                SharedPreferences.Editor editor = this.zzg;
                if (editor != null) {
                    editor.putInt("version_code", i10);
                    this.zzg.apply();
                }
                l();
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void s(String str) {
        k();
        synchronized (this.f31726a) {
            try {
                m7.n.C.f30472k.getClass();
                long jCurrentTimeMillis = System.currentTimeMillis();
                if (str != null && !str.equals(this.f31734i.f15240e)) {
                    this.f31734i = new p20(str, jCurrentTimeMillis);
                    SharedPreferences.Editor editor = this.zzg;
                    if (editor != null) {
                        editor.putString("app_settings_json", str);
                        this.zzg.putLong("app_settings_last_update_ms", jCurrentTimeMillis);
                        this.zzg.apply();
                    }
                    l();
                    Iterator it = this.f31728c.iterator();
                    while (it.hasNext()) {
                        ((Runnable) it.next()).run();
                    }
                    return;
                }
                this.f31734i.f15241f = jCurrentTimeMillis;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final p20 t() {
        p20 p20Var;
        k();
        synchronized (this.f31726a) {
            try {
                if (((Boolean) n7.y.f31093e.f31096c.b(ao.Sc)).booleanValue() && this.f31734i.a()) {
                    Iterator it = this.f31728c.iterator();
                    while (it.hasNext()) {
                        ((Runnable) it.next()).run();
                    }
                }
                p20Var = this.f31734i;
            } catch (Throwable th) {
                throw th;
            }
        }
        return p20Var;
    }

    public final void u(long j10) {
        k();
        synchronized (this.f31726a) {
            try {
                if (this.f31735j == j10) {
                    return;
                }
                this.f31735j = j10;
                SharedPreferences.Editor editor = this.zzg;
                if (editor != null) {
                    editor.putLong("app_last_background_time_ms", j10);
                    this.zzg.apply();
                }
                l();
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void v(int i10) {
        k();
        synchronized (this.f31726a) {
            try {
                if (this.f31737l == i10) {
                    return;
                }
                this.f31737l = i10;
                SharedPreferences.Editor editor = this.zzg;
                if (editor != null) {
                    editor.putInt("request_in_session_count", i10);
                    this.zzg.apply();
                }
                l();
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void w(long j10) {
        k();
        synchronized (this.f31726a) {
            try {
                if (this.f31736k == j10) {
                    return;
                }
                this.f31736k = j10;
                SharedPreferences.Editor editor = this.zzg;
                if (editor != null) {
                    editor.putLong("first_ad_req_time_ms", j10);
                    this.zzg.apply();
                }
                l();
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:25:0x0048, code lost:
    
        r3 = new org.json.JSONObject();
        r3.put("template_id", r9);
        r3.put("uses_media_view", r10);
        m7.n.C.f30472k.getClass();
        r3.put("timestamp_ms", java.lang.System.currentTimeMillis());
        r1.put(r2, r3);
        r7.f31740o.put(r8, r1);
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void x(String str, String str2, boolean z10) {
        k();
        synchronized (this.f31726a) {
            try {
                JSONArray jSONArrayOptJSONArray = this.f31740o.optJSONArray(str);
                if (jSONArrayOptJSONArray == null) {
                    jSONArrayOptJSONArray = new JSONArray();
                }
                int length = jSONArrayOptJSONArray.length();
                int i10 = 0;
                while (true) {
                    if (i10 < jSONArrayOptJSONArray.length()) {
                        JSONObject jSONObjectOptJSONObject = jSONArrayOptJSONArray.optJSONObject(i10);
                        if (jSONObjectOptJSONObject == null) {
                            return;
                        }
                        if (!str2.equals(jSONObjectOptJSONObject.optString("template_id"))) {
                            i10++;
                        } else if (z10 && jSONObjectOptJSONObject.optBoolean("uses_media_view", false)) {
                            return;
                        } else {
                            length = i10;
                        }
                    }
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        SharedPreferences.Editor editor = this.zzg;
        if (editor != null) {
            editor.putString("native_advanced_settings", this.f31740o.toString());
            this.zzg.apply();
        }
        l();
    }

    public final void y() {
        k();
        synchronized (this.f31726a) {
            try {
                this.f31740o = new JSONObject();
                SharedPreferences.Editor editor = this.zzg;
                if (editor != null) {
                    editor.remove("native_advanced_settings");
                    this.zzg.apply();
                }
                l();
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final String z() {
        String str;
        k();
        synchronized (this.f31726a) {
            str = this.f31743r;
        }
        return str;
    }

    @Override // p7.n0
    @Nullable
    public final String zzB() {
        k();
        return this.f31732g;
    }

    @Nullable
    public final wj zzR() {
        if (!this.f31727b) {
            return null;
        }
        if ((o() && q()) || !((Boolean) gp.f11821b.j()).booleanValue()) {
            return null;
        }
        synchronized (this.f31726a) {
            try {
                if (Looper.getMainLooper() == null) {
                    return null;
                }
                if (this.zze == null) {
                    this.zze = new wj();
                }
                wj wjVar = this.zze;
                synchronized (wjVar.f18084c) {
                    try {
                        if (wjVar.f18082a) {
                            q7.j.a("Content hash thread already started, quitting...");
                        } else {
                            wjVar.f18082a = true;
                            wjVar.start();
                        }
                    } finally {
                    }
                }
                q7.j.c("start fetching content...");
                return this.zze;
            } catch (Throwable th) {
                throw th;
            }
        }
    }
}
