package com.google.android.gms.internal.firebase_ml; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import com.google.android.gms.auth.api.AuthProxy; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.common.internal.GmsLogger; import com.google.android.gms.common.util.AndroidUtilsLight; import com.google.android.gms.common.util.Hex; import com.google.android.gms.internal.firebase_ml.zzej; import com.google.firebase.FirebaseApp; import com.google.firebase.ml.common.FirebaseMLException; import java.io.IOException; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; /* loaded from: classes2.dex */ public final class zzpq implements zznm { private final GoogleApiClient zzawp; private String zzawt; private final boolean zzawu; private final zzip zzawv; private final String zzsq; private static final zzfo zzawr = new zzga(); private static final zzge zzaws = zzgn.zzgx(); private static final GmsLogger zzaoz = new GmsLogger("ImageAnnotatorTask", ""); private static final Map zzax = new HashMap(); public static zzpq zza(FirebaseApp firebaseApp, boolean z, GoogleApiClient googleApiClient) { zzpq zzpqVar; synchronized (zzpq.class) { Map map = zzax; zzpqVar = map.get(firebaseApp); if (zzpqVar == null) { zzpqVar = new zzpq(firebaseApp, z, googleApiClient); map.put(firebaseApp, zzpqVar); } } return zzpqVar; } @Override // com.google.android.gms.internal.firebase_ml.zznm public final zznw zzlm() { return null; } private zzpq(FirebaseApp firebaseApp, boolean z, GoogleApiClient googleApiClient) { this.zzawu = z; if (z) { this.zzawp = googleApiClient; this.zzawv = new zzpr(this); } else { this.zzawp = null; this.zzawv = new zzps(this, zzi(firebaseApp), firebaseApp); } this.zzsq = String.format("FirebaseML_%s", firebaseApp.getName()); } private static String zzi(FirebaseApp firebaseApp) { Bundle bundle; String apiKey = firebaseApp.getOptions().getApiKey(); Context applicationContext = firebaseApp.getApplicationContext(); try { ApplicationInfo applicationInfo = applicationContext.getPackageManager().getApplicationInfo(applicationContext.getPackageName(), 128); if (applicationInfo == null || (bundle = (Bundle) ApplicationInfo.class.getField("metaData").get(applicationInfo)) == null) { return apiKey; } String string = bundle.getString("com.firebase.ml.cloud.ApiKeyForDebug"); return !TextUtils.isEmpty(string) ? string : apiKey; } catch (PackageManager.NameNotFoundException e) { GmsLogger gmsLogger = zzaoz; String valueOf = String.valueOf(applicationContext.getPackageName()); gmsLogger.e("ImageAnnotatorTask", valueOf.length() != 0 ? "No such package: ".concat(valueOf) : new String("No such package: "), e); return apiKey; } } /* JADX INFO: Access modifiers changed from: private */ public static String zza(Context context, String str) { try { byte[] packageCertificateHashBytes = AndroidUtilsLight.getPackageCertificateHashBytes(context, str); if (packageCertificateHashBytes == null) { GmsLogger gmsLogger = zzaoz; String valueOf = String.valueOf(str); gmsLogger.e("ImageAnnotatorTask", valueOf.length() != 0 ? "Could not get fingerprint hash: ".concat(valueOf) : new String("Could not get fingerprint hash: ")); return null; } return Hex.bytesToStringUppercase(packageCertificateHashBytes, false); } catch (PackageManager.NameNotFoundException e) { GmsLogger gmsLogger2 = zzaoz; String valueOf2 = String.valueOf(str); gmsLogger2.e("ImageAnnotatorTask", valueOf2.length() != 0 ? "No such package: ".concat(valueOf2) : new String("No such package: "), e); return null; } } private final String getSpatulaHeader() throws FirebaseMLException { if (!this.zzawp.isConnected()) { this.zzawp.blockingConnect(3L, TimeUnit.SECONDS); } try { return AuthProxy.ProxyApi.getSpatulaHeader(this.zzawp).await(3L, TimeUnit.SECONDS).getSpatulaHeader(); } catch (SecurityException unused) { return null; } } @Override // com.google.android.gms.internal.firebase_ml.zznm public final zzir zza(zzpn zzpnVar) throws FirebaseMLException { zzis zzc = new zzis().zzc(Collections.singletonList(new zziq().zzb(zzpnVar.features).zza(new zzjd().zze(zzpnVar.zzawk)).zza(zzpnVar.imageContext))); int i = 13; try { zzik zzhl = ((zzil) new zzil(zzawr, zzaws, new zzpt(this)).zzn(this.zzsq)).zza(this.zzawv).zzhl(); if (this.zzawu) { String spatulaHeader = getSpatulaHeader(); this.zzawt = spatulaHeader; if (TextUtils.isEmpty(spatulaHeader)) { zzaoz.e("ImageAnnotatorTask", "Failed to contact Google Play services for certificate fingerprint matching. Please ensure you have latest Google Play services installed"); throw new FirebaseMLException("Failed to contact Google Play services for certificate fingerprint matching. Please ensure you have latest Google Play services installed", 14); } } List zzhp = new zzim(zzhl).zza(zzc).zzep().zzhp(); if (zzhp != null && zzhp.size() > 0) { return zzhp.get(0); } throw new FirebaseMLException("Empty response from cloud vision api.", 13); } catch (zzek e) { GmsLogger gmsLogger = zzaoz; String valueOf = String.valueOf(e.zzei()); StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 44); sb.append("batchAnnotateImages call failed with error: "); sb.append(valueOf); gmsLogger.e("ImageAnnotatorTask", sb.toString()); if (this.zzawu) { Log.d("ImageAnnotatorTask", "If you are developing / testing on a simulator, either register your development app on Firebase console or turn off enforceCertFingerprintMatch()"); } String message = e.getMessage(); if (e.getStatusCode() == 400) { i = 14; } else if (e.zzei() != null && e.zzei().zzef() != null) { Iterator it = e.zzei().zzef().iterator(); int i2 = 13; while (it.hasNext()) { String reason = it.next().getReason(); if (reason != null) { if (reason.equals("rateLimitExceeded") || reason.equals("dailyLimitExceeded") || reason.equals("userRateLimitExceeded")) { i2 = 8; } else { if (!reason.equals("accessNotConfigured")) { if (reason.equals("forbidden") || reason.equals("inactiveBillingState")) { message = String.format("If you haven't set up billing, please go to Firebase console to set up billing: %s. If you are specifying a debug Api Key override and turned on Api Key restrictions, make sure the restrictions are set up correctly", "https://firebase.corp.google.com/u/0/project/_/overview?purchaseBillingPlan=true"); } } i2 = 7; } } if (i2 != 13) { break; } } i = i2; } throw new FirebaseMLException(message, i); } catch (IOException e2) { zzaoz.e("ImageAnnotatorTask", "batchAnnotateImages call failed with exception: ", e2); throw new FirebaseMLException("Cloud Vision batchAnnotateImages call failure", 13, e2); } } }