package com.bytedance.sdk.component.utils;

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.SystemClock;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

/* JADX INFO: loaded from: classes.dex */
public class nt {
    private static final Object fm = new Object();
    private static final Map<fm, Object> ro = new ConcurrentHashMap();

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    private static final AtomicInteger f6688lb = new AtomicInteger();
    private static AtomicBoolean yz = new AtomicBoolean(false);
    private static volatile int jnr = -1;
    private static volatile long ajl = 0;
    private static volatile int wsy = 60000;
    private static gqi wu = null;

    /* JADX INFO: renamed from: ef, reason: collision with root package name */
    private static final AtomicBoolean f6687ef = new AtomicBoolean(false);

    public interface fm {
        void fm(Context context, Intent intent, boolean z10, int i10);
    }

    public static class ro extends BroadcastReceiver {
        private ro() {
        }

        @Override // android.content.BroadcastReceiver
        public void onReceive(Context context, Intent intent) {
            try {
                nt.ro(context, intent, nt.f6688lb.get() > 0, false);
            } catch (Throwable unused) {
            }
        }
    }

    private static int lb(Context context) {
        try {
            NetworkInfo activeNetworkInfo = ((ConnectivityManager) context.getSystemService("connectivity")).getActiveNetworkInfo();
            if (activeNetworkInfo != null && activeNetworkInfo.isAvailable()) {
                int type = activeNetworkInfo.getType();
                if (type != 0) {
                    return type != 1 ? 1 : 4;
                }
                TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService("phone");
                switch (activeNetworkInfo.getSubtype()) {
                    case 1:
                    case 2:
                    case 4:
                    case 7:
                    case 11:
                    case 16:
                        return 2;
                    case 3:
                    case 5:
                    case 6:
                    case 8:
                    case 9:
                    case 10:
                    case 12:
                    case 14:
                    case 15:
                    case 17:
                        return 3;
                    case 13:
                    case 18:
                    case 19:
                        gqi gqiVar = wu;
                        return (gqiVar == null || !gqiVar.fm(context, telephonyManager)) ? 5 : 6;
                    case 20:
                        return 6;
                    default:
                        String subtypeName = activeNetworkInfo.getSubtypeName();
                        return (TextUtils.isEmpty(subtypeName) || !(subtypeName.equalsIgnoreCase("TD-SCDMA") || subtypeName.equalsIgnoreCase("WCDMA") || subtypeName.equalsIgnoreCase("CDMA2000"))) ? 1 : 3;
                }
            }
            return 0;
        } catch (Throwable unused) {
            return 1;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static void ro(final Context context, final Intent intent, final boolean z10, final boolean z11) {
        if (!z10 && z11) {
            jnr = 0;
        } else if (f6687ef.compareAndSet(false, true)) {
            com.bytedance.sdk.component.wu.fm.fm(new Runnable() { // from class: com.bytedance.sdk.component.utils.nt.1
                @Override // java.lang.Runnable
                public void run() {
                    int unused = nt.jnr = z11 ? 0 : nt.ro(context);
                    nt.f6687ef.set(false);
                    boolean z12 = nt.jnr == 0;
                    boolean z13 = z10;
                    if ((z13 || !z12) && z13) {
                        nt.ro(context, intent, nt.jnr, z11);
                    }
                }
            });
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static void ro(Context context, Intent intent, int i10, boolean z10) {
        Map<fm, Object> map = ro;
        if (map == null || map.size() <= 0) {
            return;
        }
        for (fm fmVar : map.keySet()) {
            if (fmVar != null) {
                try {
                    fmVar.fm(context, intent, !z10, i10);
                } catch (Throwable unused) {
                }
            }
        }
    }

    public static int fm(Context context, long j10) {
        long jElapsedRealtime = SystemClock.elapsedRealtime();
        if (ajl + j10 <= jElapsedRealtime) {
            return ro(context);
        }
        if (jnr == -1) {
            return ro(context);
        }
        if (jElapsedRealtime - ajl >= wsy) {
            ro(context, (Intent) null, false, false);
        }
        return jnr;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static int ro(Context context) {
        jnr = lb(context);
        ajl = SystemClock.elapsedRealtime();
        return jnr;
    }

    public static void fm(fm fmVar, Context context) {
        if (fmVar == null) {
            return;
        }
        if (!yz.get()) {
            try {
                context.registerReceiver(new ro(), new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
                yz.set(true);
            } catch (Throwable unused) {
            }
        }
        Map<fm, Object> map = ro;
        map.put(fmVar, fm);
        f6688lb.set(map.size());
    }

    public static void fm(fm fmVar) {
        if (fmVar == null) {
            return;
        }
        Map<fm, Object> map = ro;
        map.remove(fmVar);
        f6688lb.set(map.size());
    }
}
