CSD: Persist csd and recover after boot or audioserver crash

Using the Settings.Global to persist any new CSD entry on the
AudioService side. Reason for global is because the standard is defined
for a PMP and not user. When the system boots we recover the previously
persisted CSD. After a crash in audioserver we apply the AudioService
cached values to the restarted audioserver. This will include the
recovery of the csd value and records which lead to that value.
New settings should not be backed up since they are determinedi for each
device independently.

Test: logs and dumpsys after audioserver kill and reboot
Bug: 264254900
Change-Id: Ie0c7b400d466981d198145d0ca0fc9c9e29eca63
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
index b2d8996..4e218c5 100644
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -15498,6 +15498,25 @@
         public static final String AUDIO_SAFE_VOLUME_STATE = "audio_safe_volume_state";
 
         /**
+         * Persisted safe hearding current CSD value. Values are stored as float percentages where
+         * 1.f represents 100% sound dose has been reached.
+         * @hide
+         */
+        public static final String AUDIO_SAFE_CSD_CURRENT_VALUE = "audio_safe_csd_current_value";
+
+        /**
+         * Persisted safe hearding next CSD warning value. Values are stored as float percentages.
+         * @hide
+         */
+        public static final String AUDIO_SAFE_CSD_NEXT_WARNING = "audio_safe_csd_next_warning";
+
+        /**
+         * Persisted safe hearding dose records (see {@link android.media.SoundDoseRecord})
+         * @hide
+         */
+        public static final String AUDIO_SAFE_CSD_DOSE_RECORDS = "audio_safe_csd_dose_records";
+
+        /**
          * URL for tzinfo (time zone) updates
          * @hide
          */
diff --git a/core/proto/android/providers/settings/global.proto b/core/proto/android/providers/settings/global.proto
index fd0d9c5..128de8b 100644
--- a/core/proto/android/providers/settings/global.proto
+++ b/core/proto/android/providers/settings/global.proto
@@ -88,6 +88,9 @@
 
     optional SettingProto assisted_gps_enabled = 14 [ (android.privacy).dest = DEST_AUTOMATIC ];
     optional SettingProto audio_safe_volume_state = 15 [ (android.privacy).dest = DEST_AUTOMATIC ];
+    optional SettingProto audio_safe_csd_current_value = 157 [ (android.privacy).dest = DEST_AUTOMATIC ];
+    optional SettingProto audio_safe_csd_next_warning = 158 [ (android.privacy).dest = DEST_AUTOMATIC ];
+    optional SettingProto audio_safe_csd_dose_records = 159 [ (android.privacy).dest = DEST_AUTOMATIC ];
 
     reserved 17; // Used to be autofill_compat_mode_allowed_packages
 
@@ -1087,5 +1090,5 @@
 
     // Please insert fields in alphabetical order and group them into messages
     // if possible (to avoid reaching the method limit).
-    // Next tag = 157;
+    // Next tag = 160;
 }
diff --git a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProtoDumpUtil.java b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProtoDumpUtil.java
index 25b9906..7aeba95 100644
--- a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProtoDumpUtil.java
+++ b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProtoDumpUtil.java
@@ -238,6 +238,15 @@
         dumpSetting(s, p,
                 Settings.Global.AUDIO_SAFE_VOLUME_STATE,
                 GlobalSettingsProto.AUDIO_SAFE_VOLUME_STATE);
+        dumpSetting(s, p,
+                Settings.Global.AUDIO_SAFE_CSD_CURRENT_VALUE,
+                GlobalSettingsProto.AUDIO_SAFE_CSD_CURRENT_VALUE);
+        dumpSetting(s, p,
+                Settings.Global.AUDIO_SAFE_CSD_NEXT_WARNING,
+                GlobalSettingsProto.AUDIO_SAFE_CSD_NEXT_WARNING);
+        dumpSetting(s, p,
+                Settings.Global.AUDIO_SAFE_CSD_DOSE_RECORDS,
+                GlobalSettingsProto.AUDIO_SAFE_CSD_DOSE_RECORDS);
 
         final long autofillToken = p.start(GlobalSettingsProto.AUTOFILL);
         dumpSetting(s, p,
diff --git a/packages/SettingsProvider/test/src/android/provider/SettingsBackupTest.java b/packages/SettingsProvider/test/src/android/provider/SettingsBackupTest.java
index 3782ce4..f1ea482 100644
--- a/packages/SettingsProvider/test/src/android/provider/SettingsBackupTest.java
+++ b/packages/SettingsProvider/test/src/android/provider/SettingsBackupTest.java
@@ -131,6 +131,9 @@
                     Settings.Global.ART_VERIFIER_VERIFY_DEBUGGABLE,
                     Settings.Global.ASSISTED_GPS_ENABLED,
                     Settings.Global.AUDIO_SAFE_VOLUME_STATE,
+                    Settings.Global.AUDIO_SAFE_CSD_CURRENT_VALUE,
+                    Settings.Global.AUDIO_SAFE_CSD_NEXT_WARNING,
+                    Settings.Global.AUDIO_SAFE_CSD_DOSE_RECORDS,
                     Settings.Global.AUTOFILL_LOGGING_LEVEL,
                     Settings.Global.AUTOFILL_MAX_PARTITIONS_SIZE,
                     Settings.Global.AUTOFILL_MAX_VISIBLE_DATASETS,
diff --git a/services/core/java/com/android/server/audio/AudioService.java b/services/core/java/com/android/server/audio/AudioService.java
index b4992d7..998a0c4 100644
--- a/services/core/java/com/android/server/audio/AudioService.java
+++ b/services/core/java/com/android/server/audio/AudioService.java
@@ -1462,7 +1462,7 @@
 
         mNm = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
 
-        mSoundDoseHelper.configureSafeMediaVolume(/*forced=*/true, TAG);
+        mSoundDoseHelper.configureSafeMedia(/*forced=*/true, TAG);
 
         initA11yMonitoring();
 
@@ -10224,7 +10224,7 @@
             // reading new configuration "safely" (i.e. under try catch) in case anything
             // goes wrong.
             Configuration config = context.getResources().getConfiguration();
-            mSoundDoseHelper.configureSafeMediaVolume(/*forced*/false, TAG);
+            mSoundDoseHelper.configureSafeMedia(/*forced*/false, TAG);
 
             boolean cameraSoundForced = readCameraSoundForced();
             synchronized (mSettingsLock) {
diff --git a/services/core/java/com/android/server/audio/SoundDoseHelper.java b/services/core/java/com/android/server/audio/SoundDoseHelper.java
index bc61b37..b301a3b 100644
--- a/services/core/java/com/android/server/audio/SoundDoseHelper.java
+++ b/services/core/java/com/android/server/audio/SoundDoseHelper.java
@@ -38,6 +38,7 @@
 import android.os.SystemProperties;
 import android.os.UserHandle;
 import android.provider.Settings;
+import android.text.TextUtils;
 import android.util.Log;
 import android.util.MathUtils;
 
@@ -55,6 +56,7 @@
 import java.util.List;
 import java.util.Objects;
 import java.util.Set;
+import java.util.stream.Collectors;
 
 /**
  * Safe media volume management.
@@ -77,14 +79,16 @@
     // SAFE_MEDIA_VOLUME_DISABLED according to country option. If not SAFE_MEDIA_VOLUME_DISABLED, it
     // can be set to SAFE_MEDIA_VOLUME_INACTIVE by calling AudioService.disableSafeMediaVolume()
     // (when user opts out).
+    // Note: when CSD calculation is enabled the state is set to SAFE_MEDIA_VOLUME_DISABLED
     private static final int SAFE_MEDIA_VOLUME_NOT_CONFIGURED = 0;
     private static final int SAFE_MEDIA_VOLUME_DISABLED = 1;
     private static final int SAFE_MEDIA_VOLUME_INACTIVE = 2;  // confirmed
     private static final int SAFE_MEDIA_VOLUME_ACTIVE = 3;  // unconfirmed
 
-    private static final int MSG_CONFIGURE_SAFE_MEDIA_VOLUME = SAFE_MEDIA_VOLUME_MSG_START + 1;
+    private static final int MSG_CONFIGURE_SAFE_MEDIA = SAFE_MEDIA_VOLUME_MSG_START + 1;
     private static final int MSG_PERSIST_SAFE_VOLUME_STATE = SAFE_MEDIA_VOLUME_MSG_START + 2;
     private static final int MSG_PERSIST_MUSIC_ACTIVE_MS = SAFE_MEDIA_VOLUME_MSG_START + 3;
+    private static final int MSG_PERSIST_CSD_VALUES = SAFE_MEDIA_VOLUME_MSG_START + 4;
 
     private static final int UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX = (20 * 3600 * 1000); // 20 hours
 
@@ -100,14 +104,16 @@
     private static final int CSD_WARNING_TIMEOUT_MS_ACCUMULATION_START = -1;
     private static final int CSD_WARNING_TIMEOUT_MS_MOMENTARY_EXPOSURE = 5000;
 
+    private static final String PERSIST_CSD_RECORD_FIELD_SEPARATOR = ",";
+    private static final String PERSIST_CSD_RECORD_SEPARATOR_CHAR = "|";
+    private static final String PERSIST_CSD_RECORD_SEPARATOR = "\\|";
+
     private final EventLogger mLogger = new EventLogger(AudioService.LOG_NB_EVENTS_SOUND_DOSE,
             "CSD updates");
 
     private int mMcc = 0;
 
-    private final boolean mEnableCsd;
-
-    final Object mSafeMediaVolumeStateLock = new Object();
+    private final Object mSafeMediaVolumeStateLock = new Object();
     private int mSafeMediaVolumeState;
 
     // Used when safe volume warning message display is requested by setStreamVolume(). In this
@@ -148,10 +154,18 @@
     @NonNull private final AudioHandler mAudioHandler;
     @NonNull private final ISafeHearingVolumeController mVolumeController;
 
+    private final boolean mEnableCsd;
+
     private ISoundDose mSoundDose;
+
+    private final Object mCsdStateLock = new Object();
+
+    @GuardedBy("mCsdStateLock")
     private float mCurrentCsd = 0.f;
     // dose at which the next dose reached warning occurs
+    @GuardedBy("mCsdStateLock")
     private float mNextCsdWarning = 1.0f;
+    @GuardedBy("mCsdStateLock")
     private final List<SoundDoseRecord> mDoseRecords = new ArrayList<>();
 
     private final Context mContext;
@@ -169,11 +183,16 @@
         }
 
         public void onNewCsdValue(float currentCsd, SoundDoseRecord[] records) {
+            if (!mEnableCsd) {
+                Log.w(TAG, "onNewCsdValue: csd not supported, ignoring value");
+                return;
+            }
+
             Log.i(TAG, "onNewCsdValue: " + currentCsd);
-            if (mCurrentCsd < currentCsd) {
-                // dose increase: going over next threshold?
-                if ((mCurrentCsd < mNextCsdWarning) && (currentCsd >= mNextCsdWarning)) {
-                    if (mEnableCsd) {
+            synchronized (mCsdStateLock) {
+                if (mCurrentCsd < currentCsd) {
+                    // dose increase: going over next threshold?
+                    if ((mCurrentCsd < mNextCsdWarning) && (currentCsd >= mNextCsdWarning)) {
                         if (mNextCsdWarning == 5.0f) {
                             // 500% dose repeat
                             mVolumeController.postDisplayCsdWarning(
@@ -188,17 +207,18 @@
                                     getTimeoutMsForWarning(
                                             AudioManager.CSD_WARNING_DOSE_REACHED_1X));
                         }
+                        mNextCsdWarning += 1.0f;
                     }
-                    mNextCsdWarning += 1.0f;
+                } else {
+                    // dose decrease: dropping below previous threshold of warning?
+                    if ((currentCsd < mNextCsdWarning - 1.0f) && (
+                            mNextCsdWarning >= 2.0f)) {
+                        mNextCsdWarning -= 1.0f;
+                    }
                 }
-            } else {
-                // dose decrease: dropping below previous threshold of warning?
-                if ((currentCsd < mNextCsdWarning - 1.0f) && (mNextCsdWarning >= 2.0f)) {
-                    mNextCsdWarning -= 1.0f;
-                }
+                mCurrentCsd = currentCsd;
+                updateSoundDoseRecords_l(records, currentCsd);
             }
-            mCurrentCsd = currentCsd;
-            updateSoundDoseRecords(records, currentCsd);
         }
     };
 
@@ -213,20 +233,22 @@
 
         mContext = context;
 
-        mSafeMediaVolumeState = mSettings.getGlobalInt(audioService.getContentResolver(),
-                Settings.Global.AUDIO_SAFE_VOLUME_STATE, 0);
-
         mEnableCsd = mContext.getResources().getBoolean(R.bool.config_audio_csd_enabled_default);
+        if (mEnableCsd) {
+            mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_DISABLED;
+        } else {
+            mSafeMediaVolumeState = mSettings.getGlobalInt(audioService.getContentResolver(),
+                    Settings.Global.AUDIO_SAFE_VOLUME_STATE, 0);
+        }
 
         // The default safe volume index read here will be replaced by the actual value when
-        // the mcc is read by onConfigureSafeVolume()
+        // the mcc is read by onConfigureSafeMedia()
+        // For now we use the same index for RS2 initial warning with CSD
         mSafeMediaVolumeIndex = mContext.getResources().getInteger(
                 R.integer.config_safe_media_volume_index) * 10;
 
         mAlarmManager = (AlarmManager) mContext.getSystemService(
                 Context.ALARM_SERVICE);
-
-        initCsd();
     }
 
     float getRs2Value() {
@@ -455,8 +477,8 @@
         }
     }
 
-    /*package*/ void configureSafeMediaVolume(boolean forced, String caller) {
-        int msg = MSG_CONFIGURE_SAFE_MEDIA_VOLUME;
+    /*package*/ void configureSafeMedia(boolean forced, String caller) {
+        int msg = MSG_CONFIGURE_SAFE_MEDIA;
         mAudioHandler.removeMessages(msg);
 
         long time = 0;
@@ -506,8 +528,8 @@
 
     /*package*/ void handleMessage(Message msg) {
         switch (msg.what) {
-            case MSG_CONFIGURE_SAFE_MEDIA_VOLUME:
-                onConfigureSafeVolume((msg.arg1 == 1), (String) msg.obj);
+            case MSG_CONFIGURE_SAFE_MEDIA:
+                onConfigureSafeMedia((msg.arg1 == 1), (String) msg.obj);
                 break;
             case MSG_PERSIST_SAFE_VOLUME_STATE:
                 onPersistSafeVolumeState(msg.arg1);
@@ -518,8 +540,13 @@
                         Settings.Secure.UNSAFE_VOLUME_MUSIC_ACTIVE_MS, musicActiveMs,
                         UserHandle.USER_CURRENT);
                 break;
+            case MSG_PERSIST_CSD_VALUES:
+                onPersistSoundDoseRecords();
+                break;
+            default:
+                Log.e(TAG, "Unexpected msg to handle: " + msg.what);
+                break;
         }
-
     }
 
     /*package*/ void dump(PrintWriter pw) {
@@ -539,33 +566,44 @@
 
     /*package*/void reset() {
         Log.d(TAG, "Reset the sound dose helper");
-        initCsd();
-    }
-
-    private void initCsd() {
-        synchronized (mSafeMediaVolumeStateLock) {
-            if (mEnableCsd) {
-                Log.v(TAG, "Initializing sound dose");
-
-                mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_DISABLED;
-                mSoundDose = AudioSystem.getSoundDoseInterface(mSoundDoseCallback);
-                try {
-                    if (mSoundDose != null && mSoundDose.asBinder().isBinderAlive()) {
-                        if (mCurrentCsd != 0.f) {
-                            Log.d(TAG, "Resetting the saved sound dose value " + mCurrentCsd);
-                            SoundDoseRecord[] records = mDoseRecords.toArray(
-                                    new SoundDoseRecord[0]);
-                            mSoundDose.resetCsd(mCurrentCsd, records);
-                        }
+        mSoundDose = AudioSystem.getSoundDoseInterface(mSoundDoseCallback);
+        synchronized (mCsdStateLock) {
+            try {
+                if (mSoundDose != null && mSoundDose.asBinder().isBinderAlive()) {
+                    if (mCurrentCsd != 0.f) {
+                        Log.d(TAG,
+                                "Resetting the saved sound dose value " + mCurrentCsd);
+                        SoundDoseRecord[] records = mDoseRecords.toArray(
+                                new SoundDoseRecord[0]);
+                        mSoundDose.resetCsd(mCurrentCsd, records);
                     }
-                } catch (RemoteException e) {
-                    // noop
                 }
+            } catch (RemoteException e) {
+                // noop
             }
         }
     }
 
-    private void onConfigureSafeVolume(boolean force, String caller) {
+    private void initCsd() {
+        if (mEnableCsd) {
+            Log.v(TAG, "Initializing sound dose");
+
+            synchronized (mCsdStateLock) {
+                // Restore persisted values
+                mCurrentCsd = parseGlobalSettingFloat(
+                        Settings.Global.AUDIO_SAFE_CSD_CURRENT_VALUE, /* defaultValue= */0.f);
+                mNextCsdWarning = parseGlobalSettingFloat(
+                        Settings.Global.AUDIO_SAFE_CSD_NEXT_WARNING, /* defaultValue= */1.f);
+                mDoseRecords.addAll(persistedStringToRecordList(
+                        mSettings.getGlobalString(mAudioService.getContentResolver(),
+                                Settings.Global.AUDIO_SAFE_CSD_DOSE_RECORDS)));
+            }
+
+            reset();
+        }
+    }
+
+    private void onConfigureSafeMedia(boolean force, String caller) {
         synchronized (mSafeMediaVolumeStateLock) {
             int mcc = mContext.getResources().getConfiguration().mcc;
             if ((mMcc != mcc) || ((mMcc == 0) && force)) {
@@ -611,6 +649,10 @@
                                 /*obj=*/null), /*delay=*/0);
             }
         }
+
+        if (mEnableCsd) {
+            initCsd();
+        }
     }
 
     private int getTimeoutMsForWarning(@AudioManager.CsdWarning int csdWarning) {
@@ -701,7 +743,8 @@
         return null;
     }
 
-    private void updateSoundDoseRecords(SoundDoseRecord[] newRecords, float currentCsd) {
+    @GuardedBy("mCsdStateLock")
+    private void updateSoundDoseRecords_l(SoundDoseRecord[] newRecords, float currentCsd) {
         long totalDuration = 0;
         for (SoundDoseRecord record : newRecords) {
             Log.i(TAG, "  new record: " + record);
@@ -721,9 +764,87 @@
             }
         }
 
+        mAudioHandler.sendMessageAtTime(mAudioHandler.obtainMessage(MSG_PERSIST_CSD_VALUES,
+                /* arg1= */0, /* arg2= */0, /* obj= */null), /* delay= */0);
+
         mLogger.enqueue(SoundDoseEvent.getDoseUpdateEvent(currentCsd, totalDuration));
     }
 
+    private void onPersistSoundDoseRecords() {
+        synchronized (mCsdStateLock) {
+            mSettings.putGlobalString(mAudioService.getContentResolver(),
+                    Settings.Global.AUDIO_SAFE_CSD_CURRENT_VALUE,
+                    Float.toString(mCurrentCsd));
+            mSettings.putGlobalString(mAudioService.getContentResolver(),
+                    Settings.Global.AUDIO_SAFE_CSD_NEXT_WARNING,
+                    Float.toString(mNextCsdWarning));
+            mSettings.putGlobalString(mAudioService.getContentResolver(),
+                    Settings.Global.AUDIO_SAFE_CSD_DOSE_RECORDS,
+                    mDoseRecords.stream().map(
+                            SoundDoseHelper::recordToPersistedString).collect(
+                            Collectors.joining(PERSIST_CSD_RECORD_SEPARATOR_CHAR)));
+        }
+    }
+
+    private static String recordToPersistedString(SoundDoseRecord record) {
+        return record.timestamp
+                + PERSIST_CSD_RECORD_FIELD_SEPARATOR + record.duration
+                + PERSIST_CSD_RECORD_FIELD_SEPARATOR + record.value
+                + PERSIST_CSD_RECORD_FIELD_SEPARATOR + record.averageMel;
+    }
+
+    private static List<SoundDoseRecord> persistedStringToRecordList(String records) {
+        if (records == null || records.isEmpty()) {
+            return null;
+        }
+        return Arrays.stream(TextUtils.split(records, PERSIST_CSD_RECORD_SEPARATOR)).map(
+                SoundDoseHelper::persistedStringToRecord).filter(Objects::nonNull).collect(
+                Collectors.toList());
+    }
+
+    private static SoundDoseRecord persistedStringToRecord(String record) {
+        if (record == null || record.isEmpty()) {
+            return null;
+        }
+        final String[] fields = TextUtils.split(record, PERSIST_CSD_RECORD_FIELD_SEPARATOR);
+        if (fields.length != 4) {
+            Log.w(TAG, "Expecting 4 fields for a SoundDoseRecord, parsed " + fields.length);
+            return null;
+        }
+
+        final SoundDoseRecord sdRecord = new SoundDoseRecord();
+        try {
+            sdRecord.timestamp = Long.parseLong(fields[0]);
+            sdRecord.duration = Integer.parseInt(fields[1]);
+            sdRecord.value = Float.parseFloat(fields[2]);
+            sdRecord.averageMel = Float.parseFloat(fields[3]);
+        } catch (NumberFormatException e) {
+            Log.e(TAG, "Unable to parse persisted SoundDoseRecord: " + record, e);
+            return null;
+        }
+
+        return sdRecord;
+    }
+
+    private float parseGlobalSettingFloat(String audioSafeCsdCurrentValue, float defaultValue) {
+        String stringValue = mSettings.getGlobalString(mAudioService.getContentResolver(),
+                audioSafeCsdCurrentValue);
+        if (stringValue == null || stringValue.isEmpty()) {
+            Log.v(TAG, "No value stored in settings " + audioSafeCsdCurrentValue);
+            return defaultValue;
+        }
+
+        float value;
+        try {
+            value = Float.parseFloat(stringValue);
+        } catch (NumberFormatException e) {
+            Log.e(TAG, "Error parsing float from settings " + audioSafeCsdCurrentValue, e);
+            value = defaultValue;
+        }
+
+        return value;
+    }
+
     // 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 {