Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 154bbb00 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Set AccessPoint metered bit using ScoredNetwork, WifiInfo, and...

Merge "Set AccessPoint metered bit using ScoredNetwork, WifiInfo, and WifiConfiguration." into oc-dev
parents 630671a6 21f6868d
Loading
Loading
Loading
Loading
+47 −1
Original line number Diff line number Diff line
@@ -149,6 +149,7 @@ public class AccessPoint implements Comparable<AccessPoint> {

    private int mRankingScore = Integer.MIN_VALUE;
    private int mBadge = NetworkBadging.BADGING_NONE;
    private boolean mIsScoredNetworkMetered = false;

    // used to co-relate internal vs returned accesspoint.
    int mId;
@@ -248,6 +249,7 @@ public class AccessPoint implements Comparable<AccessPoint> {
        this.mScanResultCache.putAll(that.mScanResultCache);
        this.mId = that.mId;
        this.mBadge = that.mBadge;
        this.mIsScoredNetworkMetered = that.mIsScoredNetworkMetered;
        this.mRankingScore = that.mRankingScore;
    }

@@ -336,16 +338,32 @@ public class AccessPoint implements Comparable<AccessPoint> {
        builder.append(",level=").append(getLevel());
        builder.append(",rankingScore=").append(mRankingScore);
        builder.append(",badge=").append(mBadge);
        builder.append(",metered=").append(isMetered());

        return builder.append(')').toString();
    }

    /**
     * Updates the AccessPoint rankingScore, metering, and badge, returning true if the data has
     * changed.
     *
     * @param scoreCache The score cache to use to retrieve scores.
     * @param scoringUiEnabled Whether to show scoring and badging UI.
     */
    boolean update(WifiNetworkScoreCache scoreCache, boolean scoringUiEnabled) {
        boolean scoreChanged = false;
        if (scoringUiEnabled) {
            scoreChanged = updateScores(scoreCache);
        }
        return updateMetered(scoreCache) || scoreChanged;
    }

    /**
     * Updates the AccessPoint rankingScore and badge, returning true if the data has changed.
     *
     * @param scoreCache The score cache to use to retrieve scores.
     */
    boolean updateScores(WifiNetworkScoreCache scoreCache) {
    private boolean updateScores(WifiNetworkScoreCache scoreCache) {
        int oldBadge = mBadge;
        int oldRankingScore = mRankingScore;
        mBadge = NetworkBadging.BADGING_NONE;
@@ -366,6 +384,23 @@ public class AccessPoint implements Comparable<AccessPoint> {
        return (oldBadge != mBadge || oldRankingScore != mRankingScore);
    }

    /**
     * Updates the AccessPoint's metering based on {@link ScoredNetwork#meteredHint}, returning
     * true if the metering changed.
     */
    private boolean updateMetered(WifiNetworkScoreCache scoreCache) {
        boolean oldMetering = mIsScoredNetworkMetered;
        mIsScoredNetworkMetered = false;
        for (ScanResult result : mScanResultCache.values()) {
            ScoredNetwork score = scoreCache.getScoredNetwork(result);
            if (score == null) {
                continue;
            }
            mIsScoredNetworkMetered |= score.meteredHint;
        }
        return oldMetering == mIsScoredNetworkMetered;
    }

    private void evictOldScanResults() {
        long nowMs = SystemClock.elapsedRealtime();
        for (Iterator<ScanResult> iter = mScanResultCache.values().iterator(); iter.hasNext(); ) {
@@ -474,6 +509,17 @@ public class AccessPoint implements Comparable<AccessPoint> {
        mSeen = seen;
    }

    /**
     * Returns if the network is marked metered. Metering can be marked through its config in
     * {@link WifiConfiguration}, after connection in {@link WifiInfo}, or from a score config in
     * {@link ScoredNetwork}.
     */
    public boolean isMetered() {
        return mIsScoredNetworkMetered
                || (mConfig != null && mConfig.meteredHint)
                || (mInfo != null && mInfo.getMeteredHint());
    }

    public NetworkInfo getNetworkInfo() {
        return mNetworkInfo;
    }
+1 −1
Original line number Diff line number Diff line
@@ -183,7 +183,7 @@ public class AccessPointPreference extends Preference {
        }
        if (mAccessPoint.getSecurity() != AccessPoint.SECURITY_NONE) {
            mFrictionSld.setState(STATE_SECURED);
        } else if (mAccessPoint.getConfig() != null && mAccessPoint.getConfig().meteredHint) {
        } else if (mAccessPoint.isMetered()) {
            mFrictionSld.setState(STATE_METERED);
        }
        Drawable drawable = mFrictionSld.getCurrent();
+6 −12
Original line number Diff line number Diff line
@@ -543,11 +543,9 @@ public class WifiTracker {
            }
        }

        if (mNetworkScoringUiEnabled) {
        requestScoresForNetworkKeys(scoresToRequest);
        for (AccessPoint ap : accessPoints) {
                ap.updateScores(mScoreCache);
            }
            ap.update(mScoreCache, mNetworkScoringUiEnabled);
        }

        // Pre-sort accessPoints to speed preference insertion
@@ -648,7 +646,7 @@ public class WifiTracker {
            if (ap.update(connectionConfig, mLastInfo, mLastNetworkInfo)) {
                reorder = true;
            }
            if (mNetworkScoringUiEnabled && ap.updateScores(mScoreCache)) {
            if (ap.update(mScoreCache, mNetworkScoringUiEnabled)) {
                reorder = true;
            }
        }
@@ -659,15 +657,11 @@ public class WifiTracker {
    }

    /**
     * Update all the internal access points rankingScores and badge.
     * Update all the internal access points rankingScores, badge and metering.
     *
     * <p>Will trigger a resort and notify listeners of changes if applicable.
     */
    private void updateNetworkScores() {
        if (!mNetworkScoringUiEnabled) {
            return;
        }

        // Lock required to prevent accidental copying of AccessPoint states while the modification
        // is in progress. see #copyAndNotifyListeners
        long before = System.currentTimeMillis();
@@ -679,7 +673,7 @@ public class WifiTracker {

        boolean reorder = false;
        for (int i = 0; i < mInternalAccessPoints.size(); i++) {
            if (mInternalAccessPoints.get(i).updateScores(mScoreCache)) {
            if (mInternalAccessPoints.get(i).update(mScoreCache, mNetworkScoringUiEnabled)) {
                reorder = true;
            }
        }
+60 −2
Original line number Diff line number Diff line
@@ -17,15 +17,19 @@ package com.android.settingslib.wifi;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkKey;
import android.net.ScoredNetwork;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiNetworkScoreCache;
import android.net.wifi.WifiSsid;
import android.net.wifi.hotspot2.PasspointConfiguration;
import android.net.wifi.hotspot2.pps.HomeSp;
@@ -36,10 +40,11 @@ import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import android.text.SpannableString;
import android.text.style.TtsSpan;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.ArrayList;
import java.util.Collections;
@@ -50,9 +55,11 @@ public class AccessPointTest {

    private static final String TEST_SSID = "test_ssid";
    private Context mContext;
    @Mock private WifiNetworkScoreCache mWifiNetworkScoreCache;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mContext = InstrumentationRegistry.getTargetContext();
    }

@@ -74,6 +81,7 @@ public class AccessPointTest {
    @Test
    public void testCopyAccessPoint_dataShouldMatch() {
        WifiConfiguration configuration = createWifiConfiguration();
        configuration.meteredHint = true;

        NetworkInfo networkInfo =
                new NetworkInfo(ConnectivityManager.TYPE_WIFI, 2, "WIFI", "WIFI_SUBTYPE");
@@ -88,6 +96,7 @@ public class AccessPointTest {
        assertThat(originalAccessPoint.getBssid()).isEqualTo(copy.getBssid());
        assertThat(originalAccessPoint.getConfig()).isEqualTo(copy.getConfig());
        assertThat(originalAccessPoint.getSecurity()).isEqualTo(copy.getSecurity());
        assertThat(originalAccessPoint.isMetered()).isEqualTo(copy.isMetered());
        assertThat(originalAccessPoint.compareTo(copy) == 0).isTrue();
    }

@@ -230,6 +239,55 @@ public class AccessPointTest {
        assertTrue(ap.isPasspointConfig());
    }

    @Test
    public void testIsMetered_returnTrueWhenWifiConfigurationIsMetered() {
        WifiConfiguration configuration = createWifiConfiguration();
        configuration.meteredHint = true;

        NetworkInfo networkInfo =
                new NetworkInfo(ConnectivityManager.TYPE_WIFI, 2, "WIFI", "WIFI_SUBTYPE");
        AccessPoint accessPoint = new AccessPoint(mContext, configuration);
        WifiInfo wifiInfo = new WifiInfo();
        wifiInfo.setSSID(WifiSsid.createFromAsciiEncoded(configuration.SSID));
        wifiInfo.setBSSID(configuration.BSSID);
        wifiInfo.setNetworkId(configuration.networkId);
        accessPoint.update(configuration, wifiInfo, networkInfo);

        assertTrue(accessPoint.isMetered());
    };

    @Test
    public void testIsMetered_returnTrueWhenWifiInfoIsMetered() {
        WifiConfiguration configuration = createWifiConfiguration();

        NetworkInfo networkInfo =
                new NetworkInfo(ConnectivityManager.TYPE_WIFI, 2, "WIFI", "WIFI_SUBTYPE");
        AccessPoint accessPoint = new AccessPoint(mContext, configuration);
        WifiInfo wifiInfo = new WifiInfo();
        wifiInfo.setSSID(WifiSsid.createFromAsciiEncoded(configuration.SSID));
        wifiInfo.setBSSID(configuration.BSSID);
        wifiInfo.setNetworkId(configuration.networkId);
        wifiInfo.setMeteredHint(true);
        accessPoint.update(configuration, wifiInfo, networkInfo);

        assertTrue(accessPoint.isMetered());
    };

    @Test
    public void testIsMetered_returnTrueWhenScoredNetworkIsMetered() {
        AccessPoint ap = createAccessPointWithScanResultCache();

        when(mWifiNetworkScoreCache.getScoredNetwork(any(ScanResult.class)))
                .thenReturn(
                        new ScoredNetwork(
                                null /* NetworkKey */,
                                null /* rssiCurve */,
                                true /* metered */));
        ap.update(mWifiNetworkScoreCache, false /* scoringUiEnabled */);

        assertTrue(ap.isMetered());
    };

    private AccessPoint createAccessPointWithScanResultCache() {
        Bundle bundle = new Bundle();
        ArrayList<ScanResult> scanResults = new ArrayList<>();
+19 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.settingslib.wifi;
import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import static org.mockito.Mockito.any;
@@ -302,7 +303,7 @@ public class WifiTrackerTest {
                new ScoredNetwork(
                        NETWORK_KEY_2,
                        mockCurve2,
                        false /* meteredHint */,
                        true /* meteredHint */,
                        attr2);

        WifiNetworkScoreCache scoreCache = mScoreCacheCaptor.getValue();
@@ -514,6 +515,23 @@ public class WifiTrackerTest {
        }
    }

    @Test
    public void scoreCacheUpdateMeteredShouldUpdateAccessPointMetering()
            throws InterruptedException {
        WifiTracker tracker = createTrackerWithImmediateBroadcastsAndInjectInitialScanResults();
        updateScoresAndWaitForAccessPointsChangedCallback();

        List<AccessPoint> aps = tracker.getAccessPoints();

        for (AccessPoint ap : aps) {
            if (ap.getSsidStr().equals(SSID_1)) {
                assertFalse(ap.isMetered());
            } else if (ap.getSsidStr().equals(SSID_2)) {
                assertTrue(ap.isMetered());
            }
        }
    }

    @Test
    public void noBadgesShouldBeInsertedIntoAccessPointWhenScoringUiDisabled()
            throws InterruptedException {