blob: 2afcb12da730b3b1de406efde284dd2e2ac7f656 [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
Lucas Dupin8968f6a2019-08-09 17:41:15 -070022import static com.android.systemui.DejankUtils.whitelistIpcs;
Jason Monk8e4e4cb2018-12-04 11:10:24 -050023import static com.android.systemui.util.SysuiLifecycle.viewAttachLifecycle;
24
Evan Laird4bf21df2018-10-22 14:24:32 -040025import static java.lang.annotation.RetentionPolicy.SOURCE;
26
shawnlinf603ea02019-03-12 15:44:37 +080027import android.animation.LayoutTransition;
28import android.animation.ObjectAnimator;
Evan Laird4bf21df2018-10-22 14:24:32 -040029import android.annotation.IntDef;
Jason Monka6f1db3a2017-08-30 19:18:00 -040030import android.app.ActivityManager;
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040031import android.content.Context;
Jason Monkaa573e92017-01-27 17:00:29 -050032import android.content.res.Resources;
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040033import android.content.res.TypedArray;
Dan Sandler055bb612017-02-08 16:21:49 -080034import android.database.ContentObserver;
Jason Monk9a376bc2017-05-10 09:52:10 -040035import android.graphics.Rect;
Dan Sandler055bb612017-02-08 16:21:49 -080036import android.net.Uri;
37import android.os.Handler;
38import android.provider.Settings;
Salvador Martinez7de89292019-04-23 14:55:36 -070039import android.text.TextUtils;
Jason Monk9a376bc2017-05-10 09:52:10 -040040import android.util.ArraySet;
41import android.util.AttributeSet;
42import android.util.TypedValue;
Dan Sandler055bb612017-02-08 16:21:49 -080043import android.view.Gravity;
44import android.view.LayoutInflater;
Dan Sandler055bb612017-02-08 16:21:49 -080045import android.view.ViewGroup;
Jason Monkabe19742015-09-29 09:47:06 -040046import android.widget.ImageView;
Jason Monkaa573e92017-01-27 17:00:29 -050047import android.widget.LinearLayout;
Dan Sandler055bb612017-02-08 16:21:49 -080048import android.widget.TextView;
Jason Monk9a376bc2017-05-10 09:52:10 -040049
Fabian Kozynskibca09b72019-04-04 15:57:42 -040050import androidx.annotation.StyleRes;
51
Jason Monk9a376bc2017-05-10 09:52:10 -040052import com.android.settingslib.Utils;
Evan Laird2259da42019-02-08 16:48:53 -050053import com.android.settingslib.graph.ThemedBatteryDrawable;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000054import com.android.systemui.broadcast.BroadcastDispatcher;
Beverly1be62f42018-12-19 17:17:48 -050055import com.android.systemui.plugins.DarkIconDispatcher;
56import com.android.systemui.plugins.DarkIconDispatcher.DarkReceiver;
Jason Monka6f1db3a2017-08-30 19:18:00 -040057import com.android.systemui.settings.CurrentUserTracker;
Dave Mankoffbcaca8a2019-10-31 18:04:08 -040058import com.android.systemui.statusbar.CommandQueue;
Jason Monk3e189872016-01-12 09:10:34 -050059import com.android.systemui.statusbar.phone.StatusBarIconController;
Jorim Jaggi708f7722014-08-20 17:30:38 +020060import com.android.systemui.statusbar.policy.BatteryController;
Jason Monkaa573e92017-01-27 17:00:29 -050061import com.android.systemui.statusbar.policy.BatteryController.BatteryStateChangeCallback;
62import com.android.systemui.statusbar.policy.ConfigurationController;
63import com.android.systemui.statusbar.policy.ConfigurationController.ConfigurationListener;
Jason Monk3e189872016-01-12 09:10:34 -050064import com.android.systemui.tuner.TunerService;
Jason Monkaa573e92017-01-27 17:00:29 -050065import com.android.systemui.tuner.TunerService.Tunable;
Charles He6a79b0d2017-09-18 09:50:58 +010066import com.android.systemui.util.Utils.DisableStateTracker;
Jorim Jaggi708f7722014-08-20 17:30:38 +020067
Lucas Dupinc510d412018-06-12 13:08:23 -070068import java.io.FileDescriptor;
69import java.io.PrintWriter;
Evan Laird4bf21df2018-10-22 14:24:32 -040070import java.lang.annotation.Retention;
Dan Sandler055bb612017-02-08 16:21:49 -080071import java.text.NumberFormat;
72
73public class BatteryMeterView extends LinearLayout implements
Jason Monkaa573e92017-01-27 17:00:29 -050074 BatteryStateChangeCallback, Tunable, DarkReceiver, ConfigurationListener {
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040075
Evan Laird4bf21df2018-10-22 14:24:32 -040076
77 @Retention(SOURCE)
Evan Lairda5a73c52019-01-11 13:36:32 -050078 @IntDef({MODE_DEFAULT, MODE_ON, MODE_OFF, MODE_ESTIMATE})
Evan Laird4bf21df2018-10-22 14:24:32 -040079 public @interface BatteryPercentMode {}
80 public static final int MODE_DEFAULT = 0;
81 public static final int MODE_ON = 1;
82 public static final int MODE_OFF = 2;
Evan Lairda5a73c52019-01-11 13:36:32 -050083 public static final int MODE_ESTIMATE = 3;
Evan Laird4bf21df2018-10-22 14:24:32 -040084
Evan Laird2259da42019-02-08 16:48:53 -050085 private final ThemedBatteryDrawable mDrawable;
Jason Monk3e189872016-01-12 09:10:34 -050086 private final String mSlotBattery;
Dan Sandler055bb612017-02-08 16:21:49 -080087 private final ImageView mBatteryIconView;
Jason Monka6f1db3a2017-08-30 19:18:00 -040088 private final CurrentUserTracker mUserTracker;
Dan Sandler055bb612017-02-08 16:21:49 -080089 private TextView mBatteryPercentView;
90
Jorim Jaggi708f7722014-08-20 17:30:38 +020091 private BatteryController mBatteryController;
Dan Sandler055bb612017-02-08 16:21:49 -080092 private SettingObserver mSettingObserver;
Fabian Kozynskibca09b72019-04-04 15:57:42 -040093 private final @StyleRes int mPercentageStyleId;
Dan Sandler055bb612017-02-08 16:21:49 -080094 private int mTextColor;
95 private int mLevel;
Evan Laird4bf21df2018-10-22 14:24:32 -040096 private int mShowPercentMode = MODE_DEFAULT;
Jason Monkf13413e2017-02-15 15:49:32 -050097 private boolean mForceShowPercent;
Evan Laird54ff81b2018-06-13 20:08:17 -040098 private boolean mShowPercentAvailable;
Evan Laird698839b2018-12-05 15:49:12 -050099 // Some places may need to show the battery conditionally, and not obey the tuner
100 private boolean mIgnoreTunerUpdates;
101 private boolean mIsSubscribedForTunerUpdates;
Evan Lairda5a73c52019-01-11 13:36:32 -0500102 private boolean mCharging;
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -0400103
Fabian Kozynskif86df992019-04-22 14:23:47 -0400104 private DualToneHandler mDualToneHandler;
Jason Monka6f1db3a2017-08-30 19:18:00 -0400105 private int mUser;
Jason Monk9a376bc2017-05-10 09:52:10 -0400106
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000107 /**
108 * Whether we should use colors that adapt based on wallpaper/the scrim behind quick settings.
109 */
110 private boolean mUseWallpaperTextColors;
111
Evan Laird2259da42019-02-08 16:48:53 -0500112 private int mNonAdaptedSingleToneColor;
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000113 private int mNonAdaptedForegroundColor;
114 private int mNonAdaptedBackgroundColor;
115
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -0400116 public BatteryMeterView(Context context, AttributeSet attrs) {
117 this(context, attrs, 0);
118 }
119
120 public BatteryMeterView(Context context, AttributeSet attrs, int defStyle) {
121 super(context, attrs, defStyle);
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000122 BroadcastDispatcher broadcastDispatcher = Dependency.get(BroadcastDispatcher.class);
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -0400123
Dan Sandler055bb612017-02-08 16:21:49 -0800124 setOrientation(LinearLayout.HORIZONTAL);
125 setGravity(Gravity.CENTER_VERTICAL | Gravity.START);
126
John Spurlock29786fc2014-02-04 17:55:47 -0500127 TypedArray atts = context.obtainStyledAttributes(attrs, R.styleable.BatteryMeterView,
128 defStyle, 0);
129 final int frameColor = atts.getColor(R.styleable.BatteryMeterView_frameColor,
Daniel Nishi8af93cb2017-05-15 14:37:20 -0700130 context.getColor(R.color.meter_background_color));
Fabian Kozynskibca09b72019-04-04 15:57:42 -0400131 mPercentageStyleId = atts.getResourceId(R.styleable.BatteryMeterView_textAppearance, 0);
Evan Laird2259da42019-02-08 16:48:53 -0500132 mDrawable = new ThemedBatteryDrawable(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(
Dave Mankoffbcaca8a2019-10-31 18:04:08 -0400141 new DisableStateTracker(DISABLE_NONE, DISABLE2_SYSTEM_ICONS,
142 Dependency.get(CommandQueue.class)));
Charles He6a79b0d2017-09-18 09:50:58 +0100143
shawnlinf603ea02019-03-12 15:44:37 +0800144 setupLayoutTransition();
145
Jason Monk3e189872016-01-12 09:10:34 -0500146 mSlotBattery = context.getString(
147 com.android.internal.R.string.status_bar_battery);
Dan Sandler055bb612017-02-08 16:21:49 -0800148 mBatteryIconView = new ImageView(context);
149 mBatteryIconView.setImageDrawable(mDrawable);
150 final MarginLayoutParams mlp = new MarginLayoutParams(
151 getResources().getDimensionPixelSize(R.dimen.status_bar_battery_icon_width),
152 getResources().getDimensionPixelSize(R.dimen.status_bar_battery_icon_height));
153 mlp.setMargins(0, 0, 0,
154 getResources().getDimensionPixelOffset(R.dimen.battery_margin_bottom));
155 addView(mBatteryIconView, mlp);
156
157 updateShowPercent();
Fabian Kozynskif86df992019-04-22 14:23:47 -0400158 mDualToneHandler = new DualToneHandler(context);
Jason Monkd866b8a2017-03-29 15:30:44 -0400159 // Init to not dark at all.
160 onDarkChanged(new Rect(), 0, DarkIconDispatcher.DEFAULT_ICON_TINT);
Evan Lairdef160f22018-01-29 14:08:45 -0500161
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000162 mUserTracker = new CurrentUserTracker(broadcastDispatcher) {
Jason Monka6f1db3a2017-08-30 19:18:00 -0400163 @Override
164 public void onUserSwitched(int newUserId) {
165 mUser = newUserId;
166 getContext().getContentResolver().unregisterContentObserver(mSettingObserver);
167 getContext().getContentResolver().registerContentObserver(
168 Settings.System.getUriFor(SHOW_BATTERY_PERCENT), false, mSettingObserver,
169 newUserId);
Evan Laird6ead1e32018-06-19 13:39:05 -0400170 updateShowPercent();
Jason Monka6f1db3a2017-08-30 19:18:00 -0400171 }
172 };
Evan Laird39ea8102018-05-18 19:49:07 -0400173
174 setClipChildren(false);
175 setClipToPadding(false);
Jason Monk8e4e4cb2018-12-04 11:10:24 -0500176 Dependency.get(ConfigurationController.class).observe(viewAttachLifecycle(this), this);
Dan Sandler055bb612017-02-08 16:21:49 -0800177 }
178
shawnlinf603ea02019-03-12 15:44:37 +0800179 private void setupLayoutTransition() {
180 LayoutTransition transition = new LayoutTransition();
181 transition.setDuration(200);
182
183 ObjectAnimator appearAnimator = ObjectAnimator.ofFloat(null, "alpha", 0f, 1f);
184 transition.setAnimator(LayoutTransition.APPEARING, appearAnimator);
185 transition.setInterpolator(LayoutTransition.APPEARING, Interpolators.ALPHA_IN);
186
187 ObjectAnimator disappearAnimator = ObjectAnimator.ofFloat(null, "alpha", 1f, 0f);
188 transition.setInterpolator(LayoutTransition.DISAPPEARING, Interpolators.ALPHA_OUT);
189 transition.setAnimator(LayoutTransition.DISAPPEARING, disappearAnimator);
190
191 setLayoutTransition(transition);
192 }
193
Dan Sandlerdf14c202017-02-21 14:51:11 -0500194 public void setForceShowPercent(boolean show) {
Evan Laird4bf21df2018-10-22 14:24:32 -0400195 setPercentShowMode(show ? MODE_ON : MODE_DEFAULT);
196 }
197
198 /**
199 * Force a particular mode of showing percent
200 *
201 * 0 - No preference
202 * 1 - Force on
203 * 2 - Force off
204 * @param mode desired mode (none, on, off)
205 */
206 public void setPercentShowMode(@BatteryPercentMode int mode) {
207 mShowPercentMode = mode;
Jason Monkf13413e2017-02-15 15:49:32 -0500208 updateShowPercent();
209 }
210
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000211 /**
Evan Laird698839b2018-12-05 15:49:12 -0500212 * Set {@code true} to turn off BatteryMeterView's subscribing to the tuner for updates, and
213 * thus avoid it controlling its own visibility
214 *
215 * @param ignore whether to ignore the tuner or not
216 */
217 public void setIgnoreTunerUpdates(boolean ignore) {
218 mIgnoreTunerUpdates = ignore;
219 updateTunerSubscription();
220 }
221
222 private void updateTunerSubscription() {
223 if (mIgnoreTunerUpdates) {
224 unsubscribeFromTunerUpdates();
225 } else {
226 subscribeForTunerUpdates();
227 }
228 }
229
230 private void subscribeForTunerUpdates() {
231 if (mIsSubscribedForTunerUpdates || mIgnoreTunerUpdates) {
232 return;
233 }
234
235 Dependency.get(TunerService.class)
236 .addTunable(this, StatusBarIconController.ICON_BLACKLIST);
237 mIsSubscribedForTunerUpdates = true;
238 }
239
240 private void unsubscribeFromTunerUpdates() {
241 if (!mIsSubscribedForTunerUpdates) {
242 return;
243 }
244
245 Dependency.get(TunerService.class).removeTunable(this);
246 mIsSubscribedForTunerUpdates = false;
247 }
248
249 /**
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000250 * Sets whether the battery meter view uses the wallpaperTextColor. If we're not using it, we'll
251 * revert back to dark-mode-based/tinted colors.
252 *
253 * @param shouldUseWallpaperTextColor whether we should use wallpaperTextColor for all
254 * components
255 */
256 public void useWallpaperTextColor(boolean shouldUseWallpaperTextColor) {
257 if (shouldUseWallpaperTextColor == mUseWallpaperTextColors) {
258 return;
259 }
260
261 mUseWallpaperTextColors = shouldUseWallpaperTextColor;
262
263 if (mUseWallpaperTextColors) {
264 updateColors(
Jason Changb4e879d2018-04-11 11:17:58 +0800265 Utils.getColorAttrDefaultColor(mContext, R.attr.wallpaperTextColor),
Evan Laird2259da42019-02-08 16:48:53 -0500266 Utils.getColorAttrDefaultColor(mContext, R.attr.wallpaperTextColorSecondary),
267 Utils.getColorAttrDefaultColor(mContext, R.attr.wallpaperTextColor));
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000268 } else {
Evan Laird2259da42019-02-08 16:48:53 -0500269 updateColors(mNonAdaptedForegroundColor, mNonAdaptedBackgroundColor,
270 mNonAdaptedSingleToneColor);
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000271 }
272 }
273
Evan Lairdef160f22018-01-29 14:08:45 -0500274 public void setColorsFromContext(Context context) {
275 if (context == null) {
276 return;
277 }
278
Fabian Kozynskif86df992019-04-22 14:23:47 -0400279 mDualToneHandler.setColorsFromContext(context);
Evan Lairdef160f22018-01-29 14:08:45 -0500280 }
281
Jorim Jaggi0d266892014-07-28 14:49:13 +0200282 @Override
283 public boolean hasOverlappingRendering() {
284 return false;
285 }
286
Jason Monkabe19742015-09-29 09:47:06 -0400287 @Override
Jason Monk3e189872016-01-12 09:10:34 -0500288 public void onTuningChanged(String key, String newValue) {
289 if (StatusBarIconController.ICON_BLACKLIST.equals(key)) {
290 ArraySet<String> icons = StatusBarIconController.getIconBlacklist(newValue);
Jason Monk3e189872016-01-12 09:10:34 -0500291 }
292 }
293
294 @Override
Jason Monkabe19742015-09-29 09:47:06 -0400295 public void onAttachedToWindow() {
296 super.onAttachedToWindow();
Jason Monk9c7844c2017-01-18 15:21:53 -0500297 mBatteryController = Dependency.get(BatteryController.class);
Jason Monk9c7844c2017-01-18 15:21:53 -0500298 mBatteryController.addCallback(this);
Jason Monka6f1db3a2017-08-30 19:18:00 -0400299 mUser = ActivityManager.getCurrentUser();
Dan Sandler055bb612017-02-08 16:21:49 -0800300 getContext().getContentResolver().registerContentObserver(
Jason Monka6f1db3a2017-08-30 19:18:00 -0400301 Settings.System.getUriFor(SHOW_BATTERY_PERCENT), false, mSettingObserver, mUser);
Salvador Martinez7de89292019-04-23 14:55:36 -0700302 getContext().getContentResolver().registerContentObserver(
303 Settings.Global.getUriFor(Settings.Global.BATTERY_ESTIMATES_LAST_UPDATE_TIME),
304 false, mSettingObserver);
Dan Sandler055bb612017-02-08 16:21:49 -0800305 updateShowPercent();
Evan Laird698839b2018-12-05 15:49:12 -0500306 subscribeForTunerUpdates();
Jason Monka6f1db3a2017-08-30 19:18:00 -0400307 mUserTracker.startTracking();
Jason Monkabe19742015-09-29 09:47:06 -0400308 }
John Spurlock3c875662013-08-31 15:07:25 -0400309
310 @Override
Jason Monkabe19742015-09-29 09:47:06 -0400311 public void onDetachedFromWindow() {
312 super.onDetachedFromWindow();
Jason Monka6f1db3a2017-08-30 19:18:00 -0400313 mUserTracker.stopTracking();
Jason Monk88529052016-11-04 13:29:58 -0400314 mBatteryController.removeCallback(this);
Dan Sandler055bb612017-02-08 16:21:49 -0800315 getContext().getContentResolver().unregisterContentObserver(mSettingObserver);
Evan Laird698839b2018-12-05 15:49:12 -0500316 unsubscribeFromTunerUpdates();
John Spurlock3c875662013-08-31 15:07:25 -0400317 }
John Spurlockf40d08f2015-05-29 10:48:22 -0400318
Jason Monkabe19742015-09-29 09:47:06 -0400319 @Override
320 public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
jackqdyuleiff5bd942017-04-04 10:54:21 -0700321 mDrawable.setCharging(pluggedIn);
Evan Laird2259da42019-02-08 16:48:53 -0500322 mDrawable.setBatteryLevel(level);
Evan Lairda5a73c52019-01-11 13:36:32 -0500323 mCharging = pluggedIn;
Dan Sandler055bb612017-02-08 16:21:49 -0800324 mLevel = level;
325 updatePercentText();
John Spurlockf40d08f2015-05-29 10:48:22 -0400326 }
327
Jason Monkabe19742015-09-29 09:47:06 -0400328 @Override
Jason Monkc06fbb12016-01-08 14:12:18 -0500329 public void onPowerSaveChanged(boolean isPowerSave) {
Evan Laird2259da42019-02-08 16:48:53 -0500330 mDrawable.setPowerSaveEnabled(isPowerSave);
Dan Sandler055bb612017-02-08 16:21:49 -0800331 }
John Spurlockf40d08f2015-05-29 10:48:22 -0400332
Dan Sandler055bb612017-02-08 16:21:49 -0800333 private TextView loadPercentView() {
334 return (TextView) LayoutInflater.from(getContext())
335 .inflate(R.layout.battery_percentage_view, null);
336 }
337
Fabian Kozynski648e5552018-12-18 16:37:08 -0500338 /**
339 * Updates percent view by removing old one and reinflating if necessary
340 */
341 public void updatePercentView() {
342 if (mBatteryPercentView != null) {
343 removeView(mBatteryPercentView);
344 mBatteryPercentView = null;
345 }
346 updateShowPercent();
347 }
348
Dan Sandler055bb612017-02-08 16:21:49 -0800349 private void updatePercentText() {
Evan Lairda5a73c52019-01-11 13:36:32 -0500350 if (mBatteryController == null) {
351 return;
352 }
353
Dan Sandler055bb612017-02-08 16:21:49 -0800354 if (mBatteryPercentView != null) {
Evan Lairda5a73c52019-01-11 13:36:32 -0500355 if (mShowPercentMode == MODE_ESTIMATE && !mCharging) {
356 mBatteryController.getEstimatedTimeRemainingString((String estimate) -> {
Evan Laird85ee4a32019-03-06 18:09:20 -0500357 if (estimate != null) {
358 mBatteryPercentView.setText(estimate);
Amin Shaikh0fcc3372019-05-29 14:08:41 -0400359 setContentDescription(getContext().getString(
Amin Shaikh44386742019-05-31 08:53:50 -0400360 R.string.accessibility_battery_level_with_estimate,
361 mLevel, estimate));
Evan Laird85ee4a32019-03-06 18:09:20 -0500362 } else {
363 setPercentTextAtCurrentLevel();
364 }
Evan Lairda5a73c52019-01-11 13:36:32 -0500365 });
366 } else {
Evan Laird85ee4a32019-03-06 18:09:20 -0500367 setPercentTextAtCurrentLevel();
Evan Lairda5a73c52019-01-11 13:36:32 -0500368 }
Evan Laird7839fe82019-06-14 13:27:54 -0400369 } else {
370 setContentDescription(
371 getContext().getString(mCharging ? R.string.accessibility_battery_level_charging
372 : R.string.accessibility_battery_level, mLevel));
Dan Sandler055bb612017-02-08 16:21:49 -0800373 }
374 }
375
Evan Laird85ee4a32019-03-06 18:09:20 -0500376 private void setPercentTextAtCurrentLevel() {
377 mBatteryPercentView.setText(
378 NumberFormat.getPercentInstance().format(mLevel / 100f));
Amin Shaikh0fcc3372019-05-29 14:08:41 -0400379 setContentDescription(
380 getContext().getString(mCharging ? R.string.accessibility_battery_level_charging
381 : R.string.accessibility_battery_level, mLevel));
Evan Laird85ee4a32019-03-06 18:09:20 -0500382 }
383
Dan Sandler055bb612017-02-08 16:21:49 -0800384 private void updateShowPercent() {
385 final boolean showing = mBatteryPercentView != null;
Lucas Dupin8968f6a2019-08-09 17:41:15 -0700386 // TODO(b/140051051)
387 final boolean systemSetting = 0 != whitelistIpcs(() -> Settings.System
Evan Laird54ff81b2018-06-13 20:08:17 -0400388 .getIntForUser(getContext().getContentResolver(),
Lucas Dupin8968f6a2019-08-09 17:41:15 -0700389 SHOW_BATTERY_PERCENT, 0, mUser));
Evan Laird54ff81b2018-06-13 20:08:17 -0400390
Evan Laird4bf21df2018-10-22 14:24:32 -0400391 if ((mShowPercentAvailable && systemSetting && mShowPercentMode != MODE_OFF)
Evan Lairda5a73c52019-01-11 13:36:32 -0500392 || mShowPercentMode == MODE_ON || mShowPercentMode == MODE_ESTIMATE) {
Dan Sandler055bb612017-02-08 16:21:49 -0800393 if (!showing) {
394 mBatteryPercentView = loadPercentView();
Fabian Kozynskibca09b72019-04-04 15:57:42 -0400395 if (mPercentageStyleId != 0) { // Only set if specified as attribute
396 mBatteryPercentView.setTextAppearance(mPercentageStyleId);
397 }
Fabian Kozynski7c35d332019-04-12 09:24:13 -0400398 if (mTextColor != 0) mBatteryPercentView.setTextColor(mTextColor);
Dan Sandler055bb612017-02-08 16:21:49 -0800399 updatePercentText();
400 addView(mBatteryPercentView,
Dan Sandler055bb612017-02-08 16:21:49 -0800401 new ViewGroup.LayoutParams(
402 LayoutParams.WRAP_CONTENT,
403 LayoutParams.MATCH_PARENT));
404 }
405 } else {
406 if (showing) {
407 removeView(mBatteryPercentView);
408 mBatteryPercentView = null;
409 }
410 }
John Spurlockf40d08f2015-05-29 10:48:22 -0400411 }
412
Jason Monkaa573e92017-01-27 17:00:29 -0500413 @Override
414 public void onDensityOrFontScaleChanged() {
415 scaleBatteryMeterViews();
416 }
417
418 /**
419 * Looks up the scale factor for status bar icons and scales the battery view by that amount.
420 */
421 private void scaleBatteryMeterViews() {
422 Resources res = getContext().getResources();
423 TypedValue typedValue = new TypedValue();
424
425 res.getValue(R.dimen.status_bar_icon_scale_factor, typedValue, true);
426 float iconScaleFactor = typedValue.getFloat();
427
428 int batteryHeight = res.getDimensionPixelSize(R.dimen.status_bar_battery_icon_height);
429 int batteryWidth = res.getDimensionPixelSize(R.dimen.status_bar_battery_icon_width);
430 int marginBottom = res.getDimensionPixelSize(R.dimen.battery_margin_bottom);
431
432 LinearLayout.LayoutParams scaledLayoutParams = new LinearLayout.LayoutParams(
433 (int) (batteryWidth * iconScaleFactor), (int) (batteryHeight * iconScaleFactor));
Dan Sandler055bb612017-02-08 16:21:49 -0800434 scaledLayoutParams.setMargins(0, 0, 0, marginBottom);
Jason Monkaa573e92017-01-27 17:00:29 -0500435
Dan Sandler055bb612017-02-08 16:21:49 -0800436 mBatteryIconView.setLayoutParams(scaledLayoutParams);
Jason Monkaa573e92017-01-27 17:00:29 -0500437 }
438
439 @Override
440 public void onDarkChanged(Rect area, float darkIntensity, int tint) {
Jason Monk9a376bc2017-05-10 09:52:10 -0400441 float intensity = DarkIconDispatcher.isInArea(area, this) ? darkIntensity : 0;
Fabian Kozynskif86df992019-04-22 14:23:47 -0400442 mNonAdaptedSingleToneColor = mDualToneHandler.getSingleColor(intensity);
443 mNonAdaptedForegroundColor = mDualToneHandler.getFillColor(intensity);
444 mNonAdaptedBackgroundColor = mDualToneHandler.getBackgroundColor(intensity);
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000445
446 if (!mUseWallpaperTextColors) {
Evan Laird2259da42019-02-08 16:48:53 -0500447 updateColors(mNonAdaptedForegroundColor, mNonAdaptedBackgroundColor,
448 mNonAdaptedSingleToneColor);
Rohan Shahcc3d1d82018-03-30 21:24:17 +0000449 }
Dan Sandler055bb612017-02-08 16:21:49 -0800450 }
451
Evan Laird2259da42019-02-08 16:48:53 -0500452 private void updateColors(int foregroundColor, int backgroundColor, int singleToneColor) {
453 mDrawable.setColors(foregroundColor, backgroundColor, singleToneColor);
454 mTextColor = singleToneColor;
Dan Sandler055bb612017-02-08 16:21:49 -0800455 if (mBatteryPercentView != null) {
Evan Laird2259da42019-02-08 16:48:53 -0500456 mBatteryPercentView.setTextColor(singleToneColor);
Dan Sandler055bb612017-02-08 16:21:49 -0800457 }
Jason Monkabe19742015-09-29 09:47:06 -0400458 }
Jason Monk32508852017-01-18 09:17:13 -0500459
Lucas Dupinc510d412018-06-12 13:08:23 -0700460 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Evan Laird2259da42019-02-08 16:48:53 -0500461 String powerSave = mDrawable == null ? null : mDrawable.getPowerSaveEnabled() + "";
Lucas Dupinc510d412018-06-12 13:08:23 -0700462 CharSequence percent = mBatteryPercentView == null ? null : mBatteryPercentView.getText();
463 pw.println(" BatteryMeterView:");
464 pw.println(" mDrawable.getPowerSave: " + powerSave);
465 pw.println(" mBatteryPercentView.getText(): " + percent);
466 pw.println(" mTextColor: #" + Integer.toHexString(mTextColor));
467 pw.println(" mLevel: " + mLevel);
468 pw.println(" mForceShowPercent: " + mForceShowPercent);
469 }
470
Dan Sandler055bb612017-02-08 16:21:49 -0800471 private final class SettingObserver extends ContentObserver {
472 public SettingObserver(Handler handler) {
473 super(handler);
474 }
475
476 @Override
477 public void onChange(boolean selfChange, Uri uri) {
478 super.onChange(selfChange, uri);
479 updateShowPercent();
Salvador Martinez7de89292019-04-23 14:55:36 -0700480 if (TextUtils.equals(uri.getLastPathSegment(),
481 Settings.Global.BATTERY_ESTIMATES_LAST_UPDATE_TIME)) {
482 // update the text for sure if the estimate in the cache was updated
483 updatePercentText();
484 }
Dan Sandler055bb612017-02-08 16:21:49 -0800485 }
486 }
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -0400487}