summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Chalard Jean <jchalard@google.com> 2020-02-21 19:37:21 +0900
committer Chalard Jean <jchalard@google.com> 2020-02-28 06:26:27 +0000
commita11593c24e688968859bcf634b4dbd85dc87ad6e (patch)
tree2959e7b8cae504df4484931cfb92e83010de4e0b
parent3e7dd91752afd4eed63bab6e1c4b191a4eb7a477 (diff)
Revert "Add public fields to NetworkScore and make it @SystemApi"
This reverts commit a27e1eef14e575550f112a29d23fa40639387dfd. Bug: 113554781 Test: FrameworksNetTests FrameworksTelephonyTests FrameworksWifiTests Change-Id: Ie1e84bd2a16f088307b195f924f4e49335cbe367
-rwxr-xr-xapi/system-current.txt53
-rw-r--r--api/test-current.txt49
-rw-r--r--core/java/android/net/ConnectivityManager.java8
-rw-r--r--core/java/android/net/IConnectivityManager.aidl3
-rw-r--r--core/java/android/net/NetworkAgent.java50
-rw-r--r--core/java/android/net/NetworkScore.aidl19
-rw-r--r--core/java/android/net/NetworkScore.java468
-rw-r--r--services/core/java/com/android/server/ConnectivityService.java22
-rw-r--r--services/core/java/com/android/server/connectivity/NetworkAgentInfo.java2
-rw-r--r--tests/net/common/java/android/net/NetworkScoreTest.kt90
-rw-r--r--tests/net/integration/util/com/android/server/NetworkAgentWrapper.java7
-rw-r--r--tests/net/java/com/android/server/connectivity/LingerMonitorTest.java3
12 files changed, 179 insertions, 595 deletions
diff --git a/api/system-current.txt b/api/system-current.txt
index 9a1aacd04393..af50d5478318 100755
--- a/api/system-current.txt
+++ b/api/system-current.txt
@@ -6296,7 +6296,7 @@ package android.net {
}
public abstract class NetworkAgent {
- ctor public NetworkAgent(@NonNull android.content.Context, @NonNull android.os.Looper, @NonNull String, @NonNull android.net.NetworkCapabilities, @NonNull android.net.LinkProperties, @NonNull android.net.NetworkScore, @NonNull android.net.NetworkAgentConfig, @Nullable android.net.NetworkProvider);
+ ctor public NetworkAgent(@NonNull android.content.Context, @NonNull android.os.Looper, @NonNull String, @NonNull android.net.NetworkCapabilities, @NonNull android.net.LinkProperties, int, @NonNull android.net.NetworkAgentConfig, @Nullable android.net.NetworkProvider);
method @Nullable public android.net.Network getNetwork();
method public void onAddKeepalivePacketFilter(int, @NonNull android.net.KeepalivePacketData);
method public void onAutomaticReconnectDisabled();
@@ -6311,7 +6311,7 @@ package android.net {
method @NonNull public android.net.Network register();
method public void sendLinkProperties(@NonNull android.net.LinkProperties);
method public void sendNetworkCapabilities(@NonNull android.net.NetworkCapabilities);
- method public void sendNetworkScore(@NonNull android.net.NetworkScore);
+ method public void sendNetworkScore(int);
method public void sendSocketKeepaliveEvent(int, int);
method public void setConnected();
method @Deprecated public void setLegacyExtraInfo(@Nullable String);
@@ -6418,55 +6418,6 @@ package android.net {
method @NonNull @RequiresPermission(android.Manifest.permission.NETWORK_SIGNAL_STRENGTH_WAKEUP) public android.net.NetworkRequest.Builder setSignalStrength(int);
}
- public final class NetworkScore implements android.os.Parcelable {
- method public int describeContents();
- method @NonNull public android.net.NetworkScore.Metrics getEndToEndMetrics();
- method @NonNull public android.net.NetworkScore.Metrics getLinkLayerMetrics();
- method public int getRange();
- method @IntRange(from=android.net.NetworkScore.UNKNOWN_SIGNAL_STRENGTH, to=android.net.NetworkScore.MAX_SIGNAL_STRENGTH) public int getSignalStrength();
- method public boolean hasPolicy(int);
- method public boolean isExiting();
- method @NonNull public android.net.NetworkScore withExiting(boolean);
- method @NonNull public android.net.NetworkScore withSignalStrength(@IntRange(from=android.net.NetworkScore.UNKNOWN_SIGNAL_STRENGTH) int);
- method public void writeToParcel(@NonNull android.os.Parcel, int);
- field @NonNull public static final android.os.Parcelable.Creator<android.net.NetworkScore> CREATOR;
- field public static final int MAX_SIGNAL_STRENGTH = 1000; // 0x3e8
- field public static final int MIN_SIGNAL_STRENGTH = 0; // 0x0
- field public static final int POLICY_DEFAULT_SUBSCRIPTION = 8; // 0x8
- field public static final int POLICY_IGNORE_ON_WIFI = 4; // 0x4
- field public static final int POLICY_LOCKDOWN_VPN = 1; // 0x1
- field public static final int POLICY_VPN = 2; // 0x2
- field public static final int RANGE_CLOSE = 1; // 0x1
- field public static final int RANGE_LONG = 4; // 0x4
- field public static final int RANGE_MEDIUM = 3; // 0x3
- field public static final int RANGE_SHORT = 2; // 0x2
- field public static final int RANGE_UNKNOWN = 0; // 0x0
- field public static final int UNKNOWN_SIGNAL_STRENGTH = -1; // 0xffffffff
- }
-
- public static class NetworkScore.Builder {
- ctor public NetworkScore.Builder();
- method @NonNull public android.net.NetworkScore.Builder addPolicy(int);
- method @NonNull public android.net.NetworkScore build();
- method @NonNull public android.net.NetworkScore.Builder clearPolicy(int);
- method @NonNull public android.net.NetworkScore.Builder setEndToEndMetrics(@NonNull android.net.NetworkScore.Metrics);
- method @NonNull public android.net.NetworkScore.Builder setExiting(boolean);
- method @NonNull public android.net.NetworkScore.Builder setLegacyScore(int);
- method @NonNull public android.net.NetworkScore.Builder setLinkLayerMetrics(@NonNull android.net.NetworkScore.Metrics);
- method @NonNull public android.net.NetworkScore.Builder setRange(int);
- method @NonNull public android.net.NetworkScore.Builder setSignalStrength(@IntRange(from=android.net.NetworkScore.UNKNOWN_SIGNAL_STRENGTH, to=android.net.NetworkScore.MAX_SIGNAL_STRENGTH) int);
- }
-
- public static class NetworkScore.Metrics {
- ctor public NetworkScore.Metrics(@IntRange(from=android.net.NetworkScore.Metrics.LATENCY_UNKNOWN) int, @IntRange(from=android.net.NetworkScore.Metrics.BANDWIDTH_UNKNOWN) int, @IntRange(from=android.net.NetworkScore.Metrics.BANDWIDTH_UNKNOWN) int);
- field public static final int BANDWIDTH_UNKNOWN = -1; // 0xffffffff
- field @NonNull public static final android.net.NetworkScore.Metrics EMPTY;
- field public static final int LATENCY_UNKNOWN = -1; // 0xffffffff
- field @IntRange(from=android.net.NetworkScore.Metrics.BANDWIDTH_UNKNOWN) public final int downlinkBandwidthKBps;
- field @IntRange(from=android.net.NetworkScore.Metrics.LATENCY_UNKNOWN) public final int latencyMs;
- field @IntRange(from=android.net.NetworkScore.Metrics.BANDWIDTH_UNKNOWN) public final int uplinkBandwidthKBps;
- }
-
public class NetworkScoreManager {
method @RequiresPermission(anyOf={android.Manifest.permission.SCORE_NETWORKS, android.Manifest.permission.REQUEST_NETWORK_SCORES}) public boolean clearScores() throws java.lang.SecurityException;
method @RequiresPermission(anyOf={android.Manifest.permission.SCORE_NETWORKS, android.Manifest.permission.REQUEST_NETWORK_SCORES}) public void disableScoring() throws java.lang.SecurityException;
diff --git a/api/test-current.txt b/api/test-current.txt
index f25f10807753..9050833cbb47 100644
--- a/api/test-current.txt
+++ b/api/test-current.txt
@@ -1799,55 +1799,6 @@ package android.net {
field public static final int TRANSPORT_TEST = 7; // 0x7
}
- public final class NetworkScore implements android.os.Parcelable {
- method public int describeContents();
- method @NonNull public android.net.NetworkScore.Metrics getEndToEndMetrics();
- method @NonNull public android.net.NetworkScore.Metrics getLinkLayerMetrics();
- method public int getRange();
- method @IntRange(from=android.net.NetworkScore.UNKNOWN_SIGNAL_STRENGTH, to=android.net.NetworkScore.MAX_SIGNAL_STRENGTH) public int getSignalStrength();
- method public boolean hasPolicy(int);
- method public boolean isExiting();
- method @NonNull public android.net.NetworkScore withExiting(boolean);
- method @NonNull public android.net.NetworkScore withSignalStrength(@IntRange(from=android.net.NetworkScore.UNKNOWN_SIGNAL_STRENGTH) int);
- method public void writeToParcel(@NonNull android.os.Parcel, int);
- field @NonNull public static final android.os.Parcelable.Creator<android.net.NetworkScore> CREATOR;
- field public static final int MAX_SIGNAL_STRENGTH = 1000; // 0x3e8
- field public static final int MIN_SIGNAL_STRENGTH = 0; // 0x0
- field public static final int POLICY_DEFAULT_SUBSCRIPTION = 8; // 0x8
- field public static final int POLICY_IGNORE_ON_WIFI = 4; // 0x4
- field public static final int POLICY_LOCKDOWN_VPN = 1; // 0x1
- field public static final int POLICY_VPN = 2; // 0x2
- field public static final int RANGE_CLOSE = 1; // 0x1
- field public static final int RANGE_LONG = 4; // 0x4
- field public static final int RANGE_MEDIUM = 3; // 0x3
- field public static final int RANGE_SHORT = 2; // 0x2
- field public static final int RANGE_UNKNOWN = 0; // 0x0
- field public static final int UNKNOWN_SIGNAL_STRENGTH = -1; // 0xffffffff
- }
-
- public static class NetworkScore.Builder {
- ctor public NetworkScore.Builder();
- method @NonNull public android.net.NetworkScore.Builder addPolicy(int);
- method @NonNull public android.net.NetworkScore build();
- method @NonNull public android.net.NetworkScore.Builder clearPolicy(int);
- method @NonNull public android.net.NetworkScore.Builder setEndToEndMetrics(@NonNull android.net.NetworkScore.Metrics);
- method @NonNull public android.net.NetworkScore.Builder setExiting(boolean);
- method @NonNull public android.net.NetworkScore.Builder setLegacyScore(int);
- method @NonNull public android.net.NetworkScore.Builder setLinkLayerMetrics(@NonNull android.net.NetworkScore.Metrics);
- method @NonNull public android.net.NetworkScore.Builder setRange(int);
- method @NonNull public android.net.NetworkScore.Builder setSignalStrength(@IntRange(from=android.net.NetworkScore.UNKNOWN_SIGNAL_STRENGTH, to=android.net.NetworkScore.MAX_SIGNAL_STRENGTH) int);
- }
-
- public static class NetworkScore.Metrics {
- ctor public NetworkScore.Metrics(@IntRange(from=android.net.NetworkScore.Metrics.LATENCY_UNKNOWN) int, @IntRange(from=android.net.NetworkScore.Metrics.BANDWIDTH_UNKNOWN) int, @IntRange(from=android.net.NetworkScore.Metrics.BANDWIDTH_UNKNOWN) int);
- field public static final int BANDWIDTH_UNKNOWN = -1; // 0xffffffff
- field @NonNull public static final android.net.NetworkScore.Metrics EMPTY;
- field public static final int LATENCY_UNKNOWN = -1; // 0xffffffff
- field @IntRange(from=android.net.NetworkScore.Metrics.BANDWIDTH_UNKNOWN) public final int downlinkBandwidthKBps;
- field @IntRange(from=android.net.NetworkScore.Metrics.LATENCY_UNKNOWN) public final int latencyMs;
- field @IntRange(from=android.net.NetworkScore.Metrics.BANDWIDTH_UNKNOWN) public final int uplinkBandwidthKBps;
- }
-
public class NetworkStack {
field public static final String PERMISSION_MAINLINE_NETWORK_STACK = "android.permission.MAINLINE_NETWORK_STACK";
}
diff --git a/core/java/android/net/ConnectivityManager.java b/core/java/android/net/ConnectivityManager.java
index 62eff4522d70..38ef814561e6 100644
--- a/core/java/android/net/ConnectivityManager.java
+++ b/core/java/android/net/ConnectivityManager.java
@@ -3323,19 +3323,15 @@ public class ConnectivityManager {
// of dependent changes that would conflict throughout the automerger graph. Having this
// temporarily helps with the process of going through with all these dependent changes across
// the entire tree.
- // STOPSHIP (b/148055573) : remove this before R is released.
/**
* @hide
* Register a NetworkAgent with ConnectivityService.
* @return Network corresponding to NetworkAgent.
- * @deprecated use the version that takes a NetworkScore and a provider ID.
*/
@RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
- @Deprecated
public Network registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
NetworkCapabilities nc, int score, NetworkAgentConfig config) {
- final NetworkScore ns = new NetworkScore.Builder().setLegacyScore(score).build();
- return registerNetworkAgent(messenger, ni, lp, nc, ns, config, NetworkProvider.ID_NONE);
+ return registerNetworkAgent(messenger, ni, lp, nc, score, config, NetworkProvider.ID_NONE);
}
/**
@@ -3345,7 +3341,7 @@ public class ConnectivityManager {
*/
@RequiresPermission(android.Manifest.permission.NETWORK_FACTORY)
public Network registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
- NetworkCapabilities nc, NetworkScore score, NetworkAgentConfig config, int providerId) {
+ NetworkCapabilities nc, int score, NetworkAgentConfig config, int providerId) {
try {
return mService.registerNetworkAgent(messenger, ni, lp, nc, score, config, providerId);
} catch (RemoteException e) {
diff --git a/core/java/android/net/IConnectivityManager.aidl b/core/java/android/net/IConnectivityManager.aidl
index d84d05d522c2..14345608e94f 100644
--- a/core/java/android/net/IConnectivityManager.aidl
+++ b/core/java/android/net/IConnectivityManager.aidl
@@ -26,7 +26,6 @@ import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.NetworkQuotaInfo;
import android.net.NetworkRequest;
-import android.net.NetworkScore;
import android.net.NetworkState;
import android.net.ISocketKeepaliveCallback;
import android.net.ProxyInfo;
@@ -164,7 +163,7 @@ interface IConnectivityManager
void declareNetworkRequestUnfulfillable(in NetworkRequest request);
Network registerNetworkAgent(in Messenger messenger, in NetworkInfo ni, in LinkProperties lp,
- in NetworkCapabilities nc, in NetworkScore score, in NetworkAgentConfig config,
+ in NetworkCapabilities nc, int score, in NetworkAgentConfig config,
in int factorySerialNumber);
NetworkRequest requestNetwork(in NetworkCapabilities networkCapabilities,
diff --git a/core/java/android/net/NetworkAgent.java b/core/java/android/net/NetworkAgent.java
index ddf8dbbcb953..7cc569a42b0b 100644
--- a/core/java/android/net/NetworkAgent.java
+++ b/core/java/android/net/NetworkAgent.java
@@ -117,6 +117,13 @@ public abstract class NetworkAgent {
public static final int EVENT_NETWORK_PROPERTIES_CHANGED = BASE + 3;
/**
+ * Centralize the place where base network score, and network score scaling, will be
+ * stored, so as we can consistently compare apple and oranges, or wifi, ethernet and LTE
+ * @hide
+ */
+ public static final int WIFI_BASE_SCORE = 60;
+
+ /**
* Sent by the NetworkAgent to ConnectivityService to pass the current
* network score.
* obj = network score Integer
@@ -265,13 +272,7 @@ public abstract class NetworkAgent {
*/
public static final int CMD_REMOVE_KEEPALIVE_PACKET_FILTER = BASE + 17;
- // STOPSHIP (b/148055573) : remove this before R is released.
- private static NetworkScore makeNetworkScore(int score) {
- return new NetworkScore.Builder().setLegacyScore(score).build();
- }
-
/** @hide TODO: remove and replace usage with the public constructor. */
- // STOPSHIP (b/148055573) : remove this before R is released.
public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni,
NetworkCapabilities nc, LinkProperties lp, int score) {
this(looper, context, logTag, ni, nc, lp, score, null, NetworkProvider.ID_NONE);
@@ -279,7 +280,6 @@ public abstract class NetworkAgent {
}
/** @hide TODO: remove and replace usage with the public constructor. */
- // STOPSHIP (b/148055573) : remove this before R is released.
public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni,
NetworkCapabilities nc, LinkProperties lp, int score, NetworkAgentConfig config) {
this(looper, context, logTag, ni, nc, lp, score, config, NetworkProvider.ID_NONE);
@@ -287,7 +287,6 @@ public abstract class NetworkAgent {
}
/** @hide TODO: remove and replace usage with the public constructor. */
- // STOPSHIP (b/148055573) : remove this before R is released.
public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni,
NetworkCapabilities nc, LinkProperties lp, int score, int providerId) {
this(looper, context, logTag, ni, nc, lp, score, null, providerId);
@@ -295,12 +294,10 @@ public abstract class NetworkAgent {
}
/** @hide TODO: remove and replace usage with the public constructor. */
- // STOPSHIP (b/148055573) : remove this before R is released.
public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni,
NetworkCapabilities nc, LinkProperties lp, int score, NetworkAgentConfig config,
int providerId) {
- this(looper, context, logTag, nc, lp, makeNetworkScore(score), config, providerId, ni,
- true /* legacy */);
+ this(looper, context, logTag, nc, lp, score, config, providerId, ni, true /* legacy */);
register();
}
@@ -326,9 +323,8 @@ public abstract class NetworkAgent {
* @param provider the {@link NetworkProvider} managing this agent.
*/
public NetworkAgent(@NonNull Context context, @NonNull Looper looper, @NonNull String logTag,
- @NonNull NetworkCapabilities nc, @NonNull LinkProperties lp,
- @NonNull NetworkScore score, @NonNull NetworkAgentConfig config,
- @Nullable NetworkProvider provider) {
+ @NonNull NetworkCapabilities nc, @NonNull LinkProperties lp, int score,
+ @NonNull NetworkAgentConfig config, @Nullable NetworkProvider provider) {
this(looper, context, logTag, nc, lp, score, config,
provider == null ? NetworkProvider.ID_NONE : provider.getProviderId(),
getLegacyNetworkInfo(config), false /* legacy */);
@@ -338,12 +334,12 @@ public abstract class NetworkAgent {
public final Context context;
public final NetworkCapabilities capabilities;
public final LinkProperties properties;
- public final NetworkScore score;
+ public final int score;
public final NetworkAgentConfig config;
public final NetworkInfo info;
InitialConfiguration(@NonNull Context context, @NonNull NetworkCapabilities capabilities,
- @NonNull LinkProperties properties, @NonNull NetworkScore score,
- @NonNull NetworkAgentConfig config, @NonNull NetworkInfo info) {
+ @NonNull LinkProperties properties, int score, @NonNull NetworkAgentConfig config,
+ @NonNull NetworkInfo info) {
this.context = context;
this.capabilities = capabilities;
this.properties = properties;
@@ -355,7 +351,7 @@ public abstract class NetworkAgent {
private volatile InitialConfiguration mInitialConfiguration;
private NetworkAgent(@NonNull Looper looper, @NonNull Context context, @NonNull String logTag,
- @NonNull NetworkCapabilities nc, @NonNull LinkProperties lp, NetworkScore score,
+ @NonNull NetworkCapabilities nc, @NonNull LinkProperties lp, int score,
@NonNull NetworkAgentConfig config, int providerId, @NonNull NetworkInfo ni,
boolean legacy) {
mHandler = new NetworkAgentHandler(looper);
@@ -650,8 +646,22 @@ public abstract class NetworkAgent {
* Must be called by the agent to update the score of this network.
* @param score the new score.
*/
- public void sendNetworkScore(@NonNull final NetworkScore score) {
- queueOrSendMessage(EVENT_NETWORK_SCORE_CHANGED, score);
+ public void sendNetworkScore(int score) {
+ if (score < 0) {
+ throw new IllegalArgumentException("Score must be >= 0");
+ }
+ final NetworkScore ns = new NetworkScore();
+ ns.putIntExtension(NetworkScore.LEGACY_SCORE, score);
+ updateScore(ns);
+ }
+
+ /**
+ * Must be called by the agent when it has a new {@link NetworkScore} for this network.
+ * @param ns the new score.
+ * @hide TODO: unhide the NetworkScore class, and rename to sendNetworkScore.
+ */
+ public void updateScore(@NonNull NetworkScore ns) {
+ queueOrSendMessage(EVENT_NETWORK_SCORE_CHANGED, new NetworkScore(ns));
}
/**
diff --git a/core/java/android/net/NetworkScore.aidl b/core/java/android/net/NetworkScore.aidl
deleted file mode 100644
index be9a98b24e74..000000000000
--- a/core/java/android/net/NetworkScore.aidl
+++ /dev/null
@@ -1,19 +0,0 @@
-/**
- * Copyright (c) 2020, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package android.net;
-
-parcelable NetworkScore;
diff --git a/core/java/android/net/NetworkScore.java b/core/java/android/net/NetworkScore.java
index ae17378cfc4c..13f2994110a1 100644
--- a/core/java/android/net/NetworkScore.java
+++ b/core/java/android/net/NetworkScore.java
@@ -15,18 +15,12 @@
*/
package android.net;
-import android.annotation.IntDef;
-import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.Nullable;
-import android.annotation.SystemApi;
-import android.annotation.TestApi;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
import java.util.Objects;
/**
@@ -34,392 +28,57 @@ import java.util.Objects;
*
* A NetworkScore object represents the characteristics of a network that affects how good the
* network is considered for a particular use.
- *
- * This class is not thread-safe.
* @hide
*/
-@TestApi
-@SystemApi
public final class NetworkScore implements Parcelable {
- /** An object containing scoring-relevant metrics for a network. */
- public static class Metrics {
- /** Value meaning the latency is unknown. */
- public static final int LATENCY_UNKNOWN = -1;
-
- /** Value meaning the bandwidth is unknown. */
- public static final int BANDWIDTH_UNKNOWN = -1;
-
- /**
- * Round-trip delay in milliseconds to the relevant destination for this Metrics object.
- *
- * LATENCY_UNKNOWN if unknown.
- */
- @IntRange(from = LATENCY_UNKNOWN)
- public final int latencyMs;
-
- /**
- * Downlink in kB/s with the relevant destination for this Metrics object.
- *
- * BANDWIDTH_UNKNOWN if unknown. If directional bandwidth is unknown, up and downlink
- * bandwidth can have the same value.
- */
- @IntRange(from = BANDWIDTH_UNKNOWN)
- public final int downlinkBandwidthKBps;
- /**
- * Uplink in kB/s with the relevant destination for this Metrics object.
- *
- * BANDWIDTH_UNKNOWN if unknown. If directional bandwidth is unknown, up and downlink
- * bandwidth can have the same value.
- */
- @IntRange(from = BANDWIDTH_UNKNOWN)
- public final int uplinkBandwidthKBps;
-
- /** Constructor */
- public Metrics(@IntRange(from = LATENCY_UNKNOWN) final int latency,
- @IntRange(from = BANDWIDTH_UNKNOWN) final int downlinkBandwidth,
- @IntRange(from = BANDWIDTH_UNKNOWN) final int uplinkBandwidth) {
- latencyMs = latency;
- downlinkBandwidthKBps = downlinkBandwidth;
- uplinkBandwidthKBps = uplinkBandwidth;
- }
-
- /** toString */
- public String toString() {
- return "latency = " + latencyMs + " downlinkBandwidth = " + downlinkBandwidthKBps
- + "uplinkBandwidth = " + uplinkBandwidthKBps;
- }
-
- @NonNull
- public static final Metrics EMPTY =
- new Metrics(LATENCY_UNKNOWN, BANDWIDTH_UNKNOWN, BANDWIDTH_UNKNOWN);
- }
-
- /** @hide */
- @Retention(RetentionPolicy.SOURCE)
- @IntDef(flag = true, prefix = "POLICY_", value = {
- POLICY_LOCKDOWN_VPN,
- POLICY_VPN,
- POLICY_IGNORE_ON_WIFI,
- POLICY_DEFAULT_SUBSCRIPTION
- })
- public @interface Policy {
- }
-
- /**
- * This network is a VPN with lockdown enabled.
- *
- * If a network with this bit is present in the list of candidates and not connected,
- * no network can satisfy the request.
- */
- public static final int POLICY_LOCKDOWN_VPN = 1 << 0;
-
- /**
- * This network is a VPN.
- *
- * If a network with this bit is present and the request UID is included in the UID ranges
- * of this network, it outscores all other networks without this bit.
- */
- public static final int POLICY_VPN = 1 << 1;
-
- /**
- * This network should not be used if a previously validated WiFi network is available.
- *
- * If a network with this bit is present and a previously validated WiFi is present too, the
- * network with this bit is outscored by it. This stays true if the WiFi network
- * becomes unvalidated : this network will not be considered.
- */
- public static final int POLICY_IGNORE_ON_WIFI = 1 << 2;
-
- /**
- * This network is the default subscription for this transport.
- *
- * If a network with this bit is present, other networks of the same transport without this
- * bit are outscored by it. A device with two active subscriptions and a setting
- * to decide the default one would have this policy bit on the network for the default
- * subscription so that when both are active at the same time, the device chooses the
- * network associated with the default subscription rather than the one with the best link
- * quality (which will happen if policy doesn't dictate otherwise).
- */
- public static final int POLICY_DEFAULT_SUBSCRIPTION = 1 << 3;
-
- /**
- * Policy bits for this network. Filled in by the NetworkAgent.
- */
- private final int mPolicy;
-
- /**
- * Predicted metrics to the gateway (it contains latency and bandwidth to the gateway).
- * This is filled by the NetworkAgent with the theoretical values of the link if available,
- * although they may fill it with predictions from historical data if available.
- * Note that while this member cannot be null, any and all of its members can be unknown.
- */
+ // The key of bundle which is used to get the legacy network score of NetworkAgentInfo.
+ // TODO: Remove this when the transition to NetworkScore is over.
+ public static final String LEGACY_SCORE = "LEGACY_SCORE";
@NonNull
- private final Metrics mLinkLayerMetrics;
-
- /**
- * Predicted metrics to representative servers.
- * This is filled by connectivity with (if available) a best-effort estimate of the performance
- * information to servers the user connects to in similar circumstances, and predicted from
- * actual measurements if possible.
- * Note that while this member cannot be null, any and all of its members can be unknown.
- */
- @NonNull
- private final Metrics mEndToEndMetrics;
-
- /** Value meaning the signal strength is unknown. */
- public static final int UNKNOWN_SIGNAL_STRENGTH = -1;
-
- /** The smallest meaningful signal strength. */
- public static final int MIN_SIGNAL_STRENGTH = 0;
-
- /** The largest meaningful signal strength. */
- public static final int MAX_SIGNAL_STRENGTH = 1000;
-
- /**
- * User-visible measure of the strength of the signal normalized 1~1000.
- * This represents a measure of the signal strength for this network.
- * If unknown, this has value UNKNOWN_SIGNAL_STRENGTH.
- */
- // A good way to populate this value is to fill it with the number of bars displayed in
- // the system UI, scaled 0 to 1000. This is what the user sees and it makes sense to them.
- // Cellular for example could quantize the ASU value (see SignalStrength#getAsuValue) into
- // this, while WiFi could scale the RSSI (see WifiManager#calculateSignalLevel).
- @IntRange(from = UNKNOWN_SIGNAL_STRENGTH, to = MAX_SIGNAL_STRENGTH)
- private final int mSignalStrength;
-
- /** @hide */
- @Retention(RetentionPolicy.SOURCE)
- @IntDef(flag = true, prefix = "RANGE_", value = {
- RANGE_UNKNOWN, RANGE_CLOSE, RANGE_SHORT, RANGE_MEDIUM, RANGE_LONG
- })
- public @interface Range {
- }
-
- /**
- * The range of this network is not known.
- * This can be used by VPN the range of which depends on the range of the underlying network.
- */
- public static final int RANGE_UNKNOWN = 0;
-
- /**
- * This network typically only operates at close range, like an NFC beacon.
- */
- public static final int RANGE_CLOSE = 1;
-
- /**
- * This network typically operates at a range of a few meters to a few dozen meters, like WiFi.
- */
- public static final int RANGE_SHORT = 2;
-
- /**
- * This network typically operates at a range of a few dozen to a few hundred meters, like CBRS.
- */
- public static final int RANGE_MEDIUM = 3;
-
- /**
- * This network typically offers continuous connectivity up to many kilometers away, like LTE.
- */
- public static final int RANGE_LONG = 4;
+ private final Bundle mExtensions;
- /**
- * The typical range of this networking technology.
- *
- * This is one of the RANGE_* constants and is filled by the NetworkAgent.
- * This may be useful when evaluating how desirable a network is, because for two networks that
- * have equivalent performance and cost, the one that won't lose IP continuity when the user
- * moves is probably preferable.
- * Agents should fill this with the largest typical range this technology provides. See the
- * descriptions of the individual constants for guidance.
- *
- * If unknown, this is set to RANGE_UNKNOWN.
- */
- @Range private final int mRange;
-
- /**
- * A prediction of whether this network is likely to be unusable in a few seconds.
- *
- * NetworkAgents set this to true to mean they are aware that usability is limited due to
- * low signal strength, congestion, or other reasons, and indicates that the system should
- * only use this network as a last resort. An example would be a WiFi network when the device
- * is about to move outside of range.
- *
- * This is filled by the NetworkAgent. Agents that don't know whether this network is likely
- * to be unusable soon should set this to false.
- */
- private final boolean mExiting;
-
- /**
- * The legacy score, as a migration strategy from Q to R.
- * STOPSHIP : remove this before R ships.
- */
- private final int mLegacyScore;
-
- /**
- * Create a new NetworkScore object.
- */
- private NetworkScore(@Policy final int policy, @Nullable final Metrics l2Perf,
- @Nullable final Metrics e2ePerf,
- @IntRange(from = UNKNOWN_SIGNAL_STRENGTH, to = MAX_SIGNAL_STRENGTH)
- final int signalStrength,
- @Range final int range, final boolean exiting, final int legacyScore) {
- mPolicy = policy;
- mLinkLayerMetrics = null != l2Perf ? l2Perf : Metrics.EMPTY;
- mEndToEndMetrics = null != e2ePerf ? e2ePerf : Metrics.EMPTY;
- mSignalStrength = signalStrength;
- mRange = range;
- mExiting = exiting;
- mLegacyScore = legacyScore;
+ public NetworkScore() {
+ mExtensions = new Bundle();
}
- /**
- * Utility function to return a copy of this with a different exiting value.
- */
- @NonNull public NetworkScore withExiting(final boolean exiting) {
- return new NetworkScore(mPolicy, mLinkLayerMetrics, mEndToEndMetrics,
- mSignalStrength, mRange, exiting, mLegacyScore);
+ public NetworkScore(@NonNull NetworkScore source) {
+ mExtensions = new Bundle(source.mExtensions);
}
/**
- * Utility function to return a copy of this with a different signal strength.
+ * Put the value of parcelable inside the bundle by key.
*/
- @NonNull public NetworkScore withSignalStrength(
- @IntRange(from = UNKNOWN_SIGNAL_STRENGTH) final int signalStrength) {
- return new NetworkScore(mPolicy, mLinkLayerMetrics, mEndToEndMetrics,
- signalStrength, mRange, mExiting, mLegacyScore);
+ public void putExtension(@Nullable String key, @Nullable Parcelable value) {
+ mExtensions.putParcelable(key, value);
}
/**
- * Returns whether this network has a particular policy flag.
- * @param policy the policy, as one of the POLICY_* constants.
+ * Put the value of int inside the bundle by key.
*/
- public boolean hasPolicy(@Policy final int policy) {
- return 0 != (mPolicy & policy);
+ public void putIntExtension(@Nullable String key, int value) {
+ mExtensions.putInt(key, value);
}
/**
- * Returns the Metrics representing the performance of the link layer.
- *
- * This contains the theoretical performance of the link, if available.
- * Note that while this function cannot return null, any and/or all of the members of the
- * returned object can be null if unknown.
+ * Get the value of non primitive type by key.
*/
- @NonNull public Metrics getLinkLayerMetrics() {
- return mLinkLayerMetrics;
+ public <T extends Parcelable> T getExtension(@Nullable String key) {
+ return mExtensions.getParcelable(key);
}
/**
- * Returns the Metrics representing the end-to-end performance of the network.
- *
- * This contains the end-to-end performance of the link, if available.
- * Note that while this function cannot return null, any and/or all of the members of the
- * returned object can be null if unknown.
+ * Get the value of int by key.
*/
- @NonNull public Metrics getEndToEndMetrics() {
- return mEndToEndMetrics;
+ public int getIntExtension(@Nullable String key) {
+ return mExtensions.getInt(key);
}
/**
- * Returns the signal strength of this network normalized 0~1000, or UNKNOWN_SIGNAL_STRENGTH.
+ * Remove the entry by given key.
*/
- @IntRange(from = UNKNOWN_SIGNAL_STRENGTH, to = MAX_SIGNAL_STRENGTH)
- public int getSignalStrength() {
- return mSignalStrength;
- }
-
- /**
- * Returns the typical range of this network technology as one of the RANGE_* constants.
- */
- @Range public int getRange() {
- return mRange;
- }
-
- /** Returns a prediction of whether this network is likely to be unusable in a few seconds. */
- public boolean isExiting() {
- return mExiting;
- }
-
- /**
- * Get the legacy score.
- * @hide
- */
- public int getLegacyScore() {
- return mLegacyScore;
- }
-
- /** Builder for NetworkScore. */
- public static class Builder {
- private int mPolicy = 0;
- @NonNull
- private Metrics mLinkLayerMetrics = new Metrics(Metrics.LATENCY_UNKNOWN,
- Metrics.BANDWIDTH_UNKNOWN, Metrics.BANDWIDTH_UNKNOWN);
- @NonNull
- private Metrics mEndToMetrics = new Metrics(Metrics.LATENCY_UNKNOWN,
- Metrics.BANDWIDTH_UNKNOWN, Metrics.BANDWIDTH_UNKNOWN);
- private int mSignalStrength = UNKNOWN_SIGNAL_STRENGTH;
- private int mRange = RANGE_UNKNOWN;
- private boolean mExiting = false;
- private int mLegacyScore = 0;
- @NonNull private Bundle mExtensions = new Bundle();
-
- /** Create a new builder. */
- public Builder() { }
-
- /** Add a policy flag. */
- @NonNull public Builder addPolicy(@Policy final int policy) {
- mPolicy |= policy;
- return this;
- }
-
- /** Clear a policy flag */
- @NonNull public Builder clearPolicy(@Policy final int policy) {
- mPolicy &= ~policy;
- return this;
- }
-
- /** Set the link layer metrics. */
- @NonNull public Builder setLinkLayerMetrics(@NonNull final Metrics linkLayerMetrics) {
- mLinkLayerMetrics = linkLayerMetrics;
- return this;
- }
-
- /** Set the end-to-end metrics. */
- @NonNull public Builder setEndToEndMetrics(@NonNull final Metrics endToEndMetrics) {
- mEndToMetrics = endToEndMetrics;
- return this;
- }
-
- /** Set the signal strength. */
- @NonNull public Builder setSignalStrength(
- @IntRange(from = UNKNOWN_SIGNAL_STRENGTH, to = MAX_SIGNAL_STRENGTH)
- final int signalStrength) {
- mSignalStrength = signalStrength;
- return this;
- }
-
- /** Set the range. */
- @NonNull public Builder setRange(@Range final int range) {
- mRange = range;
- return this;
- }
-
- /** Set whether this network is exiting. */
- @NonNull public Builder setExiting(final boolean exiting) {
- mExiting = exiting;
- return this;
- }
-
- /** Add a parcelable extension. */
- @NonNull public Builder setLegacyScore(final int legacyScore) {
- mLegacyScore = legacyScore;
- return this;
- }
-
- /** Build the NetworkScore object represented by this builder. */
- @NonNull public NetworkScore build() {
- return new NetworkScore(mPolicy, mLinkLayerMetrics, mEndToMetrics,
- mSignalStrength, mRange, mExiting, mLegacyScore);
- }
+ public void removeExtension(@Nullable String key) {
+ mExtensions.remove(key);
}
@Override
@@ -429,17 +88,9 @@ public final class NetworkScore implements Parcelable {
@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
- dest.writeInt(mPolicy);
- dest.writeInt(mLinkLayerMetrics.latencyMs);
- dest.writeInt(mLinkLayerMetrics.downlinkBandwidthKBps);
- dest.writeInt(mLinkLayerMetrics.uplinkBandwidthKBps);
- dest.writeInt(mEndToEndMetrics.latencyMs);
- dest.writeInt(mEndToEndMetrics.downlinkBandwidthKBps);
- dest.writeInt(mEndToEndMetrics.uplinkBandwidthKBps);
- dest.writeInt(mSignalStrength);
- dest.writeInt(mRange);
- dest.writeBoolean(mExiting);
- dest.writeInt(mLegacyScore);
+ synchronized (this) {
+ dest.writeBundle(mExtensions);
+ }
}
public static final @NonNull Creator<NetworkScore> CREATOR = new Creator<NetworkScore>() {
@@ -455,52 +106,57 @@ public final class NetworkScore implements Parcelable {
};
private NetworkScore(@NonNull Parcel in) {
- mPolicy = in.readInt();
- mLinkLayerMetrics = new Metrics(in.readInt(), in.readInt(), in.readInt());
- mEndToEndMetrics = new Metrics(in.readInt(), in.readInt(), in.readInt());
- mSignalStrength = in.readInt();
- mRange = in.readInt();
- mExiting = in.readBoolean();
- mLegacyScore = in.readInt();
+ mExtensions = in.readBundle();
}
+ // TODO: Modify this method once new fields are added into this class.
@Override
public boolean equals(@Nullable Object obj) {
if (!(obj instanceof NetworkScore)) {
return false;
}
final NetworkScore other = (NetworkScore) obj;
- return mPolicy == other.mPolicy
- && mLinkLayerMetrics.latencyMs == other.mLinkLayerMetrics.latencyMs
- && mLinkLayerMetrics.uplinkBandwidthKBps
- == other.mLinkLayerMetrics.uplinkBandwidthKBps
- && mEndToEndMetrics.latencyMs == other.mEndToEndMetrics.latencyMs
- && mEndToEndMetrics.uplinkBandwidthKBps
- == other.mEndToEndMetrics.uplinkBandwidthKBps
- && mSignalStrength == other.mSignalStrength
- && mRange == other.mRange
- && mExiting == other.mExiting
- && mLegacyScore == other.mLegacyScore;
+ return bundlesEqual(mExtensions, other.mExtensions);
}
@Override
public int hashCode() {
- return Objects.hash(mPolicy,
- mLinkLayerMetrics.latencyMs, mLinkLayerMetrics.uplinkBandwidthKBps,
- mEndToEndMetrics.latencyMs, mEndToEndMetrics.uplinkBandwidthKBps,
- mSignalStrength, mRange, mExiting, mLegacyScore);
+ int result = 29;
+ for (String key : mExtensions.keySet()) {
+ final Object value = mExtensions.get(key);
+ // The key may be null, so call Objects.hash() is safer.
+ result += 31 * value.hashCode() + 37 * Objects.hash(key);
+ }
+ return result;
+ }
+
+ // mExtensions won't be null since the constructor will create it.
+ private boolean bundlesEqual(@NonNull Bundle bundle1, @NonNull Bundle bundle2) {
+ if (bundle1 == bundle2) {
+ return true;
+ }
+
+ // This is unlikely but it's fine to add this clause here.
+ if (null == bundle1 || null == bundle2) {
+ return false;
+ }
+
+ if (bundle1.size() != bundle2.size()) {
+ return false;
+ }
+
+ for (String key : bundle1.keySet()) {
+ final Object value1 = bundle1.get(key);
+ final Object value2 = bundle2.get(key);
+ if (!Objects.equals(value1, value2)) {
+ return false;
+ }
+ }
+ return true;
}
/** Convert to a string */
public String toString() {
- return "NetworkScore ["
- + "Policy = " + mPolicy
- + " LinkLayerMetrics = " + mLinkLayerMetrics
- + " EndToEndMetrics = " + mEndToEndMetrics
- + " SignalStrength = " + mSignalStrength
- + " Range = " + mRange
- + " Exiting = " + mExiting
- + " LegacyScore = " + mLegacyScore
- + "]";
+ return "NetworkScore[" + mExtensions.toString() + "]";
}
}
diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java
index f84d35fb7776..16dd3ada12c7 100644
--- a/services/core/java/com/android/server/ConnectivityService.java
+++ b/services/core/java/com/android/server/ConnectivityService.java
@@ -5779,6 +5779,20 @@ public class ConnectivityService extends IConnectivityManager.Stub
return nri.request.requestId == mDefaultRequest.requestId;
}
+ // TODO : remove this method. It's a stopgap measure to help sheperding a number of dependent
+ // changes that would conflict throughout the automerger graph. Having this method temporarily
+ // helps with the process of going through with all these dependent changes across the entire
+ // tree.
+ /**
+ * Register a new agent. {@see #registerNetworkAgent} below.
+ */
+ public Network registerNetworkAgent(Messenger messenger, NetworkInfo networkInfo,
+ LinkProperties linkProperties, NetworkCapabilities networkCapabilities,
+ int currentScore, NetworkAgentConfig networkAgentConfig) {
+ return registerNetworkAgent(messenger, networkInfo, linkProperties, networkCapabilities,
+ currentScore, networkAgentConfig, NetworkProvider.ID_NONE);
+ }
+
/**
* Register a new agent with ConnectivityService to handle a network.
*
@@ -5797,7 +5811,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
*/
public Network registerNetworkAgent(Messenger messenger, NetworkInfo networkInfo,
LinkProperties linkProperties, NetworkCapabilities networkCapabilities,
- NetworkScore currentScore, NetworkAgentConfig networkAgentConfig, int providerId) {
+ int currentScore, NetworkAgentConfig networkAgentConfig, int providerId) {
enforceNetworkFactoryPermission();
LinkProperties lp = new LinkProperties(linkProperties);
@@ -5805,10 +5819,12 @@ public class ConnectivityService extends IConnectivityManager.Stub
// TODO: Instead of passing mDefaultRequest, provide an API to determine whether a Network
// satisfies mDefaultRequest.
final NetworkCapabilities nc = new NetworkCapabilities(networkCapabilities);
+ final NetworkScore ns = new NetworkScore();
+ ns.putIntExtension(NetworkScore.LEGACY_SCORE, currentScore);
final NetworkAgentInfo nai = new NetworkAgentInfo(messenger, new AsyncChannel(),
new Network(mNetIdManager.reserveNetId()), new NetworkInfo(networkInfo), lp, nc,
- currentScore, mContext, mTrackerHandler, new NetworkAgentConfig(networkAgentConfig),
- this, mNetd, mDnsResolver, mNMS, providerId);
+ ns, mContext, mTrackerHandler, new NetworkAgentConfig(networkAgentConfig), this,
+ mNetd, mDnsResolver, mNMS, providerId);
// Make sure the network capabilities reflect what the agent info says.
nai.getAndSetNetworkCapabilities(mixInCapabilities(nai, nc));
final String extraInfo = networkInfo.getExtraInfo();
diff --git a/services/core/java/com/android/server/connectivity/NetworkAgentInfo.java b/services/core/java/com/android/server/connectivity/NetworkAgentInfo.java
index 4612cfd0f7cb..58b5cba477da 100644
--- a/services/core/java/com/android/server/connectivity/NetworkAgentInfo.java
+++ b/services/core/java/com/android/server/connectivity/NetworkAgentInfo.java
@@ -491,7 +491,7 @@ public class NetworkAgentInfo implements Comparable<NetworkAgentInfo> {
return ConnectivityConstants.EXPLICITLY_SELECTED_NETWORK_SCORE;
}
- int score = mNetworkScore.getLegacyScore();
+ int score = mNetworkScore.getIntExtension(NetworkScore.LEGACY_SCORE);
if (!lastValidated && !pretendValidated && !ignoreWifiUnvalidationPenalty() && !isVPN()) {
score -= ConnectivityConstants.UNVALIDATED_SCORE_PENALTY;
}
diff --git a/tests/net/common/java/android/net/NetworkScoreTest.kt b/tests/net/common/java/android/net/NetworkScoreTest.kt
index a63d58d5a0f6..30836b7c9be1 100644
--- a/tests/net/common/java/android/net/NetworkScoreTest.kt
+++ b/tests/net/common/java/android/net/NetworkScoreTest.kt
@@ -16,7 +16,7 @@
package android.net
-import android.net.NetworkScore.Metrics.BANDWIDTH_UNKNOWN
+import android.os.Parcelable
import androidx.test.filters.SmallTest
import androidx.test.runner.AndroidJUnit4
import com.android.testutils.assertParcelSane
@@ -28,49 +28,77 @@ import org.junit.Test
import org.junit.runner.RunWith
private const val TEST_SCORE = 80
+private const val KEY_DEFAULT_CAPABILITIES = "DEFAULT_CAPABILITIES"
@RunWith(AndroidJUnit4::class)
@SmallTest
class NetworkScoreTest {
@Test
fun testParcelNetworkScore() {
+ val networkScore = NetworkScore()
val defaultCap = NetworkCapabilities()
- val builder = NetworkScore.Builder().setLegacyScore(TEST_SCORE)
- assertEquals(TEST_SCORE, builder.build().getLegacyScore())
- assertParcelSane(builder.build(), 7)
+ networkScore.putExtension(KEY_DEFAULT_CAPABILITIES, defaultCap)
+ assertEquals(defaultCap, networkScore.getExtension(KEY_DEFAULT_CAPABILITIES))
+ networkScore.putIntExtension(NetworkScore.LEGACY_SCORE, TEST_SCORE)
+ assertEquals(TEST_SCORE, networkScore.getIntExtension(NetworkScore.LEGACY_SCORE))
+ assertParcelSane(networkScore, 1)
+ }
- builder.addPolicy(NetworkScore.POLICY_IGNORE_ON_WIFI)
- .addPolicy(NetworkScore.POLICY_DEFAULT_SUBSCRIPTION)
- .setLinkLayerMetrics(NetworkScore.Metrics(44 /* latency */,
- 380 /* downlinkBandwidth */, BANDWIDTH_UNKNOWN /* uplinkBandwidth */))
- .setEndToEndMetrics(NetworkScore.Metrics(11 /* latency */,
- BANDWIDTH_UNKNOWN /* downlinkBandwidth */, 100_000 /* uplinkBandwidth */))
- .setRange(NetworkScore.RANGE_MEDIUM)
- assertParcelSane(builder.build(), 7)
- builder.clearPolicy(NetworkScore.POLICY_IGNORE_ON_WIFI)
- val ns = builder.build()
- assertParcelSane(ns, 7)
- assertFalse(ns.hasPolicy(NetworkScore.POLICY_IGNORE_ON_WIFI))
- assertTrue(ns.hasPolicy(NetworkScore.POLICY_DEFAULT_SUBSCRIPTION))
+ @Test
+ fun testNullKeyAndValue() {
+ val networkScore = NetworkScore()
+ val defaultCap = NetworkCapabilities()
+ networkScore.putIntExtension(null, TEST_SCORE)
+ assertEquals(TEST_SCORE, networkScore.getIntExtension(null))
+ networkScore.putExtension(null, defaultCap)
+ assertEquals(defaultCap, networkScore.getExtension(null))
+ networkScore.putExtension(null, null)
+ val result: Parcelable? = networkScore.getExtension(null)
+ assertEquals(null, result)
+ }
- val exitingNs = ns.withExiting(true)
- assertNotEquals(ns.isExiting, exitingNs.isExiting)
- assertNotEquals(ns, exitingNs)
- assertParcelSane(exitingNs, 7)
+ @Test
+ fun testRemoveExtension() {
+ val networkScore = NetworkScore()
+ val defaultCap = NetworkCapabilities()
+ networkScore.putExtension(KEY_DEFAULT_CAPABILITIES, defaultCap)
+ networkScore.putIntExtension(NetworkScore.LEGACY_SCORE, TEST_SCORE)
+ assertEquals(defaultCap, networkScore.getExtension(KEY_DEFAULT_CAPABILITIES))
+ assertEquals(TEST_SCORE, networkScore.getIntExtension(NetworkScore.LEGACY_SCORE))
+ networkScore.removeExtension(KEY_DEFAULT_CAPABILITIES)
+ networkScore.removeExtension(NetworkScore.LEGACY_SCORE)
+ val result: Parcelable? = networkScore.getExtension(KEY_DEFAULT_CAPABILITIES)
+ assertEquals(null, result)
+ assertEquals(0, networkScore.getIntExtension(NetworkScore.LEGACY_SCORE))
}
@Test
fun testEqualsNetworkScore() {
- val builder1 = NetworkScore.Builder()
- val builder2 = NetworkScore.Builder()
- assertTrue(builder1.build().equals(builder2.build()))
- assertEquals(builder1.build().hashCode(), builder2.build().hashCode())
+ val ns1 = NetworkScore()
+ val ns2 = NetworkScore()
+ assertTrue(ns1.equals(ns2))
+ assertEquals(ns1.hashCode(), ns2.hashCode())
+
+ ns1.putIntExtension(NetworkScore.LEGACY_SCORE, TEST_SCORE)
+ assertFalse(ns1.equals(ns2))
+ assertNotEquals(ns1.hashCode(), ns2.hashCode())
+ ns2.putIntExtension(NetworkScore.LEGACY_SCORE, TEST_SCORE)
+ assertTrue(ns1.equals(ns2))
+ assertEquals(ns1.hashCode(), ns2.hashCode())
+
+ val defaultCap = NetworkCapabilities()
+ ns1.putExtension(KEY_DEFAULT_CAPABILITIES, defaultCap)
+ assertFalse(ns1.equals(ns2))
+ assertNotEquals(ns1.hashCode(), ns2.hashCode())
+ ns2.putExtension(KEY_DEFAULT_CAPABILITIES, defaultCap)
+ assertTrue(ns1.equals(ns2))
+ assertEquals(ns1.hashCode(), ns2.hashCode())
- builder1.setLegacyScore(TEST_SCORE)
- assertFalse(builder1.build().equals(builder2.build()))
- assertNotEquals(builder1.hashCode(), builder2.hashCode())
- builder2.setLegacyScore(TEST_SCORE)
- assertTrue(builder1.build().equals(builder2.build()))
- assertEquals(builder1.build().hashCode(), builder2.build().hashCode())
+ ns1.putIntExtension(null, 10)
+ assertFalse(ns1.equals(ns2))
+ assertNotEquals(ns1.hashCode(), ns2.hashCode())
+ ns2.putIntExtension(null, 10)
+ assertTrue(ns1.equals(ns2))
+ assertEquals(ns1.hashCode(), ns2.hashCode())
}
}
diff --git a/tests/net/integration/util/com/android/server/NetworkAgentWrapper.java b/tests/net/integration/util/com/android/server/NetworkAgentWrapper.java
index 7ae9e95a520f..a35fb407bca9 100644
--- a/tests/net/integration/util/com/android/server/NetworkAgentWrapper.java
+++ b/tests/net/integration/util/com/android/server/NetworkAgentWrapper.java
@@ -40,7 +40,6 @@ import android.net.NetworkAgentConfig;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.NetworkProvider;
-import android.net.NetworkScore;
import android.net.NetworkSpecifier;
import android.net.SocketKeepalive;
import android.net.UidRange;
@@ -156,13 +155,9 @@ public class NetworkAgentWrapper implements TestableNetworkCallback.HasNetwork {
}
}
- private NetworkScore makeNetworkScore(final int legacyScore) {
- return new NetworkScore.Builder().setLegacyScore(legacyScore).build();
- }
-
public void adjustScore(int change) {
mScore += change;
- mNetworkAgent.sendNetworkScore(makeNetworkScore(mScore));
+ mNetworkAgent.sendNetworkScore(mScore);
}
public int getScore() {
diff --git a/tests/net/java/com/android/server/connectivity/LingerMonitorTest.java b/tests/net/java/com/android/server/connectivity/LingerMonitorTest.java
index 25e9057fd13c..e863266c4b49 100644
--- a/tests/net/java/com/android/server/connectivity/LingerMonitorTest.java
+++ b/tests/net/java/com/android/server/connectivity/LingerMonitorTest.java
@@ -353,7 +353,8 @@ public class LingerMonitorTest {
NetworkCapabilities caps = new NetworkCapabilities();
caps.addCapability(0);
caps.addTransportType(transport);
- NetworkScore ns = new NetworkScore.Builder().setLegacyScore(50).build();
+ NetworkScore ns = new NetworkScore();
+ ns.putIntExtension(NetworkScore.LEGACY_SCORE, 50);
NetworkAgentInfo nai = new NetworkAgentInfo(null, null, new Network(netId), info, null,
caps, ns, mCtx, null, null /* config */, mConnService, mNetd, mDnsResolver, mNMS,
NetworkProvider.ID_NONE);