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