Render enhanced battery projection curves.

BatteryInfo now supplies standard linear or enhanced projection curves,
depending on the provider.

Note that the semantics of parsing have changed slightly in that the
value of endTime supplied to onParsingStarted is now the end time of the
historical data and does not include the projection. However, as far as
I can see there is no existing code that depends on the parsing
functionality besides BatteryInfo itself.

Also slightly optimizes the updating behavior of the UsageGraph, since
we are now reconfiguring it multiple times.

Bug: 38400320
Test: make RunSettingsRoboTests, manual on device
Change-Id: Ieff26d31356b34bb38e49f54f979fd80549864b2
This commit is contained in:
Alex Kulesza
2017-06-29 12:26:59 -04:00
parent 43d4fefb36
commit 82dbcd973d
5 changed files with 196 additions and 35 deletions

View File

@@ -30,10 +30,10 @@ import android.text.format.Formatter;
import android.util.SparseIntArray;
import com.android.internal.os.BatteryStatsHelper;
import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.R;
import com.android.settings.Utils;
import com.android.settings.graph.UsageView;
import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.R;
public class BatteryInfo {
@@ -54,18 +54,20 @@ public class BatteryInfo {
}
public void bindHistory(final UsageView view, BatteryDataParser... parsers) {
final Context context = view.getContext();
BatteryDataParser parser = new BatteryDataParser() {
SparseIntArray points = new SparseIntArray();
long startTime;
int lastTime = -1;
byte lastLevel;
int maxTime;
@Override
public void onParsingStarted(long startTime, long endTime) {
this.maxTime = (int) (endTime - startTime);
timePeriod = maxTime - (remainingTimeUs / 1000);
this.startTime = startTime;
timePeriod = endTime - startTime;
view.clearPaths();
view.configureGraph(maxTime, 100);
// Initially configure the graph for history only.
view.configureGraph((int) timePeriod, 100);
}
@Override
@@ -87,10 +89,27 @@ public class BatteryInfo {
public void onParsingDone() {
onDataGap();
// Add linear projection
if (lastTime >= 0 && remainingTimeUs != 0) {
points.put(lastTime, lastLevel);
points.put(maxTime, mCharging ? 100 : 0);
// Add projection if we have an estimate.
if (remainingTimeUs != 0) {
PowerUsageFeatureProvider provider = FeatureFactory.getFactory(context)
.getPowerUsageFeatureProvider(context);
if (!mCharging && provider.isEnhancedBatteryPredictionEnabled(context)) {
points = provider.getEnhancedBatteryPredictionCurve(context, startTime);
} else {
// Linear extrapolation.
if (lastTime >= 0) {
points.put(lastTime, lastLevel);
points.put((int) (timePeriod +
BatteryUtils.convertUsToMs(remainingTimeUs)),
mCharging ? 100 : 0);
}
}
}
// If we have a projection, reconfigure the graph to show it.
if (points != null && points.size() > 0) {
int maxTime = points.keyAt(points.size() - 1);
view.configureGraph(maxTime, 100);
view.addProjectedPath(points);
}
}
@@ -100,8 +119,7 @@ public class BatteryInfo {
parserList[i] = parsers[i];
}
parserList[parsers.length] = parser;
parse(mStats, remainingTimeUs, parserList);
final Context context = view.getContext();
parse(mStats, parserList);
String timeString = context.getString(R.string.charge_length_format,
Formatter.formatShortElapsedTime(context, timePeriod));
String remaining = "";
@@ -249,14 +267,11 @@ public class BatteryInfo {
void onParsingDone();
}
private static void parse(BatteryStats stats, long remainingTimeUs,
BatteryDataParser... parsers) {
private static void parse(BatteryStats stats, BatteryDataParser... parsers) {
long startWalltime = 0;
long endDateWalltime = 0;
long endWalltime = 0;
long historyStart = 0;
long historyEnd = 0;
byte lastLevel = -1;
long curWalltime = startWalltime;
long lastWallTime = 0;
long lastRealtime = 0;
@@ -292,17 +307,13 @@ public class BatteryInfo {
}
}
if (rec.isDeltaData()) {
if (rec.batteryLevel != lastLevel || pos == 1) {
lastLevel = rec.batteryLevel;
}
lastInteresting = pos;
historyEnd = rec.time;
}
}
}
stats.finishIteratingHistoryLocked();
endDateWalltime = lastWallTime + historyEnd - lastRealtime;
endWalltime = endDateWalltime + (remainingTimeUs / 1000);
endWalltime = lastWallTime + historyEnd - lastRealtime;
int i = 0;
final int N = lastInteresting;
@@ -310,7 +321,7 @@ public class BatteryInfo {
for (int j = 0; j < parsers.length; j++) {
parsers[j].onParsingStarted(startWalltime, endWalltime);
}
if (endDateWalltime > startWalltime && stats.startIteratingHistoryLocked()) {
if (endWalltime > startWalltime && stats.startIteratingHistoryLocked()) {
final HistoryItem rec = new HistoryItem();
while (stats.getNextHistoryLocked(rec) && i < N) {
if (rec.isDeltaData()) {