Merge "Removed Bluetooh ScanFilter hidden API usage." am: fdc344e6b6

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1845060

Change-Id: Id34dc2bb88b92ee44024c9b1ac18d8bb41b8bd2d
This commit is contained in:
Treehugger Robot
2021-10-19 14:57:26 +00:00
committed by Automerger Merge Worker
6 changed files with 191 additions and 8 deletions

View File

@@ -22,7 +22,6 @@ import static android.text.TextUtils.firstNotEmpty;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable; import android.annotation.Nullable;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.bluetooth.le.ScanFilter;
import android.compat.annotation.UnsupportedAppUsage; import android.compat.annotation.UnsupportedAppUsage;
import android.net.wifi.ScanResult; import android.net.wifi.ScanResult;
import android.os.Build; import android.os.Build;
@@ -30,9 +29,13 @@ import android.os.ParcelUuid;
import android.os.Parcelable; import android.os.Parcelable;
import android.util.Log; import android.util.Log;
import com.android.internal.annotations.VisibleForTesting;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/** @hide */ /** @hide */
@@ -73,12 +76,19 @@ public class BluetoothDeviceFilterUtils {
static boolean matchesServiceUuid(ParcelUuid serviceUuid, ParcelUuid serviceUuidMask, static boolean matchesServiceUuid(ParcelUuid serviceUuid, ParcelUuid serviceUuidMask,
BluetoothDevice device) { BluetoothDevice device) {
ParcelUuid[] uuids = device.getUuids(); boolean result = false;
final boolean result = serviceUuid == null || List<ParcelUuid> deviceUuids = device.getUuids() == null
ScanFilter.matchesServiceUuids( ? Collections.emptyList() : Arrays.asList(device.getUuids());
serviceUuid, if (serviceUuid == null) {
serviceUuidMask, result = true;
uuids == null ? Collections.emptyList() : Arrays.asList(uuids)); } else {
for (ParcelUuid parcelUuid : deviceUuids) {
UUID uuidMask = serviceUuidMask == null ? null : serviceUuidMask.getUuid();
if (uuidsMaskedEquals(parcelUuid.getUuid(), serviceUuid.getUuid(), uuidMask)) {
result = true;
}
}
}
if (DEBUG) debugLogMatchResult(result, device, serviceUuid); if (DEBUG) debugLogMatchResult(result, device, serviceUuid);
return result; return result;
} }
@@ -143,4 +153,23 @@ public class BluetoothDeviceFilterUtils {
throw new IllegalArgumentException("Unknown device type: " + device); throw new IllegalArgumentException("Unknown device type: " + device);
} }
} }
/**
* Compares two {@link #UUID} with a {@link #UUID} mask.
*
* @param data first {@link #UUID}.
* @param uuid second {@link #UUID}.
* @param mask mask {@link #UUID}.
* @return true if both UUIDs are equals when masked, false otherwise.
*/
@VisibleForTesting
public static boolean uuidsMaskedEquals(UUID data, UUID uuid, UUID mask) {
if (mask == null) {
return Objects.equals(data, uuid);
}
return (data.getLeastSignificantBits() & mask.getLeastSignificantBits())
== (uuid.getLeastSignificantBits() & mask.getLeastSignificantBits())
&& (data.getMostSignificantBits() & mask.getMostSignificantBits())
== (uuid.getMostSignificantBits() & mask.getMostSignificantBits());
}
} }

View File

@@ -75,7 +75,7 @@ public final class BluetoothLeDeviceFilter implements DeviceFilter<ScanResult> {
String renameSuffix, int renameBytesFrom, int renameBytesLength, String renameSuffix, int renameBytesFrom, int renameBytesLength,
int renameNameFrom, int renameNameLength, boolean renameBytesReverseOrder) { int renameNameFrom, int renameNameLength, boolean renameBytesReverseOrder) {
mNamePattern = namePattern; mNamePattern = namePattern;
mScanFilter = ObjectUtils.firstNotNull(scanFilter, ScanFilter.EMPTY); mScanFilter = ObjectUtils.firstNotNull(scanFilter, new ScanFilter.Builder().build());
mRawDataFilter = rawDataFilter; mRawDataFilter = rawDataFilter;
mRawDataFilterMask = rawDataFilterMask; mRawDataFilterMask = rawDataFilterMask;
mRenamePrefix = renamePrefix; mRenamePrefix = renamePrefix;

View File

@@ -0,0 +1,21 @@
package {
// See: http://go/android-license-faq
// A large-scale-change added 'default_applicable_licenses' to import
// all of the 'license_kinds' from "frameworks_base_license"
// to get the below license kinds:
// SPDX-license-identifier-Apache-2.0
default_applicable_licenses: ["frameworks_base_license"],
}
android_test {
name: "CompanionTests",
// Include all test java files.
srcs: ["src/**/*.java"],
libs: [
"android.test.runner",
"android.test.base",
],
static_libs: ["junit"],
platform_apis: true,
certificate: "platform",
}

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2011 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.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.companion.tests"
android:sharedUserId="android.uid.system" >
<application >
<uses-library android:name="android.test.runner" />
</application>
<instrumentation android:name="android.companion.CompanionTestRunner"
android:targetPackage="com.android.companion.tests"
android:label="Companion Tests" />
</manifest>

View File

@@ -0,0 +1,57 @@
/*
* Copyright (C) 2010 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 android.companion;
import android.os.ParcelUuid;
import android.test.InstrumentationTestCase;
public class BluetoothDeviceFilterUtilsTest extends InstrumentationTestCase {
private static final String TAG = "BluetoothDeviceFilterUtilsTest";
private final ParcelUuid mServiceUuid =
ParcelUuid.fromString("F0FFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF");
private final ParcelUuid mNonMatchingDeviceUuid =
ParcelUuid.fromString("FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF");
private final ParcelUuid mMatchingDeviceUuid =
ParcelUuid.fromString("F0FFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF");
private final ParcelUuid mMaskUuid =
ParcelUuid.fromString("FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF");
private final ParcelUuid mMatchingMaskUuid =
ParcelUuid.fromString("F0FFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF");
@Override
protected void setUp() throws Exception {
super.setUp();
}
public void testUuidsMaskedEquals() {
assertFalse(BluetoothDeviceFilterUtils.uuidsMaskedEquals(
mNonMatchingDeviceUuid.getUuid(),
mServiceUuid.getUuid(),
mMaskUuid.getUuid()));
assertTrue(BluetoothDeviceFilterUtils.uuidsMaskedEquals(
mMatchingDeviceUuid.getUuid(),
mServiceUuid.getUuid(),
mMaskUuid.getUuid()));
assertTrue(BluetoothDeviceFilterUtils.uuidsMaskedEquals(
mNonMatchingDeviceUuid.getUuid(),
mServiceUuid.getUuid(),
mMatchingMaskUuid.getUuid()));
}
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright (C) 2010 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 android.companion;
import android.os.Bundle;
import android.test.InstrumentationTestRunner;
import android.test.InstrumentationTestSuite;
import junit.framework.TestSuite;
/**
* Instrumentation test runner for Companion tests.
*/
public class CompanionTestRunner extends InstrumentationTestRunner {
private static final String TAG = "CompanionTestRunner";
@Override
public TestSuite getAllTests() {
TestSuite suite = new InstrumentationTestSuite(this);
suite.addTestSuite(BluetoothDeviceFilterUtilsTest.class);
return suite;
}
@Override
public ClassLoader getLoader() {
return CompanionTestRunner.class.getClassLoader();
}
@Override
public void onCreate(Bundle arguments) {
super.onCreate(arguments);
}
}