blob: 9c62686564e7b4379c6eab13b6379e6790169f63 [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;
Jason Monk3e189872016-01-12 09:10:34 -050043import android.view.View;
Dan Sandler055bb612017-02-08 16:21:49 -080044import android.view.ViewGroup;
Jason Monkabe19742015-09-29 09:47:06 -040045import android.widget.ImageView;
Jason Monkaa573e92017-01-27 17:00:29 -050046import android.widget.LinearLayout;
Dan Sandler055bb612017-02-08 16:21:49 -080047import android.widget.TextView;
Jason Monk9a376bc2017-05-10 09:52:10 -040048
49import com.android.settingslib.Utils;
Dan Sandler055bb612017-02-08 16:21:49 -080050import com.android.settingslib.graph.BatteryMeterDrawableBase;
Beverly1be62f42018-12-19 17:17:48 -050051import com.android.systemui.plugins.DarkIconDispatcher;
52import com.android.systemui.plugins.DarkIconDispatcher.DarkReceiver;
Jason Monka6f1db3a2017-08-30 19:18:00 -040053import com.android.systemui.settings.CurrentUserTracker;
Jason Monk3e189872016-01-12 09:10:34 -050054import com.android.systemui.statusbar.phone.StatusBarIconController;
Jorim Jaggi708f7722014-08-20 17:30:38 +020055import com.android.systemui.statusbar.policy.BatteryController;
Jason Monkaa573e92017-01-27 17:00:29 -050056import com.android.systemui.statusbar.policy.BatteryController.BatteryStateChangeCallback;
57import com.android.systemui.statusbar.policy.ConfigurationController;
58import com.android.systemui.statusbar.policy.ConfigurationController.ConfigurationListener;
Jason Monkf8c2f7b2017-09-06 09:22:29 -040059import com.android.systemui.statusbar.policy.IconLogger;
Jason Monk3e189872016-01-12 09:10:34 -050060import com.android.systemui.tuner.TunerService;
Jason Monkaa573e92017-01-27 17:00:29 -050061import com.android.systemui.tuner.TunerService.Tunable;
Charles He6a79b0d2017-09-18 09:50:58 +010062import com.android.systemui.util.Utils.DisableStateTracker;
Jorim Jaggi708f7722014-08-20 17:30:38 +020063
Lucas Dupinc510d412018-06-12 13:08:23 -070064import java.io.FileDescriptor;
65import java.io.PrintWriter;
Evan Laird4bf21df2018-10-22 14:24:32 -040066import java.lang.annotation.Retention;
Dan Sandler055bb612017-02-08 16:21:49 -080067import java.text.NumberFormat;
68
69public class BatteryMeterView extends LinearLayout implements
Jason Monkaa573e92017-01-27 17:00:29 -050070 BatteryStateChangeCallback, Tunable, DarkReceiver, ConfigurationListener {
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040071
Evan Laird4bf21df2018-10-22 14:24:32 -040072
73 @Retention(SOURCE)
Evan Lairda5a73c52019-01-11 13:36:32 -050074 @IntDef({MODE_DEFAULT, MODE_ON, MODE_OFF, MODE_ESTIMATE})
Evan Laird4bf21df2018-10-22 14:24:32 -040075 public @interface BatteryPercentMode {}
76 public static final int MODE_DEFAULT = 0;
77 public static final int MODE_ON = 1;
78 public static final int MODE_OFF = 2;
Evan Lairda5a73c52019-01-11 13:36:32 -050079 public static final int MODE_ESTIMATE = 3;
Evan Laird4bf21df2018-10-22 14:24:32 -040080
Dan Sandler055bb612017-02-08 16:21:49 -080081 private final BatteryMeterDrawableBase mDrawable;
Jason Monk3e189872016-01-12 09:10:34 -050082 private final String mSlotBattery;
Dan Sandler055bb612017-02-08 16:21:49 -080083 private final ImageView mBatteryIconView;
Jason Monka6f1db3a2017-08-30 19:18:00 -040084 private final CurrentUserTracker mUserTracker;
Dan Sandler055bb612017-02-08 16:21:49 -080085 private TextView mBatteryPercentView;
86
Jorim Jaggi708f7722014-08-20 17:30:38 +020087 private BatteryController mBatteryController;
Dan Sandler055bb612017-02-08 16:21:49 -080088 private SettingObserver mSettingObserver;
89 private int mTextColor;
90 private int mLevel;
Evan Laird4bf21df2018-10-22 14:24:32 -040091 private int mShowPercentMode = MODE_DEFAULT;
Jason Monkf13413e2017-02-15 15:49:32 -050092 private boolean mForceShowPercent;
Evan Laird54ff81b2018-06-13 20:08:17 -040093 private boolean mShowPercentAvailable;
Evan Laird698839b2018-12-05 15:49:12 -050094 // Some places may need to show the battery conditionally, and not obey the tuner
95 private boolean mIgnoreTunerUpdates;
96 private boolean mIsSubscribedForTunerUpdates;
Evan Lairda5a73c52019-01-11 13:36:32 -050097 private boolean mCharging;
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040098
Jason Monk9a376bc2017-05-10 09:52:10 -040099 private int mDarkModeBackgroundColor;
100 private int mDarkModeFillColor;
101
102 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
111 private int mNonAdaptedForegroundColor;
112 private int mNonAdaptedBackgroundColor;
113
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -0400114 public BatteryMeterView(Context context) {
115 this(context, null, 0);
116 }
117
118 public BatteryMeterView(Context context, AttributeSet attrs) {
119 this(context, attrs, 0);
120 }
121
122 public BatteryMeterView(Context context, AttributeSet attrs, int defStyle) {
123 super(context, attrs, defStyle);
124
Dan Sandler055bb612017-02-08 16:21:49 -0800125 setOrientation(LinearLayout.HORIZONTAL);
126 setGravity(Gravity.CENTER_VERTICAL | Gravity.START);
127
John Spurlock29786fc2014-02-04 17:55:47 -0500128 TypedArray atts = context.obtainStyledAttributes(attrs, R.styleable.BatteryMeterView,
129 defStyle, 0);
130 final int frameColor = atts.getColor(R.styleable.BatteryMeterView_frameColor,
Daniel Nishi8af93cb2017-05-15 14:37:20 -0700131 context.getColor(R.color.meter_background_color));
Dan Sandler055bb612017-02-08 16:21:49 -0800132 mDrawable = new BatteryMeterDrawableBase(context, frameColor);
John Spurlock29786fc2014-02-04 17:55:47 -0500133 atts.recycle();
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -0400134
Dan Sandler055bb612017-02-08 16:21:49 -0800135 mSettingObserver = new SettingObserver(new Handler(context.getMainLooper()));
Evan Laird54ff81b2018-06-13 20:08:17 -0400136 mShowPercentAvailable = context.getResources().getBoolean(
137 com.android.internal.R.bool.config_battery_percentage_setting_available);
138
Dan Sandler055bb612017-02-08 16:21:49 -0800139
Charles He6a79b0d2017-09-18 09:50:58 +0100140 addOnAttachStateChangeListener(
141 new DisableStateTracker(DISABLE_NONE, DISABLE2_SYSTEM_ICONS));
142
Jason Monk3e189872016-01-12 09:10:34 -0500143 mSlotBattery = context.getString(
144 com.android.internal.R.string.status_bar_battery);
Dan Sandler055bb612017-02-08 16:21:49 -0800145 mBatteryIconView = new ImageView(context);
146 mBatteryIconView.setImageDrawable(mDrawable);
147 final MarginLayoutParams mlp = new MarginLayoutParams(
148 getResources().getDimensionPixelSize(R.dimen.status_bar_battery_icon_width),
149 getResources().getDimensionPixelSize(R.dimen.status_bar_battery_icon_height));
150 mlp.setMargins(0, 0, 0,
151 getResources().getDimensionPixelOffset(R.dimen.battery_margin_bottom));
152 addView(mBatteryIconView, mlp);
153
154 updateShowPercent();
Evan Lairdef160f22018-01-29 14:08:45 -0500155 setColorsFromContext(context);
Jason Monkd866b8a2017-03-29 15:30:44 -0400156 // Init to not dark at all.
157 onDarkChanged(new Rect(), 0, DarkIconDispatcher.DEFAULT_ICON_TINT);
Evan Lairdef160f22018-01-29 14:08:45 -0500158
Jason Monka6f1db3a2017-08-30 19:18:00 -0400159 mUserTracker = new CurrentUserTracker(mContext) {
160 @Override
161 public void onUserSwitched(int newUserId) {
162 mUser = newUserId;
163 getContext().getContentResolver().unregisterContentObserver(mSettingObserver);
164 getContext().getContentResolver().registerContentObserver(
165 Settings.System.getUriFor(SHOW_BATTERY_PERCENT), false, mSettingObserver,
166 newUserId);
Evan Laird6ead1e32018-06-19 13:39:05 -0400167 updateShowPercent();
Jason Monka6f1db3a2017-08-30 19:18:00 -0400168 }
169 };
Evan Laird39ea8102018-05-18 19:49:07 -0400170
171 setClipChildren(false);
172 setClipToPadding(false);
Jason Monk8e4e4cb2018-12-04 11:10:24 -0500173 Dependency.get(ConfigurationController.class).observe(viewAttachLifecycle(this), this);
Dan Sandler055bb612017-02-08 16:21:49 -0800174 }
175
Dan Sandlerdf14c202017-02-21 14:51:11 -0500176 public void setForceShowPercent(boolean show) {
Evan Laird4bf21df2018-10-22 14:24:32 -0400177 setPercentShowMode(show ? MODE_ON : MODE_DEFAULT);
178 }
179
180 /**
181 * Force a particular mode of showing percent
182 *
183 * 0 - No preference
184 * 1 - Force on
185 * 2 - Force off
186 * @param mode desired mode (none, on, off)
187 */
188 public void setPercentShowMode(@BatteryPercentMode int mode) {
189 mShowPercentMode = mode;
Jason Monkf13413e2017-02-15 15:49:32 -0500190 updateShowPercent();
191 }
192
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000193 /**
Evan Laird698839b2018-12-05 15:49:12 -0500194 * Set {@code true} to turn off BatteryMeterView's subscribing to the tuner for updates, and
195 * thus avoid it controlling its own visibility
196 *
197 * @param ignore whether to ignore the tuner or not
198 */
199 public void setIgnoreTunerUpdates(boolean ignore) {
200 mIgnoreTunerUpdates = ignore;
201 updateTunerSubscription();
202 }
203
204 private void updateTunerSubscription() {
205 if (mIgnoreTunerUpdates) {
206 unsubscribeFromTunerUpdates();
207 } else {
208 subscribeForTunerUpdates();
209 }
210 }
211
212 private void subscribeForTunerUpdates() {
213 if (mIsSubscribedForTunerUpdates || mIgnoreTunerUpdates) {
214 return;
215 }
216
217 Dependency.get(TunerService.class)
218 .addTunable(this, StatusBarIconController.ICON_BLACKLIST);
219 mIsSubscribedForTunerUpdates = true;
220 }
221
222 private void unsubscribeFromTunerUpdates() {
223 if (!mIsSubscribedForTunerUpdates) {
224 return;
225 }
226
227 Dependency.get(TunerService.class).removeTunable(this);
228 mIsSubscribedForTunerUpdates = false;
229 }
230
231 /**
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000232 * Sets whether the battery meter view uses the wallpaperTextColor. If we're not using it, we'll
233 * revert back to dark-mode-based/tinted colors.
234 *
235 * @param shouldUseWallpaperTextColor whether we should use wallpaperTextColor for all
236 * components
237 */
238 public void useWallpaperTextColor(boolean shouldUseWallpaperTextColor) {
239 if (shouldUseWallpaperTextColor == mUseWallpaperTextColors) {
240 return;
241 }
242
243 mUseWallpaperTextColors = shouldUseWallpaperTextColor;
244
245 if (mUseWallpaperTextColors) {
246 updateColors(
Jason Changb4e879d2018-04-11 11:17:58 +0800247 Utils.getColorAttrDefaultColor(mContext, R.attr.wallpaperTextColor),
248 Utils.getColorAttrDefaultColor(mContext, R.attr.wallpaperTextColorSecondary));
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000249 } else {
250 updateColors(mNonAdaptedForegroundColor, mNonAdaptedBackgroundColor);
251 }
252 }
253
Evan Lairdef160f22018-01-29 14:08:45 -0500254 public void setColorsFromContext(Context context) {
255 if (context == null) {
256 return;
257 }
258
259 Context dualToneDarkTheme = new ContextThemeWrapper(context,
260 Utils.getThemeAttr(context, R.attr.darkIconTheme));
261 Context dualToneLightTheme = new ContextThemeWrapper(context,
262 Utils.getThemeAttr(context, R.attr.lightIconTheme));
Jason Changb4e879d2018-04-11 11:17:58 +0800263 mDarkModeBackgroundColor = Utils.getColorAttrDefaultColor(dualToneDarkTheme,
264 R.attr.backgroundColor);
265 mDarkModeFillColor = Utils.getColorAttrDefaultColor(dualToneDarkTheme,
266 R.attr.fillColor);
267 mLightModeBackgroundColor = Utils.getColorAttrDefaultColor(dualToneLightTheme,
268 R.attr.backgroundColor);
269 mLightModeFillColor = Utils.getColorAttrDefaultColor(dualToneLightTheme, R.attr.fillColor);
Evan Lairdef160f22018-01-29 14:08:45 -0500270 }
271
Jorim Jaggi0d266892014-07-28 14:49:13 +0200272 @Override
273 public boolean hasOverlappingRendering() {
274 return false;
275 }
276
Jason Monkabe19742015-09-29 09:47:06 -0400277 @Override
Jason Monk3e189872016-01-12 09:10:34 -0500278 public void onTuningChanged(String key, String newValue) {
279 if (StatusBarIconController.ICON_BLACKLIST.equals(key)) {
280 ArraySet<String> icons = StatusBarIconController.getIconBlacklist(newValue);
Jason Monkf8c2f7b2017-09-06 09:22:29 -0400281 boolean hidden = icons.contains(mSlotBattery);
282 Dependency.get(IconLogger.class).onIconVisibility(mSlotBattery, !hidden);
283 setVisibility(hidden ? View.GONE : View.VISIBLE);
Jason Monk3e189872016-01-12 09:10:34 -0500284 }
285 }
286
287 @Override
Jason Monkabe19742015-09-29 09:47:06 -0400288 public void onAttachedToWindow() {
289 super.onAttachedToWindow();
Jason Monk9c7844c2017-01-18 15:21:53 -0500290 mBatteryController = Dependency.get(BatteryController.class);
Jason Monk9c7844c2017-01-18 15:21:53 -0500291 mBatteryController.addCallback(this);
Jason Monka6f1db3a2017-08-30 19:18:00 -0400292 mUser = ActivityManager.getCurrentUser();
Dan Sandler055bb612017-02-08 16:21:49 -0800293 getContext().getContentResolver().registerContentObserver(
Jason Monka6f1db3a2017-08-30 19:18:00 -0400294 Settings.System.getUriFor(SHOW_BATTERY_PERCENT), false, mSettingObserver, mUser);
Dan Sandler055bb612017-02-08 16:21:49 -0800295 updateShowPercent();
Evan Laird698839b2018-12-05 15:49:12 -0500296 subscribeForTunerUpdates();
Jason Monka6f1db3a2017-08-30 19:18:00 -0400297 mUserTracker.startTracking();
Jason Monkabe19742015-09-29 09:47:06 -0400298 }
John Spurlock3c875662013-08-31 15:07:25 -0400299
300 @Override
Jason Monkabe19742015-09-29 09:47:06 -0400301 public void onDetachedFromWindow() {
302 super.onDetachedFromWindow();
Jason Monka6f1db3a2017-08-30 19:18:00 -0400303 mUserTracker.stopTracking();
Jason Monk88529052016-11-04 13:29:58 -0400304 mBatteryController.removeCallback(this);
Dan Sandler055bb612017-02-08 16:21:49 -0800305 getContext().getContentResolver().unregisterContentObserver(mSettingObserver);
Evan Laird698839b2018-12-05 15:49:12 -0500306 unsubscribeFromTunerUpdates();
John Spurlock3c875662013-08-31 15:07:25 -0400307 }
John Spurlockf40d08f2015-05-29 10:48:22 -0400308
Jason Monkabe19742015-09-29 09:47:06 -0400309 @Override
310 public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
Dan Sandler055bb612017-02-08 16:21:49 -0800311 mDrawable.setBatteryLevel(level);
jackqdyuleiff5bd942017-04-04 10:54:21 -0700312 mDrawable.setCharging(pluggedIn);
Evan Lairda5a73c52019-01-11 13:36:32 -0500313 mCharging = pluggedIn;
Dan Sandler055bb612017-02-08 16:21:49 -0800314 mLevel = level;
315 updatePercentText();
Jason Monkabe19742015-09-29 09:47:06 -0400316 setContentDescription(
Adrian Roos70dcf832016-04-20 15:51:42 -0700317 getContext().getString(charging ? R.string.accessibility_battery_level_charging
318 : R.string.accessibility_battery_level, level));
John Spurlockf40d08f2015-05-29 10:48:22 -0400319 }
320
Jason Monkabe19742015-09-29 09:47:06 -0400321 @Override
Jason Monkc06fbb12016-01-08 14:12:18 -0500322 public void onPowerSaveChanged(boolean isPowerSave) {
Dan Sandler055bb612017-02-08 16:21:49 -0800323 mDrawable.setPowerSave(isPowerSave);
324 }
John Spurlockf40d08f2015-05-29 10:48:22 -0400325
Dan Sandler055bb612017-02-08 16:21:49 -0800326 private TextView loadPercentView() {
327 return (TextView) LayoutInflater.from(getContext())
328 .inflate(R.layout.battery_percentage_view, null);
329 }
330
Fabian Kozynski648e5552018-12-18 16:37:08 -0500331 /**
332 * Updates percent view by removing old one and reinflating if necessary
333 */
334 public void updatePercentView() {
335 if (mBatteryPercentView != null) {
336 removeView(mBatteryPercentView);
337 mBatteryPercentView = null;
338 }
339 updateShowPercent();
340 }
341
Dan Sandler055bb612017-02-08 16:21:49 -0800342 private void updatePercentText() {
Evan Lairda5a73c52019-01-11 13:36:32 -0500343 if (mBatteryController == null) {
344 return;
345 }
346
Dan Sandler055bb612017-02-08 16:21:49 -0800347 if (mBatteryPercentView != null) {
Evan Lairda5a73c52019-01-11 13:36:32 -0500348 if (mShowPercentMode == MODE_ESTIMATE && !mCharging) {
349 mBatteryController.getEstimatedTimeRemainingString((String estimate) -> {
350 mBatteryPercentView.setText(estimate);
351 });
352 } else {
353 mBatteryPercentView.setText(
354 NumberFormat.getPercentInstance().format(mLevel / 100f));
355 }
Dan Sandler055bb612017-02-08 16:21:49 -0800356 }
357 }
358
359 private void updateShowPercent() {
360 final boolean showing = mBatteryPercentView != null;
Evan Laird54ff81b2018-06-13 20:08:17 -0400361 final boolean systemSetting = 0 != Settings.System
362 .getIntForUser(getContext().getContentResolver(),
363 SHOW_BATTERY_PERCENT, 0, mUser);
364
Evan Laird4bf21df2018-10-22 14:24:32 -0400365 if ((mShowPercentAvailable && systemSetting && mShowPercentMode != MODE_OFF)
Evan Lairda5a73c52019-01-11 13:36:32 -0500366 || mShowPercentMode == MODE_ON || mShowPercentMode == MODE_ESTIMATE) {
Dan Sandler055bb612017-02-08 16:21:49 -0800367 if (!showing) {
368 mBatteryPercentView = loadPercentView();
369 if (mTextColor != 0) mBatteryPercentView.setTextColor(mTextColor);
370 updatePercentText();
371 addView(mBatteryPercentView,
Dan Sandler055bb612017-02-08 16:21:49 -0800372 new ViewGroup.LayoutParams(
373 LayoutParams.WRAP_CONTENT,
374 LayoutParams.MATCH_PARENT));
375 }
376 } else {
377 if (showing) {
378 removeView(mBatteryPercentView);
379 mBatteryPercentView = null;
380 }
381 }
John Spurlockf40d08f2015-05-29 10:48:22 -0400382 }
383
Jason Monkaa573e92017-01-27 17:00:29 -0500384 @Override
385 public void onDensityOrFontScaleChanged() {
386 scaleBatteryMeterViews();
387 }
388
389 /**
390 * Looks up the scale factor for status bar icons and scales the battery view by that amount.
391 */
392 private void scaleBatteryMeterViews() {
393 Resources res = getContext().getResources();
394 TypedValue typedValue = new TypedValue();
395
396 res.getValue(R.dimen.status_bar_icon_scale_factor, typedValue, true);
397 float iconScaleFactor = typedValue.getFloat();
398
399 int batteryHeight = res.getDimensionPixelSize(R.dimen.status_bar_battery_icon_height);
400 int batteryWidth = res.getDimensionPixelSize(R.dimen.status_bar_battery_icon_width);
401 int marginBottom = res.getDimensionPixelSize(R.dimen.battery_margin_bottom);
402
403 LinearLayout.LayoutParams scaledLayoutParams = new LinearLayout.LayoutParams(
404 (int) (batteryWidth * iconScaleFactor), (int) (batteryHeight * iconScaleFactor));
Dan Sandler055bb612017-02-08 16:21:49 -0800405 scaledLayoutParams.setMargins(0, 0, 0, marginBottom);
Jason Monkaa573e92017-01-27 17:00:29 -0500406
Dan Sandler055bb612017-02-08 16:21:49 -0800407 mBatteryIconView.setLayoutParams(scaledLayoutParams);
Evan Lairdecc93f22017-06-16 09:57:29 -0400408 FontSizeUtils.updateFontSize(mBatteryPercentView, R.dimen.qs_time_expanded_size);
Jason Monkaa573e92017-01-27 17:00:29 -0500409 }
410
411 @Override
412 public void onDarkChanged(Rect area, float darkIntensity, int tint) {
Jason Monk9a376bc2017-05-10 09:52:10 -0400413 float intensity = DarkIconDispatcher.isInArea(area, this) ? darkIntensity : 0;
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000414 mNonAdaptedForegroundColor = getColorForDarkIntensity(
415 intensity, mLightModeFillColor, mDarkModeFillColor);
416 mNonAdaptedBackgroundColor = getColorForDarkIntensity(
417 intensity, mLightModeBackgroundColor,mDarkModeBackgroundColor);
418
419 if (!mUseWallpaperTextColors) {
420 updateColors(mNonAdaptedForegroundColor, mNonAdaptedBackgroundColor);
421 }
Dan Sandler055bb612017-02-08 16:21:49 -0800422 }
423
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000424 private void updateColors(int foregroundColor, int backgroundColor) {
425 mDrawable.setColors(foregroundColor, backgroundColor);
426 mTextColor = foregroundColor;
Dan Sandler055bb612017-02-08 16:21:49 -0800427 if (mBatteryPercentView != null) {
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000428 mBatteryPercentView.setTextColor(foregroundColor);
Dan Sandler055bb612017-02-08 16:21:49 -0800429 }
Jason Monkabe19742015-09-29 09:47:06 -0400430 }
Jason Monk32508852017-01-18 09:17:13 -0500431
Jason Monk9a376bc2017-05-10 09:52:10 -0400432 private int getColorForDarkIntensity(float darkIntensity, int lightColor, int darkColor) {
433 return (int) ArgbEvaluator.getInstance().evaluate(darkIntensity, lightColor, darkColor);
Jason Monk32508852017-01-18 09:17:13 -0500434 }
Dan Sandler055bb612017-02-08 16:21:49 -0800435
Lucas Dupinc510d412018-06-12 13:08:23 -0700436 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
437 String powerSave = mDrawable == null ? null : mDrawable.getPowerSave() + "";
438 CharSequence percent = mBatteryPercentView == null ? null : mBatteryPercentView.getText();
439 pw.println(" BatteryMeterView:");
440 pw.println(" mDrawable.getPowerSave: " + powerSave);
441 pw.println(" mBatteryPercentView.getText(): " + percent);
442 pw.println(" mTextColor: #" + Integer.toHexString(mTextColor));
443 pw.println(" mLevel: " + mLevel);
444 pw.println(" mForceShowPercent: " + mForceShowPercent);
445 }
446
Dan Sandler055bb612017-02-08 16:21:49 -0800447 private final class SettingObserver extends ContentObserver {
448 public SettingObserver(Handler handler) {
449 super(handler);
450 }
451
452 @Override
453 public void onChange(boolean selfChange, Uri uri) {
454 super.onChange(selfChange, uri);
455 updateShowPercent();
456 }
457 }
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -0400458}