• 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.ImageView;
25 import android.widget.LinearLayout;
26 import android.widget.TextView;
27 import com.android.settings.R;
28 import com.android.settings.TintablePreference;
29 import com.android.settings.Utils;
30 
31 /**
32  * Custom preference for displaying battery usage info as a bar and an icon on
33  * the left for the subsystem/app type.
34  *
35  * The battery usage info could be usage percentage or usage time. The preference
36  * won't show any icon if it is null.
37  */
38 public class PowerGaugePreference extends TintablePreference {
39     private final int mIconSize;
40 
41     private BatteryEntry mInfo;
42     private CharSequence mContentDescription;
43     private CharSequence mProgress;
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         setIcon(icon != null ? icon : new ColorDrawable(0));
62         setWidgetLayoutResource(R.layout.preference_widget_summary);
63         mInfo = info;
64         mContentDescription = contentDescription;
65         mIconSize = context.getResources().getDimensionPixelSize(R.dimen.app_icon_size);
66     }
67 
setContentDescription(String name)68     public void setContentDescription(String name) {
69         mContentDescription = name;
70         notifyChanged();
71     }
72 
setPercent(double percentOfTotal)73     public void setPercent(double percentOfTotal) {
74         mProgress = Utils.formatPercentage(percentOfTotal, true);
75         notifyChanged();
76     }
77 
getPercent()78     public String getPercent() {
79         return mProgress.toString();
80     }
81 
setSubtitle(CharSequence subtitle)82     public void setSubtitle(CharSequence subtitle) {
83         mProgress = subtitle;
84         notifyChanged();
85     }
86 
getSubtitle()87     public CharSequence getSubtitle() {
88         return mProgress;
89     }
90 
getInfo()91     BatteryEntry getInfo() {
92         return mInfo;
93     }
94 
95     @Override
onBindViewHolder(PreferenceViewHolder view)96     public void onBindViewHolder(PreferenceViewHolder view) {
97         super.onBindViewHolder(view);
98         ImageView icon = (ImageView) view.findViewById(android.R.id.icon);
99         icon.setLayoutParams(new LinearLayout.LayoutParams(mIconSize, mIconSize));
100 
101         ((TextView) view.findViewById(R.id.widget_summary)).setText(mProgress);
102         if (mContentDescription != null) {
103             final TextView titleView = (TextView) view.findViewById(android.R.id.title);
104             titleView.setContentDescription(mContentDescription);
105         }
106     }
107 }
108