@@ -457,6 +457,7 @@ public AudioService(Context context) {
457457 updateStreamVolumeAlias (false /*updateVolumes*/ );
458458 createStreamStates ();
459459
460+ mSafeMediaVolumeEnabled = new Boolean (true );
460461 synchronized (mSafeMediaVolumeEnabled ) {
461462 enforceSafeMediaVolume ();
462463 }
@@ -1715,7 +1716,9 @@ private void readAudioSettings(boolean userSwitch) {
17151716 checkAllAliasStreamVolumes ();
17161717
17171718 synchronized (mSafeMediaVolumeEnabled ) {
1718- enforceSafeMediaVolume ();
1719+ if (mSafeMediaVolumeEnabled ) {
1720+ enforceSafeMediaVolume ();
1721+ }
17191722 }
17201723
17211724 // apply new ringer mode
@@ -2184,11 +2187,14 @@ private void onCheckMusicActive() {
21842187 sendMsg (mAudioHandler ,
21852188 MSG_CHECK_MUSIC_ACTIVE ,
21862189 SENDMSG_REPLACE ,
2187- device ,
2190+ 0 ,
21882191 0 ,
21892192 null ,
21902193 MUSIC_ACTIVE_POLL_PERIOD_MS );
2191- if (AudioSystem .isStreamActive (AudioSystem .STREAM_MUSIC , 0 )) {
2194+ int index = mStreamStates [AudioSystem .STREAM_MUSIC ].getIndex (device ,
2195+ false /*lastAudible*/ );
2196+ if (AudioSystem .isStreamActive (AudioSystem .STREAM_MUSIC , 0 ) &&
2197+ (index > mSafeMediaVolumeIndex )) {
21922198 // Approximate cumulative active music time
21932199 mMusicActiveMs += MUSIC_ACTIVE_POLL_PERIOD_MS ;
21942200 if (mMusicActiveMs > UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX ) {
@@ -2461,14 +2467,6 @@ private int getDeviceForStream(int stream) {
24612467 public void setWiredDeviceConnectionState (int device , int state , String name ) {
24622468 synchronized (mConnectedDevices ) {
24632469 int delay = checkSendBecomingNoisyIntent (device , state );
2464- if ((device & mSafeMediaVolumeDevices ) != 0 ) {
2465- setSafeMediaVolumeEnabled (state != 0 );
2466- // insert delay to allow new volume to apply before switching to headphones
2467- if ((delay < SAFE_VOLUME_DELAY_MS ) && (state != 0 )) {
2468- delay = SAFE_VOLUME_DELAY_MS ;
2469- }
2470- }
2471-
24722470 queueMsgUnderWakeLock (mAudioHandler ,
24732471 MSG_SET_WIRED_DEVICE_CONNECTION_STATE ,
24742472 device ,
@@ -3519,9 +3517,20 @@ private void onSetWiredDeviceConnectionState(int device, int state, String name)
35193517 }
35203518 boolean isUsb = ((device & AudioSystem .DEVICE_OUT_ALL_USB ) != 0 );
35213519 handleDeviceConnection ((state == 1 ), device , (isUsb ? name : "" ));
3522- if ((state != 0 ) && ((device == AudioSystem .DEVICE_OUT_WIRED_HEADSET ) ||
3523- (device == AudioSystem .DEVICE_OUT_WIRED_HEADPHONE ))) {
3524- setBluetoothA2dpOnInt (false );
3520+ if (state != 0 ) {
3521+ if ((device == AudioSystem .DEVICE_OUT_WIRED_HEADSET ) ||
3522+ (device == AudioSystem .DEVICE_OUT_WIRED_HEADPHONE )) {
3523+ setBluetoothA2dpOnInt (false );
3524+ }
3525+ if ((device & mSafeMediaVolumeDevices ) != 0 ) {
3526+ sendMsg (mAudioHandler ,
3527+ MSG_CHECK_MUSIC_ACTIVE ,
3528+ SENDMSG_REPLACE ,
3529+ 0 ,
3530+ 0 ,
3531+ null ,
3532+ MUSIC_ACTIVE_POLL_PERIOD_MS );
3533+ }
35253534 }
35263535 if (!isUsb ) {
35273536 sendDeviceConnectionIntent (device , state , name );
@@ -5528,7 +5537,7 @@ public AudioRoutesInfo startWatchingRoutes(IAudioRoutesObserver observer) {
55285537 // mSafeMediaVolumeEnabled indicates whether the media volume is limited over headphones.
55295538 // It is true by default when headphones or a headset are inserted and can be overriden by
55305539 // calling AudioService.disableSafeMediaVolume() (when user opts out).
5531- private Boolean mSafeMediaVolumeEnabled = new Boolean ( false ) ;
5540+ private Boolean mSafeMediaVolumeEnabled ;
55325541 // mSafeMediaVolumeIndex is the cached value of config_safe_media_volume_index property
55335542 private final int mSafeMediaVolumeIndex ;
55345543 // mSafeMediaVolumeDevices lists the devices for which safe media volume is enforced,
@@ -5540,7 +5549,6 @@ public AudioRoutesInfo startWatchingRoutes(IAudioRoutesObserver observer) {
55405549 private int mMusicActiveMs ;
55415550 private static final int UNSAFE_VOLUME_MUSIC_ACTIVE_MS_MAX = (20 * 3600 * 1000 ); // 20 hours
55425551 private static final int MUSIC_ACTIVE_POLL_PERIOD_MS = 60000 ; // 1 minute polling interval
5543- private static final int SAFE_VOLUME_DELAY_MS = 500 ; // 500ms before switching to headphones
55445552
55455553 private void setSafeMediaVolumeEnabled (boolean on ) {
55465554 synchronized (mSafeMediaVolumeEnabled ) {
0 commit comments