1 /* 2 * Copyright (C) 2017 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 17 package com.android.settings.fuelgauge; 18 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; 28 29 import com.android.settings.R; 30 import com.android.settings.Utils; 31 import com.android.settingslib.graph.BatteryMeterDrawableBase; 32 33 public class BatteryMeterView extends ImageView { 34 @VisibleForTesting 35 BatteryMeterDrawable mDrawable; 36 @VisibleForTesting 37 ColorFilter mErrorColorFilter; 38 @VisibleForTesting 39 ColorFilter mAccentColorFilter; 40 41 public BatteryMeterView(Context context) { 42 this(context, null, 0); 43 } 44 45 public BatteryMeterView(Context context, @Nullable AttributeSet attrs) { 46 this(context, attrs, 0); 47 } 48 49 public BatteryMeterView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 50 super(context, attrs, defStyleAttr); 51 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); 57 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); 64 } 65 66 public void setBatteryLevel(int level) { 67 mDrawable.setBatteryLevel(level); 68 if (level < mDrawable.getCriticalLevel()) { 69 mDrawable.setBatteryColorFilter(mErrorColorFilter); 70 } else { 71 mDrawable.setBatteryColorFilter(mAccentColorFilter); 72 } 73 } 74 75 public int getBatteryLevel() { 76 return mDrawable.getBatteryLevel(); 77 } 78 79 public void setCharging(boolean charging) { 80 mDrawable.setCharging(charging); 81 postInvalidate(); 82 } 83 84 public boolean getCharging() { 85 return mDrawable.getCharging(); 86 } 87 88 public static class BatteryMeterDrawable extends BatteryMeterDrawableBase { 89 private final int mIntrinsicWidth; 90 private final int mIntrinsicHeight; 91 92 public BatteryMeterDrawable(Context context, int frameColor) { 93 super(context, frameColor); 94 95 mIntrinsicWidth = context.getResources() 96 .getDimensionPixelSize(R.dimen.battery_meter_width); 97 mIntrinsicHeight = context.getResources() 98 .getDimensionPixelSize(R.dimen.battery_meter_height); 99 } 100 101 @Override 102 public int getIntrinsicWidth() { 103 return mIntrinsicWidth; 104 } 105 106 @Override 107 public int getIntrinsicHeight() { 108 return mIntrinsicHeight; 109 } 110 111 public void setWarningColorFilter(@Nullable ColorFilter colorFilter) { 112 mWarningTextPaint.setColorFilter(colorFilter); 113 } 114 115 public void setBatteryColorFilter(@Nullable ColorFilter colorFilter) { 116 mFramePaint.setColorFilter(colorFilter); 117 mBatteryPaint.setColorFilter(colorFilter); 118 mBoltPaint.setColorFilter(colorFilter); 119 } 120 } 121 122 } 123