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

Commit 7bb60f03 authored by William Escande's avatar William Escande Committed by Gerrit Code Review
Browse files

Merge "Metadata: broadcast from main thread and improve logging" into main

parents afd0a936 78d2c3ed
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -16,9 +16,11 @@

package com.android.bluetooth.btservice;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.OobData;
import android.os.ParcelUuid;

import com.android.bluetooth.Utils;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;

@@ -210,8 +212,8 @@ public class AdapterNativeInterface {
        return allowLowLatencyAudioNative(allowed, address);
    }

    void metadataChanged(byte[] address, int key, byte[] value) {
        metadataChangedNative(address, key, value);
    void metadataChanged(BluetoothDevice device, int key, byte[] value) {
        metadataChangedNative(Utils.getBytesFromAddress(device.getAddress()), key, value);
    }

    boolean interopMatchAddr(String featureName, String address) {
+10 −4
Original line number Diff line number Diff line
@@ -6287,24 +6287,30 @@ public class AdapterService extends Service {

    /** Update metadata change to registered listeners */
    @VisibleForTesting
    public void metadataChanged(String address, int key, byte[] value) {
        BluetoothDevice device = mRemoteDevices.getDevice(Utils.getBytesFromAddress(address));
    public void onMetadataChanged(BluetoothDevice device, int key, byte[] value) {
        mHandler.post(() -> onMetadataChangedInternal(device, key, value));
    }

    private void onMetadataChangedInternal(BluetoothDevice device, int key, byte[] value) {
        String info = "onMetadataChangedInternal(" + device + ", " + key + ")";

        // pass just interesting metadata to native, to reduce spam
        if (key == BluetoothDevice.METADATA_LE_AUDIO) {
            mNativeInterface.metadataChanged(Utils.getBytesFromAddress(address), key, value);
            mNativeInterface.metadataChanged(device, key, value);
        }

        RemoteCallbackList<IBluetoothMetadataListener> list = mMetadataListeners.get(device);
        if (list == null) {
            Log.d(TAG, info + ": No registered listener");
            return;
        }
        int n = list.beginBroadcast();
        Log.d(TAG, info + ": Broadcast to " + n + " receivers");
        for (int i = 0; i < n; i++) {
            try {
                list.getBroadcastItem(i).onMetadataChanged(device, key, value);
            } catch (RemoteException e) {
                Log.d(TAG, "metadataChanged() - Callback #" + i + " failed (" + e + ")");
                Log.d(TAG, info + ": Callback #" + i + " failed (" + e + ")");
            }
        }
        list.finishBroadcast();
+6 −7
Original line number Diff line number Diff line
@@ -244,7 +244,7 @@ public class DatabaseManager {
            Metadata data = mMetadataCache.get(address);
            byte[] oldValue = data.getCustomizedMeta(key);
            if (oldValue != null && Arrays.equals(oldValue, newValue)) {
                Log.v(TAG, "setCustomMeta: metadata not changed.");
                Log.d(TAG, "setCustomMeta: metadata not changed.");
                return true;
            }
            logManufacturerInfo(device, key, newValue);
@@ -253,7 +253,7 @@ public class DatabaseManager {

            updateDatabase(data);
        }
        mAdapterService.metadataChanged(address, key, newValue);
        mAdapterService.onMetadataChanged(device, key, newValue);
        return true;
    }

@@ -1198,13 +1198,12 @@ public class DatabaseManager {
                                && !Arrays.asList(bondedDevices).stream()
                                        .anyMatch(device -> address.equals(device.getAddress()))) {
                            List<Integer> list = metadata.getChangedCustomizedMeta();
                            BluetoothDevice device =
                                    BluetoothAdapter.getDefaultAdapter().getRemoteDevice(address);
                            for (int key : list) {
                                mAdapterService.metadataChanged(address, key, null);
                                mAdapterService.onMetadataChanged(device, key, null);
                            }
                            Log.i(
                                    TAG,
                                    "remove unpaired device from database "
                                            + metadata.getAnonymizedAddress());
                            Log.i(TAG, "remove unpaired device from database " + device);
                            deleteDatabase(mMetadataCache.get(address));
                        }
                    });
+20 −23
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ package com.android.bluetooth.btservice.storage;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
@@ -84,6 +83,8 @@ public final class DatabaseManagerTest {
    private BluetoothDevice mTestDevice;
    private BluetoothDevice mTestDevice2;
    private BluetoothDevice mTestDevice3;
    private BluetoothDevice mTestDevice4;
    private BluetoothDevice mTestDevice5;

    private static final String LOCAL_STORAGE = "LocalStorage";
    private static final String TEST_BT_ADDR = "11:22:33:44:55:66";
@@ -93,7 +94,7 @@ public final class DatabaseManagerTest {
    private static final String OTHER_BT_ADDR2 = "22:22:22:22:22:22";
    private static final String DB_NAME = "test_db";
    private static final int A2DP_SUPPORT_OP_CODEC_TEST = 0;
    private static final int A2DP_ENALBED_OP_CODEC_TEST = 1;
    private static final int A2DP_ENABLED_OP_CODEC_TEST = 1;
    private static final int MAX_META_ID = 16;
    private static final byte[] TEST_BYTE_ARRAY = "TEST_VALUE".getBytes();

@@ -113,6 +114,8 @@ public final class DatabaseManagerTest {
        mTestDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(TEST_BT_ADDR);
        mTestDevice2 = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(TEST_BT_ADDR2);
        mTestDevice3 = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(TEST_BT_ADDR3);
        mTestDevice4 = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(OTHER_BT_ADDR1);
        mTestDevice5 = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(OTHER_BT_ADDR2);

        // Create a memory database for DatabaseManager instead of use a real database.
        mDatabase =
@@ -131,7 +134,7 @@ public final class DatabaseManagerTest {

        BluetoothDevice[] bondedDevices = {mTestDevice};
        doReturn(bondedDevices).when(mAdapterService).getBondedDevices();
        doNothing().when(mAdapterService).metadataChanged(anyString(), anyInt(), any(byte[].class));
        doNothing().when(mAdapterService).onMetadataChanged(any(), anyInt(), any());

        restartDatabaseManagerHelper();
    }
@@ -271,44 +274,44 @@ public final class DatabaseManagerTest {

        // Cases of device not in database
        testSetGetA2dpOptionalCodecsCase(
                A2DP_ENALBED_OP_CODEC_TEST,
                A2DP_ENABLED_OP_CODEC_TEST,
                false,
                BluetoothA2dp.OPTIONAL_CODECS_PREF_UNKNOWN,
                BluetoothA2dp.OPTIONAL_CODECS_PREF_UNKNOWN);
        testSetGetA2dpOptionalCodecsCase(
                A2DP_ENALBED_OP_CODEC_TEST,
                A2DP_ENABLED_OP_CODEC_TEST,
                false,
                BluetoothA2dp.OPTIONAL_CODECS_PREF_DISABLED,
                BluetoothA2dp.OPTIONAL_CODECS_PREF_UNKNOWN);
        testSetGetA2dpOptionalCodecsCase(
                A2DP_ENALBED_OP_CODEC_TEST,
                A2DP_ENABLED_OP_CODEC_TEST,
                false,
                BluetoothA2dp.OPTIONAL_CODECS_PREF_ENABLED,
                BluetoothA2dp.OPTIONAL_CODECS_PREF_UNKNOWN);
        testSetGetA2dpOptionalCodecsCase(
                A2DP_ENALBED_OP_CODEC_TEST,
                A2DP_ENABLED_OP_CODEC_TEST,
                false,
                badValue,
                BluetoothA2dp.OPTIONAL_CODECS_PREF_UNKNOWN);

        // Cases of device already in database
        testSetGetA2dpOptionalCodecsCase(
                A2DP_ENALBED_OP_CODEC_TEST,
                A2DP_ENABLED_OP_CODEC_TEST,
                true,
                BluetoothA2dp.OPTIONAL_CODECS_PREF_UNKNOWN,
                BluetoothA2dp.OPTIONAL_CODECS_PREF_UNKNOWN);
        testSetGetA2dpOptionalCodecsCase(
                A2DP_ENALBED_OP_CODEC_TEST,
                A2DP_ENABLED_OP_CODEC_TEST,
                true,
                BluetoothA2dp.OPTIONAL_CODECS_PREF_DISABLED,
                BluetoothA2dp.OPTIONAL_CODECS_PREF_DISABLED);
        testSetGetA2dpOptionalCodecsCase(
                A2DP_ENALBED_OP_CODEC_TEST,
                A2DP_ENABLED_OP_CODEC_TEST,
                true,
                BluetoothA2dp.OPTIONAL_CODECS_PREF_ENABLED,
                BluetoothA2dp.OPTIONAL_CODECS_PREF_ENABLED);
        testSetGetA2dpOptionalCodecsCase(
                A2DP_ENALBED_OP_CODEC_TEST,
                A2DP_ENABLED_OP_CODEC_TEST,
                true,
                badValue,
                BluetoothA2dp.OPTIONAL_CODECS_PREF_UNKNOWN);
@@ -333,7 +336,7 @@ public final class DatabaseManagerTest {
        TestUtils.waitForLooperToFinishScheduledTask(mDatabaseManager.getHandlerLooper());

        // Check removed device report metadata changed to null
        verify(mAdapterService).metadataChanged(OTHER_BT_ADDR1, 0, null);
        verify(mAdapterService).onMetadataChanged(mTestDevice4, 0, null);

        List<Metadata> list = mDatabase.load();

@@ -374,18 +377,14 @@ public final class DatabaseManagerTest {
        mDatabase.insert(otherData2);

        // Add OTHER_BT_ADDR1 OTHER_BT_ADDR2 to bonded devices
        BluetoothDevice otherDevice1 =
                BluetoothAdapter.getDefaultAdapter().getRemoteDevice(OTHER_BT_ADDR1);
        BluetoothDevice otherDevice2 =
                BluetoothAdapter.getDefaultAdapter().getRemoteDevice(OTHER_BT_ADDR2);
        BluetoothDevice[] bondedDevices = {otherDevice1, otherDevice2};
        BluetoothDevice[] bondedDevices = {mTestDevice4, mTestDevice5};
        doReturn(bondedDevices).when(mAdapterService).getBondedDevices();

        mDatabaseManager.removeUnusedMetadata();
        TestUtils.waitForLooperToFinishScheduledTask(mDatabaseManager.getHandlerLooper());

        // Check TEST_BT_ADDR report metadata changed to null
        verify(mAdapterService).metadataChanged(TEST_BT_ADDR, 0, null);
        verify(mAdapterService).onMetadataChanged(mTestDevice, 0, null);

        // Check number of metadata in the database
        List<Metadata> list = mDatabase.load();
@@ -1692,14 +1691,14 @@ public final class DatabaseManagerTest {
            mDatabase.insert(data);
            Assert.assertEquals(
                    expectedResult, mDatabaseManager.setCustomMeta(mTestDevice, key, testValue));
            verify(mAdapterService).metadataChanged(TEST_BT_ADDR, key, testValue);
            verify(mAdapterService).onMetadataChanged(mTestDevice, key, testValue);
            verifyTime++;
        }
        Assert.assertEquals(
                expectedResult, mDatabaseManager.setCustomMeta(mTestDevice, key, value));
        if (expectedResult) {
            // Check for callback and get value
            verify(mAdapterService, times(verifyTime)).metadataChanged(TEST_BT_ADDR, key, value);
            verify(mAdapterService, times(verifyTime)).onMetadataChanged(mTestDevice, key, value);
            Assert.assertEquals(value, mDatabaseManager.getCustomMeta(mTestDevice, key));
        } else {
            Assert.assertNull(mDatabaseManager.getCustomMeta(mTestDevice, key));
@@ -1841,9 +1840,7 @@ public final class DatabaseManagerTest {
                    return null;
                };

        doAnswer(answer)
                .when(mAdapterService)
                .metadataChanged(any(String.class), anyInt(), any(byte[].class));
        doAnswer(answer).when(mAdapterService).onMetadataChanged(any(), anyInt(), any());

        mDatabaseManager.setCustomMeta(mTestDevice, key, newValue);

+2 −2
Original line number Diff line number Diff line
@@ -4546,7 +4546,7 @@ public final class BluetoothAdapter {
            @NonNull BluetoothDevice device,
            @NonNull Executor executor,
            @NonNull OnMetadataChangedListener listener) {
        if (DBG) Log.d(TAG, "addOnMetadataChangedListener()");
        if (DBG) Log.d(TAG, "addOnMetadataChangedListener(" + device + ", " + listener + ")");
        requireNonNull(device);
        requireNonNull(executor);
        requireNonNull(listener);
@@ -4621,7 +4621,7 @@ public final class BluetoothAdapter {
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, BLUETOOTH_PRIVILEGED})
    public boolean removeOnMetadataChangedListener(
            @NonNull BluetoothDevice device, @NonNull OnMetadataChangedListener listener) {
        if (DBG) Log.d(TAG, "removeOnMetadataChangedListener()");
        if (DBG) Log.d(TAG, "removeOnMetadataChangedListener(" + device + ", " + listener + ")");
        requireNonNull(device);
        requireNonNull(listener);