Merge "Use scores from connected AP only."

This commit is contained in:
TreeHugger Robot
2017-07-20 01:26:44 +00:00
committed by Android (Google) Code Review
4 changed files with 109 additions and 17 deletions

View File

@@ -28,9 +28,11 @@ import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.NetworkInfo.DetailedState;
import android.net.NetworkInfo.State;
import android.net.NetworkKey;
import android.net.NetworkScoreManager;
import android.net.NetworkScorerAppData;
import android.net.ScoredNetwork;
import android.net.WifiKey;
import android.net.wifi.IWifiManager;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
@@ -125,6 +127,7 @@ public class AccessPoint implements Comparable<AccessPoint> {
static final String KEY_SCANRESULT = "key_scanresult";
static final String KEY_SSID = "key_ssid";
static final String KEY_SECURITY = "key_security";
static final String KEY_SPEED = "key_speed";
static final String KEY_PSKTYPE = "key_psktype";
static final String KEY_SCANRESULTCACHE = "key_scanresultcache";
static final String KEY_CONFIG = "key_config";
@@ -200,10 +203,13 @@ public class AccessPoint implements Comparable<AccessPoint> {
if (savedState.containsKey(KEY_SECURITY)) {
security = savedState.getInt(KEY_SECURITY);
}
if (savedState.containsKey(KEY_SPEED)) {
mSpeed = savedState.getInt(KEY_SPEED);
}
if (savedState.containsKey(KEY_PSKTYPE)) {
pskType = savedState.getInt(KEY_PSKTYPE);
}
mInfo = (WifiInfo) savedState.getParcelable(KEY_WIFIINFO);
mInfo = savedState.getParcelable(KEY_WIFIINFO);
if (savedState.containsKey(KEY_NETWORKINFO)) {
mNetworkInfo = savedState.getParcelable(KEY_NETWORKINFO);
}
@@ -407,17 +413,34 @@ public class AccessPoint implements Comparable<AccessPoint> {
mSpeed = Speed.NONE;
mRankingScore = Integer.MIN_VALUE;
for (ScanResult result : mScanResultCache.values()) {
ScoredNetwork score = scoreCache.getScoredNetwork(result);
if (score == null) {
continue;
if (isActive() && mInfo != null) {
NetworkKey key = new NetworkKey(new WifiKey(
AccessPoint.convertToQuotedString(ssid), mInfo.getBSSID()));
ScoredNetwork score = scoreCache.getScoredNetwork(key);
if (score != null) {
mSpeed = score.calculateBadge(mInfo.getRssi());
if (score.hasRankingScore()) {
mRankingScore = score.calculateRankingScore(mInfo.getRssi());
}
}
} else {
for (ScanResult result : mScanResultCache.values()) {
ScoredNetwork score = scoreCache.getScoredNetwork(result);
if (score == null) {
continue;
}
if (score.hasRankingScore()) {
mRankingScore = Math.max(mRankingScore, score.calculateRankingScore(result.level));
if (score.hasRankingScore()) {
mRankingScore =
Math.max(mRankingScore, score.calculateRankingScore(result.level));
}
// TODO(sghuman): Rename calculateBadge API
mSpeed = Math.max(mSpeed, score.calculateBadge(result.level));
}
// TODO(sghuman): Rename calculateBadge API
mSpeed = Math.max(mSpeed, score.calculateBadge(result.level));
}
if(WifiTracker.sVerboseLogging) {
Log.i(TAG, String.format("%s: Set speed to %d", ssid, mSpeed));
}
return (oldSpeed != mSpeed || oldRankingScore != mRankingScore);
@@ -430,12 +453,22 @@ public class AccessPoint implements Comparable<AccessPoint> {
private boolean updateMetered(WifiNetworkScoreCache scoreCache) {
boolean oldMetering = mIsScoredNetworkMetered;
mIsScoredNetworkMetered = false;
for (ScanResult result : mScanResultCache.values()) {
ScoredNetwork score = scoreCache.getScoredNetwork(result);
if (score == null) {
continue;
if (isActive() && mInfo != null) {
NetworkKey key = new NetworkKey(new WifiKey(
AccessPoint.convertToQuotedString(ssid), mInfo.getBSSID()));
ScoredNetwork score = scoreCache.getScoredNetwork(key);
if (score != null) {
mIsScoredNetworkMetered |= score.meteredHint;
}
} else {
for (ScanResult result : mScanResultCache.values()) {
ScoredNetwork score = scoreCache.getScoredNetwork(result);
if (score == null) {
continue;
}
mIsScoredNetworkMetered |= score.meteredHint;
}
mIsScoredNetworkMetered |= score.meteredHint;
}
return oldMetering == mIsScoredNetworkMetered;
}
@@ -1004,6 +1037,7 @@ public class AccessPoint implements Comparable<AccessPoint> {
public void saveWifiState(Bundle savedState) {
if (ssid != null) savedState.putString(KEY_SSID, getSsidStr());
savedState.putInt(KEY_SECURITY, security);
savedState.putInt(KEY_SPEED, mSpeed);
savedState.putInt(KEY_PSKTYPE, pskType);
if (mConfig != null) savedState.putParcelable(KEY_CONFIG, mConfig);
savedState.putParcelable(KEY_WIFIINFO, mInfo);

View File

@@ -19,10 +19,13 @@ package com.android.settingslib.wifi;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.os.Bundle;
import java.util.ArrayList;
/**
* Build and return a valid AccessPoint.
*
@@ -48,6 +51,7 @@ public class TestAccessPointBuilder {
private WifiInfo mWifiInfo;
Context mContext;
private ArrayList<ScanResult> mScanResultCache;
public TestAccessPointBuilder(Context context) {
mContext = context;
@@ -70,6 +74,9 @@ public class TestAccessPointBuilder {
if (mProviderFriendlyName != null) {
bundle.putString(AccessPoint.KEY_PROVIDER_FRIENDLY_NAME, mProviderFriendlyName);
}
if (mScanResultCache != null) {
bundle.putParcelableArrayList(AccessPoint.KEY_SCANRESULTCACHE, mScanResultCache);
}
bundle.putInt(AccessPoint.KEY_SECURITY, mSecurity);
AccessPoint ap = new AccessPoint(mContext, bundle);
@@ -186,4 +193,9 @@ public class TestAccessPointBuilder {
mBssid = bssid;
return this;
}
public TestAccessPointBuilder setScanResultCache(ArrayList<ScanResult> scanResultCache) {
mScanResultCache = scanResultCache;
return this;
}
}

View File

@@ -250,6 +250,10 @@ public class WifiTracker {
mLastNetworkInfo = mConnectivityManager.getNetworkInfo(mWifiManager.getCurrentNetwork());
final List<ScanResult> newScanResults = mWifiManager.getScanResults();
if (sVerboseLogging) {
Log.i(TAG, "Fetched scan results: " + newScanResults);
}
List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
updateAccessPointsLocked(newScanResults, configs);
@@ -480,6 +484,9 @@ public class WifiTracker {
private void updateAccessPoints() {
List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
final List<ScanResult> newScanResults = mWifiManager.getScanResults();
if (sVerboseLogging) {
Log.i(TAG, "Fetched scan results: " + newScanResults);
}
synchronized (mLock) {
if(!mStaleScanResults) {

View File

@@ -21,6 +21,8 @@ import static com.google.common.truth.Truth.assertWithMessage;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -388,6 +390,39 @@ public class AccessPointTest {
.isEqualTo(mContext.getString(R.string.speed_label_slow));
}
@Test
public void testSpeedLabel_isDerivedFromConnectedBssid() {
int rssi = -55;
String bssid = "00:00:00:00:00:00";
int networkId = 123;
WifiInfo info = new WifiInfo();
info.setRssi(rssi);
info.setSSID(WifiSsid.createFromAsciiEncoded(TEST_SSID));
info.setBSSID(bssid);
info.setNetworkId(networkId);
AccessPoint ap =
new TestAccessPointBuilder(mContext)
.setActive(true)
.setNetworkId(networkId)
.setSsid(TEST_SSID)
.setScanResultCache(buildScanResultCache())
.setWifiInfo(info)
.build();
NetworkKey key = new NetworkKey(new WifiKey('"' + TEST_SSID + '"', bssid));
when(mockWifiNetworkScoreCache.getScoredNetwork(key))
.thenReturn(buildScoredNetworkWithMockBadgeCurve());
when(mockBadgeCurve.lookupScore(anyInt())).thenReturn((byte) AccessPoint.Speed.FAST);
ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */);
verify(mockWifiNetworkScoreCache, times(2)).getScoredNetwork(key);
verify(mockWifiNetworkScoreCache, never()).getScoredNetwork(any(ScanResult.class));
assertThat(ap.getSpeed()).isEqualTo(AccessPoint.Speed.FAST);
}
@Test
public void testSummaryString_showsSpeedLabel() {
AccessPoint ap = createAccessPointWithScanResultCache();
@@ -443,6 +478,12 @@ public class AccessPointTest {
private AccessPoint createAccessPointWithScanResultCache() {
Bundle bundle = new Bundle();
ArrayList<ScanResult> scanResults = buildScanResultCache();
bundle.putParcelableArrayList(AccessPoint.KEY_SCANRESULTCACHE, scanResults);
return new AccessPoint(mContext, bundle);
}
private ArrayList<ScanResult> buildScanResultCache() {
ArrayList<ScanResult> scanResults = new ArrayList<>();
for (int i = 0; i < 5; i++) {
ScanResult scanResult = new ScanResult();
@@ -452,9 +493,7 @@ public class AccessPointTest {
scanResult.capabilities = "";
scanResults.add(scanResult);
}
bundle.putParcelableArrayList(AccessPoint.KEY_SCANRESULTCACHE, scanResults);
return new AccessPoint(mContext, bundle);
return scanResults;
}
private WifiConfiguration createWifiConfiguration() {