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; |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 20 | import android.os.Handler; |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 21 | import android.util.AttributeSet; |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 22 | import android.widget.ImageView; |
Jorim Jaggi | 708f772 | 2014-08-20 17:30:38 +0200 | [diff] [blame] | 23 | import com.android.systemui.statusbar.policy.BatteryController; |
| 24 | |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 25 | public class BatteryMeterView extends ImageView implements BatteryController.BatteryStateChangeCallback { |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 26 | |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 27 | private final BatteryMeterDrawable mDrawable; |
Jorim Jaggi | 708f772 | 2014-08-20 17:30:38 +0200 | [diff] [blame] | 28 | private BatteryController mBatteryController; |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 29 | |
| 30 | public BatteryMeterView(Context context) { |
| 31 | this(context, null, 0); |
| 32 | } |
| 33 | |
| 34 | public BatteryMeterView(Context context, AttributeSet attrs) { |
| 35 | this(context, attrs, 0); |
| 36 | } |
| 37 | |
| 38 | public BatteryMeterView(Context context, AttributeSet attrs, int defStyle) { |
| 39 | super(context, attrs, defStyle); |
| 40 | |
John Spurlock | 29786fc | 2014-02-04 17:55:47 -0500 | [diff] [blame] | 41 | TypedArray atts = context.obtainStyledAttributes(attrs, R.styleable.BatteryMeterView, |
| 42 | defStyle, 0); |
| 43 | final int frameColor = atts.getColor(R.styleable.BatteryMeterView_frameColor, |
Alan Viverette | 4a357cd | 2015-03-18 18:37:18 -0700 | [diff] [blame] | 44 | context.getColor(R.color.batterymeter_frame_color)); |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 45 | mDrawable = new BatteryMeterDrawable(context, new Handler(), frameColor); |
John Spurlock | 29786fc | 2014-02-04 17:55:47 -0500 | [diff] [blame] | 46 | atts.recycle(); |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 47 | |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 48 | setImageDrawable(mDrawable); |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 49 | } |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 50 | |
Jorim Jaggi | 0d26689 | 2014-07-28 14:49:13 +0200 | [diff] [blame] | 51 | @Override |
| 52 | public boolean hasOverlappingRendering() { |
| 53 | return false; |
| 54 | } |
| 55 | |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 56 | @Override |
| 57 | public void onAttachedToWindow() { |
| 58 | super.onAttachedToWindow(); |
| 59 | mBatteryController.addStateChangedCallback(this); |
| 60 | mDrawable.startListening(); |
| 61 | } |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 62 | |
| 63 | @Override |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 64 | public void onDetachedFromWindow() { |
| 65 | super.onDetachedFromWindow(); |
| 66 | mBatteryController.removeStateChangedCallback(this); |
| 67 | mDrawable.stopListening(); |
John Spurlock | 3c87566 | 2013-08-31 15:07:25 -0400 | [diff] [blame] | 68 | } |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 69 | |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 70 | @Override |
| 71 | public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) { |
| 72 | setContentDescription( |
| 73 | getContext().getString(R.string.accessibility_battery_level, level)); |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 74 | } |
| 75 | |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 76 | @Override |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame^] | 77 | public void onPowerSaveChanged(boolean isPowerSave) { |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 78 | |
John Spurlock | f40d08f | 2015-05-29 10:48:22 -0400 | [diff] [blame] | 79 | } |
| 80 | |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 81 | public void setBatteryController(BatteryController mBatteryController) { |
| 82 | this.mBatteryController = mBatteryController; |
| 83 | mDrawable.setBatteryController(mBatteryController); |
| 84 | } |
| 85 | |
| 86 | public void setDarkIntensity(float f) { |
| 87 | mDrawable.setDarkIntensity(f); |
| 88 | } |
Daniel Sandler | dfaf3bd | 2013-04-12 01:39:02 -0400 | [diff] [blame] | 89 | } |