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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 } | 79 } |
80 | 80 |
81 // WiFi SSID, always non-null to facilitate .equals() | 81 // WiFi SSID, always non-null to facilitate .equals() |
82 public String getWifiSsid() { | 82 public String getWifiSsid() { |
83 return mWifiSsid; | 83 return mWifiSsid; |
84 } | 84 } |
85 } | 85 } |
86 | 86 |
87 /** Queries the ConnectivityManager for information about the current connec
tion. */ | 87 /** Queries the ConnectivityManager for information about the current connec
tion. */ |
88 static class ConnectivityManagerDelegate { | 88 static class ConnectivityManagerDelegate { |
89 private static final int NETWORK_INFO_DISCONNECTED = 0; | |
90 private static final int NETWORK_INFO_CONNECTED = 1; | |
91 private static final int NETWORK_INFO_ANDROID_API_LEVEL_TOO_OLD_FOR_UNBL
OCKING = 2; | |
92 private static final int NETWORK_INFO_NOT_BLOCKED = 3; | |
93 private static final int NETWORK_INFO_APP_NOT_IN_FOREGROUND = 4; | |
94 private static final int NETWORK_INFO_UNBLOCKED = 5; | |
95 private static final int NETWORK_INFO_BOUNDARY = 6; | |
96 | |
97 private final ConnectivityManager mConnectivityManager; | 89 private final ConnectivityManager mConnectivityManager; |
98 | 90 |
99 ConnectivityManagerDelegate(Context context) { | 91 ConnectivityManagerDelegate(Context context) { |
100 mConnectivityManager = | 92 mConnectivityManager = |
101 (ConnectivityManager) context.getSystemService(Context.CONNE
CTIVITY_SERVICE); | 93 (ConnectivityManager) context.getSystemService(Context.CONNE
CTIVITY_SERVICE); |
102 } | 94 } |
103 | 95 |
104 // For testing. | 96 // For testing. |
105 ConnectivityManagerDelegate() { | 97 ConnectivityManagerDelegate() { |
106 // All the methods below should be overridden. | 98 // All the methods below should be overridden. |
107 mConnectivityManager = null; | 99 mConnectivityManager = null; |
108 } | 100 } |
109 | 101 |
110 /** | 102 /** |
111 * @return the info of the network that is available to this app. | 103 * @return the info of the network that is available to this app. |
112 */ | 104 */ |
113 @TargetApi(Build.VERSION_CODES.LOLLIPOP) | 105 @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
114 private NetworkInfo getActiveNetworkInfo() { | 106 private NetworkInfo getActiveNetworkInfo() { |
115 final NetworkInfo networkInfo = mConnectivityManager.getActiveNetwor
kInfo(); | 107 final NetworkInfo networkInfo = mConnectivityManager.getActiveNetwor
kInfo(); |
116 if (networkInfo == null) { | 108 if (networkInfo == null) { |
117 recordGetActiveNetworkInfoResult(NETWORK_INFO_DISCONNECTED); | |
118 return null; | 109 return null; |
119 } | 110 } |
120 | 111 |
121 if (networkInfo.isConnected()) { | 112 if (networkInfo.isConnected()) { |
122 recordGetActiveNetworkInfoResult(NETWORK_INFO_CONNECTED); | |
123 return networkInfo; | 113 return networkInfo; |
124 } | 114 } |
125 | 115 |
126 // If |networkInfo| is BLOCKED, but the app is in the foreground, th
en it's likely that | 116 // If |networkInfo| is BLOCKED, but the app is in the foreground, th
en it's likely that |
127 // Android hasn't finished updating the network access permissions a
s BLOCKED is only | 117 // Android hasn't finished updating the network access permissions a
s BLOCKED is only |
128 // meant for apps in the background. See https://crbug.com/677365 f
or more details. | 118 // meant for apps in the background. See https://crbug.com/677365 f
or more details. |
129 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { | 119 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { |
130 // https://crbug.com/677365 primarily affects only Lollipop and
higher versions. | 120 // https://crbug.com/677365 primarily affects only Lollipop and
higher versions. |
131 recordGetActiveNetworkInfoResult( | |
132 NETWORK_INFO_ANDROID_API_LEVEL_TOO_OLD_FOR_UNBLOCKING); | |
133 return null; | 121 return null; |
134 } | 122 } |
135 | 123 |
136 if (networkInfo.getDetailedState() != NetworkInfo.DetailedState.BLOC
KED) { | 124 if (networkInfo.getDetailedState() != NetworkInfo.DetailedState.BLOC
KED) { |
137 // Network state is not blocked which implies that network acces
s is | 125 // Network state is not blocked which implies that network acces
s is |
138 // unavailable (not just blocked to this app). | 126 // unavailable (not just blocked to this app). |
139 recordGetActiveNetworkInfoResult(NETWORK_INFO_NOT_BLOCKED); | |
140 return null; | 127 return null; |
141 } | 128 } |
142 | 129 |
143 if (ApplicationStatus.getStateForApplication() | 130 if (ApplicationStatus.getStateForApplication() |
144 != ApplicationState.HAS_RUNNING_ACTIVITIES) { | 131 != ApplicationState.HAS_RUNNING_ACTIVITIES) { |
145 // The app is not in the foreground. | 132 // The app is not in the foreground. |
146 recordGetActiveNetworkInfoResult(NETWORK_INFO_APP_NOT_IN_FOREGRO
UND); | |
147 return null; | 133 return null; |
148 } | 134 } |
149 recordGetActiveNetworkInfoResult(NETWORK_INFO_UNBLOCKED); | |
150 return networkInfo; | 135 return networkInfo; |
151 } | 136 } |
152 | 137 |
153 /** | 138 /** |
154 * Records the result of querying the network info of the current active
network. | |
155 * @param result Result of querying the network info of the current acti
ve network. | |
156 */ | |
157 private static void recordGetActiveNetworkInfoResult(int result) { | |
158 assert result >= 0 && result < NETWORK_INFO_BOUNDARY; | |
159 RecordHistogram.recordEnumeratedHistogram( | |
160 "NCN.GetActiveNetworkInfoResult", result, NETWORK_INFO_BOUND
ARY); | |
161 } | |
162 | |
163 /** | |
164 * Returns connection type and status information about the current | 139 * Returns connection type and status information about the current |
165 * default network. | 140 * default network. |
166 */ | 141 */ |
167 NetworkState getNetworkState(WifiManagerDelegate wifiManagerDelegate) { | 142 NetworkState getNetworkState(WifiManagerDelegate wifiManagerDelegate) { |
168 final NetworkInfo networkInfo = getActiveNetworkInfo(); | 143 final NetworkInfo networkInfo = getActiveNetworkInfo(); |
169 if (networkInfo == null) { | 144 if (networkInfo == null) { |
170 return new NetworkState(false, -1, -1, null); | 145 return new NetworkState(false, -1, -1, null); |
171 } | 146 } |
172 // If Wifi, then fetch SSID also | 147 // If Wifi, then fetch SSID also |
173 if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) { | 148 if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) { |
(...skipping 913 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1087 return network.getNetworkHandle(); | 1062 return network.getNetworkHandle(); |
1088 } else { | 1063 } else { |
1089 // NOTE(pauljensen): This depends on Android framework implementatio
n details. These | 1064 // NOTE(pauljensen): This depends on Android framework implementatio
n details. These |
1090 // details cannot change because Lollipop is long since released. | 1065 // details cannot change because Lollipop is long since released. |
1091 // NetIDs are only 16-bit so use parseInt. This function returns a l
ong because | 1066 // NetIDs are only 16-bit so use parseInt. This function returns a l
ong because |
1092 // getNetworkHandle() returns a long. | 1067 // getNetworkHandle() returns a long. |
1093 return Integer.parseInt(network.toString()); | 1068 return Integer.parseInt(network.toString()); |
1094 } | 1069 } |
1095 } | 1070 } |
1096 } | 1071 } |
OLD | NEW |