Merge "Refactor function to support slot size not must be two." into udc-dev am: fbe85872b3 am: 581f3e8286

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/22414532

Change-Id: I8b3205e91381b33782a041414dbcb79042951a64
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Zaiyue Xue
2023-04-07 14:21:43 +00:00
committed by Automerger Merge Worker

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); // Collects all keys in these three time slot records as all populations.
final Long nextTimestamp = currentTimestamp + DateUtils.HOUR_IN_MILLIS; final Set<String> allBatteryHistEntryKeys = new ArraySet<>();
final Long nextTwoTimestamp = nextTimestamp + DateUtils.HOUR_IN_MILLIS; for (Map<String, BatteryHistEntry> slotBatteryHistMap : slotBatteryHistoryList) {
// Fetches BatteryHistEntry data from corresponding time slot. if (slotBatteryHistMap.isEmpty()) {
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 // 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 // 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. // represent there is no enough data to apply interpolation arithmetic.
if (currentBatteryHistMap.isEmpty()
|| nextBatteryHistMap.isEmpty()
|| nextTwoBatteryHistMap.isEmpty()) {
return null; return null;
} }
allBatteryHistEntryKeys.addAll(slotBatteryHistMap.keySet());
// Collects all keys in these three time slot records as all populations. }
final Set<String> allBatteryHistEntryKeys = new ArraySet<>();
allBatteryHistEntryKeys.addAll(currentBatteryHistMap.keySet());
allBatteryHistEntryKeys.addAll(nextBatteryHistMap.keySet());
allBatteryHistEntryKeys.addAll(nextTwoBatteryHistMap.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;
long backgroundUsageTimeInMs = 0;
double consumePower = 0;
double foregroundUsageConsumePower = 0;
double foregroundServiceUsageConsumePower = 0;
double backgroundUsageConsumePower = 0;
double cachedUsageConsumePower = 0;
for (int i = 0; i < batteryHistEntries.size() - 1; i++) {
final BatteryHistEntry currentEntry = batteryHistEntries.get(i);
final BatteryHistEntry nextEntry = batteryHistEntries.get(i + 1);
foregroundUsageTimeInMs +=
getDiffValue( getDiffValue(
currentEntry.mForegroundUsageTimeInMs, currentEntry.mForegroundUsageTimeInMs,
nextEntry.mForegroundUsageTimeInMs, nextEntry.mForegroundUsageTimeInMs);
nextTwoEntry.mForegroundUsageTimeInMs); backgroundUsageTimeInMs +=
long backgroundUsageTimeInMs =
getDiffValue( getDiffValue(
currentEntry.mBackgroundUsageTimeInMs, currentEntry.mBackgroundUsageTimeInMs,
nextEntry.mBackgroundUsageTimeInMs, nextEntry.mBackgroundUsageTimeInMs);
nextTwoEntry.mBackgroundUsageTimeInMs); consumePower +=
double consumePower =
getDiffValue( getDiffValue(
currentEntry.mConsumePower, currentEntry.mConsumePower,
nextEntry.mConsumePower, nextEntry.mConsumePower);
nextTwoEntry.mConsumePower); foregroundUsageConsumePower +=
double foregroundUsageConsumePower =
getDiffValue( getDiffValue(
currentEntry.mForegroundUsageConsumePower, currentEntry.mForegroundUsageConsumePower,
nextEntry.mForegroundUsageConsumePower, nextEntry.mForegroundUsageConsumePower);
nextTwoEntry.mForegroundUsageConsumePower); foregroundServiceUsageConsumePower +=
double foregroundServiceUsageConsumePower =
getDiffValue( getDiffValue(
currentEntry.mForegroundServiceUsageConsumePower, currentEntry.mForegroundServiceUsageConsumePower,
nextEntry.mForegroundServiceUsageConsumePower, nextEntry.mForegroundServiceUsageConsumePower);
nextTwoEntry.mForegroundServiceUsageConsumePower); backgroundUsageConsumePower +=
double backgroundUsageConsumePower =
getDiffValue( getDiffValue(
currentEntry.mBackgroundUsageConsumePower, currentEntry.mBackgroundUsageConsumePower,
nextEntry.mBackgroundUsageConsumePower, nextEntry.mBackgroundUsageConsumePower);
nextTwoEntry.mBackgroundUsageConsumePower); cachedUsageConsumePower +=
double cachedUsageConsumePower =
getDiffValue( getDiffValue(
currentEntry.mCachedUsageConsumePower, currentEntry.mCachedUsageConsumePower,
nextEntry.mCachedUsageConsumePower, nextEntry.mCachedUsageConsumePower);
nextTwoEntry.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) {