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