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 */ 16 package com.android.systemui; 17 18 import android.content.Context; 19 import android.content.res.TypedArray; 20 import android.os.Handler; 21 import android.util.ArraySet; 22 import android.util.AttributeSet; 23 import android.view.View; 24 import android.widget.ImageView; 25 26 import com.android.systemui.statusbar.phone.StatusBarIconController; 27 import com.android.systemui.statusbar.policy.BatteryController; 28 import com.android.systemui.tuner.TunerService; 29 30 public class BatteryMeterView extends ImageView implements 31 BatteryController.BatteryStateChangeCallback, TunerService.Tunable { 32 33 private final BatteryMeterDrawable mDrawable; 34 private final String mSlotBattery; 35 private BatteryController mBatteryController; 36 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 48 TypedArray atts = context.obtainStyledAttributes(attrs, R.styleable.BatteryMeterView, 49 defStyle, 0); 50 final int frameColor = atts.getColor(R.styleable.BatteryMeterView_frameColor, 51 context.getColor(R.color.batterymeter_frame_color)); 52 mDrawable = new BatteryMeterDrawable(context, new Handler(), frameColor); 53 atts.recycle(); 54 55 mSlotBattery = context.getString( 56 com.android.internal.R.string.status_bar_battery); 57 setImageDrawable(mDrawable); 58 } 59 60 @Override 61 public boolean hasOverlappingRendering() { 62 return false; 63 } 64 65 @Override 66 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 74 public void onAttachedToWindow() { 75 super.onAttachedToWindow(); 76 mBatteryController.addStateChangedCallback(this); 77 mDrawable.startListening(); 78 TunerService.get(getContext()).addTunable(this, StatusBarIconController.ICON_BLACKLIST); 79 } 80 81 @Override 82 public void onDetachedFromWindow() { 83 super.onDetachedFromWindow(); 84 mBatteryController.removeStateChangedCallback(this); 85 mDrawable.stopListening(); 86 TunerService.get(getContext()).removeTunable(this); 87 } 88 89 @Override 90 public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) { 91 setContentDescription( 92 getContext().getString(charging ? R.string.accessibility_battery_level_charging 93 : R.string.accessibility_battery_level, level)); 94 } 95 96 @Override 97 public void onPowerSaveChanged(boolean isPowerSave) { 98 99 } 100 101 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 } 109 } 110