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

Commit aff1400d authored by Yiyi Shen's avatar Yiyi Shen
Browse files

[Audiosharing] Add set temp bond metadata util method

Test: atest
Bug: 392004799
Flag: com.android.settingslib.flags.enable_temporary_bond_devices_ui
Change-Id: I0cf5062f24388249ca4d0e45d6ec3a1579527b27
parent 9eac9095
Loading
Loading
Loading
Loading
+34 −0
Original line number Diff line number Diff line
@@ -1169,4 +1169,38 @@ public class BluetoothUtils {
        String metadataValue = getFastPairCustomizedField(bluetoothDevice, TEMP_BOND_TYPE);
        return Objects.equals(metadataValue, TEMP_BOND_DEVICE_METADATA_VALUE);
    }

    /**
     * Set temp bond metadata to device
     *
     * @param device the BluetoothDevice to be marked as temp bond
     *
     * Note: It is a workaround since Bluetooth API is not ready.
     *       Avoid using this method if possible
     */
    public static void setTemporaryBondMetadata(@Nullable BluetoothDevice device) {
        if (device == null) return;
        if (!Flags.enableTemporaryBondDevicesUi()) {
            Log.d(TAG, "Skip setTemporaryBondMetadata, flag is disabled");
            return;
        }
        String fastPairCustomizedMeta = getStringMetaData(device,
                METADATA_FAST_PAIR_CUSTOMIZED_FIELDS);
        String fullContentWithTag = generateExpressionWithTag(TEMP_BOND_TYPE,
                TEMP_BOND_DEVICE_METADATA_VALUE);
        if (TextUtils.isEmpty(fastPairCustomizedMeta)) {
            fastPairCustomizedMeta = fullContentWithTag;
        } else {
            String oldValue = extraTagValue(TEMP_BOND_TYPE, fastPairCustomizedMeta);
            if (TextUtils.isEmpty(oldValue)) {
                fastPairCustomizedMeta += fullContentWithTag;
            } else {
                fastPairCustomizedMeta =
                        fastPairCustomizedMeta.replace(
                                generateExpressionWithTag(TEMP_BOND_TYPE, oldValue),
                                fullContentWithTag);
            }
        }
        device.setMetadata(METADATA_FAST_PAIR_CUSTOMIZED_FIELDS, fastPairCustomizedMeta.getBytes());
    }
}
+35 −0
Original line number Diff line number Diff line
@@ -22,9 +22,11 @@ import static com.android.settingslib.flags.Flags.FLAG_ENABLE_DETERMINING_ADVANC
import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -44,6 +46,8 @@ import android.media.AudioDeviceAttributes;
import android.media.AudioDeviceInfo;
import android.media.AudioManager;
import android.net.Uri;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;
import android.util.Pair;
@@ -109,6 +113,7 @@ public class BluetoothUtilsTest {
                    + "</HEARABLE_CONTROL_SLICE_WITH_WIDTH>";
    private static final String TEMP_BOND_METADATA =
            "<TEMP_BOND_TYPE>" + LE_AUDIO_SHARING_METADATA + "</TEMP_BOND_TYPE>";
    private static final String FAKE_TEMP_BOND_METADATA = "<TEMP_BOND_TYPE>fake</TEMP_BOND_TYPE>";
    private static final String TEST_EXCLUSIVE_MANAGER_PACKAGE = "com.test.manager";
    private static final String TEST_EXCLUSIVE_MANAGER_COMPONENT = "com.test.manager/.component";
    private static final int TEST_BROADCAST_ID = 25;
@@ -1348,4 +1353,34 @@ public class BluetoothUtilsTest {

        assertThat(BluetoothUtils.isTemporaryBondDevice(mBluetoothDevice)).isTrue();
    }

    @Test
    @DisableFlags(Flags.FLAG_ENABLE_TEMPORARY_BOND_DEVICES_UI)
    public void setTemporaryBondDevice_flagOff_doNothing() {
        when(mBluetoothDevice.getMetadata(METADATA_FAST_PAIR_CUSTOMIZED_FIELDS))
                .thenReturn(new byte[]{});
        BluetoothUtils.setTemporaryBondMetadata(mBluetoothDevice);
        verify(mBluetoothDevice, never()).setMetadata(eq(METADATA_FAST_PAIR_CUSTOMIZED_FIELDS),
                any());
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_TEMPORARY_BOND_DEVICES_UI)
    public void setTemporaryBondDevice_flagOn_setCorrectValue() {
        when(mBluetoothDevice.getMetadata(METADATA_FAST_PAIR_CUSTOMIZED_FIELDS))
                .thenReturn(new byte[]{});
        BluetoothUtils.setTemporaryBondMetadata(mBluetoothDevice);
        verify(mBluetoothDevice).setMetadata(METADATA_FAST_PAIR_CUSTOMIZED_FIELDS,
                TEMP_BOND_METADATA.getBytes());
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_TEMPORARY_BOND_DEVICES_UI)
    public void setTemporaryBondDevice_flagOff_replaceAndSetCorrectValue() {
        when(mBluetoothDevice.getMetadata(METADATA_FAST_PAIR_CUSTOMIZED_FIELDS))
                .thenReturn(FAKE_TEMP_BOND_METADATA.getBytes());
        BluetoothUtils.setTemporaryBondMetadata(mBluetoothDevice);
        verify(mBluetoothDevice).setMetadata(METADATA_FAST_PAIR_CUSTOMIZED_FIELDS,
                TEMP_BOND_METADATA.getBytes());
    }
}