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