package com.google.android.gms.internal.firebase_ml; import android.app.Application; import com.google.android.gms.common.api.internal.BackgroundDetector; import com.google.android.gms.common.internal.GmsLogger; import com.google.android.gms.common.internal.Preconditions; import com.google.firebase.FirebaseApp; import com.google.firebase.ml.common.FirebaseMLException; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; /* loaded from: classes2.dex */ public final class zznx { private static final GmsLogger zzape = new GmsLogger("ModelResourceManager", ""); private static zznx zzaqc; private final zznn zzapx = zznn.zzln(); private final AtomicLong zzapy; private final Set zzapz; private final Set zzaqa; private final ConcurrentHashMap zzaqb; public static zznx zzb(FirebaseApp firebaseApp) { zznx zznxVar; synchronized (zznx.class) { if (zzaqc == null) { zzaqc = new zznx(firebaseApp); } zznxVar = zzaqc; } return zznxVar; } private zznx(FirebaseApp firebaseApp) { AtomicLong atomicLong = new AtomicLong(300000L); this.zzapy = atomicLong; this.zzapz = new HashSet(); this.zzaqa = new HashSet(); this.zzaqb = new ConcurrentHashMap<>(); if (firebaseApp.getApplicationContext() instanceof Application) { BackgroundDetector.initialize((Application) firebaseApp.getApplicationContext()); } else { zzape.e("ModelResourceManager", "No valid Application available and auto-manage cannot work"); } BackgroundDetector.getInstance().addListener(new zzny(this)); if (BackgroundDetector.getInstance().readCurrentStateIfPossible(true)) { atomicLong.set(2000L); } } public final void zza(zznw zznwVar) { synchronized (this) { Preconditions.checkNotNull(zznwVar, "Model source can not be null"); GmsLogger gmsLogger = zzape; gmsLogger.d("ModelResourceManager", "Add auto-managed model resource"); if (this.zzapz.contains(zznwVar)) { gmsLogger.i("ModelResourceManager", "The model resource is already registered."); } else { this.zzapz.add(zznwVar); zzb(zznwVar); } } } private final void zzb(zznw zznwVar) { synchronized (this) { if (zznwVar == null) { return; } this.zzapx.zza(new zznz(this, zznwVar, "OPERATION_LOAD")); if (this.zzapz.contains(zznwVar)) { zzc(zznwVar); } } } /* JADX INFO: Access modifiers changed from: private */ public final void zzlq() { synchronized (this) { Iterator it = this.zzapz.iterator(); while (it.hasNext()) { zzc(it.next()); } } } private final void zzc(zznw zznwVar) { synchronized (this) { zznz zze = zze(zznwVar); this.zzapx.zzb(zze); long j = this.zzapy.get(); GmsLogger gmsLogger = zzape; StringBuilder sb = new StringBuilder(62); sb.append("Rescheduling modelResource release after: "); sb.append(j); gmsLogger.v("ModelResourceManager", sb.toString()); this.zzapx.zza(zze, j); } } public final void zzd(zznw zznwVar) { synchronized (this) { if (zznwVar == null) { return; } zznz zze = zze(zznwVar); this.zzapx.zzb(zze); this.zzapx.zza(zze, 0L); } } private final zznz zze(zznw zznwVar) { this.zzaqb.putIfAbsent(zznwVar, new zznz(this, zznwVar, "OPERATION_RELEASE")); return this.zzaqb.get(zznwVar); } /* JADX INFO: Access modifiers changed from: package-private */ public final void zzf(zznw zznwVar) throws FirebaseMLException { if (this.zzaqa.contains(zznwVar)) { return; } try { zznwVar.zzlp(); this.zzaqa.add(zznwVar); } catch (RuntimeException e) { throw new FirebaseMLException("The load task failed", 13, e); } } }