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

Commit 216a1fde authored by Ted Wang's avatar Ted Wang Committed by Myles Watson
Browse files

Store volume for devices not support absolute volume

Reigister broadcast for AudioManager.VOLUME_CHANGED_ACTION to monitor
STREAM_MUSIC volume value change.
Store volume value for non absolute volume device whenever
STREAM_MUSIC volume value changed.

Bug: 146561367
Bug: 141746655
Test: manual
Change-Id: I317f5fa31b243eb5f8203d00ec4585c931a066fc
parent b331f871
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -114,6 +114,17 @@ public class AvrcpTargetService extends ProfileService {
                        Log.d(TAG, "request to disconnect device " + device);
                    }
                }
            } else if (action.equals(AudioManager.VOLUME_CHANGED_ACTION)) {
                int streamType = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1);
                if (streamType == AudioManager.STREAM_MUSIC) {
                    int volume = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, 0);
                    BluetoothDevice activeDevice = mFactory.getA2dpService().getActiveDevice();
                    if (activeDevice != null
                            && !mVolumeManager.getAbsoluteVolumeSupported(activeDevice)) {
                        Log.d(TAG, "stream volume change to " + volume + " " + activeDevice);
                        mVolumeManager.storeVolumeForDevice(activeDevice, volume);
                    }
                }
            }
        }
    }
@@ -185,6 +196,7 @@ public class AvrcpTargetService extends ProfileService {
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED);
        filter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(AudioManager.VOLUME_CHANGED_ACTION);
        registerReceiver(mReceiver, filter);

        // Only allow the service to be used once it is initialized
+17 −2
Original line number Diff line number Diff line
@@ -115,12 +115,11 @@ class AvrcpVolumeManager extends AudioDeviceCallback {
        volumeMapEditor.apply();
    }

    synchronized void storeVolumeForDevice(@NonNull BluetoothDevice device) {
    synchronized void storeVolumeForDevice(@NonNull BluetoothDevice device, int storeVolume) {
        if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
            return;
        }
        SharedPreferences.Editor pref = getVolumeMap().edit();
        int storeVolume =  mAudioManager.getStreamVolume(STREAM_MUSIC);
        Log.i(TAG, "storeVolume: Storing stream volume level for device " + device
                + " : " + storeVolume);
        mVolumeMap.put(device, storeVolume);
@@ -130,6 +129,11 @@ class AvrcpVolumeManager extends AudioDeviceCallback {
        pref.apply();
    }

    synchronized void storeVolumeForDevice(@NonNull BluetoothDevice device) {
        int storeVolume =  mAudioManager.getStreamVolume(STREAM_MUSIC);
        storeVolumeForDevice(device, storeVolume);
    }

    synchronized void removeStoredVolumeForDevice(@NonNull BluetoothDevice device) {
        if (device.getBondState() != BluetoothDevice.BOND_NONE) {
            return;
@@ -183,6 +187,17 @@ class AvrcpVolumeManager extends AudioDeviceCallback {
        storeVolumeForDevice(device);
    }

    /**
     * True if remote device supported Absolute volume, false if remote device is not supported or
     * not connected.
     */
    boolean getAbsoluteVolumeSupported(BluetoothDevice device) {
        if (mDeviceMap.containsKey(device)) {
            return mDeviceMap.get(device);
        }
        return false;
    }

    @Override
    public synchronized void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {
        if (mCurrentDevice == null) {