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 | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame] | 22 | import android.animation.ArgbEvaluator; |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 23 | import android.app.ActivityManager; |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 24 | import android.content.Context; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 25 | import android.content.res.Resources; |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 26 | import android.content.res.TypedArray; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 27 | import android.database.ContentObserver; |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame] | 28 | import android.graphics.Rect; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 29 | import android.net.Uri; |
| 30 | import android.os.Handler; |
| 31 | import android.provider.Settings; |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame] | 32 | import android.util.ArraySet; |
| 33 | import android.util.AttributeSet; |
| 34 | import android.util.TypedValue; |
| 35 | import android.view.ContextThemeWrapper; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 36 | import android.view.Gravity; |
| 37 | import android.view.LayoutInflater; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 38 | import android.view.View; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 39 | import android.view.ViewGroup; |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 40 | import android.widget.ImageView; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 41 | import android.widget.LinearLayout; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 42 | import android.widget.TextView; |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame] | 43 | |
| 44 | import com.android.settingslib.Utils; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 45 | import com.android.settingslib.graph.BatteryMeterDrawableBase; |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 46 | import com.android.systemui.settings.CurrentUserTracker; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 47 | import com.android.systemui.statusbar.phone.StatusBarIconController; |
Jorim Jaggi | 708f772 | 2014-08-20 17:30:38 +0200 | [diff] [blame] | 48 | import com.android.systemui.statusbar.policy.BatteryController; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 49 | import com.android.systemui.statusbar.policy.BatteryController.BatteryStateChangeCallback; |
| 50 | import com.android.systemui.statusbar.policy.ConfigurationController; |
| 51 | import com.android.systemui.statusbar.policy.ConfigurationController.ConfigurationListener; |
| 52 | import com.android.systemui.statusbar.policy.DarkIconDispatcher; |
| 53 | import com.android.systemui.statusbar.policy.DarkIconDispatcher.DarkReceiver; |
Jason Monk | f8c2f7b | 2017-09-06 09:22:29 -0400 | [diff] [blame] | 54 | import com.android.systemui.statusbar.policy.IconLogger; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 55 | import com.android.systemui.tuner.TunerService; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 56 | import com.android.systemui.tuner.TunerService.Tunable; |
Charles He | 6a79b0d | 2017-09-18 09:50:58 +0100 | [diff] [blame] | 57 | import com.android.systemui.util.Utils.DisableStateTracker; |
Evan Laird | 54ff81b | 2018-06-13 20:08:17 -0400 | [diff] [blame] | 58 | import com.android.systemui.R; |
Jorim Jaggi | 708f772 | 2014-08-20 17:30:38 +0200 | [diff] [blame] | 59 | |
Lucas Dupin | c510d41 | 2018-06-12 13:08:23 -0700 | [diff] [blame] | 60 | import java.io.FileDescriptor; |
| 61 | import java.io.PrintWriter; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 62 | import java.text.NumberFormat; |
| 63 | |
| 64 | public class BatteryMeterView extends LinearLayout implements |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 65 | BatteryStateChangeCallback, Tunable, DarkReceiver, ConfigurationListener { |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 66 | |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 67 | private final BatteryMeterDrawableBase mDrawable; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 68 | private final String mSlotBattery; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 69 | private final ImageView mBatteryIconView; |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 70 | private final CurrentUserTracker mUserTracker; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 71 | private TextView mBatteryPercentView; |
| 72 | |
Jorim Jaggi | 708f772 | 2014-08-20 17:30:38 +0200 | [diff] [blame] | 73 | private BatteryController mBatteryController; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 74 | private SettingObserver mSettingObserver; |
| 75 | private int mTextColor; |
| 76 | private int mLevel; |
Jason Monk | f13413e | 2017-02-15 15:49:32 -0500 | [diff] [blame] | 77 | private boolean mForceShowPercent; |
Evan Laird | 54ff81b | 2018-06-13 20:08:17 -0400 | [diff] [blame] | 78 | private boolean mShowPercentAvailable; |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 79 | |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame] | 80 | private int mDarkModeBackgroundColor; |
| 81 | private int mDarkModeFillColor; |
| 82 | |
| 83 | private int mLightModeBackgroundColor; |
| 84 | private int mLightModeFillColor; |
Lucas Dupin | 987f193 | 2017-05-13 21:02:52 -0700 | [diff] [blame] | 85 | private float mDarkIntensity; |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 86 | private int mUser; |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame] | 87 | |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 88 | /** |
| 89 | * Whether we should use colors that adapt based on wallpaper/the scrim behind quick settings. |
| 90 | */ |
| 91 | private boolean mUseWallpaperTextColors; |
| 92 | |
| 93 | private int mNonAdaptedForegroundColor; |
| 94 | private int mNonAdaptedBackgroundColor; |
| 95 | |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 96 | public BatteryMeterView(Context context) { |
| 97 | this(context, null, 0); |
| 98 | } |
| 99 | |
| 100 | public BatteryMeterView(Context context, AttributeSet attrs) { |
| 101 | this(context, attrs, 0); |
| 102 | } |
| 103 | |
| 104 | public BatteryMeterView(Context context, AttributeSet attrs, int defStyle) { |
| 105 | super(context, attrs, defStyle); |
| 106 | |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 107 | setOrientation(LinearLayout.HORIZONTAL); |
| 108 | setGravity(Gravity.CENTER_VERTICAL | Gravity.START); |
| 109 | |
John Spurlock | 29786fc | 2014-02-04 17:55:47 -0500 | [diff] [blame] | 110 | TypedArray atts = context.obtainStyledAttributes(attrs, R.styleable.BatteryMeterView, |
| 111 | defStyle, 0); |
| 112 | final int frameColor = atts.getColor(R.styleable.BatteryMeterView_frameColor, |
Daniel Nishi | 8af93cb | 2017-05-15 14:37:20 -0700 | [diff] [blame] | 113 | context.getColor(R.color.meter_background_color)); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 114 | mDrawable = new BatteryMeterDrawableBase(context, frameColor); |
John Spurlock | 29786fc | 2014-02-04 17:55:47 -0500 | [diff] [blame] | 115 | atts.recycle(); |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 116 | |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 117 | mSettingObserver = new SettingObserver(new Handler(context.getMainLooper())); |
Evan Laird | 54ff81b | 2018-06-13 20:08:17 -0400 | [diff] [blame] | 118 | mShowPercentAvailable = context.getResources().getBoolean( |
| 119 | com.android.internal.R.bool.config_battery_percentage_setting_available); |
| 120 | |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 121 | |
Charles He | 6a79b0d | 2017-09-18 09:50:58 +0100 | [diff] [blame] | 122 | addOnAttachStateChangeListener( |
| 123 | new DisableStateTracker(DISABLE_NONE, DISABLE2_SYSTEM_ICONS)); |
| 124 | |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 125 | mSlotBattery = context.getString( |
| 126 | com.android.internal.R.string.status_bar_battery); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 127 | mBatteryIconView = new ImageView(context); |
| 128 | mBatteryIconView.setImageDrawable(mDrawable); |
| 129 | final MarginLayoutParams mlp = new MarginLayoutParams( |
| 130 | getResources().getDimensionPixelSize(R.dimen.status_bar_battery_icon_width), |
| 131 | getResources().getDimensionPixelSize(R.dimen.status_bar_battery_icon_height)); |
| 132 | mlp.setMargins(0, 0, 0, |
| 133 | getResources().getDimensionPixelOffset(R.dimen.battery_margin_bottom)); |
| 134 | addView(mBatteryIconView, mlp); |
| 135 | |
| 136 | updateShowPercent(); |
Evan Laird | ef160f2 | 2018-01-29 14:08:45 -0500 | [diff] [blame] | 137 | setColorsFromContext(context); |
Jason Monk | d866b8a | 2017-03-29 15:30:44 -0400 | [diff] [blame] | 138 | // Init to not dark at all. |
| 139 | onDarkChanged(new Rect(), 0, DarkIconDispatcher.DEFAULT_ICON_TINT); |
Evan Laird | ef160f2 | 2018-01-29 14:08:45 -0500 | [diff] [blame] | 140 | |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 141 | mUserTracker = new CurrentUserTracker(mContext) { |
| 142 | @Override |
| 143 | public void onUserSwitched(int newUserId) { |
| 144 | mUser = newUserId; |
| 145 | getContext().getContentResolver().unregisterContentObserver(mSettingObserver); |
| 146 | getContext().getContentResolver().registerContentObserver( |
| 147 | Settings.System.getUriFor(SHOW_BATTERY_PERCENT), false, mSettingObserver, |
| 148 | newUserId); |
| 149 | } |
| 150 | }; |
Evan Laird | 39ea810 | 2018-05-18 19:49:07 -0400 | [diff] [blame] | 151 | |
| 152 | setClipChildren(false); |
| 153 | setClipToPadding(false); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Dan Sandler | df14c20 | 2017-02-21 14:51:11 -0500 | [diff] [blame] | 156 | public void setForceShowPercent(boolean show) { |
| 157 | mForceShowPercent = show; |
Jason Monk | f13413e | 2017-02-15 15:49:32 -0500 | [diff] [blame] | 158 | updateShowPercent(); |
| 159 | } |
| 160 | |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 161 | /** |
| 162 | * Sets whether the battery meter view uses the wallpaperTextColor. If we're not using it, we'll |
| 163 | * revert back to dark-mode-based/tinted colors. |
| 164 | * |
| 165 | * @param shouldUseWallpaperTextColor whether we should use wallpaperTextColor for all |
| 166 | * components |
| 167 | */ |
| 168 | public void useWallpaperTextColor(boolean shouldUseWallpaperTextColor) { |
| 169 | if (shouldUseWallpaperTextColor == mUseWallpaperTextColors) { |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | mUseWallpaperTextColors = shouldUseWallpaperTextColor; |
| 174 | |
| 175 | if (mUseWallpaperTextColors) { |
| 176 | updateColors( |
Jason Chang | b4e879d | 2018-04-11 11:17:58 +0800 | [diff] [blame] | 177 | Utils.getColorAttrDefaultColor(mContext, R.attr.wallpaperTextColor), |
| 178 | Utils.getColorAttrDefaultColor(mContext, R.attr.wallpaperTextColorSecondary)); |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 179 | } else { |
| 180 | updateColors(mNonAdaptedForegroundColor, mNonAdaptedBackgroundColor); |
| 181 | } |
| 182 | } |
| 183 | |
Evan Laird | ef160f2 | 2018-01-29 14:08:45 -0500 | [diff] [blame] | 184 | public void setColorsFromContext(Context context) { |
| 185 | if (context == null) { |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | Context dualToneDarkTheme = new ContextThemeWrapper(context, |
| 190 | Utils.getThemeAttr(context, R.attr.darkIconTheme)); |
| 191 | Context dualToneLightTheme = new ContextThemeWrapper(context, |
| 192 | Utils.getThemeAttr(context, R.attr.lightIconTheme)); |
Jason Chang | b4e879d | 2018-04-11 11:17:58 +0800 | [diff] [blame] | 193 | mDarkModeBackgroundColor = Utils.getColorAttrDefaultColor(dualToneDarkTheme, |
| 194 | R.attr.backgroundColor); |
| 195 | mDarkModeFillColor = Utils.getColorAttrDefaultColor(dualToneDarkTheme, |
| 196 | R.attr.fillColor); |
| 197 | mLightModeBackgroundColor = Utils.getColorAttrDefaultColor(dualToneLightTheme, |
| 198 | R.attr.backgroundColor); |
| 199 | mLightModeFillColor = Utils.getColorAttrDefaultColor(dualToneLightTheme, R.attr.fillColor); |
Evan Laird | ef160f2 | 2018-01-29 14:08:45 -0500 | [diff] [blame] | 200 | } |
| 201 | |
Jorim Jaggi | 0d26689 | 2014-07-28 14:49:13 +0200 | [diff] [blame] | 202 | @Override |
| 203 | public boolean hasOverlappingRendering() { |
| 204 | return false; |
| 205 | } |
| 206 | |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 207 | @Override |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 208 | public void onTuningChanged(String key, String newValue) { |
| 209 | if (StatusBarIconController.ICON_BLACKLIST.equals(key)) { |
| 210 | ArraySet<String> icons = StatusBarIconController.getIconBlacklist(newValue); |
Jason Monk | f8c2f7b | 2017-09-06 09:22:29 -0400 | [diff] [blame] | 211 | boolean hidden = icons.contains(mSlotBattery); |
| 212 | Dependency.get(IconLogger.class).onIconVisibility(mSlotBattery, !hidden); |
| 213 | setVisibility(hidden ? View.GONE : View.VISIBLE); |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
| 217 | @Override |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 218 | public void onAttachedToWindow() { |
| 219 | super.onAttachedToWindow(); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 220 | mBatteryController = Dependency.get(BatteryController.class); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 221 | mBatteryController.addCallback(this); |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 222 | mUser = ActivityManager.getCurrentUser(); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 223 | getContext().getContentResolver().registerContentObserver( |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 224 | Settings.System.getUriFor(SHOW_BATTERY_PERCENT), false, mSettingObserver, mUser); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 225 | updateShowPercent(); |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 226 | Dependency.get(TunerService.class) |
| 227 | .addTunable(this, StatusBarIconController.ICON_BLACKLIST); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 228 | Dependency.get(ConfigurationController.class).addCallback(this); |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 229 | mUserTracker.startTracking(); |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 230 | } |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 231 | |
| 232 | @Override |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 233 | public void onDetachedFromWindow() { |
| 234 | super.onDetachedFromWindow(); |
Jason Monk | a6f1db3a | 2017-08-30 19:18:00 -0400 | [diff] [blame] | 235 | mUserTracker.stopTracking(); |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 236 | mBatteryController.removeCallback(this); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 237 | getContext().getContentResolver().unregisterContentObserver(mSettingObserver); |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 238 | Dependency.get(TunerService.class).removeTunable(this); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 239 | Dependency.get(ConfigurationController.class).removeCallback(this); |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 240 | } |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 241 | |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 242 | @Override |
| 243 | public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) { |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 244 | mDrawable.setBatteryLevel(level); |
jackqdyulei | ff5bd94 | 2017-04-04 10:54:21 -0700 | [diff] [blame] | 245 | mDrawable.setCharging(pluggedIn); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 246 | mLevel = level; |
| 247 | updatePercentText(); |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 248 | setContentDescription( |
Adrian Roos | 70dcf83 | 2016-04-20 15:51:42 -0700 | [diff] [blame] | 249 | getContext().getString(charging ? R.string.accessibility_battery_level_charging |
| 250 | : R.string.accessibility_battery_level, level)); |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 251 | } |
| 252 | |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 253 | @Override |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 254 | public void onPowerSaveChanged(boolean isPowerSave) { |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 255 | mDrawable.setPowerSave(isPowerSave); |
| 256 | } |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 257 | |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 258 | private TextView loadPercentView() { |
| 259 | return (TextView) LayoutInflater.from(getContext()) |
| 260 | .inflate(R.layout.battery_percentage_view, null); |
| 261 | } |
| 262 | |
| 263 | private void updatePercentText() { |
| 264 | if (mBatteryPercentView != null) { |
| 265 | mBatteryPercentView.setText( |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame] | 266 | NumberFormat.getPercentInstance().format(mLevel / 100f)); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
| 270 | private void updateShowPercent() { |
| 271 | final boolean showing = mBatteryPercentView != null; |
Evan Laird | 54ff81b | 2018-06-13 20:08:17 -0400 | [diff] [blame] | 272 | final boolean systemSetting = 0 != Settings.System |
| 273 | .getIntForUser(getContext().getContentResolver(), |
| 274 | SHOW_BATTERY_PERCENT, 0, mUser); |
| 275 | |
| 276 | if ((mShowPercentAvailable && systemSetting) || mForceShowPercent) { |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 277 | if (!showing) { |
| 278 | mBatteryPercentView = loadPercentView(); |
| 279 | if (mTextColor != 0) mBatteryPercentView.setTextColor(mTextColor); |
| 280 | updatePercentText(); |
| 281 | addView(mBatteryPercentView, |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 282 | new ViewGroup.LayoutParams( |
| 283 | LayoutParams.WRAP_CONTENT, |
| 284 | LayoutParams.MATCH_PARENT)); |
| 285 | } |
| 286 | } else { |
| 287 | if (showing) { |
| 288 | removeView(mBatteryPercentView); |
| 289 | mBatteryPercentView = null; |
| 290 | } |
| 291 | } |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 292 | } |
| 293 | |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 294 | @Override |
| 295 | public void onDensityOrFontScaleChanged() { |
| 296 | scaleBatteryMeterViews(); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Looks up the scale factor for status bar icons and scales the battery view by that amount. |
| 301 | */ |
| 302 | private void scaleBatteryMeterViews() { |
| 303 | Resources res = getContext().getResources(); |
| 304 | TypedValue typedValue = new TypedValue(); |
| 305 | |
| 306 | res.getValue(R.dimen.status_bar_icon_scale_factor, typedValue, true); |
| 307 | float iconScaleFactor = typedValue.getFloat(); |
| 308 | |
| 309 | int batteryHeight = res.getDimensionPixelSize(R.dimen.status_bar_battery_icon_height); |
| 310 | int batteryWidth = res.getDimensionPixelSize(R.dimen.status_bar_battery_icon_width); |
| 311 | int marginBottom = res.getDimensionPixelSize(R.dimen.battery_margin_bottom); |
| 312 | |
| 313 | LinearLayout.LayoutParams scaledLayoutParams = new LinearLayout.LayoutParams( |
| 314 | (int) (batteryWidth * iconScaleFactor), (int) (batteryHeight * iconScaleFactor)); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 315 | scaledLayoutParams.setMargins(0, 0, 0, marginBottom); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 316 | |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 317 | mBatteryIconView.setLayoutParams(scaledLayoutParams); |
Evan Laird | ecc93f2 | 2017-06-16 09:57:29 -0400 | [diff] [blame] | 318 | FontSizeUtils.updateFontSize(mBatteryPercentView, R.dimen.qs_time_expanded_size); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public void onDarkChanged(Rect area, float darkIntensity, int tint) { |
Lucas Dupin | 987f193 | 2017-05-13 21:02:52 -0700 | [diff] [blame] | 323 | mDarkIntensity = darkIntensity; |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 324 | |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame] | 325 | float intensity = DarkIconDispatcher.isInArea(area, this) ? darkIntensity : 0; |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 326 | mNonAdaptedForegroundColor = getColorForDarkIntensity( |
| 327 | intensity, mLightModeFillColor, mDarkModeFillColor); |
| 328 | mNonAdaptedBackgroundColor = getColorForDarkIntensity( |
| 329 | intensity, mLightModeBackgroundColor,mDarkModeBackgroundColor); |
| 330 | |
| 331 | if (!mUseWallpaperTextColors) { |
| 332 | updateColors(mNonAdaptedForegroundColor, mNonAdaptedBackgroundColor); |
| 333 | } |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 336 | private void updateColors(int foregroundColor, int backgroundColor) { |
| 337 | mDrawable.setColors(foregroundColor, backgroundColor); |
| 338 | mTextColor = foregroundColor; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 339 | if (mBatteryPercentView != null) { |
Rohan Shah | cc3d1d8 | 2018-03-30 21:24:17 +0000 | [diff] [blame] | 340 | mBatteryPercentView.setTextColor(foregroundColor); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 341 | } |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 342 | } |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 343 | |
Lucas Dupin | 987f193 | 2017-05-13 21:02:52 -0700 | [diff] [blame] | 344 | public void setFillColor(int color) { |
| 345 | if (mLightModeFillColor == color) { |
| 346 | return; |
| 347 | } |
| 348 | mLightModeFillColor = color; |
| 349 | onDarkChanged(new Rect(), mDarkIntensity, DarkIconDispatcher.DEFAULT_ICON_TINT); |
| 350 | } |
| 351 | |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame] | 352 | private int getColorForDarkIntensity(float darkIntensity, int lightColor, int darkColor) { |
| 353 | return (int) ArgbEvaluator.getInstance().evaluate(darkIntensity, lightColor, darkColor); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 354 | } |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 355 | |
Lucas Dupin | c510d41 | 2018-06-12 13:08:23 -0700 | [diff] [blame] | 356 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 357 | String powerSave = mDrawable == null ? null : mDrawable.getPowerSave() + ""; |
| 358 | CharSequence percent = mBatteryPercentView == null ? null : mBatteryPercentView.getText(); |
| 359 | pw.println(" BatteryMeterView:"); |
| 360 | pw.println(" mDrawable.getPowerSave: " + powerSave); |
| 361 | pw.println(" mBatteryPercentView.getText(): " + percent); |
| 362 | pw.println(" mTextColor: #" + Integer.toHexString(mTextColor)); |
| 363 | pw.println(" mLevel: " + mLevel); |
| 364 | pw.println(" mForceShowPercent: " + mForceShowPercent); |
| 365 | } |
| 366 | |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 367 | private final class SettingObserver extends ContentObserver { |
| 368 | public SettingObserver(Handler handler) { |
| 369 | super(handler); |
| 370 | } |
| 371 | |
| 372 | @Override |
| 373 | public void onChange(boolean selfChange, Uri uri) { |
| 374 | super.onChange(selfChange, uri); |
| 375 | updateShowPercent(); |
| 376 | } |
| 377 | } |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 378 | } |