1 /* 2 * Copyright (C) 2018 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.batterytip; 18 19 import android.os.BatteryStats; 20 21 import com.android.settings.fuelgauge.BatteryInfo; 22 23 /** 24 * DataParser used to go through battery data and detect whether battery is 25 * heavily used. 26 */ 27 public class HighUsageDataParser implements BatteryInfo.BatteryDataParser { 28 /** 29 * time period to check the battery usage 30 */ 31 private final long mTimePeriodMs; 32 /** 33 * treat device as heavily used if battery usage is more than {@code threshold}. 1 means 1% 34 * battery usage. 35 */ 36 private int mThreshold; 37 private long mEndTimeMs; 38 private byte mEndBatteryLevel; 39 private byte mLastPeriodBatteryLevel; 40 private int mBatteryDrain; 41 HighUsageDataParser(long timePeriodMs, int threshold)42 public HighUsageDataParser(long timePeriodMs, int threshold) { 43 mTimePeriodMs = timePeriodMs; 44 mThreshold = threshold; 45 } 46 47 @Override onParsingStarted(long startTime, long endTime)48 public void onParsingStarted(long startTime, long endTime) { 49 mEndTimeMs = endTime; 50 } 51 52 @Override onDataPoint(long time, BatteryStats.HistoryItem record)53 public void onDataPoint(long time, BatteryStats.HistoryItem record) { 54 if (time == 0 || record.currentTime <= mEndTimeMs - mTimePeriodMs) { 55 // Since onDataPoint is invoked sorted by time, so we could use this way to get the 56 // closet battery level 'mTimePeriodMs' time ago. 57 mLastPeriodBatteryLevel = record.batteryLevel; 58 } 59 mEndBatteryLevel = record.batteryLevel; 60 } 61 62 @Override onDataGap()63 public void onDataGap() { 64 // do nothing 65 } 66 67 @Override onParsingDone()68 public void onParsingDone() { 69 mBatteryDrain = mLastPeriodBatteryLevel - mEndBatteryLevel; 70 } 71 72 /** 73 * Return {@code true} if the battery drain in {@link #mTimePeriodMs} is too much 74 */ isDeviceHeavilyUsed()75 public boolean isDeviceHeavilyUsed() { 76 return mBatteryDrain > mThreshold; 77 } 78 } 79 80