Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(294)

Unified Diff: talk/app/webrtc/java/android/org/webrtc/NetworkMonitorAutoDetect.java

Issue 1610243002: Move talk/app/webrtc to webrtc/api (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Removed processing of api.gyp for Chromium builds Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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
deleted file mode 100644
index d71efc674d20245f85eaca402e7ef98050b86b35..0000000000000000000000000000000000000000
--- a/talk/app/webrtc/java/android/org/webrtc/NetworkMonitorAutoDetect.java
+++ /dev/null
@@ -1,605 +0,0 @@
-/*
- * 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 android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET;
-import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
-
-
-import org.webrtc.Logging;
-
-import android.Manifest.permission;
-import android.annotation.SuppressLint;
-import android.content.BroadcastReceiver;
-import android.content.Context;
-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;
-import android.telephony.TelephonyManager;
-
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * Borrowed from Chromium's
- * src/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java
- *
- * Used by the NetworkMonitor to listen to platform changes in connectivity.
- * Note that use of this class requires that the app have the platform
- * ACCESS_NETWORK_STATE permission.
- */
-public class NetworkMonitorAutoDetect extends BroadcastReceiver {
- public static enum ConnectionType {
- CONNECTION_UNKNOWN,
- CONNECTION_ETHERNET,
- CONNECTION_WIFI,
- CONNECTION_4G,
- CONNECTION_3G,
- CONNECTION_2G,
- CONNECTION_BLUETOOTH,
- CONNECTION_NONE
- }
-
- public static class IPAddress {
- public final byte[] address;
- public IPAddress (byte[] address) {
- this.address = address;
- }
- }
-
- /** Java version of NetworkMonitor.NetworkInformation */
- public static class NetworkInformation{
- public final String name;
- public final ConnectionType type;
- public final int handle;
- public final 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
- // further divided into 2G, 3G, or 4G from the subtype.
- private final int type;
- // Defined from NetworkInfo.subtype, which is one of the TelephonyManager.NETWORK_TYPE_XXXs.
- // Will be useful to find the maximum bandwidth.
- private final int subtype;
-
- public NetworkState(boolean connected, int type, int subtype) {
- this.connected = connected;
- this.type = type;
- this.subtype = subtype;
- }
-
- public boolean isConnected() {
- return connected;
- }
-
- public int getNetworkType() {
- return type;
- }
-
- public int getNetworkSubType() {
- return subtype;
- }
- }
- /**
- * The methods in this class get called when the network changes if the callback
- * is registered with a proper network request. It is only available in Android Lollipop
- * and above.
- */
- @SuppressLint("NewApi")
- private class SimpleNetworkCallback extends NetworkCallback {
-
- @Override
- public void onAvailable(Network network) {
- Logging.d(TAG, "Network becomes available: " + network.toString());
- onNetworkChanged(network);
- }
-
- @Override
- public void onCapabilitiesChanged(
- Network network, NetworkCapabilities networkCapabilities) {
- // A capabilities change may indicate the ConnectionType has changed,
- // so forward the new NetworkInformation along to the observer.
- Logging.d(TAG, "capabilities changed: " + networkCapabilities.toString());
- onNetworkChanged(network);
- }
-
- @Override
- public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {
- // A link property change may indicate the IP address changes.
- // so forward the new NetworkInformation to the observer.
- Logging.d(TAG, "link properties changed: " + linkProperties.toString());
- onNetworkChanged(network);
- }
-
- @Override
- public void onLosing(Network network, int maxMsToLive) {
- // Tell the network is going to lose in MaxMsToLive milliseconds.
- // We may use this signal later.
- Logging.d(TAG, "Network with handle " + networkToNetId(network) +
- " is about to lose in " + maxMsToLive + "ms");
- }
-
- @Override
- public void onLost(Network network) {
- int handle = networkToNetId(network);
- Logging.d(TAG, "Network with handle " + handle + " is disconnected");
- observer.onNetworkDisconnect(handle);
- }
-
- private void onNetworkChanged(Network network) {
- NetworkInformation networkInformation = connectivityManagerDelegate.networkToInfo(network);
- if (networkInformation.type != ConnectionType.CONNECTION_UNKNOWN
- && networkInformation.type != ConnectionType.CONNECTION_NONE) {
- observer.onNetworkConnect(networkInformation);
- }
- }
- }
-
- /** Queries the ConnectivityManager for information about the current connection. */
- static class ConnectivityManagerDelegate {
- /**
- * Note: In some rare Android systems connectivityManager is null. We handle that
- * gracefully below.
- */
- private final ConnectivityManager connectivityManager;
-
- ConnectivityManagerDelegate(Context context) {
- connectivityManager =
- (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
- }
-
- // For testing.
- ConnectivityManagerDelegate() {
- // All the methods below should be overridden.
- connectivityManager = null;
- }
-
- /**
- * Returns connection type and status information about the current
- * default network.
- */
- NetworkState getNetworkState() {
- if (connectivityManager == null) {
- return new NetworkState(false, -1, -1);
- }
- return getNetworkState(connectivityManager.getActiveNetworkInfo());
- }
-
- /**
- * Returns connection type and status information about |network|.
- * Only callable on Lollipop and newer releases.
- */
- @SuppressLint("NewApi")
- NetworkState getNetworkState(Network network) {
- if (connectivityManager == null) {
- return new NetworkState(false, -1, -1);
- }
- return getNetworkState(connectivityManager.getNetworkInfo(network));
- }
-
- /**
- * Returns connection type and status information gleaned from networkInfo.
- */
- NetworkState getNetworkState(NetworkInfo networkInfo) {
- if (networkInfo == null || !networkInfo.isConnected()) {
- return new NetworkState(false, -1, -1);
- }
- return new NetworkState(true, networkInfo.getType(), networkInfo.getSubtype());
- }
-
- /**
- * Returns all connected networks.
- * Only callable on Lollipop and newer releases.
- */
- @SuppressLint("NewApi")
- Network[] getAllNetworks() {
- if (connectivityManager == null) {
- return new Network[0];
- }
- return connectivityManager.getAllNetworks();
- }
-
- List<NetworkInformation> getActiveNetworkList() {
- if (!supportNetworkCallback()) {
- return null;
- }
- ArrayList<NetworkInformation> netInfoList = new ArrayList<NetworkInformation>();
- for (Network network : getAllNetworks()) {
- NetworkInformation info = networkToInfo(network);
- if (info.name != null && info.type != ConnectionType.CONNECTION_NONE
- && info.type != ConnectionType.CONNECTION_UNKNOWN) {
- netInfoList.add(info);
- }
- }
- return netInfoList;
- }
-
- /**
- * Returns the NetID of the current default network. Returns
- * INVALID_NET_ID if no current default network connected.
- * Only callable on Lollipop and newer releases.
- */
- @SuppressLint("NewApi")
- int getDefaultNetId() {
- if (!supportNetworkCallback()) {
- return INVALID_NET_ID;
- }
- // Android Lollipop had no API to get the default network; only an
- // API to return the NetworkInfo for the default network. To
- // determine the default network one can find the network with
- // type matching that of the default network.
- final NetworkInfo defaultNetworkInfo = connectivityManager.getActiveNetworkInfo();
- if (defaultNetworkInfo == null) {
- return INVALID_NET_ID;
- }
- final Network[] networks = getAllNetworks();
- int defaultNetId = INVALID_NET_ID;
- for (Network network : networks) {
- if (!hasInternetCapability(network)) {
- continue;
- }
- final NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network);
- if (networkInfo != null && networkInfo.getType() == defaultNetworkInfo.getType()) {
- // There should not be multiple connected networks of the
- // same type. At least as of Android Marshmallow this is
- // not supported. If this becomes supported this assertion
- // may trigger. At that point we could consider using
- // ConnectivityManager.getDefaultNetwork() though this
- // may give confusing results with VPNs and is only
- // available with Android Marshmallow.
- assert defaultNetId == INVALID_NET_ID;
- defaultNetId = networkToNetId(network);
- }
- }
- return defaultNetId;
- }
-
- @SuppressLint("NewApi")
- private NetworkInformation networkToInfo(Network network) {
- LinkProperties linkProperties = connectivityManager.getLinkProperties(network);
- NetworkInformation networkInformation = new NetworkInformation(
- linkProperties.getInterfaceName(),
- getConnectionType(getNetworkState(network)),
- networkToNetId(network),
- getIPAddresses(linkProperties));
- return networkInformation;
- }
-
- /**
- * Returns true if {@code network} can provide Internet access. Can be used to
- * ignore specialized networks (e.g. IMS, FOTA).
- */
- @SuppressLint("NewApi")
- boolean hasInternetCapability(Network network) {
- if (connectivityManager == null) {
- return false;
- }
- final NetworkCapabilities capabilities =
- connectivityManager.getNetworkCapabilities(network);
- return capabilities != null && capabilities.hasCapability(NET_CAPABILITY_INTERNET);
- }
-
- /** Only callable on Lollipop and newer releases. */
- @SuppressLint("NewApi")
- public void registerNetworkCallback(NetworkCallback networkCallback) {
- connectivityManager.registerNetworkCallback(
- new NetworkRequest.Builder().addCapability(NET_CAPABILITY_INTERNET).build(),
- networkCallback);
- }
-
- /** Only callable on Lollipop and newer releases. */
- @SuppressLint("NewApi")
- public void requestMobileNetwork(NetworkCallback networkCallback) {
- NetworkRequest.Builder builder = new NetworkRequest.Builder();
- builder.addCapability(NET_CAPABILITY_INTERNET).addTransportType(TRANSPORT_CELLULAR);
- connectivityManager.requestNetwork(builder.build(), networkCallback);
- }
-
- @SuppressLint("NewApi")
- IPAddress[] getIPAddresses(LinkProperties linkProperties) {
- IPAddress[] ipAddresses = new IPAddress[linkProperties.getLinkAddresses().size()];
- int i = 0;
- for (LinkAddress linkAddress : linkProperties.getLinkAddresses()) {
- ipAddresses[i] = new IPAddress(linkAddress.getAddress().getAddress());
- ++i;
- }
- return ipAddresses;
- }
-
- @SuppressLint("NewApi")
- public void releaseCallback(NetworkCallback networkCallback) {
- if (supportNetworkCallback()) {
- Logging.d(TAG, "Unregister network callback");
- connectivityManager.unregisterNetworkCallback(networkCallback);
- }
- }
-
- public boolean supportNetworkCallback() {
- return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && connectivityManager != null;
- }
- }
-
-
- /** Queries the WifiManager for SSID of the current Wifi connection. */
- static class WifiManagerDelegate {
- private final Context context;
- WifiManagerDelegate(Context context) {
- this.context = context;
- }
-
- // For testing.
- WifiManagerDelegate() {
- // All the methods below should be overridden.
- context = null;
- }
-
- String getWifiSSID() {
- final Intent intent = context.registerReceiver(null,
- new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION));
- if (intent != null) {
- final WifiInfo wifiInfo = intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO);
- if (wifiInfo != null) {
- final String ssid = wifiInfo.getSSID();
- if (ssid != null) {
- return ssid;
- }
- }
- }
- return "";
- }
-
- }
-
- static final int INVALID_NET_ID = -1;
- private static final String TAG = "NetworkMonitorAutoDetect";
-
- // Observer for the connection type change.
- private final Observer observer;
- private final IntentFilter intentFilter;
- private final Context context;
- // Used to request mobile network. It does not do anything except for keeping
- // the callback for releasing the request.
- private final NetworkCallback mobileNetworkCallback;
- // Used to receive updates on all networks.
- private final NetworkCallback allNetworkCallback;
- // connectivityManagerDelegate and wifiManagerDelegate are only non-final for testing.
- private ConnectivityManagerDelegate connectivityManagerDelegate;
- private WifiManagerDelegate wifiManagerDelegate;
-
- private boolean isRegistered;
- private ConnectionType connectionType;
- private String wifiSSID;
-
- /**
- * Observer interface by which observer is notified of network changes.
- */
- public static interface Observer {
- /**
- * Called when default network changes.
- */
- public void onConnectionTypeChanged(ConnectionType newConnectionType);
- public void onNetworkConnect(NetworkInformation networkInfo);
- public void onNetworkDisconnect(int networkHandle);
- }
-
- /**
- * Constructs a NetworkMonitorAutoDetect. Should only be called on UI thread.
- */
- @SuppressLint("NewApi")
- public NetworkMonitorAutoDetect(Observer observer, Context context) {
- this.observer = observer;
- this.context = context;
- connectivityManagerDelegate = new ConnectivityManagerDelegate(context);
- wifiManagerDelegate = new WifiManagerDelegate(context);
-
- final NetworkState networkState = connectivityManagerDelegate.getNetworkState();
- connectionType = getConnectionType(networkState);
- wifiSSID = getWifiSSID(networkState);
- intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
-
- registerReceiver();
- if (connectivityManagerDelegate.supportNetworkCallback()) {
- mobileNetworkCallback = new NetworkCallback();
- connectivityManagerDelegate.requestMobileNetwork(mobileNetworkCallback);
- allNetworkCallback = new SimpleNetworkCallback();
- connectivityManagerDelegate.registerNetworkCallback(allNetworkCallback);
- } else {
- mobileNetworkCallback = null;
- allNetworkCallback = null;
- }
- }
-
- /**
- * Allows overriding the ConnectivityManagerDelegate for tests.
- */
- void setConnectivityManagerDelegateForTests(ConnectivityManagerDelegate delegate) {
- connectivityManagerDelegate = delegate;
- }
-
- /**
- * Allows overriding the WifiManagerDelegate for tests.
- */
- void setWifiManagerDelegateForTests(WifiManagerDelegate delegate) {
- wifiManagerDelegate = delegate;
- }
-
- /**
- * Returns whether the object has registered to receive network connectivity intents.
- * Visible for testing.
- */
- boolean isReceiverRegisteredForTesting() {
- return isRegistered;
- }
-
- List<NetworkInformation> getActiveNetworkList() {
- return connectivityManagerDelegate.getActiveNetworkList();
- }
-
- public void destroy() {
- if (allNetworkCallback != null) {
- connectivityManagerDelegate.releaseCallback(allNetworkCallback);
- }
- if (mobileNetworkCallback != null) {
- connectivityManagerDelegate.releaseCallback(mobileNetworkCallback);
- }
- unregisterReceiver();
- }
-
- /**
- * Registers a BroadcastReceiver in the given context.
- */
- private void registerReceiver() {
- if (isRegistered) return;
-
- isRegistered = true;
- context.registerReceiver(this, intentFilter);
- }
-
- /**
- * Unregisters the BroadcastReceiver in the given context.
- */
- private void unregisterReceiver() {
- if (!isRegistered) return;
-
- isRegistered = false;
- context.unregisterReceiver(this);
- }
-
- public NetworkState getCurrentNetworkState() {
- return connectivityManagerDelegate.getNetworkState();
- }
-
- /**
- * Returns NetID of device's current default connected network used for
- * communication.
- * Only implemented on Lollipop and newer releases, returns INVALID_NET_ID
- * when not implemented.
- */
- public int getDefaultNetId() {
- return connectivityManagerDelegate.getDefaultNetId();
- }
-
- public static ConnectionType getConnectionType(NetworkState networkState) {
- if (!networkState.isConnected()) {
- return ConnectionType.CONNECTION_NONE;
- }
-
- switch (networkState.getNetworkType()) {
- case ConnectivityManager.TYPE_ETHERNET:
- return ConnectionType.CONNECTION_ETHERNET;
- case ConnectivityManager.TYPE_WIFI:
- return ConnectionType.CONNECTION_WIFI;
- case ConnectivityManager.TYPE_WIMAX:
- return ConnectionType.CONNECTION_4G;
- case ConnectivityManager.TYPE_BLUETOOTH:
- return ConnectionType.CONNECTION_BLUETOOTH;
- case ConnectivityManager.TYPE_MOBILE:
- // Use information from TelephonyManager to classify the connection.
- switch (networkState.getNetworkSubType()) {
- case TelephonyManager.NETWORK_TYPE_GPRS:
- case TelephonyManager.NETWORK_TYPE_EDGE:
- case TelephonyManager.NETWORK_TYPE_CDMA:
- case TelephonyManager.NETWORK_TYPE_1xRTT:
- case TelephonyManager.NETWORK_TYPE_IDEN:
- return ConnectionType.CONNECTION_2G;
- case TelephonyManager.NETWORK_TYPE_UMTS:
- case TelephonyManager.NETWORK_TYPE_EVDO_0:
- case TelephonyManager.NETWORK_TYPE_EVDO_A:
- case TelephonyManager.NETWORK_TYPE_HSDPA:
- case TelephonyManager.NETWORK_TYPE_HSUPA:
- case TelephonyManager.NETWORK_TYPE_HSPA:
- case TelephonyManager.NETWORK_TYPE_EVDO_B:
- case TelephonyManager.NETWORK_TYPE_EHRPD:
- case TelephonyManager.NETWORK_TYPE_HSPAP:
- return ConnectionType.CONNECTION_3G;
- case TelephonyManager.NETWORK_TYPE_LTE:
- return ConnectionType.CONNECTION_4G;
- default:
- return ConnectionType.CONNECTION_UNKNOWN;
- }
- default:
- return ConnectionType.CONNECTION_UNKNOWN;
- }
- }
-
- private String getWifiSSID(NetworkState networkState) {
- if (getConnectionType(networkState) != ConnectionType.CONNECTION_WIFI) return "";
- return wifiManagerDelegate.getWifiSSID();
- }
-
- // BroadcastReceiver
- @Override
- public void onReceive(Context context, Intent intent) {
- final NetworkState networkState = getCurrentNetworkState();
- if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
- connectionTypeChanged(networkState);
- }
- }
-
- private void connectionTypeChanged(NetworkState networkState) {
- ConnectionType newConnectionType = getConnectionType(networkState);
- String newWifiSSID = getWifiSSID(networkState);
- if (newConnectionType == connectionType && newWifiSSID.equals(wifiSSID)) return;
-
- connectionType = newConnectionType;
- wifiSSID = newWifiSSID;
- Logging.d(TAG, "Network connectivity changed, type is: " + connectionType);
- observer.onConnectionTypeChanged(newConnectionType);
- }
-
- /**
- * Extracts NetID of network. Only available on Lollipop and newer releases.
- */
- @SuppressLint("NewApi")
- private static int networkToNetId(Network network) {
- // NOTE(pauljensen): This depends on Android framework implementation details.
- // Fortunately this functionality is unlikely to ever change.
- // TODO(honghaiz): When we update to Android M SDK, use Network.getNetworkHandle().
- return Integer.parseInt(network.toString());
- }
-}
« no previous file with comments | « talk/app/webrtc/java/android/org/webrtc/NetworkMonitor.java ('k') | talk/app/webrtc/java/android/org/webrtc/OWNERS » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698