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