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