Chromium Code Reviews| 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 11 matching lines...) Expand all Loading... | |
| 22 import android.net.Network; | 22 import android.net.Network; |
| 23 import android.net.NetworkCapabilities; | 23 import android.net.NetworkCapabilities; |
| 24 import android.net.NetworkInfo; | 24 import android.net.NetworkInfo; |
| 25 import android.net.NetworkRequest; | 25 import android.net.NetworkRequest; |
| 26 import android.net.wifi.WifiInfo; | 26 import android.net.wifi.WifiInfo; |
| 27 import android.net.wifi.WifiManager; | 27 import android.net.wifi.WifiManager; |
| 28 import android.os.Build; | 28 import android.os.Build; |
| 29 import android.telephony.TelephonyManager; | 29 import android.telephony.TelephonyManager; |
| 30 import android.util.Log; | 30 import android.util.Log; |
| 31 | 31 |
| 32 import org.chromium.base.ApplicationState; | |
| 33 import org.chromium.base.ApplicationStatus; | |
| 32 import org.chromium.base.ThreadUtils; | 34 import org.chromium.base.ThreadUtils; |
| 33 import org.chromium.base.VisibleForTesting; | 35 import org.chromium.base.VisibleForTesting; |
| 34 import org.chromium.base.metrics.RecordHistogram; | 36 import org.chromium.base.metrics.RecordHistogram; |
| 35 import org.chromium.net.ConnectionType.ConnectionTypeEnum; | 37 import org.chromium.net.ConnectionType.ConnectionTypeEnum; |
| 36 | 38 |
| 37 import java.io.IOException; | 39 import java.io.IOException; |
| 38 import java.util.Arrays; | 40 import java.util.Arrays; |
| 39 | 41 |
| 40 import javax.annotation.concurrent.GuardedBy; | 42 import javax.annotation.concurrent.GuardedBy; |
| 41 | 43 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 90 (ConnectivityManager) context.getSystemService(Context.CONNE CTIVITY_SERVICE); | 92 (ConnectivityManager) context.getSystemService(Context.CONNE CTIVITY_SERVICE); |
| 91 } | 93 } |
| 92 | 94 |
| 93 // For testing. | 95 // For testing. |
| 94 ConnectivityManagerDelegate() { | 96 ConnectivityManagerDelegate() { |
| 95 // All the methods below should be overridden. | 97 // All the methods below should be overridden. |
| 96 mConnectivityManager = null; | 98 mConnectivityManager = null; |
| 97 } | 99 } |
| 98 | 100 |
| 99 /** | 101 /** |
| 102 * @return the info of the network that is available to this app. | |
| 103 */ | |
| 104 @TargetApi(Build.VERSION_CODES.M) | |
| 105 NetworkInfo getNetworkInfo() { | |
| 106 NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo( ); | |
| 107 if (networkInfo == null) { | |
| 108 return null; | |
| 109 } | |
| 110 | |
| 111 if (networkInfo.isConnected()) { | |
| 112 return networkInfo; | |
| 113 } | |
| 114 | |
| 115 // Current active network is not connected. Check if the network con nectivity is blocked | |
|
pauljensen
2017/01/07 02:32:38
Can we combine and simplify this comment and the o
tbansal1
2017/01/09 18:11:14
Done.
| |
| 116 // even though there is a network available that has Internet capabi lity. This may | |
| 117 // happen because Android may not have updated the network access pe rmissions for | |
| 118 // this app. See https://crbug.com/677365 for more details. | |
| 119 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { | |
| 120 // https://crbug.com/677365 affects only Marshmallow and higher versions on which | |
| 121 // data access may be restricted to the background apps. | |
| 122 return null; | |
| 123 } | |
| 124 | |
| 125 if (networkInfo.getDetailedState() != NetworkInfo.DetailedState.BLOC KED) { | |
| 126 // Network state is not blocked which implies that network acces s is | |
| 127 // unavailable (not just blocked to this app). | |
| 128 return null; | |
| 129 } | |
| 130 | |
| 131 if (ApplicationStatus.getStateForApplication() | |
| 132 != ApplicationState.HAS_RUNNING_ACTIVITIES) { | |
| 133 // The app is not in the foreground. | |
| 134 return null; | |
| 135 } | |
| 136 | |
| 137 // Network corresponding to networkInfo is BLOCKED which implies tha t it is blocked for | |
| 138 // this app. However, since this app is in foreground, so the networ k access must be | |
| 139 // restored to this app even though Android may not have updated the network access | |
| 140 // permissions for this app. See https://crbug.com/677365 for more d etails. | |
| 141 return networkInfo; | |
| 142 } | |
| 143 | |
| 144 /** | |
| 100 * Returns connection type and status information about the current | 145 * Returns connection type and status information about the current |
| 101 * default network. | 146 * default network. |
| 102 */ | 147 */ |
| 103 NetworkState getNetworkState(WifiManagerDelegate wifiManagerDelegate) { | 148 NetworkState getNetworkState(WifiManagerDelegate wifiManagerDelegate) { |
| 104 final NetworkInfo networkInfo = mConnectivityManager.getActiveNetwor kInfo(); | 149 final NetworkInfo networkInfo = getNetworkInfo(); |
| 105 if (networkInfo == null || !networkInfo.isConnected()) { | 150 if (networkInfo == null) { |
| 106 return new NetworkState(false, -1, -1, null); | 151 return new NetworkState(false, -1, -1, null); |
| 107 } | 152 } |
| 108 // If Wifi, then fetch SSID also | 153 // If Wifi, then fetch SSID also |
| 109 if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) { | 154 if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) { |
| 110 // Since Android 4.2 the SSID can be retrieved from NetworkInfo. getExtraInfo(). | 155 // Since Android 4.2 the SSID can be retrieved from NetworkInfo. getExtraInfo(). |
| 111 if (networkInfo.getExtraInfo() != null && !"".equals(networkInfo .getExtraInfo())) { | 156 if (networkInfo.getExtraInfo() != null && !"".equals(networkInfo .getExtraInfo())) { |
| 112 return new NetworkState(true, networkInfo.getType(), network Info.getSubtype(), | 157 return new NetworkState(true, networkInfo.getType(), network Info.getSubtype(), |
| 113 networkInfo.getExtraInfo()); | 158 networkInfo.getExtraInfo()); |
| 114 } | 159 } |
| 115 // Fetch WiFi SSID directly from WifiManagerDelegate if not in N etworkInfo. | 160 // Fetch WiFi SSID directly from WifiManagerDelegate if not in N etworkInfo. |
| (...skipping 871 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 987 return network.getNetworkHandle(); | 1032 return network.getNetworkHandle(); |
| 988 } else { | 1033 } else { |
| 989 // NOTE(pauljensen): This depends on Android framework implementatio n details. These | 1034 // NOTE(pauljensen): This depends on Android framework implementatio n details. These |
| 990 // details cannot change because Lollipop is long since released. | 1035 // details cannot change because Lollipop is long since released. |
| 991 // NetIDs are only 16-bit so use parseInt. This function returns a l ong because | 1036 // NetIDs are only 16-bit so use parseInt. This function returns a l ong because |
| 992 // getNetworkHandle() returns a long. | 1037 // getNetworkHandle() returns a long. |
| 993 return Integer.parseInt(network.toString()); | 1038 return Integer.parseInt(network.toString()); |
| 994 } | 1039 } |
| 995 } | 1040 } |
| 996 } | 1041 } |
| OLD | NEW |