Loading packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothUtils.java +79 −8 Original line number Diff line number Diff line Loading @@ -28,6 +28,8 @@ public class BluetoothUtils { public static final boolean V = false; // verbose logging public static final boolean D = true; // regular logging public static final int META_INT_ERROR = -1; private static ErrorListener sErrorListener; public static int getConnectionStateSummary(int connectionState) { Loading Loading @@ -133,20 +135,16 @@ public class BluetoothUtils { final Pair<Drawable, String> pair = BluetoothUtils.getBtClassDrawableWithDescription( context, cachedDevice); final BluetoothDevice bluetoothDevice = cachedDevice.getDevice(); final boolean untetheredHeadset = bluetoothDevice != null ? Boolean.parseBoolean(bluetoothDevice.getMetadata( BluetoothDevice.METADATA_IS_UNTHETHERED_HEADSET)) : false; final boolean untetheredHeadset = getBooleanMetaData( bluetoothDevice, BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET); final int iconSize = context.getResources().getDimensionPixelSize( R.dimen.bt_nearby_icon_size); final Resources resources = context.getResources(); // Deal with untethered headset if (untetheredHeadset) { final String uriString = bluetoothDevice != null ? bluetoothDevice.getMetadata(BluetoothDevice.METADATA_MAIN_ICON) : null; final Uri iconUri = uriString != null ? Uri.parse(uriString) : null; final Uri iconUri = getUriMetaData(bluetoothDevice, BluetoothDevice.METADATA_MAIN_ICON); if (iconUri != null) { try { context.getContentResolver().takePersistableUriPermission(iconUri, Loading Loading @@ -194,4 +192,77 @@ public class BluetoothUtils { return adaptiveIcon; } /** * Get boolean Bluetooth metadata * * @param bluetoothDevice the BluetoothDevice to get metadata * @param key key value within the list of BluetoothDevice.METADATA_* * @return the boolean metdata */ public static boolean getBooleanMetaData(BluetoothDevice bluetoothDevice, int key) { if (bluetoothDevice == null) { return false; } final byte[] data = bluetoothDevice.getMetadata(key); if (data == null) { return false; } return Boolean.parseBoolean(new String(data)); } /** * Get String Bluetooth metadata * * @param bluetoothDevice the BluetoothDevice to get metadata * @param key key value within the list of BluetoothDevice.METADATA_* * @return the String metdata */ public static String getStringMetaData(BluetoothDevice bluetoothDevice, int key) { if (bluetoothDevice == null) { return null; } final byte[] data = bluetoothDevice.getMetadata(key); if (data == null) { return null; } return new String(data); } /** * Get integer Bluetooth metadata * * @param bluetoothDevice the BluetoothDevice to get metadata * @param key key value within the list of BluetoothDevice.METADATA_* * @return the int metdata */ public static int getIntMetaData(BluetoothDevice bluetoothDevice, int key) { if (bluetoothDevice == null) { return META_INT_ERROR; } final byte[] data = bluetoothDevice.getMetadata(key); if (data == null) { return META_INT_ERROR; } try { return Integer.parseInt(new String(data)); } catch (NumberFormatException e) { return META_INT_ERROR; } } /** * Get URI Bluetooth metadata * * @param bluetoothDevice the BluetoothDevice to get metadata * @param key key value within the list of BluetoothDevice.METADATA_* * @return the URI metdata */ public static Uri getUriMetaData(BluetoothDevice bluetoothDevice, int key) { String data = getStringMetaData(bluetoothDevice, key); if (data == null) { return null; } return Uri.parse(data); } } packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java +6 −10 Original line number Diff line number Diff line Loading @@ -881,16 +881,12 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> //when profile is connected, information would be available if (profileConnected) { // Update Meta data for connected device if (Boolean.parseBoolean( mDevice.getMetadata(BluetoothDevice.METADATA_IS_UNTHETHERED_HEADSET))) { try { leftBattery = Integer.parseInt( mDevice.getMetadata(BluetoothDevice.METADATA_UNTHETHERED_LEFT_BATTERY)); rightBattery = Integer.parseInt(mDevice.getMetadata( BluetoothDevice.METADATA_UNTHETHERED_RIGHT_BATTERY)); } catch (NumberFormatException e) { Log.d(TAG, "Parse error for unthethered battery level."); } if (BluetoothUtils.getBooleanMetaData( mDevice, BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET)) { leftBattery = BluetoothUtils.getIntMetaData(mDevice, BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY); rightBattery = BluetoothUtils.getIntMetaData(mDevice, BluetoothDevice.METADATA_UNTETHERED_RIGHT_BATTERY); } // Set default string with battery level in device connected situation. Loading packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothUtilsTest.java +66 −2 Original line number Diff line number Diff line Loading @@ -25,6 +25,7 @@ import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothDevice; import android.content.Context; import android.graphics.drawable.Drawable; import android.net.Uri; import android.util.Pair; import com.android.settingslib.widget.AdaptiveIcon; Loading @@ -47,6 +48,9 @@ public class BluetoothUtilsTest { private BluetoothDevice mBluetoothDevice; private Context mContext; private static final String STRING_METADATA = "string_metadata"; private static final String BOOL_METADATA = "true"; private static final String INT_METADATA = "25"; @Before public void setUp() { Loading Loading @@ -78,7 +82,7 @@ public class BluetoothUtilsTest { @Test public void getBtRainbowDrawableWithDescription_normalHeadset_returnAdaptiveIcon() { when(mBluetoothDevice.getMetadata( BluetoothDevice.METADATA_IS_UNTHETHERED_HEADSET)).thenReturn("false"); BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET)).thenReturn("false".getBytes()); when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice); when(mCachedBluetoothDevice.getAddress()).thenReturn("1f:aa:bb"); Loading @@ -86,4 +90,64 @@ public class BluetoothUtilsTest { RuntimeEnvironment.application, mCachedBluetoothDevice).first).isInstanceOf(AdaptiveIcon.class); } @Test public void getStringMetaData_hasMetaData_getCorrectMetaData() { when(mBluetoothDevice.getMetadata( BluetoothDevice.METADATA_UNTETHERED_LEFT_ICON)).thenReturn( STRING_METADATA.getBytes()); assertThat(BluetoothUtils.getStringMetaData(mBluetoothDevice, BluetoothDevice.METADATA_UNTETHERED_LEFT_ICON)).isEqualTo(STRING_METADATA); } @Test public void getIntMetaData_hasMetaData_getCorrectMetaData() { when(mBluetoothDevice.getMetadata( BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY)).thenReturn( INT_METADATA.getBytes()); assertThat(BluetoothUtils.getIntMetaData(mBluetoothDevice, BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY)) .isEqualTo(Integer.parseInt(INT_METADATA)); } @Test public void getIntMetaData_invalidMetaData_getErrorCode() { when(mBluetoothDevice.getMetadata( BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY)).thenReturn(null); assertThat(BluetoothUtils.getIntMetaData(mBluetoothDevice, BluetoothDevice.METADATA_UNTETHERED_LEFT_ICON)) .isEqualTo(BluetoothUtils.META_INT_ERROR); } @Test public void getBooleanMetaData_hasMetaData_getCorrectMetaData() { when(mBluetoothDevice.getMetadata( BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET)).thenReturn( BOOL_METADATA.getBytes()); assertThat(BluetoothUtils.getBooleanMetaData(mBluetoothDevice, BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET)).isEqualTo(true); } @Test public void getUriMetaData_hasMetaData_getCorrectMetaData() { when(mBluetoothDevice.getMetadata( BluetoothDevice.METADATA_MAIN_ICON)).thenReturn( STRING_METADATA.getBytes()); assertThat(BluetoothUtils.getUriMetaData(mBluetoothDevice, BluetoothDevice.METADATA_MAIN_ICON)).isEqualTo(Uri.parse(STRING_METADATA)); } @Test public void getUriMetaData_nullMetaData_getNullUri() { when(mBluetoothDevice.getMetadata( BluetoothDevice.METADATA_MAIN_ICON)).thenReturn(null); assertThat(BluetoothUtils.getUriMetaData(mBluetoothDevice, BluetoothDevice.METADATA_MAIN_ICON)).isNull(); } } packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java +12 −12 Original line number Diff line number Diff line Loading @@ -455,12 +455,12 @@ public class CachedBluetoothDeviceTest { updateProfileStatus(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED); when(mDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEARING_AID); when(mDevice.getMetadata(BluetoothDevice.METADATA_IS_UNTHETHERED_HEADSET)).thenReturn( "true"); when(mDevice.getMetadata(BluetoothDevice.METADATA_UNTHETHERED_LEFT_BATTERY)).thenReturn( TWS_BATTERY_LEFT); when(mDevice.getMetadata(BluetoothDevice.METADATA_UNTHETHERED_RIGHT_BATTERY)).thenReturn( TWS_BATTERY_RIGHT); when(mDevice.getMetadata(BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET)).thenReturn( "true".getBytes()); when(mDevice.getMetadata(BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY)).thenReturn( TWS_BATTERY_LEFT.getBytes()); when(mDevice.getMetadata(BluetoothDevice.METADATA_UNTETHERED_RIGHT_BATTERY)).thenReturn( TWS_BATTERY_RIGHT.getBytes()); assertThat(mCachedDevice.getConnectionSummary()).isEqualTo( "Active, L: 15% battery, R: 25% battery"); Loading @@ -472,12 +472,12 @@ public class CachedBluetoothDeviceTest { updateProfileStatus(mHfpProfile, BluetoothProfile.STATE_CONNECTED); updateProfileStatus(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED); when(mDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); when(mDevice.getMetadata(BluetoothDevice.METADATA_IS_UNTHETHERED_HEADSET)).thenReturn( "true"); when(mDevice.getMetadata(BluetoothDevice.METADATA_UNTHETHERED_LEFT_BATTERY)).thenReturn( TWS_BATTERY_LEFT); when(mDevice.getMetadata(BluetoothDevice.METADATA_UNTHETHERED_RIGHT_BATTERY)).thenReturn( TWS_BATTERY_RIGHT); when(mDevice.getMetadata(BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET)).thenReturn( "true".getBytes()); when(mDevice.getMetadata(BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY)).thenReturn( TWS_BATTERY_LEFT.getBytes()); when(mDevice.getMetadata(BluetoothDevice.METADATA_UNTETHERED_RIGHT_BATTERY)).thenReturn( TWS_BATTERY_RIGHT.getBytes()); assertThat(mCachedDevice.getConnectionSummary()).isEqualTo( "L: 15% battery, R: 25% battery"); Loading Loading
packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothUtils.java +79 −8 Original line number Diff line number Diff line Loading @@ -28,6 +28,8 @@ public class BluetoothUtils { public static final boolean V = false; // verbose logging public static final boolean D = true; // regular logging public static final int META_INT_ERROR = -1; private static ErrorListener sErrorListener; public static int getConnectionStateSummary(int connectionState) { Loading Loading @@ -133,20 +135,16 @@ public class BluetoothUtils { final Pair<Drawable, String> pair = BluetoothUtils.getBtClassDrawableWithDescription( context, cachedDevice); final BluetoothDevice bluetoothDevice = cachedDevice.getDevice(); final boolean untetheredHeadset = bluetoothDevice != null ? Boolean.parseBoolean(bluetoothDevice.getMetadata( BluetoothDevice.METADATA_IS_UNTHETHERED_HEADSET)) : false; final boolean untetheredHeadset = getBooleanMetaData( bluetoothDevice, BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET); final int iconSize = context.getResources().getDimensionPixelSize( R.dimen.bt_nearby_icon_size); final Resources resources = context.getResources(); // Deal with untethered headset if (untetheredHeadset) { final String uriString = bluetoothDevice != null ? bluetoothDevice.getMetadata(BluetoothDevice.METADATA_MAIN_ICON) : null; final Uri iconUri = uriString != null ? Uri.parse(uriString) : null; final Uri iconUri = getUriMetaData(bluetoothDevice, BluetoothDevice.METADATA_MAIN_ICON); if (iconUri != null) { try { context.getContentResolver().takePersistableUriPermission(iconUri, Loading Loading @@ -194,4 +192,77 @@ public class BluetoothUtils { return adaptiveIcon; } /** * Get boolean Bluetooth metadata * * @param bluetoothDevice the BluetoothDevice to get metadata * @param key key value within the list of BluetoothDevice.METADATA_* * @return the boolean metdata */ public static boolean getBooleanMetaData(BluetoothDevice bluetoothDevice, int key) { if (bluetoothDevice == null) { return false; } final byte[] data = bluetoothDevice.getMetadata(key); if (data == null) { return false; } return Boolean.parseBoolean(new String(data)); } /** * Get String Bluetooth metadata * * @param bluetoothDevice the BluetoothDevice to get metadata * @param key key value within the list of BluetoothDevice.METADATA_* * @return the String metdata */ public static String getStringMetaData(BluetoothDevice bluetoothDevice, int key) { if (bluetoothDevice == null) { return null; } final byte[] data = bluetoothDevice.getMetadata(key); if (data == null) { return null; } return new String(data); } /** * Get integer Bluetooth metadata * * @param bluetoothDevice the BluetoothDevice to get metadata * @param key key value within the list of BluetoothDevice.METADATA_* * @return the int metdata */ public static int getIntMetaData(BluetoothDevice bluetoothDevice, int key) { if (bluetoothDevice == null) { return META_INT_ERROR; } final byte[] data = bluetoothDevice.getMetadata(key); if (data == null) { return META_INT_ERROR; } try { return Integer.parseInt(new String(data)); } catch (NumberFormatException e) { return META_INT_ERROR; } } /** * Get URI Bluetooth metadata * * @param bluetoothDevice the BluetoothDevice to get metadata * @param key key value within the list of BluetoothDevice.METADATA_* * @return the URI metdata */ public static Uri getUriMetaData(BluetoothDevice bluetoothDevice, int key) { String data = getStringMetaData(bluetoothDevice, key); if (data == null) { return null; } return Uri.parse(data); } }
packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java +6 −10 Original line number Diff line number Diff line Loading @@ -881,16 +881,12 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> //when profile is connected, information would be available if (profileConnected) { // Update Meta data for connected device if (Boolean.parseBoolean( mDevice.getMetadata(BluetoothDevice.METADATA_IS_UNTHETHERED_HEADSET))) { try { leftBattery = Integer.parseInt( mDevice.getMetadata(BluetoothDevice.METADATA_UNTHETHERED_LEFT_BATTERY)); rightBattery = Integer.parseInt(mDevice.getMetadata( BluetoothDevice.METADATA_UNTHETHERED_RIGHT_BATTERY)); } catch (NumberFormatException e) { Log.d(TAG, "Parse error for unthethered battery level."); } if (BluetoothUtils.getBooleanMetaData( mDevice, BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET)) { leftBattery = BluetoothUtils.getIntMetaData(mDevice, BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY); rightBattery = BluetoothUtils.getIntMetaData(mDevice, BluetoothDevice.METADATA_UNTETHERED_RIGHT_BATTERY); } // Set default string with battery level in device connected situation. Loading
packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothUtilsTest.java +66 −2 Original line number Diff line number Diff line Loading @@ -25,6 +25,7 @@ import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothDevice; import android.content.Context; import android.graphics.drawable.Drawable; import android.net.Uri; import android.util.Pair; import com.android.settingslib.widget.AdaptiveIcon; Loading @@ -47,6 +48,9 @@ public class BluetoothUtilsTest { private BluetoothDevice mBluetoothDevice; private Context mContext; private static final String STRING_METADATA = "string_metadata"; private static final String BOOL_METADATA = "true"; private static final String INT_METADATA = "25"; @Before public void setUp() { Loading Loading @@ -78,7 +82,7 @@ public class BluetoothUtilsTest { @Test public void getBtRainbowDrawableWithDescription_normalHeadset_returnAdaptiveIcon() { when(mBluetoothDevice.getMetadata( BluetoothDevice.METADATA_IS_UNTHETHERED_HEADSET)).thenReturn("false"); BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET)).thenReturn("false".getBytes()); when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice); when(mCachedBluetoothDevice.getAddress()).thenReturn("1f:aa:bb"); Loading @@ -86,4 +90,64 @@ public class BluetoothUtilsTest { RuntimeEnvironment.application, mCachedBluetoothDevice).first).isInstanceOf(AdaptiveIcon.class); } @Test public void getStringMetaData_hasMetaData_getCorrectMetaData() { when(mBluetoothDevice.getMetadata( BluetoothDevice.METADATA_UNTETHERED_LEFT_ICON)).thenReturn( STRING_METADATA.getBytes()); assertThat(BluetoothUtils.getStringMetaData(mBluetoothDevice, BluetoothDevice.METADATA_UNTETHERED_LEFT_ICON)).isEqualTo(STRING_METADATA); } @Test public void getIntMetaData_hasMetaData_getCorrectMetaData() { when(mBluetoothDevice.getMetadata( BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY)).thenReturn( INT_METADATA.getBytes()); assertThat(BluetoothUtils.getIntMetaData(mBluetoothDevice, BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY)) .isEqualTo(Integer.parseInt(INT_METADATA)); } @Test public void getIntMetaData_invalidMetaData_getErrorCode() { when(mBluetoothDevice.getMetadata( BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY)).thenReturn(null); assertThat(BluetoothUtils.getIntMetaData(mBluetoothDevice, BluetoothDevice.METADATA_UNTETHERED_LEFT_ICON)) .isEqualTo(BluetoothUtils.META_INT_ERROR); } @Test public void getBooleanMetaData_hasMetaData_getCorrectMetaData() { when(mBluetoothDevice.getMetadata( BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET)).thenReturn( BOOL_METADATA.getBytes()); assertThat(BluetoothUtils.getBooleanMetaData(mBluetoothDevice, BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET)).isEqualTo(true); } @Test public void getUriMetaData_hasMetaData_getCorrectMetaData() { when(mBluetoothDevice.getMetadata( BluetoothDevice.METADATA_MAIN_ICON)).thenReturn( STRING_METADATA.getBytes()); assertThat(BluetoothUtils.getUriMetaData(mBluetoothDevice, BluetoothDevice.METADATA_MAIN_ICON)).isEqualTo(Uri.parse(STRING_METADATA)); } @Test public void getUriMetaData_nullMetaData_getNullUri() { when(mBluetoothDevice.getMetadata( BluetoothDevice.METADATA_MAIN_ICON)).thenReturn(null); assertThat(BluetoothUtils.getUriMetaData(mBluetoothDevice, BluetoothDevice.METADATA_MAIN_ICON)).isNull(); } }
packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceTest.java +12 −12 Original line number Diff line number Diff line Loading @@ -455,12 +455,12 @@ public class CachedBluetoothDeviceTest { updateProfileStatus(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED); when(mDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); mCachedDevice.onActiveDeviceChanged(true, BluetoothProfile.HEARING_AID); when(mDevice.getMetadata(BluetoothDevice.METADATA_IS_UNTHETHERED_HEADSET)).thenReturn( "true"); when(mDevice.getMetadata(BluetoothDevice.METADATA_UNTHETHERED_LEFT_BATTERY)).thenReturn( TWS_BATTERY_LEFT); when(mDevice.getMetadata(BluetoothDevice.METADATA_UNTHETHERED_RIGHT_BATTERY)).thenReturn( TWS_BATTERY_RIGHT); when(mDevice.getMetadata(BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET)).thenReturn( "true".getBytes()); when(mDevice.getMetadata(BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY)).thenReturn( TWS_BATTERY_LEFT.getBytes()); when(mDevice.getMetadata(BluetoothDevice.METADATA_UNTETHERED_RIGHT_BATTERY)).thenReturn( TWS_BATTERY_RIGHT.getBytes()); assertThat(mCachedDevice.getConnectionSummary()).isEqualTo( "Active, L: 15% battery, R: 25% battery"); Loading @@ -472,12 +472,12 @@ public class CachedBluetoothDeviceTest { updateProfileStatus(mHfpProfile, BluetoothProfile.STATE_CONNECTED); updateProfileStatus(mHearingAidProfile, BluetoothProfile.STATE_CONNECTED); when(mDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); when(mDevice.getMetadata(BluetoothDevice.METADATA_IS_UNTHETHERED_HEADSET)).thenReturn( "true"); when(mDevice.getMetadata(BluetoothDevice.METADATA_UNTHETHERED_LEFT_BATTERY)).thenReturn( TWS_BATTERY_LEFT); when(mDevice.getMetadata(BluetoothDevice.METADATA_UNTHETHERED_RIGHT_BATTERY)).thenReturn( TWS_BATTERY_RIGHT); when(mDevice.getMetadata(BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET)).thenReturn( "true".getBytes()); when(mDevice.getMetadata(BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY)).thenReturn( TWS_BATTERY_LEFT.getBytes()); when(mDevice.getMetadata(BluetoothDevice.METADATA_UNTETHERED_RIGHT_BATTERY)).thenReturn( TWS_BATTERY_RIGHT.getBytes()); assertThat(mCachedDevice.getConnectionSummary()).isEqualTo( "L: 15% battery, R: 25% battery"); Loading