From 3822c8a26d5b9cd1550319d1f425d20c71b2cda5 Mon Sep 17 00:00:00 2001 From: junyulai Date: Thu, 13 Dec 2018 12:47:51 +0800 Subject: [PATCH] Deprecate NetworkInfo Currently NetworkInfo is used by Apps to get information of network. However, to get such information, Apps need to poll NetworkInfo frequently from ConnectivityService. In order to increase the stability and reduce the maintain effort, all functionalities provided by NetworkInfo are targeted to be replaced or removed entirely. Apps should use ConnectivityManager.NetworkCallback instead, to get faster and more detailed updates from connectivity changes. Or, apps could use getNetworkCapabilities or getLinkProperties to get information synchronously, but should not mix the callbacks and synchronous methods together. Bug: 113629330 Test: atest FrameworksNetTests Change-Id: Ie8faf620958c3fa0a4a2f233b35b825de0e99ffc --- api/current.txt | 18 +++++++------- .../java/android/net/ConnectivityManager.java | 13 ++++++++++ core/java/android/net/NetworkInfo.java | 24 +++++++++++++++++++ 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/api/current.txt b/api/current.txt index 236288ccb82ee..d24d03c20cb3d 100755 --- a/api/current.txt +++ b/api/current.txt @@ -27225,7 +27225,7 @@ package android.net { method public void addDefaultNetworkActiveListener(android.net.ConnectivityManager.OnNetworkActiveListener); method public boolean bindProcessToNetwork(android.net.Network); method public android.net.Network getActiveNetwork(); - method public android.net.NetworkInfo getActiveNetworkInfo(); + method public deprecated android.net.NetworkInfo getActiveNetworkInfo(); method public deprecated android.net.NetworkInfo[] getAllNetworkInfo(); method public android.net.Network[] getAllNetworks(); method public deprecated boolean getBackgroundDataSetting(); @@ -27236,7 +27236,7 @@ package android.net { method public int getMultipathPreference(android.net.Network); method public android.net.NetworkCapabilities getNetworkCapabilities(android.net.Network); method public deprecated android.net.NetworkInfo getNetworkInfo(int); - method public android.net.NetworkInfo getNetworkInfo(android.net.Network); + method public deprecated android.net.NetworkInfo getNetworkInfo(android.net.Network); method public deprecated int getNetworkPreference(); method public byte[] getNetworkWatchlistConfigHash(); method public static deprecated android.net.Network getProcessDefaultNetwork(); @@ -27270,14 +27270,14 @@ package android.net { field public static final deprecated int DEFAULT_NETWORK_PREFERENCE = 1; // 0x1 field public static final java.lang.String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL"; field public static final java.lang.String EXTRA_CAPTIVE_PORTAL_URL = "android.net.extra.CAPTIVE_PORTAL_URL"; - field public static final java.lang.String EXTRA_EXTRA_INFO = "extraInfo"; - field public static final java.lang.String EXTRA_IS_FAILOVER = "isFailover"; + field public static final deprecated java.lang.String EXTRA_EXTRA_INFO = "extraInfo"; + field public static final deprecated java.lang.String EXTRA_IS_FAILOVER = "isFailover"; field public static final java.lang.String EXTRA_NETWORK = "android.net.extra.NETWORK"; field public static final deprecated java.lang.String EXTRA_NETWORK_INFO = "networkInfo"; field public static final java.lang.String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST"; field public static final java.lang.String EXTRA_NETWORK_TYPE = "networkType"; field public static final java.lang.String EXTRA_NO_CONNECTIVITY = "noConnectivity"; - field public static final java.lang.String EXTRA_OTHER_NETWORK_INFO = "otherNetwork"; + field public static final deprecated java.lang.String EXTRA_OTHER_NETWORK_INFO = "otherNetwork"; field public static final java.lang.String EXTRA_REASON = "reason"; field public static final int MULTIPATH_PREFERENCE_HANDOVER = 1; // 0x1 field public static final int MULTIPATH_PREFERENCE_PERFORMANCE = 4; // 0x4 @@ -27572,10 +27572,10 @@ package android.net { field public static final int TRANSPORT_WIFI_AWARE = 5; // 0x5 } - public class NetworkInfo implements android.os.Parcelable { + public deprecated class NetworkInfo implements android.os.Parcelable { method public int describeContents(); method public deprecated android.net.NetworkInfo.DetailedState getDetailedState(); - method public java.lang.String getExtraInfo(); + method public deprecated java.lang.String getExtraInfo(); method public deprecated java.lang.String getReason(); method public deprecated android.net.NetworkInfo.State getState(); method public deprecated int getSubtype(); @@ -27591,7 +27591,7 @@ package android.net { field public static final android.os.Parcelable.Creator CREATOR; } - public static final class NetworkInfo.DetailedState extends java.lang.Enum { + public static final deprecated class NetworkInfo.DetailedState extends java.lang.Enum { method public static android.net.NetworkInfo.DetailedState valueOf(java.lang.String); method public static final android.net.NetworkInfo.DetailedState[] values(); enum_constant public static final android.net.NetworkInfo.DetailedState AUTHENTICATING; @@ -27609,7 +27609,7 @@ package android.net { enum_constant public static final android.net.NetworkInfo.DetailedState VERIFYING_POOR_LINK; } - public static final class NetworkInfo.State extends java.lang.Enum { + public static final deprecated class NetworkInfo.State extends java.lang.Enum { method public static android.net.NetworkInfo.State valueOf(java.lang.String); method public static final android.net.NetworkInfo.State[] values(); enum_constant public static final android.net.NetworkInfo.State CONNECTED; diff --git a/core/java/android/net/ConnectivityManager.java b/core/java/android/net/ConnectivityManager.java index 47145874490fb..49c3dc63e1513 100644 --- a/core/java/android/net/ConnectivityManager.java +++ b/core/java/android/net/ConnectivityManager.java @@ -187,13 +187,19 @@ public class ConnectivityManager { * is for a network to which the connectivity manager was failing over * following a disconnect on another network. * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. + * + * @deprecated See {@link NetworkInfo}. */ + @Deprecated public static final String EXTRA_IS_FAILOVER = "isFailover"; /** * The lookup key for a {@link NetworkInfo} object. This is supplied when * there is another network that it may be possible to connect to. Retrieve with * {@link android.content.Intent#getParcelableExtra(String)}. + * + * @deprecated See {@link NetworkInfo}. */ + @Deprecated public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork"; /** * The lookup key for a boolean that indicates whether there is a @@ -214,7 +220,10 @@ public class ConnectivityManager { * may be passed up from the lower networking layers, and its * meaning may be specific to a particular network type. Retrieve * it with {@link android.content.Intent#getStringExtra(String)}. + * + * @deprecated See {@link NetworkInfo#getExtraInfo()}. */ + @Deprecated public static final String EXTRA_EXTRA_INFO = "extraInfo"; /** * The lookup key for an int that provides information about @@ -895,7 +904,9 @@ public class ConnectivityManager { * * @return a {@link NetworkInfo} object for the current default network * or {@code null} if no default network is currently active + * @deprecated See {@link NetworkInfo}. */ + @Deprecated @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) public NetworkInfo getActiveNetworkInfo() { try { @@ -1079,7 +1090,9 @@ public class ConnectivityManager { * @return a {@link NetworkInfo} object for the requested * network or {@code null} if the {@code Network} * is not valid. + * @deprecated See {@link NetworkInfo}. */ + @Deprecated @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) public NetworkInfo getNetworkInfo(Network network) { return getNetworkInfoForUid(network, Process.myUid(), false); diff --git a/core/java/android/net/NetworkInfo.java b/core/java/android/net/NetworkInfo.java index 1a1d2d33424c6..89d99617dfbf0 100644 --- a/core/java/android/net/NetworkInfo.java +++ b/core/java/android/net/NetworkInfo.java @@ -28,7 +28,20 @@ import java.util.EnumMap; * Describes the status of a network interface. *

Use {@link ConnectivityManager#getActiveNetworkInfo()} to get an instance that represents * the current network connection. + * + * @deprecated Callers should instead use the {@link ConnectivityManager.NetworkCallback} API to + * learn about connectivity changes, or switch to use + * {@link ConnectivityManager#getNetworkCapabilities} or + * {@link ConnectivityManager#getLinkProperties} to get information synchronously. Keep + * in mind that while callbacks are guaranteed to be called for every event in order, + * synchronous calls have no such constraints, and as such it is unadvisable to use the + * synchronous methods inside the callbacks as they will often not offer a view of + * networking that is consistent (that is: they may return a past or a future state with + * respect to the event being processed by the callback). Instead, callers are advised + * to only use the arguments of the callbacks, possibly memorizing the specific bits of + * information they need to keep from one callback to another. */ +@Deprecated public class NetworkInfo implements Parcelable { /** @@ -52,7 +65,10 @@ public class NetworkInfo implements Parcelable { * FAILEDDISCONNECTED * BLOCKEDDISCONNECTED * + * + * @deprecated See {@link NetworkInfo}. */ + @Deprecated public enum State { CONNECTING, CONNECTED, SUSPENDED, DISCONNECTING, DISCONNECTED, UNKNOWN } @@ -61,7 +77,10 @@ public class NetworkInfo implements Parcelable { * The fine-grained state of a network connection. This level of detail * is probably of interest to few applications. Most should use * {@link android.net.NetworkInfo.State State} instead. + * + * @deprecated See {@link NetworkInfo}. */ + @Deprecated public enum DetailedState { /** Ready to start data connection setup. */ IDLE, @@ -463,8 +482,10 @@ public class NetworkInfo implements Parcelable { * Set the extraInfo field. * @param extraInfo an optional {@code String} providing addditional network state * information passed up from the lower networking layers. + * @deprecated See {@link NetworkInfo#getExtraInfo}. * @hide */ + @Deprecated public void setExtraInfo(String extraInfo) { synchronized (this) { this.mExtraInfo = extraInfo; @@ -488,7 +509,10 @@ public class NetworkInfo implements Parcelable { * Report the extra information about the network state, if any was * provided by the lower networking layers. * @return the extra information, or null if not available + * @deprecated Use other services e.g. WifiManager to get additional information passed up from + * the lower networking layers. */ + @Deprecated public String getExtraInfo() { synchronized (this) { return mExtraInfo;