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

import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.ProviderInfo;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Build;
import com.google.common.base.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

/* JADX INFO: loaded from: classes2.dex */
public abstract class d5 {

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static volatile w4 f20154h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public static final c6 f20155i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public static final AtomicInteger f20156j;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final i5 f20157a;

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

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public volatile Object f20161e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final boolean f20162f;

    static {
        new AtomicReference();
        f20155i = new c6(new c6());
        f20156j = new AtomicInteger();
    }

    public d5(i5 i5Var, String str, Object obj) {
        String str2 = i5Var.f20226a;
        if (str2 == null && i5Var.f20227b == null) {
            throw new IllegalArgumentException("Must pass a valid SharedPreferences file name or ContentProvider URI");
        }
        if (str2 != null && i5Var.f20227b != null) {
            throw new IllegalArgumentException("Must pass one of SharedPreferences file name or ContentProvider URI");
        }
        this.f20157a = i5Var;
        this.f20158b = str;
        this.f20159c = obj;
        this.f20162f = true;
    }

    /* JADX WARN: Removed duplicated region for block: B:20:0x0056  */
    /* JADX WARN: Removed duplicated region for block: B:23:0x0060  */
    /* JADX WARN: Removed duplicated region for block: B:24:0x0061  */
    /* JADX WARN: Removed duplicated region for block: B:46:0x00a7 A[Catch: all -> 0x0079, TryCatch #0 {all -> 0x0079, blocks: (B:8:0x001b, B:10:0x001f, B:12:0x0028, B:14:0x0036, B:16:0x004b, B:18:0x0052, B:25:0x0063, B:26:0x0072, B:21:0x0058, B:32:0x0080, B:34:0x008b, B:47:0x00a9, B:50:0x00b1, B:51:0x00b4, B:52:0x00b8, B:37:0x0092, B:46:0x00a7, B:40:0x0099, B:43:0x00a0, B:53:0x00bc), top: B:59:0x001b }] */
    /* JADX WARN: Removed duplicated region for block: B:49:0x00af  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object a() {
        Object objD;
        androidx.collection.s sVar;
        if (!this.f20162f) {
            c6 c6Var = f20155i;
            String str = this.f20158b;
            c6Var.getClass();
            com.google.common.base.h.f(str, "flagName must not be null");
        }
        int i10 = f20156j.get();
        if (this.f20160d < i10) {
            synchronized (this) {
                try {
                    if (this.f20160d < i10) {
                        w4 w4Var = f20154h;
                        Optional optionalA = Optional.a();
                        String str2 = null;
                        if (w4Var != null) {
                            optionalA = (Optional) w4Var.f20511b.get();
                            if (optionalA.c()) {
                                x4 x4Var = (x4) optionalA.b();
                                i5 i5Var = this.f20157a;
                                Uri uri = i5Var.f20227b;
                                String string = i5Var.f20226a;
                                String str3 = i5Var.f20229d;
                                String str4 = this.f20158b;
                                x4Var.getClass();
                                if (uri != null) {
                                    string = uri.toString();
                                } else if (string == null) {
                                    sVar = null;
                                    if (sVar != null) {
                                        if (str3 != null) {
                                            str4 = str3 + str4;
                                        }
                                        str2 = (String) sVar.get(str4);
                                    }
                                }
                                androidx.collection.s sVar2 = x4Var.f20533a;
                                if (sVar2 != null) {
                                    sVar = (androidx.collection.s) sVar2.get(string);
                                }
                                if (sVar != null) {
                                }
                            }
                        }
                        com.google.common.base.h.i(w4Var != null, "Must call PhenotypeFlagInitializer.maybeInit() first");
                        if (this.f20157a.f20231f) {
                            objD = b(w4Var);
                            if (objD == null && (objD = d(w4Var)) == null) {
                                objD = this.f20159c;
                            }
                            if (optionalA.c()) {
                                objD = str2 == null ? this.f20159c : c(str2);
                            }
                            this.f20161e = objD;
                            this.f20160d = i10;
                        } else {
                            objD = d(w4Var);
                            if (objD == null && (objD = b(w4Var)) == null) {
                            }
                            if (optionalA.c()) {
                            }
                            this.f20161e = objD;
                            this.f20160d = i10;
                        }
                    }
                } finally {
                }
            }
        }
        return this.f20161e;
    }

    public final Object b(w4 w4Var) {
        com.google.common.base.e eVar;
        z4 z4Var;
        String strJ;
        z4 z4Var2;
        i5 i5Var = this.f20157a;
        if (!i5Var.f20230e && ((eVar = i5Var.f20234i) == null || ((Boolean) eVar.apply(w4Var.f20510a)).booleanValue())) {
            Context context = w4Var.f20510a;
            synchronized (z4.class) {
                try {
                    if (z4.f20550c == null) {
                        if (g0.l.a(context, "com.google.android.providers.gsf.permission.READ_GSERVICES") == 0) {
                            z4Var2 = new z4();
                            z4Var2.f20551a = context;
                            z4Var2.f20552b = new a5(null);
                            context.getContentResolver().registerContentObserver(n4.f20332a, true, (ContentObserver) z4Var2.f20552b);
                        } else {
                            z4Var2 = new z4(0);
                        }
                        z4.f20550c = z4Var2;
                    }
                    z4Var = z4.f20550c;
                } catch (Throwable th) {
                    throw th;
                }
            }
            i5 i5Var2 = this.f20157a;
            if (i5Var2.f20230e) {
                strJ = null;
            } else {
                String str = i5Var2.f20228c;
                strJ = this.f20158b;
                if (str == null || !str.isEmpty()) {
                    strJ = com.google.android.gms.measurement.internal.u1.j(str, strJ);
                }
            }
            Object objZza = z4Var.zza(strJ);
            if (objZza != null) {
                return c(objZza);
            }
        }
        return null;
    }

    public abstract Object c(Object obj);

    /* JADX WARN: Can't wrap try/catch for region: R(10:19|(1:21)(7:22|(1:24)(1:25)|26|(0)|35|36|37)|30|64|31|32|(1:34)|35|36|37) */
    /* JADX WARN: Removed duplicated region for block: B:47:0x0107  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object d(w4 w4Var) {
        y4 y4VarZza;
        boolean zBooleanValue;
        i5 i5Var = this.f20157a;
        Uri uri = i5Var.f20227b;
        if (uri != null) {
            Context context = w4Var.f20510a;
            Optional optional = c5.f20140a;
            if ("com.google.android.gms.phenotype".equals(uri.getAuthority())) {
                if (c5.f20140a.c()) {
                    zBooleanValue = ((Boolean) c5.f20140a.b()).booleanValue();
                } else {
                    synchronized (c5.f20141b) {
                        try {
                            if (c5.f20140a.c()) {
                                zBooleanValue = ((Boolean) c5.f20140a.b()).booleanValue();
                            } else {
                                boolean z10 = false;
                                if (!"com.google.android.gms".equals(context.getPackageName())) {
                                    ProviderInfo providerInfoResolveContentProvider = context.getPackageManager().resolveContentProvider("com.google.android.gms.phenotype", Build.VERSION.SDK_INT < 29 ? 0 : 268435456);
                                    if (providerInfoResolveContentProvider != null && "com.google.android.gms".equals(providerInfoResolveContentProvider.packageName)) {
                                    }
                                    c5.f20140a = Optional.d(Boolean.valueOf(z10));
                                    zBooleanValue = ((Boolean) c5.f20140a.b()).booleanValue();
                                }
                                if ((context.getPackageManager().getApplicationInfo("com.google.android.gms", 0).flags & 129) != 0) {
                                    z10 = true;
                                }
                                c5.f20140a = Optional.d(Boolean.valueOf(z10));
                                zBooleanValue = ((Boolean) c5.f20140a.b()).booleanValue();
                            }
                        } finally {
                        }
                    }
                }
                if (!zBooleanValue) {
                    y4VarZza = null;
                } else if (this.f20157a.f20233h) {
                    ContentResolver contentResolver = w4Var.f20510a.getContentResolver();
                    Context context2 = w4Var.f20510a;
                    String lastPathSegment = this.f20157a.f20227b.getLastPathSegment();
                    androidx.collection.f fVar = e5.f20175a;
                    if (lastPathSegment.contains("#")) {
                        throw new IllegalArgumentException("The passed in package cannot already have a subpackage: ".concat(lastPathSegment));
                    }
                    y4VarZza = v4.a(contentResolver, e5.a(lastPathSegment + "#" + context2.getPackageName()), new g5());
                } else {
                    y4VarZza = v4.a(w4Var.f20510a.getContentResolver(), this.f20157a.f20227b, new g5());
                }
            }
        } else {
            y4VarZza = k5.zza(w4Var.f20510a, i5Var.f20226a, new g5());
        }
        if (y4VarZza != null) {
            String str = this.f20157a.f20229d;
            String strJ = this.f20158b;
            if (str == null || !str.isEmpty()) {
                strJ = com.google.android.gms.measurement.internal.u1.j(str, strJ);
            }
            Object objZza = y4VarZza.zza(strJ);
            if (objZza != null) {
                return c(objZza);
            }
        }
        return null;
    }
}
