package com.namelessdev.mpdroid.tools;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkHelper {

    public static final int MAX_RETRY = 5;

    public static boolean isNetworkConnected(Context context) {
        return isNetworkConnected(context, new int[0]);
    }

    public static Boolean isLocalNetworkConnected(Context context) {
        return isNetworkConnected(context, new int[]{ConnectivityManager.TYPE_WIFI, ConnectivityManager.TYPE_ETHERNET});
    }

    public static Boolean isNetworkConnected(Context context, int... allowedNetworkTypes) {

        for (int attempt = 0; attempt < MAX_RETRY; attempt++) {
            if (innerIsNetworkConnected(context, allowedNetworkTypes)) {
                return true;
            } else {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    //Nothing to do
                }
            }
        }
        return false;
    }

    public static Boolean innerIsNetworkConnected(Context context, int... allowedNetworkTypes) {

        NetworkInfo networkInfo;
        boolean networkConnected = false;
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        if (cm == null) {
            networkConnected = false;
        } else {
            networkInfo = cm.getActiveNetworkInfo();
            if (networkInfo == null) {
                networkConnected = false;
            } else if (!networkInfo.isAvailable() || !networkInfo.isConnected()) {
                networkConnected = false;
            } else if (allowedNetworkTypes.length == 0) {
                networkConnected = true;
            } else {
                for (int networkType = 0; networkType < allowedNetworkTypes.length && !networkConnected; networkType++) {
                    if (allowedNetworkTypes[networkType] == networkInfo.getType()) {
                        networkConnected = true;
                    }
                }
            }
        }
        return networkConnected;
    }
}

package com.namelessdev.mpdroid.tools;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkHelper {

	public static boolean isNetworkConnected(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (cm.getActiveNetworkInfo() == null)
			return false;
		return (cm.getActiveNetworkInfo().isAvailable() && cm
				.getActiveNetworkInfo().isConnected());
	}

	public static Boolean isLocalNetworkConnected(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (cm == null) {
			return false;
		}
		NetworkInfo netWorkinfo = cm.getActiveNetworkInfo();
		return (netWorkinfo != null
				&& (netWorkinfo.getType() == ConnectivityManager.TYPE_WIFI || netWorkinfo
						.getType() == ConnectivityManager.TYPE_ETHERNET) && netWorkinfo
					.isConnected());
	}
}

Revert "Improve robustness to temporary network problems."
Revert "Improve robustness to temporary network problems."

Revert this since it does not seem to improve the lost connection management
and may have introduced a regression : see #345
This reverts commit ab94d7adb4798d3e8e0bbaeb8340e1580cf7f8a8.
