blob: 592b6039d69d3975d26c42564a75e636db9fac18 [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 Monk8e4e4cb2018-12-04 11:10:24 -050022import static com.android.systemui.util.SysuiLifecycle.viewAttachLifecycle;
23
Evan Laird4bf21df2018-10-22 14:24:32 -040024import static java.lang.annotation.RetentionPolicy.SOURCE;
25
Jason Monk9a376bc2017-05-10 09:52:10 -040026import android.animation.ArgbEvaluator;
Evan Laird4bf21df2018-10-22 14:24:32 -040027import android.annotation.IntDef;
Jason Monka6f1db3a2017-08-30 19:18:00 -040028import android.app.ActivityManager;
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040029import android.content.Context;
Jason Monkaa573e92017-01-27 17:00:29 -050030import android.content.res.Resources;
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040031import android.content.res.TypedArray;
Dan Sandler055bb612017-02-08 16:21:49 -080032import android.database.ContentObserver;
Jason Monk9a376bc2017-05-10 09:52:10 -040033import android.graphics.Rect;
Dan Sandler055bb612017-02-08 16:21:49 -080034import android.net.Uri;
35import android.os.Handler;
36import android.provider.Settings;
Jason Monk9a376bc2017-05-10 09:52:10 -040037import android.util.ArraySet;
38import android.util.AttributeSet;
39import android.util.TypedValue;
40import android.view.ContextThemeWrapper;
Dan Sandler055bb612017-02-08 16:21:49 -080041import android.view.Gravity;
42import android.view.LayoutInflater;
Dan Sandler055bb612017-02-08 16:21:49 -080043import android.view.ViewGroup;
Jason Monkabe19742015-09-29 09:47:06 -040044import android.widget.ImageView;
Jason Monkaa573e92017-01-27 17:00:29 -050045import android.widget.LinearLayout;
Dan Sandler055bb612017-02-08 16:21:49 -080046import android.widget.TextView;
Jason Monk9a376bc2017-05-10 09:52:10 -040047
48import com.android.settingslib.Utils;
Evan Laird2259da42019-02-08 16:48:53 -050049import com.android.settingslib.graph.ThemedBatteryDrawable;
Beverly1be62f42018-12-19 17:17:48 -050050import com.android.systemui.plugins.DarkIconDispatcher;
51import com.android.systemui.plugins.DarkIconDispatcher.DarkReceiver;
Jason Monka6f1db3a2017-08-30 19:18:00 -040052import com.android.systemui.settings.CurrentUserTracker;
Jason Monk3e189872016-01-12 09:10:34 -050053import com.android.systemui.statusbar.phone.StatusBarIconController;
Jorim Jaggi708f7722014-08-20 17:30:38 +020054import com.android.systemui.statusbar.policy.BatteryController;
Jason Monkaa573e92017-01-27 17:00:29 -050055import com.android.systemui.statusbar.policy.BatteryController.BatteryStateChangeCallback;
56import com.android.systemui.statusbar.policy.ConfigurationController;
57import com.android.systemui.statusbar.policy.ConfigurationController.ConfigurationListener;
Jason Monk3e189872016-01-12 09:10:34 -050058import com.android.systemui.tuner.TunerService;
Jason Monkaa573e92017-01-27 17:00:29 -050059import com.android.systemui.tuner.TunerService.Tunable;
Charles He6a79b0d2017-09-18 09:50:58 +010060import com.android.systemui.util.Utils.DisableStateTracker;
Jorim Jaggi708f7722014-08-20 17:30:38 +020061
Lucas Dupinc510d412018-06-12 13:08:23 -070062import java.io.FileDescriptor;
63import java.io.PrintWriter;
Evan Laird4bf21df2018-10-22 14:24:32 -040064import java.lang.annotation.Retention;
Dan Sandler055bb612017-02-08 16:21:49 -080065import java.text.NumberFormat;
66
67public class BatteryMeterView extends LinearLayout implements
Jason Monkaa573e92017-01-27 17:00:29 -050068 BatteryStateChangeCallback, Tunable, DarkReceiver, ConfigurationListener {
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040069
Evan Laird4bf21df2018-10-22 14:24:32 -040070
71 @Retention(SOURCE)
Evan Lairda5a73c52019-01-11 13:36:32 -050072 @IntDef({MODE_DEFAULT, MODE_ON, MODE_OFF, MODE_ESTIMATE})
Evan Laird4bf21df2018-10-22 14:24:32 -040073 public @interface BatteryPercentMode {}
74 public static final int MODE_DEFAULT = 0;
75 public static final int MODE_ON = 1;
76 public static final int MODE_OFF = 2;
Evan Lairda5a73c52019-01-11 13:36:32 -050077 public static final int MODE_ESTIMATE = 3;
Evan Laird4bf21df2018-10-22 14:24:32 -040078
Evan Laird2259da42019-02-08 16:48:53 -050079 private final ThemedBatteryDrawable mDrawable;
Jason Monk3e189872016-01-12 09:10:34 -050080 private final String mSlotBattery;
Dan Sandler055bb612017-02-08 16:21:49 -080081 private final ImageView mBatteryIconView;
Jason Monka6f1db3a2017-08-30 19:18:00 -040082 private final CurrentUserTracker mUserTracker;
Dan Sandler055bb612017-02-08 16:21:49 -080083 private TextView mBatteryPercentView;
84
Jorim Jaggi708f7722014-08-20 17:30:38 +020085 private BatteryController mBatteryController;
Dan Sandler055bb612017-02-08 16:21:49 -080086 private SettingObserver mSettingObserver;
87 private int mTextColor;
88 private int mLevel;
Evan Laird4bf21df2018-10-22 14:24:32 -040089 private int mShowPercentMode = MODE_DEFAULT;
Jason Monkf13413e2017-02-15 15:49:32 -050090 private boolean mForceShowPercent;
Evan Laird54ff81b2018-06-13 20:08:17 -040091 private boolean mShowPercentAvailable;
Evan Laird698839b2018-12-05 15:49:12 -050092 // Some places may need to show the battery conditionally, and not obey the tuner
93 private boolean mIgnoreTunerUpdates;
94 private boolean mIsSubscribedForTunerUpdates;
Evan Lairda5a73c52019-01-11 13:36:32 -050095 private boolean mCharging;
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040096
Evan Laird2259da42019-02-08 16:48:53 -050097 private int mDarkModeSingleToneColor;
Jason Monk9a376bc2017-05-10 09:52:10 -040098 private int mDarkModeBackgroundColor;
99 private int mDarkModeFillColor;
100
Evan Laird2259da42019-02-08 16:48:53 -0500101 private int mLightModeSingleToneColor;
Jason Monk9a376bc2017-05-10 09:52:10 -0400102 private int mLightModeBackgroundColor;
103 private int mLightModeFillColor;
Jason Monka6f1db3a2017-08-30 19:18:00 -0400104 private int mUser;
Jason Monk9a376bc2017-05-10 09:52:10 -0400105
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000106 /**
107 * Whether we should use colors that adapt based on wallpaper/the scrim behind quick settings.
108 */
109 private boolean mUseWallpaperTextColors;
110
Evan Laird2259da42019-02-08 16:48:53 -0500111 private int mNonAdaptedSingleToneColor;
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000112 private int mNonAdaptedForegroundColor;
113 private int mNonAdaptedBackgroundColor;
114
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -0400115 public BatteryMeterView(Context context) {
116 this(context, null, 0);
117 }
118
119 public BatteryMeterView(Context context, AttributeSet attrs) {
120 this(context, attrs, 0);
121 }
122
123 public BatteryMeterView(Context context, AttributeSet attrs, int defStyle) {
124 super(context, attrs, defStyle);
125
Dan Sandler055bb612017-02-08 16:21:49 -0800126 setOrientation(LinearLayout.HORIZONTAL);
127 setGravity(Gravity.CENTER_VERTICAL | Gravity.START);
128
John Spurlock29786fc2014-02-04 17:55:47 -0500129 TypedArray atts = context.obtainStyledAttributes(attrs, R.styleable.BatteryMeterView,
130 defStyle, 0);
131 final int frameColor = atts.getColor(R.styleable.BatteryMeterView_frameColor,
Daniel Nishi8af93cb2017-05-15 14:37:20 -0700132 context.getColor(R.color.meter_background_color));
Evan Laird2259da42019-02-08 16:48:53 -0500133 mDrawable = new ThemedBatteryDrawable(context, frameColor);
John Spurlock29786fc2014-02-04 17:55:47 -0500134 atts.recycle();
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -0400135
Dan Sandler055bb612017-02-08 16:21:49 -0800136 mSettingObserver = new SettingObserver(new Handler(context.getMainLooper()));
Evan Laird54ff81b2018-06-13 20:08:17 -0400137 mShowPercentAvailable = context.getResources().getBoolean(
138 com.android.internal.R.bool.config_battery_percentage_setting_available);
139
Dan Sandler055bb612017-02-08 16:21:49 -0800140
Charles He6a79b0d2017-09-18 09:50:58 +0100141 addOnAttachStateChangeListener(
142 new DisableStateTracker(DISABLE_NONE, DISABLE2_SYSTEM_ICONS));
143
Jason Monk3e189872016-01-12 09:10:34 -0500144 mSlotBattery = context.getString(
145 com.android.internal.R.string.status_bar_battery);
Dan Sandler055bb612017-02-08 16:21:49 -0800146 mBatteryIconView = new ImageView(context);
147 mBatteryIconView.setImageDrawable(mDrawable);
148 final MarginLayoutParams mlp = new MarginLayoutParams(
149 getResources().getDimensionPixelSize(R.dimen.status_bar_battery_icon_width),
150 getResources().getDimensionPixelSize(R.dimen.status_bar_battery_icon_height));
151 mlp.setMargins(0, 0, 0,
152 getResources().getDimensionPixelOffset(R.dimen.battery_margin_bottom));
153 addView(mBatteryIconView, mlp);
154
155 updateShowPercent();
Evan Lairdef160f22018-01-29 14:08:45 -0500156 setColorsFromContext(context);
Jason Monkd866b8a2017-03-29 15:30:44 -0400157 // Init to not dark at all.
158 onDarkChanged(new Rect(), 0, DarkIconDispatcher.DEFAULT_ICON_TINT);
Evan Lairdef160f22018-01-29 14:08:45 -0500159
Jason Monka6f1db3a2017-08-30 19:18:00 -0400160 mUserTracker = new CurrentUserTracker(mContext) {
161 @Override
162 public void onUserSwitched(int newUserId) {
163 mUser = newUserId;
164 getContext().getContentResolver().unregisterContentObserver(mSettingObserver);
165 getContext().getContentResolver().registerContentObserver(
166 Settings.System.getUriFor(SHOW_BATTERY_PERCENT), false, mSettingObserver,
167 newUserId);
Evan Laird6ead1e32018-06-19 13:39:05 -0400168 updateShowPercent();
Jason Monka6f1db3a2017-08-30 19:18:00 -0400169 }
170 };
Evan Laird39ea8102018-05-18 19:49:07 -0400171
172 setClipChildren(false);
173 setClipToPadding(false);
Jason Monk8e4e4cb2018-12-04 11:10:24 -0500174 Dependency.get(ConfigurationController.class).observe(viewAttachLifecycle(this), this);
Evan Laird2259da42019-02-08 16:48:53 -0500175
176 // Needed for PorderDuff.Mode.CLEAR operations to work properly, but redraws don't happen
177 // enough to justify a hardware layer.
178 setLayerType(LAYER_TYPE_SOFTWARE, null);
Dan Sandler055bb612017-02-08 16:21:49 -0800179 }
180
Dan Sandlerdf14c202017-02-21 14:51:11 -0500181 public void setForceShowPercent(boolean show) {
Evan Laird4bf21df2018-10-22 14:24:32 -0400182 setPercentShowMode(show ? MODE_ON : MODE_DEFAULT);
183 }
184
185 /**
186 * Force a particular mode of showing percent
187 *
188 * 0 - No preference
189 * 1 - Force on
190 * 2 - Force off
191 * @param mode desired mode (none, on, off)
192 */
193 public void setPercentShowMode(@BatteryPercentMode int mode) {
194 mShowPercentMode = mode;
Jason Monkf13413e2017-02-15 15:49:32 -0500195 updateShowPercent();
196 }
197
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000198 /**
Evan Laird698839b2018-12-05 15:49:12 -0500199 * Set {@code true} to turn off BatteryMeterView's subscribing to the tuner for updates, and
200 * thus avoid it controlling its own visibility
201 *
202 * @param ignore whether to ignore the tuner or not
203 */
204 public void setIgnoreTunerUpdates(boolean ignore) {
205 mIgnoreTunerUpdates = ignore;
206 updateTunerSubscription();
207 }
208
209 private void updateTunerSubscription() {
210 if (mIgnoreTunerUpdates) {
211 unsubscribeFromTunerUpdates();
212 } else {
213 subscribeForTunerUpdates();
214 }
215 }
216
217 private void subscribeForTunerUpdates() {
218 if (mIsSubscribedForTunerUpdates || mIgnoreTunerUpdates) {
219 return;
220 }
221
222 Dependency.get(TunerService.class)
223 .addTunable(this, StatusBarIconController.ICON_BLACKLIST);
224 mIsSubscribedForTunerUpdates = true;
225 }
226
227 private void unsubscribeFromTunerUpdates() {
228 if (!mIsSubscribedForTunerUpdates) {
229 return;
230 }
231
232 Dependency.get(TunerService.class).removeTunable(this);
233 mIsSubscribedForTunerUpdates = false;
234 }
235
236 /**
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000237 * Sets whether the battery meter view uses the wallpaperTextColor. If we're not using it, we'll
238 * revert back to dark-mode-based/tinted colors.
239 *
240 * @param shouldUseWallpaperTextColor whether we should use wallpaperTextColor for all
241 * components
242 */
243 public void useWallpaperTextColor(boolean shouldUseWallpaperTextColor) {
244 if (shouldUseWallpaperTextColor == mUseWallpaperTextColors) {
245 return;
246 }
247
248 mUseWallpaperTextColors = shouldUseWallpaperTextColor;
249
250 if (mUseWallpaperTextColors) {
251 updateColors(
Jason Changb4e879d2018-04-11 11:17:58 +0800252 Utils.getColorAttrDefaultColor(mContext, R.attr.wallpaperTextColor),
Evan Laird2259da42019-02-08 16:48:53 -0500253 Utils.getColorAttrDefaultColor(mContext, R.attr.wallpaperTextColorSecondary),
254 Utils.getColorAttrDefaultColor(mContext, R.attr.wallpaperTextColor));
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000255 } else {
Evan Laird2259da42019-02-08 16:48:53 -0500256 updateColors(mNonAdaptedForegroundColor, mNonAdaptedBackgroundColor,
257 mNonAdaptedSingleToneColor);
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000258 }
259 }
260
Evan Lairdef160f22018-01-29 14:08:45 -0500261 public void setColorsFromContext(Context context) {
262 if (context == null) {
263 return;
264 }
265
266 Context dualToneDarkTheme = new ContextThemeWrapper(context,
267 Utils.getThemeAttr(context, R.attr.darkIconTheme));
268 Context dualToneLightTheme = new ContextThemeWrapper(context,
269 Utils.getThemeAttr(context, R.attr.lightIconTheme));
Evan Laird2259da42019-02-08 16:48:53 -0500270 mDarkModeSingleToneColor = Utils.getColorAttrDefaultColor(dualToneDarkTheme,
271 R.attr.singleToneColor);
Jason Changb4e879d2018-04-11 11:17:58 +0800272 mDarkModeBackgroundColor = Utils.getColorAttrDefaultColor(dualToneDarkTheme,
273 R.attr.backgroundColor);
274 mDarkModeFillColor = Utils.getColorAttrDefaultColor(dualToneDarkTheme,
275 R.attr.fillColor);
Evan Laird2259da42019-02-08 16:48:53 -0500276 mLightModeSingleToneColor = Utils.getColorAttrDefaultColor(dualToneLightTheme,
277 R.attr.singleToneColor);
Jason Changb4e879d2018-04-11 11:17:58 +0800278 mLightModeBackgroundColor = Utils.getColorAttrDefaultColor(dualToneLightTheme,
279 R.attr.backgroundColor);
280 mLightModeFillColor = Utils.getColorAttrDefaultColor(dualToneLightTheme, R.attr.fillColor);
Evan Lairdef160f22018-01-29 14:08:45 -0500281 }
282
Jorim Jaggi0d266892014-07-28 14:49:13 +0200283 @Override
284 public boolean hasOverlappingRendering() {
285 return false;
286 }
287
Jason Monkabe19742015-09-29 09:47:06 -0400288 @Override
Jason Monk3e189872016-01-12 09:10:34 -0500289 public void onTuningChanged(String key, String newValue) {
290 if (StatusBarIconController.ICON_BLACKLIST.equals(key)) {
291 ArraySet<String> icons = StatusBarIconController.getIconBlacklist(newValue);
Jason Monk3e189872016-01-12 09:10:34 -0500292 }
293 }
294
295 @Override
Jason Monkabe19742015-09-29 09:47:06 -0400296 public void onAttachedToWindow() {
297 super.onAttachedToWindow();
Jason Monk9c7844c2017-01-18 15:21:53 -0500298 mBatteryController = Dependency.get(BatteryController.class);
Jason Monk9c7844c2017-01-18 15:21:53 -0500299 mBatteryController.addCallback(this);
Jason Monka6f1db3a2017-08-30 19:18:00 -0400300 mUser = ActivityManager.getCurrentUser();
Dan Sandler055bb612017-02-08 16:21:49 -0800301 getContext().getContentResolver().registerContentObserver(
Jason Monka6f1db3a2017-08-30 19:18:00 -0400302 Settings.System.getUriFor(SHOW_BATTERY_PERCENT), false, mSettingObserver, mUser);
Dan Sandler055bb612017-02-08 16:21:49 -0800303 updateShowPercent();
Evan Laird698839b2018-12-05 15:49:12 -0500304 subscribeForTunerUpdates();
Jason Monka6f1db3a2017-08-30 19:18:00 -0400305 mUserTracker.startTracking();
Jason Monkabe19742015-09-29 09:47:06 -0400306 }
John Spurlock3c875662013-08-31 15:07:25 -0400307
308 @Override
Jason Monkabe19742015-09-29 09:47:06 -0400309 public void onDetachedFromWindow() {
310 super.onDetachedFromWindow();
Jason Monka6f1db3a2017-08-30 19:18:00 -0400311 mUserTracker.stopTracking();
Jason Monk88529052016-11-04 13:29:58 -0400312 mBatteryController.removeCallback(this);
Dan Sandler055bb612017-02-08 16:21:49 -0800313 getContext().getContentResolver().unregisterContentObserver(mSettingObserver);
Evan Laird698839b2018-12-05 15:49:12 -0500314 unsubscribeFromTunerUpdates();
John Spurlock3c875662013-08-31 15:07:25 -0400315 }
John Spurlockf40d08f2015-05-29 10:48:22 -0400316
Jason Monkabe19742015-09-29 09:47:06 -0400317 @Override
318 public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
jackqdyuleiff5bd942017-04-04 10:54:21 -0700319 mDrawable.setCharging(pluggedIn);
Evan Laird2259da42019-02-08 16:48:53 -0500320 mDrawable.setBatteryLevel(level);
Evan Lairda5a73c52019-01-11 13:36:32 -0500321 mCharging = pluggedIn;
Dan Sandler055bb612017-02-08 16:21:49 -0800322 mLevel = level;
323 updatePercentText();
Jason Monkabe19742015-09-29 09:47:06 -0400324 setContentDescription(
Adrian Roos70dcf832016-04-20 15:51:42 -0700325 getContext().getString(charging ? R.string.accessibility_battery_level_charging
326 : R.string.accessibility_battery_level, level));
John Spurlockf40d08f2015-05-29 10:48:22 -0400327 }
328
Jason Monkabe19742015-09-29 09:47:06 -0400329 @Override
Jason Monkc06fbb12016-01-08 14:12:18 -0500330 public void onPowerSaveChanged(boolean isPowerSave) {
Evan Laird2259da42019-02-08 16:48:53 -0500331 mDrawable.setPowerSaveEnabled(isPowerSave);
Dan Sandler055bb612017-02-08 16:21:49 -0800332 }
John Spurlockf40d08f2015-05-29 10:48:22 -0400333
Dan Sandler055bb612017-02-08 16:21:49 -0800334 private TextView loadPercentView() {
335 return (TextView) LayoutInflater.from(getContext())
336 .inflate(R.layout.battery_percentage_view, null);
337 }
338
Fabian Kozynski648e5552018-12-18 16:37:08 -0500339 /**
340 * Updates percent view by removing old one and reinflating if necessary
341 */
342 public void updatePercentView() {
343 if (mBatteryPercentView != null) {
344 removeView(mBatteryPercentView);
345 mBatteryPercentView = null;
346 }
347 updateShowPercent();
348 }
349
Dan Sandler055bb612017-02-08 16:21:49 -0800350 private void updatePercentText() {
Evan Lairda5a73c52019-01-11 13:36:32 -0500351 if (mBatteryController == null) {
352 return;
353 }
354
Dan Sandler055bb612017-02-08 16:21:49 -0800355 if (mBatteryPercentView != null) {
Evan Lairda5a73c52019-01-11 13:36:32 -0500356 if (mShowPercentMode == MODE_ESTIMATE && !mCharging) {
357 mBatteryController.getEstimatedTimeRemainingString((String estimate) -> {
358 mBatteryPercentView.setText(estimate);
359 });
360 } else {
361 mBatteryPercentView.setText(
362 NumberFormat.getPercentInstance().format(mLevel / 100f));
363 }
Dan Sandler055bb612017-02-08 16:21:49 -0800364 }
365 }
366
367 private void updateShowPercent() {
368 final boolean showing = mBatteryPercentView != null;
Evan Laird54ff81b2018-06-13 20:08:17 -0400369 final boolean systemSetting = 0 != Settings.System
370 .getIntForUser(getContext().getContentResolver(),
371 SHOW_BATTERY_PERCENT, 0, mUser);
372
Evan Laird4bf21df2018-10-22 14:24:32 -0400373 if ((mShowPercentAvailable && systemSetting && mShowPercentMode != MODE_OFF)
Evan Lairda5a73c52019-01-11 13:36:32 -0500374 || mShowPercentMode == MODE_ON || mShowPercentMode == MODE_ESTIMATE) {
Dan Sandler055bb612017-02-08 16:21:49 -0800375 if (!showing) {
376 mBatteryPercentView = loadPercentView();
377 if (mTextColor != 0) mBatteryPercentView.setTextColor(mTextColor);
378 updatePercentText();
379 addView(mBatteryPercentView,
Dan Sandler055bb612017-02-08 16:21:49 -0800380 new ViewGroup.LayoutParams(
381 LayoutParams.WRAP_CONTENT,
382 LayoutParams.MATCH_PARENT));
383 }
384 } else {
385 if (showing) {
386 removeView(mBatteryPercentView);
387 mBatteryPercentView = null;
388 }
389 }
John Spurlockf40d08f2015-05-29 10:48:22 -0400390 }
391
Jason Monkaa573e92017-01-27 17:00:29 -0500392 @Override
393 public void onDensityOrFontScaleChanged() {
394 scaleBatteryMeterViews();
395 }
396
397 /**
398 * Looks up the scale factor for status bar icons and scales the battery view by that amount.
399 */
400 private void scaleBatteryMeterViews() {
401 Resources res = getContext().getResources();
402 TypedValue typedValue = new TypedValue();
403
404 res.getValue(R.dimen.status_bar_icon_scale_factor, typedValue, true);
405 float iconScaleFactor = typedValue.getFloat();
406
407 int batteryHeight = res.getDimensionPixelSize(R.dimen.status_bar_battery_icon_height);
408 int batteryWidth = res.getDimensionPixelSize(R.dimen.status_bar_battery_icon_width);
409 int marginBottom = res.getDimensionPixelSize(R.dimen.battery_margin_bottom);
410
411 LinearLayout.LayoutParams scaledLayoutParams = new LinearLayout.LayoutParams(
412 (int) (batteryWidth * iconScaleFactor), (int) (batteryHeight * iconScaleFactor));
Dan Sandler055bb612017-02-08 16:21:49 -0800413 scaledLayoutParams.setMargins(0, 0, 0, marginBottom);
Jason Monkaa573e92017-01-27 17:00:29 -0500414
Dan Sandler055bb612017-02-08 16:21:49 -0800415 mBatteryIconView.setLayoutParams(scaledLayoutParams);
Evan Lairdecc93f22017-06-16 09:57:29 -0400416 FontSizeUtils.updateFontSize(mBatteryPercentView, R.dimen.qs_time_expanded_size);
Jason Monkaa573e92017-01-27 17:00:29 -0500417 }
418
419 @Override
420 public void onDarkChanged(Rect area, float darkIntensity, int tint) {
Jason Monk9a376bc2017-05-10 09:52:10 -0400421 float intensity = DarkIconDispatcher.isInArea(area, this) ? darkIntensity : 0;
Evan Laird2259da42019-02-08 16:48:53 -0500422 mNonAdaptedSingleToneColor = getColorForDarkIntensity(
423 intensity, mLightModeSingleToneColor, mDarkModeSingleToneColor);
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000424 mNonAdaptedForegroundColor = getColorForDarkIntensity(
425 intensity, mLightModeFillColor, mDarkModeFillColor);
426 mNonAdaptedBackgroundColor = getColorForDarkIntensity(
427 intensity, mLightModeBackgroundColor,mDarkModeBackgroundColor);
428
429 if (!mUseWallpaperTextColors) {
Evan Laird2259da42019-02-08 16:48:53 -0500430 updateColors(mNonAdaptedForegroundColor, mNonAdaptedBackgroundColor,
431 mNonAdaptedSingleToneColor);
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000432 }
Dan Sandler055bb612017-02-08 16:21:49 -0800433 }
434
Evan Laird2259da42019-02-08 16:48:53 -0500435 private void updateColors(int foregroundColor, int backgroundColor, int singleToneColor) {
436 mDrawable.setColors(foregroundColor, backgroundColor, singleToneColor);
437 mTextColor = singleToneColor;
Dan Sandler055bb612017-02-08 16:21:49 -0800438 if (mBatteryPercentView != null) {
Evan Laird2259da42019-02-08 16:48:53 -0500439 mBatteryPercentView.setTextColor(singleToneColor);
Dan Sandler055bb612017-02-08 16:21:49 -0800440 }
Jason Monkabe19742015-09-29 09:47:06 -0400441 }
Jason Monk32508852017-01-18 09:17:13 -0500442
Jason Monk9a376bc2017-05-10 09:52:10 -0400443 private int getColorForDarkIntensity(float darkIntensity, int lightColor, int darkColor) {
444 return (int) ArgbEvaluator.getInstance().evaluate(darkIntensity, lightColor, darkColor);
Jason Monk32508852017-01-18 09:17:13 -0500445 }
Dan Sandler055bb612017-02-08 16:21:49 -0800446
Lucas Dupinc510d412018-06-12 13:08:23 -0700447 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Evan Laird2259da42019-02-08 16:48:53 -0500448 String powerSave = mDrawable == null ? null : mDrawable.getPowerSaveEnabled() + "";
Lucas Dupinc510d412018-06-12 13:08:23 -0700449 CharSequence percent = mBatteryPercentView == null ? null : mBatteryPercentView.getText();
450 pw.println(" BatteryMeterView:");
451 pw.println(" mDrawable.getPowerSave: " + powerSave);
452 pw.println(" mBatteryPercentView.getText(): " + percent);
453 pw.println(" mTextColor: #" + Integer.toHexString(mTextColor));
454 pw.println(" mLevel: " + mLevel);
455 pw.println(" mForceShowPercent: " + mForceShowPercent);
456 }
457
Dan Sandler055bb612017-02-08 16:21:49 -0800458 private final class SettingObserver extends ContentObserver {
459 public SettingObserver(Handler handler) {
460 super(handler);
461 }
462
463 @Override
464 public void onChange(boolean selfChange, Uri uri) {
465 super.onChange(selfChange, uri);
466 updateShowPercent();
467 }
468 }
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -0400469}