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

Commit d967fee1 authored by Vlad Popa's avatar Vlad Popa
Browse files

Add death handler for IAudioDeviceVolumeDispatcher

Also allow to adjust the volume infos that are used for listening to
volume changes.

Test: atest AudioDeviceVolumeManagerTest
Bug: 396216663
Flag: EXEMPT bugfix
Change-Id: I938cf278f914b5f4c2e130530a89a4d13aabd04d
parent 1901a217
Loading
Loading
Loading
Loading
+37 −5
Original line number Diff line number Diff line
@@ -954,7 +954,8 @@ public class AudioService extends IAudioService.Stub
    /**
     * Stores information about a device using absolute volume behavior.
     */
    private static final class AbsoluteVolumeDeviceInfo {
    private static final class AbsoluteVolumeDeviceInfo implements IBinder.DeathRecipient {
        private final AudioService mParent;
        private final AudioDeviceAttributes mDevice;
        private final List<VolumeInfo> mVolumeInfos;
        private final IAudioDeviceVolumeDispatcher mCallback;
@@ -962,16 +963,26 @@ public class AudioService extends IAudioService.Stub
        private @AudioManager.AbsoluteDeviceVolumeBehavior int mDeviceVolumeBehavior;
        private AbsoluteVolumeDeviceInfo(
                AudioService parent,
                AudioDeviceAttributes device,
                List<VolumeInfo> volumeInfos,
                IAudioDeviceVolumeDispatcher callback,
                boolean handlesVolumeAdjustment,
                @AudioManager.AbsoluteDeviceVolumeBehavior int behavior) {
            this.mParent = parent;
            this.mDevice = device;
            this.mVolumeInfos = volumeInfos;
            this.mCallback = callback;
            this.mHandlesVolumeAdjustment = handlesVolumeAdjustment;
            this.mDeviceVolumeBehavior = behavior;
            try {
                this.mCallback.asBinder().linkToDeath(this, 0);
            } catch (RemoteException | NullPointerException e) {
                // NPE can be raised when mocking the callback object
                Slog.w(TAG, "Exception: " + e
                        + "\nCannot listen to callback binder death for device " + mDevice);
            }
        }
        /**
@@ -991,6 +1002,25 @@ public class AudioService extends IAudioService.Stub
            }
            return null;
        }
        @Override
        public void binderDied() {
            if (mParent.removeAudioSystemDeviceOutFromAbsVolumeDevices(mDevice.getInternalType())
                    != null) {
                mParent.dispatchDeviceVolumeBehavior(mDevice,
                        AudioManager.DEVICE_VOLUME_BEHAVIOR_VARIABLE);
            }
        }
        public void unlinkToDeath() {
            try {
                mCallback.asBinder().unlinkToDeath(this, 0);
            } catch (NullPointerException e) {
                // NPE can be raised when mocking the callback object
                Slog.w(TAG, "Exception: " + e
                        + "\nCannot unlink to death, null binder object for device " + mDevice);
            }
        }
    }
    // Devices for the which use the "absolute volume" concept (framework sends audio signal
@@ -8142,16 +8172,16 @@ public class AudioService extends IAudioService.Stub
        int deviceOut = device.getInternalType();
        if (register) {
            AbsoluteVolumeDeviceInfo info = new AbsoluteVolumeDeviceInfo(
            AbsoluteVolumeDeviceInfo info = new AbsoluteVolumeDeviceInfo(this,
                    device, volumes, cb, handlesVolumeAdjustment, deviceVolumeBehavior);
            final AbsoluteVolumeDeviceInfo oldInfo = getAbsoluteVolumeDeviceInfo(deviceOut);
            addAudioSystemDeviceOutToAbsVolumeDevices(deviceOut, info);
            boolean volumeBehaviorChanged = (oldInfo == null)
                    || (oldInfo.mDeviceVolumeBehavior != deviceVolumeBehavior);
            if (volumeBehaviorChanged) {
                removeAudioSystemDeviceOutFromFullVolumeDevices(deviceOut);
                removeAudioSystemDeviceOutFromFixedVolumeDevices(deviceOut);
                addAudioSystemDeviceOutToAbsVolumeDevices(deviceOut, info);
                dispatchDeviceVolumeBehavior(device, deviceVolumeBehavior);
            }
@@ -8177,8 +8207,10 @@ public class AudioService extends IAudioService.Stub
                }
            }
        } else {
            boolean wasAbsVol = removeAudioSystemDeviceOutFromAbsVolumeDevices(deviceOut) != null;
            if (wasAbsVol) {
            AbsoluteVolumeDeviceInfo deviceInfo = removeAudioSystemDeviceOutFromAbsVolumeDevices(
                    deviceOut);
            if (deviceInfo != null) {
                deviceInfo.unlinkToDeath();
                dispatchDeviceVolumeBehavior(device, AudioManager.DEVICE_VOLUME_BEHAVIOR_VARIABLE);
            }
        }