Hook up the api for subsequent pairing

Test: build no error
Bug: 202335820
Change-Id: I14b372661552144fe26f4294b7b39ae6c1af7832
diff --git a/nearby/service/java/com/android/server/nearby/common/bloomfilter/FastPairBloomFilterHasher.java b/nearby/service/java/com/android/server/nearby/common/bloomfilter/FastPairBloomFilterHasher.java
new file mode 100644
index 0000000..0ccee97
--- /dev/null
+++ b/nearby/service/java/com/android/server/nearby/common/bloomfilter/FastPairBloomFilterHasher.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2022 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.nearby.common.bloomfilter;
+
+import com.google.common.hash.Hashing;
+
+import java.nio.ByteBuffer;
+
+/**
+ * Hasher which hashes a value using SHA-256 and splits it into parts, each of which can be
+ * converted to an index.
+ */
+public class FastPairBloomFilterHasher implements BloomFilter.Hasher {
+
+    private static final int NUM_INDEXES = 8;
+
+    @Override
+    public int[] getHashes(byte[] value) {
+        byte[] hash = Hashing.sha256().hashBytes(value).asBytes();
+        ByteBuffer buffer = ByteBuffer.wrap(hash);
+        int[] hashes = new int[NUM_INDEXES];
+        for (int i = 0; i < NUM_INDEXES; i++) {
+            hashes[i] = buffer.getInt();
+        }
+        return hashes;
+    }
+}
diff --git a/nearby/service/java/com/android/server/nearby/fastpair/FastPairAdvHandler.java b/nearby/service/java/com/android/server/nearby/fastpair/FastPairAdvHandler.java
index 0151543..672d785 100644
--- a/nearby/service/java/com/android/server/nearby/fastpair/FastPairAdvHandler.java
+++ b/nearby/service/java/com/android/server/nearby/fastpair/FastPairAdvHandler.java
@@ -16,6 +16,9 @@
 
 package com.android.server.nearby.fastpair;
 
+import static com.google.common.primitives.Bytes.concat;
+
+import android.accounts.Account;
 import android.annotation.Nullable;
 import android.content.Context;
 import android.nearby.FastPairDevice;
@@ -23,6 +26,7 @@
 import android.util.Log;
 
 import com.android.server.nearby.common.bloomfilter.BloomFilter;
+import com.android.server.nearby.common.bloomfilter.FastPairBloomFilterHasher;
 import com.android.server.nearby.common.locator.Locator;
 import com.android.server.nearby.fastpair.halfsheet.FastPairHalfSheetManager;
 import com.android.server.nearby.provider.FastPairDataProvider;
@@ -34,6 +38,7 @@
 import java.util.List;
 
 import service.proto.Cache;
+import service.proto.Data;
 import service.proto.Rpcs;
 
 /**
@@ -88,7 +93,29 @@
             }
 
         } else {
-            // Start to process bloomfilter
+            // Start to process bloom filter
+            List<Account> accountList =
+                    FastPairDataProvider.getInstance().loadFastPairEligibleAccounts();
+            byte[] bloomFilterByteArray = FastPairDecoder.getBloomFilter(fastPairDevice.getData());
+            byte[] bloomFilterSalt = FastPairDecoder.getBloomFilterSalt(fastPairDevice.getData());
+            for (Account account : accountList) {
+                try {
+                    List<Data.FastPairDeviceWithAccountKey> listDevices =
+                            FastPairDataProvider.getInstance().loadFastPairDevicesWithAccountKey(
+                                    account);
+                    Data.FastPairDeviceWithAccountKey recognizedDevice =
+                            findRecognizedDevice(listDevices,
+                                    new BloomFilter(bloomFilterByteArray,
+                                            new FastPairBloomFilterHasher()), bloomFilterSalt);
+                    if (recognizedDevice != null) {
+                        Log.d(TAG, "find matched device show notification to remind user to pair");
+                        return;
+                    }
+                } catch (IllegalStateException e) {
+                    Log.e(TAG, "OEM does not construct fast pair data proxy correctly");
+                }
+
+            }
         }
     }
 
@@ -98,13 +125,13 @@
      * is inside the bloom filter.
      */
     @Nullable
-    static FastPairDevice findRecognizedDevice(
-            List<FastPairDevice> devices, BloomFilter bloomFilter, byte[] salt) {
-        for (FastPairDevice device : devices) {
-            // byte[] rotatedKey = concat(device.getAccountKey().toByteArray(), salt);
-//            if (bloomFilter.possiblyContains(rotatedKey)) {
-//                return device;
-//            }
+    static Data.FastPairDeviceWithAccountKey findRecognizedDevice(
+            List<Data.FastPairDeviceWithAccountKey> devices, BloomFilter bloomFilter, byte[] salt) {
+        for (Data.FastPairDeviceWithAccountKey device : devices) {
+            byte[] rotatedKey = concat(device.getAccountKey().toByteArray(), salt);
+            if (bloomFilter.possiblyContains(rotatedKey)) {
+                return device;
+            }
         }
         return null;
     }