Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9aeb4468 authored by Etienne Ruffieux's avatar Etienne Ruffieux
Browse files

Removed Bluetooh ScanFilter hidden API usage.

Replaced hidden API usage in Companion by public
methods.

Tag: #feature
Bug: 200202917
Test: atest CompanionTests
Change-Id: Ifc7cdcbf40e23d1c8870e4dcd44772dcae55120a
parent 5757f89d
Loading
Loading
Loading
Loading
+36 −7
Original line number Diff line number Diff line
@@ -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());
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -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;
+21 −0
Original line number Diff line number Diff line
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",
}
+28 −0
Original line number Diff line number Diff line
<?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>
+57 −0
Original line number Diff line number Diff line
/*
 * 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()));
    }
}
Loading