From 7bcd8afcce33d74c0a04124bad7bbb0663ba45af Mon Sep 17 00:00:00 2001 From: Alan Huang Date: Thu, 12 May 2022 08:42:41 +0000 Subject: [PATCH] Add API in SettingsLib/BluetoothUtils to get the control Uri from Bluetooth metadata Bug: 229048602 Test: make -j64 RunSettingsLibRoboTests Change-Id: I97dec95843eb75e4033dd0ff6ae0b45c638d563f --- .../settingslib/bluetooth/BluetoothUtils.java | 40 +++++++++++++++++++ .../bluetooth/BluetoothUtilsTest.java | 14 +++++++ 2 files changed, 54 insertions(+) diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothUtils.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothUtils.java index c9af4d53f9a83..fea7475fc0871 100644 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothUtils.java +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothUtils.java @@ -30,6 +30,9 @@ import com.android.settingslib.widget.AdaptiveOutlineDrawable; import java.io.IOException; import java.util.List; +import java.util.Locale; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class BluetoothUtils { private static final String TAG = "BluetoothUtils"; @@ -39,6 +42,8 @@ public class BluetoothUtils { public static final int META_INT_ERROR = -1; public static final String BT_ADVANCED_HEADER_ENABLED = "bt_advanced_header_enabled"; + private static final int METADATA_FAST_PAIR_CUSTOMIZED_FIELDS = 25; + private static final String KEY_HEARABLE_CONTROL_SLICE = "HEARABLE_CONTROL_SLICE_WITH_WIDTH"; private static ErrorListener sErrorListener; @@ -384,8 +389,43 @@ public class BluetoothUtils { return Uri.parse(data); } + /** + * Get URI Bluetooth metadata for extra control + * + * @param bluetoothDevice the BluetoothDevice to get metadata + * @return the URI metadata + */ + public static String getControlUriMetaData(BluetoothDevice bluetoothDevice) { + String data = getStringMetaData(bluetoothDevice, METADATA_FAST_PAIR_CUSTOMIZED_FIELDS); + return extraTagValue(KEY_HEARABLE_CONTROL_SLICE, data); + } + @SuppressLint("NewApi") // Hidden API made public private static boolean doesClassMatch(BluetoothClass btClass, int classId) { return btClass.doesClassMatch(classId); } + + private static String extraTagValue(String tag, String metaData) { + if (TextUtils.isEmpty(metaData)) { + return null; + } + Pattern pattern = Pattern.compile(generateExpressionWithTag(tag, "(.*?)")); + Matcher matcher = pattern.matcher(metaData); + if (matcher.find()) { + return matcher.group(1); + } + return null; + } + + private static String getTagStart(String tag) { + return String.format(Locale.ENGLISH, "<%s>", tag); + } + + private static String getTagEnd(String tag) { + return String.format(Locale.ENGLISH, "", tag); + } + + private static String generateExpressionWithTag(String tag, String value) { + return getTagStart(tag) + value + getTagEnd(tag); + } } diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothUtilsTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothUtilsTest.java index 2e855145e1529..1c0ea1a1f4b01 100644 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothUtilsTest.java +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothUtilsTest.java @@ -51,6 +51,11 @@ public class BluetoothUtilsTest { private static final String STRING_METADATA = "string_metadata"; private static final String BOOL_METADATA = "true"; private static final String INT_METADATA = "25"; + private static final int METADATA_FAST_PAIR_CUSTOMIZED_FIELDS = 25; + private static final String KEY_HEARABLE_CONTROL_SLICE = "HEARABLE_CONTROL_SLICE_WITH_WIDTH"; + private static final String CONTROL_METADATA = + "" + STRING_METADATA + + ""; @Before public void setUp() { @@ -151,6 +156,15 @@ public class BluetoothUtilsTest { BluetoothDevice.METADATA_MAIN_ICON)).isNull(); } + @Test + public void getControlUriMetaData_hasMetaData_returnsCorrectMetaData() { + when(mBluetoothDevice.getMetadata(METADATA_FAST_PAIR_CUSTOMIZED_FIELDS)).thenReturn( + CONTROL_METADATA.getBytes()); + + assertThat(BluetoothUtils.getControlUriMetaData(mBluetoothDevice)).isEqualTo( + STRING_METADATA); + } + @Test public void isAdvancedDetailsHeader_untetheredHeadset_returnTrue() { when(mBluetoothDevice.getMetadata(