package com.google.android.gms.games.internal; import android.app.Activity; import android.app.Application; import android.os.Looper; import androidx.annotation.GuardedBy; import androidx.annotation.MainThread; import androidx.annotation.Nullable; import androidx.annotation.VisibleForTesting; import androidx.lifecycle.LifecycleKt$$ExternalSyntheticBackportWithForwarding0; import com.google.android.gms.common.internal.Preconditions; import com.google.android.gms.tasks.TaskExecutors; import java.lang.ref.WeakReference; import java.util.Collections; import java.util.Iterator; import java.util.Set; import java.util.WeakHashMap; import java.util.concurrent.atomic.AtomicReference; /* loaded from: classes2.dex */ public final class zzg { public static final /* synthetic */ int zza = 0; private static final AtomicReference zzb = new AtomicReference(); private final Application zzc; @Nullable @GuardedBy("lock") private WeakReference zzg; private final Application.ActivityLifecycleCallbacks zzd = new zzf(this, null); private final Object zze = new Object(); @GuardedBy("lock") private final Set zzf = Collections.newSetFromMap(new WeakHashMap()); @GuardedBy("lock") private boolean zzh = false; @VisibleForTesting(otherwise = 2) public zzg(Application application) { this.zzc = application; } public static zzg zzb(Application application) { Preconditions.checkNotNull(application); AtomicReference atomicReference = zzb; zzg zzgVar = (zzg) atomicReference.get(); if (zzgVar != null) { return zzgVar; } zzg zzgVar2 = new zzg(application); while (!LifecycleKt$$ExternalSyntheticBackportWithForwarding0.m(atomicReference, null, zzgVar2) && atomicReference.get() == null) { } return (zzg) zzb.get(); } public static /* bridge */ /* synthetic */ void zzc(zzg zzgVar, Activity activity) { synchronized (zzgVar.zze) { try { WeakReference weakReference = zzgVar.zzg; if (weakReference == null) { return; } if (weakReference.get() == activity) { zzgVar.zzg = null; } } catch (Throwable th) { throw th; } } } public static /* bridge */ /* synthetic */ void zzd(zzg zzgVar, Activity activity) { Preconditions.checkNotNull(activity); synchronized (zzgVar.zze) { try { if (zzgVar.zza() == activity) { return; } zzgVar.zzg = new WeakReference(activity); Iterator it = zzgVar.zzf.iterator(); while (it.hasNext()) { ((zzd) it.next()).zza(activity); } } catch (Throwable th) { throw th; } } } /* JADX INFO: Access modifiers changed from: private */ @MainThread /* renamed from: zzh, reason: merged with bridge method [inline-methods] */ public final void zzf(zzd zzdVar) { Activity zza2 = zza(); if (zza2 == null) { return; } zzdVar.zza(zza2); } @Nullable public final Activity zza() { Activity activity; synchronized (this.zze) { WeakReference weakReference = this.zzg; activity = weakReference == null ? null : (Activity) weakReference.get(); } return activity; } public final void zze(final zzd zzdVar) { Preconditions.checkNotNull(zzdVar); synchronized (this.zze) { this.zzf.add(zzdVar); } if (Looper.myLooper() == Looper.getMainLooper()) { zzf(zzdVar); } else { TaskExecutors.MAIN_THREAD.execute(new Runnable() { // from class: com.google.android.gms.games.internal.zzc @Override // java.lang.Runnable public final void run() { zzg.this.zzf(zzdVar); } }); } } public final void zzg() { synchronized (this.zze) { try { if (!this.zzh) { this.zzc.registerActivityLifecycleCallbacks(this.zzd); this.zzh = true; } } catch (Throwable th) { throw th; } } } }