Show prediction time when both value are ready

Bug: 215767460
Test: make -j64 RunSettingsRoboTests
Change-Id: I07947b3eca1f656e0dc603f9b9839825dd3149fd
diff --git a/src/com/android/settings/bluetooth/AdvancedBluetoothDetailsHeaderController.java b/src/com/android/settings/bluetooth/AdvancedBluetoothDetailsHeaderController.java
index b60f1b6..1c12c6a 100644
--- a/src/com/android/settings/bluetooth/AdvancedBluetoothDetailsHeaderController.java
+++ b/src/com/android/settings/bluetooth/AdvancedBluetoothDetailsHeaderController.java
@@ -99,6 +99,10 @@
     @VisibleForTesting
     boolean mIsRegisterCallback = false;
     @VisibleForTesting
+    boolean mIsLeftDeviceEstimateReady;
+    @VisibleForTesting
+    boolean mIsRightDeviceEstimateReady;
+    @VisibleForTesting
     final BluetoothAdapter.OnMetadataChangedListener mMetadataListener =
             new BluetoothAdapter.OnMetadataChangedListener() {
                 @Override
@@ -226,6 +230,8 @@
                         BluetoothDevice.METADATA_UNTETHERED_RIGHT_CHARGING,
                         R.string.bluetooth_right_name,
                         RIGHT_DEVICE_ID);
+
+                showBothDevicesBatteryPredictionIfNecessary();
             }
         }
     }
@@ -365,8 +371,13 @@
                                 + ", ESTIMATE_READY : " + estimateReady
                                 + ", BATTERY_ESTIMATE : " + batteryEstimate);
                     }
-                    showBatteryPredictionIfNecessary(estimateReady, batteryEstimate,
-                            linearLayout);
+
+                    showBatteryPredictionIfNecessary(estimateReady, batteryEstimate, linearLayout);
+                    if (batteryId == LEFT_DEVICE_ID) {
+                        mIsLeftDeviceEstimateReady = estimateReady == 1;
+                    } else if (batteryId == RIGHT_DEVICE_ID) {
+                        mIsRightDeviceEstimateReady = estimateReady == 1;
+                    }
                 }
             } finally {
                 cursor.close();
@@ -380,7 +391,6 @@
         ThreadUtils.postOnMainThread(() -> {
             final TextView textView = linearLayout.findViewById(R.id.bt_battery_prediction);
             if (estimateReady == 1) {
-                textView.setVisibility(View.VISIBLE);
                 textView.setText(
                         StringUtil.formatElapsedTime(
                                 mContext,
@@ -393,6 +403,24 @@
         });
     }
 
+    @VisibleForTesting
+    void showBothDevicesBatteryPredictionIfNecessary() {
+        TextView leftDeviceTextView =
+                mLayoutPreference.findViewById(R.id.layout_left)
+                        .findViewById(R.id.bt_battery_prediction);
+        TextView rightDeviceTextView =
+                mLayoutPreference.findViewById(R.id.layout_right)
+                        .findViewById(R.id.bt_battery_prediction);
+
+        boolean isBothDevicesEstimateReady =
+                mIsLeftDeviceEstimateReady && mIsRightDeviceEstimateReady;
+        int visibility = isBothDevicesEstimateReady ? View.VISIBLE : View.GONE;
+        ThreadUtils.postOnMainThread(() -> {
+            leftDeviceTextView.setVisibility(visibility);
+            rightDeviceTextView.setVisibility(visibility);
+        });
+    }
+
     private void showBatteryIcon(LinearLayout linearLayout, int level, int lowBatteryLevel,
             boolean charging) {
         final boolean enableLowBattery = level <= lowBatteryLevel && !charging;
diff --git a/tests/robotests/src/com/android/settings/bluetooth/AdvancedBluetoothDetailsHeaderControllerTest.java b/tests/robotests/src/com/android/settings/bluetooth/AdvancedBluetoothDetailsHeaderControllerTest.java
index 6087ef2..51cad70 100644
--- a/tests/robotests/src/com/android/settings/bluetooth/AdvancedBluetoothDetailsHeaderControllerTest.java
+++ b/tests/robotests/src/com/android/settings/bluetooth/AdvancedBluetoothDetailsHeaderControllerTest.java
@@ -387,34 +387,52 @@
     }
 
     @Test
-    public void showBatteryPredictionIfNecessary_estimateReadyIsAvailable_showView() {
-        mController.showBatteryPredictionIfNecessary(1, 14218009,
-                mLayoutPreference.findViewById(R.id.layout_left));
-        mController.showBatteryPredictionIfNecessary(1, 14218009,
-                mLayoutPreference.findViewById(R.id.layout_middle));
-        mController.showBatteryPredictionIfNecessary(1, 14218009,
-                mLayoutPreference.findViewById(R.id.layout_right));
+    public void estimateReadyIsBothAvailable_showsView() {
+        mController.mIsLeftDeviceEstimateReady = true;
+        mController.mIsRightDeviceEstimateReady = true;
+
+        mController.showBothDevicesBatteryPredictionIfNecessary();
 
         assertBatteryPredictionVisible(mLayoutPreference.findViewById(R.id.layout_left),
                 View.VISIBLE);
-        assertBatteryPredictionVisible(mLayoutPreference.findViewById(R.id.layout_middle),
-                View.VISIBLE);
         assertBatteryPredictionVisible(mLayoutPreference.findViewById(R.id.layout_right),
                 View.VISIBLE);
     }
 
     @Test
-    public void showBatteryPredictionIfNecessary_estimateReadyIsNotAvailable_notShowView() {
-        mController.showBatteryPredictionIfNecessary(0, 14218009,
-                mLayoutPreference.findViewById(R.id.layout_left));
-        mController.showBatteryPredictionIfNecessary(0, 14218009,
-                mLayoutPreference.findViewById(R.id.layout_middle));
-        mController.showBatteryPredictionIfNecessary(0, 14218009,
-                mLayoutPreference.findViewById(R.id.layout_right));
+    public void leftDeviceEstimateIsReadyRightDeviceIsNotReady_notShowView() {
+        mController.mIsLeftDeviceEstimateReady = true;
+        mController.mIsRightDeviceEstimateReady = false;
+
+        mController.showBothDevicesBatteryPredictionIfNecessary();
 
         assertBatteryPredictionVisible(mLayoutPreference.findViewById(R.id.layout_left),
                 View.GONE);
-        assertBatteryPredictionVisible(mLayoutPreference.findViewById(R.id.layout_middle),
+        assertBatteryPredictionVisible(mLayoutPreference.findViewById(R.id.layout_right),
+                View.GONE);
+    }
+
+    @Test
+    public void leftDeviceEstimateIsNotReadyRightDeviceIsReady_notShowView() {
+        mController.mIsLeftDeviceEstimateReady = false;
+        mController.mIsRightDeviceEstimateReady = true;
+
+        mController.showBothDevicesBatteryPredictionIfNecessary();
+
+        assertBatteryPredictionVisible(mLayoutPreference.findViewById(R.id.layout_left),
+                View.GONE);
+        assertBatteryPredictionVisible(mLayoutPreference.findViewById(R.id.layout_right),
+                View.GONE);
+    }
+
+    @Test
+    public void bothDevicesEstimateIsNotReady_notShowView() {
+        mController.mIsLeftDeviceEstimateReady = false;
+        mController.mIsRightDeviceEstimateReady = false;
+
+        mController.showBothDevicesBatteryPredictionIfNecessary();
+
+        assertBatteryPredictionVisible(mLayoutPreference.findViewById(R.id.layout_left),
                 View.GONE);
         assertBatteryPredictionVisible(mLayoutPreference.findViewById(R.id.layout_right),
                 View.GONE);