• 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.ColorDrawable;
21 import android.graphics.drawable.Drawable;
22 import android.support.v7.preference.PreferenceViewHolder;
23 import android.util.AttributeSet;
24 import android.widget.TextView;
25 
26 import com.android.settings.R;
27 import com.android.settings.Utils;
28 import com.android.settings.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 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         setIcon(icon != null ? icon : new ColorDrawable(0));
61         setWidgetLayoutResource(R.layout.preference_widget_summary);
62         mInfo = info;
63         mContentDescription = contentDescription;
64         mShowAnomalyIcon = false;
65     }
66 
setContentDescription(String name)67     public void setContentDescription(String name) {
68         mContentDescription = name;
69         notifyChanged();
70     }
71 
setPercent(double percentOfTotal)72     public void setPercent(double percentOfTotal) {
73         mProgress = Utils.formatPercentage(percentOfTotal, true);
74         notifyChanged();
75     }
76 
getPercent()77     public String getPercent() {
78         return mProgress.toString();
79     }
80 
setSubtitle(CharSequence subtitle)81     public void setSubtitle(CharSequence subtitle) {
82         mProgress = subtitle;
83         notifyChanged();
84     }
85 
getSubtitle()86     public CharSequence getSubtitle() {
87         return mProgress;
88     }
89 
shouldShowAnomalyIcon(boolean showAnomalyIcon)90     public void shouldShowAnomalyIcon(boolean showAnomalyIcon) {
91         mShowAnomalyIcon = showAnomalyIcon;
92         notifyChanged();
93     }
94 
showAnomalyIcon()95     public boolean showAnomalyIcon() {
96         return mShowAnomalyIcon;
97     }
98 
getInfo()99     BatteryEntry getInfo() {
100         return mInfo;
101     }
102 
103     @Override
onBindViewHolder(PreferenceViewHolder view)104     public void onBindViewHolder(PreferenceViewHolder view) {
105         super.onBindViewHolder(view);
106 
107         final TextView subtitle = (TextView) view.findViewById(R.id.widget_summary);
108         subtitle.setText(mProgress);
109         if (mShowAnomalyIcon) {
110             subtitle.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.ic_warning_24dp, 0,
111                     0, 0);
112         } else {
113             subtitle.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0);
114         }
115         if (mContentDescription != null) {
116             final TextView titleView = (TextView) view.findViewById(android.R.id.title);
117             titleView.setContentDescription(mContentDescription);
118         }
119     }
120 }
121