OSDN Git Service

Merge "Fix storage summary background colors." into oc-dev
[android-x86/packages-apps-Settings.git] / src / com / android / settings / fuelgauge / BatteryMeterView.java
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 void setCharging(boolean charging) {
76         mDrawable.setCharging(charging);
77     }
78
79     public static class BatteryMeterDrawable extends BatteryMeterDrawableBase {
80         private final int mIntrinsicWidth;
81         private final int mIntrinsicHeight;
82
83         public BatteryMeterDrawable(Context context, int frameColor) {
84             super(context, frameColor);
85
86             mIntrinsicWidth = context.getResources()
87                     .getDimensionPixelSize(R.dimen.battery_meter_width);
88             mIntrinsicHeight = context.getResources()
89                     .getDimensionPixelSize(R.dimen.battery_meter_height);
90         }
91
92         @Override
93         public int getIntrinsicWidth() {
94             return mIntrinsicWidth;
95         }
96
97         @Override
98         public int getIntrinsicHeight() {
99             return mIntrinsicHeight;
100         }
101
102         public void setWarningColorFilter(@Nullable ColorFilter colorFilter) {
103             mWarningTextPaint.setColorFilter(colorFilter);
104         }
105
106         public void setBatteryColorFilter(@Nullable ColorFilter colorFilter) {
107             mFramePaint.setColorFilter(colorFilter);
108             mBatteryPaint.setColorFilter(colorFilter);
109             mBoltPaint.setColorFilter(colorFilter);
110         }
111     }
112
113 }