Volume: Persist unsafe volume playback time.

Instead of warning after every reboot, remember the
playback time after a user confirmation and only
reset after the 20 hour playback threshold.

Bug:16543104
Change-Id: I783358d97b88302a28fe77a8eb88bcd338ef1c87
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
index 34d7c80..6a77805 100644
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -4630,6 +4630,13 @@
         public static final String SKIP_FIRST_USE_HINTS = "skip_first_use_hints";
 
         /**
+         * Persisted playback time after a user confirmation of an unsafe volume level.
+         *
+         * @hide
+         */
+        public static final String UNSAFE_VOLUME_MUSIC_ACTIVE_MS = "unsafe_volume_music_active_ms";
+
+        /**
          * This are the settings to be backed up.
          *
          * NOTE: Settings are backed up and restored in the order they appear
diff --git a/media/java/android/media/AudioService.java b/media/java/android/media/AudioService.java
index d43aceb..ae7c501 100644
--- a/media/java/android/media/AudioService.java
+++ b/media/java/android/media/AudioService.java
@@ -71,6 +71,7 @@
 import android.telecomm.TelecommManager;
 import android.text.TextUtils;
 import android.util.Log;
+import android.util.MathUtils;
 import android.util.Slog;
 import android.view.KeyEvent;
 import android.view.Surface;
@@ -197,6 +198,7 @@
     private static final int MSG_BROADCAST_BT_CONNECTION_STATE = 19;
     private static final int MSG_UNLOAD_SOUND_EFFECTS = 20;
     private static final int MSG_SYSTEM_READY = 21;
+    private static final int MSG_PERSIST_MUSIC_ACTIVE_MS = 22;
     // start of messages handled under wakelock
     //   these messages can only be queued, i.e. sent with queueMsgUnderWakeLock(),
     //   and not with sendMsg(..., ..., SENDMSG_QUEUE, ...)
@@ -2147,6 +2149,9 @@
         checkAllAliasStreamVolumes();
 
         synchronized (mSafeMediaVolumeState) {
+            mMusicActiveMs = MathUtils.constrain(Settings.Secure.getIntForUser(mContentResolver,
+                    Settings.Secure.UNSAFE_VOLUME_MUSIC_ACTIVE_MS, 0, UserHandle.USER_CURRENT),
+                    0, UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX);
             if (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE) {
                 enforceSafeMediaVolume();
             }
@@ -2719,12 +2724,17 @@
                             setSafeMediaVolumeEnabled(true);
                             mMusicActiveMs = 0;
                         }
+                        saveMusicActiveMs();
                     }
                 }
             }
         }
     }
 
+    private void saveMusicActiveMs() {
+        mAudioHandler.obtainMessage(MSG_PERSIST_MUSIC_ACTIVE_MS, mMusicActiveMs, 0).sendToTarget();
+    }
+
     private void onConfigureSafeVolume(boolean force) {
         synchronized (mSafeMediaVolumeState) {
             int mcc = mContext.getResources().getConfiguration().mcc;
@@ -2745,8 +2755,13 @@
                     // the 30 seconds timeout for forced configuration. In this case we don't reset
                     // it to "active".
                     if (mSafeMediaVolumeState != SAFE_MEDIA_VOLUME_INACTIVE) {
-                        mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_ACTIVE;
-                        enforceSafeMediaVolume();
+                        if (mMusicActiveMs == 0) {
+                            mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_ACTIVE;
+                            enforceSafeMediaVolume();
+                        } else {
+                            // We have existing playback time recorded, already confirmed.
+                            mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_INACTIVE;
+                        }
                     }
                 } else {
                     persistedState = SAFE_MEDIA_VOLUME_DISABLED;
@@ -4074,6 +4089,13 @@
                 case MSG_SYSTEM_READY:
                     onSystemReady();
                     break;
+
+                case MSG_PERSIST_MUSIC_ACTIVE_MS:
+                    final int musicActiveMs = msg.arg1;
+                    Settings.Secure.putIntForUser(mContentResolver,
+                            Settings.Secure.UNSAFE_VOLUME_MUSIC_ACTIVE_MS, musicActiveMs,
+                            UserHandle.USER_CURRENT);
+                    break;
             }
         }
     }
@@ -4901,7 +4923,8 @@
                     enforceSafeMediaVolume();
                 } else if (!on && (mSafeMediaVolumeState == SAFE_MEDIA_VOLUME_ACTIVE)) {
                     mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_INACTIVE;
-                    mMusicActiveMs = 0;
+                    mMusicActiveMs = 1;  // nonzero = confirmed
+                    saveMusicActiveMs();
                     sendMsg(mAudioHandler,
                             MSG_CHECK_MUSIC_ACTIVE,
                             SENDMSG_REPLACE,
@@ -5075,6 +5098,7 @@
         pw.print("  mSafeMediaVolumeIndex="); pw.println(mSafeMediaVolumeIndex);
         pw.print("  mPendingVolumeCommand="); pw.println(mPendingVolumeCommand);
         pw.print("  mMusicActiveMs="); pw.println(mMusicActiveMs);
+        pw.print("  mMcc="); pw.println(mMcc);
     }
 
     private static String safeMediaVolumeStateToString(Integer state) {