Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f0d14952 authored by Vlad Popa's avatar Vlad Popa
Browse files

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
parent b6bb3b65
Loading
Loading
Loading
Loading
+24 −9
Original line number Diff line number Diff line
@@ -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 {