use of com.fanap.podchat.util.NetworkUtils.NetworkStateReceiver 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();
}
}
Aggregations