package com.blstream.studybox;

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 java.util.ArrayList;
import java.util.List;

/**
 * Class for monitoring network connection status.
 * In every activity should be:
 *  public ConnectionStatusReceiver connectionStatusReceiver = new ConnectionStatusReceiver();
 * and
 *  registerReceiver(connectionStatusReceiver, ConnectionStatusReceiver.filter);
 * in OnResume() method, and
 *  unregisterReceiver(connectionStatusReceiver);
 * in OnPause() method.
 *
 * This receiver provides also interface "ConnectionStatusReceiverListener" with two methods :
 * - networkAvailable()
 * - networkUnavailable()
 * To use this you need to implement "ConnectionStatusReceiver.ConnectionStatusReceiverListener" in
 * your activity and call "connectionStatusReceiver.addListener(this);", and
 * "connectionStatusReceiver.removeListener(this);".
 */

public class ConnectionStatusReceiver extends BroadcastReceiver {
    private final static  String ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
    public final static IntentFilter filter = new IntentFilter(ACTION);

    private SnackbarManager snackbarManager = null;

    private ConnectivityManager connectivityManager;
    private List<ConnectionStatusReceiverListener> listeners;
    private boolean connected;

    public ConnectionStatusReceiver() {
        listeners = new ArrayList<>();
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null || intent.getExtras() == null)
            return;

        connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        if (checkStateChanged(connectivityManager))
            notifyStateToAll();

        notifySnackbarManager(context);
    }

    private boolean checkStateChanged(ConnectivityManager connectivityManager) {
        boolean prev = connected;
        NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
        connected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
        return prev != connected;
    }

    private void notifyStateToAll() {
        for (ConnectionStatusReceiverListener listener : listeners)
            notifyState(listener);
    }

    private void notifyState(ConnectionStatusReceiverListener listener) {
        if (listener != null) {
            if (connected) {
                listener.networkAvailable();
            } else {
                listener.networkUnavailable();
            }
        }
    }

    private void notifySnackbarManager(Context context) {
        if (connected)
            notifySnackbarClassNetworkAvailable();
        else
            notifySnackbarClassNetworkUnavailable(context);
    }

    private void notifySnackbarClassNetworkAvailable() {
        if (snackbarManager != null) {
            snackbarManager.networkAvailable();
        }
    }

    private void notifySnackbarClassNetworkUnavailable(Context context) {
        if (snackbarManager == null) {
            snackbarManager = new SnackbarManager(context);
        }
        snackbarManager.networkUnavailable();
    }

    public void addListener(ConnectionStatusReceiverListener listener) {
        listeners.add(listener);
        notifyState(listener);
    }

    public void removeListener(ConnectionStatusReceiverListener listener) {
        listeners.remove(listener);
    }

    public interface ConnectionStatusReceiverListener {
        void networkAvailable();

        void networkUnavailable();
    }
}
@@ -3718,9 +3718,77 @@
 ;%0A    %7D%0A
-%7D
+%0A    public boolean isConnected()%7B%0A        return connected;%0A    %7D%0A%7D%0A

add isConnected() method