1 /* 2 * Copyright (C) 2009 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 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.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 CharSequence mContentDescription; 41 private CharSequence mProgress; 42 private boolean mShowAnomalyIcon; 43 PowerGaugePreference(Context context, Drawable icon, CharSequence contentDescription, BatteryEntry info)44 public PowerGaugePreference(Context context, Drawable icon, CharSequence contentDescription, 45 BatteryEntry info) { 46 this(context, null, icon, contentDescription, info); 47 } 48 PowerGaugePreference(Context context)49 public PowerGaugePreference(Context context) { 50 this(context, null, null, null, null); 51 } 52 PowerGaugePreference(Context context, AttributeSet attrs)53 public PowerGaugePreference(Context context, AttributeSet attrs) { 54 this(context, attrs, null, null, null); 55 } 56 PowerGaugePreference(Context context, AttributeSet attrs, Drawable icon, CharSequence contentDescription, BatteryEntry info)57 private PowerGaugePreference(Context context, AttributeSet attrs, Drawable icon, 58 CharSequence contentDescription, BatteryEntry info) { 59 super(context, attrs); 60 if (icon != null) { 61 setIcon(icon); 62 } 63 setWidgetLayoutResource(R.layout.preference_widget_summary); 64 mInfo = info; 65 mContentDescription = contentDescription; 66 mShowAnomalyIcon = false; 67 } 68 setContentDescription(String name)69 public void setContentDescription(String name) { 70 mContentDescription = name; 71 notifyChanged(); 72 } 73 setPercent(double percentOfTotal)74 public void setPercent(double percentOfTotal) { 75 mProgress = Utils.formatPercentage(percentOfTotal, true); 76 notifyChanged(); 77 } 78 getPercent()79 public String getPercent() { 80 return mProgress.toString(); 81 } 82 setSubtitle(CharSequence subtitle)83 public void setSubtitle(CharSequence subtitle) { 84 mProgress = subtitle; 85 notifyChanged(); 86 } 87 getSubtitle()88 public CharSequence getSubtitle() { 89 return mProgress; 90 } 91 shouldShowAnomalyIcon(boolean showAnomalyIcon)92 public void shouldShowAnomalyIcon(boolean showAnomalyIcon) { 93 mShowAnomalyIcon = showAnomalyIcon; 94 notifyChanged(); 95 } 96 showAnomalyIcon()97 public boolean showAnomalyIcon() { 98 return mShowAnomalyIcon; 99 } 100 getInfo()101 BatteryEntry getInfo() { 102 return mInfo; 103 } 104 105 @Override onBindViewHolder(PreferenceViewHolder view)106 public void onBindViewHolder(PreferenceViewHolder view) { 107 super.onBindViewHolder(view); 108 109 final TextView subtitle = (TextView) view.findViewById(R.id.widget_summary); 110 subtitle.setText(mProgress); 111 if (mShowAnomalyIcon) { 112 subtitle.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.ic_warning_24dp, 0, 113 0, 0); 114 } else { 115 subtitle.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0); 116 } 117 if (mContentDescription != null) { 118 final TextView titleView = (TextView) view.findViewById(android.R.id.title); 119 titleView.setContentDescription(mContentDescription); 120 } 121 } 122 } 123