翰林优商.apk(点击下载) / NetworkMonitor.java


package com.kwad.sdk.core;

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 androidx.core.content.ContextCompat;
import com.kuaishou.weapon.p0.h;
import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public final class NetworkMonitor {
    private static volatile boolean aSn = false;
    private final List<WeakReference<a>> XC;
    private boolean aSo;
    private final BroadcastReceiver aSp;

    /* access modifiers changed from: package-private */
    public enum Holder {
        INSTANCE;
        
        private NetworkMonitor mInstance = new NetworkMonitor((byte) 0);

        private Holder() {
        }

        /* access modifiers changed from: package-private */
        public final NetworkMonitor getInstance() {
            return this.mInstance;
        }
    }

    public enum NetworkState {
        NETWORK_NONE,
        NETWORK_MOBILE,
        NETWORK_WIFI
    }

    public interface a {
        void a(NetworkState networkState);
    }

    private NetworkMonitor() {
        this.XC = Collections.synchronizedList(new LinkedList());
        this.aSo = false;
        this.aSp = new BroadcastReceiver() {
            /* class com.kwad.sdk.core.NetworkMonitor.AnonymousClass1 */

            public final void onReceive(Context context, Intent intent) {
                ConnectivityManager connectivityManager;
                NetworkMonitor networkMonitor;
                NetworkState networkState;
                try {
                    if ((ContextCompat.checkSelfPermission(context, h.b) == 0) && (connectivityManager = (ConnectivityManager) context.getSystemService("connectivity")) != null) {
                        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
                        if (activeNetworkInfo == null || !activeNetworkInfo.isConnected()) {
                            NetworkMonitor.this.b(NetworkState.NETWORK_NONE);
                            return;
                        }
                        if (1 == activeNetworkInfo.getType()) {
                            networkMonitor = NetworkMonitor.this;
                            networkState = NetworkState.NETWORK_WIFI;
                        } else if (activeNetworkInfo.getType() == 0) {
                            networkMonitor = NetworkMonitor.this;
                            networkState = NetworkState.NETWORK_MOBILE;
                        } else {
                            networkMonitor = NetworkMonitor.this;
                            networkState = NetworkState.NETWORK_NONE;
                        }
                        networkMonitor.b(networkState);
                    }
                } catch (Throwable th) {
                    th.printStackTrace();
                }
            }
        };
    }

    /* synthetic */ NetworkMonitor(byte b) {
        this();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void b(NetworkState networkState) {
        synchronized (this.XC) {
            Iterator<WeakReference<a>> it = this.XC.iterator();
            while (it.hasNext()) {
                WeakReference<a> next = it.next();
                if (next != null) {
                    a aVar = next.get();
                    if (aVar != null) {
                        aVar.a(networkState);
                    }
                }
                it.remove();
            }
        }
    }

    private synchronized void bf(Context context) {
        if (!aSn) {
            if (context != null) {
                context.getApplicationContext().registerReceiver(this.aSp, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
                aSn = true;
            }
        }
    }

    public static NetworkMonitor getInstance() {
        return Holder.INSTANCE.getInstance();
    }

    public final boolean Ku() {
        return !this.aSo;
    }

    public final void Kv() {
        this.aSo = true;
    }

    public final void a(Context context, a aVar) {
        bf(context);
        this.XC.add(new WeakReference<>(aVar));
    }

    public final void a(a aVar) {
        if (aVar != null) {
            synchronized (this.XC) {
                Iterator<WeakReference<a>> it = this.XC.iterator();
                while (true) {
                    if (!it.hasNext()) {
                        break;
                    }
                    WeakReference<a> next = it.next();
                    if (next != null) {
                        a aVar2 = next.get();
                        if (aVar2 != null) {
                            if (aVar == aVar2) {
                                it.remove();
                                break;
                            }
                        }
                    }
                    it.remove();
                }
            }
        }
    }
}