package com.applovin.impl; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Handler; import android.os.Looper; import android.telephony.PhoneStateListener; import android.telephony.ServiceState; import android.telephony.TelephonyDisplayInfo; import android.telephony.TelephonyManager; import java.lang.ref.WeakReference; import java.util.Iterator; import java.util.concurrent.CopyOnWriteArrayList; /* loaded from: classes.dex */ public final class fg { private static fg e; private final Handler a = new Handler(Looper.getMainLooper()); private final CopyOnWriteArrayList b = new CopyOnWriteArrayList(); private final Object c = new Object(); private int d = 0; public static final class b { private static volatile boolean a; } public interface c { void a(int i); } private fg(Context context) { IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); context.registerReceiver(new d(), intentFilter); } public void b(final c cVar) { b(); this.b.add(new WeakReference(cVar)); this.a.post(new Runnable() { // from class: com.applovin.impl.fg$$ExternalSyntheticLambda0 @Override // java.lang.Runnable public final void run() { fg.this.a(cVar); } }); } /* JADX INFO: Access modifiers changed from: private */ public static int c(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity"); int i = 0; if (connectivityManager == null) { return 0; } try { NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); i = 1; if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) { int type = activeNetworkInfo.getType(); if (type != 0) { if (type == 1) { return 2; } if (type == 9) { return 7; } if (type != 4 && type != 5) { return type != 6 ? 8 : 5; } } return a(activeNetworkInfo); } } catch (SecurityException unused) { } return i; } public final class d extends BroadcastReceiver { private d() { } @Override // android.content.BroadcastReceiver public void onReceive(Context context, Intent intent) { int c = fg.c(context); int i = xp.a; if (i >= 29 && !b.a && c == 5) { try { TelephonyManager telephonyManager = (TelephonyManager) b1.a((TelephonyManager) context.getSystemService("phone")); e eVar = new e(); if (i < 31) { telephonyManager.listen(eVar, 1); } else { telephonyManager.listen(eVar, 1048576); } telephonyManager.listen(eVar, 0); return; } catch (RuntimeException unused) { } } fg.this.a(c); } } public class e extends PhoneStateListener { private e() { } @Override // android.telephony.PhoneStateListener public void onServiceStateChanged(ServiceState serviceState) { String serviceState2 = serviceState == null ? "" : serviceState.toString(); fg.this.a(serviceState2.contains("nrState=CONNECTED") || serviceState2.contains("nrState=NOT_RESTRICTED") ? 10 : 5); } @Override // android.telephony.PhoneStateListener public void onDisplayInfoChanged(TelephonyDisplayInfo telephonyDisplayInfo) { int overrideNetworkType; overrideNetworkType = telephonyDisplayInfo.getOverrideNetworkType(); fg.this.a(overrideNetworkType == 3 || overrideNetworkType == 4 ? 10 : 5); } } private void b() { Iterator it = this.b.iterator(); while (it.hasNext()) { WeakReference weakReference = (WeakReference) it.next(); if (weakReference.get() == null) { this.b.remove(weakReference); } } } public static synchronized fg b(Context context) { fg fgVar; synchronized (fg.class) { try { if (e == null) { e = new fg(context); } fgVar = e; } catch (Throwable th) { throw th; } } return fgVar; } private static int a(NetworkInfo networkInfo) { switch (networkInfo.getSubtype()) { case 1: case 2: return 3; case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: case 12: case 14: case 15: case 17: return 4; case 13: return 5; case 16: case 19: default: return 6; case 18: return 2; case 20: return xp.a >= 29 ? 9 : 0; } } public int a() { int i; synchronized (this.c) { i = this.d; } return i; } /* JADX INFO: Access modifiers changed from: private */ public /* synthetic */ void a(c cVar) { cVar.a(a()); } /* JADX INFO: Access modifiers changed from: private */ public void a(int i) { synchronized (this.c) { try { if (this.d == i) { return; } this.d = i; Iterator it = this.b.iterator(); while (it.hasNext()) { WeakReference weakReference = (WeakReference) it.next(); c cVar = (c) weakReference.get(); if (cVar != null) { cVar.a(i); } else { this.b.remove(weakReference); } } } catch (Throwable th) { throw th; } } } }