Merge "Refactor function to support slot size not must be two." into udc-dev

This commit is contained in:
Zaiyue Xue
2023-04-07 13:09:11 +00:00
committed by Android (Google) Code Review

View File

@@ -1446,31 +1446,39 @@ public final class DataProcessor {
final int workProfileUserId = final int workProfileUserId =
userHandle != null ? userHandle.getIdentifier() : Integer.MIN_VALUE; userHandle != null ? userHandle.getIdentifier() : Integer.MIN_VALUE;
// Each time slot usage diff data = // Each time slot usage diff data =
// Math.abs(timestamp[i+2] data - timestamp[i+1] data) + // sum(Math.abs(timestamp[i+1] data - timestamp[i] data));
// Math.abs(timestamp[i+1] data - timestamp[i] data); // since we want to aggregate every hour usage diff data into a single time slot.
// since we want to aggregate every two hours data into a single time slot.
for (int dailyIndex = 0; dailyIndex < hourlyBatteryLevelsPerDay.size(); dailyIndex++) { for (int dailyIndex = 0; dailyIndex < hourlyBatteryLevelsPerDay.size(); dailyIndex++) {
final Map<Integer, BatteryDiffData> dailyDiffMap = new ArrayMap<>(); final Map<Integer, BatteryDiffData> dailyDiffMap = new ArrayMap<>();
resultMap.put(dailyIndex, dailyDiffMap); resultMap.put(dailyIndex, dailyDiffMap);
if (hourlyBatteryLevelsPerDay.get(dailyIndex) == null) { if (hourlyBatteryLevelsPerDay.get(dailyIndex) == null) {
continue; continue;
} }
final List<Long> timestamps = hourlyBatteryLevelsPerDay.get(dailyIndex).getTimestamps(); final List<Long> hourlyTimestamps =
for (int hourlyIndex = 0; hourlyIndex < timestamps.size() - 1; hourlyIndex++) { hourlyBatteryLevelsPerDay.get(dailyIndex).getTimestamps();
for (int hourlyIndex = 0; hourlyIndex < hourlyTimestamps.size() - 1; hourlyIndex++) {
final Long startTimestamp = hourlyTimestamps.get(hourlyIndex);
final Long endTimestamp = hourlyTimestamps.get(hourlyIndex + 1);
final long slotDuration = endTimestamp - startTimestamp;
List<Map<String, BatteryHistEntry>> slotBatteryHistoryList = new ArrayList<>();
for (Long timestamp = startTimestamp; timestamp <= endTimestamp;
timestamp += DateUtils.HOUR_IN_MILLIS) {
slotBatteryHistoryList.add(
batteryHistoryMap.getOrDefault(timestamp, EMPTY_BATTERY_MAP));
}
final BatteryDiffData hourlyBatteryDiffData = final BatteryDiffData hourlyBatteryDiffData =
insertHourlyUsageDiffDataPerSlot( insertHourlyUsageDiffDataPerSlot(
context, context,
currentUserId, currentUserId,
workProfileUserId, workProfileUserId,
hourlyIndex, slotDuration,
timestamps,
systemAppsPackageNames, systemAppsPackageNames,
systemAppsUids, systemAppsUids,
appUsagePeriodMap == null appUsagePeriodMap == null
|| appUsagePeriodMap.get(dailyIndex) == null || appUsagePeriodMap.get(dailyIndex) == null
? null ? null
: appUsagePeriodMap.get(dailyIndex).get(hourlyIndex), : appUsagePeriodMap.get(dailyIndex).get(hourlyIndex),
batteryHistoryMap); slotBatteryHistoryList);
dailyDiffMap.put(hourlyIndex, hourlyBatteryDiffData); dailyDiffMap.put(hourlyIndex, hourlyBatteryDiffData);
} }
} }
@@ -1508,54 +1516,42 @@ public final class DataProcessor {
final Context context, final Context context,
final int currentUserId, final int currentUserId,
final int workProfileUserId, final int workProfileUserId,
final int currentIndex, final long slotDuration,
final List<Long> timestamps,
final Set<String> systemAppsPackageNames, final Set<String> systemAppsPackageNames,
final Set<Integer> systemAppsUids, final Set<Integer> systemAppsUids,
final Map<Long, Map<String, List<AppUsagePeriod>>> appUsageMap, final Map<Long, Map<String, List<AppUsagePeriod>>> appUsageMap,
final Map<Long, Map<String, BatteryHistEntry>> batteryHistoryMap) { final List<Map<String, BatteryHistEntry>> slotBatteryHistoryList) {
final List<BatteryDiffEntry> appEntries = new ArrayList<>(); final List<BatteryDiffEntry> appEntries = new ArrayList<>();
final List<BatteryDiffEntry> systemEntries = new ArrayList<>(); final List<BatteryDiffEntry> systemEntries = new ArrayList<>();
final Long currentTimestamp = timestamps.get(currentIndex);
final Long nextTimestamp = currentTimestamp + DateUtils.HOUR_IN_MILLIS;
final Long nextTwoTimestamp = nextTimestamp + DateUtils.HOUR_IN_MILLIS;
// Fetches BatteryHistEntry data from corresponding time slot.
final Map<String, BatteryHistEntry> currentBatteryHistMap =
batteryHistoryMap.getOrDefault(currentTimestamp, EMPTY_BATTERY_MAP);
final Map<String, BatteryHistEntry> nextBatteryHistMap =
batteryHistoryMap.getOrDefault(nextTimestamp, EMPTY_BATTERY_MAP);
final Map<String, BatteryHistEntry> nextTwoBatteryHistMap =
batteryHistoryMap.getOrDefault(nextTwoTimestamp, EMPTY_BATTERY_MAP);
// We should not get the empty list since we have at least one fake data to record
// the battery level and status in each time slot, the empty list is used to
// represent there is no enough data to apply interpolation arithmetic.
if (currentBatteryHistMap.isEmpty()
|| nextBatteryHistMap.isEmpty()
|| nextTwoBatteryHistMap.isEmpty()) {
return null;
}
// Collects all keys in these three time slot records as all populations. // Collects all keys in these three time slot records as all populations.
final Set<String> allBatteryHistEntryKeys = new ArraySet<>(); final Set<String> allBatteryHistEntryKeys = new ArraySet<>();
allBatteryHistEntryKeys.addAll(currentBatteryHistMap.keySet()); for (Map<String, BatteryHistEntry> slotBatteryHistMap : slotBatteryHistoryList) {
allBatteryHistEntryKeys.addAll(nextBatteryHistMap.keySet()); if (slotBatteryHistMap.isEmpty()) {
allBatteryHistEntryKeys.addAll(nextTwoBatteryHistMap.keySet()); // We should not get the empty list since we have at least one fake data to record
// the battery level and status in each time slot, the empty list is used to
// represent there is no enough data to apply interpolation arithmetic.
return null;
}
allBatteryHistEntryKeys.addAll(slotBatteryHistMap.keySet());
}
// Calculates all packages diff usage data in a specific time slot. // Calculates all packages diff usage data in a specific time slot.
for (String key : allBatteryHistEntryKeys) { for (String key : allBatteryHistEntryKeys) {
if (key == null) { if (key == null) {
continue; continue;
} }
final BatteryHistEntry currentEntry =
currentBatteryHistMap.getOrDefault(key, EMPTY_BATTERY_HIST_ENTRY);
final BatteryHistEntry nextEntry =
nextBatteryHistMap.getOrDefault(key, EMPTY_BATTERY_HIST_ENTRY);
final BatteryHistEntry nextTwoEntry =
nextTwoBatteryHistMap.getOrDefault(key, EMPTY_BATTERY_HIST_ENTRY);
final BatteryHistEntry selectedBatteryEntry = BatteryHistEntry selectedBatteryEntry = null;
selectBatteryHistEntry(currentEntry, nextEntry, nextTwoEntry); final List<BatteryHistEntry> batteryHistEntries = new ArrayList<>();
for (Map<String, BatteryHistEntry> slotBatteryHistMap : slotBatteryHistoryList) {
BatteryHistEntry entry =
slotBatteryHistMap.getOrDefault(key, EMPTY_BATTERY_HIST_ENTRY);
batteryHistEntries.add(entry);
if (selectedBatteryEntry == null && entry != EMPTY_BATTERY_HIST_ENTRY) {
selectedBatteryEntry = entry;
}
}
if (selectedBatteryEntry == null) { if (selectedBatteryEntry == null) {
continue; continue;
} }
@@ -1568,41 +1564,45 @@ public final class DataProcessor {
} }
// Cumulative values is a specific time slot for a specific app. // Cumulative values is a specific time slot for a specific app.
long foregroundUsageTimeInMs = long foregroundUsageTimeInMs = 0;
getDiffValue( long backgroundUsageTimeInMs = 0;
currentEntry.mForegroundUsageTimeInMs, double consumePower = 0;
nextEntry.mForegroundUsageTimeInMs, double foregroundUsageConsumePower = 0;
nextTwoEntry.mForegroundUsageTimeInMs); double foregroundServiceUsageConsumePower = 0;
long backgroundUsageTimeInMs = double backgroundUsageConsumePower = 0;
getDiffValue( double cachedUsageConsumePower = 0;
currentEntry.mBackgroundUsageTimeInMs, for (int i = 0; i < batteryHistEntries.size() - 1; i++) {
nextEntry.mBackgroundUsageTimeInMs, final BatteryHistEntry currentEntry = batteryHistEntries.get(i);
nextTwoEntry.mBackgroundUsageTimeInMs); final BatteryHistEntry nextEntry = batteryHistEntries.get(i + 1);
double consumePower = foregroundUsageTimeInMs +=
getDiffValue( getDiffValue(
currentEntry.mConsumePower, currentEntry.mForegroundUsageTimeInMs,
nextEntry.mConsumePower, nextEntry.mForegroundUsageTimeInMs);
nextTwoEntry.mConsumePower); backgroundUsageTimeInMs +=
double foregroundUsageConsumePower = getDiffValue(
getDiffValue( currentEntry.mBackgroundUsageTimeInMs,
currentEntry.mForegroundUsageConsumePower, nextEntry.mBackgroundUsageTimeInMs);
nextEntry.mForegroundUsageConsumePower, consumePower +=
nextTwoEntry.mForegroundUsageConsumePower); getDiffValue(
double foregroundServiceUsageConsumePower = currentEntry.mConsumePower,
getDiffValue( nextEntry.mConsumePower);
currentEntry.mForegroundServiceUsageConsumePower, foregroundUsageConsumePower +=
nextEntry.mForegroundServiceUsageConsumePower, getDiffValue(
nextTwoEntry.mForegroundServiceUsageConsumePower); currentEntry.mForegroundUsageConsumePower,
double backgroundUsageConsumePower = nextEntry.mForegroundUsageConsumePower);
getDiffValue( foregroundServiceUsageConsumePower +=
currentEntry.mBackgroundUsageConsumePower, getDiffValue(
nextEntry.mBackgroundUsageConsumePower, currentEntry.mForegroundServiceUsageConsumePower,
nextTwoEntry.mBackgroundUsageConsumePower); nextEntry.mForegroundServiceUsageConsumePower);
double cachedUsageConsumePower = backgroundUsageConsumePower +=
getDiffValue( getDiffValue(
currentEntry.mCachedUsageConsumePower, currentEntry.mBackgroundUsageConsumePower,
nextEntry.mCachedUsageConsumePower, nextEntry.mBackgroundUsageConsumePower);
nextTwoEntry.mCachedUsageConsumePower); cachedUsageConsumePower +=
getDiffValue(
currentEntry.mCachedUsageConsumePower,
nextEntry.mCachedUsageConsumePower);
}
// Excludes entry since we don't have enough data to calculate. // Excludes entry since we don't have enough data to calculate.
if (foregroundUsageTimeInMs == 0 if (foregroundUsageTimeInMs == 0
&& backgroundUsageTimeInMs == 0 && backgroundUsageTimeInMs == 0
@@ -1613,13 +1613,13 @@ public final class DataProcessor {
// will apply the interpolation arithmetic. // will apply the interpolation arithmetic.
final float totalUsageTimeInMs = final float totalUsageTimeInMs =
foregroundUsageTimeInMs + backgroundUsageTimeInMs; foregroundUsageTimeInMs + backgroundUsageTimeInMs;
if (totalUsageTimeInMs > TOTAL_HOURLY_TIME_THRESHOLD) { if (totalUsageTimeInMs > slotDuration) {
final float ratio = TOTAL_HOURLY_TIME_THRESHOLD / totalUsageTimeInMs; final float ratio = slotDuration / totalUsageTimeInMs;
if (sDebug) { if (sDebug) {
Log.w(TAG, String.format("abnormal usage time %d|%d for:\n%s", Log.w(TAG, String.format("abnormal usage time %d|%d for:\n%s",
Duration.ofMillis(foregroundUsageTimeInMs).getSeconds(), Duration.ofMillis(foregroundUsageTimeInMs).getSeconds(),
Duration.ofMillis(backgroundUsageTimeInMs).getSeconds(), Duration.ofMillis(backgroundUsageTimeInMs).getSeconds(),
currentEntry)); selectedBatteryEntry));
} }
foregroundUsageTimeInMs = foregroundUsageTimeInMs =
Math.round(foregroundUsageTimeInMs * ratio); Math.round(foregroundUsageTimeInMs * ratio);
@@ -1634,14 +1634,14 @@ public final class DataProcessor {
// Compute the screen on time and make sure it won't exceed the threshold. // Compute the screen on time and make sure it won't exceed the threshold.
final long screenOnTime = Math.min( final long screenOnTime = Math.min(
(long) TOTAL_HOURLY_TIME_THRESHOLD, (long) slotDuration,
getScreenOnTime( getScreenOnTime(
appUsageMap, appUsageMap,
selectedBatteryEntry.mUserId, selectedBatteryEntry.mUserId,
selectedBatteryEntry.mPackageName)); selectedBatteryEntry.mPackageName));
// Make sure the background + screen-on time will not exceed the threshold. // Make sure the background + screen-on time will not exceed the threshold.
backgroundUsageTimeInMs = Math.min( backgroundUsageTimeInMs = Math.min(
backgroundUsageTimeInMs, (long) TOTAL_HOURLY_TIME_THRESHOLD - screenOnTime); backgroundUsageTimeInMs, (long) slotDuration - screenOnTime);
final BatteryDiffEntry currentBatteryDiffEntry = new BatteryDiffEntry( final BatteryDiffEntry currentBatteryDiffEntry = new BatteryDiffEntry(
context, context,
foregroundUsageTimeInMs, foregroundUsageTimeInMs,
@@ -1948,23 +1948,12 @@ public final class DataProcessor {
return calendar.getTimeInMillis(); return calendar.getTimeInMillis();
} }
private static long getDiffValue(long v1, long v2, long v3) { private static long getDiffValue(long v1, long v2) {
return (v2 > v1 ? v2 - v1 : 0) + (v3 > v2 ? v3 - v2 : 0); return v2 > v1 ? v2 - v1 : 0;
} }
private static double getDiffValue(double v1, double v2, double v3) { private static double getDiffValue(double v1, double v2) {
return (v2 > v1 ? v2 - v1 : 0) + (v3 > v2 ? v3 - v2 : 0); return v2 > v1 ? v2 - v1 : 0;
}
@Nullable
private static BatteryHistEntry selectBatteryHistEntry(
final BatteryHistEntry... batteryHistEntries) {
for (BatteryHistEntry entry : batteryHistEntries) {
if (entry != null && entry != EMPTY_BATTERY_HIST_ENTRY) {
return entry;
}
}
return null;
} }
private static Set<String> getSystemAppsPackageNames(Context context) { private static Set<String> getSystemAppsPackageNames(Context context) {