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
15package com.android.settings.fuelgauge;
16
17import android.os.BatteryStats.HistoryItem;
18import android.telephony.ServiceState;
19import android.util.SparseIntArray;
20import com.android.settings.Utils;
21import com.android.settings.fuelgauge.BatteryActiveView.BatteryActiveProvider;
22
23public class BatteryCellParser implements BatteryInfo.BatteryDataParser, BatteryActiveProvider {
24
25    private final SparseIntArray mData = new SparseIntArray();
26
27    private int mLastValue;
28    private long mLength;
29    private long mLastTime;
30
31    protected int getValue(HistoryItem rec) {
32        int bin;
33        if (((rec.states & HistoryItem.STATE_PHONE_STATE_MASK)
34                >> HistoryItem.STATE_PHONE_STATE_SHIFT)
35                == ServiceState.STATE_POWER_OFF) {
36            bin = 0;
37        } else if ((rec.states & HistoryItem.STATE_PHONE_SCANNING_FLAG) != 0) {
38            bin = 1;
39        } else {
40            bin = (rec.states & HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK)
41                    >> HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT;
42            bin += 2;
43        }
44        return bin;
45    }
46
47    @Override
48    public void onParsingStarted(long startTime, long endTime) {
49        mLength = endTime - startTime;
50    }
51
52    @Override
53    public void onDataPoint(long time, HistoryItem record) {
54        int value = getValue(record);
55        if (value != mLastValue) {
56            mData.put((int) time, value);
57            mLastValue = value;
58        }
59        mLastTime = time;
60    }
61
62    @Override
63    public void onDataGap() {
64        if (mLastValue != 0) {
65            mData.put((int) mLastTime, 0);
66            mLastValue = 0;
67        }
68    }
69
70    @Override
71    public void onParsingDone() {
72        if (mLastValue != 0) {
73            mData.put((int) mLastTime, 0);
74            mLastValue = 0;
75        }
76    }
77
78    @Override
79    public long getPeriod() {
80        return mLength;
81    }
82
83    @Override
84    public boolean hasData() {
85        return mData.size() > 1;
86    }
87
88    @Override
89    public SparseIntArray getColorArray() {
90        SparseIntArray ret = new SparseIntArray();
91        for (int i = 0; i < mData.size(); i++) {
92            ret.put(mData.keyAt(i), getColor(mData.valueAt(i)));
93        }
94        return ret;
95    }
96
97    private int getColor(int i) {
98        return Utils.BADNESS_COLORS[i];
99    }
100}
101