OLD | NEW |
1 /* | 1 /* |
2 * libjingle | 2 * libjingle |
3 * Copyright 2015 Google Inc. | 3 * Copyright 2015 Google Inc. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are met: | 6 * modification, are permitted provided that the following conditions are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright notice, | 8 * 1. Redistributions of source code must retain the above copyright notice, |
9 * this list of conditions and the following disclaimer. | 9 * this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, | 10 * 2. Redistributions in binary form must reproduce the above copyright notice, |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 } | 147 } |
148 | 148 |
149 private void setAutoDetectConnectivityStateInternal(boolean shouldAutoDetect)
{ | 149 private void setAutoDetectConnectivityStateInternal(boolean shouldAutoDetect)
{ |
150 if (!shouldAutoDetect) { | 150 if (!shouldAutoDetect) { |
151 destroyAutoDetector(); | 151 destroyAutoDetector(); |
152 return; | 152 return; |
153 } | 153 } |
154 if (autoDetector == null) { | 154 if (autoDetector == null) { |
155 autoDetector = new NetworkMonitorAutoDetect( | 155 autoDetector = new NetworkMonitorAutoDetect( |
156 new NetworkMonitorAutoDetect.Observer() { | 156 new NetworkMonitorAutoDetect.Observer() { |
| 157 |
157 @Override | 158 @Override |
158 public void onConnectionTypeChanged(ConnectionType newConnectionType)
{ | 159 public void onConnectionTypeChanged(ConnectionType newConnectionType)
{ |
159 updateCurrentConnectionType(newConnectionType); | 160 updateCurrentConnectionType(newConnectionType); |
160 } | 161 } |
| 162 |
161 @Override | 163 @Override |
162 public void onNetworkConnect(NetworkInformation networkInfo) { | 164 public void onNetworkConnect(NetworkInformation networkInfo) { |
163 updateNetworkInformation(networkInfo); | 165 notifyObserversOfNetworkConnect(networkInfo); |
| 166 } |
| 167 |
| 168 @Override |
| 169 public void onNetworkDisconnect(int networkHandle) { |
| 170 notifyObserversOfNetworkDisconnect(networkHandle); |
164 } | 171 } |
165 }, | 172 }, |
166 applicationContext); | 173 applicationContext); |
167 final NetworkMonitorAutoDetect.NetworkState networkState = | 174 final NetworkMonitorAutoDetect.NetworkState networkState = |
168 autoDetector.getCurrentNetworkState(); | 175 autoDetector.getCurrentNetworkState(); |
169 updateCurrentConnectionType(autoDetector.getConnectionType(networkState)); | 176 updateCurrentConnectionType(autoDetector.getConnectionType(networkState)); |
| 177 updateActiveNetworkList(); |
170 } | 178 } |
171 } | 179 } |
172 | 180 |
173 private void updateCurrentConnectionType(ConnectionType newConnectionType) { | 181 private void updateCurrentConnectionType(ConnectionType newConnectionType) { |
174 currentConnectionType = newConnectionType; | 182 currentConnectionType = newConnectionType; |
175 notifyObserversOfConnectionTypeChange(newConnectionType); | 183 notifyObserversOfConnectionTypeChange(newConnectionType); |
176 } | 184 } |
177 | 185 |
178 /** | 186 /** |
179 * Alerts all observers of a connection change. | 187 * Alerts all observers of a connection change. |
180 */ | 188 */ |
181 private void notifyObserversOfConnectionTypeChange(ConnectionType newConnectio
nType) { | 189 private void notifyObserversOfConnectionTypeChange(ConnectionType newConnectio
nType) { |
182 for (long nativeObserver : nativeNetworkObservers) { | 190 for (long nativeObserver : nativeNetworkObservers) { |
183 nativeNotifyConnectionTypeChanged(nativeObserver); | 191 nativeNotifyConnectionTypeChanged(nativeObserver); |
184 } | 192 } |
185 for (NetworkObserver observer : networkObservers) { | 193 for (NetworkObserver observer : networkObservers) { |
186 observer.onConnectionTypeChanged(newConnectionType); | 194 observer.onConnectionTypeChanged(newConnectionType); |
187 } | 195 } |
188 } | 196 } |
189 | 197 |
190 private void updateNetworkInformation(NetworkInformation networkInfo) { | 198 private void notifyObserversOfNetworkConnect(NetworkInformation networkInfo) { |
191 for (long nativeObserver : nativeNetworkObservers) { | 199 for (long nativeObserver : nativeNetworkObservers) { |
192 nativeNotifyOfNetworkConnect(nativeObserver, networkInfo); | 200 nativeNotifyOfNetworkConnect(nativeObserver, networkInfo); |
193 } | 201 } |
194 } | 202 } |
195 | 203 |
| 204 private void notifyObserversOfNetworkDisconnect(int networkHandle) { |
| 205 for (long nativeObserver : nativeNetworkObservers) { |
| 206 nativeNotifyOfNetworkDisconnect(nativeObserver, networkHandle); |
| 207 } |
| 208 } |
| 209 |
| 210 private void updateActiveNetworkList() { |
| 211 NetworkInformation[] networkInfos = autoDetector.getActiveNetworkList(); |
| 212 if (networkInfos.length == 0) { |
| 213 return; |
| 214 } |
| 215 for (long nativeObserver : nativeNetworkObservers) { |
| 216 nativeNotifyOfActiveNetworkList(nativeObserver, networkInfos); |
| 217 } |
| 218 } |
| 219 |
196 /** | 220 /** |
197 * Adds an observer for any connection type changes. | 221 * Adds an observer for any connection type changes. |
198 */ | 222 */ |
199 public static void addNetworkObserver(NetworkObserver observer) { | 223 public static void addNetworkObserver(NetworkObserver observer) { |
200 getInstance().addNetworkObserverInternal(observer); | 224 getInstance().addNetworkObserverInternal(observer); |
201 } | 225 } |
202 | 226 |
203 private void addNetworkObserverInternal(NetworkObserver observer) { | 227 private void addNetworkObserverInternal(NetworkObserver observer) { |
204 networkObservers.add(observer); | 228 networkObservers.add(observer); |
205 } | 229 } |
(...skipping 11 matching lines...) Expand all Loading... |
217 | 241 |
218 /** | 242 /** |
219 * Checks if there currently is connectivity. | 243 * Checks if there currently is connectivity. |
220 */ | 244 */ |
221 public static boolean isOnline() { | 245 public static boolean isOnline() { |
222 ConnectionType connectionType = getInstance().getCurrentConnectionType(); | 246 ConnectionType connectionType = getInstance().getCurrentConnectionType(); |
223 return connectionType != ConnectionType.CONNECTION_UNKNOWN | 247 return connectionType != ConnectionType.CONNECTION_UNKNOWN |
224 && connectionType != ConnectionType.CONNECTION_NONE; | 248 && connectionType != ConnectionType.CONNECTION_NONE; |
225 } | 249 } |
226 | 250 |
227 private native long nativeCreateNetworkMonitor(); | |
228 | |
229 private native void nativeNotifyConnectionTypeChanged(long nativePtr); | 251 private native void nativeNotifyConnectionTypeChanged(long nativePtr); |
230 | |
231 private native void nativeNotifyOfNetworkConnect(long nativePtr, NetworkInform
ation networkInfo); | 252 private native void nativeNotifyOfNetworkConnect(long nativePtr, NetworkInform
ation networkInfo); |
| 253 private native void nativeNotifyOfNetworkDisconnect(long nativePtr, int networ
kHandle); |
| 254 private native void nativeNotifyOfActiveNetworkList(long nativePtr, |
| 255 NetworkInformation[] netwo
rkInfos); |
232 | 256 |
233 // For testing only. | 257 // For testing only. |
234 static void resetInstanceForTests(Context context) { | 258 static void resetInstanceForTests(Context context) { |
235 instance = new NetworkMonitor(context); | 259 instance = new NetworkMonitor(context); |
236 } | 260 } |
237 | 261 |
238 // For testing only. | 262 // For testing only. |
239 public static NetworkMonitorAutoDetect getAutoDetectorForTest() { | 263 public static NetworkMonitorAutoDetect getAutoDetectorForTest() { |
240 return getInstance().autoDetector; | 264 return getInstance().autoDetector; |
241 } | 265 } |
242 } | 266 } |
OLD | NEW |