1 /* 2 * Copyright (C) 2022 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.batteryusage; 18 19 import android.content.Context; 20 import android.graphics.drawable.Drawable; 21 import android.util.AttributeSet; 22 import android.widget.TextView; 23 24 import androidx.preference.PreferenceViewHolder; 25 26 import com.android.settings.R; 27 import com.android.settings.Utils; 28 import com.android.settingslib.widget.AppPreference; 29 30 /** 31 * Custom preference for displaying battery usage info as a bar and an icon on 32 * the left for the subsystem/app type. 33 * 34 * The battery usage info could be usage percentage or usage time. The preference 35 * won't show any icon if it is null. 36 */ 37 public class PowerGaugePreference extends AppPreference { 38 39 private BatteryEntry mInfo; 40 private BatteryDiffEntry mBatteryDiffEntry; 41 private CharSequence mContentDescription; 42 private CharSequence mProgress; 43 private boolean mShowAnomalyIcon; 44 PowerGaugePreference(Context context, Drawable icon, CharSequence contentDescription, BatteryEntry info)45 public PowerGaugePreference(Context context, Drawable icon, CharSequence contentDescription, 46 BatteryEntry info) { 47 this(context, null, icon, contentDescription, info); 48 } 49 PowerGaugePreference(Context context)50 public PowerGaugePreference(Context context) { 51 this(context, null, null, null, null); 52 } 53 PowerGaugePreference(Context context, AttributeSet attrs)54 public PowerGaugePreference(Context context, AttributeSet attrs) { 55 this(context, attrs, null, null, null); 56 } 57 PowerGaugePreference(Context context, AttributeSet attrs, Drawable icon, CharSequence contentDescription, BatteryEntry info)58 private PowerGaugePreference(Context context, AttributeSet attrs, Drawable icon, 59 CharSequence contentDescription, BatteryEntry info) { 60 super(context, attrs); 61 if (icon != null) { 62 setIcon(icon); 63 } 64 setWidgetLayoutResource(R.layout.preference_widget_summary); 65 mInfo = info; 66 mContentDescription = contentDescription; 67 mShowAnomalyIcon = false; 68 } 69 70 /** Sets the content description. */ setContentDescription(String name)71 public void setContentDescription(String name) { 72 mContentDescription = name; 73 notifyChanged(); 74 } 75 76 /** Sets the percent of total. */ setPercent(double percentOfTotal)77 public void setPercent(double percentOfTotal) { 78 mProgress = Utils.formatPercentage(percentOfTotal, true); 79 notifyChanged(); 80 } 81 82 /** Gets the percent of total. */ getPercent()83 public String getPercent() { 84 return mProgress.toString(); 85 } 86 87 /** Sets the subtitle. */ setSubtitle(CharSequence subtitle)88 public void setSubtitle(CharSequence subtitle) { 89 mProgress = subtitle; 90 notifyChanged(); 91 } 92 93 /** Gets the subtitle. */ getSubtitle()94 public CharSequence getSubtitle() { 95 return mProgress; 96 } 97 98 /** Sets whether to show anomaly icon */ shouldShowAnomalyIcon(boolean showAnomalyIcon)99 public void shouldShowAnomalyIcon(boolean showAnomalyIcon) { 100 mShowAnomalyIcon = showAnomalyIcon; 101 notifyChanged(); 102 } 103 104 /** Gets whether to show anomaly icon */ showAnomalyIcon()105 public boolean showAnomalyIcon() { 106 return mShowAnomalyIcon; 107 } 108 setBatteryDiffEntry(BatteryDiffEntry entry)109 public void setBatteryDiffEntry(BatteryDiffEntry entry) { 110 mBatteryDiffEntry = entry; 111 } 112 getInfo()113 BatteryEntry getInfo() { 114 return mInfo; 115 } 116 getBatteryDiffEntry()117 BatteryDiffEntry getBatteryDiffEntry() { 118 return mBatteryDiffEntry; 119 } 120 121 @Override onBindViewHolder(PreferenceViewHolder view)122 public void onBindViewHolder(PreferenceViewHolder view) { 123 super.onBindViewHolder(view); 124 125 final TextView subtitle = (TextView) view.findViewById(R.id.widget_summary); 126 subtitle.setText(mProgress); 127 if (mShowAnomalyIcon) { 128 subtitle.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.ic_warning_24dp, 0, 129 0, 0); 130 } else { 131 subtitle.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0); 132 } 133 if (mContentDescription != null) { 134 final TextView titleView = (TextView) view.findViewById(android.R.id.title); 135 titleView.setContentDescription(mContentDescription); 136 } 137 } 138 } 139