Index: talk/app/webrtc/java/android/org/webrtc/NetworkChangeNotifier.java |
diff --git a/talk/app/webrtc/java/android/org/webrtc/NetworkChangeNotifier.java b/talk/app/webrtc/java/android/org/webrtc/NetworkChangeNotifier.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c9d36f67ad63e725907545265f2d154119f29027 |
--- /dev/null |
+++ b/talk/app/webrtc/java/android/org/webrtc/NetworkChangeNotifier.java |
@@ -0,0 +1,205 @@ |
+/* |
+ * libjingle |
+ * Copyright 2015 Google Inc. |
+ * |
+ * Redistribution and use in source and binary forms, with or without |
+ * modification, are permitted provided that the following conditions are met: |
+ * |
+ * 1. Redistributions of source code must retain the above copyright notice, |
+ * this list of conditions and the following disclaimer. |
+ * 2. Redistributions in binary form must reproduce the above copyright notice, |
+ * this list of conditions and the following disclaimer in the documentation |
+ * and/or other materials provided with the distribution. |
+ * 3. The name of the author may not be used to endorse or promote products |
+ * derived from this software without specific prior written permission. |
+ * |
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
+ * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+ */ |
+ |
+package org.webrtc; |
+ |
+import static org.webrtc.NetworkChangeNotifierAutoDetect.ConnectionType; |
+import static org.webrtc.NetworkChangeNotifierAutoDetect.INVALID_NET_ID; |
+ |
+import android.content.Context; |
+ |
+import java.util.ArrayList; |
+ |
+/** |
+ * Borrowed from Chromium's src/net/android/java/src/org/chromium/net/NetworkChangeNotifier.java |
+ * |
+ * Triggers updates to the underlying network state from OS networking events. |
+ * |
+ * WARNING: This class is not thread-safe. |
+ */ |
+public class NetworkChangeNotifier { |
+ /** |
+ * Alerted when the connection type of the network changes. |
+ * The alert is fired on the UI thread. |
+ */ |
+ public interface ConnectionTypeObserver { |
+ public void onConnectionTypeChanged(ConnectionType connectionType); |
+ } |
+ |
+ private static NetworkChangeNotifier instance; |
+ |
+ private final Context applicationContext; |
+ |
+ // A pointer to the native network change notifier, which receives notification |
+ // from this class. |
+ private final long nativeNetworkChangeNotifier; |
+ |
+ // Java side observers of the connection type changes. |
+ private final ArrayList<ConnectionTypeObserver> connectionTypeObservers; |
+ |
+ // Object that detects the connection type changes. |
+ private NetworkChangeNotifierAutoDetect autoDetector; |
+ |
+ private ConnectionType currentConnectionType = ConnectionType.CONNECTION_UNKNOWN; |
+ |
+ private NetworkChangeNotifier(Context context) { |
+ applicationContext = context.getApplicationContext(); |
+ |
+ nativeNetworkChangeNotifier = nativeCreateNetworkChangeNotifier(); |
+ connectionTypeObservers = new ArrayList<ConnectionTypeObserver>(); |
+ } |
+ |
+ /** |
+ * Initializes the singleton once. |
+ */ |
+ public static NetworkChangeNotifier init(Context context) { |
+ if (!isInitialized()) { |
+ instance = new NetworkChangeNotifier(context); |
+ } |
+ return instance; |
+ } |
+ |
+ public static boolean isInitialized() { |
+ return instance != null; |
+ } |
+ |
+ /** |
+ * Returns the singleton instance. |
+ */ |
+ public static NetworkChangeNotifier getInstance() { |
+ return instance; |
+ } |
+ |
+ /** |
+ * Enables auto detection of the current network state based on notifications from the system. |
+ * Note that passing true here requires the embedding app have the platform ACCESS_NETWORK_STATE |
+ * permission. |
+ * |
+ * @param shouldAutoDetect true if the NetworkChangeNotifier should listen for system changes in |
+ * network connectivity. |
+ */ |
+ public static void setAutoDetectConnectivityState(boolean shouldAutoDetect) { |
+ getInstance().setAutoDetectConnectivityStateInternal(shouldAutoDetect); |
+ } |
+ |
+ private ConnectionType getCurrentConnectionType() { |
+ return currentConnectionType; |
+ } |
+ |
+ private int getCurrentDefaultNetId() { |
+ return autoDetector == null ? INVALID_NET_ID : autoDetector.getDefaultNetId(); |
+ } |
+ |
+ private void destroyAutoDetector() { |
+ if (autoDetector != null) { |
+ autoDetector.destroy(); |
+ autoDetector = null; |
+ } |
+ } |
+ |
+ private void setAutoDetectConnectivityStateInternal(boolean shouldAutoDetect) { |
+ if (!shouldAutoDetect) { |
+ destroyAutoDetector(); |
+ return; |
+ } |
+ if (autoDetector == null) { |
+ autoDetector = new NetworkChangeNotifierAutoDetect( |
+ new NetworkChangeNotifierAutoDetect.Observer() { |
+ @Override |
+ public void onConnectionTypeChanged(ConnectionType newConnectionType) { |
+ updateCurrentConnectionType(newConnectionType); |
+ } |
+ }, |
+ applicationContext); |
+ final NetworkChangeNotifierAutoDetect.NetworkState networkState = |
+ autoDetector.getCurrentNetworkState(); |
+ updateCurrentConnectionType(autoDetector.getCurrentConnectionType(networkState)); |
+ } |
+ } |
+ |
+ private void updateCurrentConnectionType(ConnectionType newConnectionType) { |
+ currentConnectionType = newConnectionType; |
+ notifyObserversOfConnectionTypeChange(newConnectionType); |
+ } |
+ |
+ /** |
+ * Alerts all observers of a connection change. |
+ */ |
+ private void notifyObserversOfConnectionTypeChange(ConnectionType newConnectionType) { |
+ if (nativeNetworkChangeNotifier != 0) { |
+ nativeNotifyConnectionTypeChanged(nativeNetworkChangeNotifier); |
+ } |
+ for (ConnectionTypeObserver observer : connectionTypeObservers) { |
+ observer.onConnectionTypeChanged(newConnectionType); |
+ } |
+ } |
+ |
+ /** |
+ * Adds an observer for any connection type changes. |
+ */ |
+ public static void addConnectionTypeObserver(ConnectionTypeObserver observer) { |
+ getInstance().addConnectionTypeObserverInternal(observer); |
+ } |
+ |
+ private void addConnectionTypeObserverInternal(ConnectionTypeObserver observer) { |
+ connectionTypeObservers.add(observer); |
+ } |
+ |
+ /** |
+ * Removes an observer for any connection type changes. |
+ */ |
+ public static void removeConnectionTypeObserver(ConnectionTypeObserver observer) { |
+ getInstance().removeConnectionTypeObserverInternal(observer); |
+ } |
+ |
+ private void removeConnectionTypeObserverInternal(ConnectionTypeObserver observer) { |
+ connectionTypeObservers.remove(observer); |
+ } |
+ |
+ /** |
+ * Checks if there currently is connectivity. |
+ */ |
+ public static boolean isOnline() { |
+ ConnectionType connectionType = getInstance().getCurrentConnectionType(); |
+ return connectionType != ConnectionType.CONNECTION_UNKNOWN |
+ && connectionType != ConnectionType.CONNECTION_NONE; |
+ } |
+ |
+ private native long nativeCreateNetworkChangeNotifier(); |
+ |
+ private native void nativeNotifyConnectionTypeChanged(long nativePtr); |
+ |
+ // For testing only. |
+ static void resetInstanceForTests(Context context) { |
+ instance = new NetworkChangeNotifier(context); |
+ } |
+ |
+ // For testing only. |
+ public static NetworkChangeNotifierAutoDetect getAutoDetectorForTest() { |
+ return getInstance().autoDetector; |
+ } |
+} |