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

Commit e869d83e authored by Sundeep Ghuman's avatar Sundeep Ghuman
Browse files

Create Settings Flags to Disable Scoring UI changes.

When disabled, this will prevent badges from being shown in the status
bar or wifi picker as well as prevent access points in the picker from
being rearranged based on ranking scores.

Fix missing permission dropped from previous CL to run
NetworkControllerWifiTest and refactored tests to enable new setting.

Bug: 34712533
Test: runtest --path
frameworks/base/packages/SettingsLib/tests/integ/src/com/android/settingslib/wifi/WifiTrackerTest.java
and runtest --path
frameworks/base/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/NetworkControllerWifiTest.java

Change-Id: I79c97f2205ebb70c0f7f5b1f66f7207055e5769b
parent ef756c3a
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -8171,6 +8171,14 @@ public final class Settings {
        @SystemApi
        public static final String WIFI_WAKEUP_ENABLED = "wifi_wakeup_enabled";

        /**
         * Value to specify whether network quality scores and badging should be shown in the UI.
         *
         * Type: int (0 for false, 1 for true)
         * @hide
         */
        public static final String NETWORK_SCORING_UI_ENABLED = "network_scoring_ui_enabled";

        /**
         * Value to specify if network recommendations from
         * {@link com.android.server.NetworkScoreService} are enabled.
+31 −5
Original line number Diff line number Diff line
@@ -16,9 +16,11 @@
package com.android.settingslib.wifi;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
@@ -38,6 +40,7 @@ import android.os.ConditionVariable;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.provider.Settings;
import android.support.annotation.WorkerThread;
import android.util.ArraySet;
import android.util.Log;
@@ -136,6 +139,8 @@ public class WifiTracker {
    private final NetworkScoreManager mNetworkScoreManager;
    private final WifiNetworkScoreCache mScoreCache;
    private final Set<NetworkKey> mRequestedScores = new ArraySet<>();
    private boolean mNetworkScoringUiEnabled;
    private final ContentObserver mObserver;

    @VisibleForTesting
    Scanner mScanner;
@@ -215,6 +220,16 @@ public class WifiTracker {
                Message.obtain(mWorkHandler, WorkHandler.MSG_UPDATE_NETWORK_SCORES).sendToTarget();
            }
        });

        mObserver = new ContentObserver(mWorkHandler) {
            @Override
            public void onChange(boolean selfChange) {
                mNetworkScoringUiEnabled =
                        Settings.Global.getInt(
                                mContext.getContentResolver(),
                                Settings.Global.NETWORK_SCORING_UI_ENABLED, 0) == 1;
            }
        };
    }

    /**
@@ -274,6 +289,11 @@ public class WifiTracker {
            }
        });

        mContext.getContentResolver().registerContentObserver(
                Settings.Global.getUriFor(Settings.Global.NETWORK_SCORING_UI_ENABLED),
                false /* notifyForDescendants */,
                mObserver);
        mObserver.onChange(false /* selfChange */); // Set the initial value for mScoringUiEnabled

        resumeScanning();
        if (!mRegistered) {
@@ -327,6 +347,7 @@ public class WifiTracker {
                unregisterAndClearScoreCache();
            }
        });
        mContext.getContentResolver().unregisterContentObserver(mObserver);
    }

    @WorkerThread
@@ -537,11 +558,12 @@ public class WifiTracker {
            }
        }


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

        // Pre-sort accessPoints to speed preference insertion
        Collections.sort(accessPoints);
@@ -641,7 +663,7 @@ public class WifiTracker {
            if (ap.update(connectionConfig, mLastInfo, mLastNetworkInfo)) {
                reorder = true;
            }
            if (ap.updateScores(mScoreCache)) {
            if (mNetworkScoringUiEnabled && ap.updateScores(mScoreCache)) {
                reorder = true;
            }
        }
@@ -657,6 +679,10 @@ public class WifiTracker {
     * <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();
+65 −9
Original line number Diff line number Diff line
@@ -110,7 +110,7 @@ public class WifiTrackerTest {
    private HandlerThread mWorkerThread;
    private Looper mLooper;
    private Looper mMainLooper;
    private int mOriginalSettingValue;
    private int mOriginalScoringUiSettingValue;

    @Before
    public void setUp() {
@@ -175,19 +175,23 @@ public class WifiTrackerTest {
                  }
                }).when(mockWifiListener).onAccessPointsChanged();

        mOriginalSettingValue = Settings.Global.getInt(
        // Turn on Scoring UI features
        mOriginalScoringUiSettingValue = Settings.Global.getInt(
                InstrumentationRegistry.getTargetContext().getContentResolver(),
            Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED,
                Settings.Global.NETWORK_SCORING_UI_ENABLED,
                0 /* disabled */);

        Settings.Global.putInt(
                InstrumentationRegistry.getTargetContext().getContentResolver(),
                Settings.Global.NETWORK_SCORING_UI_ENABLED,
                1 /* enabled */);
    }

    @After
    public void cleanUp() {
        Settings.Global.putInt(
                InstrumentationRegistry.getTargetContext().getContentResolver(),
            Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED,
            mOriginalSettingValue);
                Settings.Global.NETWORK_SCORING_UI_ENABLED,
                mOriginalScoringUiSettingValue);
    }

    private static ScanResult buildScanResult1() {
@@ -333,9 +337,18 @@ public class WifiTrackerTest {

        WifiNetworkScoreCache scoreCache = mScoreCacheCaptor.getValue();

        CountDownLatch latch = new CountDownLatch(1);
        doAnswer(
                (invocation) -> {
                        latch.countDown();
                        return null;
                }).when(mockNetworkScoreManager)
                        .unregisterNetworkScoreCache(NetworkKey.TYPE_WIFI, scoreCache);

        // Test unregister
        tracker.stopTracking();

        latch.await(LATCH_TIMEOUT, TimeUnit.MILLISECONDS);
        verify(mockNetworkScoreManager)
                .unregisterNetworkScoreCache(NetworkKey.TYPE_WIFI, scoreCache);
    }
@@ -385,7 +398,28 @@ public class WifiTrackerTest {
        assertTrue(aps.size() == 2);
        assertEquals(aps.get(0).getSsidStr(), SSID_2);
        assertEquals(aps.get(1).getSsidStr(), SSID_1);
    }

    @Test
    public void scoreCacheUpdateScoresShouldNotChangeSortOrderWhenSortingDisabled()
            throws InterruptedException {
        Settings.Global.putInt(
                InstrumentationRegistry.getTargetContext().getContentResolver(),
                Settings.Global.NETWORK_SCORING_UI_ENABLED,
                0 /* disabled */);

        WifiTracker tracker = createTrackerAndInjectInitialScanResults();
        List<AccessPoint> aps = tracker.getAccessPoints();
        assertTrue(aps.size() == 2);
        assertEquals(aps.get(0).getSsidStr(), SSID_1);
        assertEquals(aps.get(1).getSsidStr(), SSID_2);

        updateScoresAndWaitForAccessPointsChangedCallback();

        aps = tracker.getAccessPoints();
        assertTrue(aps.size() == 2);
        assertEquals(aps.get(0).getSsidStr(), SSID_1);
        assertEquals(aps.get(1).getSsidStr(), SSID_2);
    }

    @Test
@@ -404,6 +438,28 @@ public class WifiTrackerTest {
        }
    }

    @Test
    public void noBadgesShouldBeInsertedIntoAccessPointWhenScoringUiDisabled()
            throws InterruptedException {
        Settings.Global.putInt(
                InstrumentationRegistry.getTargetContext().getContentResolver(),
                Settings.Global.NETWORK_SCORING_UI_ENABLED,
                0 /* disabled */);

        WifiTracker tracker = createTrackerAndInjectInitialScanResults();
        updateScoresAndWaitForAccessPointsChangedCallback();

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

        for (AccessPoint ap : aps) {
            if (ap.getSsidStr().equals(SSID_1)) {
                assertEquals(ScoredNetwork.BADGING_NONE, ap.getBadge());
            } else if (ap.getSsidStr().equals(SSID_2)) {
                assertEquals(ScoredNetwork.BADGING_NONE, ap.getBadge());
            }
        }
    }

    @Test
    public void scoresShouldBeRequestedForNewScanResultOnly()  throws InterruptedException {
        mRequestScoresLatch = new CountDownLatch(2);
+13 −12
Original line number Diff line number Diff line
@@ -56,7 +56,7 @@ public class WifiSignalController extends
    private final WifiNetworkScoreCache mScoreCache;
    private final WifiStatusTracker mWifiTracker;

    private boolean mScoringEnabled = false;
    private boolean mScoringUiEnabled = false;

    public WifiSignalController(Context context, boolean hasMobileData,
            CallbackHandler callbackHandler, NetworkControllerImpl networkController,
@@ -95,25 +95,26 @@ public class WifiSignalController extends

        // Setup scoring
        mNetworkScoreManager = networkScoreManager;
        configureScoringGating();
        registerScoreCache();
    }

    private void configureScoringGating() {
        ContentObserver observer = new ContentObserver(new Handler(Looper.getMainLooper())) {
            @Override
            public void onChange(boolean selfChange) {
                mScoringEnabled =
                mScoringUiEnabled =
                        Settings.Global.getInt(
                                mContext.getContentResolver(),
                                Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED, 0) == 1;
                if (!mScoringEnabled) {
                    mScoreCache.clearScores();
                }
                                Settings.Global.NETWORK_SCORING_UI_ENABLED, 0) == 1;
            }
        };
        ContentResolver cr = mContext.getContentResolver();
        cr.registerContentObserver(
                Settings.Global.getUriFor(Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED),
        mContext.getContentResolver().registerContentObserver(
                Settings.Global.getUriFor(Settings.Global.NETWORK_SCORING_UI_ENABLED),
                false /* notifyForDescendants */,
                observer);
        observer.onChange(false /* selfChange */);
        registerScoreCache();

        observer.onChange(false /* selfChange */); // Set the initial values
    }

    private void registerScoreCache() {
@@ -199,7 +200,7 @@ public class WifiSignalController extends
     * <p>{@link #updateScoreCacheIfNecessary} should be called prior to this method.
     */
    private int getWifiBadgeEnum() {
        if (!mScoringEnabled || mWifiTracker.networkKey == null) {
        if (!mScoringUiEnabled || mWifiTracker.networkKey == null) {
            return ScoredNetwork.BADGING_NONE;
        }
        ScoredNetwork score = mScoreCache.getScoredNetwork(mWifiTracker.networkKey);
+1 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CONNECTIVITY_INTERNAL" />
    <uses-permission android:name="android.permission.MANAGE_NETWORK_POLICY" />
    <uses-permission android:name="android.permission.REQUEST_NETWORK_SCORES" />

    <application>
        <uses-library android:name="android.test.runner" />
Loading