content: /**
   * This file was modified by Amazon:
   * Copyright 2015-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * A copy of the License is located at
   *
   *     http://aws.amazon.com/apache2.0/
   *
   * or in the "license" file accompanying this file. This file is distributed
   * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
   * express or implied. See the License for the specific language governing
   * permissions and limitations under the License.
   */
  /*
   * Copyright (c) 2014, Nexage, Inc. All rights reserved.
   * Copyright (C) 2016 Amazon Inc.
   *
   * Provided under BSD-3 license as follows:
   *
   * Redistribution and use in source and binary forms, with or without modification, are permitted
   * provided that the following conditions are met:
   *
   * Redistributions of source code must retain the above copyright notice, this list of conditions
   * and the following disclaimer.
   *
   * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
   *  and the following
   * disclaimer in the documentation and/or other materials provided with the distribution.
   *
   * Neither the name of Nexage nor the names of its contributors may be used to endorse or promote
   * products derived from this software without specific prior written permission.
   *
   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
   * IMPLIED WARRANTIES,
   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
   * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
   * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   */
  package com.amazon.android.ads.vast.util;
  
  import android.content.Context;
  import android.net.ConnectivityManager;
  import android.net.Network;
  import android.net.NetworkInfo;
  import android.util.Log;
  
  public class NetworkTools {
  
      private static final String TAG = HttpTools.class.getName();
  
      /**
       * This method returns true if it's connected to Internet
       */
      public static boolean connectedToInternet(Context context) {
  
          Log.d(TAG, "Testing connectivity:");
  
          ConnectivityManager cm = (ConnectivityManager) context
                  .getSystemService(Context.CONNECTIVITY_SERVICE);
  
          Network[] networks = cm.getAllNetworks();
  
          NetworkInfo wifiNetwork = getNetworkInfo(cm, networks, ConnectivityManager.TYPE_WIFI);
          if (wifiNetwork != null && wifiNetwork.isConnected()) {
              Log.d(TAG, "Connected to Internet");
              return true;
          }
  
          NetworkInfo mobileNetwork = getNetworkInfo(cm, networks, ConnectivityManager.TYPE_MOBILE);
          if (mobileNetwork != null && mobileNetwork.isConnected()) {
              Log.d(TAG, "Connected to Internet");
              return true;
          }
  
          NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
          if (activeNetwork != null && activeNetwork.isConnected()) {
              Log.d(TAG, "Connected to Internet");
              return true;
          }
          Log.d(TAG, "No Internet connection");
          return false;
      }
  
      /**
       * Get the NetworkInfo for the connection with the given connectionType
       *
       * @param cm             ConnectivityMonitor for the context
       * @param networks       List of
       * @param connectionType Type of network connection, e.g., ConnectivityManager.TYPE_WIFI, etc
       * @return The NetworkInfo for the given connection type, or null if the connection type does
       * not exist
       */
      private static NetworkInfo getNetworkInfo(ConnectivityManager cm, Network[] networks,
                                                int connectionType) {
  
          for (Network network : networks) {
              NetworkInfo networkInfo = cm.getNetworkInfo(network);
              if (networkInfo.getType() == connectionType) {
                  return networkInfo;
              }
          }
          return null;
      }
  }
  