Add BluetoothLeScanner, ScanResult and ScanCallback.
Test: wraper class skips test. Will be tested by other tests.
Bug: 200231384
Change-Id: Ia4de9e7174257ba8b327fcf328bb6e3c0a0cb34d
diff --git a/nearby/service/java/com/android/server/nearby/common/bluetooth/testability/android/bluetooth/le/BluetoothLeScanner.java b/nearby/service/java/com/android/server/nearby/common/bluetooth/testability/android/bluetooth/le/BluetoothLeScanner.java
new file mode 100644
index 0000000..8a13abe
--- /dev/null
+++ b/nearby/service/java/com/android/server/nearby/common/bluetooth/testability/android/bluetooth/le/BluetoothLeScanner.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright 2021 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.bluetooth.testability.android.bluetooth.le;
+
+import android.annotation.TargetApi;
+import android.app.PendingIntent;
+import android.bluetooth.le.ScanFilter;
+import android.bluetooth.le.ScanSettings;
+import android.os.Build;
+
+import java.util.List;
+
+import javax.annotation.Nullable;
+
+/**
+ * Mockable wrapper of {@link android.bluetooth.le.BluetoothLeScanner}.
+ */
+@TargetApi(Build.VERSION_CODES.LOLLIPOP)
+public class BluetoothLeScanner {
+
+ private final android.bluetooth.le.BluetoothLeScanner mWrappedBluetoothLeScanner;
+
+ private BluetoothLeScanner(android.bluetooth.le.BluetoothLeScanner bluetoothLeScanner) {
+ mWrappedBluetoothLeScanner = bluetoothLeScanner;
+ }
+
+ /**
+ * See {@link android.bluetooth.le.BluetoothLeScanner#startScan(List, ScanSettings,
+ * android.bluetooth.le.ScanCallback)}.
+ */
+ public void startScan(List<ScanFilter> filters, ScanSettings settings,
+ ScanCallback callback) {
+ mWrappedBluetoothLeScanner.startScan(filters, settings, callback.unwrap());
+ }
+
+ /**
+ * See {@link android.bluetooth.le.BluetoothLeScanner#startScan(List, ScanSettings,
+ * PendingIntent)}.
+ */
+ public void startScan(
+ List<ScanFilter> filters, ScanSettings settings, PendingIntent callbackIntent) {
+ mWrappedBluetoothLeScanner.startScan(filters, settings, callbackIntent);
+ }
+
+ /**
+ * See {@link
+ * android.bluetooth.le.BluetoothLeScanner#startScan(android.bluetooth.le.ScanCallback)}.
+ */
+ public void startScan(ScanCallback callback) {
+ mWrappedBluetoothLeScanner.startScan(callback.unwrap());
+ }
+
+ /**
+ * See
+ * {@link android.bluetooth.le.BluetoothLeScanner#stopScan(android.bluetooth.le.ScanCallback)}.
+ */
+ public void stopScan(ScanCallback callback) {
+ mWrappedBluetoothLeScanner.stopScan(callback.unwrap());
+ }
+
+ /** See {@link android.bluetooth.le.BluetoothLeScanner#stopScan(PendingIntent)}. */
+ public void stopScan(PendingIntent callbackIntent) {
+ mWrappedBluetoothLeScanner.stopScan(callbackIntent);
+ }
+
+ /** Wraps a Bluetooth LE scanner. */
+ @Nullable
+ public static BluetoothLeScanner wrap(
+ @Nullable android.bluetooth.le.BluetoothLeScanner bluetoothLeScanner) {
+ if (bluetoothLeScanner == null) {
+ return null;
+ }
+ return new BluetoothLeScanner(bluetoothLeScanner);
+ }
+}
diff --git a/nearby/service/java/com/android/server/nearby/common/bluetooth/testability/android/bluetooth/le/ScanCallback.java b/nearby/service/java/com/android/server/nearby/common/bluetooth/testability/android/bluetooth/le/ScanCallback.java
new file mode 100644
index 0000000..70926a7
--- /dev/null
+++ b/nearby/service/java/com/android/server/nearby/common/bluetooth/testability/android/bluetooth/le/ScanCallback.java
@@ -0,0 +1,93 @@
+/*
+ * Copyright 2021 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.bluetooth.testability.android.bluetooth.le;
+
+import android.annotation.TargetApi;
+import android.os.Build;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Wrapper of {@link android.bluetooth.le.ScanCallback} that uses mockable objects.
+ */
+@TargetApi(Build.VERSION_CODES.LOLLIPOP)
+public abstract class ScanCallback {
+
+ /** See {@link android.bluetooth.le.ScanCallback#SCAN_FAILED_ALREADY_STARTED} */
+ public static final int SCAN_FAILED_ALREADY_STARTED =
+ android.bluetooth.le.ScanCallback.SCAN_FAILED_ALREADY_STARTED;
+
+ /** See {@link android.bluetooth.le.ScanCallback#SCAN_FAILED_APPLICATION_REGISTRATION_FAILED} */
+ public static final int SCAN_FAILED_APPLICATION_REGISTRATION_FAILED =
+ android.bluetooth.le.ScanCallback.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED;
+
+ /** See {@link android.bluetooth.le.ScanCallback#SCAN_FAILED_FEATURE_UNSUPPORTED} */
+ public static final int SCAN_FAILED_FEATURE_UNSUPPORTED =
+ android.bluetooth.le.ScanCallback.SCAN_FAILED_FEATURE_UNSUPPORTED;
+
+ /** See {@link android.bluetooth.le.ScanCallback#SCAN_FAILED_INTERNAL_ERROR} */
+ public static final int SCAN_FAILED_INTERNAL_ERROR =
+ android.bluetooth.le.ScanCallback.SCAN_FAILED_INTERNAL_ERROR;
+
+ private final android.bluetooth.le.ScanCallback mWrappedScanCallback =
+ new InternalScanCallback();
+
+ /**
+ * See {@link android.bluetooth.le.ScanCallback#onScanFailed(int)}
+ */
+ public void onScanFailed(int errorCode) {}
+
+ /**
+ * See
+ * {@link android.bluetooth.le.ScanCallback#onScanResult(int, android.bluetooth.le.ScanResult)}.
+ */
+ public void onScanResult(int callbackType, ScanResult result) {}
+
+ /**
+ * See {@link
+ * android.bluetooth.le.ScanCallback#onBatchScanResult(List<android.bluetooth.le.ScanResult>)}.
+ */
+ public void onBatchScanResults(List<ScanResult> results) {}
+
+ /** Unwraps scan callback. */
+ public android.bluetooth.le.ScanCallback unwrap() {
+ return mWrappedScanCallback;
+ }
+
+ /** Forward callback to testable instance. */
+ private class InternalScanCallback extends android.bluetooth.le.ScanCallback {
+ @Override
+ public void onScanFailed(int errorCode) {
+ ScanCallback.this.onScanFailed(errorCode);
+ }
+
+ @Override
+ public void onScanResult(int callbackType, android.bluetooth.le.ScanResult result) {
+ ScanCallback.this.onScanResult(callbackType, ScanResult.wrap(result));
+ }
+
+ @Override
+ public void onBatchScanResults(List<android.bluetooth.le.ScanResult> results) {
+ List<ScanResult> wrappedScanResults = new ArrayList<>();
+ for (android.bluetooth.le.ScanResult result : results) {
+ wrappedScanResults.add(ScanResult.wrap(result));
+ }
+ ScanCallback.this.onBatchScanResults(wrappedScanResults);
+ }
+ }
+}
diff --git a/nearby/service/java/com/android/server/nearby/common/bluetooth/testability/android/bluetooth/le/ScanResult.java b/nearby/service/java/com/android/server/nearby/common/bluetooth/testability/android/bluetooth/le/ScanResult.java
new file mode 100644
index 0000000..1a6b7b3
--- /dev/null
+++ b/nearby/service/java/com/android/server/nearby/common/bluetooth/testability/android/bluetooth/le/ScanResult.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2021 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.bluetooth.testability.android.bluetooth.le;
+
+import android.annotation.TargetApi;
+import android.bluetooth.le.ScanRecord;
+import android.os.Build;
+
+import com.android.server.nearby.common.bluetooth.testability.android.bluetooth.BluetoothDevice;
+
+import javax.annotation.Nullable;
+
+/**
+ * Mockable wrapper of {@link android.bluetooth.le.ScanResult}.
+ */
+@TargetApi(Build.VERSION_CODES.LOLLIPOP)
+public class ScanResult {
+
+ private final android.bluetooth.le.ScanResult mWrappedScanResult;
+
+ private ScanResult(android.bluetooth.le.ScanResult scanResult) {
+ mWrappedScanResult = scanResult;
+ }
+
+ /** See {@link android.bluetooth.le.ScanResult#getScanRecord()}. */
+ @Nullable
+ public ScanRecord getScanRecord() {
+ return mWrappedScanResult.getScanRecord();
+ }
+
+ /** See {@link android.bluetooth.le.ScanResult#getRssi()}. */
+ public int getRssi() {
+ return mWrappedScanResult.getRssi();
+ }
+
+ /** See {@link android.bluetooth.le.ScanResult#getTimestampNanos()}. */
+ public long getTimestampNanos() {
+ return mWrappedScanResult.getTimestampNanos();
+ }
+
+ /** See {@link android.bluetooth.le.ScanResult#getDevice()}. */
+ public BluetoothDevice getDevice() {
+ return BluetoothDevice.wrap(mWrappedScanResult.getDevice());
+ }
+
+ /** Creates a wrapper of scan result. */
+ public static ScanResult wrap(android.bluetooth.le.ScanResult scanResult) {
+ return new ScanResult(scanResult);
+ }
+}