• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.settings.fuelgauge;
16 
17 import android.annotation.Nullable;
18 import android.content.Context;
19 import android.graphics.Canvas;
20 import android.graphics.Paint;
21 import android.util.AttributeSet;
22 import android.util.SparseIntArray;
23 import android.view.View;
24 
25 public class BatteryActiveView extends View {
26 
27     private final Paint mPaint = new Paint();
28     private BatteryActiveProvider mProvider;
29 
BatteryActiveView(Context context, @Nullable AttributeSet attrs)30     public BatteryActiveView(Context context, @Nullable AttributeSet attrs) {
31         super(context, attrs);
32     }
33 
setProvider(BatteryActiveProvider provider)34     public void setProvider(BatteryActiveProvider provider) {
35         mProvider = provider;
36         if (getWidth() != 0) {
37             postInvalidate();
38         }
39     }
40 
41     @Override
onSizeChanged(int w, int h, int oldw, int oldh)42     protected void onSizeChanged(int w, int h, int oldw, int oldh) {
43         super.onSizeChanged(w, h, oldw, oldh);
44         if (getWidth() != 0) {
45             postInvalidate();
46         }
47     }
48 
49     @Override
onDraw(Canvas canvas)50     protected void onDraw(Canvas canvas) {
51         if (mProvider == null) {
52             return;
53         }
54         SparseIntArray array = mProvider.getColorArray();
55         float period = mProvider.getPeriod();
56         for (int i = 0; i < array.size() - 1; i++) {
57             drawColor(canvas, array.keyAt(i), array.keyAt(i + 1), array.valueAt(i), period);
58         }
59     }
60 
drawColor(Canvas canvas, int start, int end, int color, float period)61     private void drawColor(Canvas canvas, int start, int end, int color, float period) {
62         if (color == 0) {
63             return;
64         }
65         mPaint.setColor(color);
66         canvas.drawRect(start / period * getWidth(), 0, end / period * getWidth(), getHeight(),
67                 mPaint);
68     }
69 
70     public interface BatteryActiveProvider {
hasData()71         boolean hasData();
getPeriod()72         long getPeriod();
getColorArray()73         SparseIntArray getColorArray();
74     }
75 }
76