blob: d8b95cc28b57c1253015c88559b074d174833a7d [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;
John Spurlockf40d08f2015-05-29 10:48:22 -040020import android.os.Handler;
Jason Monk3e189872016-01-12 09:10:34 -050021import android.util.ArraySet;
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040022import android.util.AttributeSet;
Jason Monk3e189872016-01-12 09:10:34 -050023import android.view.View;
Jason Monkabe19742015-09-29 09:47:06 -040024import android.widget.ImageView;
Winsonc0d70582016-01-29 10:24:39 -080025
Jason Monk3e189872016-01-12 09:10:34 -050026import com.android.systemui.statusbar.phone.StatusBarIconController;
Jorim Jaggi708f7722014-08-20 17:30:38 +020027import com.android.systemui.statusbar.policy.BatteryController;
Jason Monk3e189872016-01-12 09:10:34 -050028import com.android.systemui.tuner.TunerService;
Jorim Jaggi708f7722014-08-20 17:30:38 +020029
Jason Monk3e189872016-01-12 09:10:34 -050030public class BatteryMeterView extends ImageView implements
31 BatteryController.BatteryStateChangeCallback, TunerService.Tunable {
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040032
Jason Monkabe19742015-09-29 09:47:06 -040033 private final BatteryMeterDrawable mDrawable;
Jason Monk3e189872016-01-12 09:10:34 -050034 private final String mSlotBattery;
Jorim Jaggi708f7722014-08-20 17:30:38 +020035 private BatteryController mBatteryController;
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040036
37 public BatteryMeterView(Context context) {
38 this(context, null, 0);
39 }
40
41 public BatteryMeterView(Context context, AttributeSet attrs) {
42 this(context, attrs, 0);
43 }
44
45 public BatteryMeterView(Context context, AttributeSet attrs, int defStyle) {
46 super(context, attrs, defStyle);
47
John Spurlock29786fc2014-02-04 17:55:47 -050048 TypedArray atts = context.obtainStyledAttributes(attrs, R.styleable.BatteryMeterView,
49 defStyle, 0);
50 final int frameColor = atts.getColor(R.styleable.BatteryMeterView_frameColor,
Alan Viverette4a357cd2015-03-18 18:37:18 -070051 context.getColor(R.color.batterymeter_frame_color));
Jason Monkabe19742015-09-29 09:47:06 -040052 mDrawable = new BatteryMeterDrawable(context, new Handler(), frameColor);
John Spurlock29786fc2014-02-04 17:55:47 -050053 atts.recycle();
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040054
Jason Monk3e189872016-01-12 09:10:34 -050055 mSlotBattery = context.getString(
56 com.android.internal.R.string.status_bar_battery);
Jason Monkabe19742015-09-29 09:47:06 -040057 setImageDrawable(mDrawable);
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -040058 }
John Spurlock3c875662013-08-31 15:07:25 -040059
Jorim Jaggi0d266892014-07-28 14:49:13 +020060 @Override
61 public boolean hasOverlappingRendering() {
62 return false;
63 }
64
Jason Monkabe19742015-09-29 09:47:06 -040065 @Override
Jason Monk3e189872016-01-12 09:10:34 -050066 public void onTuningChanged(String key, String newValue) {
67 if (StatusBarIconController.ICON_BLACKLIST.equals(key)) {
68 ArraySet<String> icons = StatusBarIconController.getIconBlacklist(newValue);
69 setVisibility(icons.contains(mSlotBattery) ? View.GONE : View.VISIBLE);
70 }
71 }
72
73 @Override
Jason Monkabe19742015-09-29 09:47:06 -040074 public void onAttachedToWindow() {
75 super.onAttachedToWindow();
76 mBatteryController.addStateChangedCallback(this);
77 mDrawable.startListening();
Jason Monk3e189872016-01-12 09:10:34 -050078 TunerService.get(getContext()).addTunable(this, StatusBarIconController.ICON_BLACKLIST);
Jason Monkabe19742015-09-29 09:47:06 -040079 }
John Spurlock3c875662013-08-31 15:07:25 -040080
81 @Override
Jason Monkabe19742015-09-29 09:47:06 -040082 public void onDetachedFromWindow() {
83 super.onDetachedFromWindow();
84 mBatteryController.removeStateChangedCallback(this);
85 mDrawable.stopListening();
Jason Monk3e189872016-01-12 09:10:34 -050086 TunerService.get(getContext()).removeTunable(this);
John Spurlock3c875662013-08-31 15:07:25 -040087 }
John Spurlockf40d08f2015-05-29 10:48:22 -040088
Jason Monkabe19742015-09-29 09:47:06 -040089 @Override
90 public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
91 setContentDescription(
Adrian Roos70dcf832016-04-20 15:51:42 -070092 getContext().getString(charging ? R.string.accessibility_battery_level_charging
93 : R.string.accessibility_battery_level, level));
John Spurlockf40d08f2015-05-29 10:48:22 -040094 }
95
Jason Monkabe19742015-09-29 09:47:06 -040096 @Override
Jason Monkc06fbb12016-01-08 14:12:18 -050097 public void onPowerSaveChanged(boolean isPowerSave) {
John Spurlockf40d08f2015-05-29 10:48:22 -040098
John Spurlockf40d08f2015-05-29 10:48:22 -040099 }
100
Jason Monkabe19742015-09-29 09:47:06 -0400101 public void setBatteryController(BatteryController mBatteryController) {
102 this.mBatteryController = mBatteryController;
103 mDrawable.setBatteryController(mBatteryController);
104 }
105
106 public void setDarkIntensity(float f) {
107 mDrawable.setDarkIntensity(f);
108 }
Daniel Sandlerdfaf3bd2013-04-12 01:39:02 -0400109}