Loading android/app/src/com/android/bluetooth/mcp/McpService.java +58 −18 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.GuardedBy; import com.android.internal.annotations.VisibleForTesting; import java.util.HashMap; Loading @@ -41,8 +42,11 @@ public class McpService extends ProfileService { private static final String TAG = "BluetoothMcpService"; private static McpService sMcpService; private static MediaControlProfile sGmcsForTesting; private static MediaControlProfile sGmcs; private Object mLock = new Object(); @GuardedBy("mLock") private MediaControlProfile mGmcs; private Map<BluetoothDevice, Integer> mDeviceAuthorizations = new HashMap<>(); private Handler mHandler = new Handler(Looper.getMainLooper()); Loading @@ -68,11 +72,12 @@ public class McpService extends ProfileService { @VisibleForTesting public static MediaControlProfile getMediaControlProfile() { return sGmcs; return sGmcsForTesting; } @VisibleForTesting public static void setMediaControlProfileForTesting(MediaControlProfile mediaControlProfile) { sGmcs = mediaControlProfile; sGmcsForTesting = mediaControlProfile; } @Override Loading Loading @@ -100,11 +105,19 @@ public class McpService extends ProfileService { // Mark service as started setMcpService(this); if (sGmcs == null) { synchronized (mLock) { if (getGmcsLocked() == null) { // Initialize the Media Control Service Server sGmcs = new MediaControlProfile(this); mGmcs = new MediaControlProfile(this); // Requires this service to be already started thus we have to make it an async call mHandler.post(() -> sGmcs.init()); mHandler.post(() -> { synchronized (mLock) { if (mGmcs != null) { mGmcs.init(); } } }); } } return true; Loading @@ -121,9 +134,17 @@ public class McpService extends ProfileService { return true; } if (sGmcs != null) { sGmcs.cleanup(); sGmcs = null; synchronized (mLock) { // A runnable for calling mGmcs.init() could be pending on mHandler mHandler.removeCallbacksAndMessages(null); if (mGmcs != null) { mGmcs.cleanup(); mGmcs = null; } if (sGmcsForTesting != null) { sGmcsForTesting.cleanup(); sGmcsForTesting = null; } } // Mark service as stopped Loading @@ -138,6 +159,17 @@ public class McpService extends ProfileService { } } @Override public void dump(StringBuilder sb) { super.dump(sb); synchronized (mLock) { MediaControlProfile gmcs = getGmcsLocked(); if (gmcs != null) { gmcs.dump(sb); } } } public void onDeviceUnauthorized(BluetoothDevice device) { Log.w(TAG, "onDeviceUnauthorized - authorization notification not implemented yet "); } Loading @@ -148,7 +180,12 @@ public class McpService extends ProfileService { : BluetoothDevice.ACCESS_REJECTED; mDeviceAuthorizations.put(device, authorization); sGmcs.onDeviceAuthorizationSet(device); synchronized (mLock) { MediaControlProfile gmcs = getGmcsLocked(); if (gmcs != null) { gmcs.onDeviceAuthorizationSet(device); } } } public int getDeviceAuthorization(BluetoothDevice device) { Loading @@ -157,6 +194,15 @@ public class McpService extends ProfileService { return mDeviceAuthorizations.getOrDefault(device, BluetoothDevice.ACCESS_UNKNOWN); } @GuardedBy("mLock") private MediaControlProfile getGmcsLocked() { if (sGmcsForTesting != null) { return sGmcsForTesting; } else { return mGmcs; } } /** * Binder object: must be a static class or memory leak may occur */ Loading Loading @@ -195,10 +241,4 @@ public class McpService extends ProfileService { mService = null; } } @Override public void dump(StringBuilder sb) { super.dump(sb); sGmcs.dump(sb); } } Loading
android/app/src/com/android/bluetooth/mcp/McpService.java +58 −18 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.GuardedBy; import com.android.internal.annotations.VisibleForTesting; import java.util.HashMap; Loading @@ -41,8 +42,11 @@ public class McpService extends ProfileService { private static final String TAG = "BluetoothMcpService"; private static McpService sMcpService; private static MediaControlProfile sGmcsForTesting; private static MediaControlProfile sGmcs; private Object mLock = new Object(); @GuardedBy("mLock") private MediaControlProfile mGmcs; private Map<BluetoothDevice, Integer> mDeviceAuthorizations = new HashMap<>(); private Handler mHandler = new Handler(Looper.getMainLooper()); Loading @@ -68,11 +72,12 @@ public class McpService extends ProfileService { @VisibleForTesting public static MediaControlProfile getMediaControlProfile() { return sGmcs; return sGmcsForTesting; } @VisibleForTesting public static void setMediaControlProfileForTesting(MediaControlProfile mediaControlProfile) { sGmcs = mediaControlProfile; sGmcsForTesting = mediaControlProfile; } @Override Loading Loading @@ -100,11 +105,19 @@ public class McpService extends ProfileService { // Mark service as started setMcpService(this); if (sGmcs == null) { synchronized (mLock) { if (getGmcsLocked() == null) { // Initialize the Media Control Service Server sGmcs = new MediaControlProfile(this); mGmcs = new MediaControlProfile(this); // Requires this service to be already started thus we have to make it an async call mHandler.post(() -> sGmcs.init()); mHandler.post(() -> { synchronized (mLock) { if (mGmcs != null) { mGmcs.init(); } } }); } } return true; Loading @@ -121,9 +134,17 @@ public class McpService extends ProfileService { return true; } if (sGmcs != null) { sGmcs.cleanup(); sGmcs = null; synchronized (mLock) { // A runnable for calling mGmcs.init() could be pending on mHandler mHandler.removeCallbacksAndMessages(null); if (mGmcs != null) { mGmcs.cleanup(); mGmcs = null; } if (sGmcsForTesting != null) { sGmcsForTesting.cleanup(); sGmcsForTesting = null; } } // Mark service as stopped Loading @@ -138,6 +159,17 @@ public class McpService extends ProfileService { } } @Override public void dump(StringBuilder sb) { super.dump(sb); synchronized (mLock) { MediaControlProfile gmcs = getGmcsLocked(); if (gmcs != null) { gmcs.dump(sb); } } } public void onDeviceUnauthorized(BluetoothDevice device) { Log.w(TAG, "onDeviceUnauthorized - authorization notification not implemented yet "); } Loading @@ -148,7 +180,12 @@ public class McpService extends ProfileService { : BluetoothDevice.ACCESS_REJECTED; mDeviceAuthorizations.put(device, authorization); sGmcs.onDeviceAuthorizationSet(device); synchronized (mLock) { MediaControlProfile gmcs = getGmcsLocked(); if (gmcs != null) { gmcs.onDeviceAuthorizationSet(device); } } } public int getDeviceAuthorization(BluetoothDevice device) { Loading @@ -157,6 +194,15 @@ public class McpService extends ProfileService { return mDeviceAuthorizations.getOrDefault(device, BluetoothDevice.ACCESS_UNKNOWN); } @GuardedBy("mLock") private MediaControlProfile getGmcsLocked() { if (sGmcsForTesting != null) { return sGmcsForTesting; } else { return mGmcs; } } /** * Binder object: must be a static class or memory leak may occur */ Loading Loading @@ -195,10 +241,4 @@ public class McpService extends ProfileService { mService = null; } } @Override public void dump(StringBuilder sb) { super.dump(sb); sGmcs.dump(sb); } }