[NS D03] Migrate the bad wifi avoidance policy
Test: ConnectivityServiceTest Bug: 113554781 Change-Id: I688593cc0379a0d2042e30fbe83e549dcb02723e
This commit is contained in:
@@ -16,8 +16,13 @@
|
|||||||
|
|
||||||
package com.android.server.connectivity;
|
package com.android.server.connectivity;
|
||||||
|
|
||||||
|
import static android.net.NetworkScore.POLICY_IGNORE_ON_WIFI;
|
||||||
|
|
||||||
|
import static com.android.internal.util.FunctionalUtils.findFirst;
|
||||||
|
|
||||||
import android.annotation.NonNull;
|
import android.annotation.NonNull;
|
||||||
import android.annotation.Nullable;
|
import android.annotation.Nullable;
|
||||||
|
import android.net.NetworkCapabilities;
|
||||||
import android.net.NetworkRequest;
|
import android.net.NetworkRequest;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -37,15 +42,33 @@ public class NetworkRanker {
|
|||||||
@NonNull final Collection<NetworkAgentInfo> nais) {
|
@NonNull final Collection<NetworkAgentInfo> nais) {
|
||||||
final ArrayList<NetworkAgentInfo> candidates = new ArrayList<>(nais);
|
final ArrayList<NetworkAgentInfo> candidates = new ArrayList<>(nais);
|
||||||
candidates.removeIf(nai -> !nai.satisfies(request));
|
candidates.removeIf(nai -> !nai.satisfies(request));
|
||||||
|
// Enforce policy.
|
||||||
|
filterBadWifiAvoidancePolicy(candidates);
|
||||||
|
|
||||||
NetworkAgentInfo bestNetwork = null;
|
NetworkAgentInfo bestNetwork = null;
|
||||||
int bestScore = Integer.MIN_VALUE;
|
int bestScore = Integer.MIN_VALUE;
|
||||||
for (final NetworkAgentInfo nai : candidates) {
|
for (final NetworkAgentInfo nai : candidates) {
|
||||||
if (nai.getCurrentScore() > bestScore) {
|
final int score = nai.getCurrentScore();
|
||||||
|
if (score > bestScore) {
|
||||||
bestNetwork = nai;
|
bestNetwork = nai;
|
||||||
bestScore = nai.getCurrentScore();
|
bestScore = score;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return bestNetwork;
|
return bestNetwork;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If some network with wifi transport is present, drop all networks with POLICY_IGNORE_ON_WIFI.
|
||||||
|
private void filterBadWifiAvoidancePolicy(
|
||||||
|
@NonNull final ArrayList<NetworkAgentInfo> candidates) {
|
||||||
|
final NetworkAgentInfo wifi = findFirst(candidates,
|
||||||
|
nai -> nai.networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
|
||||||
|
&& nai.everValidated
|
||||||
|
// Horrible hack : there is old UI that will let a user say they want to
|
||||||
|
// override the policy only for this network only at this time and it
|
||||||
|
// feeds into the following member. This old UI should probably be removed
|
||||||
|
// but for now keep backward compatibility.
|
||||||
|
&& !nai.avoidUnvalidated);
|
||||||
|
if (null == wifi) return; // No wifi : this policy doesn't apply
|
||||||
|
candidates.removeIf(nai -> nai.getNetworkScore().hasPolicy(POLICY_IGNORE_ON_WIFI));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package com.android.server.connectivity
|
package com.android.server.connectivity
|
||||||
|
|
||||||
|
import android.net.NetworkCapabilities
|
||||||
import android.net.NetworkRequest
|
import android.net.NetworkRequest
|
||||||
import androidx.test.filters.SmallTest
|
import androidx.test.filters.SmallTest
|
||||||
import androidx.test.runner.AndroidJUnit4
|
import androidx.test.runner.AndroidJUnit4
|
||||||
@@ -35,6 +36,7 @@ class NetworkRankerTest {
|
|||||||
private fun makeNai(satisfy: Boolean, score: Int) = mock(NetworkAgentInfo::class.java).also {
|
private fun makeNai(satisfy: Boolean, score: Int) = mock(NetworkAgentInfo::class.java).also {
|
||||||
doReturn(satisfy).`when`(it).satisfies(any())
|
doReturn(satisfy).`when`(it).satisfies(any())
|
||||||
doReturn(score).`when`(it).currentScore
|
doReturn(score).`when`(it).currentScore
|
||||||
|
it.networkCapabilities = NetworkCapabilities()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user