Check permission before sending batch scan result

Use same checks as for regular scan results

Bug: 172670415
Test: tested using sample app on master branch
Change-Id: I4274026943ce64a51a30c3fbf6cc85eec853ad4f
(cherry picked from commit 9c202eda348241c5adf1b18a5a1e0ad31b2d30f8)
diff --git a/src/com/android/bluetooth/gatt/GattService.java b/src/com/android/bluetooth/gatt/GattService.java
index 87e6e52..8a792fe 100644
--- a/src/com/android/bluetooth/gatt/GattService.java
+++ b/src/com/android/bluetooth/gatt/GattService.java
@@ -1633,6 +1633,15 @@
         mScanManager.callbackDone(clientIf, status);
     }
 
+    ScanClient findBatchScanClientById(int scannerId) {
+        for (ScanClient client : mScanManager.getBatchScanQueue()) {
+            if (client.scannerId == scannerId) {
+                return client;
+            }
+        }
+        return null;
+    }
+
     void onBatchScanReports(int status, int scannerId, int reportType, int numRecords,
             byte[] recordData) throws RemoteException {
         if (DBG) {
@@ -1647,12 +1656,36 @@
             if (app == null) {
                 return;
             }
+
+            ScanClient client = findBatchScanClientById(scannerId);
+            if (client == null) {
+                return;
+            }
+
+            ArrayList<ScanResult> permittedResults;
+            if (hasScanResultPermission(client)) {
+                permittedResults = new ArrayList<ScanResult>(results);
+            } else {
+                permittedResults = new ArrayList<ScanResult>();
+                for (ScanResult scanResult : results) {
+                    for (String associatedDevice : client.associatedDevices) {
+                        if (associatedDevice.equalsIgnoreCase(scanResult.getDevice()
+                                    .getAddress())) {
+                            permittedResults.add(scanResult);
+                        }
+                    }
+                }
+                if (permittedResults.isEmpty()) {
+                    return;
+                }
+            }
+
             if (app.callback != null) {
-                app.callback.onBatchScanResults(new ArrayList<ScanResult>(results));
+                app.callback.onBatchScanResults(permittedResults);
             } else {
                 // PendingIntent based
                 try {
-                    sendResultsByPendingIntent(app.info, new ArrayList<ScanResult>(results),
+                    sendResultsByPendingIntent(app.info, permittedResults,
                             ScanSettings.CALLBACK_TYPE_ALL_MATCHES);
                 } catch (PendingIntent.CanceledException e) {
                 }
@@ -1688,13 +1721,31 @@
         if (app == null) {
             return;
         }
+
+        ArrayList<ScanResult> permittedResults;
+        if (hasScanResultPermission(client)) {
+            permittedResults = new ArrayList<ScanResult>(allResults);
+        } else {
+            permittedResults = new ArrayList<ScanResult>();
+            for (ScanResult scanResult : allResults) {
+                for (String associatedDevice : client.associatedDevices) {
+                    if (associatedDevice.equalsIgnoreCase(scanResult.getDevice().getAddress())) {
+                        permittedResults.add(scanResult);
+                    }
+                }
+            }
+            if (permittedResults.isEmpty()) {
+                return;
+            }
+        }
+
         if (client.filters == null || client.filters.isEmpty()) {
-            sendBatchScanResults(app, client, new ArrayList<ScanResult>(allResults));
+            sendBatchScanResults(app, client, permittedResults);
             // TODO: Question to reviewer: Shouldn't there be a return here?
         }
         // Reconstruct the scan results.
         ArrayList<ScanResult> results = new ArrayList<ScanResult>();
-        for (ScanResult scanResult : allResults) {
+        for (ScanResult scanResult : permittedResults) {
             if (matchesFilters(client, scanResult)) {
                 results.add(scanResult);
             }