From 7246a4f8421ebddea754d02b892acccdfcd50ddb Mon Sep 17 00:00:00 2001 From: Tim Peng Date: Wed, 31 Mar 2021 13:38:35 +0800 Subject: [PATCH] [Rohan]The battery level show up "-1%", not sync with Rohan. -Hide battery information when it is unknown Bug: 184017556 Test: make -j50 RunSettingsRoboTests ROBOTEST_FILTER=AdvancedBluetoothDetailsHeaderControllerTest Change-Id: I5f94ccae364f1420e04af21ba425c29ac6a11dd2 --- ...ancedBluetoothDetailsHeaderController.java | 11 +++++--- ...dBluetoothDetailsHeaderControllerTest.java | 27 +++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/com/android/settings/bluetooth/AdvancedBluetoothDetailsHeaderController.java b/src/com/android/settings/bluetooth/AdvancedBluetoothDetailsHeaderController.java index bc5aa470eba..4c30baa5b27 100644 --- a/src/com/android/settings/bluetooth/AdvancedBluetoothDetailsHeaderController.java +++ b/src/com/android/settings/bluetooth/AdvancedBluetoothDetailsHeaderController.java @@ -289,10 +289,15 @@ public class AdvancedBluetoothDetailsHeaderController extends BasePreferenceCont } else { if (deviceId == MAIN_DEVICE_ID) { linearLayout.setVisibility(View.VISIBLE); - batterySummaryView.setText(com.android.settings.Utils.formatPercentage( - bluetoothDevice.getBatteryLevel())); - batterySummaryView.setVisibility(View.VISIBLE); linearLayout.findViewById(R.id.bt_battery_icon).setVisibility(View.GONE); + int level = bluetoothDevice.getBatteryLevel(); + if (level != BluetoothDevice.BATTERY_LEVEL_UNKNOWN + && level != BluetoothDevice.BATTERY_LEVEL_BLUETOOTH_OFF) { + batterySummaryView.setText(com.android.settings.Utils.formatPercentage(level)); + batterySummaryView.setVisibility(View.VISIBLE); + } else { + batterySummaryView.setVisibility(View.GONE); + } } else { // Hide it if it doesn't have battery information linearLayout.setVisibility(View.GONE); diff --git a/tests/robotests/src/com/android/settings/bluetooth/AdvancedBluetoothDetailsHeaderControllerTest.java b/tests/robotests/src/com/android/settings/bluetooth/AdvancedBluetoothDetailsHeaderControllerTest.java index 8e03d681f33..215cddb43aa 100644 --- a/tests/robotests/src/com/android/settings/bluetooth/AdvancedBluetoothDetailsHeaderControllerTest.java +++ b/tests/robotests/src/com/android/settings/bluetooth/AdvancedBluetoothDetailsHeaderControllerTest.java @@ -41,6 +41,7 @@ import com.android.settings.core.SettingsUIDeviceConfig; import com.android.settings.fuelgauge.BatteryMeterView; import com.android.settings.testutils.shadow.ShadowDeviceConfig; import com.android.settings.testutils.shadow.ShadowEntityHeaderController; +import com.android.settingslib.bluetooth.BluetoothUtils; import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.utils.StringUtil; import com.android.settingslib.widget.LayoutPreference; @@ -135,6 +136,32 @@ public class AdvancedBluetoothDetailsHeaderControllerTest { assertBatteryLevel(mLayoutPreference.findViewById(R.id.layout_middle), BATTERY_LEVEL_MAIN); } + @Test + public void refresh_connectedWatch_unknownBatteryLevel_shouldNotShowBatteryLevel() { + when(mBluetoothDevice.getMetadata( + BluetoothDevice.METADATA_DEVICE_TYPE)).thenReturn( + BluetoothDevice.DEVICE_TYPE_WATCH.getBytes()); + when(mBluetoothDevice.getMetadata( + BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET)).thenReturn( + String.valueOf(false).getBytes()); + when(mBluetoothDevice.getMetadata( + BluetoothDevice.METADATA_MAIN_BATTERY)).thenReturn( + String.valueOf(BluetoothUtils.META_INT_ERROR).getBytes()); + when(mBluetoothDevice.getBatteryLevel()).thenReturn(BluetoothDevice.BATTERY_LEVEL_UNKNOWN); + when(mCachedDevice.isConnected()).thenReturn(true); + + mController.refresh(); + + assertThat(mLayoutPreference.findViewById(R.id.layout_left).getVisibility()).isEqualTo( + View.GONE); + assertThat(mLayoutPreference.findViewById(R.id.layout_right).getVisibility()).isEqualTo( + View.GONE); + assertThat(mLayoutPreference.findViewById(R.id.layout_middle).getVisibility()).isEqualTo( + View.VISIBLE); + assertThat(mLayoutPreference.findViewById(R.id.layout_middle) + .requireViewById(R.id.bt_battery_summary).getVisibility()).isEqualTo(View.GONE); + } + @Test public void refresh_connectedUntetheredHeadset_behaveAsExpected() { when(mBluetoothDevice.getMetadata(