• 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.os.BatteryStats.HistoryItem;
18 import android.util.SparseBooleanArray;
19 import android.util.SparseIntArray;
20 import com.android.settings.fuelgauge.BatteryActiveView.BatteryActiveProvider;
21 
22 public class BatteryFlagParser implements BatteryInfo.BatteryDataParser, BatteryActiveProvider {
23 
24     private final SparseBooleanArray mData = new SparseBooleanArray();
25     private final int mFlag;
26     private final boolean mState2;
27     private final int mAccentColor;
28 
29     private boolean mLastSet;
30     private long mLength;
31     private long mLastTime;
32 
BatteryFlagParser(int accent, boolean state2, int flag)33     public BatteryFlagParser(int accent, boolean state2, int flag) {
34         mAccentColor = accent;
35         mFlag = flag;
36         mState2 = state2;
37     }
38 
isSet(HistoryItem record)39     protected boolean isSet(HistoryItem record) {
40         return ((mState2 ? record.states2 : record.states) & mFlag) != 0;
41     }
42 
43     @Override
onParsingStarted(long startTime, long endTime)44     public void onParsingStarted(long startTime, long endTime) {
45         mLength = endTime - startTime;
46     }
47 
48     @Override
onDataPoint(long time, HistoryItem record)49     public void onDataPoint(long time, HistoryItem record) {
50         boolean isSet = isSet(record);
51         if (isSet != mLastSet) {
52             mData.put((int) time, isSet);
53             mLastSet = isSet;
54         }
55         mLastTime = time;
56     }
57 
58     @Override
onDataGap()59     public void onDataGap() {
60         if (mLastSet) {
61             mData.put((int) mLastTime, false);
62             mLastSet = false;
63         }
64     }
65 
66     @Override
onParsingDone()67     public void onParsingDone() {
68         if (mLastSet) {
69             mData.put((int) mLastTime, false);
70             mLastSet = false;
71         }
72     }
73 
74     @Override
getPeriod()75     public long getPeriod() {
76         return mLength;
77     }
78 
79     @Override
hasData()80     public boolean hasData() {
81         return mData.size() > 1;
82     }
83 
84     @Override
getColorArray()85     public SparseIntArray getColorArray() {
86         SparseIntArray ret = new SparseIntArray();
87         for (int i = 0; i < mData.size(); i++) {
88             ret.put(mData.keyAt(i), getColor(mData.valueAt(i)));
89         }
90         return ret;
91     }
92 
getColor(boolean b)93     private int getColor(boolean b) {
94         if (b) {
95             return mAccentColor;
96         }
97         return 0;
98     }
99 }
100