package e8;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.content.pm.SigningInfo;
import android.os.Build;
import android.os.RemoteException;
import android.os.StrictMode;
import android.util.Log;
import com.google.android.gms.common.internal.r0;
import com.google.android.gms.dynamite.DynamiteModule$LoadingException;
import com.google.android.gms.internal.common.zzah;
import com.google.android.gms.measurement.internal.a0;
import java.util.Arrays;

/* JADX INFO: loaded from: classes2.dex */
public final class h {

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static h f25528c;

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

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

    public h(Context context) {
        this.f25529a = context.getApplicationContext();
    }

    public static h b(Context context) {
        com.google.android.gms.common.internal.w.checkNotNull(context);
        synchronized (h.class) {
            try {
                if (f25528c == null) {
                    s.a(context);
                    f25528c = new h(context);
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        return f25528c;
    }

    /* JADX WARN: Code restructure failed: missing block: B:53:0x00cf, code lost:
    
        r5 = r9;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static final boolean d(PackageInfo packageInfo, boolean z10) {
        zzah zzahVarK;
        if (packageInfo == null) {
            return false;
        }
        if (z10 && ("com.android.vending".equals(packageInfo.packageName) || "com.google.android.gms".equals(packageInfo.packageName))) {
            ApplicationInfo applicationInfo = packageInfo.applicationInfo;
            z10 = (applicationInfo == null || (applicationInfo.flags & 129) == 0) ? false : true;
        }
        try {
            zzah zzahVar = z10 ? r.f25547c : r.f25546b;
            int i10 = Build.VERSION.SDK_INT;
            if (i10 < 28) {
                Signature[] signatureArr = packageInfo.signatures;
                byte[] byteArray = null;
                if (signatureArr != null && signatureArr.length == 1) {
                    byteArray = signatureArr[0].toByteArray();
                }
                if (byteArray != null) {
                    com.google.android.gms.internal.common.c cVar = zzah.f20090b;
                    Object[] objArr = {byteArray};
                    com.bumptech.glide.f.x(1, objArr);
                    zzahVarK = zzah.l(1, objArr);
                } else {
                    zzahVarK = zzah.k();
                }
            } else {
                if (i10 < 28) {
                    throw new IllegalStateException();
                }
                SigningInfo signingInfo = packageInfo.signingInfo;
                if (signingInfo == null || signingInfo.hasMultipleSigners() || signingInfo.getSigningCertificateHistory() == null) {
                    zzahVarK = zzah.k();
                } else {
                    com.google.android.gms.internal.common.c cVar2 = zzah.f20090b;
                    com.google.android.gms.internal.common.b bVar = new com.google.android.gms.internal.common.b();
                    bVar.f20077a = new Object[4];
                    bVar.f20078b = 0;
                    for (Signature signature : signingInfo.getSigningCertificateHistory()) {
                        bVar.a(signature.toByteArray());
                    }
                    bVar.f20079c = true;
                    zzahVarK = zzah.l(bVar.f20078b, bVar.f20077a);
                }
            }
            if (zzahVarK.isEmpty()) {
                throw new IllegalArgumentException("Unable to obtain package certificate history.");
            }
            zzah zzahVarI = zzahVarK.i();
            int size = zzahVarI.size();
            int i11 = 0;
            while (i11 < size) {
                byte[] bArr = (byte[]) zzahVarI.get(i11);
                com.google.android.gms.internal.common.c cVarListIterator = zzahVar.listIterator(0);
                do {
                    int i12 = i11 + 1;
                    if (cVarListIterator.hasNext()) {
                    }
                } while (!Arrays.equals(bArr, (byte[]) cVarListIterator.next()));
                return true;
            }
            return false;
        } catch (IllegalArgumentException unused) {
            return (z10 ? e(packageInfo, r.f25545a) : e(packageInfo, r.f25545a[0])) != null;
        }
    }

    public static n e(PackageInfo packageInfo, n... nVarArr) {
        Signature[] signatureArr = packageInfo.signatures;
        if (signatureArr == null || signatureArr.length != 1) {
            return null;
        }
        o oVar = new o(packageInfo.signatures[0].toByteArray());
        for (int i10 = 0; i10 < nVarArr.length; i10++) {
            if (nVarArr[i10].equals(oVar)) {
                return nVarArr[i10];
            }
        }
        return null;
    }

    public final d5.c a() {
        if (((d5.c) this.f25530b) == null) {
            synchronized (this) {
                try {
                    if (((d5.c) this.f25530b) == null) {
                        this.f25530b = ((d5.a) this.f25529a).build();
                    }
                    if (((d5.c) this.f25530b) == null) {
                        this.f25530b = new a0(9);
                    }
                } finally {
                }
            }
        }
        return (d5.c) this.f25530b;
    }

    /* JADX WARN: Removed duplicated region for block: B:52:0x00de  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean c(int i10) {
        y yVarB;
        int length;
        y yVarB2;
        ApplicationInfo applicationInfo;
        String[] packagesForUid = ((Context) this.f25529a).getPackageManager().getPackagesForUid(i10);
        if (packagesForUid == null || (length = packagesForUid.length) == 0) {
            yVarB = y.b("no pkgs");
        } else {
            yVarB = null;
            int i11 = 0;
            while (true) {
                if (i11 >= length) {
                    com.google.android.gms.common.internal.w.checkNotNull(yVarB);
                    break;
                }
                String str = packagesForUid[i11];
                if (str == null) {
                    yVarB = y.b("null pkg");
                } else if (str.equals((String) this.f25530b)) {
                    yVarB = y.f25570c;
                } else {
                    m mVar = s.f25548a;
                    StrictMode.ThreadPolicy threadPolicyAllowThreadDiskReads = StrictMode.allowThreadDiskReads();
                    try {
                        s.b();
                    } catch (RemoteException | DynamiteModule$LoadingException unused) {
                    } finally {
                    }
                    if (((r0) s.f25550c).zzg()) {
                        w wVar = new w();
                        wVar.f25568a = str;
                        wVar.a(g.a((Context) this.f25529a));
                        wVar.b();
                        yVarB2 = s.c(wVar.c());
                    } else {
                        try {
                            PackageInfo packageInfo = ((Context) this.f25529a).getPackageManager().getPackageInfo(str, Build.VERSION.SDK_INT >= 28 ? 134217792 : 64);
                            boolean zA = g.a((Context) this.f25529a);
                            if (packageInfo == null) {
                                yVarB2 = y.b("null pkg");
                            } else {
                                Signature[] signatureArr = packageInfo.signatures;
                                if (signatureArr == null || signatureArr.length != 1) {
                                    yVarB2 = y.b("single cert required");
                                } else {
                                    o oVar = new o(packageInfo.signatures[0].toByteArray());
                                    String str2 = packageInfo.packageName;
                                    threadPolicyAllowThreadDiskReads = StrictMode.allowThreadDiskReads();
                                    try {
                                        y yVarD = s.d(str2, oVar, zA, false);
                                        StrictMode.setThreadPolicy(threadPolicyAllowThreadDiskReads);
                                        if (!yVarD.f25571a || (applicationInfo = packageInfo.applicationInfo) == null || (applicationInfo.flags & 2) == 0) {
                                            yVarB2 = yVarD;
                                        } else {
                                            threadPolicyAllowThreadDiskReads = StrictMode.allowThreadDiskReads();
                                            try {
                                                y yVarD2 = s.d(str2, oVar, false, true);
                                                StrictMode.setThreadPolicy(threadPolicyAllowThreadDiskReads);
                                                if (yVarD2.f25571a) {
                                                    yVarB2 = y.b("debuggable release cert app rejected");
                                                }
                                            } finally {
                                            }
                                        }
                                    } finally {
                                    }
                                }
                            }
                        } catch (PackageManager.NameNotFoundException e10) {
                            yVarB = y.c("no pkg ".concat(str), e10);
                        }
                    }
                    if (yVarB2.f25571a) {
                        this.f25530b = str;
                    }
                    yVarB = yVarB2;
                }
                if (yVarB.f25571a) {
                    break;
                }
                i11++;
            }
        }
        if (!yVarB.f25571a && Log.isLoggable("GoogleCertificatesRslt", 3)) {
            if (yVarB.f25572b != null) {
                yVarB.a();
            } else {
                yVarB.a();
            }
        }
        return yVarB.f25571a;
    }

    public h(d5.a aVar) {
        this.f25529a = aVar;
    }
}
