Records LE Scan Activity Count

- LE batch scan result count
- LE scan enable/disable count
- LE scan timeout count

Bug: 236321914
Test: atest BluetoothInstrumentationTests and device test
Tag: #feature
Change-Id: Iecac88cd375d8bb8067594615e48a763edc310d5
diff --git a/android/app/src/com/android/bluetooth/gatt/AppScanStats.java b/android/app/src/com/android/bluetooth/gatt/AppScanStats.java
index 676d2a8..46617b1 100644
--- a/android/app/src/com/android/bluetooth/gatt/AppScanStats.java
+++ b/android/app/src/com/android/bluetooth/gatt/AppScanStats.java
@@ -272,6 +272,7 @@
                 mWorkSourceUtil.getUids(), mWorkSourceUtil.getTags(),
                 BluetoothStatsLog.BLE_SCAN_STATE_CHANGED__STATE__ON,
                 scan.isFilterScan, scan.isBackgroundScan, scan.isOpportunisticScan);
+        recordScanAppCountMetricsStart(scan);
 
         mOngoingScans.put(scannerId, scan);
     }
@@ -338,6 +339,44 @@
                 mWorkSourceUtil.getUids(), mWorkSourceUtil.getTags(),
                 BluetoothStatsLog.BLE_SCAN_STATE_CHANGED__STATE__OFF,
                 scan.isFilterScan, scan.isBackgroundScan, scan.isOpportunisticScan);
+        recordScanAppCountMetricsStop(scan);
+    }
+
+    private void recordScanAppCountMetricsStart(LastScan scan) {
+        MetricsLogger logger = MetricsLogger.getInstance();
+        logger.cacheCount(BluetoothProtoEnums.LE_SCAN_COUNT_TOTAL_ENABLE, 1);
+        if (scan.isAutoBatchScan) {
+            logger.cacheCount(BluetoothProtoEnums.LE_SCAN_COUNT_AUTO_BATCH_ENABLE, 1);
+        } else if (scan.isBatchScan) {
+            logger.cacheCount(BluetoothProtoEnums.LE_SCAN_COUNT_BATCH_ENABLE, 1);
+        } else {
+            if (scan.isFilterScan) {
+                logger.cacheCount(BluetoothProtoEnums.LE_SCAN_COUNT_FILTERED_ENABLE, 1);
+            } else {
+                logger.cacheCount(BluetoothProtoEnums.LE_SCAN_COUNT_UNFILTERED_ENABLE, 1);
+            }
+        }
+    }
+
+    private void recordScanAppCountMetricsStop(LastScan scan) {
+        MetricsLogger logger = MetricsLogger.getInstance();
+        logger.cacheCount(BluetoothProtoEnums.LE_SCAN_COUNT_TOTAL_DISABLE, 1);
+        if (scan.isAutoBatchScan) {
+            logger.cacheCount(BluetoothProtoEnums.LE_SCAN_COUNT_AUTO_BATCH_DISABLE, 1);
+        } else if (scan.isBatchScan) {
+            logger.cacheCount(BluetoothProtoEnums.LE_SCAN_COUNT_BATCH_DISABLE, 1);
+        } else {
+            if (scan.isFilterScan) {
+                logger.cacheCount(BluetoothProtoEnums.LE_SCAN_COUNT_FILTERED_DISABLE, 1);
+            } else {
+                logger.cacheCount(BluetoothProtoEnums.LE_SCAN_COUNT_UNFILTERED_DISABLE, 1);
+            }
+        }
+    }
+
+    synchronized void recordScanTimeoutCountMetrics() {
+        MetricsLogger.getInstance()
+                .cacheCount(BluetoothProtoEnums.LE_SCAN_ABUSE_COUNT_SCAN_TIMEOUT, 1);
     }
 
     static void initScanRadioState() {
@@ -374,20 +413,20 @@
         if (!sIsRadioStarted) {
             return;
         }
+        MetricsLogger logger = MetricsLogger.getInstance();
         long currentTime = SystemClock.elapsedRealtime();
         long radioScanDuration = currentTime - sRadioStartTime;
         double scanWeight = getScanWeight(sRadioScanMode) * 0.01;
         long weightedDuration = (long) (radioScanDuration * scanWeight);
 
         if (weightedDuration > 0) {
-            MetricsLogger.getInstance().cacheCount(
-                    BluetoothProtoEnums.LE_SCAN_RADIO_DURATION_REGULAR, weightedDuration);
+            logger.cacheCount(BluetoothProtoEnums.LE_SCAN_RADIO_DURATION_REGULAR, weightedDuration);
             if (sIsScreenOn) {
-                MetricsLogger.getInstance().cacheCount(
+                logger.cacheCount(
                         BluetoothProtoEnums.LE_SCAN_RADIO_DURATION_REGULAR_SCREEN_ON,
                         weightedDuration);
             } else {
-                MetricsLogger.getInstance().cacheCount(
+                logger.cacheCount(
                         BluetoothProtoEnums.LE_SCAN_RADIO_DURATION_REGULAR_SCREEN_OFF,
                         weightedDuration);
             }
@@ -427,18 +466,35 @@
             if (!sIsRadioStarted) {
                 return;
             }
-            MetricsLogger.getInstance().cacheCount(
-                    BluetoothProtoEnums.LE_SCAN_RESULTS_COUNT_REGULAR, 1);
+            MetricsLogger logger = MetricsLogger.getInstance();
+            logger.cacheCount(BluetoothProtoEnums.LE_SCAN_RESULTS_COUNT_REGULAR, 1);
             if (sIsScreenOn) {
-                MetricsLogger.getInstance().cacheCount(
-                        BluetoothProtoEnums.LE_SCAN_RESULTS_COUNT_REGULAR_SCREEN_ON, 1);
+                logger.cacheCount(BluetoothProtoEnums.LE_SCAN_RESULTS_COUNT_REGULAR_SCREEN_ON, 1);
             } else {
-                MetricsLogger.getInstance().cacheCount(
-                        BluetoothProtoEnums.LE_SCAN_RESULTS_COUNT_REGULAR_SCREEN_OFF, 1);
+                logger.cacheCount(BluetoothProtoEnums.LE_SCAN_RESULTS_COUNT_REGULAR_SCREEN_OFF, 1);
             }
         }
     }
 
+    static void recordBatchScanRadioResultCount(int numRecords) {
+        boolean isScreenOn;
+        synchronized (sLock) {
+            isScreenOn = sIsScreenOn;
+        }
+        MetricsLogger logger = MetricsLogger.getInstance();
+        logger.cacheCount(BluetoothProtoEnums.LE_SCAN_RESULTS_COUNT_BATCH_BUNDLE, 1);
+        logger.cacheCount(BluetoothProtoEnums.LE_SCAN_RESULTS_COUNT_BATCH, numRecords);
+        if (isScreenOn) {
+            logger.cacheCount(BluetoothProtoEnums.LE_SCAN_RESULTS_COUNT_BATCH_BUNDLE_SCREEN_ON, 1);
+            logger.cacheCount(
+                    BluetoothProtoEnums.LE_SCAN_RESULTS_COUNT_BATCH_SCREEN_ON, numRecords);
+        } else {
+            logger.cacheCount(BluetoothProtoEnums.LE_SCAN_RESULTS_COUNT_BATCH_BUNDLE_SCREEN_OFF, 1);
+            logger.cacheCount(
+                    BluetoothProtoEnums.LE_SCAN_RESULTS_COUNT_BATCH_SCREEN_OFF, numRecords);
+        }
+    }
+
     static void setScreenState(boolean isScreenOn) {
         synchronized (sLock) {
             if (sIsScreenOn == isScreenOn) {
diff --git a/android/app/src/com/android/bluetooth/gatt/GattService.java b/android/app/src/com/android/bluetooth/gatt/GattService.java
index 60be202..a8cad1f 100644
--- a/android/app/src/com/android/bluetooth/gatt/GattService.java
+++ b/android/app/src/com/android/bluetooth/gatt/GattService.java
@@ -187,16 +187,15 @@
             UUID.fromString("00001846-0000-1000-8000-00805F9B34FB"), // CSIS
     };
 
-    /**
-     * Example raw beacons captured from a Blue Charm BC011
-     */
-    private static final String[] TEST_MODE_BEACONS = new String[] {
-            "020106",
-            "0201060303AAFE1716AAFE10EE01626C7565636861726D626561636F6E730009168020691E0EFE13551109426C7565436861726D5F313639363835000000",
-            "0201060303AAFE1716AAFE00EE626C7565636861726D31000000000001000009168020691E0EFE13551109426C7565436861726D5F313639363835000000",
-            "0201060303AAFE1116AAFE20000BF017000008874803FB93540916802069080EFE13551109426C7565436861726D5F313639363835000000000000000000",
-            "0201061AFF4C000215426C7565436861726D426561636F6E730EFE1355C509168020691E0EFE13551109426C7565436861726D5F31363936383500000000",
-    };
+    /** Example raw beacons captured from a Blue Charm BC011 */
+    private static final String[] TEST_MODE_BEACONS =
+            new String[] {
+                "020106",
+                "0201060303AAFE1716AAFE10EE01626C7565636861726D626561636F6E730009168020691E0EFE13551109426C7565436861726D5F313639363835000000",
+                "0201060303AAFE1716AAFE00EE626C7565636861726D31000000000001000009168020691E0EFE13551109426C7565436861726D5F313639363835000000",
+                "0201060303AAFE1116AAFE20000BF017000008874803FB93540916802069080EFE13551109426C7565436861726D5F313639363835000000000000000000",
+                "0201061AFF4C000215426C7565436861726D426561636F6E730EFE1355C509168020691E0EFE13551109426C7565436861726D5F31363936383500000000",
+            };
 
     /**
      * Keep the arguments passed in for the PendingIntent.
@@ -2719,6 +2718,7 @@
         // When in testing mode, ignore all real-world events
         if (isTestModeEnabled()) return;
 
+        AppScanStats.recordBatchScanRadioResultCount(numRecords);
         onBatchScanReportsInternal(status, scannerId, reportType, numRecords, recordData);
     }
 
diff --git a/android/app/src/com/android/bluetooth/gatt/ScanManager.java b/android/app/src/com/android/bluetooth/gatt/ScanManager.java
index e29117b..7f53316 100644
--- a/android/app/src/com/android/bluetooth/gatt/ScanManager.java
+++ b/android/app/src/com/android/bluetooth/gatt/ScanManager.java
@@ -1349,14 +1349,15 @@
                                     + client.scannerId + ")");
                     setOpportunisticScanClient(client);
                     removeScanFilters(client.scannerId);
-                    client.stats.setScanTimeout(client.scannerId);
+
                 } else {
                     Log.w(TAG,
                             "Moving filtered scan client to downgraded scan (scannerId "
                                     + client.scannerId + ")");
                     client.updateScanMode(ScanSettings.SCAN_MODE_LOW_POWER);
-                    client.stats.setScanTimeout(client.scannerId);
                 }
+                client.stats.setScanTimeout(client.scannerId);
+                client.stats.recordScanTimeoutCountMetrics();
             }
 
             // The scan should continue for background scans