Bluetooth: Display battery level of connected devices
* Add handler for BluetoothDevice.ACTION_BATTERY_LEVEL_CHANGED intent
* Check battery level information when UI is updated
* Show battery level in Quick Settings connected device summary line
* Show battery level in Bluetooth Settings connected device summary line
* Show battery level in Bluetooth device details page device summary
line
* Add unit test for CachedBluetoothDevice, change HeadsetProfile and
HidProfile to not final to enable mocking
Bug: 35874078
Test: make, unit test, connect to remote devices, connect/disconnect
profiles
Change-Id: I729048cace73aab29337a8002a2897d2acf22fa6
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java
index 9c01dac..4bb4b40 100644
--- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java
+++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java
@@ -419,6 +419,14 @@
}
}
+ /**
+ * Get battery level from remote device
+ * @return battery level in percentage [0-100], or {@link BluetoothDevice#BATTERY_LEVEL_UNKNOWN}
+ */
+ public int getBatteryLevel() {
+ return mDevice.getBatteryLevel();
+ }
+
void refresh() {
dispatchAttributesChanged();
}
@@ -837,7 +845,7 @@
/**
* @return resource for string that discribes the connection state of this device.
*/
- public int getConnectionSummary() {
+ public String getConnectionSummary() {
boolean profileConnected = false; // at least one profile is connected
boolean a2dpNotConnected = false; // A2DP is preferred but not connected
boolean hfpNotConnected = false; // HFP is preferred but not connected
@@ -848,7 +856,7 @@
switch (connectionStatus) {
case BluetoothProfile.STATE_CONNECTING:
case BluetoothProfile.STATE_DISCONNECTING:
- return Utils.getConnectionStateSummary(connectionStatus);
+ return mContext.getString(Utils.getConnectionStateSummary(connectionStatus));
case BluetoothProfile.STATE_CONNECTED:
profileConnected = true;
@@ -868,18 +876,54 @@
}
}
+ String batteryLevelPercentageString = null;
+ // Android framework should only set mBatteryLevel to valid range [0-100] or
+ // BluetoothDevice.BATTERY_LEVEL_UNKNOWN, any other value should be a framework bug.
+ // Thus assume here that if value is not BluetoothDevice.BATTERY_LEVEL_UNKNOWN, it must
+ // be valid
+ final int batteryLevel = getBatteryLevel();
+ if (batteryLevel != BluetoothDevice.BATTERY_LEVEL_UNKNOWN) {
+ // TODO: name com.android.settingslib.bluetooth.Utils something different
+ batteryLevelPercentageString =
+ com.android.settingslib.Utils.formatPercentage(batteryLevel);
+ }
+
if (profileConnected) {
if (a2dpNotConnected && hfpNotConnected) {
- return R.string.bluetooth_connected_no_headset_no_a2dp;
+ if (batteryLevelPercentageString != null) {
+ return mContext.getString(
+ R.string.bluetooth_connected_no_headset_no_a2dp_battery_level,
+ batteryLevelPercentageString);
+ } else {
+ return mContext.getString(R.string.bluetooth_connected_no_headset_no_a2dp);
+ }
+
} else if (a2dpNotConnected) {
- return R.string.bluetooth_connected_no_a2dp;
+ if (batteryLevelPercentageString != null) {
+ return mContext.getString(R.string.bluetooth_connected_no_a2dp_battery_level,
+ batteryLevelPercentageString);
+ } else {
+ return mContext.getString(R.string.bluetooth_connected_no_a2dp);
+ }
+
} else if (hfpNotConnected) {
- return R.string.bluetooth_connected_no_headset;
+ if (batteryLevelPercentageString != null) {
+ return mContext.getString(R.string.bluetooth_connected_no_headset_battery_level,
+ batteryLevelPercentageString);
+ } else {
+ return mContext.getString(R.string.bluetooth_connected_no_headset);
+ }
} else {
- return R.string.bluetooth_connected;
+ if (batteryLevelPercentageString != null) {
+ return mContext.getString(R.string.bluetooth_connected_battery_level,
+ batteryLevelPercentageString);
+ } else {
+ return mContext.getString(R.string.bluetooth_connected);
+ }
}
}
- return getBondState() == BluetoothDevice.BOND_BONDING ? R.string.bluetooth_pairing : 0;
+ return getBondState() == BluetoothDevice.BOND_BONDING ?
+ mContext.getString(R.string.bluetooth_pairing) : null;
}
}