Merge changes from topic "dynamic_vdbg"

* changes:
  [AWARE] Restructure debugging flags to enable dynamic updates
  [RTT] Convert to dynamic logging configuration
  [HDM] Dynamically enable verbose logging in HDM
  [WIFI] Move verbose logging init to Injector
diff --git a/service/java/com/android/server/wifi/WifiConnectivityManager.java b/service/java/com/android/server/wifi/WifiConnectivityManager.java
index 458f73a..c67e7c6 100644
--- a/service/java/com/android/server/wifi/WifiConnectivityManager.java
+++ b/service/java/com/android/server/wifi/WifiConnectivityManager.java
@@ -814,24 +814,38 @@
             }
         }
 
+        boolean isScanNeeded = true;
         boolean isFullBandScan = true;
+        boolean isTrafficOverThreshold = mWifiInfo.txSuccessRate > mFullScanMaxTxRate
+                || mWifiInfo.rxSuccessRate > mFullScanMaxRxRate;
 
-        // If the WiFi traffic is heavy, only partial scan is initiated.
-        if (mWifiState == WIFI_STATE_CONNECTED
-                && (mWifiInfo.txSuccessRate > mFullScanMaxTxRate
-                    || mWifiInfo.rxSuccessRate > mFullScanMaxRxRate)) {
-            localLog("No full band scan due to ongoing traffic");
-            isFullBandScan = false;
+        // If the WiFi traffic is heavy, only partial scan is proposed.
+        if (mWifiState == WIFI_STATE_CONNECTED && isTrafficOverThreshold) {
+            // If only partial scan is proposed and firmware roaming control is supported,
+            // we will not issue any scan because firmware roaming will take care of
+            // intra-SSID roam.
+            if (mConnectivityHelper.isFirmwareRoamingSupported()) {
+                localLog("No partial scan because firmware roaming is supported.");
+                isScanNeeded = false;
+            } else {
+                localLog("No full band scan due to ongoing traffic");
+                isFullBandScan = false;
+            }
         }
 
-        mLastPeriodicSingleScanTimeStamp = currentTimeStamp;
-        startSingleScan(isFullBandScan, WIFI_WORK_SOURCE);
-        schedulePeriodicScanTimer(mPeriodicSingleScanInterval);
+        if (isScanNeeded) {
+            mLastPeriodicSingleScanTimeStamp = currentTimeStamp;
+            startSingleScan(isFullBandScan, WIFI_WORK_SOURCE);
+            schedulePeriodicScanTimer(mPeriodicSingleScanInterval);
 
-        // Set up the next scan interval in an exponential backoff fashion.
-        mPeriodicSingleScanInterval *= 2;
-        if (mPeriodicSingleScanInterval >  MAX_PERIODIC_SCAN_INTERVAL_MS) {
-            mPeriodicSingleScanInterval = MAX_PERIODIC_SCAN_INTERVAL_MS;
+            // Set up the next scan interval in an exponential backoff fashion.
+            mPeriodicSingleScanInterval *= 2;
+            if (mPeriodicSingleScanInterval >  MAX_PERIODIC_SCAN_INTERVAL_MS) {
+                mPeriodicSingleScanInterval = MAX_PERIODIC_SCAN_INTERVAL_MS;
+            }
+        } else {
+            // Since we already skipped this scan, keep the same scan interval for next scan.
+            schedulePeriodicScanTimer(mPeriodicSingleScanInterval);
         }
     }
 
diff --git a/service/java/com/android/server/wifi/WifiDiagnostics.java b/service/java/com/android/server/wifi/WifiDiagnostics.java
index 921faa3..30294bd 100644
--- a/service/java/com/android/server/wifi/WifiDiagnostics.java
+++ b/service/java/com/android/server/wifi/WifiDiagnostics.java
@@ -454,6 +454,10 @@
             return false;
         }
 
+        if (!isVerboseLoggingEnabled()) {
+            return false;
+        }
+
         for (WifiNative.RingBufferStatus buffer : mRingBuffers){
 
             if ((buffer.flag & RING_BUFFER_FLAG_HAS_PER_PACKET_ENTRIES) != 0) {
diff --git a/tests/wifitests/runtests.sh b/tests/wifitests/runtests.sh
index 7a34bf7..d5b631c 100755
--- a/tests/wifitests/runtests.sh
+++ b/tests/wifitests/runtests.sh
@@ -41,5 +41,4 @@
 adb install -r -g "$OUT/data/app/FrameworksWifiTests/FrameworksWifiTests.apk"
 
 adb shell am instrument -w "$@" \
-  -e notAnnotation com.android.server.wifi.DisabledForUpdateToAnyMatcher \
   'com.android.server.wifi.test/com.android.server.wifi.CustomTestRunner'
diff --git a/tests/wifitests/src/com/android/server/wifi/DisabledForUpdateToAnyMatcher.java b/tests/wifitests/src/com/android/server/wifi/DisabledForUpdateToAnyMatcher.java
deleted file mode 100644
index 6f1df47..0000000
--- a/tests/wifitests/src/com/android/server/wifi/DisabledForUpdateToAnyMatcher.java
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.server.wifi;
-
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-
-@Retention(RetentionPolicy.RUNTIME)
-public @interface DisabledForUpdateToAnyMatcher {
-}
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java
index 4fabd9d..30f823a 100644
--- a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java
@@ -1027,11 +1027,12 @@
      * Verify that we perform partial scan when the currently connected network's tx/rx success
      * rate is high and when the currently connected network is present in scan
      * cache in WifiConfigManager.
+     * WifiConnectivityManager does partial scan only when firmware roaming is not supported.
      *
-     * Expected behavior: WifiConnectivityManager does full band scan.
+     * Expected behavior: WifiConnectivityManager does partial scan.
      */
     @Test
-    public void checkSingleScanSettingsWhenConnectedWithHighDataRate() {
+    public void checkPartialScanRequestedWithHighDataRateWithoutFwRoaming() {
         mWifiInfo.txSuccessRate = mFullScanMaxTxPacketRate * 2;
         mWifiInfo.rxSuccessRate = mFullScanMaxRxPacketRate * 2;
 
@@ -1044,6 +1045,7 @@
                 .thenReturn(new WifiConfiguration());
         when(mWifiConfigManager.fetchChannelSetForNetworkForPartialScan(anyInt(), anyLong(),
                 anyInt())).thenReturn(channelList);
+        when(mWifiConnectivityHelper.isFirmwareRoamingSupported()).thenReturn(false);
 
         doAnswer(new AnswerWithArguments() {
             public void answer(ScanSettings settings, ScanListener listener,
@@ -1066,6 +1068,52 @@
     }
 
     /**
+     * Verify that we skip the partial scan when:
+     * 1. The currently connected network's tx/rx success rate is high.
+     * 2. When the currently connected network is present in scan
+     * cache in WifiConfigManager.
+     * 3. When firmware roaming is supported.
+     * Expected behavior: WifiConnectivityManager does no scan, but periodic scans
+     * are still scheduled.
+     */
+    @Test
+    public void checkPartialScanSkippedWithHighDataRateWithFwRoaming() {
+        mWifiInfo.txSuccessRate = mFullScanMaxTxPacketRate * 2;
+        mWifiInfo.rxSuccessRate = mFullScanMaxRxPacketRate * 2;
+
+        long currentTimeStamp = CURRENT_SYSTEM_TIME_MS;
+        when(mClock.getElapsedSinceBootMillis()).thenReturn(currentTimeStamp);
+
+        final HashSet<Integer> channelList = new HashSet<>();
+        channelList.add(1);
+        channelList.add(2);
+        channelList.add(3);
+
+        when(mWifiStateMachine.getCurrentWifiConfiguration())
+                .thenReturn(new WifiConfiguration());
+        when(mWifiConfigManager.fetchChannelSetForNetworkForPartialScan(anyInt(), anyLong(),
+                anyInt())).thenReturn(channelList);
+        // No scan will be requested when firmware roaming control is not supported.
+        when(mWifiConnectivityHelper.isFirmwareRoamingSupported()).thenReturn(true);
+
+        // Set screen to ON
+        mWifiConnectivityManager.handleScreenStateChanged(true);
+
+        // Set WiFi to connected state to trigger periodic scan
+        mWifiConnectivityManager.handleConnectionStateChanged(
+                WifiConnectivityManager.WIFI_STATE_CONNECTED);
+
+        verify(mWifiScanner, never()).startScan(anyObject(), anyObject(), anyObject());
+
+        // Get the first periodic scan interval to check that we are still scheduling
+        // periodic scans.
+        long firstIntervalMs = mAlarmManager
+                .getTriggerTimeMillis(WifiConnectivityManager.PERIODIC_SCAN_TIMER_TAG)
+                - currentTimeStamp;
+        assertEquals(firstIntervalMs, WifiConnectivityManager.PERIODIC_SCAN_INTERVAL_MS);
+    }
+
+    /**
      * Verify that we fall back to full band scan when the currently connected network's tx/rx
      * success rate is high and the currently connected network is not present in scan cache in
      * WifiConfigManager. This is simulated by returning an empty hashset in |makeChannelList|.
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiDiagnosticsTest.java b/tests/wifitests/src/com/android/server/wifi/WifiDiagnosticsTest.java
index 6b93e05..f607b33 100644
--- a/tests/wifitests/src/com/android/server/wifi/WifiDiagnosticsTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/WifiDiagnosticsTest.java
@@ -20,6 +20,7 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.AdditionalMatchers.gt;
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Matchers.contains;
 import static org.mockito.Mockito.anyInt;
@@ -119,7 +120,7 @@
     /** Verifies that startLogging() registers a logging event handler. */
     @Test
     public void startLoggingRegistersLogEventHandler() throws Exception {
-        final boolean verbosityToggle = false;  // even default mode wants log events from HAL
+        final boolean verbosityToggle = false;  // even default mode registers handler
         mWifiDiagnostics.startLogging(verbosityToggle);
         verify(mWifiNative).setLoggingEventHandler(anyObject());
     }
@@ -131,7 +132,7 @@
     @Test
     public void startLoggingRegistersLogEventHandlerIfPriorAttemptFailed()
             throws Exception {
-        final boolean verbosityToggle = false;  // even default mode wants log events from HAL
+        final boolean verbosityToggle = false;  // even default mode registers handler
 
         when(mWifiNative.setLoggingEventHandler(anyObject())).thenReturn(false);
         mWifiDiagnostics.startLogging(verbosityToggle);
@@ -147,7 +148,7 @@
     @Test
     public void startLoggingDoesNotRegisterLogEventHandlerIfPriorAttemptSucceeded()
             throws Exception {
-        final boolean verbosityToggle = false;  // even default mode wants log events from HAL
+        final boolean verbosityToggle = false;  // even default mode registers handler
 
         when(mWifiNative.setLoggingEventHandler(anyObject())).thenReturn(true);
         mWifiDiagnostics.startLogging(verbosityToggle);
@@ -166,19 +167,32 @@
      * b) instructs WifiNative to enable ring buffers of the appropriate log level.
      */
     @Test
-    public void startLoggingStopsAndRestartsRingBufferLogging() throws Exception {
-        final boolean verbosityToggle = false;
+    public void startLoggingStopsAndRestartsRingBufferLoggingInVerboseMode() throws Exception {
+        final boolean verbosityToggle = true;
         setBuildPropertiesToEnableRingBuffers();
         mWifiDiagnostics.startLogging(verbosityToggle);
         verify(mWifiNative).startLoggingRingBuffer(
                 eq(WifiDiagnostics.VERBOSE_NO_LOG), anyInt(), anyInt(), anyInt(),
                 eq(FAKE_RING_BUFFER_NAME));
         verify(mWifiNative).startLoggingRingBuffer(
-                eq(WifiDiagnostics.VERBOSE_NORMAL_LOG), anyInt(), anyInt(), anyInt(),
+                eq(WifiDiagnostics.VERBOSE_LOG_WITH_WAKEUP), anyInt(), anyInt(), anyInt(),
                 eq(FAKE_RING_BUFFER_NAME));
     }
 
     @Test
+    public void startLoggingStopsButDoesNotStartRingBufferLoggingInNormalMode() throws Exception {
+        final boolean verbosityToggle = false;
+        setBuildPropertiesToEnableRingBuffers();
+        mWifiDiagnostics.startLogging(verbosityToggle);
+        verify(mWifiNative).startLoggingRingBuffer(
+                eq(WifiDiagnostics.VERBOSE_NO_LOG), anyInt(), anyInt(), anyInt(),
+                eq(FAKE_RING_BUFFER_NAME));
+        verify(mWifiNative, never()).startLoggingRingBuffer(
+                gt(WifiDiagnostics.VERBOSE_NO_LOG), anyInt(), anyInt(), anyInt(),
+                anyString());
+    }
+
+    @Test
     public void startLoggingDoesNotStartRingBuffersOnUserBuilds() throws Exception {
         final boolean verbosityToggle = true;
         mWifiDiagnostics.startLogging(verbosityToggle);
@@ -189,7 +203,7 @@
     /** Verifies that, if a log handler was registered, then stopLogging() resets it. */
     @Test
     public void stopLoggingResetsLogHandlerIfHandlerWasRegistered() throws Exception {
-        final boolean verbosityToggle = false;  // even default mode wants log events from HAL
+        final boolean verbosityToggle = false;  // even default mode registers handler
 
         when(mWifiNative.setLoggingEventHandler(anyObject())).thenReturn(true);
         mWifiDiagnostics.startLogging(verbosityToggle);
@@ -202,7 +216,6 @@
     /** Verifies that, if a log handler is not registered, stopLogging() skips resetLogHandler(). */
     @Test
     public void stopLoggingOnlyResetsLogHandlerIfHandlerWasRegistered() throws Exception {
-        final boolean verbosityToggle = false;  // even default mode wants log events from HAL
         mWifiDiagnostics.stopLogging();
         verify(mWifiNative, never()).resetLogHandler();
     }
@@ -210,7 +223,7 @@
     /** Verifies that stopLogging() remembers that we've reset the log handler. */
     @Test
     public void multipleStopLoggingCallsOnlyResetLogHandlerOnce() throws Exception {
-        final boolean verbosityToggle = false;  // even default mode wants log events from HAL
+        final boolean verbosityToggle = false;  // even default mode registers handler
 
         when(mWifiNative.setLoggingEventHandler(anyObject())).thenReturn(true);
         mWifiDiagnostics.startLogging(verbosityToggle);