Update NetworkMonitor so that it can be started from java code.

BUG=b/69623057

Change-Id: I30e0d23e82f5feeadc05e5fc9a4b87e7d3e2da8c
Reviewed-on: https://webrtc-review.googlesource.com/31752
Commit-Queue: Honghai Zhang <honghaiz@webrtc.org>
Reviewed-by: Sami Kalliomäki <sakal@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#21473}
This commit is contained in:
Honghai Zhang
2017-12-21 13:22:58 -08:00
committed by Commit Bot
parent 94dc17710f
commit 0e894c3889
3 changed files with 81 additions and 66 deletions

View File

@ -10,27 +10,26 @@
package org.webrtc;
import static org.webrtc.NetworkMonitorAutoDetect.ConnectionType;
import static org.webrtc.NetworkMonitorAutoDetect.INVALID_NET_ID;
import static org.webrtc.NetworkMonitorAutoDetect.NetworkInformation;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import java.util.ArrayList;
import java.util.List;
import org.webrtc.NetworkMonitorAutoDetect.ConnectionType;
import org.webrtc.NetworkMonitorAutoDetect.NetworkInformation;
/**
* 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.
* <p>Triggers updates to the underlying network state from OS networking events.
*
* WARNING: This class is not thread-safe.
* <p>Thread-safety is ensured for methods that may be called from both native code and java code,
* including getInstance(), startMonitoring(), and stopMonitoring().
*/
public class NetworkMonitor {
/**
* Alerted when the connection type of the network changes.
* The alert is fired on the UI thread.
* Alerted when the connection type of the network changes. The alert is fired on the UI thread.
*/
public interface NetworkObserver {
public void onConnectionTypeChanged(ConnectionType connectionType);
@ -38,37 +37,39 @@ public class NetworkMonitor {
private static final String TAG = "NetworkMonitor";
// We are storing application context so it is okay.
private static NetworkMonitor instance;
// Lazy initialization holder class idiom for static fields.
private static class InstanceHolder {
// We are storing application context so it is okay.
static final NetworkMonitor instance = new NetworkMonitor();
}
// Native observers of the connection type changes.
private final ArrayList<Long> nativeNetworkObservers;
// Java observers of the connection type changes.
private final ArrayList<NetworkObserver> networkObservers;
// Object that detects the connection type changes.
private final Object autoDetectorLock = new Object();
// Object that detects the connection type changes and brings up mobile networks.
private NetworkMonitorAutoDetect autoDetector;
// Also guarded by autoDetectorLock.
private int numMonitors;
private ConnectionType currentConnectionType = ConnectionType.CONNECTION_UNKNOWN;
private NetworkMonitor() {
nativeNetworkObservers = new ArrayList<Long>();
networkObservers = new ArrayList<NetworkObserver>();
numMonitors = 0;
}
// TODO(sakal): Remove once downstream dependencies have been updated.
@Deprecated
public static void init(Context context) {}
/**
* Returns the singleton instance.
*/
/** Returns the singleton instance. This may be called from native or from Java code. */
@CalledByNative
public static NetworkMonitor getInstance() {
if (instance == null) {
instance = new NetworkMonitor();
}
return instance;
return InstanceHolder.instance;
}
private static void assertIsTrue(boolean condition) {
@ -78,38 +79,62 @@ public class NetworkMonitor {
}
/**
* Enables auto detection of the current network state based on notifications
* from the system. Note that this requires the embedding app have the
* platform ACCESS_NETWORK_STATE permission.
* Enables auto detection of the network state change and brings up mobile networks for using
* multi-networking. This requires the embedding app have the platform ACCESS_NETWORK_STATE and
* CHANGE_NETWORK_STATE permission.
*/
public void startMonitoring() {
synchronized (autoDetectorLock) {
++numMonitors;
if (autoDetector == null) {
autoDetector = createAutoDetector(ContextUtils.getApplicationContext());
}
currentConnectionType =
NetworkMonitorAutoDetect.getConnectionType(autoDetector.getCurrentNetworkState());
}
}
/**
* Enables auto detection of the network state change and brings up mobile networks for using
* multi-networking. This requires the embedding app have the platform ACCESS_NETWORK_STATE and
* CHANGE_NETWORK_STATE permission.
*/
@CalledByNative
private void startMonitoring(long nativeObserver) {
Logging.d(TAG, "Start monitoring from native observer " + nativeObserver);
Logging.d(TAG, "Start monitoring with native observer " + nativeObserver);
startMonitoring();
// The native observers expect a network list update after they call startMonitoring.
nativeNetworkObservers.add(nativeObserver);
if (autoDetector == null) {
createAutoDetector();
}
// The observers expect a network list update after they call startMonitoring.
final NetworkMonitorAutoDetect.NetworkState networkState =
autoDetector.getCurrentNetworkState();
updateCurrentConnectionType(NetworkMonitorAutoDetect.getConnectionType(networkState));
updateObserverActiveNetworkList(nativeObserver);
// currentConnectionType was updated in startMonitoring().
// Need to notify the native observers here.
notifyObserversOfConnectionTypeChange(currentConnectionType);
}
/** Stop network monitoring. If no one is monitoring networks, destroy and reset autoDetector. */
public void stopMonitoring() {
synchronized (autoDetectorLock) {
if (--numMonitors == 0) {
autoDetector.destroy();
autoDetector = null;
}
}
}
@CalledByNative
private void stopMonitoring(long nativeObserver) {
Logging.d(TAG, "Stop monitoring from native observer " + nativeObserver);
Logging.d(TAG, "Stop monitoring with native observer " + nativeObserver);
stopMonitoring();
nativeNetworkObservers.remove(nativeObserver);
if (nativeNetworkObservers.isEmpty()) {
autoDetector.destroy();
autoDetector = null;
}
}
// Returns true if network binding is supported on this platform.
@CalledByNative
private boolean networkBindingSupported() {
return autoDetector != null && autoDetector.supportNetworkCallback();
synchronized (autoDetectorLock) {
return autoDetector != null && autoDetector.supportNetworkCallback();
}
}
@CalledByNative
@ -122,11 +147,13 @@ public class NetworkMonitor {
}
private long getCurrentDefaultNetId() {
return autoDetector == null ? INVALID_NET_ID : autoDetector.getDefaultNetId();
synchronized (autoDetectorLock) {
return autoDetector == null ? INVALID_NET_ID : autoDetector.getDefaultNetId();
}
}
private void createAutoDetector() {
autoDetector = new NetworkMonitorAutoDetect(new NetworkMonitorAutoDetect.Observer() {
private NetworkMonitorAutoDetect createAutoDetector(Context appContext) {
return new NetworkMonitorAutoDetect(new NetworkMonitorAutoDetect.Observer() {
@Override
public void onConnectionTypeChanged(ConnectionType newConnectionType) {
@ -142,7 +169,7 @@ public class NetworkMonitor {
public void onNetworkDisconnect(long networkHandle) {
notifyObserversOfNetworkDisconnect(networkHandle);
}
}, ContextUtils.getApplicationContext());
}, appContext);
}
private void updateCurrentConnectionType(ConnectionType newConnectionType) {
@ -150,9 +177,7 @@ public class NetworkMonitor {
notifyObserversOfConnectionTypeChange(newConnectionType);
}
/**
* Alerts all observers of a connection change.
*/
/** Alerts all observers of a connection change. */
private void notifyObserversOfConnectionTypeChange(ConnectionType newConnectionType) {
for (long nativeObserver : nativeNetworkObservers) {
nativeNotifyConnectionTypeChanged(nativeObserver);
@ -175,7 +200,10 @@ public class NetworkMonitor {
}
private void updateObserverActiveNetworkList(long nativeObserver) {
List<NetworkInformation> networkInfoList = autoDetector.getActiveNetworkList();
List<NetworkInformation> networkInfoList;
synchronized (autoDetectorLock) {
networkInfoList = (autoDetector == null) ? null : autoDetector.getActiveNetworkList();
}
if (networkInfoList == null || networkInfoList.size() == 0) {
return;
}
@ -185,9 +213,7 @@ public class NetworkMonitor {
nativeNotifyOfActiveNetworkList(nativeObserver, networkInfos);
}
/**
* Adds an observer for any connection type changes.
*/
/** Adds an observer for any connection type changes. */
public static void addNetworkObserver(NetworkObserver observer) {
getInstance().addNetworkObserverInternal(observer);
}
@ -196,9 +222,7 @@ public class NetworkMonitor {
networkObservers.add(observer);
}
/**
* Removes an observer for any connection type changes.
*/
/** Removes an observer for any connection type changes. */
public static void removeNetworkObserver(NetworkObserver observer) {
getInstance().removeNetworkObserverInternal(observer);
}
@ -207,9 +231,7 @@ public class NetworkMonitor {
networkObservers.remove(observer);
}
/**
* Checks if there currently is connectivity.
*/
/** Checks if there currently is connectivity. */
public static boolean isOnline() {
ConnectionType connectionType = getInstance().getCurrentConnectionType();
return connectionType != ConnectionType.CONNECTION_NONE;
@ -229,17 +251,9 @@ public class NetworkMonitor {
long nativePtr, NetworkInformation[] networkInfos);
// For testing only.
static void resetInstanceForTests() {
instance = new NetworkMonitor();
}
// For testing only.
static void createAutoDetectorForTest() {
getInstance().createAutoDetector();
}
// For testing only.
static NetworkMonitorAutoDetect getAutoDetectorForTest() {
return getInstance().autoDetector;
static NetworkMonitorAutoDetect getAutoDetectorForTest(Context context) {
NetworkMonitor networkMonitor = getInstance();
NetworkMonitorAutoDetect autoDetector = networkMonitor.createAutoDetector(context);
return networkMonitor.autoDetector = autoDetector;
}
}

View File

@ -305,6 +305,9 @@ public class NetworkMonitorAutoDetect extends BroadcastReceiver {
@SuppressLint("NewApi")
private NetworkInformation networkToInfo(Network network) {
if (connectivityManager == null) {
return null;
}
LinkProperties linkProperties = connectivityManager.getLinkProperties(network);
// getLinkProperties will return null if the network is unknown.
if (linkProperties == null) {