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