OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.net; | 5 package org.chromium.net; |
6 | 6 |
7 import static android.net.ConnectivityManager.TYPE_VPN; | 7 import static android.net.ConnectivityManager.TYPE_VPN; |
8 import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET; | 8 import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET; |
9 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VPN; | 9 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VPN; |
10 import static android.net.NetworkCapabilities.TRANSPORT_VPN; | 10 import static android.net.NetworkCapabilities.TRANSPORT_VPN; |
(...skipping 12 matching lines...) Expand all Loading... | |
23 import android.net.NetworkInfo; | 23 import android.net.NetworkInfo; |
24 import android.net.NetworkRequest; | 24 import android.net.NetworkRequest; |
25 import android.net.wifi.WifiInfo; | 25 import android.net.wifi.WifiInfo; |
26 import android.net.wifi.WifiManager; | 26 import android.net.wifi.WifiManager; |
27 import android.os.Build; | 27 import android.os.Build; |
28 import android.telephony.TelephonyManager; | 28 import android.telephony.TelephonyManager; |
29 import android.util.Log; | 29 import android.util.Log; |
30 | 30 |
31 import org.chromium.base.ThreadUtils; | 31 import org.chromium.base.ThreadUtils; |
32 import org.chromium.base.VisibleForTesting; | 32 import org.chromium.base.VisibleForTesting; |
33 import org.chromium.base.metrics.RecordHistogram; | |
33 | 34 |
34 import java.io.IOException; | 35 import java.io.IOException; |
35 import java.util.Arrays; | 36 import java.util.Arrays; |
36 | 37 |
37 /** | 38 /** |
38 * Used by the NetworkChangeNotifier to listens to platform changes in connectiv ity. | 39 * Used by the NetworkChangeNotifier to listens to platform changes in connectiv ity. |
39 * Note that use of this class requires that the app have the platform | 40 * Note that use of this class requires that the app have the platform |
40 * ACCESS_NETWORK_STATE permission. | 41 * ACCESS_NETWORK_STATE permission. |
41 */ | 42 */ |
42 public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver { | 43 public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
80 } | 81 } |
81 | 82 |
82 /** | 83 /** |
83 * Returns connection type and status information about the current | 84 * Returns connection type and status information about the current |
84 * default network. | 85 * default network. |
85 */ | 86 */ |
86 NetworkState getNetworkState() { | 87 NetworkState getNetworkState() { |
87 return getNetworkState(mConnectivityManager.getActiveNetworkInfo()); | 88 return getNetworkState(mConnectivityManager.getActiveNetworkInfo()); |
88 } | 89 } |
89 | 90 |
91 // Fetch NetworkInfo and record UMA for NPEs. | |
xunjieli
2016/06/14 13:55:06
nit: s/Fetch/Fetches. Maybe also spell out NPE.
pauljensen
2016/06/14 13:59:03
Done.
| |
92 private NetworkInfo getNetworkInfo(Network network) { | |
93 try { | |
94 NetworkInfo networkInfo = mConnectivityManager.getNetworkInfo(ne twork); | |
95 RecordHistogram.recordBooleanHistogram("NCN.getNetInfo1stSuccess ", true); | |
96 return networkInfo; | |
97 } catch (NullPointerException firstException) { | |
98 RecordHistogram.recordBooleanHistogram("NCN.getNetInfo1stSuccess ", false); | |
99 try { | |
100 NetworkInfo networkInfo = mConnectivityManager.getNetworkInf o(network); | |
xunjieli
2016/06/14 13:55:06
Why do we do getNetworkInfo twice?
pauljensen
2016/06/14 13:59:03
To see if trying again might be a viable solution.
xunjieli
2016/06/14 14:10:50
I see. Could you make a quick comment here and bel
pauljensen
2016/06/14 14:59:41
Done.
| |
101 RecordHistogram.recordBooleanHistogram("NCN.getNetInfo2ndSuc cess", true); | |
102 return networkInfo; | |
103 } catch (NullPointerException secondException) { | |
104 RecordHistogram.recordBooleanHistogram("NCN.getNetInfo2ndSuc cess", false); | |
105 throw secondException; | |
106 } | |
107 } | |
108 } | |
109 | |
90 /** | 110 /** |
91 * Returns connection type and status information about |network|. | 111 * Returns connection type and status information about |network|. |
92 * Only callable on Lollipop and newer releases. | 112 * Only callable on Lollipop and newer releases. |
93 */ | 113 */ |
94 @TargetApi(Build.VERSION_CODES.LOLLIPOP) | 114 @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
95 NetworkState getNetworkState(Network network) { | 115 NetworkState getNetworkState(Network network) { |
96 final NetworkInfo networkInfo = mConnectivityManager.getNetworkInfo( network); | 116 final NetworkInfo networkInfo = getNetworkInfo(network); |
97 if (networkInfo != null && networkInfo.getType() == TYPE_VPN) { | 117 if (networkInfo != null && networkInfo.getType() == TYPE_VPN) { |
98 // When a VPN is in place the underlying network type can be que ried via | 118 // When a VPN is in place the underlying network type can be que ried via |
99 // getActiveNeworkInfo() thanks to | 119 // getActiveNeworkInfo() thanks to |
100 // https://android.googlesource.com/platform/frameworks/base/+/d 6a7980d | 120 // https://android.googlesource.com/platform/frameworks/base/+/d 6a7980d |
101 return getNetworkState(); | 121 return getNetworkState(); |
102 } | 122 } |
103 return getNetworkState(networkInfo); | 123 return getNetworkState(networkInfo); |
104 } | 124 } |
105 | 125 |
106 /** | 126 /** |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
184 // API to return the NetworkInfo for the default network. To | 204 // API to return the NetworkInfo for the default network. To |
185 // determine the default network one can find the network with | 205 // determine the default network one can find the network with |
186 // type matching that of the default network. | 206 // type matching that of the default network. |
187 final NetworkInfo defaultNetworkInfo = mConnectivityManager.getActiv eNetworkInfo(); | 207 final NetworkInfo defaultNetworkInfo = mConnectivityManager.getActiv eNetworkInfo(); |
188 if (defaultNetworkInfo == null) { | 208 if (defaultNetworkInfo == null) { |
189 return NetId.INVALID; | 209 return NetId.INVALID; |
190 } | 210 } |
191 final Network[] networks = getAllNetworksFiltered(this, null); | 211 final Network[] networks = getAllNetworksFiltered(this, null); |
192 int defaultNetId = NetId.INVALID; | 212 int defaultNetId = NetId.INVALID; |
193 for (Network network : networks) { | 213 for (Network network : networks) { |
194 final NetworkInfo networkInfo = mConnectivityManager.getNetworkI nfo(network); | 214 final NetworkInfo networkInfo = getNetworkInfo(network); |
195 if (networkInfo != null | 215 if (networkInfo != null |
196 && (networkInfo.getType() == defaultNetworkInfo.getType( ) | 216 && (networkInfo.getType() == defaultNetworkInfo.getType( ) |
197 // getActiveNetworkInfo() will not return TYP E_VPN types due to | 217 // getActiveNetworkInfo() will not return TYP E_VPN types due to |
198 // https://android.googlesource.com/platform/ frameworks/base/+/d6a7980d | 218 // https://android.googlesource.com/platform/ frameworks/base/+/d6a7980d |
199 // so networkInfo.getType() can't be matched against | 219 // so networkInfo.getType() can't be matched against |
200 // defaultNetworkInfo.getType() but networkIn fo.getType() should | 220 // defaultNetworkInfo.getType() but networkIn fo.getType() should |
201 // be TYPE_VPN. In the case of a VPN, getAllN etworks() will have | 221 // be TYPE_VPN. In the case of a VPN, getAllN etworks() will have |
202 // returned just this VPN if it applies. | 222 // returned just this VPN if it applies. |
203 || networkInfo.getType() == TYPE_VPN)) { | 223 || networkInfo.getType() == TYPE_VPN)) { |
204 // There should not be multiple connected networks of the | 224 // There should not be multiple connected networks of the |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
248 if (wifiInfo != null) { | 268 if (wifiInfo != null) { |
249 final String ssid = wifiInfo.getSSID(); | 269 final String ssid = wifiInfo.getSSID(); |
250 if (ssid != null) { | 270 if (ssid != null) { |
251 return ssid; | 271 return ssid; |
252 } | 272 } |
253 } | 273 } |
254 } | 274 } |
255 return ""; | 275 return ""; |
256 } | 276 } |
257 | 277 |
278 // Fetch WifiInfo and record UMA for NPEs. | |
279 private WifiInfo getWifiInfo() { | |
280 try { | |
281 WifiInfo wifiInfo = mWifiManager.getConnectionInfo(); | |
282 RecordHistogram.recordBooleanHistogram("NCN.getWifiInfo1stSucces s", true); | |
283 return wifiInfo; | |
284 } catch (NullPointerException firstException) { | |
285 RecordHistogram.recordBooleanHistogram("NCN.getWifiInfo1stSucces s", false); | |
286 try { | |
287 WifiInfo wifiInfo = mWifiManager.getConnectionInfo(); | |
288 RecordHistogram.recordBooleanHistogram("NCN.getWifiInfo2ndSu ccess", true); | |
289 return wifiInfo; | |
290 } catch (NullPointerException secondException) { | |
291 RecordHistogram.recordBooleanHistogram("NCN.getWifiInfo2ndSu ccess", false); | |
292 throw secondException; | |
293 } | |
294 } | |
295 } | |
296 | |
258 /* | 297 /* |
259 * Requires ACCESS_WIFI_STATE permission to get the real link speed, els e returns | 298 * Requires ACCESS_WIFI_STATE permission to get the real link speed, els e returns |
260 * UNKNOWN_LINK_SPEED. | 299 * UNKNOWN_LINK_SPEED. |
261 */ | 300 */ |
262 int getLinkSpeedInMbps() { | 301 int getLinkSpeedInMbps() { |
263 if (!mHasWifiPermission || mWifiManager == null) return UNKNOWN_LINK _SPEED; | 302 if (!mHasWifiPermission || mWifiManager == null) return UNKNOWN_LINK _SPEED; |
264 final WifiInfo wifiInfo = mWifiManager.getConnectionInfo(); | 303 final WifiInfo wifiInfo = getWifiInfo(); |
265 if (wifiInfo == null) return UNKNOWN_LINK_SPEED; | 304 if (wifiInfo == null) return UNKNOWN_LINK_SPEED; |
266 | 305 |
267 // wifiInfo.getLinkSpeed returns the current wifi linkspeed, which c an change even | 306 // wifiInfo.getLinkSpeed returns the current wifi linkspeed, which c an change even |
268 // though the connection type hasn't changed. | 307 // though the connection type hasn't changed. |
269 return wifiInfo.getLinkSpeed(); | 308 return wifiInfo.getLinkSpeed(); |
270 } | 309 } |
271 | 310 |
272 boolean getHasWifiPermission() { | 311 boolean getHasWifiPermission() { |
273 return mHasWifiPermission; | 312 return mHasWifiPermission; |
274 } | 313 } |
(...skipping 612 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
887 */ | 926 */ |
888 @TargetApi(Build.VERSION_CODES.LOLLIPOP) | 927 @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
889 @VisibleForTesting | 928 @VisibleForTesting |
890 static int networkToNetId(Network network) { | 929 static int networkToNetId(Network network) { |
891 // NOTE(pauljensen): This depends on Android framework implementation de tails. | 930 // NOTE(pauljensen): This depends on Android framework implementation de tails. |
892 // Fortunately this functionality is unlikely to ever change. | 931 // Fortunately this functionality is unlikely to ever change. |
893 // TODO(pauljensen): When we update to Android M SDK, use Network.getNet workHandle(). | 932 // TODO(pauljensen): When we update to Android M SDK, use Network.getNet workHandle(). |
894 return Integer.parseInt(network.toString()); | 933 return Integer.parseInt(network.toString()); |
895 } | 934 } |
896 } | 935 } |
OLD | NEW |