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;
    }
}

@@ -172,56 +172,9 @@
  %7B%0A%0A
-    public static final int MAX_RETRY = 5;%0A%0A    
+%09
 publ
@@ -233,771 +233,281 @@
 ) %7B%0A
-        return isNetworkConnected(context, new int%5B0%5D);%0A    %7D%0A%0A    public static Boolean isLocalNetworkConnected(Context context) %7B%0A        return isNetworkConnected(context, new int%5B%5D%7BConnectivityManager.TYPE_WIFI, ConnectivityManager.TYPE_ETHERNET%7D);%0A    %7D%0A%0A    public static Boolean isNetworkConnected(Context context, int... allowedNetworkTypes) %7B%0A%0A        for (int attempt = 0; attempt %3C MAX_RETRY; attempt++) %7B%0A            if (innerIsNetworkConnected(context, allowedNetworkTypes)) %7B%0A                return true;%0A            %7D else %7B%0A                try %7B%0A                    Thread.sleep(500);%0A                %7D catch (InterruptedException e) %7B%0A                    //Nothing to do%0A                %7D%0A            %7D%0A        %7D%0A        return false;%0A    %7D%0A%0A    
+%09%09ConnectivityManager cm = (ConnectivityManager) context%0A%09%09%09%09.getSystemService(Context.CONNECTIVITY_SERVICE);%0A%09%09if (cm.getActiveNetworkInfo() == null)%0A%09%09%09return false;%0A%09%09return (cm.getActiveNetworkInfo().isAvailable() && cm%0A%09%09%09%09.getActiveNetworkInfo().isConnected());%0A%09%7D%0A%0A%09
 publ
@@ -529,14 +529,14 @@
 an i
-nnerIs
+sLocal
 Netw
@@ -567,124 +567,14 @@
 text
-, int... allowedNetworkTypes) %7B%0A%0A        NetworkInfo networkInfo;%0A        boolean networkConnected = false;%0A        
+) %7B%0A%09%09
 Conn
@@ -624,32 +624,20 @@
 context%0A
-                
+%09%09%09%09
 .getSyst
@@ -677,25 +677,18 @@
 RVICE);%0A
-%0A        
+%09%09
 if (cm =
@@ -701,83 +701,51 @@
 ) %7B%0A
-            networkConnected = false;%0A        %7D else %7B%0A           
+%09%09%09return false;%0A%09%09%7D%0A%09%09NetworkInfo
  net
-workI
+Worki
 nfo 
@@ -777,691 +777,209 @@
 ();%0A
-            if
+%09%09return
  (net
-workI
+Worki
 nfo 
-=
+!
 = null
-) %7B%0A                networkConnected = false;%0A            %7D else if
+%0A%09%09%09%09&&
  (
-!
 net
-workInfo.isAvailable() %7C%7C !networkInfo.isConnected()) %7B%0A                networkConnected = false;%0A            %7D else if (allowedNetworkTypes.length == 0) %7B%0A                networkConnected = true;%0A            %7D else %7B%0A                for (int networkType = 0;
+Workinfo.getType() == ConnectivityManager.TYPE_WIFI %7C%7C
  net
-workType %3C allowedNetworkTypes.length && !networkConnected; networkType++) %7B%0A                    if (allowedNetworkTypes%5BnetworkType%5D ==
+Workinfo%0A%09%09%09%09%09%09.getType() == ConnectivityManager.TYPE_ETHERNET) &&
  net
-workInfo.getType()) %7B%0A                        networkConnected = true;%0A                    %7D%0A                %7D%0A            %7D%0A        %7D%0A        return networkConnected;%0A    
+Workinfo%0A%09%09%09%09%09.isConnected());%0A%09
 %7D%0A%7D%0A

Revert "Improve robustness to temporary network problems."