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

Commit a03d1727 authored by Kyunglyul Hyun's avatar Kyunglyul Hyun
Browse files

mcp: reset MediaControlProfile instance in stop()

When McpService was reused (e.g. stop() and start())
MediaControlProfile was not initialized so GMCS is not created.
It could happen even when McpService is recreated since it is
a static variable.

This CL ensures clear MediaControlProfile instance so that
next time McpService is started, GMCS is created again.

Tag: #feature
Bug: 209606901
Test: atest McpServiceTest
Change-Id: Ibcc6cdad7224e3c363b30510fdfcf228828014f6
parent 030236f8
Loading
Loading
Loading
Loading
+16 −9
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.util.Log;

import com.android.bluetooth.Utils;
import com.android.bluetooth.btservice.ProfileService;
import com.android.internal.annotations.VisibleForTesting;

import java.util.HashMap;
import java.util.Map;
@@ -41,7 +42,7 @@ public class McpService extends ProfileService {

    private static McpService sMcpService;

    private static MediaControlProfile mGmcs;
    private static MediaControlProfile sGmcs;
    private Map<BluetoothDevice, Integer> mDeviceAuthorizations = new HashMap<>();
    private Handler mHandler = new Handler(Looper.getMainLooper());

@@ -65,8 +66,13 @@ public class McpService extends ProfileService {
        return sMcpService;
    }

    @VisibleForTesting
    public static MediaControlProfile getMediaControlProfile() {
        return sGmcs;
    }

    public static void setMediaControlProfileForTesting(MediaControlProfile mediaControlProfile) {
        mGmcs = mediaControlProfile;
        sGmcs = mediaControlProfile;
    }

    @Override
@@ -94,11 +100,11 @@ public class McpService extends ProfileService {
        // Mark service as started
        setMcpService(this);

        if (mGmcs == null) {
        if (sGmcs == null) {
            // Initialize the Media Control Service Server
            mGmcs = new MediaControlProfile(this);
            sGmcs = new MediaControlProfile(this);
            // Requires this service to be already started thus we have to make it an async call
            mHandler.post(() -> mGmcs.init());
            mHandler.post(() -> sGmcs.init());
        }

        return true;
@@ -115,8 +121,9 @@ public class McpService extends ProfileService {
            return true;
        }

        if (mGmcs != null) {
            mGmcs.cleanup();
        if (sGmcs != null) {
            sGmcs.cleanup();
            sGmcs = null;
        }

        // Mark service as stopped
@@ -141,7 +148,7 @@ public class McpService extends ProfileService {
                : BluetoothDevice.ACCESS_REJECTED;
        mDeviceAuthorizations.put(device, authorization);

        mGmcs.onDeviceAuthorizationSet(device);
        sGmcs.onDeviceAuthorizationSet(device);
    }

    public int getDeviceAuthorization(BluetoothDevice device) {
@@ -192,6 +199,6 @@ public class McpService extends ProfileService {
    @Override
    public void dump(StringBuilder sb) {
        super.dump(sb);
        mGmcs.dump(sb);
        sGmcs.dump(sb);
    }
}
+27 −4
Original line number Diff line number Diff line
@@ -50,11 +50,15 @@ public class McpServiceTest {
    private McpService mMcpService;
    private Context mTargetContext;

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

    @Mock private AdapterService mAdapterService;
    @Mock private MediaControlGattService mMockMcpService;
    @Mock private MediaControlProfile mMediaControlProfile;
    @Mock
    private AdapterService mAdapterService;
    @Mock
    private MediaControlGattService mMockMcpService;
    @Mock
    private MediaControlProfile mMediaControlProfile;

    @Before
    public void setUp() throws Exception {
@@ -115,4 +119,23 @@ public class McpServiceTest {
        Assert.assertEquals(BluetoothDevice.ACCESS_REJECTED,
                mMcpService.getDeviceAuthorization(device1));
    }

    @Test
    public void testStopMcpService() {
        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
            public void run() {
                Assert.assertTrue(mMcpService.stop());
            }
        });
        Assert.assertNull(McpService.getMcpService());
        Assert.assertNull(McpService.getMediaControlProfile());

        McpService.setMediaControlProfileForTesting(mMediaControlProfile);
        // Try to restart the service. Note: must be done on the main thread
        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
            public void run() {
                Assert.assertTrue(mMcpService.start());
            }
        });
    }
}