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

Commit e0cec887 authored by Ted Wang's avatar Ted Wang Committed by android-build-merger
Browse files

Merge "Remove storeVolumeForDevice() in AvrcpTargetService" am: 18e6dad2

am: 31ab2ade

Change-Id: I66b9298ef192727730569d3abec1dc5be1f8ab9d
parents b69a5f17 31ab2ade
Loading
Loading
Loading
Loading
+0 −34
Original line number Diff line number Diff line
@@ -433,19 +433,9 @@ public class A2dpService extends ProfileService {
        }
    }

    private void storeActiveDeviceVolume() {
        // Make sure volume has been stored before been removed from active.
        if (mFactory.getAvrcpTargetService() != null && mActiveDevice != null) {
            mFactory.getAvrcpTargetService().storeVolumeForDevice(mActiveDevice);
        }
    }

    private void removeActiveDevice(boolean forceStopPlayingAudio) {
        BluetoothDevice previousActiveDevice = mActiveDevice;
        synchronized (mStateMachines) {
            // Make sure volume has been store before device been remove from active.
            storeActiveDeviceVolume();

            // This needs to happen before we inform the audio manager that the device
            // disconnected. Please see comment in updateAndBroadcastActiveDevice() for why.
            updateAndBroadcastActiveDevice(null);
@@ -499,22 +489,6 @@ 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) {
            // Switch active device from A2DP to Hearing Aids.
            if (DBG) {
                Log.d(TAG, "earlyNotifyHearingAidActive: Save volume for " + mActiveDevice);
            }
            storeActiveDeviceVolume();
        }
    }

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

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

            // This needs to happen before we inform the audio manager that the device
            // disconnected. Please see comment in updateAndBroadcastActiveDevice() for why.
            updateAndBroadcastActiveDevice(device);
+0 −18
Original line number Diff line number Diff line
@@ -242,24 +242,6 @@ public class AvrcpTargetService extends ProfileService {
        mVolumeManager.volumeDeviceSwitched(device);
    }

    /**
     * Store the current system volume for a device in order to be retrieved later.
     */
    public void storeVolumeForDevice(BluetoothDevice device) {
        if (device == null) return;

        List<BluetoothDevice> HAActiveDevices = null;
        if (mFactory.getHearingAidService() != null) {
            HAActiveDevices = mFactory.getHearingAidService().getActiveDevices();
        }
        if (HAActiveDevices != null
                && (HAActiveDevices.get(0) != null || HAActiveDevices.get(1) != null)) {
            Log.d(TAG, "Do not store volume when Hearing Aid devices is active");
            return;
        }
        mVolumeManager.storeVolumeForDevice(device);
    }

    /**
     * Remove the stored volume for a device.
     */
+0 −10
Original line number Diff line number Diff line
@@ -33,7 +33,6 @@ 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;
@@ -517,15 +516,6 @@ public class HearingAidService extends ProfileService {
            Long deviceHiSyncId = mDeviceHiSyncIdMap.getOrDefault(device,
                    BluetoothHearingAid.HI_SYNC_ID_INVALID);
            if (deviceHiSyncId != mActiveDeviceHiSyncId) {
                // 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();
                }
                mActiveDeviceHiSyncId = deviceHiSyncId;
                reportActiveDevice(device);
            }
+0 −34
Original line number Diff line number Diff line
@@ -39,9 +39,7 @@ import androidx.test.runner.AndroidJUnit4;

import com.android.bluetooth.R;
import com.android.bluetooth.TestUtils;
import com.android.bluetooth.avrcp.AvrcpTargetService;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.ServiceFactory;
import com.android.bluetooth.btservice.storage.DatabaseManager;

import org.junit.After;
@@ -78,8 +76,6 @@ public class A2dpServiceTest {
    @Mock private AdapterService mAdapterService;
    @Mock private A2dpNativeInterface mA2dpNativeInterface;
    @Mock private DatabaseManager mDatabaseManager;
    @Mock private AvrcpTargetService mAvrcpTargetService;
    @Mock private ServiceFactory mFactory;

    @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule();

@@ -98,13 +94,11 @@ public class A2dpServiceTest {
        TestUtils.setAdapterService(mAdapterService);
        doReturn(MAX_CONNECTED_AUDIO_DEVICES).when(mAdapterService).getMaxConnectedAudioDevices();
        doReturn(false).when(mAdapterService).isQuietModeEnabled();
        doReturn(mAvrcpTargetService).when(mFactory).getAvrcpTargetService();

        mAdapter = BluetoothAdapter.getDefaultAdapter();

        startService();
        mA2dpService.mA2dpNativeInterface = mA2dpNativeInterface;
        mA2dpService.mFactory = mFactory;

        // Override the timeout value to speed up the test
        A2dpStateMachine.sConnectTimeoutMs = TIMEOUT_MS;    // 1s
@@ -252,8 +246,6 @@ public class A2dpServiceTest {
        });
        // Verify that setActiveDevice(null) was called during shutdown
        verify(mA2dpNativeInterface).setActiveDevice(null);
        // Verify that storeVolumeForDevice(mTestDevice) was called during shutdown
        verify(mAvrcpTargetService).storeVolumeForDevice(mTestDevice);
        // Try to restart the service. Note: must be done on the main thread.
        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
            public void run() {
@@ -796,13 +788,11 @@ public class A2dpServiceTest {
        Assert.assertEquals(mTestDevice, mA2dpService.getActiveDevice());
        Assert.assertTrue(mA2dpService.setSilenceMode(mTestDevice, true));
        verify(mA2dpNativeInterface).setSilenceDevice(mTestDevice, true);
        verify(mAvrcpTargetService).storeVolumeForDevice(mTestDevice);
        Assert.assertNull(mA2dpService.getActiveDevice());

        // Test whether active device been resumeed after disable silence mode.
        Assert.assertTrue(mA2dpService.setSilenceMode(mTestDevice, false));
        verify(mA2dpNativeInterface).setSilenceDevice(mTestDevice, false);
        verify(mAvrcpTargetService).storeVolumeForDevice(mTestDevice);
        Assert.assertEquals(mTestDevice, mA2dpService.getActiveDevice());

        // Test that active device should not be changed when silence a non-active device
@@ -810,13 +800,11 @@ public class A2dpServiceTest {
        Assert.assertEquals(mTestDevice, mA2dpService.getActiveDevice());
        Assert.assertTrue(mA2dpService.setSilenceMode(otherDevice, true));
        verify(mA2dpNativeInterface).setSilenceDevice(otherDevice, true);
        verify(mAvrcpTargetService).storeVolumeForDevice(mTestDevice);
        Assert.assertEquals(mTestDevice, mA2dpService.getActiveDevice());

        // Test that active device should not be changed when another device exits silence mode
        Assert.assertTrue(mA2dpService.setSilenceMode(otherDevice, false));
        verify(mA2dpNativeInterface).setSilenceDevice(otherDevice, false);
        verify(mAvrcpTargetService).storeVolumeForDevice(mTestDevice);
        Assert.assertEquals(mTestDevice, mA2dpService.getActiveDevice());
    }

@@ -906,28 +894,6 @@ public class A2dpServiceTest {
                verifySupportTime, verifyNotSupportTime, verifyEnabledTime);
    }

    /**
     * Test that volume level of previous active device will be stored after set active device.
     */
    @Test
    public void testStoreVolumeAfterSetActiveDevice() {
        BluetoothDevice otherDevice = mAdapter.getRemoteDevice("05:04:03:02:01:00");
        connectDevice(otherDevice);
        connectDevice(mTestDevice);
        doReturn(true).when(mA2dpNativeInterface).setActiveDevice(any(BluetoothDevice.class));
        doReturn(true).when(mA2dpNativeInterface).setActiveDevice(null);
        Assert.assertTrue(mA2dpService.setActiveDevice(mTestDevice));

        // Test volume stored for previous active device an adjust for current active device
        Assert.assertTrue(mA2dpService.setActiveDevice(otherDevice));
        verify(mAvrcpTargetService).storeVolumeForDevice(mTestDevice);
        verify(mAvrcpTargetService).getRememberedVolumeForDevice(otherDevice);

        // Test volume store for previous active device when set active device to null
        Assert.assertTrue(mA2dpService.setActiveDevice(null));
        verify(mAvrcpTargetService).storeVolumeForDevice(otherDevice);
    }

    private void connectDevice(BluetoothDevice device) {
        connectDeviceWithCodecStatus(device, null);
    }