package com.google.firebase.firestore.remote; import android.app.Activity; import android.app.Application; import android.content.BroadcastReceiver; import android.content.ComponentCallbacks2; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.res.Configuration; import android.net.ConnectivityManager; import android.net.Network; import android.net.NetworkInfo; import android.os.Bundle; import com.google.firebase.firestore.remote.AndroidConnectivityMonitor; import com.google.firebase.firestore.remote.ConnectivityMonitor; import com.google.firebase.firestore.util.Assert; import com.google.firebase.firestore.util.Consumer; import com.google.firebase.firestore.util.Logger; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; /* loaded from: classes2.dex */ public final class AndroidConnectivityMonitor implements ConnectivityMonitor { private final List> callbacks = new ArrayList(); private final ConnectivityManager connectivityManager; private final Context context; private Runnable unregisterRunnable; public AndroidConnectivityMonitor(Context context) { Assert.hardAssert(context != null, "Context must be non-null", new Object[0]); this.context = context; this.connectivityManager = (ConnectivityManager) context.getSystemService("connectivity"); configureBackgroundStateListener(); configureNetworkMonitoring(); } @Override // com.google.firebase.firestore.remote.ConnectivityMonitor public final void addCallback(Consumer consumer) { synchronized (this.callbacks) { this.callbacks.add(consumer); } } private void configureNetworkMonitoring() { if (this.connectivityManager != null) { final DefaultNetworkCallback defaultNetworkCallback = new DefaultNetworkCallback(); this.connectivityManager.registerDefaultNetworkCallback(defaultNetworkCallback); this.unregisterRunnable = new Runnable(this, defaultNetworkCallback) { // from class: com.google.firebase.firestore.remote.AndroidConnectivityMonitor$$ExternalSyntheticLambda0 public final AndroidConnectivityMonitor f$0; public final AndroidConnectivityMonitor.DefaultNetworkCallback f$1; @Override // java.lang.Runnable public final void run() { this.f$0.m241xddc22e50(this.f$1); } { this.f$0 = this; this.f$1 = defaultNetworkCallback; } }; } else { final NetworkReceiver networkReceiver = new NetworkReceiver(); this.context.registerReceiver(networkReceiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE")); this.unregisterRunnable = new Runnable(this, networkReceiver) { // from class: com.google.firebase.firestore.remote.AndroidConnectivityMonitor$$ExternalSyntheticLambda1 public final AndroidConnectivityMonitor f$0; public final AndroidConnectivityMonitor.NetworkReceiver f$1; @Override // java.lang.Runnable public final void run() { this.f$0.m242x2b81a651(this.f$1); } { this.f$0 = this; this.f$1 = networkReceiver; } }; } } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: lambda$configureNetworkMonitoring$0$com-google-firebase-firestore-remote-AndroidConnectivityMonitor, reason: not valid java name */ public final /* synthetic */ void m241xddc22e50(DefaultNetworkCallback defaultNetworkCallback) { this.connectivityManager.unregisterNetworkCallback(defaultNetworkCallback); } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: lambda$configureNetworkMonitoring$1$com-google-firebase-firestore-remote-AndroidConnectivityMonitor, reason: not valid java name */ public final /* synthetic */ void m242x2b81a651(NetworkReceiver networkReceiver) { this.context.unregisterReceiver(networkReceiver); } private void configureBackgroundStateListener() { Application application = (Application) this.context.getApplicationContext(); AtomicBoolean atomicBoolean = new AtomicBoolean(); application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks(this, atomicBoolean) { // from class: com.google.firebase.firestore.remote.AndroidConnectivityMonitor.1 final AndroidConnectivityMonitor this$0; final AtomicBoolean val$inBackground; @Override // android.app.Application.ActivityLifecycleCallbacks public void onActivityDestroyed(Activity activity) { } @Override // android.app.Application.ActivityLifecycleCallbacks public void onActivityPaused(Activity activity) { } @Override // android.app.Application.ActivityLifecycleCallbacks public void onActivitySaveInstanceState(Activity activity, Bundle bundle) { } @Override // android.app.Application.ActivityLifecycleCallbacks public void onActivityStopped(Activity activity) { } { this.this$0 = this; this.val$inBackground = atomicBoolean; } @Override // android.app.Application.ActivityLifecycleCallbacks public void onActivityCreated(Activity activity, Bundle bundle) { if (this.val$inBackground.compareAndSet(true, false)) { this.this$0.raiseForegroundNotification(); } } @Override // android.app.Application.ActivityLifecycleCallbacks public void onActivityStarted(Activity activity) { if (this.val$inBackground.compareAndSet(true, false)) { this.this$0.raiseForegroundNotification(); } } @Override // android.app.Application.ActivityLifecycleCallbacks public void onActivityResumed(Activity activity) { if (this.val$inBackground.compareAndSet(true, false)) { this.this$0.raiseForegroundNotification(); } } }); application.registerComponentCallbacks(new ComponentCallbacks2(this, atomicBoolean) { // from class: com.google.firebase.firestore.remote.AndroidConnectivityMonitor.2 final AndroidConnectivityMonitor this$0; final AtomicBoolean val$inBackground; @Override // android.content.ComponentCallbacks public void onConfigurationChanged(Configuration configuration) { } @Override // android.content.ComponentCallbacks public void onLowMemory() { } { this.this$0 = this; this.val$inBackground = atomicBoolean; } @Override // android.content.ComponentCallbacks2 public void onTrimMemory(int i) { if (i == 20) { this.val$inBackground.set(true); } } }); } public final void raiseForegroundNotification() { Logger.debug("AndroidConnectivityMonitor", "App has entered the foreground.", new Object[0]); if (isConnected()) { raiseCallbacks(true); } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public class DefaultNetworkCallback extends ConnectivityManager.NetworkCallback { final AndroidConnectivityMonitor this$0; private DefaultNetworkCallback(AndroidConnectivityMonitor androidConnectivityMonitor) { this.this$0 = androidConnectivityMonitor; } @Override // android.net.ConnectivityManager.NetworkCallback public void onAvailable(Network network) { this.this$0.raiseCallbacks(true); } @Override // android.net.ConnectivityManager.NetworkCallback public void onLost(Network network) { this.this$0.raiseCallbacks(false); } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes2.dex */ public class NetworkReceiver extends BroadcastReceiver { final AndroidConnectivityMonitor this$0; private boolean wasConnected; private NetworkReceiver(AndroidConnectivityMonitor androidConnectivityMonitor) { this.this$0 = androidConnectivityMonitor; this.wasConnected = false; } @Override // android.content.BroadcastReceiver public void onReceive(Context context, Intent intent) { boolean isConnected = this.this$0.isConnected(); if (this.this$0.isConnected() && !this.wasConnected) { this.this$0.raiseCallbacks(true); } else if (!isConnected && this.wasConnected) { this.this$0.raiseCallbacks(false); } this.wasConnected = isConnected; } } /* JADX INFO: Access modifiers changed from: private */ public void raiseCallbacks(boolean z) { synchronized (this.callbacks) { Iterator> it = this.callbacks.iterator(); while (it.hasNext()) { it.next().accept(z ? ConnectivityMonitor.NetworkStatus.REACHABLE : ConnectivityMonitor.NetworkStatus.UNREACHABLE); } } } /* JADX INFO: Access modifiers changed from: private */ public boolean isConnected() { NetworkInfo activeNetworkInfo = ((ConnectivityManager) this.context.getSystemService("connectivity")).getActiveNetworkInfo(); return activeNetworkInfo != null && activeNetworkInfo.isConnected(); } }