blob: 14f2c4aea6673d762e10106bb36af8354436e873 [file] [log] [blame]
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -04001/*
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 Sandlerdfaf3bd2013-04-12 01:39:02 -040016package com.android.systemui;
17
Evan Lairdbcf631d2017-03-10 10:56:45 -050018import static android.provider.Settings.System.SHOW_BATTERY_PERCENT;
19
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040020import android.content.Context;
Jason Monkaa573e92017-01-27 17:00:29 -050021import android.content.res.Resources;
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040022import android.content.res.TypedArray;
Jason Monkaa573e92017-01-27 17:00:29 -050023import android.graphics.Rect;
Jason Monk3e189872016-01-12 09:10:34 -050024import android.util.ArraySet;
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040025import android.util.AttributeSet;
Jason Monkaa573e92017-01-27 17:00:29 -050026import android.util.TypedValue;
Dan Sandler055bb612017-02-08 16:21:49 -080027import android.database.ContentObserver;
28import android.net.Uri;
29import android.os.Handler;
30import android.provider.Settings;
Dan Sandler055bb612017-02-08 16:21:49 -080031import android.view.Gravity;
32import android.view.LayoutInflater;
Jason Monk3e189872016-01-12 09:10:34 -050033import android.view.View;
Dan Sandler055bb612017-02-08 16:21:49 -080034import android.view.ViewGroup;
Jason Monkabe19742015-09-29 09:47:06 -040035import android.widget.ImageView;
Jason Monkaa573e92017-01-27 17:00:29 -050036import android.widget.LinearLayout;
Winsonc0d70582016-01-29 10:24:39 -080037
Dan Sandler055bb612017-02-08 16:21:49 -080038import android.widget.TextView;
39import com.android.settingslib.graph.BatteryMeterDrawableBase;
Jason Monk3e189872016-01-12 09:10:34 -050040import com.android.systemui.statusbar.phone.StatusBarIconController;
Jorim Jaggi708f7722014-08-20 17:30:38 +020041import com.android.systemui.statusbar.policy.BatteryController;
Jason Monkaa573e92017-01-27 17:00:29 -050042import com.android.systemui.statusbar.policy.BatteryController.BatteryStateChangeCallback;
43import com.android.systemui.statusbar.policy.ConfigurationController;
44import com.android.systemui.statusbar.policy.ConfigurationController.ConfigurationListener;
45import com.android.systemui.statusbar.policy.DarkIconDispatcher;
46import com.android.systemui.statusbar.policy.DarkIconDispatcher.DarkReceiver;
Jason Monk3e189872016-01-12 09:10:34 -050047import com.android.systemui.tuner.TunerService;
Jason Monkaa573e92017-01-27 17:00:29 -050048import com.android.systemui.tuner.TunerService.Tunable;
Jorim Jaggi708f7722014-08-20 17:30:38 +020049
Dan Sandler055bb612017-02-08 16:21:49 -080050import java.text.NumberFormat;
51
52public class BatteryMeterView extends LinearLayout implements
Jason Monkaa573e92017-01-27 17:00:29 -050053 BatteryStateChangeCallback, Tunable, DarkReceiver, ConfigurationListener {
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040054
Dan Sandler055bb612017-02-08 16:21:49 -080055 private final BatteryMeterDrawableBase mDrawable;
Jason Monk3e189872016-01-12 09:10:34 -050056 private final String mSlotBattery;
Dan Sandler055bb612017-02-08 16:21:49 -080057 private final ImageView mBatteryIconView;
58 private TextView mBatteryPercentView;
59
Jorim Jaggi708f7722014-08-20 17:30:38 +020060 private BatteryController mBatteryController;
Dan Sandler055bb612017-02-08 16:21:49 -080061 private SettingObserver mSettingObserver;
62 private int mTextColor;
63 private int mLevel;
Jason Monkf13413e2017-02-15 15:49:32 -050064 private boolean mForceShowPercent;
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040065
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 Sandler055bb612017-02-08 16:21:49 -080077 setOrientation(LinearLayout.HORIZONTAL);
78 setGravity(Gravity.CENTER_VERTICAL | Gravity.START);
79
John Spurlock29786fc2014-02-04 17:55:47 -050080 TypedArray atts = context.obtainStyledAttributes(attrs, R.styleable.BatteryMeterView,
81 defStyle, 0);
82 final int frameColor = atts.getColor(R.styleable.BatteryMeterView_frameColor,
Alan Viverette4a357cd2015-03-18 18:37:18 -070083 context.getColor(R.color.batterymeter_frame_color));
Dan Sandler055bb612017-02-08 16:21:49 -080084 mDrawable = new BatteryMeterDrawableBase(context, frameColor);
John Spurlock29786fc2014-02-04 17:55:47 -050085 atts.recycle();
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040086
Dan Sandler055bb612017-02-08 16:21:49 -080087 mSettingObserver = new SettingObserver(new Handler(context.getMainLooper()));
88
Jason Monk3e189872016-01-12 09:10:34 -050089 mSlotBattery = context.getString(
90 com.android.internal.R.string.status_bar_battery);
Dan Sandler055bb612017-02-08 16:21:49 -080091 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 Sandlerdf14c202017-02-21 14:51:11 -0500103 public void setForceShowPercent(boolean show) {
104 mForceShowPercent = show;
Jason Monkf13413e2017-02-15 15:49:32 -0500105 updateShowPercent();
106 }
107
Dan Sandler055bb612017-02-08 16:21:49 -0800108 // StatusBarIconController reaches in here and adjusts the layout parameters of the icon
109 public ImageView getBatteryIconView() {
110 return mBatteryIconView;
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -0400111 }
John Spurlock3c875662013-08-31 15:07:25 -0400112
Jorim Jaggi0d266892014-07-28 14:49:13 +0200113 @Override
114 public boolean hasOverlappingRendering() {
115 return false;
116 }
117
Jason Monkabe19742015-09-29 09:47:06 -0400118 @Override
Jason Monk3e189872016-01-12 09:10:34 -0500119 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 Monkabe19742015-09-29 09:47:06 -0400127 public void onAttachedToWindow() {
128 super.onAttachedToWindow();
Jason Monk9c7844c2017-01-18 15:21:53 -0500129 mBatteryController = Dependency.get(BatteryController.class);
Jason Monk9c7844c2017-01-18 15:21:53 -0500130 mBatteryController.addCallback(this);
Dan Sandler055bb612017-02-08 16:21:49 -0800131 getContext().getContentResolver().registerContentObserver(
Evan Lairdbcf631d2017-03-10 10:56:45 -0500132 Settings.System.getUriFor(SHOW_BATTERY_PERCENT), false, mSettingObserver);
Dan Sandler055bb612017-02-08 16:21:49 -0800133 updateShowPercent();
Jason Monkde850bb2017-02-01 19:26:30 -0500134 Dependency.get(TunerService.class).addTunable(this, StatusBarIconController.ICON_BLACKLIST);
Jason Monkaa573e92017-01-27 17:00:29 -0500135 Dependency.get(ConfigurationController.class).addCallback(this);
Jason Monkabe19742015-09-29 09:47:06 -0400136 }
John Spurlock3c875662013-08-31 15:07:25 -0400137
138 @Override
Jason Monkabe19742015-09-29 09:47:06 -0400139 public void onDetachedFromWindow() {
140 super.onDetachedFromWindow();
Jason Monk88529052016-11-04 13:29:58 -0400141 mBatteryController.removeCallback(this);
Dan Sandler055bb612017-02-08 16:21:49 -0800142 getContext().getContentResolver().unregisterContentObserver(mSettingObserver);
Jason Monkde850bb2017-02-01 19:26:30 -0500143 Dependency.get(TunerService.class).removeTunable(this);
Jason Monkaa573e92017-01-27 17:00:29 -0500144 Dependency.get(ConfigurationController.class).removeCallback(this);
John Spurlock3c875662013-08-31 15:07:25 -0400145 }
John Spurlockf40d08f2015-05-29 10:48:22 -0400146
Jason Monkabe19742015-09-29 09:47:06 -0400147 @Override
148 public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
Dan Sandler055bb612017-02-08 16:21:49 -0800149 mDrawable.setBatteryLevel(level);
150 mDrawable.setPluggedIn(pluggedIn);
151 mLevel = level;
152 updatePercentText();
Jason Monkabe19742015-09-29 09:47:06 -0400153 setContentDescription(
Adrian Roos70dcf832016-04-20 15:51:42 -0700154 getContext().getString(charging ? R.string.accessibility_battery_level_charging
155 : R.string.accessibility_battery_level, level));
John Spurlockf40d08f2015-05-29 10:48:22 -0400156 }
157
Jason Monkabe19742015-09-29 09:47:06 -0400158 @Override
Jason Monkc06fbb12016-01-08 14:12:18 -0500159 public void onPowerSaveChanged(boolean isPowerSave) {
Dan Sandler055bb612017-02-08 16:21:49 -0800160 mDrawable.setPowerSave(isPowerSave);
161 }
John Spurlockf40d08f2015-05-29 10:48:22 -0400162
Dan Sandler055bb612017-02-08 16:21:49 -0800163 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(),
Evan Lairdbcf631d2017-03-10 10:56:45 -0500178 SHOW_BATTERY_PERCENT, 0) || mForceShowPercent) {
Dan Sandler055bb612017-02-08 16:21:49 -0800179 if (!showing) {
180 mBatteryPercentView = loadPercentView();
181 if (mTextColor != 0) mBatteryPercentView.setTextColor(mTextColor);
182 updatePercentText();
183 addView(mBatteryPercentView,
Dan Sandler055bb612017-02-08 16:21:49 -0800184 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 Spurlockf40d08f2015-05-29 10:48:22 -0400194 }
195
Jason Monkaa573e92017-01-27 17:00:29 -0500196 @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 Sandler055bb612017-02-08 16:21:49 -0800217 scaledLayoutParams.setMargins(0, 0, 0, marginBottom);
Jason Monkaa573e92017-01-27 17:00:29 -0500218
Dan Sandler055bb612017-02-08 16:21:49 -0800219 mBatteryIconView.setLayoutParams(scaledLayoutParams);
Jason Monkaa573e92017-01-27 17:00:29 -0500220 }
221
222 @Override
223 public void onDarkChanged(Rect area, float darkIntensity, int tint) {
224 mDrawable.setDarkIntensity(DarkIconDispatcher.isInArea(area, this) ? darkIntensity : 0);
Dan Sandler055bb612017-02-08 16:21:49 -0800225 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 Monkabe19742015-09-29 09:47:06 -0400233 }
Jason Monk32508852017-01-18 09:17:13 -0500234
235 public void setRawColors(int fgColor, int bgColor) {
236 mDrawable.setColors(fgColor, bgColor);
237 }
Dan Sandler055bb612017-02-08 16:21:49 -0800238
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 Sandlerdfaf3bd2013-04-12 01:39:02 -0400250}