Search in sources :

Example 1 with NetworkPingSender

use of com.fanap.podchat.util.NetworkUtils.NetworkPingSender in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method enableNetworkStateListener.

private void enableNetworkStateListener() {
    if (networkStateReceiver == null) {
        networkStateReceiver = new NetworkStateReceiver();
        // it ping and check network availability
        pinger = new NetworkPingSender(context, new NetworkStateListener() {

            @Override
            public void networkAvailable() {
                tryToConnectOrReconnect();
            }

            @Override
            public void networkUnavailable() {
                closeSocketServer();
            }

            @Override
            public void sendPingToServer() {
                pingForCheckConnection();
            }

            @Override
            public void onConnectionLost() {
                chatState = CLOSED;
                scheduleForReconnect();
            }
        });
        // get default config for connection check if it wasn't set.
        if (networkStateConfig == null) {
            networkStateConfig = NetworkPingSender.NetworkStateConfig.getDefault();
        }
        networkStateReceiver.setHostName(networkStateConfig.getHostName());
        networkStateReceiver.setPort(networkStateConfig.getPort());
        networkStateReceiver.setTimeOut(networkStateConfig.getConnectTimeout());
        pinger.setConfig(networkStateConfig);
        pinger.setStateListener((Chat) this);
        // it listen to turning on and off wifi or mobile data and accessing to internet
        AtomicBoolean initState = new AtomicBoolean(true);
        networkStateReceiver.addListener(new NetworkStateListener() {

            @Override
            public void networkAvailable() {
                Log.i(TAG, "Network State Changed, Available");
                tryToConnectOrReconnect();
                pinger.startPing();
            }

            @Override
            public void networkUnavailable() {
                if (initState.get()) {
                    initState.set(false);
                    return;
                }
                Log.e(TAG, "Network State Changed, Unavailable");
                closeSocketServer();
            }
        });
        registerNetworkReceiver();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NetworkPingSender(com.fanap.podchat.util.NetworkUtils.NetworkPingSender) NetworkStateListener(com.fanap.podchat.util.NetworkUtils.NetworkStateListener) NetworkStateReceiver(com.fanap.podchat.util.NetworkUtils.NetworkStateReceiver)

Aggregations

NetworkPingSender (com.fanap.podchat.util.NetworkUtils.NetworkPingSender)1 NetworkStateListener (com.fanap.podchat.util.NetworkUtils.NetworkStateListener)1 NetworkStateReceiver (com.fanap.podchat.util.NetworkUtils.NetworkStateReceiver)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1