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

Commit d5e4b836 authored by Eric Laurent's avatar Eric Laurent Committed by Automerger Merge Worker
Browse files

Merge "AudioService: default volume group is not restored" am: 9a9dab22

Change-Id: I206e083e70ad6f4c4a81cb5e8e239addcc2652f8
parents a3937db0 9a9dab22
Loading
Loading
Loading
Loading
+61 −22
Original line number Diff line number Diff line
@@ -1251,10 +1251,10 @@ public class AudioService extends IAudioService.Stub
    private void updateDefaultVolumes() {
        for (int stream = 0; stream < mStreamStates.length; stream++) {
            if (stream != mStreamVolumeAlias[stream]) {
                AudioSystem.DEFAULT_STREAM_VOLUME[stream] = rescaleIndex(
                        AudioSystem.DEFAULT_STREAM_VOLUME[mStreamVolumeAlias[stream]],
                AudioSystem.DEFAULT_STREAM_VOLUME[stream] = (rescaleIndex(
                        AudioSystem.DEFAULT_STREAM_VOLUME[mStreamVolumeAlias[stream]] * 10,
                        mStreamVolumeAlias[stream],
                        stream);
                        stream) + 5) / 10;
            }
        }
    }
@@ -4498,7 +4498,9 @@ public class AudioService extends IAudioService.Stub
            } catch (IllegalArgumentException e) {
                // Volume Groups without attributes are not controllable through set/get volume
                // using attributes. Do not append them.
                Log.d(TAG, "volume group " + avg.name() + " for internal policy needs");
                if (DEBUG_VOL) {
                    Log.v(TAG, "volume group " + avg.name() + " for internal policy needs");
                }
                continue;
            }
            sVolumeGroupStates.append(avg.getId(), new VolumeGroupState(avg));
@@ -4519,7 +4521,9 @@ public class AudioService extends IAudioService.Stub
    }

    private void readVolumeGroupsSettings() {
        Log.v(TAG, "readVolumeGroupsSettings");
        if (DEBUG_VOL) {
            Log.v(TAG, "readVolumeGroupsSettings.");
        }
        for (int i = 0; i < sVolumeGroupStates.size(); i++) {
            final VolumeGroupState vgs = sVolumeGroupStates.valueAt(i);
            vgs.readSettings();
@@ -4529,7 +4533,9 @@ public class AudioService extends IAudioService.Stub

    // Called upon crash of AudioServer
    private void restoreVolumeGroups() {
        if (DEBUG_VOL) {
            Log.v(TAG, "restoreVolumeGroups");
        }
        for (int i = 0; i < sVolumeGroupStates.size(); i++) {
            final VolumeGroupState vgs = sVolumeGroupStates.valueAt(i);
            vgs.applyAllVolumes();
@@ -4565,7 +4571,9 @@ public class AudioService extends IAudioService.Stub

        private VolumeGroupState(AudioVolumeGroup avg) {
            mAudioVolumeGroup = avg;
            if (DEBUG_VOL) {
                Log.v(TAG, "VolumeGroupState for " + avg.toString());
            }
            for (final AudioAttributes aa : avg.getAudioAttributes()) {
                if (!aa.equals(AudioProductStrategy.sDefaultAttributes)) {
                    mAudioAttributes = aa;
@@ -4660,11 +4668,19 @@ public class AudioService extends IAudioService.Stub
            return mIndexMin;
        }

        private boolean isValidLegacyStreamType() {
            return (mLegacyStreamType != AudioSystem.STREAM_DEFAULT)
                    && (mLegacyStreamType < mStreamStates.length);
        }

        public void applyAllVolumes() {
            synchronized (VolumeGroupState.class) {
                if (mLegacyStreamType != AudioSystem.STREAM_DEFAULT) {
                    // No-op to avoid regression with stream based volume management
                    return;
                int deviceForStream = AudioSystem.DEVICE_NONE;
                int volumeIndexForStream = 0;
                if (isValidLegacyStreamType()) {
                    // Prevent to apply settings twice when group is associated to public stream
                    deviceForStream = getDeviceForStream(mLegacyStreamType);
                    volumeIndexForStream = getStreamVolume(mLegacyStreamType);
                }
                // apply device specific volumes first
                int index;
@@ -4672,16 +4688,30 @@ public class AudioService extends IAudioService.Stub
                    final int device = mIndexMap.keyAt(i);
                    if (device != AudioSystem.DEVICE_OUT_DEFAULT) {
                        index = mIndexMap.valueAt(i);
                        if (device == deviceForStream && volumeIndexForStream == index) {
                            continue;
                        }
                        if (DEBUG_VOL) {
                            Log.v(TAG, "applyAllVolumes: restore index " + index + " for group "
                                    + mAudioVolumeGroup.name() + " and device "
                                    + AudioSystem.getOutputDeviceName(device));
                        }
                        setVolumeIndexInt(index, device, 0 /*flags*/);
                    }
                }
                // apply default volume last: by convention , default device volume will be used
                index = getIndex(AudioSystem.DEVICE_OUT_DEFAULT);
                Log.v(TAG, "applyAllVolumes: restore default device index " + index + " for group "
                        + mAudioVolumeGroup.name());
                if (DEBUG_VOL) {
                    Log.v(TAG, "applyAllVolumes: restore default device index " + index
                            + " for group " + mAudioVolumeGroup.name());
                }
                if (isValidLegacyStreamType()) {
                    int defaultStreamIndex = (mStreamStates[mLegacyStreamType]
                            .getIndex(AudioSystem.DEVICE_OUT_DEFAULT) + 5) / 10;
                    if (defaultStreamIndex == index) {
                        return;
                    }
                }
                setVolumeIndexInt(index, AudioSystem.DEVICE_OUT_DEFAULT, 0 /*flags*/);
            }
        }
@@ -4690,9 +4720,12 @@ public class AudioService extends IAudioService.Stub
            if (mUseFixedVolume) {
                return;
            }
            if (DEBUG_VOL) {
                Log.v(TAG, "persistVolumeGroup: storing index " + getIndex(device) + " for group "
                    + mAudioVolumeGroup.name() + " and device "
                    + AudioSystem.getOutputDeviceName(device));
                        + mAudioVolumeGroup.name()
                        + ", device " + AudioSystem.getOutputDeviceName(device)
                        + " and User=" + ActivityManager.getCurrentUser());
            }
            boolean success = Settings.System.putIntForUser(mContentResolver,
                    getSettingNameForDevice(device),
                    getIndex(device),
@@ -4722,12 +4755,18 @@ public class AudioService extends IAudioService.Stub
                    index = Settings.System.getIntForUser(
                            mContentResolver, name, defaultIndex, UserHandle.USER_CURRENT);
                    if (index == -1) {
                        if (DEBUG_VOL) {
                            Log.e(TAG, "readSettings: No index stored for group "
                                + mAudioVolumeGroup.name() + ", device " + name);
                                    + mAudioVolumeGroup.name() + ", device " + name
                                    + ", User=" + ActivityManager.getCurrentUser());
                        }
                        continue;
                    }
                    if (DEBUG_VOL) {
                        Log.v(TAG, "readSettings: found stored index " + getValidIndex(index)
                             + " for group " + mAudioVolumeGroup.name() + ", device: " + name);
                                 + " for group " + mAudioVolumeGroup.name() + ", device: " + name
                                 + ", User=" + ActivityManager.getCurrentUser());
                    }
                    mIndexMap.put(device, getValidIndex(index));
                }
            }