Make BatterySettings Asynchronous and use enhanced estimate

this CL changes BatteryInfo methods to all use the async style
callback approach it had for one of the methods. Non-async methods
are now annotated to only be used in worker threads. BatteryInfo
can now be obtained via callback by calling one of the async
methods. Alternatively if there is a worker thread available
the synchronous methods similar to the old ones can be used.

The callback methods have all been changed so that they cascade to a
async method that takes all the required info as paremeters. This
will minimize the amount of churn in files that currently use
BatteryInfo.

A new loader was created that can be used to get BatteryInfo in
places that wish to get it. This loader is used in
PowerUsageSummary to get the BatteryInfo.

Test: Robotests
Bug: 38399275
Bug: 38398949
Bug: 38399654
Change-Id: Ic5a82d8ca4c85fad1b883226327ec083badf861d
This commit is contained in:
Salvador Martinez
2017-05-30 11:18:09 -07:00
parent 31d5b3de6f
commit 9cfa7720f4
9 changed files with 237 additions and 191 deletions

View File

@@ -506,81 +506,82 @@ public class BatteryHistoryChart extends View {
mMaxPercentLabelString = Utils.formatPercentage(100);
mMinPercentLabelString = Utils.formatPercentage(0);
mInfo = BatteryInfo.getBatteryInfo(getContext(), mBatteryBroadcast, mStats,
elapsedRealtimeUs);
mDrainString = "";
mChargeDurationString = "";
setContentDescription(mInfo.chargeLabelString);
BatteryInfo.getBatteryInfo(getContext(), info -> {
mInfo = info;
mDrainString = "";
mChargeDurationString = "";
setContentDescription(mInfo.chargeLabelString);
int pos = 0;
int lastInteresting = 0;
byte lastLevel = -1;
mBatLow = 0;
mBatHigh = 100;
mStartWallTime = 0;
mEndDataWallTime = 0;
mEndWallTime = 0;
mHistStart = 0;
mHistEnd = 0;
long lastWallTime = 0;
long lastRealtime = 0;
int aggrStates = 0;
int aggrStates2 = 0;
boolean first = true;
if (stats.startIteratingHistoryLocked()) {
final HistoryItem rec = new HistoryItem();
while (stats.getNextHistoryLocked(rec)) {
pos++;
if (first) {
first = false;
mHistStart = rec.time;
}
if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
|| rec.cmd == HistoryItem.CMD_RESET) {
// If there is a ridiculously large jump in time, then we won't be
// able to create a good chart with that data, so just ignore the
// times we got before and pretend like our data extends back from
// the time we have now.
// Also, if we are getting a time change and we are less than 5 minutes
// since the start of the history real time, then also use this new
// time to compute the base time, since whatever time we had before is
// pretty much just noise.
if (rec.currentTime > (lastWallTime+(180*24*60*60*1000L))
|| rec.time < (mHistStart+(5*60*1000L))) {
mStartWallTime = 0;
int pos = 0;
int lastInteresting = 0;
byte lastLevel = -1;
mBatLow = 0;
mBatHigh = 100;
mStartWallTime = 0;
mEndDataWallTime = 0;
mEndWallTime = 0;
mHistStart = 0;
mHistEnd = 0;
long lastWallTime = 0;
long lastRealtime = 0;
int aggrStates = 0;
int aggrStates2 = 0;
boolean first = true;
if (stats.startIteratingHistoryLocked()) {
final HistoryItem rec = new HistoryItem();
while (stats.getNextHistoryLocked(rec)) {
pos++;
if (first) {
first = false;
mHistStart = rec.time;
}
lastWallTime = rec.currentTime;
lastRealtime = rec.time;
if (mStartWallTime == 0) {
mStartWallTime = lastWallTime - (lastRealtime-mHistStart);
if (rec.cmd == HistoryItem.CMD_CURRENT_TIME
|| rec.cmd == HistoryItem.CMD_RESET) {
// If there is a ridiculously large jump in time, then we won't be
// able to create a good chart with that data, so just ignore the
// times we got before and pretend like our data extends back from
// the time we have now.
// Also, if we are getting a time change and we are less than 5 minutes
// since the start of the history real time, then also use this new
// time to compute the base time, since whatever time we had before is
// pretty much just noise.
if (rec.currentTime > (lastWallTime+(180*24*60*60*1000L))
|| rec.time < (mHistStart+(5*60*1000L))) {
mStartWallTime = 0;
}
lastWallTime = rec.currentTime;
lastRealtime = rec.time;
if (mStartWallTime == 0) {
mStartWallTime = lastWallTime - (lastRealtime-mHistStart);
}
}
}
if (rec.isDeltaData()) {
if (rec.batteryLevel != lastLevel || pos == 1) {
lastLevel = rec.batteryLevel;
if (rec.isDeltaData()) {
if (rec.batteryLevel != lastLevel || pos == 1) {
lastLevel = rec.batteryLevel;
}
lastInteresting = pos;
mHistDataEnd = rec.time;
aggrStates |= rec.states;
aggrStates2 |= rec.states2;
}
lastInteresting = pos;
mHistDataEnd = rec.time;
aggrStates |= rec.states;
aggrStates2 |= rec.states2;
}
}
}
mHistEnd = mHistDataEnd + (mInfo.remainingTimeUs/1000);
mEndDataWallTime = lastWallTime + mHistDataEnd - lastRealtime;
mEndWallTime = mEndDataWallTime + (mInfo.remainingTimeUs/1000);
mNumHist = lastInteresting;
mHaveGps = (aggrStates&HistoryItem.STATE_GPS_ON_FLAG) != 0;
mHaveFlashlight = (aggrStates2&HistoryItem.STATE2_FLASHLIGHT_FLAG) != 0;
mHaveCamera = (aggrStates2&HistoryItem.STATE2_CAMERA_FLAG) != 0;
mHaveWifi = (aggrStates2&HistoryItem.STATE2_WIFI_RUNNING_FLAG) != 0
|| (aggrStates&(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG
|HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG
|HistoryItem.STATE_WIFI_SCAN_FLAG)) != 0;
if (!com.android.settings.Utils.isWifiOnly(getContext())) {
mHavePhoneSignal = true;
}
if (mHistEnd <= mHistStart) mHistEnd = mHistStart+1;
mHistEnd = mHistDataEnd + (mInfo.remainingTimeUs/1000);
mEndDataWallTime = lastWallTime + mHistDataEnd - lastRealtime;
mEndWallTime = mEndDataWallTime + (mInfo.remainingTimeUs/1000);
mNumHist = lastInteresting;
mHaveGps = (aggrStates&HistoryItem.STATE_GPS_ON_FLAG) != 0;
mHaveFlashlight = (aggrStates2&HistoryItem.STATE2_FLASHLIGHT_FLAG) != 0;
mHaveCamera = (aggrStates2&HistoryItem.STATE2_CAMERA_FLAG) != 0;
mHaveWifi = (aggrStates2&HistoryItem.STATE2_WIFI_RUNNING_FLAG) != 0
|| (aggrStates&(HistoryItem.STATE_WIFI_FULL_LOCK_FLAG
|HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG
|HistoryItem.STATE_WIFI_SCAN_FLAG)) != 0;
if (!com.android.settings.Utils.isWifiOnly(getContext())) {
mHavePhoneSignal = true;
}
if (mHistEnd <= mHistStart) mHistEnd = mHistStart+1;
}, mStats, false /* shortString */);
}
@Override