blob: 1ae06d7512555c329d87337428146f53ea34e2f4 [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
Charles He6a79b0d2017-09-18 09:50:58 +010018import static android.app.StatusBarManager.DISABLE2_SYSTEM_ICONS;
19import static android.app.StatusBarManager.DISABLE_NONE;
Evan Lairdbcf631d2017-03-10 10:56:45 -050020import static android.provider.Settings.System.SHOW_BATTERY_PERCENT;
21
Jason Monk9a376bc2017-05-10 09:52:10 -040022import android.animation.ArgbEvaluator;
Jason Monka6f1db3a2017-08-30 19:18:00 -040023import android.app.ActivityManager;
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040024import android.content.Context;
Jason Monkaa573e92017-01-27 17:00:29 -050025import android.content.res.Resources;
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040026import android.content.res.TypedArray;
Dan Sandler055bb612017-02-08 16:21:49 -080027import android.database.ContentObserver;
Jason Monk9a376bc2017-05-10 09:52:10 -040028import android.graphics.Rect;
Dan Sandler055bb612017-02-08 16:21:49 -080029import android.net.Uri;
30import android.os.Handler;
31import android.provider.Settings;
Jason Monk9a376bc2017-05-10 09:52:10 -040032import android.util.ArraySet;
33import android.util.AttributeSet;
34import android.util.TypedValue;
35import android.view.ContextThemeWrapper;
Dan Sandler055bb612017-02-08 16:21:49 -080036import android.view.Gravity;
37import android.view.LayoutInflater;
Jason Monk3e189872016-01-12 09:10:34 -050038import android.view.View;
Dan Sandler055bb612017-02-08 16:21:49 -080039import android.view.ViewGroup;
Jason Monkabe19742015-09-29 09:47:06 -040040import android.widget.ImageView;
Jason Monkaa573e92017-01-27 17:00:29 -050041import android.widget.LinearLayout;
Dan Sandler055bb612017-02-08 16:21:49 -080042import android.widget.TextView;
Jason Monk9a376bc2017-05-10 09:52:10 -040043
44import com.android.settingslib.Utils;
Dan Sandler055bb612017-02-08 16:21:49 -080045import com.android.settingslib.graph.BatteryMeterDrawableBase;
Jason Monka6f1db3a2017-08-30 19:18:00 -040046import com.android.systemui.settings.CurrentUserTracker;
Jason Monk3e189872016-01-12 09:10:34 -050047import com.android.systemui.statusbar.phone.StatusBarIconController;
Jorim Jaggi708f7722014-08-20 17:30:38 +020048import com.android.systemui.statusbar.policy.BatteryController;
Jason Monkaa573e92017-01-27 17:00:29 -050049import com.android.systemui.statusbar.policy.BatteryController.BatteryStateChangeCallback;
50import com.android.systemui.statusbar.policy.ConfigurationController;
51import com.android.systemui.statusbar.policy.ConfigurationController.ConfigurationListener;
52import com.android.systemui.statusbar.policy.DarkIconDispatcher;
53import com.android.systemui.statusbar.policy.DarkIconDispatcher.DarkReceiver;
Jason Monkf8c2f7b2017-09-06 09:22:29 -040054import com.android.systemui.statusbar.policy.IconLogger;
Jason Monk3e189872016-01-12 09:10:34 -050055import com.android.systemui.tuner.TunerService;
Jason Monkaa573e92017-01-27 17:00:29 -050056import com.android.systemui.tuner.TunerService.Tunable;
Charles He6a79b0d2017-09-18 09:50:58 +010057import com.android.systemui.util.Utils.DisableStateTracker;
Jorim Jaggi708f7722014-08-20 17:30:38 +020058
Dan Sandler055bb612017-02-08 16:21:49 -080059import java.text.NumberFormat;
60
61public class BatteryMeterView extends LinearLayout implements
Jason Monkaa573e92017-01-27 17:00:29 -050062 BatteryStateChangeCallback, Tunable, DarkReceiver, ConfigurationListener {
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040063
Dan Sandler055bb612017-02-08 16:21:49 -080064 private final BatteryMeterDrawableBase mDrawable;
Jason Monk3e189872016-01-12 09:10:34 -050065 private final String mSlotBattery;
Dan Sandler055bb612017-02-08 16:21:49 -080066 private final ImageView mBatteryIconView;
Jason Monka6f1db3a2017-08-30 19:18:00 -040067 private final CurrentUserTracker mUserTracker;
Dan Sandler055bb612017-02-08 16:21:49 -080068 private TextView mBatteryPercentView;
69
Jorim Jaggi708f7722014-08-20 17:30:38 +020070 private BatteryController mBatteryController;
Dan Sandler055bb612017-02-08 16:21:49 -080071 private SettingObserver mSettingObserver;
72 private int mTextColor;
73 private int mLevel;
Jason Monkf13413e2017-02-15 15:49:32 -050074 private boolean mForceShowPercent;
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040075
Jason Monk9a376bc2017-05-10 09:52:10 -040076 private int mDarkModeBackgroundColor;
77 private int mDarkModeFillColor;
78
79 private int mLightModeBackgroundColor;
80 private int mLightModeFillColor;
Lucas Dupin987f1932017-05-13 21:02:52 -070081 private float mDarkIntensity;
Jason Monka6f1db3a2017-08-30 19:18:00 -040082 private int mUser;
Jason Monk9a376bc2017-05-10 09:52:10 -040083
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040084 public BatteryMeterView(Context context) {
85 this(context, null, 0);
86 }
87
88 public BatteryMeterView(Context context, AttributeSet attrs) {
89 this(context, attrs, 0);
90 }
91
92 public BatteryMeterView(Context context, AttributeSet attrs, int defStyle) {
93 super(context, attrs, defStyle);
94
Dan Sandler055bb612017-02-08 16:21:49 -080095 setOrientation(LinearLayout.HORIZONTAL);
96 setGravity(Gravity.CENTER_VERTICAL | Gravity.START);
97
John Spurlock29786fc2014-02-04 17:55:47 -050098 TypedArray atts = context.obtainStyledAttributes(attrs, R.styleable.BatteryMeterView,
99 defStyle, 0);
100 final int frameColor = atts.getColor(R.styleable.BatteryMeterView_frameColor,
Daniel Nishi8af93cb2017-05-15 14:37:20 -0700101 context.getColor(R.color.meter_background_color));
Dan Sandler055bb612017-02-08 16:21:49 -0800102 mDrawable = new BatteryMeterDrawableBase(context, frameColor);
John Spurlock29786fc2014-02-04 17:55:47 -0500103 atts.recycle();
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -0400104
Dan Sandler055bb612017-02-08 16:21:49 -0800105 mSettingObserver = new SettingObserver(new Handler(context.getMainLooper()));
106
Charles He6a79b0d2017-09-18 09:50:58 +0100107 addOnAttachStateChangeListener(
108 new DisableStateTracker(DISABLE_NONE, DISABLE2_SYSTEM_ICONS));
109
Jason Monk3e189872016-01-12 09:10:34 -0500110 mSlotBattery = context.getString(
111 com.android.internal.R.string.status_bar_battery);
Dan Sandler055bb612017-02-08 16:21:49 -0800112 mBatteryIconView = new ImageView(context);
113 mBatteryIconView.setImageDrawable(mDrawable);
114 final MarginLayoutParams mlp = new MarginLayoutParams(
115 getResources().getDimensionPixelSize(R.dimen.status_bar_battery_icon_width),
116 getResources().getDimensionPixelSize(R.dimen.status_bar_battery_icon_height));
117 mlp.setMargins(0, 0, 0,
118 getResources().getDimensionPixelOffset(R.dimen.battery_margin_bottom));
119 addView(mBatteryIconView, mlp);
120
121 updateShowPercent();
Evan Lairdef160f22018-01-29 14:08:45 -0500122 setColorsFromContext(context);
Jason Monkd866b8a2017-03-29 15:30:44 -0400123 // Init to not dark at all.
124 onDarkChanged(new Rect(), 0, DarkIconDispatcher.DEFAULT_ICON_TINT);
Evan Lairdef160f22018-01-29 14:08:45 -0500125
Jason Monka6f1db3a2017-08-30 19:18:00 -0400126 mUserTracker = new CurrentUserTracker(mContext) {
127 @Override
128 public void onUserSwitched(int newUserId) {
129 mUser = newUserId;
130 getContext().getContentResolver().unregisterContentObserver(mSettingObserver);
131 getContext().getContentResolver().registerContentObserver(
132 Settings.System.getUriFor(SHOW_BATTERY_PERCENT), false, mSettingObserver,
133 newUserId);
134 }
135 };
Dan Sandler055bb612017-02-08 16:21:49 -0800136 }
137
Dan Sandlerdf14c202017-02-21 14:51:11 -0500138 public void setForceShowPercent(boolean show) {
139 mForceShowPercent = show;
Jason Monkf13413e2017-02-15 15:49:32 -0500140 updateShowPercent();
141 }
142
Evan Lairdef160f22018-01-29 14:08:45 -0500143 public void setColorsFromContext(Context context) {
144 if (context == null) {
145 return;
146 }
147
148 Context dualToneDarkTheme = new ContextThemeWrapper(context,
149 Utils.getThemeAttr(context, R.attr.darkIconTheme));
150 Context dualToneLightTheme = new ContextThemeWrapper(context,
151 Utils.getThemeAttr(context, R.attr.lightIconTheme));
152 mDarkModeBackgroundColor = Utils.getColorAttr(dualToneDarkTheme, R.attr.backgroundColor);
153 mDarkModeFillColor = Utils.getColorAttr(dualToneDarkTheme, R.attr.fillColor);
154 mLightModeBackgroundColor = Utils.getColorAttr(dualToneLightTheme, R.attr.backgroundColor);
155 mLightModeFillColor = Utils.getColorAttr(dualToneLightTheme, R.attr.fillColor);
156 }
157
Jorim Jaggi0d266892014-07-28 14:49:13 +0200158 @Override
159 public boolean hasOverlappingRendering() {
160 return false;
161 }
162
Jason Monkabe19742015-09-29 09:47:06 -0400163 @Override
Jason Monk3e189872016-01-12 09:10:34 -0500164 public void onTuningChanged(String key, String newValue) {
165 if (StatusBarIconController.ICON_BLACKLIST.equals(key)) {
166 ArraySet<String> icons = StatusBarIconController.getIconBlacklist(newValue);
Jason Monkf8c2f7b2017-09-06 09:22:29 -0400167 boolean hidden = icons.contains(mSlotBattery);
168 Dependency.get(IconLogger.class).onIconVisibility(mSlotBattery, !hidden);
169 setVisibility(hidden ? View.GONE : View.VISIBLE);
Jason Monk3e189872016-01-12 09:10:34 -0500170 }
171 }
172
173 @Override
Jason Monkabe19742015-09-29 09:47:06 -0400174 public void onAttachedToWindow() {
175 super.onAttachedToWindow();
Jason Monk9c7844c2017-01-18 15:21:53 -0500176 mBatteryController = Dependency.get(BatteryController.class);
Jason Monk9c7844c2017-01-18 15:21:53 -0500177 mBatteryController.addCallback(this);
Jason Monka6f1db3a2017-08-30 19:18:00 -0400178 mUser = ActivityManager.getCurrentUser();
Dan Sandler055bb612017-02-08 16:21:49 -0800179 getContext().getContentResolver().registerContentObserver(
Jason Monka6f1db3a2017-08-30 19:18:00 -0400180 Settings.System.getUriFor(SHOW_BATTERY_PERCENT), false, mSettingObserver, mUser);
Dan Sandler055bb612017-02-08 16:21:49 -0800181 updateShowPercent();
Rohan Shah8a0d3fc2018-03-29 18:28:07 +0000182 Dependency.get(TunerService.class).addTunable(this, StatusBarIconController.ICON_BLACKLIST);
Jason Monkaa573e92017-01-27 17:00:29 -0500183 Dependency.get(ConfigurationController.class).addCallback(this);
Jason Monka6f1db3a2017-08-30 19:18:00 -0400184 mUserTracker.startTracking();
Jason Monkabe19742015-09-29 09:47:06 -0400185 }
John Spurlock3c875662013-08-31 15:07:25 -0400186
187 @Override
Jason Monkabe19742015-09-29 09:47:06 -0400188 public void onDetachedFromWindow() {
189 super.onDetachedFromWindow();
Jason Monka6f1db3a2017-08-30 19:18:00 -0400190 mUserTracker.stopTracking();
Jason Monk88529052016-11-04 13:29:58 -0400191 mBatteryController.removeCallback(this);
Dan Sandler055bb612017-02-08 16:21:49 -0800192 getContext().getContentResolver().unregisterContentObserver(mSettingObserver);
Jason Monkde850bb2017-02-01 19:26:30 -0500193 Dependency.get(TunerService.class).removeTunable(this);
Jason Monkaa573e92017-01-27 17:00:29 -0500194 Dependency.get(ConfigurationController.class).removeCallback(this);
John Spurlock3c875662013-08-31 15:07:25 -0400195 }
John Spurlockf40d08f2015-05-29 10:48:22 -0400196
Jason Monkabe19742015-09-29 09:47:06 -0400197 @Override
198 public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
Dan Sandler055bb612017-02-08 16:21:49 -0800199 mDrawable.setBatteryLevel(level);
jackqdyuleiff5bd942017-04-04 10:54:21 -0700200 mDrawable.setCharging(pluggedIn);
Dan Sandler055bb612017-02-08 16:21:49 -0800201 mLevel = level;
202 updatePercentText();
Jason Monkabe19742015-09-29 09:47:06 -0400203 setContentDescription(
Adrian Roos70dcf832016-04-20 15:51:42 -0700204 getContext().getString(charging ? R.string.accessibility_battery_level_charging
205 : R.string.accessibility_battery_level, level));
John Spurlockf40d08f2015-05-29 10:48:22 -0400206 }
207
Jason Monkabe19742015-09-29 09:47:06 -0400208 @Override
Jason Monkc06fbb12016-01-08 14:12:18 -0500209 public void onPowerSaveChanged(boolean isPowerSave) {
Dan Sandler055bb612017-02-08 16:21:49 -0800210 mDrawable.setPowerSave(isPowerSave);
211 }
John Spurlockf40d08f2015-05-29 10:48:22 -0400212
Dan Sandler055bb612017-02-08 16:21:49 -0800213 private TextView loadPercentView() {
214 return (TextView) LayoutInflater.from(getContext())
215 .inflate(R.layout.battery_percentage_view, null);
216 }
217
218 private void updatePercentText() {
219 if (mBatteryPercentView != null) {
220 mBatteryPercentView.setText(
Jason Monk9a376bc2017-05-10 09:52:10 -0400221 NumberFormat.getPercentInstance().format(mLevel / 100f));
Dan Sandler055bb612017-02-08 16:21:49 -0800222 }
223 }
224
225 private void updateShowPercent() {
226 final boolean showing = mBatteryPercentView != null;
Jason Monka6f1db3a2017-08-30 19:18:00 -0400227 if (0 != Settings.System.getIntForUser(getContext().getContentResolver(),
228 SHOW_BATTERY_PERCENT, 0, mUser) || mForceShowPercent) {
Dan Sandler055bb612017-02-08 16:21:49 -0800229 if (!showing) {
230 mBatteryPercentView = loadPercentView();
231 if (mTextColor != 0) mBatteryPercentView.setTextColor(mTextColor);
232 updatePercentText();
233 addView(mBatteryPercentView,
Dan Sandler055bb612017-02-08 16:21:49 -0800234 new ViewGroup.LayoutParams(
235 LayoutParams.WRAP_CONTENT,
236 LayoutParams.MATCH_PARENT));
237 }
238 } else {
239 if (showing) {
240 removeView(mBatteryPercentView);
241 mBatteryPercentView = null;
242 }
243 }
John Spurlockf40d08f2015-05-29 10:48:22 -0400244 }
245
Jason Monkaa573e92017-01-27 17:00:29 -0500246 @Override
247 public void onDensityOrFontScaleChanged() {
248 scaleBatteryMeterViews();
249 }
250
251 /**
252 * Looks up the scale factor for status bar icons and scales the battery view by that amount.
253 */
254 private void scaleBatteryMeterViews() {
255 Resources res = getContext().getResources();
256 TypedValue typedValue = new TypedValue();
257
258 res.getValue(R.dimen.status_bar_icon_scale_factor, typedValue, true);
259 float iconScaleFactor = typedValue.getFloat();
260
261 int batteryHeight = res.getDimensionPixelSize(R.dimen.status_bar_battery_icon_height);
262 int batteryWidth = res.getDimensionPixelSize(R.dimen.status_bar_battery_icon_width);
263 int marginBottom = res.getDimensionPixelSize(R.dimen.battery_margin_bottom);
264
265 LinearLayout.LayoutParams scaledLayoutParams = new LinearLayout.LayoutParams(
266 (int) (batteryWidth * iconScaleFactor), (int) (batteryHeight * iconScaleFactor));
Dan Sandler055bb612017-02-08 16:21:49 -0800267 scaledLayoutParams.setMargins(0, 0, 0, marginBottom);
Jason Monkaa573e92017-01-27 17:00:29 -0500268
Dan Sandler055bb612017-02-08 16:21:49 -0800269 mBatteryIconView.setLayoutParams(scaledLayoutParams);
Evan Lairdecc93f22017-06-16 09:57:29 -0400270 FontSizeUtils.updateFontSize(mBatteryPercentView, R.dimen.qs_time_expanded_size);
Jason Monkaa573e92017-01-27 17:00:29 -0500271 }
272
273 @Override
274 public void onDarkChanged(Rect area, float darkIntensity, int tint) {
Lucas Dupin987f1932017-05-13 21:02:52 -0700275 mDarkIntensity = darkIntensity;
Jason Monk9a376bc2017-05-10 09:52:10 -0400276 float intensity = DarkIconDispatcher.isInArea(area, this) ? darkIntensity : 0;
Rohan Shah8a0d3fc2018-03-29 18:28:07 +0000277 int foreground = getColorForDarkIntensity(intensity, mLightModeFillColor,
278 mDarkModeFillColor);
279 int background = getColorForDarkIntensity(intensity, mLightModeBackgroundColor,
280 mDarkModeBackgroundColor);
281 mDrawable.setColors(foreground, background);
282 setTextColor(foreground);
Dan Sandler055bb612017-02-08 16:21:49 -0800283 }
284
Rohan Shah8a0d3fc2018-03-29 18:28:07 +0000285 public void setTextColor(int color) {
286 mTextColor = color;
Dan Sandler055bb612017-02-08 16:21:49 -0800287 if (mBatteryPercentView != null) {
Rohan Shah8a0d3fc2018-03-29 18:28:07 +0000288 mBatteryPercentView.setTextColor(color);
Dan Sandler055bb612017-02-08 16:21:49 -0800289 }
Jason Monkabe19742015-09-29 09:47:06 -0400290 }
Jason Monk32508852017-01-18 09:17:13 -0500291
Lucas Dupin987f1932017-05-13 21:02:52 -0700292 public void setFillColor(int color) {
293 if (mLightModeFillColor == color) {
294 return;
295 }
296 mLightModeFillColor = color;
297 onDarkChanged(new Rect(), mDarkIntensity, DarkIconDispatcher.DEFAULT_ICON_TINT);
298 }
299
Jason Monk9a376bc2017-05-10 09:52:10 -0400300 private int getColorForDarkIntensity(float darkIntensity, int lightColor, int darkColor) {
301 return (int) ArgbEvaluator.getInstance().evaluate(darkIntensity, lightColor, darkColor);
Jason Monk32508852017-01-18 09:17:13 -0500302 }
Dan Sandler055bb612017-02-08 16:21:49 -0800303
304 private final class SettingObserver extends ContentObserver {
305 public SettingObserver(Handler handler) {
306 super(handler);
307 }
308
309 @Override
310 public void onChange(boolean selfChange, Uri uri) {
311 super.onChange(selfChange, uri);
312 updateShowPercent();
313 }
314 }
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -0400315}