package com.google.firebase; import android.app.Application; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Handler; import android.os.Looper; import android.text.TextUtils; import com.google.android.gms.common.api.internal.BackgroundDetector; import com.google.android.gms.common.internal.Objects; import com.google.android.gms.common.internal.Preconditions; import com.google.android.gms.common.util.Base64Utils; import com.google.android.gms.common.util.PlatformVersion; import com.google.android.gms.common.util.ProcessUtils; import com.google.firebase.components.Component; import com.google.firebase.components.ComponentDiscovery; import com.google.firebase.components.ComponentDiscoveryService; import com.google.firebase.components.ComponentRegistrar; import com.google.firebase.components.ComponentRuntime; import com.google.firebase.components.Lazy; import com.google.firebase.events.Publisher; import com.google.firebase.heartbeatinfo.DefaultHeartBeatController; import com.google.firebase.inject.Provider; import com.google.firebase.internal.DataCollectionConfigStorage; import com.google.firebase.tracing.ComponentMonitor; import com.google.firebase.tracing.FirebaseTrace; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.Executor; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; import o.VMo; import o.nyh; /* loaded from: classes.dex */ public class FirebaseApp { private final Context applicationContext; private final ComponentRuntime componentRuntime; private final Lazy dataCollectionConfigStorage; private final Provider defaultHeartBeatController; private final String name; private final FirebaseOptions options; private static final Object LOCK = new Object(); private static final Executor UI_EXECUTOR = new UiExecutor(); static final Map INSTANCES = new nyh(); private final AtomicBoolean automaticResourceManagementEnabled = new AtomicBoolean(false); private final AtomicBoolean deleted = new AtomicBoolean(); private final List backgroundStateChangeListeners = new CopyOnWriteArrayList(); private final List lifecycleListeners = new CopyOnWriteArrayList(); /* loaded from: classes.dex */ public interface BackgroundStateChangeListener { void onBackgroundStateChanged(boolean z); } public Context getApplicationContext() { checkNotDeleted(); return this.applicationContext; } public String getName() { checkNotDeleted(); return this.name; } public FirebaseOptions getOptions() { checkNotDeleted(); return this.options; } public boolean equals(Object obj) { if (obj instanceof FirebaseApp) { return this.name.equals(((FirebaseApp) obj).getName()); } return false; } public int hashCode() { return this.name.hashCode(); } public String toString() { return Objects.toStringHelper(this).add("name", this.name).add("options", this.options).toString(); } public static FirebaseApp getInstance() { FirebaseApp firebaseApp; synchronized (LOCK) { firebaseApp = INSTANCES.get("[DEFAULT]"); if (firebaseApp == null) { StringBuilder sb = new StringBuilder("Default FirebaseApp is not initialized in this process "); sb.append(ProcessUtils.getMyProcessName()); sb.append(". Make sure to call FirebaseApp.initializeApp(Context) first."); throw new IllegalStateException(sb.toString()); } } return firebaseApp; } public static FirebaseApp getInstance(String str) { FirebaseApp firebaseApp; String obj; synchronized (LOCK) { firebaseApp = INSTANCES.get(normalize(str)); if (firebaseApp != null) { firebaseApp.defaultHeartBeatController.get().registerHeartBeat(); } else { List allAppNames = getAllAppNames(); if (allAppNames.isEmpty()) { obj = ""; } else { StringBuilder sb = new StringBuilder("Available app names: "); sb.append(TextUtils.join(", ", allAppNames)); obj = sb.toString(); } throw new IllegalStateException(String.format("FirebaseApp with name %s doesn't exist. %s", str, obj)); } } return firebaseApp; } public static FirebaseApp initializeApp(Context context) { synchronized (LOCK) { if (INSTANCES.containsKey("[DEFAULT]")) { return getInstance(); } FirebaseOptions fromResource = FirebaseOptions.fromResource(context); if (fromResource == null) { return null; } return initializeApp(context, fromResource); } } public static FirebaseApp initializeApp(Context context, FirebaseOptions firebaseOptions) { return initializeApp(context, firebaseOptions, "[DEFAULT]"); } public static FirebaseApp initializeApp(Context context, FirebaseOptions firebaseOptions, String str) { FirebaseApp firebaseApp; GlobalBackgroundStateListener.ensureBackgroundStateListenerRegistered(context); String normalize = normalize(str); if (context.getApplicationContext() != null) { context = context.getApplicationContext(); } synchronized (LOCK) { Map map = INSTANCES; boolean containsKey = map.containsKey(normalize); StringBuilder sb = new StringBuilder("FirebaseApp name "); sb.append(normalize); sb.append(" already exists!"); Preconditions.checkState(!containsKey, sb.toString()); Preconditions.checkNotNull(context, "Application context cannot be null."); firebaseApp = new FirebaseApp(context, normalize, firebaseOptions); map.put(normalize, firebaseApp); } firebaseApp.initializeAllApis(); return firebaseApp; } public T get(Class cls) { checkNotDeleted(); return (T) this.componentRuntime.get(cls); } public boolean isDataCollectionDefaultEnabled() { checkNotDeleted(); return this.dataCollectionConfigStorage.get().isEnabled(); } protected FirebaseApp(final Context context, String str, FirebaseOptions firebaseOptions) { this.applicationContext = (Context) Preconditions.checkNotNull(context); this.name = Preconditions.checkNotEmpty(str); this.options = (FirebaseOptions) Preconditions.checkNotNull(firebaseOptions); FirebaseTrace.pushTrace("Firebase"); FirebaseTrace.pushTrace("ComponentDiscovery"); List> discoverLazy = ComponentDiscovery.forContext(context, ComponentDiscoveryService.class).discoverLazy(); FirebaseTrace.popTrace(); FirebaseTrace.pushTrace("Runtime"); ComponentRuntime build = ComponentRuntime.builder(UI_EXECUTOR).addLazyComponentRegistrars(discoverLazy).addComponentRegistrar(new FirebaseCommonRegistrar()).addComponent(Component.of(context, Context.class, new Class[0])).addComponent(Component.of(this, FirebaseApp.class, new Class[0])).addComponent(Component.of(firebaseOptions, FirebaseOptions.class, new Class[0])).setProcessor(new ComponentMonitor()).build(); this.componentRuntime = build; FirebaseTrace.popTrace(); this.dataCollectionConfigStorage = new Lazy<>(new Provider(this, context) { // from class: com.google.firebase.FirebaseApp$$ExternalSyntheticLambda0 public final FirebaseApp f$0; public final Context f$1; @Override // com.google.firebase.inject.Provider public final Object get() { return this.f$0.m165lambda$new$0$comgooglefirebaseFirebaseApp(this.f$1); } { this.f$0 = this; this.f$1 = context; } }); this.defaultHeartBeatController = build.getProvider(DefaultHeartBeatController.class); addBackgroundStateChangeListener(new BackgroundStateChangeListener(this) { // from class: com.google.firebase.FirebaseApp$$ExternalSyntheticLambda1 public final FirebaseApp f$0; @Override // com.google.firebase.FirebaseApp.BackgroundStateChangeListener public final void onBackgroundStateChanged(boolean z) { this.f$0.m166lambda$new$1$comgooglefirebaseFirebaseApp(z); } { this.f$0 = this; } }); FirebaseTrace.popTrace(); } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: lambda$new$0$com-google-firebase-FirebaseApp, reason: not valid java name */ public /* synthetic */ DataCollectionConfigStorage m165lambda$new$0$comgooglefirebaseFirebaseApp(Context context) { return new DataCollectionConfigStorage(context, getPersistenceKey(), (Publisher) this.componentRuntime.get(Publisher.class)); } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: lambda$new$1$com-google-firebase-FirebaseApp, reason: not valid java name */ public /* synthetic */ void m166lambda$new$1$comgooglefirebaseFirebaseApp(boolean z) { if (z) { return; } this.defaultHeartBeatController.get().registerHeartBeat(); } private void checkNotDeleted() { Preconditions.checkState(!this.deleted.get(), "FirebaseApp was deleted"); } public boolean isDefaultApp() { return "[DEFAULT]".equals(getName()); } /* JADX INFO: Access modifiers changed from: private */ public void notifyBackgroundStateChangeListeners(boolean z) { Iterator it = this.backgroundStateChangeListeners.iterator(); while (it.hasNext()) { it.next().onBackgroundStateChanged(z); } } public void addBackgroundStateChangeListener(BackgroundStateChangeListener backgroundStateChangeListener) { checkNotDeleted(); if (this.automaticResourceManagementEnabled.get() && BackgroundDetector.getInstance().isInBackground()) { backgroundStateChangeListener.onBackgroundStateChanged(true); } this.backgroundStateChangeListeners.add(backgroundStateChangeListener); } public String getPersistenceKey() { StringBuilder sb = new StringBuilder(); sb.append(Base64Utils.encodeUrlSafeNoPadding(getName().getBytes(Charset.defaultCharset()))); sb.append("+"); sb.append(Base64Utils.encodeUrlSafeNoPadding(getOptions().getApplicationId().getBytes(Charset.defaultCharset()))); return sb.toString(); } public void addLifecycleEventListener(FirebaseAppLifecycleListener firebaseAppLifecycleListener) { checkNotDeleted(); Preconditions.checkNotNull(firebaseAppLifecycleListener); this.lifecycleListeners.add(firebaseAppLifecycleListener); } private static List getAllAppNames() { ArrayList arrayList = new ArrayList(); synchronized (LOCK) { Iterator it = INSTANCES.values().iterator(); while (it.hasNext()) { arrayList.add(it.next().getName()); } } Collections.sort(arrayList); return arrayList; } /* JADX INFO: Access modifiers changed from: private */ public void initializeAllApis() { if (!VMo.d(this.applicationContext)) { getName(); UserUnlockReceiver.ensureReceiverRegistered(this.applicationContext); } else { getName(); this.componentRuntime.initializeEagerComponents(isDefaultApp()); this.defaultHeartBeatController.get().registerHeartBeat(); } } private static String normalize(String str) { return str.trim(); } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes.dex */ public static class UserUnlockReceiver extends BroadcastReceiver { private static AtomicReference INSTANCE = new AtomicReference<>(); private final Context applicationContext; public UserUnlockReceiver(Context context) { this.applicationContext = context; } /* JADX INFO: Access modifiers changed from: private */ public static void ensureReceiverRegistered(Context context) { if (INSTANCE.get() == null) { UserUnlockReceiver userUnlockReceiver = new UserUnlockReceiver(context); AtomicReference atomicReference = INSTANCE; while (!atomicReference.compareAndSet(null, userUnlockReceiver)) { if (atomicReference.get() != null) { return; } } context.registerReceiver(userUnlockReceiver, new IntentFilter("android.intent.action.USER_UNLOCKED")); } } @Override // android.content.BroadcastReceiver public void onReceive(Context context, Intent intent) { synchronized (FirebaseApp.LOCK) { Iterator it = FirebaseApp.INSTANCES.values().iterator(); while (it.hasNext()) { it.next().initializeAllApis(); } } unregister(); } public void unregister() { this.applicationContext.unregisterReceiver(this); } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes.dex */ public static class GlobalBackgroundStateListener implements BackgroundDetector.BackgroundStateChangeListener { private static AtomicReference INSTANCE = new AtomicReference<>(); private GlobalBackgroundStateListener() { } /* JADX INFO: Access modifiers changed from: private */ public static void ensureBackgroundStateListenerRegistered(Context context) { if (PlatformVersion.isAtLeastIceCreamSandwich() && (context.getApplicationContext() instanceof Application)) { Application application = (Application) context.getApplicationContext(); if (INSTANCE.get() == null) { GlobalBackgroundStateListener globalBackgroundStateListener = new GlobalBackgroundStateListener(); AtomicReference atomicReference = INSTANCE; while (!atomicReference.compareAndSet(null, globalBackgroundStateListener)) { if (atomicReference.get() != null) { return; } } BackgroundDetector.initialize(application); BackgroundDetector.getInstance().addListener(globalBackgroundStateListener); } } } @Override // com.google.android.gms.common.api.internal.BackgroundDetector.BackgroundStateChangeListener public void onBackgroundStateChanged(boolean z) { synchronized (FirebaseApp.LOCK) { Iterator it = new ArrayList(FirebaseApp.INSTANCES.values()).iterator(); while (it.hasNext()) { FirebaseApp firebaseApp = (FirebaseApp) it.next(); if (firebaseApp.automaticResourceManagementEnabled.get()) { firebaseApp.notifyBackgroundStateChangeListeners(z); } } } } } /* loaded from: classes.dex */ static class UiExecutor implements Executor { private static final Handler HANDLER = new Handler(Looper.getMainLooper()); private UiExecutor() { } @Override // java.util.concurrent.Executor public void execute(Runnable runnable) { HANDLER.post(runnable); } } }