Merge "Add API in SettingsLib/BluetoothUtils to get the control Uri from Bluetooth metadata" into tm-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
814f53c5e1
@@ -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, "</%s>", tag);
|
||||
}
|
||||
|
||||
private static String generateExpressionWithTag(String tag, String value) {
|
||||
return getTagStart(tag) + value + getTagEnd(tag);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 =
|
||||
"<HEARABLE_CONTROL_SLICE_WITH_WIDTH>" + STRING_METADATA
|
||||
+ "</HEARABLE_CONTROL_SLICE_WITH_WIDTH>";
|
||||
|
||||
@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(
|
||||
|
||||
Reference in New Issue
Block a user