Loading android/app/src/com/android/bluetooth/mcp/McpService.java +16 −9 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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()); Loading @@ -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 Loading Loading @@ -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; Loading @@ -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 Loading @@ -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) { Loading Loading @@ -192,6 +199,6 @@ public class McpService extends ProfileService { @Override public void dump(StringBuilder sb) { super.dump(sb); mGmcs.dump(sb); sGmcs.dump(sb); } } android/app/tests/unit/src/com/android/bluetooth/mcp/McpServiceTest.java +27 −4 Original line number Diff line number Diff line Loading @@ -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 { Loading Loading @@ -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()); } }); } } Loading
android/app/src/com/android/bluetooth/mcp/McpService.java +16 −9 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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()); Loading @@ -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 Loading Loading @@ -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; Loading @@ -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 Loading @@ -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) { Loading Loading @@ -192,6 +199,6 @@ public class McpService extends ProfileService { @Override public void dump(StringBuilder sb) { super.dump(sb); mGmcs.dump(sb); sGmcs.dump(sb); } }
android/app/tests/unit/src/com/android/bluetooth/mcp/McpServiceTest.java +27 −4 Original line number Diff line number Diff line Loading @@ -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 { Loading Loading @@ -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()); } }); } }