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

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

Change-Id: I03fe7ee813070c7d26d3409edfdf599572021cbb
This commit is contained in:
Treehugger Robot
2021-10-19 15:32:24 +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.Nullable;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.le.ScanFilter;
import android.compat.annotation.UnsupportedAppUsage;
import android.net.wifi.ScanResult;
import android.os.Build;
@@ -30,9 +29,13 @@ import android.os.ParcelUuid;
import android.os.Parcelable;
import android.util.Log;
import com.android.internal.annotations.VisibleForTesting;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.regex.Pattern;
/** @hide */
@@ -73,12 +76,19 @@ public class BluetoothDeviceFilterUtils {
static boolean matchesServiceUuid(ParcelUuid serviceUuid, ParcelUuid serviceUuidMask,
BluetoothDevice device) {
ParcelUuid[] uuids = device.getUuids();
final boolean result = serviceUuid == null ||
ScanFilter.matchesServiceUuids(
serviceUuid,
serviceUuidMask,
uuids == null ? Collections.emptyList() : Arrays.asList(uuids));
boolean result = false;
List<ParcelUuid> deviceUuids = device.getUuids() == null
? Collections.emptyList() : Arrays.asList(device.getUuids());
if (serviceUuid == null) {
result = true;
} 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);
return result;
}
@@ -143,4 +153,23 @@ public class BluetoothDeviceFilterUtils {
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,
int renameNameFrom, int renameNameLength, boolean renameBytesReverseOrder) {
mNamePattern = namePattern;
mScanFilter = ObjectUtils.firstNotNull(scanFilter, ScanFilter.EMPTY);
mScanFilter = ObjectUtils.firstNotNull(scanFilter, new ScanFilter.Builder().build());
mRawDataFilter = rawDataFilter;
mRawDataFilterMask = rawDataFilterMask;
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);
}
}