Merge "Support help message when battery is unavailable(1/2)" into sc-dev am: 57f27f7a30

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/14124308

Change-Id: I7b4da94c19efcf4b5af8a838adbb05865dc330e1
diff --git a/res/layout/preference_battery_error.xml b/res/layout/preference_battery_error.xml
new file mode 100644
index 0000000..66ff96a
--- /dev/null
+++ b/res/layout/preference_battery_error.xml
@@ -0,0 +1,59 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2021 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+<!-- TODO(b/179237551): Refine the layout once the design been lock down. -->
+<FrameLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content">
+    <com.google.android.material.card.MaterialCardView
+        android:id="@+id/container"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_marginStart="24dp"
+        android:layout_marginEnd="24dp"
+        style="@style/ContextualCardStyle">
+        <LinearLayout
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:gravity="top|start"
+            android:orientation="vertical"
+            android:padding="16dp"
+            android:background="@color/homepage_emergency_background">
+
+            <ImageView
+                android:id="@+id/battery_icon"
+                android:src="@drawable/ic_battery_alert_24dp"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content" />
+
+            <TextView android:id="@+id/summary_text"
+                      android:layout_width="match_parent"
+                      android:layout_height="wrap_content"
+                      android:text="@string/battery_missing_message"
+                      android:textAppearance="?android:attr/textAppearanceSmall"
+                      android:textColor="?android:attr/textColorSecondary" />
+
+            <TextView android:id="@+id/link_text"
+                      android:layout_width="wrap_content"
+                      android:layout_height="wrap_content"
+                      android:layout_gravity="end"
+                      android:text="@string/battery_missing_link_message"
+                      android:textAppearance="?android:attr/textAppearanceSmall"
+                      android:textColor="?android:attr/colorError" />
+        </LinearLayout>
+    </com.google.android.material.card.MaterialCardView>
+</FrameLayout>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index ddcd75e..d44c71e 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -6057,9 +6057,9 @@
     <string name="battery_header_title_alternate"><xliff:g id="number" example="88">^1</xliff:g><small> <font size="20"><xliff:g id="unit" example="%">%</xliff:g></font></small></string>
 
     <!-- Summary for top level battery tile if battery is not present. [CHAR LIMIT=NONE] -->
-    <string name="battery_missing_message">Problem reading your battery meter</string>
-    <!-- Summary to battery page if battery is not present. [CHAR LIMIT=NONE] -->
-    <string name="battery_missing_help_message">Problem reading your battery meter. Tap to <annotation id="url">learn more</annotation></string>
+    <string name="battery_missing_message">Can’t update battery info</string>
+    <!-- Help text if battery is not present. [CHAR LIMIT=NONE] -->
+    <string name="battery_missing_link_message"></string>
 
     <!-- Title for force stop dialog [CHAR LIMIT=30] -->
     <string name="dialog_stop_title">Stop app?</string>
diff --git a/res/xml/power_usage_summary.xml b/res/xml/power_usage_summary.xml
index 8c6fe41..0d37d58 100644
--- a/res/xml/power_usage_summary.xml
+++ b/res/xml/power_usage_summary.xml
@@ -28,11 +28,10 @@
         android:selectable="false"
         settings:controller="com.android.settings.fuelgauge.BatteryHeaderPreferenceController" />
 
-    <Preference
-        android:fragment="com.android.settings.fuelgauge.PowerUsageAdvanced"
-        android:key="battery_usage_summary"
-        android:title="@string/advanced_battery_preference_title"
-        app:iconSpaceReserved="false"
+    <com.android.settingslib.widget.LayoutPreference
+        android:key="battery_help_message"
+        android:layout="@layout/preference_battery_error"
+        android:selectable="false"
         settings:searchable="false" />
 
     <com.android.settings.widget.CardPreference
@@ -41,6 +40,13 @@
         settings:controller="com.android.settings.fuelgauge.batterytip.BatteryTipPreferenceController" />
 
     <Preference
+        android:fragment="com.android.settings.fuelgauge.PowerUsageAdvanced"
+        android:key="battery_usage_summary"
+        android:title="@string/advanced_battery_preference_title"
+        app:iconSpaceReserved="false"
+        settings:searchable="false" />
+
+    <Preference
         android:fragment="com.android.settings.fuelgauge.batterysaver.BatterySaverSettings"
         android:key="battery_saver_summary"
         android:title="@string/battery_saver"
diff --git a/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceController.java b/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceController.java
index d0fa8e9..e5030b9 100644
--- a/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceController.java
+++ b/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceController.java
@@ -91,7 +91,7 @@
         if (com.android.settings.Utils.isBatteryPresent(mContext)) {
             quickUpdateHeaderPreference();
         } else {
-            //TODO(b/179237551): Make new progress bar widget support help message
+            mBatteryUsageProgressBarPref.setVisible(false);
         }
     }
 
diff --git a/src/com/android/settings/fuelgauge/PowerUsageSummary.java b/src/com/android/settings/fuelgauge/PowerUsageSummary.java
index 7dfca22..326113e 100644
--- a/src/com/android/settings/fuelgauge/PowerUsageSummary.java
+++ b/src/com/android/settings/fuelgauge/PowerUsageSummary.java
@@ -29,6 +29,7 @@
 import androidx.annotation.VisibleForTesting;
 import androidx.loader.app.LoaderManager;
 import androidx.loader.content.Loader;
+import androidx.preference.Preference;
 
 import com.android.settings.R;
 import com.android.settings.SettingsActivity;
@@ -51,6 +52,8 @@
 public class PowerUsageSummary extends PowerUsageBase implements
         BatteryTipPreferenceController.BatteryTipListener {
 
+    private static final String KEY_BATTERY_ERROR = "battery_help_message";
+
     static final String TAG = "PowerUsageSummary";
 
     @VisibleForTesting
@@ -70,9 +73,11 @@
     @VisibleForTesting
     BatteryHeaderPreferenceController mBatteryHeaderPreferenceController;
     @VisibleForTesting
+    BatteryTipPreferenceController mBatteryTipPreferenceController;
+    @VisibleForTesting
     boolean mNeedUpdateBatteryTip;
     @VisibleForTesting
-    BatteryTipPreferenceController mBatteryTipPreferenceController;
+    Preference mHelpPreference;
 
     @VisibleForTesting
     final ContentObserver mSettingsObserver = new ContentObserver(new Handler()) {
@@ -152,8 +157,14 @@
 
         mBatteryUtils = BatteryUtils.getInstance(getContext());
 
+        mHelpPreference = findPreference(KEY_BATTERY_ERROR);
+        mHelpPreference.setVisible(false);
+
         if (Utils.isBatteryPresent(getContext())) {
             restartBatteryInfoLoader();
+        } else {
+            // Present help preference when battery is unavailable.
+            mHelpPreference.setVisible(true);
         }
         mBatteryTipPreferenceController.restoreInstanceState(icicle);
         updateBatteryTipFlag(icicle);
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceControllerTest.java
index f6c292d..1508385 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceControllerTest.java
@@ -308,6 +308,15 @@
                 BasePreferenceController.AVAILABLE_UNSEARCHABLE);
     }
 
+    @Test
+    public void displayPreference_batteryNotPresent_isInvisible() {
+        ShadowUtils.setIsBatteryPresent(false);
+
+        mController.displayPreference(mPreferenceScreen);
+
+        assertThat(mBatteryUsageProgressBarPref.isVisible()).isFalse();
+    }
+
     private CharSequence formatBatteryPercentageText() {
         return TextUtils.expandTemplate(mContext.getText(R.string.battery_header_title_alternate),
                 NumberFormat.getIntegerInstance().format(BATTERY_LEVEL));