| 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 11 matching lines...) Expand all Loading... |
| 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 */ | 26 */ |
| 27 | 27 |
| 28 package org.webrtc; | 28 package org.webrtc; |
| 29 | 29 |
| 30 import static org.webrtc.NetworkMonitorAutoDetect.ConnectionType; | 30 import static org.webrtc.NetworkMonitorAutoDetect.ConnectionType; |
| 31 import static org.webrtc.NetworkMonitorAutoDetect.INVALID_NET_ID; | 31 import static org.webrtc.NetworkMonitorAutoDetect.INVALID_NET_ID; |
| 32 import static org.webrtc.NetworkMonitorAutoDetect.NetworkInformation; |
| 32 | 33 |
| 33 import android.content.Context; | 34 import android.content.Context; |
| 34 import android.util.Log; | 35 import android.util.Log; |
| 35 | 36 |
| 36 import java.util.ArrayList; | 37 import java.util.ArrayList; |
| 37 | 38 |
| 38 /** | 39 /** |
| 39 * Borrowed from Chromium's src/net/android/java/src/org/chromium/net/NetworkCha
ngeNotifier.java | 40 * Borrowed from Chromium's src/net/android/java/src/org/chromium/net/NetworkCha
ngeNotifier.java |
| 40 * | 41 * |
| 41 * Triggers updates to the underlying network state from OS networking events. | 42 * Triggers updates to the underlying network state from OS networking events. |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 destroyAutoDetector(); | 150 destroyAutoDetector(); |
| 150 return; | 151 return; |
| 151 } | 152 } |
| 152 if (autoDetector == null) { | 153 if (autoDetector == null) { |
| 153 autoDetector = new NetworkMonitorAutoDetect( | 154 autoDetector = new NetworkMonitorAutoDetect( |
| 154 new NetworkMonitorAutoDetect.Observer() { | 155 new NetworkMonitorAutoDetect.Observer() { |
| 155 @Override | 156 @Override |
| 156 public void onConnectionTypeChanged(ConnectionType newConnectionType)
{ | 157 public void onConnectionTypeChanged(ConnectionType newConnectionType)
{ |
| 157 updateCurrentConnectionType(newConnectionType); | 158 updateCurrentConnectionType(newConnectionType); |
| 158 } | 159 } |
| 160 public void onNetworkConnect(NetworkInformation networkInfo) { |
| 161 updateNetworkInformation(networkInfo); |
| 162 } |
| 159 }, | 163 }, |
| 160 applicationContext); | 164 applicationContext); |
| 161 final NetworkMonitorAutoDetect.NetworkState networkState = | 165 final NetworkMonitorAutoDetect.NetworkState networkState = |
| 162 autoDetector.getCurrentNetworkState(); | 166 autoDetector.getCurrentNetworkState(); |
| 163 updateCurrentConnectionType(autoDetector.getCurrentConnectionType(networkS
tate)); | 167 updateCurrentConnectionType(autoDetector.getConnectionType(networkState)); |
| 164 } | 168 } |
| 165 } | 169 } |
| 166 | 170 |
| 167 private void updateCurrentConnectionType(ConnectionType newConnectionType) { | 171 private void updateCurrentConnectionType(ConnectionType newConnectionType) { |
| 168 currentConnectionType = newConnectionType; | 172 currentConnectionType = newConnectionType; |
| 169 notifyObserversOfConnectionTypeChange(newConnectionType); | 173 notifyObserversOfConnectionTypeChange(newConnectionType); |
| 170 } | 174 } |
| 171 | 175 |
| 172 /** | 176 /** |
| 173 * Alerts all observers of a connection change. | 177 * Alerts all observers of a connection change. |
| 174 */ | 178 */ |
| 175 private void notifyObserversOfConnectionTypeChange(ConnectionType newConnectio
nType) { | 179 private void notifyObserversOfConnectionTypeChange(ConnectionType newConnectio
nType) { |
| 176 for (long nativeObserver : nativeNetworkObservers) { | 180 for (long nativeObserver : nativeNetworkObservers) { |
| 177 nativeNotifyConnectionTypeChanged(nativeObserver); | 181 nativeNotifyConnectionTypeChanged(nativeObserver); |
| 178 } | 182 } |
| 179 for (NetworkObserver observer : networkObservers) { | 183 for (NetworkObserver observer : networkObservers) { |
| 180 observer.onConnectionTypeChanged(newConnectionType); | 184 observer.onConnectionTypeChanged(newConnectionType); |
| 181 } | 185 } |
| 182 } | 186 } |
| 183 | 187 |
| 188 private void updateNetworkInformation(NetworkInformation networkInfo) { |
| 189 for (long nativeObserver : nativeNetworkObservers) { |
| 190 nativeNotifyOfNetworkConnect(nativeObserver, networkInfo); |
| 191 } |
| 192 } |
| 193 |
| 184 /** | 194 /** |
| 185 * Adds an observer for any connection type changes. | 195 * Adds an observer for any connection type changes. |
| 186 */ | 196 */ |
| 187 public static void addNetworkObserver(NetworkObserver observer) { | 197 public static void addNetworkObserver(NetworkObserver observer) { |
| 188 getInstance().addNetworkObserverInternal(observer); | 198 getInstance().addNetworkObserverInternal(observer); |
| 189 } | 199 } |
| 190 | 200 |
| 191 private void addNetworkObserverInternal(NetworkObserver observer) { | 201 private void addNetworkObserverInternal(NetworkObserver observer) { |
| 192 networkObservers.add(observer); | 202 networkObservers.add(observer); |
| 193 } | 203 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 209 public static boolean isOnline() { | 219 public static boolean isOnline() { |
| 210 ConnectionType connectionType = getInstance().getCurrentConnectionType(); | 220 ConnectionType connectionType = getInstance().getCurrentConnectionType(); |
| 211 return connectionType != ConnectionType.CONNECTION_UNKNOWN | 221 return connectionType != ConnectionType.CONNECTION_UNKNOWN |
| 212 && connectionType != ConnectionType.CONNECTION_NONE; | 222 && connectionType != ConnectionType.CONNECTION_NONE; |
| 213 } | 223 } |
| 214 | 224 |
| 215 private native long nativeCreateNetworkMonitor(); | 225 private native long nativeCreateNetworkMonitor(); |
| 216 | 226 |
| 217 private native void nativeNotifyConnectionTypeChanged(long nativePtr); | 227 private native void nativeNotifyConnectionTypeChanged(long nativePtr); |
| 218 | 228 |
| 229 private native void nativeNotifyOfNetworkConnect(long nativePtr, NetworkInform
ation networkInfo); |
| 230 |
| 219 // For testing only. | 231 // For testing only. |
| 220 static void resetInstanceForTests(Context context) { | 232 static void resetInstanceForTests(Context context) { |
| 221 instance = new NetworkMonitor(context); | 233 instance = new NetworkMonitor(context); |
| 222 } | 234 } |
| 223 | 235 |
| 224 // For testing only. | 236 // For testing only. |
| 225 public static NetworkMonitorAutoDetect getAutoDetectorForTest() { | 237 public static NetworkMonitorAutoDetect getAutoDetectorForTest() { |
| 226 return getInstance().autoDetector; | 238 return getInstance().autoDetector; |
| 227 } | 239 } |
| 228 } | 240 } |
| OLD | NEW |