This source file includes following definitions.
- JNINamespace
 
- onConnectionTypeChanged
 
- init
 
- isInitialized
 
- resetInstanceForTests
 
- getCurrentConnectionType
 
- addNativeObserver
 
- removeNativeObserver
 
- getInstance
 
- setAutoDetectConnectivityState
 
- destroyAutoDetector
 
- setAutoDetectConnectivityStateInternal
 
- forceConnectivityState
 
- forceConnectivityStateInternal
 
- updateCurrentConnectionType
 
- notifyObserversOfConnectionTypeChange
 
- addConnectionTypeObserver
 
- addConnectionTypeObserverInternal
 
- removeConnectionTypeObserver
 
- removeConnectionTypeObserverInternal
 
- NativeClassQualifiedName
 
- nativeNotifyConnectionTypeChanged
 
- getAutoDetectorForTest
 
- isOnline
 
package org.chromium.net;
import android.content.Context;
import org.chromium.base.CalledByNative;
import org.chromium.base.JNINamespace;
import org.chromium.base.NativeClassQualifiedName;
import org.chromium.base.ObserverList;
import java.util.ArrayList;
@JNINamespace("net")
public class NetworkChangeNotifier {
    
    public interface ConnectionTypeObserver {
        public void onConnectionTypeChanged(int connectionType);
    }
    
    public static final int CONNECTION_UNKNOWN = 0;
    public static final int CONNECTION_ETHERNET = 1;
    public static final int CONNECTION_WIFI = 2;
    public static final int CONNECTION_2G = 3;
    public static final int CONNECTION_3G = 4;
    public static final int CONNECTION_4G = 5;
    public static final int CONNECTION_NONE = 6;
    private final Context mContext;
    private final ArrayList<Long> mNativeChangeNotifiers;
    private final ObserverList<ConnectionTypeObserver> mConnectionTypeObservers;
    private NetworkChangeNotifierAutoDetect mAutoDetector;
    private int mCurrentConnectionType = CONNECTION_UNKNOWN;
    private static NetworkChangeNotifier sInstance;
    private NetworkChangeNotifier(Context context) {
        mContext = context.getApplicationContext();
        mNativeChangeNotifiers = new ArrayList<Long>();
        mConnectionTypeObservers = new ObserverList<ConnectionTypeObserver>();
    }
    
    @CalledByNative
    public static NetworkChangeNotifier init(Context context) {
        if (sInstance == null) {
            sInstance = new NetworkChangeNotifier(context);
        }
        return sInstance;
    }
    public static boolean isInitialized() {
        return sInstance != null;
    }
    static void resetInstanceForTests(Context context) {
        sInstance = new NetworkChangeNotifier(context);
    }
    @CalledByNative
    public int getCurrentConnectionType() {
        return mCurrentConnectionType;
    }
    
    @CalledByNative
    public void addNativeObserver(long nativeChangeNotifier) {
        mNativeChangeNotifiers.add(nativeChangeNotifier);
    }
    
    @CalledByNative
    public void removeNativeObserver(long nativeChangeNotifier) {
        mNativeChangeNotifiers.remove(nativeChangeNotifier);
    }
    
    public static NetworkChangeNotifier getInstance() {
        assert sInstance != null;
        return sInstance;
    }
    
    public static void setAutoDetectConnectivityState(boolean shouldAutoDetect) {
        getInstance().setAutoDetectConnectivityStateInternal(shouldAutoDetect);
    }
    private void destroyAutoDetector() {
        if (mAutoDetector != null) {
            mAutoDetector.destroy();
            mAutoDetector = null;
        }
    }
    private void setAutoDetectConnectivityStateInternal(boolean shouldAutoDetect) {
        if (shouldAutoDetect) {
            if (mAutoDetector == null) {
                mAutoDetector = new NetworkChangeNotifierAutoDetect(
                    new NetworkChangeNotifierAutoDetect.Observer() {
                        @Override
                        public void onConnectionTypeChanged(int newConnectionType) {
                            updateCurrentConnectionType(newConnectionType);
                        }
                    },
                    mContext);
                mCurrentConnectionType = mAutoDetector.getCurrentConnectionType();
            }
        } else {
            destroyAutoDetector();
        }
    }
    
    @CalledByNative
    public static void forceConnectivityState(boolean networkAvailable) {
        setAutoDetectConnectivityState(false);
        getInstance().forceConnectivityStateInternal(networkAvailable);
    }
    private void forceConnectivityStateInternal(boolean forceOnline) {
        boolean connectionCurrentlyExists = mCurrentConnectionType != CONNECTION_NONE;
        if (connectionCurrentlyExists != forceOnline) {
            updateCurrentConnectionType(forceOnline ? CONNECTION_UNKNOWN : CONNECTION_NONE);
        }
    }
    private void updateCurrentConnectionType(int newConnectionType) {
        mCurrentConnectionType = newConnectionType;
        notifyObserversOfConnectionTypeChange(newConnectionType);
    }
    
    void notifyObserversOfConnectionTypeChange(int newConnectionType) {
        for (Long nativeChangeNotifier : mNativeChangeNotifiers) {
            nativeNotifyConnectionTypeChanged(nativeChangeNotifier, newConnectionType);
        }
        for (ConnectionTypeObserver observer : mConnectionTypeObservers) {
            observer.onConnectionTypeChanged(newConnectionType);
        }
    }
    
    public static void addConnectionTypeObserver(ConnectionTypeObserver observer) {
        getInstance().addConnectionTypeObserverInternal(observer);
    }
    private void addConnectionTypeObserverInternal(ConnectionTypeObserver observer) {
        mConnectionTypeObservers.addObserver(observer);
    }
    
    public static void removeConnectionTypeObserver(ConnectionTypeObserver observer) {
        getInstance().removeConnectionTypeObserverInternal(observer);
    }
    private void removeConnectionTypeObserverInternal(ConnectionTypeObserver observer) {
        mConnectionTypeObservers.removeObserver(observer);
    }
    @NativeClassQualifiedName("NetworkChangeNotifierDelegateAndroid")
    private native void nativeNotifyConnectionTypeChanged(long nativePtr, int newConnectionType);
    
    public static NetworkChangeNotifierAutoDetect getAutoDetectorForTest() {
        return getInstance().mAutoDetector;
    }
    
    public static boolean isOnline() {
        int connectionType = getInstance().getCurrentConnectionType();
        return connectionType != CONNECTION_UNKNOWN && connectionType != CONNECTION_NONE;
    }
}