blob: 9dd39d432527d0e42d266b1d86ec39366b9cbfcd [file] [log] [blame]
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -04001/*
2 * Copyright (C) 2013 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 */
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040016package com.android.systemui;
17
Evan Lairdbcf631d2017-03-10 10:56:45 -050018import static android.provider.Settings.System.SHOW_BATTERY_PERCENT;
19
Jason Monk9a376bc2017-05-10 09:52:10 -040020import android.animation.ArgbEvaluator;
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040021import android.content.Context;
Jason Monkaa573e92017-01-27 17:00:29 -050022import android.content.res.Resources;
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040023import android.content.res.TypedArray;
Dan Sandler055bb612017-02-08 16:21:49 -080024import android.database.ContentObserver;
Jason Monk9a376bc2017-05-10 09:52:10 -040025import android.graphics.Rect;
Dan Sandler055bb612017-02-08 16:21:49 -080026import android.net.Uri;
27import android.os.Handler;
28import android.provider.Settings;
Jason Monk9a376bc2017-05-10 09:52:10 -040029import android.util.ArraySet;
30import android.util.AttributeSet;
31import android.util.TypedValue;
32import android.view.ContextThemeWrapper;
Dan Sandler055bb612017-02-08 16:21:49 -080033import android.view.Gravity;
34import android.view.LayoutInflater;
Jason Monk3e189872016-01-12 09:10:34 -050035import android.view.View;
Dan Sandler055bb612017-02-08 16:21:49 -080036import android.view.ViewGroup;
Jason Monkabe19742015-09-29 09:47:06 -040037import android.widget.ImageView;
Jason Monkaa573e92017-01-27 17:00:29 -050038import android.widget.LinearLayout;
Dan Sandler055bb612017-02-08 16:21:49 -080039import android.widget.TextView;
Jason Monk9a376bc2017-05-10 09:52:10 -040040
41import com.android.settingslib.Utils;
Dan Sandler055bb612017-02-08 16:21:49 -080042import com.android.settingslib.graph.BatteryMeterDrawableBase;
Jason Monk3e189872016-01-12 09:10:34 -050043import com.android.systemui.statusbar.phone.StatusBarIconController;
Jorim Jaggi708f7722014-08-20 17:30:38 +020044import com.android.systemui.statusbar.policy.BatteryController;
Jason Monkaa573e92017-01-27 17:00:29 -050045import com.android.systemui.statusbar.policy.BatteryController.BatteryStateChangeCallback;
46import com.android.systemui.statusbar.policy.ConfigurationController;
47import com.android.systemui.statusbar.policy.ConfigurationController.ConfigurationListener;
48import com.android.systemui.statusbar.policy.DarkIconDispatcher;
49import com.android.systemui.statusbar.policy.DarkIconDispatcher.DarkReceiver;
Jason Monk3e189872016-01-12 09:10:34 -050050import com.android.systemui.tuner.TunerService;
Jason Monkaa573e92017-01-27 17:00:29 -050051import com.android.systemui.tuner.TunerService.Tunable;
Jorim Jaggi708f7722014-08-20 17:30:38 +020052
Dan Sandler055bb612017-02-08 16:21:49 -080053import java.text.NumberFormat;
54
55public class BatteryMeterView extends LinearLayout implements
Jason Monkaa573e92017-01-27 17:00:29 -050056 BatteryStateChangeCallback, Tunable, DarkReceiver, ConfigurationListener {
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040057
Dan Sandler055bb612017-02-08 16:21:49 -080058 private final BatteryMeterDrawableBase mDrawable;
Jason Monk3e189872016-01-12 09:10:34 -050059 private final String mSlotBattery;
Dan Sandler055bb612017-02-08 16:21:49 -080060 private final ImageView mBatteryIconView;
61 private TextView mBatteryPercentView;
62
Jorim Jaggi708f7722014-08-20 17:30:38 +020063 private BatteryController mBatteryController;
Dan Sandler055bb612017-02-08 16:21:49 -080064 private SettingObserver mSettingObserver;
65 private int mTextColor;
66 private int mLevel;
Jason Monkf13413e2017-02-15 15:49:32 -050067 private boolean mForceShowPercent;
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040068
Jason Monk9a376bc2017-05-10 09:52:10 -040069 private int mDarkModeBackgroundColor;
70 private int mDarkModeFillColor;
71
72 private int mLightModeBackgroundColor;
73 private int mLightModeFillColor;
74
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040075 public BatteryMeterView(Context context) {
76 this(context, null, 0);
77 }
78
79 public BatteryMeterView(Context context, AttributeSet attrs) {
80 this(context, attrs, 0);
81 }
82
83 public BatteryMeterView(Context context, AttributeSet attrs, int defStyle) {
84 super(context, attrs, defStyle);
85
Dan Sandler055bb612017-02-08 16:21:49 -080086 setOrientation(LinearLayout.HORIZONTAL);
87 setGravity(Gravity.CENTER_VERTICAL | Gravity.START);
88
John Spurlock29786fc2014-02-04 17:55:47 -050089 TypedArray atts = context.obtainStyledAttributes(attrs, R.styleable.BatteryMeterView,
90 defStyle, 0);
91 final int frameColor = atts.getColor(R.styleable.BatteryMeterView_frameColor,
Alan Viverette4a357cd2015-03-18 18:37:18 -070092 context.getColor(R.color.batterymeter_frame_color));
Dan Sandler055bb612017-02-08 16:21:49 -080093 mDrawable = new BatteryMeterDrawableBase(context, frameColor);
John Spurlock29786fc2014-02-04 17:55:47 -050094 atts.recycle();
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040095
Dan Sandler055bb612017-02-08 16:21:49 -080096 mSettingObserver = new SettingObserver(new Handler(context.getMainLooper()));
97
Jason Monk3e189872016-01-12 09:10:34 -050098 mSlotBattery = context.getString(
99 com.android.internal.R.string.status_bar_battery);
Dan Sandler055bb612017-02-08 16:21:49 -0800100 mBatteryIconView = new ImageView(context);
101 mBatteryIconView.setImageDrawable(mDrawable);
102 final MarginLayoutParams mlp = new MarginLayoutParams(
103 getResources().getDimensionPixelSize(R.dimen.status_bar_battery_icon_width),
104 getResources().getDimensionPixelSize(R.dimen.status_bar_battery_icon_height));
105 mlp.setMargins(0, 0, 0,
106 getResources().getDimensionPixelOffset(R.dimen.battery_margin_bottom));
107 addView(mBatteryIconView, mlp);
108
109 updateShowPercent();
Jason Monk9a376bc2017-05-10 09:52:10 -0400110
111 Context dualToneDarkTheme = new ContextThemeWrapper(context,
112 Utils.getThemeAttr(context, R.attr.darkIconTheme));
113 Context dualToneLightTheme = new ContextThemeWrapper(context,
114 Utils.getThemeAttr(context, R.attr.lightIconTheme));
115 mDarkModeBackgroundColor = Utils.getColorAttr(dualToneDarkTheme, R.attr.backgroundColor);
116 mDarkModeFillColor = Utils.getColorAttr(dualToneDarkTheme, R.attr.fillColor);
117 mLightModeBackgroundColor = Utils.getColorAttr(dualToneLightTheme, R.attr.backgroundColor);
118 mLightModeFillColor = Utils.getColorAttr(dualToneLightTheme, R.attr.fillColor);
119
Jason Monkd866b8a2017-03-29 15:30:44 -0400120 // Init to not dark at all.
121 onDarkChanged(new Rect(), 0, DarkIconDispatcher.DEFAULT_ICON_TINT);
Dan Sandler055bb612017-02-08 16:21:49 -0800122 }
123
Dan Sandlerdf14c202017-02-21 14:51:11 -0500124 public void setForceShowPercent(boolean show) {
125 mForceShowPercent = show;
Jason Monkf13413e2017-02-15 15:49:32 -0500126 updateShowPercent();
127 }
128
Jorim Jaggi0d266892014-07-28 14:49:13 +0200129 @Override
130 public boolean hasOverlappingRendering() {
131 return false;
132 }
133
Jason Monkabe19742015-09-29 09:47:06 -0400134 @Override
Jason Monk3e189872016-01-12 09:10:34 -0500135 public void onTuningChanged(String key, String newValue) {
136 if (StatusBarIconController.ICON_BLACKLIST.equals(key)) {
137 ArraySet<String> icons = StatusBarIconController.getIconBlacklist(newValue);
138 setVisibility(icons.contains(mSlotBattery) ? View.GONE : View.VISIBLE);
139 }
140 }
141
142 @Override
Jason Monkabe19742015-09-29 09:47:06 -0400143 public void onAttachedToWindow() {
144 super.onAttachedToWindow();
Jason Monk9c7844c2017-01-18 15:21:53 -0500145 mBatteryController = Dependency.get(BatteryController.class);
Jason Monk9c7844c2017-01-18 15:21:53 -0500146 mBatteryController.addCallback(this);
Dan Sandler055bb612017-02-08 16:21:49 -0800147 getContext().getContentResolver().registerContentObserver(
Evan Lairdbcf631d2017-03-10 10:56:45 -0500148 Settings.System.getUriFor(SHOW_BATTERY_PERCENT), false, mSettingObserver);
Dan Sandler055bb612017-02-08 16:21:49 -0800149 updateShowPercent();
Jason Monkde850bb2017-02-01 19:26:30 -0500150 Dependency.get(TunerService.class).addTunable(this, StatusBarIconController.ICON_BLACKLIST);
Jason Monkaa573e92017-01-27 17:00:29 -0500151 Dependency.get(ConfigurationController.class).addCallback(this);
Jason Monkabe19742015-09-29 09:47:06 -0400152 }
John Spurlock3c875662013-08-31 15:07:25 -0400153
154 @Override
Jason Monkabe19742015-09-29 09:47:06 -0400155 public void onDetachedFromWindow() {
156 super.onDetachedFromWindow();
Jason Monk88529052016-11-04 13:29:58 -0400157 mBatteryController.removeCallback(this);
Dan Sandler055bb612017-02-08 16:21:49 -0800158 getContext().getContentResolver().unregisterContentObserver(mSettingObserver);
Jason Monkde850bb2017-02-01 19:26:30 -0500159 Dependency.get(TunerService.class).removeTunable(this);
Jason Monkaa573e92017-01-27 17:00:29 -0500160 Dependency.get(ConfigurationController.class).removeCallback(this);
John Spurlock3c875662013-08-31 15:07:25 -0400161 }
John Spurlockf40d08f2015-05-29 10:48:22 -0400162
Jason Monkabe19742015-09-29 09:47:06 -0400163 @Override
164 public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
Dan Sandler055bb612017-02-08 16:21:49 -0800165 mDrawable.setBatteryLevel(level);
jackqdyuleiff5bd942017-04-04 10:54:21 -0700166 mDrawable.setCharging(pluggedIn);
Dan Sandler055bb612017-02-08 16:21:49 -0800167 mLevel = level;
168 updatePercentText();
Jason Monkabe19742015-09-29 09:47:06 -0400169 setContentDescription(
Adrian Roos70dcf832016-04-20 15:51:42 -0700170 getContext().getString(charging ? R.string.accessibility_battery_level_charging
171 : R.string.accessibility_battery_level, level));
John Spurlockf40d08f2015-05-29 10:48:22 -0400172 }
173
Jason Monkabe19742015-09-29 09:47:06 -0400174 @Override
Jason Monkc06fbb12016-01-08 14:12:18 -0500175 public void onPowerSaveChanged(boolean isPowerSave) {
Dan Sandler055bb612017-02-08 16:21:49 -0800176 mDrawable.setPowerSave(isPowerSave);
177 }
John Spurlockf40d08f2015-05-29 10:48:22 -0400178
Dan Sandler055bb612017-02-08 16:21:49 -0800179 private TextView loadPercentView() {
180 return (TextView) LayoutInflater.from(getContext())
181 .inflate(R.layout.battery_percentage_view, null);
182 }
183
184 private void updatePercentText() {
185 if (mBatteryPercentView != null) {
186 mBatteryPercentView.setText(
Jason Monk9a376bc2017-05-10 09:52:10 -0400187 NumberFormat.getPercentInstance().format(mLevel / 100f));
Dan Sandler055bb612017-02-08 16:21:49 -0800188 }
189 }
190
191 private void updateShowPercent() {
192 final boolean showing = mBatteryPercentView != null;
193 if (0 != Settings.System.getInt(getContext().getContentResolver(),
Evan Lairdbcf631d2017-03-10 10:56:45 -0500194 SHOW_BATTERY_PERCENT, 0) || mForceShowPercent) {
Dan Sandler055bb612017-02-08 16:21:49 -0800195 if (!showing) {
196 mBatteryPercentView = loadPercentView();
197 if (mTextColor != 0) mBatteryPercentView.setTextColor(mTextColor);
198 updatePercentText();
199 addView(mBatteryPercentView,
Evan Laird09ebefd2017-03-24 10:55:29 -0400200 0,
Dan Sandler055bb612017-02-08 16:21:49 -0800201 new ViewGroup.LayoutParams(
202 LayoutParams.WRAP_CONTENT,
203 LayoutParams.MATCH_PARENT));
204 }
205 } else {
206 if (showing) {
207 removeView(mBatteryPercentView);
208 mBatteryPercentView = null;
209 }
210 }
John Spurlockf40d08f2015-05-29 10:48:22 -0400211 }
212
Jason Monkaa573e92017-01-27 17:00:29 -0500213 @Override
214 public void onDensityOrFontScaleChanged() {
215 scaleBatteryMeterViews();
216 }
217
218 /**
219 * Looks up the scale factor for status bar icons and scales the battery view by that amount.
220 */
221 private void scaleBatteryMeterViews() {
222 Resources res = getContext().getResources();
223 TypedValue typedValue = new TypedValue();
224
225 res.getValue(R.dimen.status_bar_icon_scale_factor, typedValue, true);
226 float iconScaleFactor = typedValue.getFloat();
227
228 int batteryHeight = res.getDimensionPixelSize(R.dimen.status_bar_battery_icon_height);
229 int batteryWidth = res.getDimensionPixelSize(R.dimen.status_bar_battery_icon_width);
230 int marginBottom = res.getDimensionPixelSize(R.dimen.battery_margin_bottom);
231
232 LinearLayout.LayoutParams scaledLayoutParams = new LinearLayout.LayoutParams(
233 (int) (batteryWidth * iconScaleFactor), (int) (batteryHeight * iconScaleFactor));
Dan Sandler055bb612017-02-08 16:21:49 -0800234 scaledLayoutParams.setMargins(0, 0, 0, marginBottom);
Jason Monkaa573e92017-01-27 17:00:29 -0500235
Dan Sandler055bb612017-02-08 16:21:49 -0800236 mBatteryIconView.setLayoutParams(scaledLayoutParams);
Jason Monkaa573e92017-01-27 17:00:29 -0500237 }
238
239 @Override
240 public void onDarkChanged(Rect area, float darkIntensity, int tint) {
Jason Monk9a376bc2017-05-10 09:52:10 -0400241 float intensity = DarkIconDispatcher.isInArea(area, this) ? darkIntensity : 0;
242 int foreground = getColorForDarkIntensity(intensity, mLightModeFillColor,
243 mDarkModeFillColor);
244 int background = getColorForDarkIntensity(intensity, mLightModeBackgroundColor,
245 mDarkModeBackgroundColor);
246 mDrawable.setColors(foreground, background);
247 setTextColor(foreground);
Dan Sandler055bb612017-02-08 16:21:49 -0800248 }
249
250 public void setTextColor(int color) {
251 mTextColor = color;
252 if (mBatteryPercentView != null) {
253 mBatteryPercentView.setTextColor(color);
254 }
Jason Monkabe19742015-09-29 09:47:06 -0400255 }
Jason Monk32508852017-01-18 09:17:13 -0500256
Jason Monk9a376bc2017-05-10 09:52:10 -0400257 private int getColorForDarkIntensity(float darkIntensity, int lightColor, int darkColor) {
258 return (int) ArgbEvaluator.getInstance().evaluate(darkIntensity, lightColor, darkColor);
Jason Monk32508852017-01-18 09:17:13 -0500259 }
Dan Sandler055bb612017-02-08 16:21:49 -0800260
261 private final class SettingObserver extends ContentObserver {
262 public SettingObserver(Handler handler) {
263 super(handler);
264 }
265
266 @Override
267 public void onChange(boolean selfChange, Uri uri) {
268 super.onChange(selfChange, uri);
269 updateShowPercent();
270 }
271 }
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -0400272}