From aa5ee4d65f8788e2a0afcd198367450853fd72ac Mon Sep 17 00:00:00 2001 From: John Spurlock Date: Fri, 25 Jul 2014 13:05:12 -0400 Subject: [PATCH] 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 --- core/java/android/provider/Settings.java | 7 +++++ media/java/android/media/AudioService.java | 30 +++++++++++++++++++--- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java index 34d7c802afa09..6a77805437bd9 100644 --- a/core/java/android/provider/Settings.java +++ b/core/java/android/provider/Settings.java @@ -4629,6 +4629,13 @@ public final class Settings { */ 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. * diff --git a/media/java/android/media/AudioService.java b/media/java/android/media/AudioService.java index d43aceb04799a..ae7c50130c899 100644 --- a/media/java/android/media/AudioService.java +++ b/media/java/android/media/AudioService.java @@ -71,6 +71,7 @@ import android.provider.Settings.System; 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 @@ public class AudioService extends IAudioService.Stub { 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 @@ public class AudioService extends IAudioService.Stub { 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 @@ public class AudioService extends IAudioService.Stub { 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 @@ public class AudioService extends IAudioService.Stub { // 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 @@ public class AudioService extends IAudioService.Stub { 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 @@ public class AudioService extends IAudioService.Stub { 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 @@ public class AudioService extends IAudioService.Stub { 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) {