Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 1 | /* |
| 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 Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 16 | package com.android.systemui; |
| 17 | |
Charles He | 6a79b0d | 2017-09-18 09:50:58 +0100 | [diff] [blame] | 18 | import static android.app.StatusBarManager.DISABLE2_SYSTEM_ICONS; |
| 19 | import static android.app.StatusBarManager.DISABLE_NONE; |
Evan Laird | bcf631d | 2017-03-10 10:56:45 -0500 | [diff] [blame] | 20 | import static android.provider.Settings.System.SHOW_BATTERY_PERCENT; |
| 21 | |
Jason Monk | 8e4e4cb | 2018-12-04 11:10:24 -0500 | [diff] [blame] | 22 | import static com.android.systemui.util.SysuiLifecycle.viewAttachLifecycle; |
| 23 | |
Evan Laird | 4bf21df | 2018-10-22 14:24:32 -0400 | [diff] [blame] | 24 | import static java.lang.annotation.RetentionPolicy.SOURCE; |
| 25 | |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame] | 26 | import android.animation.ArgbEvaluator; |
Evan Laird | 4bf21df | 2018-10-22 14:24:32 -0400 | [diff] [blame] | 27 | import android.annotation.IntDef; |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 28 | import android.app.ActivityManager; |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 29 | import android.content.Context; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 30 | import android.content.res.Resources; |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 31 | import android.content.res.TypedArray; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 32 | import android.database.ContentObserver; |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame] | 33 | import android.graphics.Rect; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 34 | import android.net.Uri; |
| 35 | import android.os.Handler; |
| 36 | import android.provider.Settings; |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame] | 37 | import android.util.ArraySet; |
| 38 | import android.util.AttributeSet; |
| 39 | import android.util.TypedValue; |
| 40 | import android.view.ContextThemeWrapper; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 41 | import android.view.Gravity; |
| 42 | import android.view.LayoutInflater; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 43 | import android.view.View; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 44 | import android.view.ViewGroup; |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 45 | import android.widget.ImageView; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 46 | import android.widget.LinearLayout; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 47 | import android.widget.TextView; |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame] | 48 | |
| 49 | import com.android.settingslib.Utils; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 50 | import com.android.settingslib.graph.BatteryMeterDrawableBase; |
Beverly | 1be62f4 | 2018-12-19 17:17:48 -0500 | [diff] [blame] | 51 | import com.android.systemui.plugins.DarkIconDispatcher; |
| 52 | import com.android.systemui.plugins.DarkIconDispatcher.DarkReceiver; |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 53 | import com.android.systemui.settings.CurrentUserTracker; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 54 | import com.android.systemui.statusbar.phone.StatusBarIconController; |
Jorim Jaggi | 708f772 | 2014-08-20 17:30:38 +0200 | [diff] [blame] | 55 | import com.android.systemui.statusbar.policy.BatteryController; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 56 | import com.android.systemui.statusbar.policy.BatteryController.BatteryStateChangeCallback; |
| 57 | import com.android.systemui.statusbar.policy.ConfigurationController; |
| 58 | import com.android.systemui.statusbar.policy.ConfigurationController.ConfigurationListener; |
Jason Monk | f8c2f7b | 2017-09-06 09:22:29 -0400 | [diff] [blame] | 59 | import com.android.systemui.statusbar.policy.IconLogger; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 60 | import com.android.systemui.tuner.TunerService; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 61 | import com.android.systemui.tuner.TunerService.Tunable; |
Charles He | 6a79b0d | 2017-09-18 09:50:58 +0100 | [diff] [blame] | 62 | import com.android.systemui.util.Utils.DisableStateTracker; |
Jorim Jaggi | 708f772 | 2014-08-20 17:30:38 +0200 | [diff] [blame] | 63 | |
Lucas Dupin | c510d41 | 2018-06-12 13:08:23 -0700 | [diff] [blame] | 64 | import java.io.FileDescriptor; |
| 65 | import java.io.PrintWriter; |
Evan Laird | 4bf21df | 2018-10-22 14:24:32 -0400 | [diff] [blame] | 66 | import java.lang.annotation.Retention; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 67 | import java.text.NumberFormat; |
| 68 | |
| 69 | public class BatteryMeterView extends LinearLayout implements |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 70 | BatteryStateChangeCallback, Tunable, DarkReceiver, ConfigurationListener { |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 71 | |
Evan Laird | 4bf21df | 2018-10-22 14:24:32 -0400 | [diff] [blame] | 72 | |
| 73 | @Retention(SOURCE) |
Evan Laird | a5a73c5 | 2019-01-11 13:36:32 -0500 | [diff] [blame^] | 74 | @IntDef({MODE_DEFAULT, MODE_ON, MODE_OFF, MODE_ESTIMATE}) |
Evan Laird | 4bf21df | 2018-10-22 14:24:32 -0400 | [diff] [blame] | 75 | 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 Laird | a5a73c5 | 2019-01-11 13:36:32 -0500 | [diff] [blame^] | 79 | public static final int MODE_ESTIMATE = 3; |
Evan Laird | 4bf21df | 2018-10-22 14:24:32 -0400 | [diff] [blame] | 80 | |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 81 | private final BatteryMeterDrawableBase mDrawable; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 82 | private final String mSlotBattery; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 83 | private final ImageView mBatteryIconView; |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 84 | private final CurrentUserTracker mUserTracker; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 85 | private TextView mBatteryPercentView; |
| 86 | |
Jorim Jaggi | 708f772 | 2014-08-20 17:30:38 +0200 | [diff] [blame] | 87 | private BatteryController mBatteryController; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 88 | private SettingObserver mSettingObserver; |
| 89 | private int mTextColor; |
| 90 | private int mLevel; |
Evan Laird | 4bf21df | 2018-10-22 14:24:32 -0400 | [diff] [blame] | 91 | private int mShowPercentMode = MODE_DEFAULT; |
Jason Monk | f13413e | 2017-02-15 15:49:32 -0500 | [diff] [blame] | 92 | private boolean mForceShowPercent; |
Evan Laird | 54ff81b | 2018-06-13 20:08:17 -0400 | [diff] [blame] | 93 | private boolean mShowPercentAvailable; |
Evan Laird | 698839b | 2018-12-05 15:49:12 -0500 | [diff] [blame] | 94 | // Some places may need to show the battery conditionally, and not obey the tuner |
| 95 | private boolean mIgnoreTunerUpdates; |
| 96 | private boolean mIsSubscribedForTunerUpdates; |
Evan Laird | a5a73c5 | 2019-01-11 13:36:32 -0500 | [diff] [blame^] | 97 | private boolean mCharging; |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 98 | |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame] | 99 | private int mDarkModeBackgroundColor; |
| 100 | private int mDarkModeFillColor; |
| 101 | |
| 102 | private int mLightModeBackgroundColor; |
| 103 | private int mLightModeFillColor; |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 104 | private int mUser; |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame] | 105 | |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 106 | /** |
| 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 Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 114 | 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 Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 125 | setOrientation(LinearLayout.HORIZONTAL); |
| 126 | setGravity(Gravity.CENTER_VERTICAL | Gravity.START); |
| 127 | |
John Spurlock | 29786fc | 2014-02-04 17:55:47 -0500 | [diff] [blame] | 128 | TypedArray atts = context.obtainStyledAttributes(attrs, R.styleable.BatteryMeterView, |
| 129 | defStyle, 0); |
| 130 | final int frameColor = atts.getColor(R.styleable.BatteryMeterView_frameColor, |
Daniel Nishi | 8af93cb | 2017-05-15 14:37:20 -0700 | [diff] [blame] | 131 | context.getColor(R.color.meter_background_color)); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 132 | mDrawable = new BatteryMeterDrawableBase(context, frameColor); |
John Spurlock | 29786fc | 2014-02-04 17:55:47 -0500 | [diff] [blame] | 133 | atts.recycle(); |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 134 | |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 135 | mSettingObserver = new SettingObserver(new Handler(context.getMainLooper())); |
Evan Laird | 54ff81b | 2018-06-13 20:08:17 -0400 | [diff] [blame] | 136 | mShowPercentAvailable = context.getResources().getBoolean( |
| 137 | com.android.internal.R.bool.config_battery_percentage_setting_available); |
| 138 | |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 139 | |
Charles He | 6a79b0d | 2017-09-18 09:50:58 +0100 | [diff] [blame] | 140 | addOnAttachStateChangeListener( |
| 141 | new DisableStateTracker(DISABLE_NONE, DISABLE2_SYSTEM_ICONS)); |
| 142 | |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 143 | mSlotBattery = context.getString( |
| 144 | com.android.internal.R.string.status_bar_battery); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 145 | 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 Laird | ef160f2 | 2018-01-29 14:08:45 -0500 | [diff] [blame] | 155 | setColorsFromContext(context); |
Jason Monk | d866b8a | 2017-03-29 15:30:44 -0400 | [diff] [blame] | 156 | // Init to not dark at all. |
| 157 | onDarkChanged(new Rect(), 0, DarkIconDispatcher.DEFAULT_ICON_TINT); |
Evan Laird | ef160f2 | 2018-01-29 14:08:45 -0500 | [diff] [blame] | 158 | |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 159 | 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 Laird | 6ead1e3 | 2018-06-19 13:39:05 -0400 | [diff] [blame] | 167 | updateShowPercent(); |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 168 | } |
| 169 | }; |
Evan Laird | 39ea810 | 2018-05-18 19:49:07 -0400 | [diff] [blame] | 170 | |
| 171 | setClipChildren(false); |
| 172 | setClipToPadding(false); |
Jason Monk | 8e4e4cb | 2018-12-04 11:10:24 -0500 | [diff] [blame] | 173 | Dependency.get(ConfigurationController.class).observe(viewAttachLifecycle(this), this); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Dan Sandler | df14c20 | 2017-02-21 14:51:11 -0500 | [diff] [blame] | 176 | public void setForceShowPercent(boolean show) { |
Evan Laird | 4bf21df | 2018-10-22 14:24:32 -0400 | [diff] [blame] | 177 | 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 Monk | f13413e | 2017-02-15 15:49:32 -0500 | [diff] [blame] | 190 | updateShowPercent(); |
| 191 | } |
| 192 | |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 193 | /** |
Evan Laird | 698839b | 2018-12-05 15:49:12 -0500 | [diff] [blame] | 194 | * 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 Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 232 | * 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 Chang | b4e879d | 2018-04-11 11:17:58 +0800 | [diff] [blame] | 247 | Utils.getColorAttrDefaultColor(mContext, R.attr.wallpaperTextColor), |
| 248 | Utils.getColorAttrDefaultColor(mContext, R.attr.wallpaperTextColorSecondary)); |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 249 | } else { |
| 250 | updateColors(mNonAdaptedForegroundColor, mNonAdaptedBackgroundColor); |
| 251 | } |
| 252 | } |
| 253 | |
Evan Laird | ef160f2 | 2018-01-29 14:08:45 -0500 | [diff] [blame] | 254 | 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 Chang | b4e879d | 2018-04-11 11:17:58 +0800 | [diff] [blame] | 263 | 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 Laird | ef160f2 | 2018-01-29 14:08:45 -0500 | [diff] [blame] | 270 | } |
| 271 | |
Jorim Jaggi | 0d26689 | 2014-07-28 14:49:13 +0200 | [diff] [blame] | 272 | @Override |
| 273 | public boolean hasOverlappingRendering() { |
| 274 | return false; |
| 275 | } |
| 276 | |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 277 | @Override |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 278 | public void onTuningChanged(String key, String newValue) { |
| 279 | if (StatusBarIconController.ICON_BLACKLIST.equals(key)) { |
| 280 | ArraySet<String> icons = StatusBarIconController.getIconBlacklist(newValue); |
Jason Monk | f8c2f7b | 2017-09-06 09:22:29 -0400 | [diff] [blame] | 281 | boolean hidden = icons.contains(mSlotBattery); |
| 282 | Dependency.get(IconLogger.class).onIconVisibility(mSlotBattery, !hidden); |
| 283 | setVisibility(hidden ? View.GONE : View.VISIBLE); |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
| 287 | @Override |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 288 | public void onAttachedToWindow() { |
| 289 | super.onAttachedToWindow(); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 290 | mBatteryController = Dependency.get(BatteryController.class); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 291 | mBatteryController.addCallback(this); |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 292 | mUser = ActivityManager.getCurrentUser(); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 293 | getContext().getContentResolver().registerContentObserver( |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 294 | Settings.System.getUriFor(SHOW_BATTERY_PERCENT), false, mSettingObserver, mUser); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 295 | updateShowPercent(); |
Evan Laird | 698839b | 2018-12-05 15:49:12 -0500 | [diff] [blame] | 296 | subscribeForTunerUpdates(); |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 297 | mUserTracker.startTracking(); |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 298 | } |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 299 | |
| 300 | @Override |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 301 | public void onDetachedFromWindow() { |
| 302 | super.onDetachedFromWindow(); |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 303 | mUserTracker.stopTracking(); |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 304 | mBatteryController.removeCallback(this); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 305 | getContext().getContentResolver().unregisterContentObserver(mSettingObserver); |
Evan Laird | 698839b | 2018-12-05 15:49:12 -0500 | [diff] [blame] | 306 | unsubscribeFromTunerUpdates(); |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 307 | } |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 308 | |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 309 | @Override |
| 310 | public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) { |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 311 | mDrawable.setBatteryLevel(level); |
jackqdyulei | ff5bd94 | 2017-04-04 10:54:21 -0700 | [diff] [blame] | 312 | mDrawable.setCharging(pluggedIn); |
Evan Laird | a5a73c5 | 2019-01-11 13:36:32 -0500 | [diff] [blame^] | 313 | mCharging = pluggedIn; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 314 | mLevel = level; |
| 315 | updatePercentText(); |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 316 | setContentDescription( |
Adrian Roos | 70dcf83 | 2016-04-20 15:51:42 -0700 | [diff] [blame] | 317 | getContext().getString(charging ? R.string.accessibility_battery_level_charging |
| 318 | : R.string.accessibility_battery_level, level)); |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 319 | } |
| 320 | |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 321 | @Override |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 322 | public void onPowerSaveChanged(boolean isPowerSave) { |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 323 | mDrawable.setPowerSave(isPowerSave); |
| 324 | } |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 325 | |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 326 | private TextView loadPercentView() { |
| 327 | return (TextView) LayoutInflater.from(getContext()) |
| 328 | .inflate(R.layout.battery_percentage_view, null); |
| 329 | } |
| 330 | |
Fabian Kozynski | 648e555 | 2018-12-18 16:37:08 -0500 | [diff] [blame] | 331 | /** |
| 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 Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 342 | private void updatePercentText() { |
Evan Laird | a5a73c5 | 2019-01-11 13:36:32 -0500 | [diff] [blame^] | 343 | if (mBatteryController == null) { |
| 344 | return; |
| 345 | } |
| 346 | |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 347 | if (mBatteryPercentView != null) { |
Evan Laird | a5a73c5 | 2019-01-11 13:36:32 -0500 | [diff] [blame^] | 348 | 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 Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
| 359 | private void updateShowPercent() { |
| 360 | final boolean showing = mBatteryPercentView != null; |
Evan Laird | 54ff81b | 2018-06-13 20:08:17 -0400 | [diff] [blame] | 361 | final boolean systemSetting = 0 != Settings.System |
| 362 | .getIntForUser(getContext().getContentResolver(), |
| 363 | SHOW_BATTERY_PERCENT, 0, mUser); |
| 364 | |
Evan Laird | 4bf21df | 2018-10-22 14:24:32 -0400 | [diff] [blame] | 365 | if ((mShowPercentAvailable && systemSetting && mShowPercentMode != MODE_OFF) |
Evan Laird | a5a73c5 | 2019-01-11 13:36:32 -0500 | [diff] [blame^] | 366 | || mShowPercentMode == MODE_ON || mShowPercentMode == MODE_ESTIMATE) { |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 367 | if (!showing) { |
| 368 | mBatteryPercentView = loadPercentView(); |
| 369 | if (mTextColor != 0) mBatteryPercentView.setTextColor(mTextColor); |
| 370 | updatePercentText(); |
| 371 | addView(mBatteryPercentView, |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 372 | 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 Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 382 | } |
| 383 | |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 384 | @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 Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 405 | scaledLayoutParams.setMargins(0, 0, 0, marginBottom); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 406 | |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 407 | mBatteryIconView.setLayoutParams(scaledLayoutParams); |
Evan Laird | ecc93f2 | 2017-06-16 09:57:29 -0400 | [diff] [blame] | 408 | FontSizeUtils.updateFontSize(mBatteryPercentView, R.dimen.qs_time_expanded_size); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | @Override |
| 412 | public void onDarkChanged(Rect area, float darkIntensity, int tint) { |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame] | 413 | float intensity = DarkIconDispatcher.isInArea(area, this) ? darkIntensity : 0; |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 414 | mNonAdaptedForegroundColor = getColorForDarkIntensity( |
| 415 | intensity, mLightModeFillColor, mDarkModeFillColor); |
| 416 | mNonAdaptedBackgroundColor = getColorForDarkIntensity( |
| 417 | intensity, mLightModeBackgroundColor,mDarkModeBackgroundColor); |
| 418 | |
| 419 | if (!mUseWallpaperTextColors) { |
| 420 | updateColors(mNonAdaptedForegroundColor, mNonAdaptedBackgroundColor); |
| 421 | } |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 422 | } |
| 423 | |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 424 | private void updateColors(int foregroundColor, int backgroundColor) { |
| 425 | mDrawable.setColors(foregroundColor, backgroundColor); |
| 426 | mTextColor = foregroundColor; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 427 | if (mBatteryPercentView != null) { |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 428 | mBatteryPercentView.setTextColor(foregroundColor); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 429 | } |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 430 | } |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 431 | |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame] | 432 | private int getColorForDarkIntensity(float darkIntensity, int lightColor, int darkColor) { |
| 433 | return (int) ArgbEvaluator.getInstance().evaluate(darkIntensity, lightColor, darkColor); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 434 | } |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 435 | |
Lucas Dupin | c510d41 | 2018-06-12 13:08:23 -0700 | [diff] [blame] | 436 | 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 Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 447 | 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 Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 458 | } |