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