package com.ntels.onecable.admin.common.websocket;

import com.ntels.onecable.admin.common.util.Dlog;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v4.content.LocalBroadcastManager;

public class NetworkStateReceiver extends BroadcastReceiver {

    private static final String TAG = NetworkStateReceiver.class.getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent) {
        Dlog.i(TAG, "Network connectivity change");

        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        boolean networkIsOn = activeNetworkInfo != null && activeNetworkInfo.isConnected();

        Dlog.i(TAG, "Network connectivity change networkIsOn =" + networkIsOn);
        Intent broadcastIntent = new Intent(WebSocketService.ACTION_NETWORK_STATE_CHANGED);
        broadcastIntent.putExtra(WebSocketService.ACTION_NETWORK_STATE_CHANGED, networkIsOn);
        LocalBroadcastManager.getInstance(context).sendBroadcast(broadcastIntent);
    }
}