| 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; |
| 33 |
| 34 import org.webrtc.Logging; |
| 32 | 35 |
| 33 import android.content.Context; | 36 import android.content.Context; |
| 34 import android.util.Log; | |
| 35 | 37 |
| 36 import java.util.ArrayList; | 38 import java.util.ArrayList; |
| 37 | 39 |
| 38 /** | 40 /** |
| 39 * Borrowed from Chromium's src/net/android/java/src/org/chromium/net/NetworkCha
ngeNotifier.java | 41 * Borrowed from Chromium's src/net/android/java/src/org/chromium/net/NetworkCha
ngeNotifier.java |
| 40 * | 42 * |
| 41 * Triggers updates to the underlying network state from OS networking events. | 43 * Triggers updates to the underlying network state from OS networking events. |
| 42 * | 44 * |
| 43 * WARNING: This class is not thread-safe. | 45 * WARNING: This class is not thread-safe. |
| 44 */ | 46 */ |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 } | 112 } |
| 111 | 113 |
| 112 private static void assertIsTrue(boolean condition) { | 114 private static void assertIsTrue(boolean condition) { |
| 113 if (!condition) { | 115 if (!condition) { |
| 114 throw new AssertionError("Expected to be true"); | 116 throw new AssertionError("Expected to be true"); |
| 115 } | 117 } |
| 116 } | 118 } |
| 117 | 119 |
| 118 // Called by the native code. | 120 // Called by the native code. |
| 119 private void startMonitoring(long nativeObserver) { | 121 private void startMonitoring(long nativeObserver) { |
| 120 Log.d(TAG, "Start monitoring from native observer " + nativeObserver); | 122 Logging.d(TAG, "Start monitoring from native observer " + nativeObserver); |
| 121 nativeNetworkObservers.add(nativeObserver); | 123 nativeNetworkObservers.add(nativeObserver); |
| 122 setAutoDetectConnectivityStateInternal(true); | 124 setAutoDetectConnectivityStateInternal(true); |
| 123 } | 125 } |
| 124 | 126 |
| 125 // Called by the native code. | 127 // Called by the native code. |
| 126 private void stopMonitoring(long nativeObserver) { | 128 private void stopMonitoring(long nativeObserver) { |
| 127 Log.d(TAG, "Stop monitoring from native observer " + nativeObserver); | 129 Logging.d(TAG, "Stop monitoring from native observer " + nativeObserver); |
| 128 setAutoDetectConnectivityStateInternal(false); | 130 setAutoDetectConnectivityStateInternal(false); |
| 129 nativeNetworkObservers.remove(nativeObserver); | 131 nativeNetworkObservers.remove(nativeObserver); |
| 130 } | 132 } |
| 131 | 133 |
| 132 private ConnectionType getCurrentConnectionType() { | 134 private ConnectionType getCurrentConnectionType() { |
| 133 return currentConnectionType; | 135 return currentConnectionType; |
| 134 } | 136 } |
| 135 | 137 |
| 136 private int getCurrentDefaultNetId() { | 138 private int getCurrentDefaultNetId() { |
| 137 return autoDetector == null ? INVALID_NET_ID : autoDetector.getDefaultNetId(
); | 139 return autoDetector == null ? INVALID_NET_ID : autoDetector.getDefaultNetId(
); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 149 destroyAutoDetector(); | 151 destroyAutoDetector(); |
| 150 return; | 152 return; |
| 151 } | 153 } |
| 152 if (autoDetector == null) { | 154 if (autoDetector == null) { |
| 153 autoDetector = new NetworkMonitorAutoDetect( | 155 autoDetector = new NetworkMonitorAutoDetect( |
| 154 new NetworkMonitorAutoDetect.Observer() { | 156 new NetworkMonitorAutoDetect.Observer() { |
| 155 @Override | 157 @Override |
| 156 public void onConnectionTypeChanged(ConnectionType newConnectionType)
{ | 158 public void onConnectionTypeChanged(ConnectionType newConnectionType)
{ |
| 157 updateCurrentConnectionType(newConnectionType); | 159 updateCurrentConnectionType(newConnectionType); |
| 158 } | 160 } |
| 161 @Override |
| 162 public void onNetworkConnect(NetworkInformation networkInfo) { |
| 163 updateNetworkInformation(networkInfo); |
| 164 } |
| 159 }, | 165 }, |
| 160 applicationContext); | 166 applicationContext); |
| 161 final NetworkMonitorAutoDetect.NetworkState networkState = | 167 final NetworkMonitorAutoDetect.NetworkState networkState = |
| 162 autoDetector.getCurrentNetworkState(); | 168 autoDetector.getCurrentNetworkState(); |
| 163 updateCurrentConnectionType(autoDetector.getCurrentConnectionType(networkS
tate)); | 169 updateCurrentConnectionType(autoDetector.getConnectionType(networkState)); |
| 164 } | 170 } |
| 165 } | 171 } |
| 166 | 172 |
| 167 private void updateCurrentConnectionType(ConnectionType newConnectionType) { | 173 private void updateCurrentConnectionType(ConnectionType newConnectionType) { |
| 168 currentConnectionType = newConnectionType; | 174 currentConnectionType = newConnectionType; |
| 169 notifyObserversOfConnectionTypeChange(newConnectionType); | 175 notifyObserversOfConnectionTypeChange(newConnectionType); |
| 170 } | 176 } |
| 171 | 177 |
| 172 /** | 178 /** |
| 173 * Alerts all observers of a connection change. | 179 * Alerts all observers of a connection change. |
| 174 */ | 180 */ |
| 175 private void notifyObserversOfConnectionTypeChange(ConnectionType newConnectio
nType) { | 181 private void notifyObserversOfConnectionTypeChange(ConnectionType newConnectio
nType) { |
| 176 for (long nativeObserver : nativeNetworkObservers) { | 182 for (long nativeObserver : nativeNetworkObservers) { |
| 177 nativeNotifyConnectionTypeChanged(nativeObserver); | 183 nativeNotifyConnectionTypeChanged(nativeObserver); |
| 178 } | 184 } |
| 179 for (NetworkObserver observer : networkObservers) { | 185 for (NetworkObserver observer : networkObservers) { |
| 180 observer.onConnectionTypeChanged(newConnectionType); | 186 observer.onConnectionTypeChanged(newConnectionType); |
| 181 } | 187 } |
| 182 } | 188 } |
| 183 | 189 |
| 190 private void updateNetworkInformation(NetworkInformation networkInfo) { |
| 191 for (long nativeObserver : nativeNetworkObservers) { |
| 192 nativeNotifyOfNetworkConnect(nativeObserver, networkInfo); |
| 193 } |
| 194 } |
| 195 |
| 184 /** | 196 /** |
| 185 * Adds an observer for any connection type changes. | 197 * Adds an observer for any connection type changes. |
| 186 */ | 198 */ |
| 187 public static void addNetworkObserver(NetworkObserver observer) { | 199 public static void addNetworkObserver(NetworkObserver observer) { |
| 188 getInstance().addNetworkObserverInternal(observer); | 200 getInstance().addNetworkObserverInternal(observer); |
| 189 } | 201 } |
| 190 | 202 |
| 191 private void addNetworkObserverInternal(NetworkObserver observer) { | 203 private void addNetworkObserverInternal(NetworkObserver observer) { |
| 192 networkObservers.add(observer); | 204 networkObservers.add(observer); |
| 193 } | 205 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 209 public static boolean isOnline() { | 221 public static boolean isOnline() { |
| 210 ConnectionType connectionType = getInstance().getCurrentConnectionType(); | 222 ConnectionType connectionType = getInstance().getCurrentConnectionType(); |
| 211 return connectionType != ConnectionType.CONNECTION_UNKNOWN | 223 return connectionType != ConnectionType.CONNECTION_UNKNOWN |
| 212 && connectionType != ConnectionType.CONNECTION_NONE; | 224 && connectionType != ConnectionType.CONNECTION_NONE; |
| 213 } | 225 } |
| 214 | 226 |
| 215 private native long nativeCreateNetworkMonitor(); | 227 private native long nativeCreateNetworkMonitor(); |
| 216 | 228 |
| 217 private native void nativeNotifyConnectionTypeChanged(long nativePtr); | 229 private native void nativeNotifyConnectionTypeChanged(long nativePtr); |
| 218 | 230 |
| 231 private native void nativeNotifyOfNetworkConnect(long nativePtr, NetworkInform
ation networkInfo); |
| 232 |
| 219 // For testing only. | 233 // For testing only. |
| 220 static void resetInstanceForTests(Context context) { | 234 static void resetInstanceForTests(Context context) { |
| 221 instance = new NetworkMonitor(context); | 235 instance = new NetworkMonitor(context); |
| 222 } | 236 } |
| 223 | 237 |
| 224 // For testing only. | 238 // For testing only. |
| 225 public static NetworkMonitorAutoDetect getAutoDetectorForTest() { | 239 public static NetworkMonitorAutoDetect getAutoDetectorForTest() { |
| 226 return getInstance().autoDetector; | 240 return getInstance().autoDetector; |
| 227 } | 241 } |
| 228 } | 242 } |
| OLD | NEW |