2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.fuelgauge;
19 import android.annotation.Nullable;
20 import android.content.Context;
21 import android.graphics.Color;
22 import android.graphics.ColorFilter;
23 import android.graphics.PorterDuff;
24 import android.graphics.PorterDuffColorFilter;
25 import android.support.annotation.VisibleForTesting;
26 import android.util.AttributeSet;
27 import android.widget.ImageView;
29 import com.android.settings.R;
30 import com.android.settings.Utils;
31 import com.android.settingslib.graph.BatteryMeterDrawableBase;
33 public class BatteryMeterView extends ImageView {
35 BatteryMeterDrawable mDrawable;
37 ColorFilter mErrorColorFilter;
39 ColorFilter mAccentColorFilter;
41 public BatteryMeterView(Context context) {
42 this(context, null, 0);
45 public BatteryMeterView(Context context, @Nullable AttributeSet attrs) {
46 this(context, attrs, 0);
49 public BatteryMeterView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
50 super(context, attrs, defStyleAttr);
52 final int frameColor = context.getColor(R.color.meter_background_color);
53 mAccentColorFilter = new PorterDuffColorFilter(
54 Utils.getColorAttr(context, android.R.attr.colorAccent), PorterDuff.Mode.SRC_IN);
55 mErrorColorFilter = new PorterDuffColorFilter(
56 context.getColor(R.color.battery_icon_color_error), PorterDuff.Mode.SRC_IN);
58 mDrawable = new BatteryMeterDrawable(context, frameColor);
59 mDrawable.setShowPercent(false);
60 mDrawable.setBatteryColorFilter(mAccentColorFilter);
61 mDrawable.setWarningColorFilter(
62 new PorterDuffColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN));
63 setImageDrawable(mDrawable);
66 public void setBatteryLevel(int level) {
67 mDrawable.setBatteryLevel(level);
68 if (level < mDrawable.getCriticalLevel()) {
69 mDrawable.setBatteryColorFilter(mErrorColorFilter);
71 mDrawable.setBatteryColorFilter(mAccentColorFilter);
75 public void setCharging(boolean charging) {
76 mDrawable.setCharging(charging);
79 public static class BatteryMeterDrawable extends BatteryMeterDrawableBase {
80 private final int mIntrinsicWidth;
81 private final int mIntrinsicHeight;
83 public BatteryMeterDrawable(Context context, int frameColor) {
84 super(context, frameColor);
86 mIntrinsicWidth = context.getResources()
87 .getDimensionPixelSize(R.dimen.battery_meter_width);
88 mIntrinsicHeight = context.getResources()
89 .getDimensionPixelSize(R.dimen.battery_meter_height);
93 public int getIntrinsicWidth() {
94 return mIntrinsicWidth;
98 public int getIntrinsicHeight() {
99 return mIntrinsicHeight;
102 public void setWarningColorFilter(@Nullable ColorFilter colorFilter) {
103 mWarningTextPaint.setColorFilter(colorFilter);
106 public void setBatteryColorFilter(@Nullable ColorFilter colorFilter) {
107 mFramePaint.setColorFilter(colorFilter);
108 mBatteryPaint.setColorFilter(colorFilter);
109 mBoltPaint.setColorFilter(colorFilter);