Chromium Code Reviews| 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..175ee27d74a0ee439274fdab0ace84ee235ef740 100644 |
| --- a/talk/app/webrtc/java/android/org/webrtc/NetworkMonitorAutoDetect.java |
| +++ b/talk/app/webrtc/java/android/org/webrtc/NetworkMonitorAutoDetect.java |
| @@ -37,9 +37,13 @@ import android.content.Intent; |
| import android.content.IntentFilter; |
| import android.content.pm.PackageManager; |
| import android.net.ConnectivityManager; |
| +import android.net.ConnectivityManager.NetworkCallback; |
| +import android.net.LinkAddress; |
| +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; |
| @@ -66,6 +70,28 @@ public class NetworkMonitorAutoDetect extends BroadcastReceiver { |
| CONNECTION_NONE |
| } |
| + public static class IPAddress { |
| + public byte[] address; |
|
AlexG
2016/01/15 18:32:37
final?
honghaiz3
2016/01/15 19:17:56
Done.
|
| + public IPAddress (byte[] address) { |
| + this.address = address; |
| + } |
| + } |
| + |
| + /** Java version of NetworkMonitor.NetworkInformation */ |
| + public static class NetworkInformation{ |
| + public String name; |
|
AlexG
2016/01/15 18:32:37
final and for all other fields?
honghaiz3
2016/01/15 19:17:56
Done.
|
| + public ConnectionType type; |
| + public int handle; |
| + public IPAddress[] ipAddresses; |
| + public NetworkInformation(String name, ConnectionType type, int handle, |
| + IPAddress[] addresses) { |
| + this.name = name; |
| + this.type = type; |
| + this.handle = handle; |
| + this.ipAddresses = addresses; |
| + } |
| + }; |
| + |
| static class NetworkState { |
| private final boolean connected; |
| // Defined from ConnectivityManager.TYPE_XXX for non-mobile; for mobile, it is |
| @@ -101,6 +127,7 @@ public class NetworkMonitorAutoDetect extends BroadcastReceiver { |
| * gracefully below. |
| */ |
| private final ConnectivityManager connectivityManager; |
| + private NetworkCallback networkCallback; |
| ConnectivityManagerDelegate(Context context) { |
| connectivityManager = |
| @@ -211,30 +238,68 @@ 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 || |
| + connectivityManager == null) { |
| + return; |
| + } |
| + networkCallback = new NetworkCallback() { |
| + @Override |
| + public void onAvailable(Network network) { |
| + super.onAvailable(network); |
| + LinkProperties linkProperties = connectivityManager.getLinkProperties(network); |
| + NetworkInformation networkInformation = new NetworkInformation( |
| + linkProperties.getInterfaceName(), |
| + getConnectionType(getNetworkState(network)), |
| + networkToNetId(network), |
| + getIPAddresses(linkProperties)); |
| + Log.i(TAG, "Network " + networkInformation.name + " is connected "); |
|
AlexG
2016/01/15 18:32:37
Use Logging.i instead for all log messages in Java
honghaiz3
2016/01/15 19:17:56
I think Logging.d for all old Log.i.
|
| + observer.onNetworkConnect(networkInformation); |
| + } |
| + }; |
| + Log.i(TAG, "Requesting cellular network"); |
| + NetworkRequest.Builder builder = new NetworkRequest.Builder(); |
| + builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); |
| + builder.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR); |
| + connectivityManager.requestNetwork(builder.build(), networkCallback); |
| + } |
| + |
| + @SuppressLint("NewApi") |
| + IPAddress[] getIPAddresses(LinkProperties linkProperties) { |
| + LinkAddress[] linkAddresses = |
| + new LinkAddress[linkProperties.getLinkAddresses().size()]; |
| + linkProperties.getLinkAddresses().toArray(linkAddresses); |
| + IPAddress[] ipAddresses = new IPAddress[linkAddresses.length]; |
| + for (int i = 0; i < linkAddresses.length; ++i) { |
|
AlexG
2016/01/15 18:32:37
Why not just iterate through the list instead of c
honghaiz3
2016/01/15 19:17:56
Done.
|
| + ipAddresses[i] = new IPAddress(linkAddresses[i].getAddress().getAddress()); |
| + } |
| + return ipAddresses; |
| + } |
| + |
| + @SuppressLint("NewApi") |
| + public void releaseCallback() { |
| + if (networkCallback != null) { |
| + connectivityManager.unregisterNetworkCallback(networkCallback); |
|
AlexG
2016/01/15 18:32:37
Add check for Build.VERSION.SDK_INT < Build.VERSIO
honghaiz3
2016/01/15 19:17:56
Done.
|
| + networkCallback = null; |
| + } |
| + } |
| + |
| } |
| + |
| /** Queries the WifiManager for SSID of the current Wifi connection. */ |
| static class WifiManagerDelegate { |
| private final Context context; |
| - private final WifiManager wifiManager; |
| - private final boolean hasWifiPermission; |
| - |
| WifiManagerDelegate(Context context) { |
| this.context = context; |
| - |
| - hasWifiPermission = context.getPackageManager().checkPermission( |
| - permission.ACCESS_WIFI_STATE, context.getPackageName()) |
| - == PackageManager.PERMISSION_GRANTED; |
| - wifiManager = hasWifiPermission |
| - ? (WifiManager) context.getSystemService(Context.WIFI_SERVICE) : null; |
| } |
| // For testing. |
| WifiManagerDelegate() { |
| // All the methods below should be overridden. |
| context = null; |
| - wifiManager = null; |
| - hasWifiPermission = false; |
| } |
| String getWifiSSID() { |
| @@ -252,9 +317,6 @@ public class NetworkMonitorAutoDetect extends BroadcastReceiver { |
| return ""; |
| } |
| - boolean getHasWifiPermission() { |
| - return hasWifiPermission; |
| - } |
| } |
| static final int INVALID_NET_ID = -1; |
| @@ -280,6 +342,7 @@ public class NetworkMonitorAutoDetect extends BroadcastReceiver { |
| * Called when default network changes. |
| */ |
| public void onConnectionTypeChanged(ConnectionType newConnectionType); |
| + public void onNetworkConnect(NetworkInformation networkInfo); |
| } |
| /** |
| @@ -292,8 +355,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 +394,7 @@ public class NetworkMonitorAutoDetect extends BroadcastReceiver { |
| if (!isRegistered) { |
| isRegistered = true; |
| context.registerReceiver(this, intentFilter); |
| + connectivityManagerDelegate.requestMobileNetwork(observer); |
| } |
| } |
| @@ -341,6 +405,7 @@ public class NetworkMonitorAutoDetect extends BroadcastReceiver { |
| if (isRegistered) { |
| isRegistered = false; |
| context.unregisterReceiver(this); |
| + connectivityManagerDelegate.releaseCallback(); |
| } |
| } |
| @@ -361,7 +426,7 @@ public class NetworkMonitorAutoDetect extends BroadcastReceiver { |
| return connectivityManagerDelegate.getDefaultNetId(); |
| } |
| - public ConnectionType getCurrentConnectionType(NetworkState networkState) { |
| + public static ConnectionType getConnectionType(NetworkState networkState) { |
| if (!networkState.isConnected()) { |
| return ConnectionType.CONNECTION_NONE; |
| } |
| @@ -404,8 +469,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 +484,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; |