• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.car.developeroptions.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.util.AttributeSet;
26 import android.widget.ImageView;
27 
28 import androidx.annotation.VisibleForTesting;
29 
30 import com.android.car.developeroptions.R;
31 import com.android.car.developeroptions.Utils;
32 import com.android.settingslib.graph.ThemedBatteryDrawable;
33 
34 public class BatteryMeterView extends ImageView {
35     @VisibleForTesting
36     BatteryMeterDrawable mDrawable;
37     @VisibleForTesting
38     ColorFilter mErrorColorFilter;
39     @VisibleForTesting
40     ColorFilter mAccentColorFilter;
41     private boolean mPowerSaveEnabled;
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 
61         mDrawable = new BatteryMeterDrawable(context, frameColor);
62         mDrawable.setColorFilter(mAccentColorFilter);
63         setImageDrawable(mDrawable);
64         setLayerType(LAYER_TYPE_SOFTWARE, null);
65     }
66 
setBatteryLevel(int level)67     public void setBatteryLevel(int level) {
68         mDrawable.setBatteryLevel(level);
69         if (level < mDrawable.getCriticalLevel()) {
70             mDrawable.setColorFilter(mErrorColorFilter);
71         } else {
72             mDrawable.setColorFilter(mAccentColorFilter);
73         }
74     }
75 
setPowerSave(boolean powerSave)76     public void setPowerSave(boolean powerSave) {
77         mDrawable.setPowerSaveEnabled(powerSave);
78         mPowerSaveEnabled = powerSave;
79     }
80 
getPowerSave()81     public boolean getPowerSave() {
82         return mPowerSaveEnabled;
83     }
84 
getBatteryLevel()85     public int getBatteryLevel() {
86         return mDrawable.getLevel();
87     }
88 
setCharging(boolean charging)89     public void setCharging(boolean charging) {
90         mDrawable.setCharging(charging);
91         postInvalidate();
92     }
93 
getCharging()94     public boolean getCharging() {
95         return mDrawable.getCharging();
96     }
97 
98     public static class BatteryMeterDrawable extends ThemedBatteryDrawable {
99         private final int mIntrinsicWidth;
100         private final int mIntrinsicHeight;
101 
BatteryMeterDrawable(Context context, int frameColor)102         public BatteryMeterDrawable(Context context, int frameColor) {
103             super(context, frameColor);
104 
105             mIntrinsicWidth = context.getResources()
106                     .getDimensionPixelSize(R.dimen.battery_meter_width);
107             mIntrinsicHeight = context.getResources()
108                     .getDimensionPixelSize(R.dimen.battery_meter_height);
109         }
110 
111         @Override
getIntrinsicWidth()112         public int getIntrinsicWidth() {
113             return mIntrinsicWidth;
114         }
115 
116         @Override
getIntrinsicHeight()117         public int getIntrinsicHeight() {
118             return mIntrinsicHeight;
119         }
120     }
121 }
122