| Index: talk/app/webrtc/java/android/org/webrtc/NetworkMonitorAutoDetect.java
|
| diff --git a/talk/app/webrtc/java/android/org/webrtc/NetworkMonitorAutoDetect.java b/talk/app/webrtc/java/android/org/webrtc/NetworkMonitorAutoDetect.java
|
| index 950dcdfa445a25ce6ecae9c7055045692703546d..d812c4287af4fd2cf73adb8b368cf1888e7cc885 100644
|
| --- a/talk/app/webrtc/java/android/org/webrtc/NetworkMonitorAutoDetect.java
|
| +++ b/talk/app/webrtc/java/android/org/webrtc/NetworkMonitorAutoDetect.java
|
| @@ -37,14 +37,17 @@ import android.content.Intent;
|
| import android.content.IntentFilter;
|
| import android.content.pm.PackageManager;
|
| import android.net.ConnectivityManager;
|
| +import android.net.LinkProperties;
|
| import android.net.Network;
|
| import android.net.NetworkCapabilities;
|
| import android.net.NetworkInfo;
|
| +import android.net.NetworkRequest;
|
| import android.net.wifi.WifiInfo;
|
| import android.net.wifi.WifiManager;
|
| import android.os.Build;
|
| import android.telephony.TelephonyManager;
|
| import android.util.Log;
|
| +import android.net.ConnectivityManager.NetworkCallback;
|
|
|
| /**
|
| * Borrowed from Chromium's
|
| @@ -66,6 +69,18 @@ public class NetworkMonitorAutoDetect extends BroadcastReceiver {
|
| CONNECTION_NONE
|
| }
|
|
|
| + /** Java version of NetworkMonitor.NetworkInformation */
|
| + public static class NetworkInformation{
|
| + public String name;
|
| + public ConnectionType type;
|
| + public int handle;
|
| + public NetworkInformation(String name, ConnectionType type, int handle) {
|
| + this.name = name;
|
| + this.type = type;
|
| + this.handle = handle;
|
| + }
|
| + };
|
| +
|
| static class NetworkState {
|
| private final boolean connected;
|
| // Defined from ConnectivityManager.TYPE_XXX for non-mobile; for mobile, it is
|
| @@ -101,6 +116,7 @@ public class NetworkMonitorAutoDetect extends BroadcastReceiver {
|
| * gracefully below.
|
| */
|
| private final ConnectivityManager connectivityManager;
|
| + private NetworkCallback networkCallback;
|
|
|
| ConnectivityManagerDelegate(Context context) {
|
| connectivityManager =
|
| @@ -198,6 +214,25 @@ public class NetworkMonitorAutoDetect extends BroadcastReceiver {
|
| return defaultNetId;
|
| }
|
|
|
| + @SuppressLint("NewApi")
|
| + NetworkInformation[] getAllNetworkInfos() {
|
| + if (connectivityManager == null) {
|
| + return new NetworkInformation[0];
|
| + }
|
| + Network[] networks = getAllNetworks();
|
| + NetworkInformation[] netInfos = new NetworkInformation[networks.length];
|
| + for (int i = 0; i < networks.length; ++i) {
|
| + Network network = networks[i];
|
| + LinkProperties linkProperties = connectivityManager.getLinkProperties(network);
|
| + NetworkInformation networkInformation = new NetworkInformation(
|
| + linkProperties.getInterfaceName(),
|
| + getConnectionType(getNetworkState(network)),
|
| + networkToNetId(network));
|
| + netInfos[i] = networkInformation;
|
| + }
|
| + return netInfos;
|
| + }
|
| +
|
| /**
|
| * Returns true if {@code network} can provide Internet access. Can be used to
|
| * ignore specialized networks (e.g. IMS, FOTA).
|
| @@ -211,8 +246,59 @@ public class NetworkMonitorAutoDetect extends BroadcastReceiver {
|
| connectivityManager.getNetworkCapabilities(network);
|
| return capabilities != null && capabilities.hasCapability(NET_CAPABILITY_INTERNET);
|
| }
|
| +
|
| + @SuppressLint("NewApi")
|
| + public void requestMobileNetwork(final Observer observer) {
|
| + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
|
| + return;
|
| + }
|
| + networkCallback = new NetworkCallback() {
|
| + @Override
|
| + public void onAvailable(Network network) {
|
| + // Just keep it on.
|
| + LinkProperties linkProperties = connectivityManager.getLinkProperties(network);
|
| + Log.d(TAG, "XXX Network become available: interface name: " +
|
| + linkProperties.getInterfaceName() + " link addresses: " +
|
| + linkProperties.getLinkAddresses().toString());
|
| + super.onAvailable(network);
|
| + int handle = networkToNetId(network);
|
| + Log.d(TAG, "XXX network handle = " + handle);
|
| + NetworkInformation networkInformation = new NetworkInformation(
|
| + linkProperties.getInterfaceName(),
|
| + getConnectionType(getNetworkState(network)),
|
| + networkToNetId(network));
|
| + observer.onNetworkAvailable(networkInformation);
|
| + }
|
| +
|
| + @Override
|
| + public void onCapabilitiesChanged(Network network, NetworkCapabilities capabilities) {
|
| + Log.d(TAG, "XXX capabilities changed");
|
| + super.onCapabilitiesChanged(network, capabilities);
|
| + }
|
| + @Override
|
| + public void onLinkPropertiesChanged(Network network, LinkProperties properties) {
|
| + Log.d(TAG, "XXX properties changed");
|
| + super.onLinkPropertiesChanged(network, properties);
|
| + }
|
| + };
|
| + Log.d(TAG, "request network");
|
| + NetworkRequest.Builder builder = new NetworkRequest.Builder();
|
| + builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
|
| + builder.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
|
| + NetworkRequest request = builder.build();
|
| + connectivityManager.requestNetwork(request, networkCallback);
|
| + }
|
| +
|
| + public void releaseCallback() {
|
| + if (networkCallback != null) {
|
| + connectivityManager.unregisterNetworkCallback(networkCallback);
|
| + networkCallback = null;
|
| + }
|
| + }
|
| +
|
| }
|
|
|
| +
|
| /** Queries the WifiManager for SSID of the current Wifi connection. */
|
| static class WifiManagerDelegate {
|
| private final Context context;
|
| @@ -280,6 +366,7 @@ public class NetworkMonitorAutoDetect extends BroadcastReceiver {
|
| * Called when default network changes.
|
| */
|
| public void onConnectionTypeChanged(ConnectionType newConnectionType);
|
| + public void onNetworkAvailable(NetworkInformation networkInfo);
|
| }
|
|
|
| /**
|
| @@ -292,8 +379,8 @@ public class NetworkMonitorAutoDetect extends BroadcastReceiver {
|
| wifiManagerDelegate = new WifiManagerDelegate(context);
|
|
|
| final NetworkState networkState = connectivityManagerDelegate.getNetworkState();
|
| - connectionType = getCurrentConnectionType(networkState);
|
| - wifiSSID = getCurrentWifiSSID(networkState);
|
| + connectionType = getConnectionType(networkState);
|
| + wifiSSID = getWifiSSID(networkState);
|
| intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
|
| registerReceiver();
|
| }
|
| @@ -331,6 +418,7 @@ public class NetworkMonitorAutoDetect extends BroadcastReceiver {
|
| if (!isRegistered) {
|
| isRegistered = true;
|
| context.registerReceiver(this, intentFilter);
|
| + connectivityManagerDelegate.requestMobileNetwork(observer);
|
| }
|
| }
|
|
|
| @@ -341,6 +429,7 @@ public class NetworkMonitorAutoDetect extends BroadcastReceiver {
|
| if (isRegistered) {
|
| isRegistered = false;
|
| context.unregisterReceiver(this);
|
| + connectivityManagerDelegate.releaseCallback();
|
| }
|
| }
|
|
|
| @@ -361,7 +450,14 @@ public class NetworkMonitorAutoDetect extends BroadcastReceiver {
|
| return connectivityManagerDelegate.getDefaultNetId();
|
| }
|
|
|
| - public ConnectionType getCurrentConnectionType(NetworkState networkState) {
|
| + public NetworkInformation[] getAllNetworkInfos() {
|
| + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
|
| + return new NetworkInformation[0];
|
| + }
|
| + return connectivityManagerDelegate.getAllNetworkInfos();
|
| + }
|
| +
|
| + public static ConnectionType getConnectionType(NetworkState networkState) {
|
| if (!networkState.isConnected()) {
|
| return ConnectionType.CONNECTION_NONE;
|
| }
|
| @@ -404,8 +500,8 @@ public class NetworkMonitorAutoDetect extends BroadcastReceiver {
|
| }
|
| }
|
|
|
| - private String getCurrentWifiSSID(NetworkState networkState) {
|
| - if (getCurrentConnectionType(networkState) != ConnectionType.CONNECTION_WIFI) return "";
|
| + private String getWifiSSID(NetworkState networkState) {
|
| + if (getConnectionType(networkState) != ConnectionType.CONNECTION_WIFI) return "";
|
| return wifiManagerDelegate.getWifiSSID();
|
| }
|
|
|
| @@ -419,8 +515,8 @@ public class NetworkMonitorAutoDetect extends BroadcastReceiver {
|
| }
|
|
|
| private void connectionTypeChanged(NetworkState networkState) {
|
| - ConnectionType newConnectionType = getCurrentConnectionType(networkState);
|
| - String newWifiSSID = getCurrentWifiSSID(networkState);
|
| + ConnectionType newConnectionType = getConnectionType(networkState);
|
| + String newWifiSSID = getWifiSSID(networkState);
|
| if (newConnectionType == connectionType && newWifiSSID.equals(wifiSSID)) return;
|
|
|
| connectionType = newConnectionType;
|
| @@ -439,4 +535,5 @@ public class NetworkMonitorAutoDetect extends BroadcastReceiver {
|
| // TODO(honghaiz): When we update to Android M SDK, use Network.getNetworkHandle().
|
| return Integer.parseInt(network.toString());
|
| }
|
| +
|
| }
|
|
|