CSD: Remove cached obsolete records

SoundDoseRecords with negative values are obsolete and need to be
removed in order to keep the cached records in sync with the native
side.

Test: dumpsys and logs
Bug: 262058952
Change-Id: I9db4b27c63a624f27be237e1540c0881b1893247
This commit is contained in:
Vlad Popa
2023-01-03 15:09:05 +01:00
parent b6bb3b656b
commit f0d149529a

View File

@@ -202,15 +202,7 @@ public class SoundDoseHelper {
}
}
mCurrentCsd = currentCsd;
mDoseRecords.addAll(Arrays.asList(records));
long totalDuration = 0;
for (SoundDoseRecord record : records) {
Log.i(TAG, " new record: csd=" + record.value
+ " averageMel=" + record.averageMel + " timestamp=" + record.timestamp
+ " duration=" + record.duration);
totalDuration += record.duration;
}
mLogger.enqueue(SoundDoseEvent.getDoseUpdateEvent(currentCsd, totalDuration));
updateSoundDoseRecords(records, currentCsd);
}
};
@@ -626,6 +618,29 @@ public class SoundDoseHelper {
return null;
}
private void updateSoundDoseRecords(SoundDoseRecord[] newRecords, float currentCsd) {
long totalDuration = 0;
for (SoundDoseRecord record : newRecords) {
Log.i(TAG, " new record: " + record);
totalDuration += record.duration;
if (record.value < 0) {
// Negative value means the record timestamp exceeded the CSD rolling window size
// and needs to be removed
if (!mDoseRecords.removeIf(
r -> r.value == -record.value && r.timestamp == record.timestamp
&& r.averageMel == record.averageMel
&& r.duration == record.duration)) {
Log.w(TAG, "Could not find cached record to remove: " + record);
}
} else {
mDoseRecords.add(record);
}
}
mLogger.enqueue(SoundDoseEvent.getDoseUpdateEvent(currentCsd, totalDuration));
}
// StreamVolumeCommand contains the information needed to defer the process of
// setStreamVolume() in case the user has to acknowledge the safe volume warning message.
private static class StreamVolumeCommand {