1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.dialer.common;
18 
19 import android.content.Context;
20 import android.content.pm.PackageManager;
21 import android.net.ConnectivityManager;
22 import android.net.NetworkInfo;
23 import android.net.wifi.WifiInfo;
24 import android.net.wifi.WifiManager;
25 import android.support.annotation.Nullable;
26 import android.support.annotation.RequiresPermission;
27 import android.support.annotation.StringDef;
28 import android.telephony.TelephonyManager;
29 import android.text.TextUtils;
30 import java.lang.annotation.Retention;
31 import java.lang.annotation.RetentionPolicy;
32 import java.util.Objects;
33 
34 /** Utility class for dealing with network */
35 public class NetworkUtil {
36 
37   /* Returns the current network type. */
38   @RequiresPermission("android.permission.ACCESS_NETWORK_STATE")
39   @NetworkType
getCurrentNetworkType(@ullable Context context)40   public static String getCurrentNetworkType(@Nullable Context context) {
41     if (context == null) {
42       return NetworkType.NONE;
43     }
44     ConnectivityManager connectivityManager =
45         (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
46     return getNetworkType(connectivityManager.getActiveNetworkInfo());
47   }
48 
49   /* Returns the current network info. */
50   @Nullable
51   @RequiresPermission("android.permission.ACCESS_NETWORK_STATE")
getCurrentNetworkInfo(@ullable Context context)52   public static NetworkInfo getCurrentNetworkInfo(@Nullable Context context) {
53     if (context == null) {
54       return null;
55     }
56     ConnectivityManager connectivityManager =
57         (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
58     return connectivityManager.getActiveNetworkInfo();
59   }
60 
61   /**
62    * Returns the current network type as a string. For mobile network types the subtype name of the
63    * network is appended.
64    */
65   @RequiresPermission("android.permission.ACCESS_NETWORK_STATE")
getCurrentNetworkTypeName(@ullable Context context)66   public static String getCurrentNetworkTypeName(@Nullable Context context) {
67     if (context == null) {
68       return NetworkType.NONE;
69     }
70     ConnectivityManager connectivityManager =
71         (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
72     NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
73     @NetworkType String networkType = getNetworkType(netInfo);
74     if (isNetworkTypeMobile(networkType)) {
75       return networkType + " (" + netInfo.getSubtypeName() + ")";
76     }
77     return networkType;
78   }
79 
80   @NetworkType
getNetworkType(@ullable NetworkInfo netInfo)81   public static String getNetworkType(@Nullable NetworkInfo netInfo) {
82     if (netInfo == null || !netInfo.isConnected()) {
83       return NetworkType.NONE;
84     }
85     switch (netInfo.getType()) {
86       case ConnectivityManager.TYPE_WIFI:
87         return NetworkType.WIFI;
88       case ConnectivityManager.TYPE_MOBILE:
89         return getMobileNetworkType(netInfo.getSubtype());
90       default:
91         return NetworkType.UNKNOWN;
92     }
93   }
94 
isNetworkTypeMobile(@etworkType String networkType)95   public static boolean isNetworkTypeMobile(@NetworkType String networkType) {
96     return Objects.equals(networkType, NetworkType.MOBILE_2G)
97         || Objects.equals(networkType, NetworkType.MOBILE_3G)
98         || Objects.equals(networkType, NetworkType.MOBILE_4G);
99   }
100 
101   @RequiresPermission("android.permission.ACCESS_NETWORK_STATE")
getCurrentNetworkName(Context context)102   public static String getCurrentNetworkName(Context context) {
103     @NetworkType String networkType = getCurrentNetworkType(context);
104     switch (networkType) {
105       case NetworkType.WIFI:
106         return getWifiNetworkName(context);
107       case NetworkType.MOBILE_2G:
108       case NetworkType.MOBILE_3G:
109       case NetworkType.MOBILE_4G:
110       case NetworkType.MOBILE_UNKNOWN:
111         return getMobileNetworkName(context);
112       default:
113         return "";
114     }
115   }
116 
getWifiNetworkName(Context context)117   private static String getWifiNetworkName(Context context) {
118     WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
119     String name = null;
120     if (context.checkSelfPermission("android.permission.ACCESS_WIFI_STATE")
121         == PackageManager.PERMISSION_GRANTED) {
122       //noinspection MissingPermission
123       WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
124       if (wifiInfo == null) {
125         return "";
126       }
127       name = wifiInfo.getSSID();
128     }
129     return TextUtils.isEmpty(name)
130         ? context.getString(R.string.network_name_wifi)
131         : name.replaceAll("\"", "");
132   }
133 
getMobileNetworkName(Context context)134   private static String getMobileNetworkName(Context context) {
135     TelephonyManager telephonyMgr =
136         (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
137     String name = telephonyMgr.getNetworkOperatorName();
138     return TextUtils.isEmpty(name)
139         ? context.getString(R.string.network_name_mobile)
140         : name.replaceAll("\"", "");
141   }
142 
143   @NetworkType
getMobileNetworkType(int networkSubtype)144   private static String getMobileNetworkType(int networkSubtype) {
145     switch (networkSubtype) {
146       case TelephonyManager.NETWORK_TYPE_1xRTT:
147       case TelephonyManager.NETWORK_TYPE_CDMA:
148       case TelephonyManager.NETWORK_TYPE_EDGE:
149       case TelephonyManager.NETWORK_TYPE_GPRS:
150       case TelephonyManager.NETWORK_TYPE_IDEN:
151         return NetworkType.MOBILE_2G;
152       case TelephonyManager.NETWORK_TYPE_EHRPD:
153       case TelephonyManager.NETWORK_TYPE_EVDO_0:
154       case TelephonyManager.NETWORK_TYPE_EVDO_A:
155       case TelephonyManager.NETWORK_TYPE_EVDO_B:
156       case TelephonyManager.NETWORK_TYPE_HSDPA:
157       case TelephonyManager.NETWORK_TYPE_HSPA:
158       case TelephonyManager.NETWORK_TYPE_HSPAP:
159       case TelephonyManager.NETWORK_TYPE_HSUPA:
160       case TelephonyManager.NETWORK_TYPE_UMTS:
161         return NetworkType.MOBILE_3G;
162       case TelephonyManager.NETWORK_TYPE_LTE:
163         return NetworkType.MOBILE_4G;
164       default:
165         return NetworkType.MOBILE_UNKNOWN;
166     }
167   }
168 
169   /** Network types. */
170   @Retention(RetentionPolicy.SOURCE)
171   @StringDef(
172     value = {
173       NetworkType.NONE,
174       NetworkType.WIFI,
175       NetworkType.MOBILE_2G,
176       NetworkType.MOBILE_3G,
177       NetworkType.MOBILE_4G,
178       NetworkType.MOBILE_UNKNOWN,
179       NetworkType.UNKNOWN
180     }
181   )
182   public @interface NetworkType {
183 
184     String NONE = "NONE";
185     String WIFI = "WIFI";
186     String MOBILE_2G = "MOBILE_2G";
187     String MOBILE_3G = "MOBILE_3G";
188     String MOBILE_4G = "MOBILE_4G";
189     String MOBILE_UNKNOWN = "MOBILE_UNKNOWN";
190     String UNKNOWN = "UNKNOWN";
191   }
192 }
193