blob: 29a8da0d421c4a959dc63f72176a27924d6cedab [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
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040018import android.content.Context;
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040019import android.content.res.TypedArray;
Jason Monk3e189872016-01-12 09:10:34 -050020import android.util.ArraySet;
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040021import android.util.AttributeSet;
Jason Monk3e189872016-01-12 09:10:34 -050022import android.view.View;
Jason Monkabe19742015-09-29 09:47:06 -040023import android.widget.ImageView;
Winsonc0d70582016-01-29 10:24:39 -080024
Jason Monk3e189872016-01-12 09:10:34 -050025import com.android.systemui.statusbar.phone.StatusBarIconController;
Jorim Jaggi708f7722014-08-20 17:30:38 +020026import com.android.systemui.statusbar.policy.BatteryController;
Jason Monk3e189872016-01-12 09:10:34 -050027import com.android.systemui.tuner.TunerService;
Jorim Jaggi708f7722014-08-20 17:30:38 +020028
Jason Monk3e189872016-01-12 09:10:34 -050029public class BatteryMeterView extends ImageView implements
30 BatteryController.BatteryStateChangeCallback, TunerService.Tunable {
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040031
Jason Monkabe19742015-09-29 09:47:06 -040032 private final BatteryMeterDrawable mDrawable;
Jason Monk3e189872016-01-12 09:10:34 -050033 private final String mSlotBattery;
Jorim Jaggi708f7722014-08-20 17:30:38 +020034 private BatteryController mBatteryController;
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040035
36 public BatteryMeterView(Context context) {
37 this(context, null, 0);
38 }
39
40 public BatteryMeterView(Context context, AttributeSet attrs) {
41 this(context, attrs, 0);
42 }
43
44 public BatteryMeterView(Context context, AttributeSet attrs, int defStyle) {
45 super(context, attrs, defStyle);
46
John Spurlock29786fc2014-02-04 17:55:47 -050047 TypedArray atts = context.obtainStyledAttributes(attrs, R.styleable.BatteryMeterView,
48 defStyle, 0);
49 final int frameColor = atts.getColor(R.styleable.BatteryMeterView_frameColor,
Alan Viverette4a357cd2015-03-18 18:37:18 -070050 context.getColor(R.color.batterymeter_frame_color));
Geoffrey Pitsch959ee222016-09-13 16:07:44 -040051 mDrawable = new BatteryMeterDrawable(context, frameColor);
John Spurlock29786fc2014-02-04 17:55:47 -050052 atts.recycle();
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040053
Jason Monk3e189872016-01-12 09:10:34 -050054 mSlotBattery = context.getString(
55 com.android.internal.R.string.status_bar_battery);
Jason Monkabe19742015-09-29 09:47:06 -040056 setImageDrawable(mDrawable);
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040057 }
John Spurlock3c875662013-08-31 15:07:25 -040058
Jorim Jaggi0d266892014-07-28 14:49:13 +020059 @Override
60 public boolean hasOverlappingRendering() {
61 return false;
62 }
63
Jason Monkabe19742015-09-29 09:47:06 -040064 @Override
Jason Monk3e189872016-01-12 09:10:34 -050065 public void onTuningChanged(String key, String newValue) {
66 if (StatusBarIconController.ICON_BLACKLIST.equals(key)) {
67 ArraySet<String> icons = StatusBarIconController.getIconBlacklist(newValue);
68 setVisibility(icons.contains(mSlotBattery) ? View.GONE : View.VISIBLE);
69 }
70 }
71
72 @Override
Jason Monkabe19742015-09-29 09:47:06 -040073 public void onAttachedToWindow() {
74 super.onAttachedToWindow();
Jason Monk9c7844c2017-01-18 15:21:53 -050075 mBatteryController = Dependency.get(BatteryController.class);
76 mDrawable.setBatteryController(mBatteryController);
77 mBatteryController.addCallback(this);
78 mDrawable.startListening();
Jason Monkde850bb2017-02-01 19:26:30 -050079 Dependency.get(TunerService.class).addTunable(this, StatusBarIconController.ICON_BLACKLIST);
Jason Monkabe19742015-09-29 09:47:06 -040080 }
John Spurlock3c875662013-08-31 15:07:25 -040081
82 @Override
Jason Monkabe19742015-09-29 09:47:06 -040083 public void onDetachedFromWindow() {
84 super.onDetachedFromWindow();
Jason Monk88529052016-11-04 13:29:58 -040085 mBatteryController.removeCallback(this);
Jason Monkabe19742015-09-29 09:47:06 -040086 mDrawable.stopListening();
Jason Monkde850bb2017-02-01 19:26:30 -050087 Dependency.get(TunerService.class).removeTunable(this);
John Spurlock3c875662013-08-31 15:07:25 -040088 }
John Spurlockf40d08f2015-05-29 10:48:22 -040089
Jason Monkabe19742015-09-29 09:47:06 -040090 @Override
91 public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
92 setContentDescription(
Adrian Roos70dcf832016-04-20 15:51:42 -070093 getContext().getString(charging ? R.string.accessibility_battery_level_charging
94 : R.string.accessibility_battery_level, level));
John Spurlockf40d08f2015-05-29 10:48:22 -040095 }
96
Jason Monkabe19742015-09-29 09:47:06 -040097 @Override
Jason Monkc06fbb12016-01-08 14:12:18 -050098 public void onPowerSaveChanged(boolean isPowerSave) {
John Spurlockf40d08f2015-05-29 10:48:22 -040099
John Spurlockf40d08f2015-05-29 10:48:22 -0400100 }
101
Jason Monkabe19742015-09-29 09:47:06 -0400102 public void setDarkIntensity(float f) {
103 mDrawable.setDarkIntensity(f);
104 }
Jason Monk32508852017-01-18 09:17:13 -0500105
106 public void setRawColors(int fgColor, int bgColor) {
107 mDrawable.setColors(fgColor, bgColor);
108 }
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -0400109}