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.ColorFilter; 22 import android.graphics.PorterDuff; 23 import android.graphics.PorterDuffColorFilter; 24 import android.util.AttributeSet; 25 import android.widget.ImageView; 26 27 import androidx.annotation.VisibleForTesting; 28 29 import com.android.settings.R; 30 import com.android.settings.Utils; 31 import com.android.settingslib.graph.ThemedBatteryDrawable; 32 33 public class BatteryMeterView extends ImageView { 34 @VisibleForTesting 35 BatteryMeterDrawable mDrawable; 36 @VisibleForTesting 37 ColorFilter mErrorColorFilter; 38 @VisibleForTesting 39 ColorFilter mAccentColorFilter; 40 @VisibleForTesting 41 ColorFilter mForegroundColorFilter; 42 BatteryMeterView(Context context)43 public BatteryMeterView(Context context) { 44 this(context, null, 0); 45 } 46 BatteryMeterView(Context context, @Nullable AttributeSet attrs)47 public BatteryMeterView(Context context, @Nullable AttributeSet attrs) { 48 this(context, attrs, 0); 49 } 50 BatteryMeterView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)51 public BatteryMeterView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 52 super(context, attrs, defStyleAttr); 53 54 final int frameColor = context.getColor(R.color.meter_background_color); 55 mAccentColorFilter = new PorterDuffColorFilter( 56 Utils.getColorAttrDefaultColor(context, android.R.attr.colorAccent), 57 PorterDuff.Mode.SRC); 58 mErrorColorFilter = new PorterDuffColorFilter( 59 context.getColor(R.color.battery_icon_color_error), PorterDuff.Mode.SRC_IN); 60 mForegroundColorFilter =new PorterDuffColorFilter( 61 Utils.getColorAttrDefaultColor(context, android.R.attr.colorForeground), 62 PorterDuff.Mode.SRC); 63 mDrawable = new BatteryMeterDrawable(context, frameColor); 64 mDrawable.setColorFilter(mAccentColorFilter); 65 setImageDrawable(mDrawable); 66 } 67 setBatteryLevel(int level)68 public void setBatteryLevel(int level) { 69 mDrawable.setBatteryLevel(level); 70 updateColorFilter(); 71 } 72 setPowerSave(boolean powerSave)73 public void setPowerSave(boolean powerSave) { 74 mDrawable.setPowerSaveEnabled(powerSave); 75 updateColorFilter(); 76 } 77 getPowerSave()78 public boolean getPowerSave() { 79 return mDrawable.getPowerSaveEnabled(); 80 } 81 getBatteryLevel()82 public int getBatteryLevel() { 83 return mDrawable.getBatteryLevel(); 84 } 85 setCharging(boolean charging)86 public void setCharging(boolean charging) { 87 mDrawable.setCharging(charging); 88 postInvalidate(); 89 } 90 getCharging()91 public boolean getCharging() { 92 return mDrawable.getCharging(); 93 } 94 updateColorFilter()95 private void updateColorFilter() { 96 final boolean powerSaveEnabled = mDrawable.getPowerSaveEnabled(); 97 final int level = mDrawable.getBatteryLevel(); 98 if (powerSaveEnabled) { 99 mDrawable.setColorFilter(mForegroundColorFilter); 100 } else if (level < mDrawable.getCriticalLevel()) { 101 mDrawable.setColorFilter(mErrorColorFilter); 102 } else { 103 mDrawable.setColorFilter(mAccentColorFilter); 104 } 105 } 106 107 public static class BatteryMeterDrawable extends ThemedBatteryDrawable { 108 private final int mIntrinsicWidth; 109 private final int mIntrinsicHeight; 110 BatteryMeterDrawable(Context context, int frameColor)111 public BatteryMeterDrawable(Context context, int frameColor) { 112 super(context, frameColor); 113 114 mIntrinsicWidth = context.getResources() 115 .getDimensionPixelSize(R.dimen.battery_meter_width); 116 mIntrinsicHeight = context.getResources() 117 .getDimensionPixelSize(R.dimen.battery_meter_height); 118 } 119 BatteryMeterDrawable(Context context, int frameColor, int width, int height)120 public BatteryMeterDrawable(Context context, int frameColor, int width, int height) { 121 super(context, frameColor); 122 123 mIntrinsicWidth = width; 124 mIntrinsicHeight = height; 125 } 126 127 @Override getIntrinsicWidth()128 public int getIntrinsicWidth() { 129 return mIntrinsicWidth; 130 } 131 132 @Override getIntrinsicHeight()133 public int getIntrinsicHeight() { 134 return mIntrinsicHeight; 135 } 136 } 137 } 138