Get low battery level through metadata

Bug: 183469297
Test: make -j50 RunSettingsRoboTests ROBOTEST_FILTER=AdvancedBluetoothDetailsHeaderControllerTest
Change-Id: Iae8971a5c1f183fe141d4246e23d8feb6d68d0ff
This commit is contained in:
Tim Peng
2021-03-23 15:14:13 +08:00
parent 90df1c9de1
commit 7e6762de8a
2 changed files with 83 additions and 7 deletions

View File

@@ -192,6 +192,7 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont
updateSubLayout(mLayoutPreference.findViewById(R.id.layout_middle), updateSubLayout(mLayoutPreference.findViewById(R.id.layout_middle),
BluetoothDevice.METADATA_MAIN_ICON, BluetoothDevice.METADATA_MAIN_ICON,
BluetoothDevice.METADATA_MAIN_BATTERY, BluetoothDevice.METADATA_MAIN_BATTERY,
BluetoothDevice.METADATA_MAIN_LOW_BATTERY_THRESHOLD,
BluetoothDevice.METADATA_MAIN_CHARGING, BluetoothDevice.METADATA_MAIN_CHARGING,
/* titleResId */ 0, /* titleResId */ 0,
MAIN_DEVICE_ID); MAIN_DEVICE_ID);
@@ -202,6 +203,7 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont
updateSubLayout(mLayoutPreference.findViewById(R.id.layout_left), updateSubLayout(mLayoutPreference.findViewById(R.id.layout_left),
BluetoothDevice.METADATA_UNTETHERED_LEFT_ICON, BluetoothDevice.METADATA_UNTETHERED_LEFT_ICON,
BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY, BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY,
BluetoothDevice.METADATA_UNTETHERED_LEFT_LOW_BATTERY_THRESHOLD,
BluetoothDevice.METADATA_UNTETHERED_LEFT_CHARGING, BluetoothDevice.METADATA_UNTETHERED_LEFT_CHARGING,
R.string.bluetooth_left_name, R.string.bluetooth_left_name,
LEFT_DEVICE_ID); LEFT_DEVICE_ID);
@@ -209,6 +211,7 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont
updateSubLayout(mLayoutPreference.findViewById(R.id.layout_middle), updateSubLayout(mLayoutPreference.findViewById(R.id.layout_middle),
BluetoothDevice.METADATA_UNTETHERED_CASE_ICON, BluetoothDevice.METADATA_UNTETHERED_CASE_ICON,
BluetoothDevice.METADATA_UNTETHERED_CASE_BATTERY, BluetoothDevice.METADATA_UNTETHERED_CASE_BATTERY,
BluetoothDevice.METADATA_UNTETHERED_CASE_LOW_BATTERY_THRESHOLD,
BluetoothDevice.METADATA_UNTETHERED_CASE_CHARGING, BluetoothDevice.METADATA_UNTETHERED_CASE_CHARGING,
R.string.bluetooth_middle_name, R.string.bluetooth_middle_name,
CASE_DEVICE_ID); CASE_DEVICE_ID);
@@ -216,6 +219,7 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont
updateSubLayout(mLayoutPreference.findViewById(R.id.layout_right), updateSubLayout(mLayoutPreference.findViewById(R.id.layout_right),
BluetoothDevice.METADATA_UNTETHERED_RIGHT_ICON, BluetoothDevice.METADATA_UNTETHERED_RIGHT_ICON,
BluetoothDevice.METADATA_UNTETHERED_RIGHT_BATTERY, BluetoothDevice.METADATA_UNTETHERED_RIGHT_BATTERY,
BluetoothDevice.METADATA_UNTETHERED_RIGHT_LOW_BATTERY_THRESHOLD,
BluetoothDevice.METADATA_UNTETHERED_RIGHT_CHARGING, BluetoothDevice.METADATA_UNTETHERED_RIGHT_CHARGING,
R.string.bluetooth_right_name, R.string.bluetooth_right_name,
RIGHT_DEVICE_ID); RIGHT_DEVICE_ID);
@@ -243,7 +247,7 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont
} }
private void updateSubLayout(LinearLayout linearLayout, int iconMetaKey, int batteryMetaKey, private void updateSubLayout(LinearLayout linearLayout, int iconMetaKey, int batteryMetaKey,
int chargeMetaKey, int titleResId, int deviceId) { int lowBatteryMetaKey, int chargeMetaKey, int titleResId, int deviceId) {
if (linearLayout == null) { if (linearLayout == null) {
return; return;
} }
@@ -273,7 +277,15 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont
linearLayout.setVisibility(View.VISIBLE); linearLayout.setVisibility(View.VISIBLE);
batterySummaryView.setText(com.android.settings.Utils.formatPercentage(batteryLevel)); batterySummaryView.setText(com.android.settings.Utils.formatPercentage(batteryLevel));
batterySummaryView.setVisibility(View.VISIBLE); batterySummaryView.setVisibility(View.VISIBLE);
showBatteryIcon(linearLayout, batteryLevel, charging, batteryMetaKey); int lowBatteryLevel = BluetoothUtils.getIntMetaData(bluetoothDevice, lowBatteryMetaKey);
if (lowBatteryLevel == BluetoothUtils.META_INT_ERROR) {
if (batteryMetaKey == BluetoothDevice.METADATA_UNTETHERED_CASE_BATTERY) {
lowBatteryLevel = CASE_LOW_BATTERY_LEVEL;
} else {
lowBatteryLevel = LOW_BATTERY_LEVEL;
}
}
showBatteryIcon(linearLayout, batteryLevel, lowBatteryLevel, charging);
} else { } else {
if (deviceId == MAIN_DEVICE_ID) { if (deviceId == MAIN_DEVICE_ID) {
linearLayout.setVisibility(View.VISIBLE); linearLayout.setVisibility(View.VISIBLE);
@@ -354,11 +366,8 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont
}); });
} }
private void showBatteryIcon(LinearLayout linearLayout, int level, boolean charging, private void showBatteryIcon(LinearLayout linearLayout, int level, int lowBatteryLevel,
int batteryMetaKey) { boolean charging) {
final int lowBatteryLevel =
batteryMetaKey == BluetoothDevice.METADATA_UNTETHERED_CASE_BATTERY
? CASE_LOW_BATTERY_LEVEL : LOW_BATTERY_LEVEL;
final boolean enableLowBattery = level <= lowBatteryLevel && !charging; final boolean enableLowBattery = level <= lowBatteryLevel && !charging;
final ImageView imageView = linearLayout.findViewById(R.id.bt_battery_icon); final ImageView imageView = linearLayout.findViewById(R.id.bt_battery_icon);
if (enableLowBattery) { if (enableLowBattery) {

View File

@@ -62,6 +62,9 @@ public class AdvancedBluetoothDetailsHeaderControllerTest {
private static final int BATTERY_LEVEL_RIGHT = 45; private static final int BATTERY_LEVEL_RIGHT = 45;
private static final int LOW_BATTERY_LEVEL = 15; private static final int LOW_BATTERY_LEVEL = 15;
private static final int CASE_LOW_BATTERY_LEVEL = 19; private static final int CASE_LOW_BATTERY_LEVEL = 19;
private static final int LOW_BATTERY_LEVEL_THRESHOLD = 15;
private static final int BATTERY_LEVEL_5 = 5;
private static final int BATTERY_LEVEL_50 = 50;
private static final String ICON_URI = "content://test.provider/icon.png"; private static final String ICON_URI = "content://test.provider/icon.png";
private static final String MAC_ADDRESS = "04:52:C7:0B:D8:3C"; private static final String MAC_ADDRESS = "04:52:C7:0B:D8:3C";
@@ -201,6 +204,70 @@ public class AdvancedBluetoothDetailsHeaderControllerTest {
assertThat(layout.findViewById(R.id.header_icon).getVisibility()).isEqualTo(View.VISIBLE); assertThat(layout.findViewById(R.id.header_icon).getVisibility()).isEqualTo(View.VISIBLE);
} }
@Test
public void refresh_underLowBatteryThreshold_showAlertIcon() {
when(mBluetoothDevice.getMetadata(
BluetoothDevice.METADATA_DEVICE_TYPE)).thenReturn(
BluetoothDevice.DEVICE_TYPE_WATCH.getBytes());
when(mBluetoothDevice.getMetadata(
BluetoothDevice.METADATA_MAIN_BATTERY)).thenReturn(
String.valueOf(BATTERY_LEVEL_5).getBytes());
when(mBluetoothDevice.getMetadata(
BluetoothDevice.METADATA_MAIN_LOW_BATTERY_THRESHOLD)).thenReturn(
String.valueOf(LOW_BATTERY_LEVEL_THRESHOLD).getBytes());
when(mBluetoothDevice.getMetadata(
BluetoothDevice.METADATA_MAIN_CHARGING)).thenReturn(
String.valueOf(false).getBytes());
when(mCachedDevice.isConnected()).thenReturn(true);
mController.refresh();
assertBatteryIcon(mLayoutPreference.findViewById(R.id.layout_middle),
R.drawable.ic_battery_alert_24dp);
}
@Test
public void refresh_underLowBatteryThresholdInCharging_showAlertIcon() {
when(mBluetoothDevice.getMetadata(
BluetoothDevice.METADATA_DEVICE_TYPE)).thenReturn(
BluetoothDevice.DEVICE_TYPE_WATCH.getBytes());
when(mBluetoothDevice.getMetadata(
BluetoothDevice.METADATA_MAIN_BATTERY)).thenReturn(
String.valueOf(BATTERY_LEVEL_5).getBytes());
when(mBluetoothDevice.getMetadata(
BluetoothDevice.METADATA_MAIN_LOW_BATTERY_THRESHOLD)).thenReturn(
String.valueOf(LOW_BATTERY_LEVEL_THRESHOLD).getBytes());
when(mBluetoothDevice.getMetadata(
BluetoothDevice.METADATA_MAIN_CHARGING)).thenReturn(
String.valueOf(true).getBytes());
when(mCachedDevice.isConnected()).thenReturn(true);
mController.refresh();
assertBatteryIcon(mLayoutPreference.findViewById(R.id.layout_middle), /* resId= */-1);
}
@Test
public void refresh_aboveLowBatteryThreshold_noAlertIcon() {
when(mBluetoothDevice.getMetadata(
BluetoothDevice.METADATA_DEVICE_TYPE)).thenReturn(
BluetoothDevice.DEVICE_TYPE_WATCH.getBytes());
when(mBluetoothDevice.getMetadata(
BluetoothDevice.METADATA_MAIN_BATTERY)).thenReturn(
String.valueOf(BATTERY_LEVEL_50).getBytes());
when(mBluetoothDevice.getMetadata(
BluetoothDevice.METADATA_MAIN_LOW_BATTERY_THRESHOLD)).thenReturn(
String.valueOf(LOW_BATTERY_LEVEL_THRESHOLD).getBytes());
when(mBluetoothDevice.getMetadata(
BluetoothDevice.METADATA_MAIN_CHARGING)).thenReturn(
String.valueOf(false).getBytes());
when(mCachedDevice.isConnected()).thenReturn(true);
mController.refresh();
assertBatteryIcon(mLayoutPreference.findViewById(R.id.layout_middle), /* resId= */-1);
}
@Test @Test
public void refresh_withLowBatteryAndUncharged_showAlertIcon() { public void refresh_withLowBatteryAndUncharged_showAlertIcon() {
when(mBluetoothDevice.getMetadata( when(mBluetoothDevice.getMetadata(