1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.fuelgauge 18 19 import android.content.Context 20 import androidx.annotation.VisibleForTesting 21 import androidx.preference.Preference 22 import com.android.settings.R 23 import com.android.settings.fuelgauge.BatteryBroadcastReceiver.BatteryUpdateType.BATTERY_NOT_PRESENT 24 import com.android.settingslib.Utils 25 import com.android.settingslib.datastore.KeyValueStore 26 import com.android.settingslib.datastore.NoOpKeyedObservable 27 import com.android.settingslib.fuelgauge.BatteryUtils 28 import com.android.settingslib.metadata.PersistentPreference 29 import com.android.settingslib.metadata.PreferenceLifecycleContext 30 import com.android.settingslib.metadata.PreferenceLifecycleProvider 31 import com.android.settingslib.metadata.PreferenceMetadata 32 import com.android.settingslib.metadata.RangeValue 33 import com.android.settingslib.metadata.ReadWritePermit 34 import com.android.settingslib.preference.PreferenceBinding 35 import com.android.settingslib.widget.UsageProgressBarPreference 36 37 // LINT.IfChange 38 class BatteryHeaderPreference : 39 PersistentPreference<Int>, 40 PreferenceMetadata, 41 PreferenceBinding, 42 PreferenceLifecycleProvider, 43 RangeValue { 44 45 @VisibleForTesting var batteryBroadcastReceiver: BatteryBroadcastReceiver? = null 46 47 override val key: String 48 get() = KEY 49 50 override val title: Int 51 get() = R.string.summary_placeholder 52 createWidgetnull53 override fun createWidget(context: Context) = UsageProgressBarPreference(context) 54 55 override fun bind(preference: Preference, metadata: PreferenceMetadata) { 56 super.bind(preference, metadata) 57 preference.isSelectable = false 58 if (preference is UsageProgressBarPreference) { 59 quickUpdateHeaderPreference(preference) 60 } 61 } 62 isIndexablenull63 override fun isIndexable(context: Context) = false 64 65 override fun onCreate(context: PreferenceLifecycleContext) { 66 super.onCreate(context) 67 batteryBroadcastReceiver = 68 BatteryBroadcastReceiver(context).apply { 69 setBatteryChangedListener { 70 if (it != BATTERY_NOT_PRESENT) { 71 context.notifyPreferenceChange(KEY) 72 } 73 } 74 } 75 } 76 onStartnull77 override fun onStart(context: PreferenceLifecycleContext) { 78 super.onStart(context) 79 batteryBroadcastReceiver?.register() 80 } 81 onStopnull82 override fun onStop(context: PreferenceLifecycleContext) { 83 super.onStop(context) 84 batteryBroadcastReceiver?.unRegister() 85 } 86 storagenull87 override fun storage(context: Context): KeyValueStore = 88 object : NoOpKeyedObservable<String>(), KeyValueStore { 89 override fun contains(key: String) = BatteryUtils.getBatteryIntent(context) != null 90 91 @Suppress("UNCHECKED_CAST") 92 override fun <T : Any> getValue(key: String, valueType: Class<T>): T? { 93 val batteryIntent = BatteryUtils.getBatteryIntent(context) ?: return null 94 return Utils.getBatteryLevel(batteryIntent) as T 95 } 96 97 override fun <T : Any> setValue(key: String, valueType: Class<T>, value: T?) = 98 throw UnsupportedOperationException() 99 } 100 getMinValuenull101 override fun getMinValue(context: Context): Int = 0 102 103 override fun getMaxValue(context: Context): Int = 100 104 105 override fun getReadPermit(context: Context, myUid: Int, callingUid: Int) = 106 ReadWritePermit.ALLOW 107 108 override fun getWritePermit(context: Context, value: Int?, myUid: Int, callingUid: Int) = 109 ReadWritePermit.DISALLOW 110 111 companion object { 112 private const val KEY = "battery_header" 113 private const val BATTERY_MAX_LEVEL: Long = 100L 114 115 private fun quickUpdateHeaderPreference(preference: UsageProgressBarPreference) { 116 val batteryIntent = BatteryUtils.getBatteryIntent(preference.context) ?: return 117 val batteryLevel: Int = Utils.getBatteryLevel(batteryIntent) 118 preference.apply { 119 setUsageSummary(com.android.settings.Utils.formatPercentage(batteryLevel)) 120 setPercent(batteryLevel.toLong(), BATTERY_MAX_LEVEL) 121 setBottomSummary("") 122 } 123 } 124 } 125 } 126 // LINT.ThenChange(BatteryHeaderPreferenceController.java) 127