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

Commit d24fd2d4 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Fix the volume of A2DP devices which switching audio"

parents fa7070cb b4585a92
Loading
Loading
Loading
Loading
+26 −4
Original line number Diff line number Diff line
@@ -495,6 +495,24 @@ public class A2dpService extends ProfileService {
        return true;
    }

    /**
     * Early notification that Hearing Aids will be the active device. This allows the A2DP to save
     * its volume before the Audio Service starts changing its media stream.
     */
    public void earlyNotifyHearingAidActive() {
        enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH ADMIN permission");

        synchronized (mStateMachines) {
            if ((mActiveDevice != null) && (AvrcpTargetService.get() != null)) {
                // Switch active device from A2DP to Hearing Aids.
                if (DBG) {
                    Log.d(TAG, "earlyNotifyHearingAidActive: Save volume for " + mActiveDevice);
                }
                AvrcpTargetService.get().storeVolumeForDevice(mActiveDevice);
            }
        }
    }

    /**
     * Set the active device.
     *
@@ -534,6 +552,14 @@ public class A2dpService extends ProfileService {
            codecStatus = sm.getCodecStatus();

            boolean deviceChanged = !Objects.equals(device, mActiveDevice);
            if ((AvrcpTargetService.get() != null) && (mActiveDevice != null) && deviceChanged) {
                // Switch from one A2DP to another A2DP device
                if (DBG) {
                    Log.d(TAG, "Switch A2DP devices to " + device + " from " + mActiveDevice);
                }
                AvrcpTargetService.get().storeVolumeForDevice(mActiveDevice);
            }

            // This needs to happen before we inform the audio manager that the device
            // disconnected. Please see comment in updateAndBroadcastActiveDevice() for why.
            updateAndBroadcastActiveDevice(device);
@@ -875,10 +901,6 @@ public class A2dpService extends ProfileService {

        synchronized (mStateMachines) {
            if (AvrcpTargetService.get() != null) {
                if (mActiveDevice != null) {
                    AvrcpTargetService.get().storeVolumeForDevice(mActiveDevice);
                }

                AvrcpTargetService.get().volumeDeviceSwitched(device);
            }

+17 −0
Original line number Diff line number Diff line
@@ -33,9 +33,11 @@ import android.util.StatsLog;

import com.android.bluetooth.BluetoothMetricsProto;
import com.android.bluetooth.Utils;
import com.android.bluetooth.a2dp.A2dpService;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.MetricsLogger;
import com.android.bluetooth.btservice.ProfileService;
import com.android.bluetooth.btservice.ServiceFactory;
import com.android.internal.annotations.VisibleForTesting;

import java.util.ArrayList;
@@ -75,6 +77,8 @@ public class HearingAidService extends ProfileService {
    private BroadcastReceiver mBondStateChangedReceiver;
    private BroadcastReceiver mConnectionStateChangedReceiver;

    private final ServiceFactory mFactory = new ServiceFactory();

    @Override
    protected IProfileServiceBinder initBinder() {
        return new BluetoothHearingAidBinder(this);
@@ -608,6 +612,19 @@ public class HearingAidService extends ProfileService {

        StatsLog.write(StatsLog.BLUETOOTH_ACTIVE_DEVICE_CHANGED, BluetoothProfile.HEARING_AID,
                mAdapterService.obfuscateAddress(device));

        if (device != null) {
            // Give an early notification to A2DP that active device is being switched
            // to Hearing Aids before the Audio Service.
            final A2dpService a2dpService = mFactory.getA2dpService();
            if (a2dpService != null) {
                if (DBG) {
                    Log.d(TAG, "earlyNotifyHearingAidActive for " + device);
                }
                a2dpService.earlyNotifyHearingAidActive();
            }
        }

        Intent intent = new Intent(BluetoothHearingAid.ACTION_ACTIVE_DEVICE_CHANGED);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT