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

Commit 05274f34 authored by Eric Laurent's avatar Eric Laurent
Browse files

AudioService: improve initial safe volume delay

AudioService relies on a valid mmc in order to enforce the headset
volume limitation or not. There is a timeout to enforce the limitation
if no mcc is configured after boot.
Until this timeout is reached or a valid SIM is detected the headset
volume is not limited.

This change makes that the last known volume limitation state (enforced or
not) is persisted so that next time we boot, last known state is applied until
a new mcc is configured if any. In most cases, the mcc does not change from one
boot to the next and we do the right thing. If teh mcc does change, the correct
policy will be enforced when the mcc is detected or after the timeout.

Also fix a bug where the volume panel was not displayed if the limitation mechanism
is triggered at the first press on VOL+ key.

Bug 7455275.

Change-Id: Id0f2996d893d38c6a14f4f9e4a0e9e3be17ef127
parent 89ac38bf
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -5314,6 +5314,12 @@ public final class Settings {
         */
        public static final String DOCK_AUDIO_MEDIA_ENABLED = "dock_audio_media_enabled";

        /**
         * Persisted safe headphone volume management state by AudioService
         * @hide
         */
        public static final String AUDIO_SAFE_VOLUME_STATE = "audio_safe_volume_state";

        /**
         * Settings to backup. This is here so that it's in the same place as the settings
         * keys and easy to update.
+97 −61
Original line number Diff line number Diff line
@@ -157,6 +157,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
    private static final int MSG_BROADCAST_AUDIO_BECOMING_NOISY = 25;
    private static final int MSG_CONFIGURE_SAFE_MEDIA_VOLUME = 26;
    private static final int MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED = 27;
    private static final int MSG_PERSIST_SAFE_VOLUME_STATE = 28;

    // flags for MSG_PERSIST_VOLUME indicating if current and/or last audible volume should be
    // persisted
@@ -481,6 +482,14 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
                null,
                0);

        mSafeMediaVolumeState = new Integer(Settings.Global.getInt(mContentResolver,
                                                        Settings.Global.AUDIO_SAFE_VOLUME_STATE,
                                                        SAFE_MEDIA_VOLUME_NOT_CONFIGURED));
        // The default safe volume index read here will be replaced by the actual value when
        // the mcc is read by onConfigureSafeVolume()
        mSafeMediaVolumeIndex = mContext.getResources().getInteger(
                com.android.internal.R.integer.config_safe_media_volume_index) * 10;

        readPersistedSettings();
        mSettingsObserver = new SettingsObserver();
        updateStreamVolumeAlias(false /*updateVolumes*/);
@@ -488,8 +497,6 @@ public class AudioService extends IAudioService.Stub implements OnFinished {

        mMediaServerOk = true;

        mSafeMediaVolumeState = new Integer(SAFE_MEDIA_VOLUME_NOT_CONFIGURED);

        // Call setRingerModeInt() to apply correct mute
        // state on streams affected by ringer mode.
        mRingerModeMutedStreams = 0;
@@ -822,14 +829,15 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
        // convert one UI step (+/-1) into a number of internal units on the stream alias
        int step = rescaleIndex(10, streamType, streamTypeAlias);

        if ((direction == AudioManager.ADJUST_RAISE) &&
                !checkSafeMediaVolume(streamTypeAlias, aliasIndex + step, device)) {
            return;
        }

        int index;
        int oldIndex;

        if ((direction == AudioManager.ADJUST_RAISE) &&
                !checkSafeMediaVolume(streamTypeAlias, aliasIndex + step, device)) {
            index = mStreamStates[streamType].getIndex(device,
                                                 (streamState.muteCount() != 0)  /* lastAudible */);
            oldIndex = index;
        } else {
            flags &= ~AudioManager.FLAG_FIXED_VOLUME;
            if ((streamTypeAlias == AudioSystem.STREAM_MUSIC) &&
                   ((device & mFixedVolumeDevices) != 0)) {
@@ -888,6 +896,7 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
                    index = mStreamStates[streamType].getIndex(device, false  /* lastAudible */);
                }
            }
        }
        sendVolumeUpdate(streamType, oldIndex, index, flags);
    }

@@ -2306,13 +2315,31 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
                        com.android.internal.R.integer.config_safe_media_volume_index) * 10;
                boolean safeMediaVolumeEnabled = mContext.getResources().getBoolean(
                        com.android.internal.R.bool.config_safe_media_volume_enabled);

                // The persisted state is either "disabled" or "active": this is the state applied
                // next time we boot and cannot be "inactive"
                int persistedState;
                if (safeMediaVolumeEnabled) {
                    persistedState = SAFE_MEDIA_VOLUME_ACTIVE;
                    // The state can already be "inactive" here if the user has forced it before
                    // 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();
                    }
                } else {
                    persistedState = SAFE_MEDIA_VOLUME_DISABLED;
                    mSafeMediaVolumeState = SAFE_MEDIA_VOLUME_DISABLED;
                }
                mMcc = mcc;
                sendMsg(mAudioHandler,
                        MSG_PERSIST_SAFE_VOLUME_STATE,
                        SENDMSG_QUEUE,
                        persistedState,
                        0,
                        null,
                        0);
            }
        }
    }
@@ -3224,6 +3251,12 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
            AudioSystem.setForceUse(usage, config);
        }

        private void onPersistSafeVolumeState(int state) {
            Settings.Global.putInt(mContentResolver,
                    Settings.Global.AUDIO_SAFE_VOLUME_STATE,
                    state);
        }

        @Override
        public void handleMessage(Message msg) {

@@ -3433,6 +3466,9 @@ public class AudioService extends IAudioService.Stub implements OnFinished {
                case MSG_CONFIGURE_SAFE_MEDIA_VOLUME:
                    onConfigureSafeVolume((msg.what == MSG_CONFIGURE_SAFE_MEDIA_VOLUME_FORCED));
                    break;
                case MSG_PERSIST_SAFE_VOLUME_STATE:
                    onPersistSafeVolumeState(msg.arg1);
                    break;
            }
        }
    }