View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14   */
15  
16  package org.fourthline.cling.android;
17  
18  import android.content.Context;
19  import android.net.ConnectivityManager;
20  import android.net.NetworkInfo;
21  import org.fourthline.cling.model.ModelUtil;
22  
23  import java.util.logging.Logger;
24  
25  /**
26   * Android network helpers.
27   *
28   * @author Michael Pujos
29   */
30  public class NetworkUtils {
31  
32      final private static Logger log = Logger.getLogger(NetworkUtils.class.getName());
33  
34      static public NetworkInfo getConnectedNetworkInfo(Context context) {
35  
36          ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
37  
38          NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
39          if (networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected()) {
40              return networkInfo;
41          }
42  
43          networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
44          if (networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected()) return networkInfo;
45  
46          networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
47          if (networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected()) return networkInfo;
48  
49          networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIMAX);
50          if (networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected()) return networkInfo;
51  
52          networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
53          if (networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected()) return networkInfo;
54  
55          log.info("Could not find any connected network...");
56  
57          return null;
58      }
59  
60      static public boolean isEthernet(NetworkInfo networkInfo) {
61          return isNetworkType(networkInfo, ConnectivityManager.TYPE_ETHERNET);
62      }
63  
64      static public boolean isWifi(NetworkInfo networkInfo) {
65          return isNetworkType(networkInfo, ConnectivityManager.TYPE_WIFI) || ModelUtil.ANDROID_EMULATOR;
66      }
67  
68      static public boolean isMobile(NetworkInfo networkInfo) {
69          return isNetworkType(networkInfo, ConnectivityManager.TYPE_MOBILE) || isNetworkType(networkInfo, ConnectivityManager.TYPE_WIMAX);
70      }
71  
72      static public boolean isNetworkType(NetworkInfo networkInfo, int type) {
73          return networkInfo != null && networkInfo.getType() == type;
74      }
75  
76      static public boolean isSSDPAwareNetwork(NetworkInfo networkInfo) {
77          return isWifi(networkInfo) || isEthernet(networkInfo);
78      }
79  
80  }