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

Commit f1e3ee9e authored by Jakub Pawłowski's avatar Jakub Pawłowski Committed by Gerrit Code Review
Browse files

Merge "mcp: reset MediaControlProfile instance in stop()"

parents 765350a5 a03d1727
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());
            }
        });
    }
}