[PixelCare] Catalyst migration for battery header

NO_IFTTT=initialization

Bug: 372774754
Test: manual
Flag: com.android.settings.flags.catalyst_power_usage_summary_screen
Change-Id: Icc1c46069147830b3360a467ffc2e666dadd3ddd
diff --git a/res/xml/power_usage_summary.xml b/res/xml/power_usage_summary.xml
index 21a836d..c7cf873 100644
--- a/res/xml/power_usage_summary.xml
+++ b/res/xml/power_usage_summary.xml
@@ -26,6 +26,7 @@
         android:title="@string/summary_placeholder"
         android:selectable="false"
         android:paddingBottom="0px"
+        android:persistent="false"
         settings:controller="com.android.settings.fuelgauge.BatteryHeaderPreferenceController" />
 
     <com.android.settings.fuelgauge.BatteryHeaderTextPreference
diff --git a/src/com/android/settings/fuelgauge/BatteryHeaderPreference.kt b/src/com/android/settings/fuelgauge/BatteryHeaderPreference.kt
new file mode 100644
index 0000000..2a9b0e8
--- /dev/null
+++ b/src/com/android/settings/fuelgauge/BatteryHeaderPreference.kt
@@ -0,0 +1,95 @@
+/*
+ * Copyright (C) 2024 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.
+ */
+
+package com.android.settings.fuelgauge
+
+import android.content.Context
+import androidx.annotation.VisibleForTesting
+import androidx.preference.Preference
+import com.android.settings.R
+import com.android.settings.fuelgauge.BatteryBroadcastReceiver.BatteryUpdateType.BATTERY_NOT_PRESENT
+import com.android.settingslib.Utils
+import com.android.settingslib.fuelgauge.BatteryUtils
+import com.android.settingslib.metadata.PreferenceLifecycleContext
+import com.android.settingslib.metadata.PreferenceLifecycleProvider
+import com.android.settingslib.metadata.PreferenceMetadata
+import com.android.settingslib.preference.PreferenceBinding
+import com.android.settingslib.widget.UsageProgressBarPreference
+
+// LINT.IfChange
+class BatteryHeaderPreference :
+    PreferenceMetadata,
+    PreferenceBinding,
+    PreferenceLifecycleProvider {
+
+    @VisibleForTesting
+    var batteryBroadcastReceiver: BatteryBroadcastReceiver? = null
+
+    override val key: String
+        get() = KEY
+
+    override val title: Int
+        get() = R.string.summary_placeholder
+
+    override fun createWidget(context: Context) = UsageProgressBarPreference(context)
+
+    override fun bind(preference: Preference, metadata: PreferenceMetadata) {
+        super.bind(preference, metadata)
+        preference.isSelectable = false
+        if (preference is UsageProgressBarPreference) {
+            quickUpdateHeaderPreference(preference)
+        }
+    }
+
+    override fun isIndexable(context: Context) = false
+
+    override fun onCreate(context: PreferenceLifecycleContext) {
+        super.onCreate(context)
+        batteryBroadcastReceiver = BatteryBroadcastReceiver(context).apply {
+            setBatteryChangedListener {
+                if (it != BATTERY_NOT_PRESENT) {
+                    context.notifyPreferenceChange(KEY)
+                }
+            }
+        }
+    }
+
+    override fun onStart(context: PreferenceLifecycleContext) {
+        super.onStart(context)
+        batteryBroadcastReceiver?.register();
+    }
+
+    override fun onStop(context: PreferenceLifecycleContext) {
+        super.onStop(context)
+        batteryBroadcastReceiver?.unRegister();
+    }
+
+    companion object {
+        private const val KEY = "battery_header"
+        private const val BATTERY_MAX_LEVEL: Long = 100L
+
+        private fun quickUpdateHeaderPreference(preference: UsageProgressBarPreference) {
+            val batteryIntent = BatteryUtils.getBatteryIntent(preference.context) ?: return
+            val batteryLevel: Int = Utils.getBatteryLevel(batteryIntent)
+            preference.apply {
+                setUsageSummary(com.android.settings.Utils.formatPercentage(batteryLevel))
+                setPercent(batteryLevel.toLong(), BATTERY_MAX_LEVEL)
+                setBottomSummary("")
+            }
+        }
+    }
+}
+// LINT.ThenChange(BatteryHeaderPreferenceController.java)
diff --git a/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceController.java b/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceController.java
index 18c0f01..adcd7d1 100644
--- a/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceController.java
+++ b/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceController.java
@@ -34,6 +34,7 @@
 import com.android.settingslib.Utils;
 import com.android.settingslib.widget.UsageProgressBarPreference;
 
+// LINT.IfChange
 /** Controller that update the battery header view */
 public class BatteryHeaderPreferenceController extends BasePreferenceController
         implements PreferenceControllerMixin, LifecycleEventObserver {
@@ -113,3 +114,4 @@
         return com.android.settings.Utils.formatPercentage(batteryLevel);
     }
 }
+// LINT.ThenChange(BatteryHeaderPreference.kt)
diff --git a/src/com/android/settings/fuelgauge/batteryusage/PowerUsageSummaryScreen.kt b/src/com/android/settings/fuelgauge/batteryusage/PowerUsageSummaryScreen.kt
index b397bf3..9ff46b9 100644
--- a/src/com/android/settings/fuelgauge/batteryusage/PowerUsageSummaryScreen.kt
+++ b/src/com/android/settings/fuelgauge/batteryusage/PowerUsageSummaryScreen.kt
@@ -19,6 +19,7 @@
 import com.android.settings.R
 import com.android.settings.display.BatteryPercentageSwitchPreference
 import com.android.settings.flags.Flags
+import com.android.settings.fuelgauge.BatteryHeaderPreference
 import com.android.settingslib.metadata.PreferenceAvailabilityProvider
 import com.android.settingslib.metadata.PreferenceIconProvider
 import com.android.settingslib.metadata.ProvidePreferenceScreen
@@ -53,8 +54,10 @@
             R.drawable.ic_settings_battery_white
         }
 
-    override fun getPreferenceHierarchy(context: Context) =
-        preferenceHierarchy(this) { +BatteryPercentageSwitchPreference() }
+    override fun getPreferenceHierarchy(context: Context) = preferenceHierarchy(this) {
+        +BatteryHeaderPreference()
+        +BatteryPercentageSwitchPreference()
+    }
 
     companion object {
         const val KEY = "power_usage_summary_screen"
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceControllerTest.java
index a4b8b29..44d386f 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceControllerTest.java
@@ -48,6 +48,7 @@
 import org.robolectric.RuntimeEnvironment;
 import org.robolectric.annotation.Config;
 
+// LINT.IfChange
 @RunWith(RobolectricTestRunner.class)
 @Config(shadows = {ShadowEntityHeaderController.class, ShadowUtils.class})
 public class BatteryHeaderPreferenceControllerTest {
@@ -164,3 +165,4 @@
         return com.android.settings.Utils.formatPercentage(BATTERY_LEVEL);
     }
 }
+// LINT.ThenChange(BatteryHeaderPreferenceTest.java)
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceTest.kt b/tests/robotests/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceTest.kt
new file mode 100644
index 0000000..27e14c8
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/fuelgauge/BatteryHeaderPreferenceTest.kt
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2024 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.
+ */
+
+package com.android.settings.fuelgauge
+
+import android.content.BroadcastReceiver
+import android.content.Context
+import android.content.ContextWrapper
+import android.content.Intent
+import android.content.IntentFilter
+import android.os.BatteryManager.EXTRA_LEVEL
+import android.os.BatteryManager.EXTRA_SCALE
+import androidx.test.core.app.ApplicationProvider
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import com.android.settingslib.metadata.PreferenceLifecycleContext
+import com.android.settingslib.preference.createAndBindWidget
+import com.android.settingslib.widget.UsageProgressBarPreference
+import com.google.common.truth.Truth.assertThat
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.mockito.kotlin.mock
+import org.mockito.kotlin.verify
+
+// LINT.IfChange
+@RunWith(AndroidJUnit4::class)
+class BatteryHeaderPreferenceTest {
+    private val mockLifecycleContext = mock<PreferenceLifecycleContext>()
+    private val mockBatteryBroadcastReceiver = mock<BatteryBroadcastReceiver>()
+    private val batteryHeaderPreference = BatteryHeaderPreference()
+
+    private val context: Context =
+        object : ContextWrapper(ApplicationProvider.getApplicationContext()) {
+            override fun registerReceiver(receiver: BroadcastReceiver?, filter: IntentFilter?) =
+                Intent(Intent.ACTION_BATTERY_CHANGED)
+                    .putExtra(EXTRA_LEVEL, 60)
+                    .putExtra(EXTRA_SCALE, 100)
+        }
+
+    @Test
+    fun createAndBindWidget_selectableIsFalse() {
+        val usageProgressBarPreference =
+            batteryHeaderPreference.createAndBindWidget<UsageProgressBarPreference>(context)
+
+        assertThat(usageProgressBarPreference.isSelectable).isFalse()
+    }
+
+    @Test
+    fun onCreate_createBatteryBroadcastReceiver() {
+        batteryHeaderPreference.onCreate(mockLifecycleContext)
+
+        assertThat(batteryHeaderPreference.batteryBroadcastReceiver).isNotNull()
+    }
+
+    @Test
+    fun onStart_invokeRegisterMethod() {
+        batteryHeaderPreference.batteryBroadcastReceiver = mockBatteryBroadcastReceiver
+
+        batteryHeaderPreference.onStart(mockLifecycleContext)
+
+        verify(mockBatteryBroadcastReceiver).register()
+    }
+
+    @Test
+    fun onStop_invokeUnRegisterMethod() {
+        batteryHeaderPreference.batteryBroadcastReceiver = mockBatteryBroadcastReceiver
+
+        batteryHeaderPreference.onStop(mockLifecycleContext)
+
+        verify(mockBatteryBroadcastReceiver).unRegister()
+    }
+}
+// LINT.ThenChange(BatteryHeaderPreferenceControllerTest.java)