package com.google.firebase; import K.k; import android.annotation.TargetApi; import android.app.Application; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.text.TextUtils; import android.util.Log; import com.google.android.gms.common.annotation.KeepForSdk; 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.android.gms.measurement.api.AppMeasurementSdk; 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.concurrent.ExecutorsRegistrar; import com.google.firebase.concurrent.UiExecutor; 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.provider.FirebaseInitProvider; 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.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; import s.j; /* loaded from: classes3.dex */ public class FirebaseApp { public static final String DEFAULT_APP_NAME = "[DEFAULT]"; private static final String LOG_TAG = "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(); static final Map INSTANCES = new j(0); 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(); @KeepForSdk /* loaded from: classes3.dex */ public interface BackgroundStateChangeListener { @KeepForSdk void onBackgroundStateChanged(boolean z3); } @TargetApi(14) /* loaded from: classes3.dex */ public static class GlobalBackgroundStateListener implements BackgroundDetector.BackgroundStateChangeListener { private static AtomicReference INSTANCE = new AtomicReference<>(); private GlobalBackgroundStateListener() { } 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 z3) { synchronized (FirebaseApp.LOCK) { try { Iterator it = new ArrayList(FirebaseApp.INSTANCES.values()).iterator(); while (it.hasNext()) { FirebaseApp firebaseApp = (FirebaseApp) it.next(); if (firebaseApp.automaticResourceManagementEnabled.get()) { firebaseApp.notifyBackgroundStateChangeListeners(z3); } } } catch (Throwable th) { throw th; } } } } @TargetApi(24) /* loaded from: classes3.dex */ public static class UserUnlockReceiver extends BroadcastReceiver { private static AtomicReference INSTANCE = new AtomicReference<>(); private final Context applicationContext; public UserUnlockReceiver(Context context) { this.applicationContext = context; } 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) { try { Iterator it = FirebaseApp.INSTANCES.values().iterator(); while (it.hasNext()) { it.next().initializeAllApis(); } } catch (Throwable th) { throw th; } } unregister(); } public void unregister() { this.applicationContext.unregisterReceiver(this); } } public FirebaseApp(Context context, String str, FirebaseOptions firebaseOptions) { this.applicationContext = (Context) Preconditions.checkNotNull(context); this.name = Preconditions.checkNotEmpty(str); this.options = (FirebaseOptions) Preconditions.checkNotNull(firebaseOptions); StartupTime startupTime = FirebaseInitProvider.getStartupTime(); FirebaseTrace.pushTrace("Firebase"); FirebaseTrace.pushTrace("ComponentDiscovery"); List> discoverLazy = ComponentDiscovery.forContext(context, ComponentDiscoveryService.class).discoverLazy(); FirebaseTrace.popTrace(); FirebaseTrace.pushTrace("Runtime"); ComponentRuntime.Builder processor = ComponentRuntime.builder(UiExecutor.INSTANCE).addLazyComponentRegistrars(discoverLazy).addComponentRegistrar(new FirebaseCommonRegistrar()).addComponentRegistrar(new ExecutorsRegistrar()).addComponent(Component.of(context, (Class) Context.class, (Class[]) new Class[0])).addComponent(Component.of(this, (Class) FirebaseApp.class, (Class[]) new Class[0])).addComponent(Component.of(firebaseOptions, (Class) FirebaseOptions.class, (Class[]) new Class[0])).setProcessor(new ComponentMonitor()); if (k.a(context) && FirebaseInitProvider.isCurrentlyInitializing()) { processor.addComponent(Component.of(startupTime, (Class) StartupTime.class, (Class[]) new Class[0])); } ComponentRuntime build = processor.build(); this.componentRuntime = build; FirebaseTrace.popTrace(); this.dataCollectionConfigStorage = new Lazy<>((Provider) new a(this, context)); this.defaultHeartBeatController = build.getProvider(DefaultHeartBeatController.class); addBackgroundStateChangeListener(new BackgroundStateChangeListener() { // from class: com.google.firebase.b @Override // com.google.firebase.FirebaseApp.BackgroundStateChangeListener public final void onBackgroundStateChanged(boolean z3) { FirebaseApp.this.lambda$new$1(z3); } }); FirebaseTrace.popTrace(); } private void checkNotDeleted() { Preconditions.checkState(!this.deleted.get(), "FirebaseApp was deleted"); } public static void clearInstancesForTest() { synchronized (LOCK) { INSTANCES.clear(); } } private static List getAllAppNames() { ArrayList arrayList = new ArrayList(); synchronized (LOCK) { try { Iterator it = INSTANCES.values().iterator(); while (it.hasNext()) { arrayList.add(it.next().getName()); } } catch (Throwable th) { throw th; } } Collections.sort(arrayList); return arrayList; } public static List getApps(Context context) { ArrayList arrayList; synchronized (LOCK) { arrayList = new ArrayList(INSTANCES.values()); } return arrayList; } public static FirebaseApp getInstance() { FirebaseApp firebaseApp; synchronized (LOCK) { try { firebaseApp = INSTANCES.get(DEFAULT_APP_NAME); if (firebaseApp == null) { throw new IllegalStateException("Default FirebaseApp is not initialized in this process " + ProcessUtils.getMyProcessName() + ". Make sure to call FirebaseApp.initializeApp(Context) first."); } firebaseApp.defaultHeartBeatController.get().registerHeartBeat(); } catch (Throwable th) { throw th; } } return firebaseApp; } public void initializeAllApis() { if (!k.a(this.applicationContext)) { Log.i(LOG_TAG, "Device in Direct Boot Mode: postponing initialization of Firebase APIs for app " + getName()); UserUnlockReceiver.ensureReceiverRegistered(this.applicationContext); return; } Log.i(LOG_TAG, "Device unlocked: initializing all Firebase APIs for app " + getName()); this.componentRuntime.initializeEagerComponents(isDefaultApp()); this.defaultHeartBeatController.get().registerHeartBeat(); } public static FirebaseApp initializeApp(Context context) { synchronized (LOCK) { try { if (INSTANCES.containsKey(DEFAULT_APP_NAME)) { return getInstance(); } FirebaseOptions fromResource = FirebaseOptions.fromResource(context); if (fromResource == null) { Log.w(LOG_TAG, "Default FirebaseApp failed to initialize because no default options were found. This usually means that com.google.gms:google-services was not applied to your gradle project."); return null; } return initializeApp(context, fromResource); } catch (Throwable th) { throw th; } } } public /* synthetic */ DataCollectionConfigStorage lambda$new$0(Context context) { return new DataCollectionConfigStorage(context, getPersistenceKey(), (Publisher) this.componentRuntime.get(Publisher.class)); } public /* synthetic */ void lambda$new$1(boolean z3) { if (z3) { return; } this.defaultHeartBeatController.get().registerHeartBeat(); } private static String normalize(String str) { return str.trim(); } public void notifyBackgroundStateChangeListeners(boolean z3) { Log.d(LOG_TAG, "Notifying background state change listeners."); Iterator it = this.backgroundStateChangeListeners.iterator(); while (it.hasNext()) { it.next().onBackgroundStateChanged(z3); } } private void notifyOnAppDeleted() { Iterator it = this.lifecycleListeners.iterator(); while (it.hasNext()) { it.next().onDeleted(this.name, this.options); } } @KeepForSdk public void addBackgroundStateChangeListener(BackgroundStateChangeListener backgroundStateChangeListener) { checkNotDeleted(); if (this.automaticResourceManagementEnabled.get() && BackgroundDetector.getInstance().isInBackground()) { backgroundStateChangeListener.onBackgroundStateChanged(true); } this.backgroundStateChangeListeners.add(backgroundStateChangeListener); } @KeepForSdk public void addLifecycleEventListener(FirebaseAppLifecycleListener firebaseAppLifecycleListener) { checkNotDeleted(); Preconditions.checkNotNull(firebaseAppLifecycleListener); this.lifecycleListeners.add(firebaseAppLifecycleListener); } public void delete() { if (this.deleted.compareAndSet(false, true)) { synchronized (LOCK) { INSTANCES.remove(this.name); } notifyOnAppDeleted(); } } public boolean equals(Object obj) { if (obj instanceof FirebaseApp) { return this.name.equals(((FirebaseApp) obj).getName()); } return false; } @KeepForSdk public T get(Class cls) { checkNotDeleted(); return (T) this.componentRuntime.get(cls); } public Context getApplicationContext() { checkNotDeleted(); return this.applicationContext; } public String getName() { checkNotDeleted(); return this.name; } public FirebaseOptions getOptions() { checkNotDeleted(); return this.options; } @KeepForSdk public String getPersistenceKey() { return Base64Utils.encodeUrlSafeNoPadding(getName().getBytes(Charset.defaultCharset())) + "+" + Base64Utils.encodeUrlSafeNoPadding(getOptions().getApplicationId().getBytes(Charset.defaultCharset())); } public int hashCode() { return this.name.hashCode(); } public void initializeAllComponents() { this.componentRuntime.initializeAllComponentsForTests(); } @KeepForSdk public boolean isDataCollectionDefaultEnabled() { checkNotDeleted(); return this.dataCollectionConfigStorage.get().isEnabled(); } @KeepForSdk public boolean isDefaultApp() { return DEFAULT_APP_NAME.equals(getName()); } @KeepForSdk public void removeBackgroundStateChangeListener(BackgroundStateChangeListener backgroundStateChangeListener) { checkNotDeleted(); this.backgroundStateChangeListeners.remove(backgroundStateChangeListener); } @KeepForSdk public void removeLifecycleEventListener(FirebaseAppLifecycleListener firebaseAppLifecycleListener) { checkNotDeleted(); Preconditions.checkNotNull(firebaseAppLifecycleListener); this.lifecycleListeners.remove(firebaseAppLifecycleListener); } public void setAutomaticResourceManagementEnabled(boolean z3) { checkNotDeleted(); if (this.automaticResourceManagementEnabled.compareAndSet(!z3, z3)) { boolean isInBackground = BackgroundDetector.getInstance().isInBackground(); if (z3 && isInBackground) { notifyBackgroundStateChangeListeners(true); } else { if (z3 || !isInBackground) { return; } notifyBackgroundStateChangeListeners(false); } } } @KeepForSdk public void setDataCollectionDefaultEnabled(Boolean bool) { checkNotDeleted(); this.dataCollectionConfigStorage.get().setEnabled(bool); } public String toString() { return Objects.toStringHelper(this).add(AppMeasurementSdk.ConditionalUserProperty.NAME, this.name).add("options", this.options).toString(); } @KeepForSdk @Deprecated public void setDataCollectionDefaultEnabled(boolean z3) { setDataCollectionDefaultEnabled(Boolean.valueOf(z3)); } @KeepForSdk public static String getPersistenceKey(String str, FirebaseOptions firebaseOptions) { return Base64Utils.encodeUrlSafeNoPadding(str.getBytes(Charset.defaultCharset())) + "+" + Base64Utils.encodeUrlSafeNoPadding(firebaseOptions.getApplicationId().getBytes(Charset.defaultCharset())); } public static FirebaseApp getInstance(String str) { FirebaseApp firebaseApp; String str2; synchronized (LOCK) { try { firebaseApp = INSTANCES.get(normalize(str)); if (firebaseApp != null) { firebaseApp.defaultHeartBeatController.get().registerHeartBeat(); } else { List allAppNames = getAllAppNames(); if (allAppNames.isEmpty()) { str2 = ""; } else { str2 = "Available app names: " + TextUtils.join(", ", allAppNames); } throw new IllegalStateException("FirebaseApp with name " + str + " doesn't exist. " + str2); } } finally { } } return firebaseApp; } public static FirebaseApp initializeApp(Context context, FirebaseOptions firebaseOptions) { return initializeApp(context, firebaseOptions, DEFAULT_APP_NAME); } 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; Preconditions.checkState(!map.containsKey(normalize), "FirebaseApp name " + normalize + " already exists!"); Preconditions.checkNotNull(context, "Application context cannot be null."); firebaseApp = new FirebaseApp(context, normalize, firebaseOptions); map.put(normalize, firebaseApp); } firebaseApp.initializeAllApis(); return firebaseApp; } }