diff --git a/core/java/android/net/NetworkAgent.java b/core/java/android/net/NetworkAgent.java index 7cc569a42b..fef353f604 100644 --- a/core/java/android/net/NetworkAgent.java +++ b/core/java/android/net/NetworkAgent.java @@ -126,7 +126,7 @@ public abstract class NetworkAgent { /** * Sent by the NetworkAgent to ConnectivityService to pass the current * network score. - * obj = network score Integer + * arg1 = network score int * @hide */ public static final int EVENT_NETWORK_SCORE_CHANGED = BASE + 4; @@ -650,18 +650,7 @@ public abstract class NetworkAgent { 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)); + queueOrSendMessage(EVENT_NETWORK_SCORE_CHANGED, score, 0); } /** diff --git a/core/java/android/net/NetworkScore.java b/core/java/android/net/NetworkScore.java deleted file mode 100644 index 13f2994110..0000000000 --- a/core/java/android/net/NetworkScore.java +++ /dev/null @@ -1,162 +0,0 @@ -/* - * Copyright (C) 2019 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; - -import android.annotation.NonNull; -import android.annotation.Nullable; -import android.os.Bundle; -import android.os.Parcel; -import android.os.Parcelable; - -import java.util.Objects; - -/** - * Object representing the quality of a network as perceived by the user. - * - * A NetworkScore object represents the characteristics of a network that affects how good the - * network is considered for a particular use. - * @hide - */ -public final class NetworkScore implements Parcelable { - - // 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 Bundle mExtensions; - - public NetworkScore() { - mExtensions = new Bundle(); - } - - public NetworkScore(@NonNull NetworkScore source) { - mExtensions = new Bundle(source.mExtensions); - } - - /** - * Put the value of parcelable inside the bundle by key. - */ - public void putExtension(@Nullable String key, @Nullable Parcelable value) { - mExtensions.putParcelable(key, value); - } - - /** - * Put the value of int inside the bundle by key. - */ - public void putIntExtension(@Nullable String key, int value) { - mExtensions.putInt(key, value); - } - - /** - * Get the value of non primitive type by key. - */ - public T getExtension(@Nullable String key) { - return mExtensions.getParcelable(key); - } - - /** - * Get the value of int by key. - */ - public int getIntExtension(@Nullable String key) { - return mExtensions.getInt(key); - } - - /** - * Remove the entry by given key. - */ - public void removeExtension(@Nullable String key) { - mExtensions.remove(key); - } - - @Override - public int describeContents() { - return 0; - } - - @Override - public void writeToParcel(@NonNull Parcel dest, int flags) { - synchronized (this) { - dest.writeBundle(mExtensions); - } - } - - public static final @NonNull Creator CREATOR = new Creator() { - @Override - public NetworkScore createFromParcel(@NonNull Parcel in) { - return new NetworkScore(in); - } - - @Override - public NetworkScore[] newArray(int size) { - return new NetworkScore[size]; - } - }; - - private NetworkScore(@NonNull Parcel in) { - 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 bundlesEqual(mExtensions, other.mExtensions); - } - - @Override - public int hashCode() { - 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[" + mExtensions.toString() + "]"; - } -} diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java index a3fb55cec5..e251658b10 100644 --- a/services/core/java/com/android/server/ConnectivityService.java +++ b/services/core/java/com/android/server/ConnectivityService.java @@ -104,7 +104,6 @@ import android.net.NetworkPolicyManager; import android.net.NetworkProvider; import android.net.NetworkQuotaInfo; import android.net.NetworkRequest; -import android.net.NetworkScore; import android.net.NetworkSpecifier; import android.net.NetworkStack; import android.net.NetworkStackClient; @@ -2724,8 +2723,7 @@ public class ConnectivityService extends IConnectivityManager.Stub break; } case NetworkAgent.EVENT_NETWORK_SCORE_CHANGED: { - final NetworkScore ns = (NetworkScore) msg.obj; - updateNetworkScore(nai, ns); + updateNetworkScore(nai, msg.arg1); break; } case NetworkAgent.EVENT_SET_EXPLICITLY_SELECTED: { @@ -5812,12 +5810,10 @@ 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, - ns, mContext, mTrackerHandler, new NetworkAgentConfig(networkAgentConfig), this, - mNetd, mDnsResolver, mNMS, providerId); + currentScore, 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(); @@ -7075,9 +7071,9 @@ public class ConnectivityService extends IConnectivityManager.Stub } } - private void updateNetworkScore(NetworkAgentInfo nai, NetworkScore ns) { - if (VDBG || DDBG) log("updateNetworkScore for " + nai.toShortString() + " to " + ns); - nai.setNetworkScore(ns); + private void updateNetworkScore(@NonNull final NetworkAgentInfo nai, final int score) { + if (VDBG || DDBG) log("updateNetworkScore for " + nai.toShortString() + " to " + score); + nai.setScore(score); rematchAllNetworksAndRequests(); sendUpdatedScoreToFactories(nai); } diff --git a/services/core/java/com/android/server/connectivity/NetworkAgentInfo.java b/services/core/java/com/android/server/connectivity/NetworkAgentInfo.java index 58b5cba477..23b954c03c 100644 --- a/services/core/java/com/android/server/connectivity/NetworkAgentInfo.java +++ b/services/core/java/com/android/server/connectivity/NetworkAgentInfo.java @@ -32,7 +32,6 @@ import android.net.NetworkCapabilities; import android.net.NetworkInfo; import android.net.NetworkMonitorManager; import android.net.NetworkRequest; -import android.net.NetworkScore; import android.net.NetworkState; import android.os.Handler; import android.os.INetworkManagementService; @@ -236,10 +235,8 @@ public class NetworkAgentInfo implements Comparable { // validated). private boolean mLingering; - // This represents the characteristics of a network that affects how good the network is - // considered for a particular use. - @NonNull - private NetworkScore mNetworkScore; + // This represents the quality of the network with no clear scale. + private int mScore; // The list of NetworkRequests being satisfied by this Network. private final SparseArray mNetworkRequests = new SparseArray<>(); @@ -268,7 +265,7 @@ public class NetworkAgentInfo implements Comparable { private final Handler mHandler; public NetworkAgentInfo(Messenger messenger, AsyncChannel ac, Network net, NetworkInfo info, - LinkProperties lp, NetworkCapabilities nc, @NonNull NetworkScore ns, Context context, + LinkProperties lp, NetworkCapabilities nc, int score, Context context, Handler handler, NetworkAgentConfig config, ConnectivityService connService, INetd netd, IDnsResolver dnsResolver, INetworkManagementService nms, int factorySerialNumber) { this.messenger = messenger; @@ -277,7 +274,7 @@ public class NetworkAgentInfo implements Comparable { networkInfo = info; linkProperties = lp; networkCapabilities = nc; - mNetworkScore = ns; + mScore = score; clatd = new Nat464Xlat(this, netd, dnsResolver, nms); mConnService = connService; mContext = context; @@ -491,7 +488,7 @@ public class NetworkAgentInfo implements Comparable { return ConnectivityConstants.EXPLICITLY_SELECTED_NETWORK_SCORE; } - int score = mNetworkScore.getIntExtension(NetworkScore.LEGACY_SCORE); + int score = mScore; if (!lastValidated && !pretendValidated && !ignoreWifiUnvalidationPenalty() && !isVPN()) { score -= ConnectivityConstants.UNVALIDATED_SCORE_PENALTY; } @@ -520,13 +517,8 @@ public class NetworkAgentInfo implements Comparable { return getCurrentScore(true); } - public void setNetworkScore(@NonNull NetworkScore ns) { - mNetworkScore = ns; - } - - @NonNull - public NetworkScore getNetworkScore() { - return mNetworkScore; + public void setScore(final int score) { + mScore = score; } public NetworkState getNetworkState() { diff --git a/tests/net/java/com/android/server/ConnectivityServiceTest.java b/tests/net/java/com/android/server/ConnectivityServiceTest.java index 1efc2a5602..c2c3ba3e8b 100644 --- a/tests/net/java/com/android/server/ConnectivityServiceTest.java +++ b/tests/net/java/com/android/server/ConnectivityServiceTest.java @@ -6750,7 +6750,7 @@ public class ConnectivityServiceTest { public void testCheckConnectivityDiagnosticsPermissionsNetworkStack() throws Exception { final NetworkAgentInfo naiWithoutUid = new NetworkAgentInfo( - null, null, null, null, null, new NetworkCapabilities(), null, + null, null, null, null, null, new NetworkCapabilities(), 0, mServiceContext, null, null, mService, null, null, null, 0); mServiceContext.setPermission( @@ -6766,7 +6766,7 @@ public class ConnectivityServiceTest { public void testCheckConnectivityDiagnosticsPermissionsNoLocationPermission() throws Exception { final NetworkAgentInfo naiWithoutUid = new NetworkAgentInfo( - null, null, null, null, null, new NetworkCapabilities(), null, + null, null, null, null, null, new NetworkCapabilities(), 0, mServiceContext, null, null, mService, null, null, null, 0); mServiceContext.setPermission(android.Manifest.permission.NETWORK_STACK, PERMISSION_DENIED); @@ -6782,7 +6782,7 @@ public class ConnectivityServiceTest { public void testCheckConnectivityDiagnosticsPermissionsActiveVpn() throws Exception { final NetworkAgentInfo naiWithoutUid = new NetworkAgentInfo( - null, null, null, null, null, new NetworkCapabilities(), null, + null, null, null, null, null, new NetworkCapabilities(), 0, mServiceContext, null, null, mService, null, null, null, 0); setupLocationPermissions(Build.VERSION_CODES.Q, true, AppOpsManager.OPSTR_FINE_LOCATION, @@ -6808,7 +6808,7 @@ public class ConnectivityServiceTest { nc.setAdministratorUids(Arrays.asList(Process.myUid())); final NetworkAgentInfo naiWithUid = new NetworkAgentInfo( - null, null, null, null, null, nc, null, mServiceContext, null, null, + null, null, null, null, null, nc, 0, mServiceContext, null, null, mService, null, null, null, 0); setupLocationPermissions(Build.VERSION_CODES.Q, true, AppOpsManager.OPSTR_FINE_LOCATION, @@ -6830,7 +6830,7 @@ public class ConnectivityServiceTest { nc.setAdministratorUids(Arrays.asList(Process.myUid())); final NetworkAgentInfo naiWithUid = new NetworkAgentInfo( - null, null, null, null, null, nc, null, mServiceContext, null, null, + null, null, null, null, null, nc, 0, mServiceContext, null, null, mService, null, null, null, 0); setupLocationPermissions(Build.VERSION_CODES.Q, true, AppOpsManager.OPSTR_FINE_LOCATION, diff --git a/tests/net/java/com/android/server/connectivity/LingerMonitorTest.java b/tests/net/java/com/android/server/connectivity/LingerMonitorTest.java index e863266c4b..24a8717722 100644 --- a/tests/net/java/com/android/server/connectivity/LingerMonitorTest.java +++ b/tests/net/java/com/android/server/connectivity/LingerMonitorTest.java @@ -38,7 +38,6 @@ import android.net.Network; import android.net.NetworkCapabilities; import android.net.NetworkInfo; import android.net.NetworkProvider; -import android.net.NetworkScore; import android.os.INetworkManagementService; import android.text.format.DateUtils; @@ -353,10 +352,8 @@ public class LingerMonitorTest { NetworkCapabilities caps = new NetworkCapabilities(); caps.addCapability(0); caps.addTransportType(transport); - 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, + caps, 50, mCtx, null, null /* config */, mConnService, mNetd, mDnsResolver, mNMS, NetworkProvider.ID_NONE); nai.everValidated = true; return nai;