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 | |
Evan Laird | bcf631d | 2017-03-10 10:56:45 -0500 | [diff] [blame] | 18 | import static android.provider.Settings.System.SHOW_BATTERY_PERCENT; |
| 19 | |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame^] | 20 | import android.animation.ArgbEvaluator; |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 21 | import android.content.Context; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 22 | import android.content.res.Resources; |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 23 | import android.content.res.TypedArray; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 24 | import android.database.ContentObserver; |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame^] | 25 | import android.graphics.Rect; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 26 | import android.net.Uri; |
| 27 | import android.os.Handler; |
| 28 | import android.provider.Settings; |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame^] | 29 | import android.util.ArraySet; |
| 30 | import android.util.AttributeSet; |
| 31 | import android.util.TypedValue; |
| 32 | import android.view.ContextThemeWrapper; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 33 | import android.view.Gravity; |
| 34 | import android.view.LayoutInflater; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 35 | import android.view.View; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 36 | import android.view.ViewGroup; |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 37 | import android.widget.ImageView; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 38 | import android.widget.LinearLayout; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 39 | import android.widget.TextView; |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame^] | 40 | |
| 41 | import com.android.settingslib.Utils; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 42 | import com.android.settingslib.graph.BatteryMeterDrawableBase; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 43 | import com.android.systemui.statusbar.phone.StatusBarIconController; |
Jorim Jaggi | 708f772 | 2014-08-20 17:30:38 +0200 | [diff] [blame] | 44 | import com.android.systemui.statusbar.policy.BatteryController; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 45 | import com.android.systemui.statusbar.policy.BatteryController.BatteryStateChangeCallback; |
| 46 | import com.android.systemui.statusbar.policy.ConfigurationController; |
| 47 | import com.android.systemui.statusbar.policy.ConfigurationController.ConfigurationListener; |
| 48 | import com.android.systemui.statusbar.policy.DarkIconDispatcher; |
| 49 | import com.android.systemui.statusbar.policy.DarkIconDispatcher.DarkReceiver; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 50 | import com.android.systemui.tuner.TunerService; |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 51 | import com.android.systemui.tuner.TunerService.Tunable; |
Jorim Jaggi | 708f772 | 2014-08-20 17:30:38 +0200 | [diff] [blame] | 52 | |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 53 | import java.text.NumberFormat; |
| 54 | |
| 55 | public class BatteryMeterView extends LinearLayout implements |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 56 | BatteryStateChangeCallback, Tunable, DarkReceiver, ConfigurationListener { |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 57 | |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 58 | private final BatteryMeterDrawableBase mDrawable; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 59 | private final String mSlotBattery; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 60 | private final ImageView mBatteryIconView; |
| 61 | private TextView mBatteryPercentView; |
| 62 | |
Jorim Jaggi | 708f772 | 2014-08-20 17:30:38 +0200 | [diff] [blame] | 63 | private BatteryController mBatteryController; |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 64 | private SettingObserver mSettingObserver; |
| 65 | private int mTextColor; |
| 66 | private int mLevel; |
Jason Monk | f13413e | 2017-02-15 15:49:32 -0500 | [diff] [blame] | 67 | private boolean mForceShowPercent; |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 68 | |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame^] | 69 | private int mDarkModeBackgroundColor; |
| 70 | private int mDarkModeFillColor; |
| 71 | |
| 72 | private int mLightModeBackgroundColor; |
| 73 | private int mLightModeFillColor; |
| 74 | |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 75 | public BatteryMeterView(Context context) { |
| 76 | this(context, null, 0); |
| 77 | } |
| 78 | |
| 79 | public BatteryMeterView(Context context, AttributeSet attrs) { |
| 80 | this(context, attrs, 0); |
| 81 | } |
| 82 | |
| 83 | public BatteryMeterView(Context context, AttributeSet attrs, int defStyle) { |
| 84 | super(context, attrs, defStyle); |
| 85 | |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 86 | setOrientation(LinearLayout.HORIZONTAL); |
| 87 | setGravity(Gravity.CENTER_VERTICAL | Gravity.START); |
| 88 | |
John Spurlock | 29786fc | 2014-02-04 17:55:47 -0500 | [diff] [blame] | 89 | TypedArray atts = context.obtainStyledAttributes(attrs, R.styleable.BatteryMeterView, |
| 90 | defStyle, 0); |
| 91 | final int frameColor = atts.getColor(R.styleable.BatteryMeterView_frameColor, |
Alan Viverette | 4a357cd | 2015-03-18 18:37:18 -0700 | [diff] [blame] | 92 | context.getColor(R.color.batterymeter_frame_color)); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 93 | mDrawable = new BatteryMeterDrawableBase(context, frameColor); |
John Spurlock | 29786fc | 2014-02-04 17:55:47 -0500 | [diff] [blame] | 94 | atts.recycle(); |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 95 | |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 96 | mSettingObserver = new SettingObserver(new Handler(context.getMainLooper())); |
| 97 | |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 98 | mSlotBattery = context.getString( |
| 99 | com.android.internal.R.string.status_bar_battery); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 100 | mBatteryIconView = new ImageView(context); |
| 101 | mBatteryIconView.setImageDrawable(mDrawable); |
| 102 | final MarginLayoutParams mlp = new MarginLayoutParams( |
| 103 | getResources().getDimensionPixelSize(R.dimen.status_bar_battery_icon_width), |
| 104 | getResources().getDimensionPixelSize(R.dimen.status_bar_battery_icon_height)); |
| 105 | mlp.setMargins(0, 0, 0, |
| 106 | getResources().getDimensionPixelOffset(R.dimen.battery_margin_bottom)); |
| 107 | addView(mBatteryIconView, mlp); |
| 108 | |
| 109 | updateShowPercent(); |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame^] | 110 | |
| 111 | Context dualToneDarkTheme = new ContextThemeWrapper(context, |
| 112 | Utils.getThemeAttr(context, R.attr.darkIconTheme)); |
| 113 | Context dualToneLightTheme = new ContextThemeWrapper(context, |
| 114 | Utils.getThemeAttr(context, R.attr.lightIconTheme)); |
| 115 | mDarkModeBackgroundColor = Utils.getColorAttr(dualToneDarkTheme, R.attr.backgroundColor); |
| 116 | mDarkModeFillColor = Utils.getColorAttr(dualToneDarkTheme, R.attr.fillColor); |
| 117 | mLightModeBackgroundColor = Utils.getColorAttr(dualToneLightTheme, R.attr.backgroundColor); |
| 118 | mLightModeFillColor = Utils.getColorAttr(dualToneLightTheme, R.attr.fillColor); |
| 119 | |
Jason Monk | d866b8a | 2017-03-29 15:30:44 -0400 | [diff] [blame] | 120 | // Init to not dark at all. |
| 121 | onDarkChanged(new Rect(), 0, DarkIconDispatcher.DEFAULT_ICON_TINT); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Dan Sandler | df14c20 | 2017-02-21 14:51:11 -0500 | [diff] [blame] | 124 | public void setForceShowPercent(boolean show) { |
| 125 | mForceShowPercent = show; |
Jason Monk | f13413e | 2017-02-15 15:49:32 -0500 | [diff] [blame] | 126 | updateShowPercent(); |
| 127 | } |
| 128 | |
Jorim Jaggi | 0d26689 | 2014-07-28 14:49:13 +0200 | [diff] [blame] | 129 | @Override |
| 130 | public boolean hasOverlappingRendering() { |
| 131 | return false; |
| 132 | } |
| 133 | |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 134 | @Override |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 135 | public void onTuningChanged(String key, String newValue) { |
| 136 | if (StatusBarIconController.ICON_BLACKLIST.equals(key)) { |
| 137 | ArraySet<String> icons = StatusBarIconController.getIconBlacklist(newValue); |
| 138 | setVisibility(icons.contains(mSlotBattery) ? View.GONE : View.VISIBLE); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | @Override |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 143 | public void onAttachedToWindow() { |
| 144 | super.onAttachedToWindow(); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 145 | mBatteryController = Dependency.get(BatteryController.class); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 146 | mBatteryController.addCallback(this); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 147 | getContext().getContentResolver().registerContentObserver( |
Evan Laird | bcf631d | 2017-03-10 10:56:45 -0500 | [diff] [blame] | 148 | Settings.System.getUriFor(SHOW_BATTERY_PERCENT), false, mSettingObserver); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 149 | updateShowPercent(); |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 150 | Dependency.get(TunerService.class).addTunable(this, StatusBarIconController.ICON_BLACKLIST); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 151 | Dependency.get(ConfigurationController.class).addCallback(this); |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 152 | } |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 153 | |
| 154 | @Override |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 155 | public void onDetachedFromWindow() { |
| 156 | super.onDetachedFromWindow(); |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 157 | mBatteryController.removeCallback(this); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 158 | getContext().getContentResolver().unregisterContentObserver(mSettingObserver); |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 159 | Dependency.get(TunerService.class).removeTunable(this); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 160 | Dependency.get(ConfigurationController.class).removeCallback(this); |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 161 | } |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 162 | |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 163 | @Override |
| 164 | public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) { |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 165 | mDrawable.setBatteryLevel(level); |
jackqdyulei | ff5bd94 | 2017-04-04 10:54:21 -0700 | [diff] [blame] | 166 | mDrawable.setCharging(pluggedIn); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 167 | mLevel = level; |
| 168 | updatePercentText(); |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 169 | setContentDescription( |
Adrian Roos | 70dcf83 | 2016-04-20 15:51:42 -0700 | [diff] [blame] | 170 | getContext().getString(charging ? R.string.accessibility_battery_level_charging |
| 171 | : R.string.accessibility_battery_level, level)); |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 172 | } |
| 173 | |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 174 | @Override |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 175 | public void onPowerSaveChanged(boolean isPowerSave) { |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 176 | mDrawable.setPowerSave(isPowerSave); |
| 177 | } |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 178 | |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 179 | private TextView loadPercentView() { |
| 180 | return (TextView) LayoutInflater.from(getContext()) |
| 181 | .inflate(R.layout.battery_percentage_view, null); |
| 182 | } |
| 183 | |
| 184 | private void updatePercentText() { |
| 185 | if (mBatteryPercentView != null) { |
| 186 | mBatteryPercentView.setText( |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame^] | 187 | NumberFormat.getPercentInstance().format(mLevel / 100f)); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | private void updateShowPercent() { |
| 192 | final boolean showing = mBatteryPercentView != null; |
| 193 | if (0 != Settings.System.getInt(getContext().getContentResolver(), |
Evan Laird | bcf631d | 2017-03-10 10:56:45 -0500 | [diff] [blame] | 194 | SHOW_BATTERY_PERCENT, 0) || mForceShowPercent) { |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 195 | if (!showing) { |
| 196 | mBatteryPercentView = loadPercentView(); |
| 197 | if (mTextColor != 0) mBatteryPercentView.setTextColor(mTextColor); |
| 198 | updatePercentText(); |
| 199 | addView(mBatteryPercentView, |
Evan Laird | 09ebefd | 2017-03-24 10:55:29 -0400 | [diff] [blame] | 200 | 0, |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 201 | new ViewGroup.LayoutParams( |
| 202 | LayoutParams.WRAP_CONTENT, |
| 203 | LayoutParams.MATCH_PARENT)); |
| 204 | } |
| 205 | } else { |
| 206 | if (showing) { |
| 207 | removeView(mBatteryPercentView); |
| 208 | mBatteryPercentView = null; |
| 209 | } |
| 210 | } |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 211 | } |
| 212 | |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 213 | @Override |
| 214 | public void onDensityOrFontScaleChanged() { |
| 215 | scaleBatteryMeterViews(); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Looks up the scale factor for status bar icons and scales the battery view by that amount. |
| 220 | */ |
| 221 | private void scaleBatteryMeterViews() { |
| 222 | Resources res = getContext().getResources(); |
| 223 | TypedValue typedValue = new TypedValue(); |
| 224 | |
| 225 | res.getValue(R.dimen.status_bar_icon_scale_factor, typedValue, true); |
| 226 | float iconScaleFactor = typedValue.getFloat(); |
| 227 | |
| 228 | int batteryHeight = res.getDimensionPixelSize(R.dimen.status_bar_battery_icon_height); |
| 229 | int batteryWidth = res.getDimensionPixelSize(R.dimen.status_bar_battery_icon_width); |
| 230 | int marginBottom = res.getDimensionPixelSize(R.dimen.battery_margin_bottom); |
| 231 | |
| 232 | LinearLayout.LayoutParams scaledLayoutParams = new LinearLayout.LayoutParams( |
| 233 | (int) (batteryWidth * iconScaleFactor), (int) (batteryHeight * iconScaleFactor)); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 234 | scaledLayoutParams.setMargins(0, 0, 0, marginBottom); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 235 | |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 236 | mBatteryIconView.setLayoutParams(scaledLayoutParams); |
Jason Monk | aa573e9 | 2017-01-27 17:00:29 -0500 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public void onDarkChanged(Rect area, float darkIntensity, int tint) { |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame^] | 241 | float intensity = DarkIconDispatcher.isInArea(area, this) ? darkIntensity : 0; |
| 242 | int foreground = getColorForDarkIntensity(intensity, mLightModeFillColor, |
| 243 | mDarkModeFillColor); |
| 244 | int background = getColorForDarkIntensity(intensity, mLightModeBackgroundColor, |
| 245 | mDarkModeBackgroundColor); |
| 246 | mDrawable.setColors(foreground, background); |
| 247 | setTextColor(foreground); |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | public void setTextColor(int color) { |
| 251 | mTextColor = color; |
| 252 | if (mBatteryPercentView != null) { |
| 253 | mBatteryPercentView.setTextColor(color); |
| 254 | } |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 255 | } |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 256 | |
Jason Monk | 9a376bc | 2017-05-10 09:52:10 -0400 | [diff] [blame^] | 257 | private int getColorForDarkIntensity(float darkIntensity, int lightColor, int darkColor) { |
| 258 | return (int) ArgbEvaluator.getInstance().evaluate(darkIntensity, lightColor, darkColor); |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 259 | } |
Dan Sandler | 055bb61 | 2017-02-08 16:21:49 -0800 | [diff] [blame] | 260 | |
| 261 | private final class SettingObserver extends ContentObserver { |
| 262 | public SettingObserver(Handler handler) { |
| 263 | super(handler); |
| 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public void onChange(boolean selfChange, Uri uri) { |
| 268 | super.onChange(selfChange, uri); |
| 269 | updateShowPercent(); |
| 270 | } |
| 271 | } |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 272 | } |