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; |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 19 | import android.content.res.TypedArray; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 20 | import android.util.ArraySet; |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 21 | import android.util.AttributeSet; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 22 | import android.view.View; |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 23 | import android.widget.ImageView; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 24 | |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 25 | import com.android.systemui.statusbar.phone.StatusBarIconController; |
Jorim Jaggi | 708f772 | 2014-08-20 17:30:38 +0200 | [diff] [blame] | 26 | import com.android.systemui.statusbar.policy.BatteryController; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 27 | import com.android.systemui.tuner.TunerService; |
Jorim Jaggi | 708f772 | 2014-08-20 17:30:38 +0200 | [diff] [blame] | 28 | |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 29 | public class BatteryMeterView extends ImageView implements |
| 30 | BatteryController.BatteryStateChangeCallback, TunerService.Tunable { |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 31 | |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 32 | private final BatteryMeterDrawable mDrawable; |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 33 | private final String mSlotBattery; |
Jorim Jaggi | 708f772 | 2014-08-20 17:30:38 +0200 | [diff] [blame] | 34 | private BatteryController mBatteryController; |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 35 | |
| 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 Spurlock | 29786fc | 2014-02-04 17:55:47 -0500 | [diff] [blame] | 47 | TypedArray atts = context.obtainStyledAttributes(attrs, R.styleable.BatteryMeterView, |
| 48 | defStyle, 0); |
| 49 | final int frameColor = atts.getColor(R.styleable.BatteryMeterView_frameColor, |
Alan Viverette | 4a357cd | 2015-03-18 18:37:18 -0700 | [diff] [blame] | 50 | context.getColor(R.color.batterymeter_frame_color)); |
Geoffrey Pitsch | 959ee22 | 2016-09-13 16:07:44 -0400 | [diff] [blame] | 51 | mDrawable = new BatteryMeterDrawable(context, frameColor); |
John Spurlock | 29786fc | 2014-02-04 17:55:47 -0500 | [diff] [blame] | 52 | atts.recycle(); |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 53 | |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 54 | mSlotBattery = context.getString( |
| 55 | com.android.internal.R.string.status_bar_battery); |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 56 | setImageDrawable(mDrawable); |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 57 | } |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 58 | |
Jorim Jaggi | 0d26689 | 2014-07-28 14:49:13 +0200 | [diff] [blame] | 59 | @Override |
| 60 | public boolean hasOverlappingRendering() { |
| 61 | return false; |
| 62 | } |
| 63 | |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 64 | @Override |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 65 | 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 Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 73 | public void onAttachedToWindow() { |
| 74 | super.onAttachedToWindow(); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame^] | 75 | mBatteryController = Dependency.get(BatteryController.class); |
| 76 | mDrawable.setBatteryController(mBatteryController); |
| 77 | mBatteryController.addCallback(this); |
| 78 | mDrawable.startListening(); |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 79 | TunerService.get(getContext()).addTunable(this, StatusBarIconController.ICON_BLACKLIST); |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 80 | } |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 81 | |
| 82 | @Override |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 83 | public void onDetachedFromWindow() { |
| 84 | super.onDetachedFromWindow(); |
Jason Monk | 8852905 | 2016-11-04 13:29:58 -0400 | [diff] [blame] | 85 | mBatteryController.removeCallback(this); |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 86 | mDrawable.stopListening(); |
Jason Monk | 3e18987 | 2016-01-12 09:10:34 -0500 | [diff] [blame] | 87 | TunerService.get(getContext()).removeTunable(this); |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 88 | } |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 89 | |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 90 | @Override |
| 91 | public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) { |
| 92 | setContentDescription( |
Adrian Roos | 70dcf83 | 2016-04-20 15:51:42 -0700 | [diff] [blame] | 93 | getContext().getString(charging ? R.string.accessibility_battery_level_charging |
| 94 | : R.string.accessibility_battery_level, level)); |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 95 | } |
| 96 | |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 97 | @Override |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 98 | public void onPowerSaveChanged(boolean isPowerSave) { |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 99 | |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 100 | } |
| 101 | |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 102 | public void setDarkIntensity(float f) { |
| 103 | mDrawable.setDarkIntensity(f); |
| 104 | } |
Jason Monk | 3250885 | 2017-01-18 09:17:13 -0500 | [diff] [blame] | 105 | |
| 106 | public void setRawColors(int fgColor, int bgColor) { |
| 107 | mDrawable.setColors(fgColor, bgColor); |
| 108 | } |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 109 | } |