Change score cache eviction time to settings value.

Reduce default eviction time from intended 24 hours (erroneously input
as 24 days) to 20 minutes.

Bug: b/63073866
Test: runtest --path
frameworks/base/packages/SettingsLib/tests/integ/src/com/android/settingslib/wifi/AccessPointTest.java

Change-Id: I89eae2483b9a65a65d7cf5b1151952609b6b7fd7
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
index e22c566..d09d027 100755
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -8556,6 +8556,16 @@
         public static final String NETWORK_SCORING_UI_ENABLED = "network_scoring_ui_enabled";
 
         /**
+         * Value to specify how long in milliseconds to retain seen score cache curves to be used
+         * when generating SSID only bases score curves.
+         *
+         * Type: long
+         * @hide
+         */
+        public static final String SPEED_LABEL_CACHE_EVICTION_AGE_MILLIS =
+                "speed_label_cache_eviction_age_millis";
+
+        /**
          * Value to specify if network recommendations from
          * {@link com.android.server.NetworkScoreService} are enabled.
          *
diff --git a/core/tests/coretests/src/android/provider/SettingsBackupTest.java b/core/tests/coretests/src/android/provider/SettingsBackupTest.java
index 8a57ea9..ce390e8 100644
--- a/core/tests/coretests/src/android/provider/SettingsBackupTest.java
+++ b/core/tests/coretests/src/android/provider/SettingsBackupTest.java
@@ -327,6 +327,7 @@
                     Settings.Global.SMS_SHORT_CODE_RULE,
                     Settings.Global.SMS_SHORT_CODES_UPDATE_CONTENT_URL,
                     Settings.Global.SMS_SHORT_CODES_UPDATE_METADATA_URL,
+                    Settings.Global.SPEED_LABEL_CACHE_EVICTION_AGE_MILLIS,
                     Settings.Global.STORAGE_BENCHMARK_INTERVAL,
                     Settings.Global.SYNC_MAX_RETRY_DELAY_IN_SECONDS,
                     Settings.Global.SYS_FREE_STORAGE_LOG_INTERVAL,
diff --git a/packages/SettingsLib/src/com/android/settingslib/wifi/AccessPoint.java b/packages/SettingsLib/src/com/android/settingslib/wifi/AccessPoint.java
index dd629c4..ae0ee2b 100644
--- a/packages/SettingsLib/src/com/android/settingslib/wifi/AccessPoint.java
+++ b/packages/SettingsLib/src/com/android/settingslib/wifi/AccessPoint.java
@@ -51,7 +51,6 @@
 import android.text.Spannable;
 import android.text.SpannableString;
 import android.text.TextUtils;
-import android.text.format.DateUtils;
 import android.text.style.TtsSpan;
 import android.util.Log;
 
@@ -134,10 +133,6 @@
      */
     private final Map<String, TimestampedScoredNetwork> mScoredNetworkCache = new HashMap<>();
 
-    /** Maximum age in millis of cached scored networks in {@link #mScoredNetworkCache}. */
-    @VisibleForTesting static final long MAX_CACHED_SCORE_AGE_MILLIS =
-            24 * DateUtils.DAY_IN_MILLIS;
-
     /** Maximum age of scan results to hold onto while actively scanning. **/
     private static final long MAX_SCAN_RESULT_AGE_MILLIS = 15000;
 
@@ -436,13 +431,18 @@
      * Updates the AccessPoint rankingScore, metering, and speed, 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.
+     * @param scoreCache The score cache to use to retrieve scores
+     * @param scoringUiEnabled Whether to show scoring and badging UI
+     * @param maxScoreCacheAgeMillis the maximum age in milliseconds of scores to consider when
+     *         generating speed labels
      */
-    boolean update(WifiNetworkScoreCache scoreCache, boolean scoringUiEnabled) {
+    boolean update(
+            WifiNetworkScoreCache scoreCache,
+            boolean scoringUiEnabled,
+            long maxScoreCacheAgeMillis) {
         boolean scoreChanged = false;
         if (scoringUiEnabled) {
-            scoreChanged = updateScores(scoreCache);
+            scoreChanged = updateScores(scoreCache, maxScoreCacheAgeMillis);
         }
         return updateMetered(scoreCache) || scoreChanged;
     }
@@ -450,15 +450,18 @@
     /**
      * Updates the AccessPoint rankingScore and speed, returning true if the data has changed.
      *
-     * <p>Any cached {@link TimestampedScoredNetwork} objects older than
-     * {@link #MAX_CACHED_SCORE_AGE_MILLIS} will be removed when this method is invoked.
+     * <p>Any cached {@link TimestampedScoredNetwork} objects older than the given max age in millis
+     * will be removed when this method is invoked.
      *
      * <p>Precondition: {@link #mRssi} is up to date before invoking this method.
      *
-     * @param scoreCache The score cache to use to retrieve scores.
+     * @param scoreCache The score cache to use to retrieve scores
+     * @param maxScoreCacheAgeMillis the maximum age in milliseconds of scores to consider when
+     *         generating speed labels
+     *
      * @return true if the set speed has changed
      */
-    private boolean updateScores(WifiNetworkScoreCache scoreCache) {
+    private boolean updateScores(WifiNetworkScoreCache scoreCache, long maxScoreCacheAgeMillis) {
         long nowMillis = SystemClock.elapsedRealtime();
         for (ScanResult result : mScanResultCache.values()) {
             ScoredNetwork score = scoreCache.getScoredNetwork(result);
@@ -476,7 +479,7 @@
         }
 
         // Remove old cached networks
-        long evictionCutoff = nowMillis - MAX_CACHED_SCORE_AGE_MILLIS;
+        long evictionCutoff = nowMillis - maxScoreCacheAgeMillis;
         Iterator<TimestampedScoredNetwork> iterator = mScoredNetworkCache.values().iterator();
         iterator.forEachRemaining(timestampedScoredNetwork -> {
             if (timestampedScoredNetwork.getUpdatedTimestampMillis() < evictionCutoff) {
diff --git a/packages/SettingsLib/src/com/android/settingslib/wifi/WifiTracker.java b/packages/SettingsLib/src/com/android/settingslib/wifi/WifiTracker.java
index 8ef6a0d..27cab07 100644
--- a/packages/SettingsLib/src/com/android/settingslib/wifi/WifiTracker.java
+++ b/packages/SettingsLib/src/com/android/settingslib/wifi/WifiTracker.java
@@ -41,6 +41,7 @@
 import android.os.Message;
 import android.provider.Settings;
 import android.support.annotation.GuardedBy;
+import android.text.format.DateUtils;
 import android.util.ArraySet;
 import android.util.Log;
 import android.util.SparseArray;
@@ -65,7 +66,12 @@
  * Tracks saved or available wifi networks and their state.
  */
 public class WifiTracker {
-    // TODO(b/36733768): Remove flag includeSaved and includePasspoints.
+    /**
+     * Default maximum age in millis of cached scored networks in
+     * {@link AccessPoint#mScoredNetworkCache} to be used for speed label generation.
+     */
+    private static final long MAX_CACHED_SCORE_AGE_MILLIS_DEFAULT =
+            20 * DateUtils.MINUTE_IN_MILLIS;
 
     private static final String TAG = "WifiTracker";
     private static final boolean DBG() {
@@ -76,6 +82,8 @@
      * and used so as to assist with in-the-field WiFi connectivity debugging  */
     public static boolean sVerboseLogging;
 
+    // TODO(b/36733768): Remove flag includeSaved and includePasspoints.
+
     // TODO: Allow control of this?
     // Combo scans can take 5-6s to complete - set to 10s.
     private static final int WIFI_RESCAN_INTERVAL_MS = 10 * 1000;
@@ -138,7 +146,9 @@
     private final NetworkScoreManager mNetworkScoreManager;
     private final WifiNetworkScoreCache mScoreCache;
     private boolean mNetworkScoringUiEnabled;
-    private final ContentObserver mObserver;
+    private final ContentObserver mScoringEnabledObserver;
+    private final ContentObserver mSpeedLabelCacheAgeObserver;
+    private long mMaxSpeedLabelScoreCacheAge;
 
     @GuardedBy("mLock")
     private final Set<NetworkKey> mRequestedScores = new ArraySet<>();
@@ -237,7 +247,7 @@
             }
         });
 
-        mObserver = new ContentObserver(mWorkHandler) {
+        mScoringEnabledObserver = new ContentObserver(mWorkHandler) {
             @Override
             public void onChange(boolean selfChange) {
                 mNetworkScoringUiEnabled =
@@ -246,6 +256,17 @@
                                 Settings.Global.NETWORK_SCORING_UI_ENABLED, 0) == 1;
             }
         };
+
+        mSpeedLabelCacheAgeObserver = new ContentObserver(mWorkHandler) {
+            @Override
+            public void onChange(boolean selfChange) {
+                mMaxSpeedLabelScoreCacheAge =
+                        Settings.Global.getLong(
+                                mContext.getContentResolver(),
+                                Settings.Global.SPEED_LABEL_CACHE_EVICTION_AGE_MILLIS,
+                                MAX_CACHED_SCORE_AGE_MILLIS_DEFAULT);
+            }
+        };
     }
 
     /** Synchronously update the list of access points with the latest information. */
@@ -324,8 +345,15 @@
             mContext.getContentResolver().registerContentObserver(
                     Settings.Global.getUriFor(Settings.Global.NETWORK_SCORING_UI_ENABLED),
                     false /* notifyForDescendants */,
-                    mObserver);
-            mObserver.onChange(false /* selfChange */); // Set mScoringUiEnabled
+                    mScoringEnabledObserver);
+            mScoringEnabledObserver.onChange(false /* selfChange */); // Set mScoringUiEnabled
+
+            mContext.getContentResolver().registerContentObserver(
+                    Settings.Global.getUriFor(
+                            Settings.Global.SPEED_LABEL_CACHE_EVICTION_AGE_MILLIS),
+                    false /* notifyForDescendants */,
+                    mSpeedLabelCacheAgeObserver);
+            mSpeedLabelCacheAgeObserver.onChange(false /* selfChange */); // Set initial value
 
             resumeScanning();
             if (!mRegistered) {
@@ -377,7 +405,8 @@
             }
             unregisterScoreCache();
             pauseScanning();
-            mContext.getContentResolver().unregisterContentObserver(mObserver);
+            mContext.getContentResolver().unregisterContentObserver(mScoringEnabledObserver);
+            mContext.getContentResolver().unregisterContentObserver(mSpeedLabelCacheAgeObserver);
 
             mWorkHandler.removePendingMessages();
             mMainHandler.removePendingMessages();
@@ -616,7 +645,7 @@
 
         requestScoresForNetworkKeys(scoresToRequest);
         for (AccessPoint ap : accessPoints) {
-            ap.update(mScoreCache, mNetworkScoringUiEnabled);
+            ap.update(mScoreCache, mNetworkScoringUiEnabled, mMaxSpeedLabelScoreCacheAge);
         }
 
         // Pre-sort accessPoints to speed preference insertion
@@ -717,7 +746,7 @@
                     updated = true;
                     if (previouslyConnected != ap.isActive()) reorder = true;
                 }
-                if (ap.update(mScoreCache, mNetworkScoringUiEnabled)) {
+                if (ap.update(mScoreCache, mNetworkScoringUiEnabled, mMaxSpeedLabelScoreCacheAge)) {
                     reorder = true;
                     updated = true;
                 }
@@ -751,7 +780,8 @@
         synchronized (mLock) {
             boolean updated = false;
             for (int i = 0; i < mInternalAccessPoints.size(); i++) {
-                if (mInternalAccessPoints.get(i).update(mScoreCache, mNetworkScoringUiEnabled)) {
+                if (mInternalAccessPoints.get(i).update(
+                        mScoreCache, mNetworkScoringUiEnabled, mMaxSpeedLabelScoreCacheAge)) {
                     updated = true;
                 }
             }
diff --git a/packages/SettingsLib/tests/integ/src/com/android/settingslib/wifi/AccessPointTest.java b/packages/SettingsLib/tests/integ/src/com/android/settingslib/wifi/AccessPointTest.java
index b678b3a..37a6970 100644
--- a/packages/SettingsLib/tests/integ/src/com/android/settingslib/wifi/AccessPointTest.java
+++ b/packages/SettingsLib/tests/integ/src/com/android/settingslib/wifi/AccessPointTest.java
@@ -45,6 +45,7 @@
 import android.support.test.filters.SmallTest;
 import android.support.test.runner.AndroidJUnit4;
 import android.text.SpannableString;
+import android.text.format.DateUtils;
 import android.text.style.TtsSpan;
 
 import com.android.settingslib.R;
@@ -72,6 +73,8 @@
     private static final RssiCurve FAST_BADGE_CURVE =
             new RssiCurve(-150, 10, new byte[]{Speed.FAST});
     public static final String TEST_BSSID = "00:00:00:00:00:00";
+    private static final long MAX_SCORE_CACHE_AGE_MILLIS =
+            20 * DateUtils.MINUTE_IN_MILLIS;;
 
     private Context mContext;
     @Mock private RssiCurve mockBadgeCurve;
@@ -335,7 +338,8 @@
                                 null /* NetworkKey */,
                                 null /* rssiCurve */,
                                 true /* metered */));
-        ap.update(mockWifiNetworkScoreCache, false /* scoringUiEnabled */);
+        ap.update(mockWifiNetworkScoreCache, false /* scoringUiEnabled */,
+                MAX_SCORE_CACHE_AGE_MILLIS);
 
         assertThat(ap.isMetered()).isTrue();
     }
@@ -364,7 +368,8 @@
                 .thenReturn(buildScoredNetworkWithMockBadgeCurve());
         when(mockBadgeCurve.lookupScore(anyInt())).thenReturn((byte) AccessPoint.Speed.VERY_FAST);
 
-        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */);
+        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */,
+                MAX_SCORE_CACHE_AGE_MILLIS);
 
         assertThat(ap.getSpeed()).isEqualTo(AccessPoint.Speed.VERY_FAST);
         assertThat(ap.getSpeedLabel())
@@ -379,7 +384,8 @@
                 .thenReturn(buildScoredNetworkWithMockBadgeCurve());
         when(mockBadgeCurve.lookupScore(anyInt())).thenReturn((byte) AccessPoint.Speed.FAST);
 
-        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */);
+        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */,
+                MAX_SCORE_CACHE_AGE_MILLIS);
 
         assertThat(ap.getSpeed()).isEqualTo(AccessPoint.Speed.FAST);
         assertThat(ap.getSpeedLabel())
@@ -394,7 +400,8 @@
                 .thenReturn(buildScoredNetworkWithMockBadgeCurve());
         when(mockBadgeCurve.lookupScore(anyInt())).thenReturn((byte) AccessPoint.Speed.MODERATE);
 
-        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */);
+        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */,
+                MAX_SCORE_CACHE_AGE_MILLIS);
 
         assertThat(ap.getSpeed()).isEqualTo(AccessPoint.Speed.MODERATE);
         assertThat(ap.getSpeedLabel())
@@ -409,7 +416,8 @@
                 .thenReturn(buildScoredNetworkWithMockBadgeCurve());
         when(mockBadgeCurve.lookupScore(anyInt())).thenReturn((byte) AccessPoint.Speed.SLOW);
 
-        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */);
+        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */,
+                MAX_SCORE_CACHE_AGE_MILLIS);
 
         assertThat(ap.getSpeed()).isEqualTo(AccessPoint.Speed.SLOW);
         assertThat(ap.getSpeedLabel())
@@ -424,7 +432,8 @@
                 .thenReturn(buildScoredNetworkWithMockBadgeCurve());
         when(mockBadgeCurve.lookupScore(anyInt())).thenReturn((byte) AccessPoint.Speed.VERY_FAST);
 
-        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */);
+        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */,
+                MAX_SCORE_CACHE_AGE_MILLIS);
 
         assertThat(ap.getSummary()).isEqualTo(mContext.getString(R.string.speed_label_very_fast));
     }
@@ -442,7 +451,8 @@
                 .thenReturn(buildScoredNetworkWithMockBadgeCurve());
         when(mockBadgeCurve.lookupScore(anyInt())).thenReturn((byte) AccessPoint.Speed.VERY_FAST);
 
-        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */);
+        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */,
+                MAX_SCORE_CACHE_AGE_MILLIS);
         String summary = ap.verboseScanResultSummary(scanResults.get(0), null);
 
         assertThat(summary.contains(mContext.getString(R.string.speed_label_very_fast))).isTrue();
@@ -457,7 +467,8 @@
                 .thenReturn(buildScoredNetworkWithMockBadgeCurve());
         when(mockBadgeCurve.lookupScore(anyInt())).thenReturn((byte) AccessPoint.Speed.VERY_FAST);
 
-        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */);
+        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */,
+                MAX_SCORE_CACHE_AGE_MILLIS);
 
         String expectedString = mContext.getString(R.string.speed_label_very_fast) + " / "
                 + mContext.getString(R.string.wifi_remembered);
@@ -864,7 +875,8 @@
 
         int expectedSpeed = (speed1 + speed2) / 2;
 
-        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */);
+        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */,
+                MAX_SCORE_CACHE_AGE_MILLIS);
 
         assertThat(ap.getSpeed()).isEqualTo(expectedSpeed);
     }
@@ -884,7 +896,8 @@
         when(mockWifiNetworkScoreCache.getScoredNetwork(SCAN_RESULTS.get(1)))
                 .thenReturn(buildScoredNetworkWithGivenBadgeCurve(badgeCurve2));
 
-        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */);
+        ap.update(
+        mockWifiNetworkScoreCache, true /* scoringUiEnabled */, MAX_SCORE_CACHE_AGE_MILLIS);
 
         assertThat(ap.getSpeed()).isEqualTo(speed1);
     }
@@ -925,7 +938,8 @@
         when(mockWifiNetworkScoreCache.getScoredNetwork(scanResultConnected))
                 .thenReturn(null);
 
-        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */);
+        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */,
+                MAX_SCORE_CACHE_AGE_MILLIS);
 
         assertThat(ap.getSpeed()).isEqualTo(fallbackSpeed);
     }
@@ -952,7 +966,8 @@
         AccessPoint ap =
                 createApWithFastTimestampedScoredNetworkCache(SystemClock.elapsedRealtime());
 
-        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */);
+        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */,
+                MAX_SCORE_CACHE_AGE_MILLIS);
 
         assertThat(ap.getSpeed()).isEqualTo(Speed.FAST);
     }
@@ -960,11 +975,12 @@
     @Test
     public void testNetworkScoresAreUsedForSpeedLabelGenerationWhenWithinAgeRange() {
         long withinRangeTimeMillis =
-                SystemClock.elapsedRealtime() - (AccessPoint.MAX_CACHED_SCORE_AGE_MILLIS - 10000);
+                SystemClock.elapsedRealtime() - (MAX_SCORE_CACHE_AGE_MILLIS - 10000);
         AccessPoint ap =
                 createApWithFastTimestampedScoredNetworkCache(withinRangeTimeMillis);
 
-        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */);
+        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */,
+                MAX_SCORE_CACHE_AGE_MILLIS);
 
         assertThat(ap.getSpeed()).isEqualTo(Speed.FAST);
     }
@@ -972,11 +988,12 @@
     @Test
     public void testOldNetworkScoresAreNotUsedForSpeedLabelGeneration() {
         long tooOldTimeMillis =
-                SystemClock.elapsedRealtime() - (AccessPoint.MAX_CACHED_SCORE_AGE_MILLIS + 1);
+                SystemClock.elapsedRealtime() - (MAX_SCORE_CACHE_AGE_MILLIS + 1);
         AccessPoint ap =
                 createApWithFastTimestampedScoredNetworkCache(tooOldTimeMillis);
 
-        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */);
+        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */,
+                MAX_SCORE_CACHE_AGE_MILLIS);
 
         assertThat(ap.getSpeed()).isEqualTo(Speed.NONE);
     }
@@ -984,7 +1001,7 @@
     @Test
     public void testUpdateScoresRefreshesScoredNetworkCacheTimestamps () {
         long tooOldTimeMillis =
-                SystemClock.elapsedRealtime() - (AccessPoint.MAX_CACHED_SCORE_AGE_MILLIS + 1);
+                SystemClock.elapsedRealtime() - (MAX_SCORE_CACHE_AGE_MILLIS + 1);
 
         ScoredNetwork scoredNetwork = buildScoredNetworkWithGivenBadgeCurve(FAST_BADGE_CURVE);
         TimestampedScoredNetwork recentScore = new TimestampedScoredNetwork(
@@ -1002,7 +1019,8 @@
         when(mockWifiNetworkScoreCache.getScoredNetwork(any(ScanResult.class)))
                 .thenReturn(scoredNetwork);
 
-        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */);
+        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */,
+                MAX_SCORE_CACHE_AGE_MILLIS);
 
         // Fast should still be returned since cache was updated with recent time
         assertThat(ap.getSpeed()).isEqualTo(Speed.FAST);
@@ -1011,7 +1029,7 @@
     @Test
     public void testUpdateScoresRefreshesScoredNetworkCacheWithNewSpeed () {
         long tooOldTimeMillis =
-                SystemClock.elapsedRealtime() - (AccessPoint.MAX_CACHED_SCORE_AGE_MILLIS + 1);
+                SystemClock.elapsedRealtime() - (MAX_SCORE_CACHE_AGE_MILLIS + 1);
 
         ScoredNetwork scoredNetwork = buildScoredNetworkWithGivenBadgeCurve(FAST_BADGE_CURVE);
         TimestampedScoredNetwork recentScore = new TimestampedScoredNetwork(
@@ -1031,7 +1049,8 @@
                 .thenReturn(buildScoredNetworkWithMockBadgeCurve());
         when(mockBadgeCurve.lookupScore(anyInt())).thenReturn((byte) newSpeed);
 
-        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */);
+        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */,
+                MAX_SCORE_CACHE_AGE_MILLIS);
 
         // Fast should still be returned since cache was updated with recent time
         assertThat(ap.getSpeed()).isEqualTo(newSpeed);