• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
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 
setContentDescription(String name)70     public void setContentDescription(String name) {
71         mContentDescription = name;
72         notifyChanged();
73     }
74 
setPercent(double percentOfTotal)75     public void setPercent(double percentOfTotal) {
76         mProgress = Utils.formatPercentage(percentOfTotal, true);
77         notifyChanged();
78     }
79 
getPercent()80     public String getPercent() {
81         return mProgress.toString();
82     }
83 
setSubtitle(CharSequence subtitle)84     public void setSubtitle(CharSequence subtitle) {
85         mProgress = subtitle;
86         notifyChanged();
87     }
88 
getSubtitle()89     public CharSequence getSubtitle() {
90         return mProgress;
91     }
92 
shouldShowAnomalyIcon(boolean showAnomalyIcon)93     public void shouldShowAnomalyIcon(boolean showAnomalyIcon) {
94         mShowAnomalyIcon = showAnomalyIcon;
95         notifyChanged();
96     }
97 
showAnomalyIcon()98     public boolean showAnomalyIcon() {
99         return mShowAnomalyIcon;
100     }
101 
setBatteryDiffEntry(BatteryDiffEntry entry)102     public void setBatteryDiffEntry(BatteryDiffEntry entry) {
103         mBatteryDiffEntry = entry;
104     }
105 
getInfo()106     BatteryEntry getInfo() {
107         return mInfo;
108     }
109 
getBatteryDiffEntry()110     BatteryDiffEntry getBatteryDiffEntry() {
111         return mBatteryDiffEntry;
112     }
113 
114     @Override
onBindViewHolder(PreferenceViewHolder view)115     public void onBindViewHolder(PreferenceViewHolder view) {
116         super.onBindViewHolder(view);
117 
118         final TextView subtitle = (TextView) view.findViewById(R.id.widget_summary);
119         subtitle.setText(mProgress);
120         if (mShowAnomalyIcon) {
121             subtitle.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.ic_warning_24dp, 0,
122                     0, 0);
123         } else {
124             subtitle.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0);
125         }
126         if (mContentDescription != null) {
127             final TextView titleView = (TextView) view.findViewById(android.R.id.title);
128             titleView.setContentDescription(mContentDescription);
129         }
130     }
131 }
132