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 static android.provider.Settings.System.SHOW_BATTERY_PERCENT; 19 20 import android.animation.ArgbEvaluator; 21 import android.content.Context; 22 import android.content.res.Resources; 23 import android.content.res.TypedArray; 24 import android.database.ContentObserver; 25 import android.graphics.Rect; 26 import android.net.Uri; 27 import android.os.Handler; 28 import android.provider.Settings; 29 import android.util.ArraySet; 30 import android.util.AttributeSet; 31 import android.util.TypedValue; 32 import android.view.ContextThemeWrapper; 33 import android.view.Gravity; 34 import android.view.LayoutInflater; 35 import android.view.View; 36 import android.view.ViewGroup; 37 import android.widget.ImageView; 38 import android.widget.LinearLayout; 39 import android.widget.TextView; 40 41 import com.android.settingslib.Utils; 42 import com.android.settingslib.graph.BatteryMeterDrawableBase; 43 import com.android.systemui.statusbar.phone.StatusBarIconController; 44 import com.android.systemui.statusbar.policy.BatteryController; 45 import com.android.systemui.statusbar.policy.BatteryController.BatteryStateChangeCallback; 46 import com.android.systemui.statusbar.policy.ConfigurationController; 47 import com.android.systemui.statusbar.policy.ConfigurationController.ConfigurationListener; 48 import com.android.systemui.statusbar.policy.DarkIconDispatcher; 49 import com.android.systemui.statusbar.policy.DarkIconDispatcher.DarkReceiver; 50 import com.android.systemui.tuner.TunerService; 51 import com.android.systemui.tuner.TunerService.Tunable; 52 53 import java.text.NumberFormat; 54 55 public class BatteryMeterView extends LinearLayout implements 56 BatteryStateChangeCallback, Tunable, DarkReceiver, ConfigurationListener { 57 58 private final BatteryMeterDrawableBase mDrawable; 59 private final String mSlotBattery; 60 private final ImageView mBatteryIconView; 61 private TextView mBatteryPercentView; 62 63 private BatteryController mBatteryController; 64 private SettingObserver mSettingObserver; 65 private int mTextColor; 66 private int mLevel; 67 private boolean mForceShowPercent; 68 69 private int mDarkModeBackgroundColor; 70 private int mDarkModeFillColor; 71 72 private int mLightModeBackgroundColor; 73 private int mLightModeFillColor; 74 75 public BatteryMeterView(Context context) { 76 this(context, null, 0); 77 } 78 79 public BatteryMeterView(Context context, AttributeSet attrs) { 80 this(context, attrs, 0); 81 } 82 83 public BatteryMeterView(Context context, AttributeSet attrs, int defStyle) { 84 super(context, attrs, defStyle); 85 86 setOrientation(LinearLayout.HORIZONTAL); 87 setGravity(Gravity.CENTER_VERTICAL | Gravity.START); 88 89 TypedArray atts = context.obtainStyledAttributes(attrs, R.styleable.BatteryMeterView, 90 defStyle, 0); 91 final int frameColor = atts.getColor(R.styleable.BatteryMeterView_frameColor, 92 context.getColor(R.color.meter_background_color)); 93 mDrawable = new BatteryMeterDrawableBase(context, frameColor); 94 atts.recycle(); 95 96 mSettingObserver = new SettingObserver(new Handler(context.getMainLooper())); 97 98 mSlotBattery = context.getString( 99 com.android.internal.R.string.status_bar_battery); 100 mBatteryIconView = new ImageView(context); 101 mBatteryIconView.setImageDrawable(mDrawable); 102 final MarginLayoutParams mlp = new MarginLayoutParams( 103 getResources().getDimensionPixelSize(R.dimen.status_bar_battery_icon_width), 104 getResources().getDimensionPixelSize(R.dimen.status_bar_battery_icon_height)); 105 mlp.setMargins(0, 0, 0, 106 getResources().getDimensionPixelOffset(R.dimen.battery_margin_bottom)); 107 addView(mBatteryIconView, mlp); 108 109 updateShowPercent(); 110 111 Context dualToneDarkTheme = new ContextThemeWrapper(context, 112 Utils.getThemeAttr(context, R.attr.darkIconTheme)); 113 Context dualToneLightTheme = new ContextThemeWrapper(context, 114 Utils.getThemeAttr(context, R.attr.lightIconTheme)); 115 mDarkModeBackgroundColor = Utils.getColorAttr(dualToneDarkTheme, R.attr.backgroundColor); 116 mDarkModeFillColor = Utils.getColorAttr(dualToneDarkTheme, R.attr.fillColor); 117 mLightModeBackgroundColor = Utils.getColorAttr(dualToneLightTheme, R.attr.backgroundColor); 118 mLightModeFillColor = Utils.getColorAttr(dualToneLightTheme, R.attr.fillColor); 119 120 // Init to not dark at all. 121 onDarkChanged(new Rect(), 0, DarkIconDispatcher.DEFAULT_ICON_TINT); 122 } 123 124 public void setForceShowPercent(boolean show) { 125 mForceShowPercent = show; 126 updateShowPercent(); 127 } 128 129 @Override 130 public boolean hasOverlappingRendering() { 131 return false; 132 } 133 134 @Override 135 public void onTuningChanged(String key, String newValue) { 136 if (StatusBarIconController.ICON_BLACKLIST.equals(key)) { 137 ArraySet<String> icons = StatusBarIconController.getIconBlacklist(newValue); 138 setVisibility(icons.contains(mSlotBattery) ? View.GONE : View.VISIBLE); 139 } 140 } 141 142 @Override 143 public void onAttachedToWindow() { 144 super.onAttachedToWindow(); 145 mBatteryController = Dependency.get(BatteryController.class); 146 mBatteryController.addCallback(this); 147 getContext().getContentResolver().registerContentObserver( 148 Settings.System.getUriFor(SHOW_BATTERY_PERCENT), false, mSettingObserver); 149 updateShowPercent(); 150 Dependency.get(TunerService.class).addTunable(this, StatusBarIconController.ICON_BLACKLIST); 151 Dependency.get(ConfigurationController.class).addCallback(this); 152 } 153 154 @Override 155 public void onDetachedFromWindow() { 156 super.onDetachedFromWindow(); 157 mBatteryController.removeCallback(this); 158 getContext().getContentResolver().unregisterContentObserver(mSettingObserver); 159 Dependency.get(TunerService.class).removeTunable(this); 160 Dependency.get(ConfigurationController.class).removeCallback(this); 161 } 162 163 @Override 164 public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) { 165 mDrawable.setBatteryLevel(level); 166 mDrawable.setCharging(pluggedIn); 167 mLevel = level; 168 updatePercentText(); 169 setContentDescription( 170 getContext().getString(charging ? R.string.accessibility_battery_level_charging 171 : R.string.accessibility_battery_level, level)); 172 } 173 174 @Override 175 public void onPowerSaveChanged(boolean isPowerSave) { 176 mDrawable.setPowerSave(isPowerSave); 177 } 178 179 private TextView loadPercentView() { 180 return (TextView) LayoutInflater.from(getContext()) 181 .inflate(R.layout.battery_percentage_view, null); 182 } 183 184 private void updatePercentText() { 185 if (mBatteryPercentView != null) { 186 mBatteryPercentView.setText( 187 NumberFormat.getPercentInstance().format(mLevel / 100f)); 188 } 189 } 190 191 private void updateShowPercent() { 192 final boolean showing = mBatteryPercentView != null; 193 if (0 != Settings.System.getInt(getContext().getContentResolver(), 194 SHOW_BATTERY_PERCENT, 0) || mForceShowPercent) { 195 if (!showing) { 196 mBatteryPercentView = loadPercentView(); 197 if (mTextColor != 0) mBatteryPercentView.setTextColor(mTextColor); 198 updatePercentText(); 199 addView(mBatteryPercentView, 200 0, 201 new ViewGroup.LayoutParams( 202 LayoutParams.WRAP_CONTENT, 203 LayoutParams.MATCH_PARENT)); 204 } 205 } else { 206 if (showing) { 207 removeView(mBatteryPercentView); 208 mBatteryPercentView = null; 209 } 210 } 211 } 212 213 @Override 214 public void onDensityOrFontScaleChanged() { 215 scaleBatteryMeterViews(); 216 } 217 218 /** 219 * Looks up the scale factor for status bar icons and scales the battery view by that amount. 220 */ 221 private void scaleBatteryMeterViews() { 222 Resources res = getContext().getResources(); 223 TypedValue typedValue = new TypedValue(); 224 225 res.getValue(R.dimen.status_bar_icon_scale_factor, typedValue, true); 226 float iconScaleFactor = typedValue.getFloat(); 227 228 int batteryHeight = res.getDimensionPixelSize(R.dimen.status_bar_battery_icon_height); 229 int batteryWidth = res.getDimensionPixelSize(R.dimen.status_bar_battery_icon_width); 230 int marginBottom = res.getDimensionPixelSize(R.dimen.battery_margin_bottom); 231 232 LinearLayout.LayoutParams scaledLayoutParams = new LinearLayout.LayoutParams( 233 (int) (batteryWidth * iconScaleFactor), (int) (batteryHeight * iconScaleFactor)); 234 scaledLayoutParams.setMargins(0, 0, 0, marginBottom); 235 236 mBatteryIconView.setLayoutParams(scaledLayoutParams); 237 FontSizeUtils.updateFontSize(mBatteryPercentView, R.dimen.qs_time_expanded_size); 238 } 239 240 @Override 241 public void onDarkChanged(Rect area, float darkIntensity, int tint) { 242 float intensity = DarkIconDispatcher.isInArea(area, this) ? darkIntensity : 0; 243 int foreground = getColorForDarkIntensity(intensity, mLightModeFillColor, 244 mDarkModeFillColor); 245 int background = getColorForDarkIntensity(intensity, mLightModeBackgroundColor, 246 mDarkModeBackgroundColor); 247 mDrawable.setColors(foreground, background); 248 setTextColor(foreground); 249 } 250 251 public void setTextColor(int color) { 252 mTextColor = color; 253 if (mBatteryPercentView != null) { 254 mBatteryPercentView.setTextColor(color); 255 } 256 } 257 258 private int getColorForDarkIntensity(float darkIntensity, int lightColor, int darkColor) { 259 return (int) ArgbEvaluator.getInstance().evaluate(darkIntensity, lightColor, darkColor); 260 } 261 262 private final class SettingObserver extends ContentObserver { 263 public SettingObserver(Handler handler) { 264 super(handler); 265 } 266 267 @Override 268 public void onChange(boolean selfChange, Uri uri) { 269 super.onChange(selfChange, uri); 270 updateShowPercent(); 271 } 272 } 273 } 274