package androidx.media3.exoplayer.scheduler; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.ConnectivityManager; import android.net.Network; import android.net.NetworkCapabilities; import android.os.Handler; import androidx.media3.common.util.Assertions; import androidx.media3.common.util.Util; import androidx.media3.exoplayer.scheduler.RequirementsWatcher; /* loaded from: classes2.dex */ public final class RequirementsWatcher { private final Context context; private final Handler handler = Util.createHandlerForCurrentOrMainLooper(); private final Listener listener; private NetworkCallback networkCallback; private int notMetRequirements; private DeviceStatusChangeReceiver receiver; private final Requirements requirements; /* loaded from: classes2.dex */ public interface Listener { void onRequirementsStateChanged(RequirementsWatcher requirementsWatcher, int i); } public Requirements getRequirements() { return this.requirements; } public RequirementsWatcher(Context context, Listener listener, Requirements requirements) { this.context = context.getApplicationContext(); this.listener = listener; this.requirements = requirements; } public int start() { this.notMetRequirements = this.requirements.getNotMetRequirements(this.context); IntentFilter intentFilter = new IntentFilter(); if (this.requirements.isNetworkRequired()) { if (Util.SDK_INT >= 24) { registerNetworkCallbackV24(); } else { intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); } } if (this.requirements.isChargingRequired()) { intentFilter.addAction("android.intent.action.ACTION_POWER_CONNECTED"); intentFilter.addAction("android.intent.action.ACTION_POWER_DISCONNECTED"); } if (this.requirements.isIdleRequired()) { if (Util.SDK_INT >= 23) { intentFilter.addAction("android.os.action.DEVICE_IDLE_MODE_CHANGED"); } else { intentFilter.addAction("android.intent.action.SCREEN_ON"); intentFilter.addAction("android.intent.action.SCREEN_OFF"); } } if (this.requirements.isStorageNotLowRequired()) { intentFilter.addAction("android.intent.action.DEVICE_STORAGE_LOW"); intentFilter.addAction("android.intent.action.DEVICE_STORAGE_OK"); } DeviceStatusChangeReceiver deviceStatusChangeReceiver = new DeviceStatusChangeReceiver(); this.receiver = deviceStatusChangeReceiver; this.context.registerReceiver(deviceStatusChangeReceiver, intentFilter, null, this.handler); return this.notMetRequirements; } public void stop() { this.context.unregisterReceiver((BroadcastReceiver) Assertions.checkNotNull(this.receiver)); this.receiver = null; if (Util.SDK_INT < 24 || this.networkCallback == null) { return; } unregisterNetworkCallbackV24(); } private void registerNetworkCallbackV24() { ConnectivityManager connectivityManager = (ConnectivityManager) Assertions.checkNotNull((ConnectivityManager) this.context.getSystemService("connectivity")); NetworkCallback networkCallback = new NetworkCallback(); this.networkCallback = networkCallback; connectivityManager.registerDefaultNetworkCallback(networkCallback); } private void unregisterNetworkCallbackV24() { ((ConnectivityManager) Assertions.checkNotNull((ConnectivityManager) this.context.getSystemService("connectivity"))).unregisterNetworkCallback((ConnectivityManager.NetworkCallback) Assertions.checkNotNull(this.networkCallback)); this.networkCallback = null; } /* JADX INFO: Access modifiers changed from: private */ public void checkRequirements() { int notMetRequirements = this.requirements.getNotMetRequirements(this.context); if (this.notMetRequirements != notMetRequirements) { this.notMetRequirements = notMetRequirements; this.listener.onRequirementsStateChanged(this, notMetRequirements); } } /* JADX INFO: Access modifiers changed from: private */ public void recheckNotMetNetworkRequirements() { if ((this.notMetRequirements & 3) == 0) { return; } checkRequirements(); } /* JADX INFO: Access modifiers changed from: private */ /* loaded from: classes2.dex */ public class DeviceStatusChangeReceiver extends BroadcastReceiver { private DeviceStatusChangeReceiver() { } @Override // android.content.BroadcastReceiver public void onReceive(Context context, Intent intent) { if (isInitialStickyBroadcast()) { return; } RequirementsWatcher.this.checkRequirements(); } } /* JADX INFO: Access modifiers changed from: private */ /* loaded from: classes2.dex */ public final class NetworkCallback extends ConnectivityManager.NetworkCallback { private boolean networkValidated; private boolean receivedCapabilitiesChange; private NetworkCallback() { } @Override // android.net.ConnectivityManager.NetworkCallback public void onAvailable(Network network) { postCheckRequirements(); } @Override // android.net.ConnectivityManager.NetworkCallback public void onLost(Network network) { postCheckRequirements(); } @Override // android.net.ConnectivityManager.NetworkCallback public void onBlockedStatusChanged(Network network, boolean z) { if (z) { return; } postRecheckNotMetNetworkRequirements(); } @Override // android.net.ConnectivityManager.NetworkCallback public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) { boolean hasCapability = networkCapabilities.hasCapability(16); if (this.receivedCapabilitiesChange && this.networkValidated == hasCapability) { if (hasCapability) { postRecheckNotMetNetworkRequirements(); } } else { this.receivedCapabilitiesChange = true; this.networkValidated = hasCapability; postCheckRequirements(); } } private void postCheckRequirements() { RequirementsWatcher.this.handler.post(new Runnable() { // from class: androidx.media3.exoplayer.scheduler.RequirementsWatcher$NetworkCallback$$ExternalSyntheticLambda1 @Override // java.lang.Runnable public final void run() { RequirementsWatcher.NetworkCallback.this.m5255xcc18be42(); } }); } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: lambda$postCheckRequirements$0$androidx-media3-exoplayer-scheduler-RequirementsWatcher$NetworkCallback, reason: not valid java name */ public /* synthetic */ void m5255xcc18be42() { if (RequirementsWatcher.this.networkCallback != null) { RequirementsWatcher.this.checkRequirements(); } } private void postRecheckNotMetNetworkRequirements() { RequirementsWatcher.this.handler.post(new Runnable() { // from class: androidx.media3.exoplayer.scheduler.RequirementsWatcher$NetworkCallback$$ExternalSyntheticLambda0 @Override // java.lang.Runnable public final void run() { RequirementsWatcher.NetworkCallback.this.m5256xfb2bca45(); } }); } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: lambda$postRecheckNotMetNetworkRequirements$1$androidx-media3-exoplayer-scheduler-RequirementsWatcher$NetworkCallback, reason: not valid java name */ public /* synthetic */ void m5256xfb2bca45() { if (RequirementsWatcher.this.networkCallback != null) { RequirementsWatcher.this.recheckNotMetNetworkRequirements(); } } } }