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

import android.content.ContentResolver;
import androidx.annotation.GuardedBy;
import androidx.annotation.Nullable;
import java.util.HashMap;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

/* JADX INFO: loaded from: classes2.dex */
public final class r4 implements p4 {

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

    @Nullable
    @GuardedBy("this")
    private HashMap<String, String> zzb = null;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final HashMap f20387b = new HashMap(16, 1.0f);

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final HashMap f20388c = new HashMap(16, 1.0f);

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final HashMap f20389d = new HashMap(16, 1.0f);

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final HashMap f20390e = new HashMap(16, 1.0f);

    @Nullable
    @GuardedBy("this")
    private Object zzg = null;

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final String[] f20392g = new String[0];

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final s4 f20393h = new s4();

    @Override // com.google.android.gms.internal.measurement.p4
    @Nullable
    public final String zza(@Nullable ContentResolver contentResolver, String str, @Nullable String str2) {
        if (contentResolver == null) {
            throw new IllegalStateException("ContentResolver needed with GservicesDelegateSupplier.init()");
        }
        synchronized (this) {
            try {
                if (this.zzb == null) {
                    this.f20386a.set(false);
                    this.zzb = new HashMap<>(16, 1.0f);
                    this.zzg = new Object();
                    contentResolver.registerContentObserver(n4.f20332a, true, new v0.a(this, 3));
                } else if (this.f20386a.getAndSet(false)) {
                    this.zzb.clear();
                    this.f20387b.clear();
                    this.f20388c.clear();
                    this.f20389d.clear();
                    this.f20390e.clear();
                    this.zzg = new Object();
                    this.f20391f = false;
                }
                Object obj = this.zzg;
                if (this.zzb.containsKey(str)) {
                    String str3 = this.zzb.get(str);
                    return str3 != null ? str3 : null;
                }
                for (String str4 : this.f20392g) {
                    if (str.startsWith(str4)) {
                        if (!this.f20391f) {
                            try {
                                HashMap<String, String> mapA = this.f20393h.a(contentResolver, this.f20392g, new c6());
                                if (!mapA.isEmpty()) {
                                    Set<String> setKeySet = mapA.keySet();
                                    setKeySet.removeAll(this.f20387b.keySet());
                                    setKeySet.removeAll(this.f20388c.keySet());
                                    setKeySet.removeAll(this.f20389d.keySet());
                                    setKeySet.removeAll(this.f20390e.keySet());
                                }
                                if (!mapA.isEmpty()) {
                                    if (this.zzb.isEmpty()) {
                                        this.zzb = mapA;
                                    } else {
                                        this.zzb.putAll(mapA);
                                    }
                                }
                                this.f20391f = true;
                            } catch (zzgt unused) {
                            }
                            if (this.zzb.containsKey(str)) {
                                String str5 = this.zzb.get(str);
                                return str5 != null ? str5 : null;
                            }
                        }
                        return null;
                    }
                }
                try {
                    String strZza = this.f20393h.zza(contentResolver, str);
                    if (strZza != null && strZza.equals(null)) {
                        strZza = null;
                    }
                    synchronized (this) {
                        try {
                            if (obj == this.zzg) {
                                this.zzb.put(str, strZza);
                            }
                        } finally {
                        }
                    }
                    if (strZza != null) {
                        return strZza;
                    }
                    return null;
                } catch (zzgt unused2) {
                    return null;
                }
            } finally {
            }
        }
    }
}
