• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.view.View;
23 import android.view.ViewGroup;
24 import android.widget.TextView;
25 
26 import androidx.preference.PreferenceViewHolder;
27 
28 import com.android.settings.R;
29 import com.android.settings.Utils;
30 import com.android.settingslib.widget.AppPreference;
31 
32 /**
33  * Custom preference for displaying battery usage info as a bar and an icon on
34  * the left for the subsystem/app type.
35  *
36  * The battery usage info could be usage percentage or usage time. The preference
37  * won't show any icon if it is null.
38  */
39 public class PowerGaugePreference extends AppPreference {
40 
41     // Please see go/battery-usage-app-list-alpha
42     private static final float SELECTABLE_ALPHA = 1f;
43     private static final float UNSELECTABLE_ALPHA_LIGHT_MODE = 0.65f;
44     private static final float UNSELECTABLE_ALPHA_DARK_MODE = 0.5f;
45 
46     private BatteryEntry mInfo;
47     private BatteryDiffEntry mBatteryDiffEntry;
48     private CharSequence mContentDescription;
49     private CharSequence mProgress;
50     private boolean mShowAnomalyIcon;
51 
PowerGaugePreference(Context context, Drawable icon, CharSequence contentDescription, BatteryEntry info)52     public PowerGaugePreference(Context context, Drawable icon, CharSequence contentDescription,
53             BatteryEntry info) {
54         this(context, null, icon, contentDescription, info);
55     }
56 
PowerGaugePreference(Context context)57     public PowerGaugePreference(Context context) {
58         this(context, null, null, null, null);
59     }
60 
PowerGaugePreference(Context context, AttributeSet attrs)61     public PowerGaugePreference(Context context, AttributeSet attrs) {
62         this(context, attrs, null, null, null);
63     }
64 
PowerGaugePreference(Context context, AttributeSet attrs, Drawable icon, CharSequence contentDescription, BatteryEntry info)65     private PowerGaugePreference(Context context, AttributeSet attrs, Drawable icon,
66             CharSequence contentDescription, BatteryEntry info) {
67         super(context, attrs);
68         if (icon != null) {
69             setIcon(icon);
70         }
71         setWidgetLayoutResource(R.layout.preference_widget_summary);
72         mInfo = info;
73         mContentDescription = contentDescription;
74         mShowAnomalyIcon = false;
75     }
76 
77     /** Sets the content description. */
setContentDescription(String name)78     public void setContentDescription(String name) {
79         mContentDescription = name;
80         notifyChanged();
81     }
82 
83     /** Sets the percentage to show. */
setPercentage(CharSequence percentage)84     public void setPercentage(CharSequence percentage) {
85         mProgress = percentage;
86         notifyChanged();
87     }
88 
89     /** Gets the percentage to show. */
getPercentage()90     public String getPercentage() {
91         return mProgress.toString();
92     }
93 
94     /** Sets whether to show anomaly icon */
shouldShowAnomalyIcon(boolean showAnomalyIcon)95     public void shouldShowAnomalyIcon(boolean showAnomalyIcon) {
96         mShowAnomalyIcon = showAnomalyIcon;
97         notifyChanged();
98     }
99 
100     /** Gets whether to show anomaly icon */
showAnomalyIcon()101     public boolean showAnomalyIcon() {
102         return mShowAnomalyIcon;
103     }
104 
setBatteryDiffEntry(BatteryDiffEntry entry)105     public void setBatteryDiffEntry(BatteryDiffEntry entry) {
106         mBatteryDiffEntry = entry;
107     }
108 
getInfo()109     BatteryEntry getInfo() {
110         return mInfo;
111     }
112 
getBatteryDiffEntry()113     BatteryDiffEntry getBatteryDiffEntry() {
114         return mBatteryDiffEntry;
115     }
116 
117     @Override
onBindViewHolder(PreferenceViewHolder view)118     public void onBindViewHolder(PreferenceViewHolder view) {
119         super.onBindViewHolder(view);
120 
121         final boolean isNightMode = Utils.isNightMode(getContext());
122         final float alpha = isSelectable() ? SELECTABLE_ALPHA
123                 : (isNightMode ? UNSELECTABLE_ALPHA_DARK_MODE : UNSELECTABLE_ALPHA_LIGHT_MODE);
124         setViewAlpha(view.itemView, alpha);
125 
126         final TextView subtitle = (TextView) view.findViewById(R.id.widget_summary);
127         subtitle.setText(mProgress);
128         if (mShowAnomalyIcon) {
129             subtitle.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.ic_warning_24dp, 0,
130                     0, 0);
131         } else {
132             subtitle.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0);
133         }
134         if (mContentDescription != null) {
135             final TextView titleView = (TextView) view.findViewById(android.R.id.title);
136             titleView.setContentDescription(mContentDescription);
137         }
138     }
139 
setViewAlpha(View view, float alpha)140     private static void setViewAlpha(View view, float alpha) {
141         if (view instanceof ViewGroup) {
142             final ViewGroup viewGroup = (ViewGroup) view;
143             for (int i = viewGroup.getChildCount() - 1; i >= 0; i--) {
144                 setViewAlpha(viewGroup.getChildAt(i), alpha);
145             }
146         } else {
147             view.setAlpha(alpha);
148         }
149     }
150 }
151