From cfa7abc12ee79e6a51173f279371cd75b38c74ff Mon Sep 17 00:00:00 2001 From: Sungsoo Lim Date: Wed, 12 Feb 2020 09:32:05 +0900 Subject: [PATCH] Refactor BluetoothRouteProvider Test: pass MediaRoute2 test Change-Id: I3098f46f2c502c224b05217409351bc5f5860811 --- .../server/media/BluetoothRouteProvider.java | 98 +++++++++---------- .../media/SystemMediaRoute2Provider.java | 10 +- 2 files changed, 52 insertions(+), 56 deletions(-) diff --git a/services/core/java/com/android/server/media/BluetoothRouteProvider.java b/services/core/java/com/android/server/media/BluetoothRouteProvider.java index 647430399bc97..8379614cf423a 100644 --- a/services/core/java/com/android/server/media/BluetoothRouteProvider.java +++ b/services/core/java/com/android/server/media/BluetoothRouteProvider.java @@ -29,7 +29,6 @@ import android.content.Intent; import android.content.IntentFilter; import android.media.AudioManager; import android.media.MediaRoute2Info; -import android.text.TextUtils; import android.util.Slog; import android.util.SparseBooleanArray; @@ -49,6 +48,8 @@ class BluetoothRouteProvider { @SuppressWarnings("WeakerAccess") /* synthetic access */ final Map mBluetoothRoutes = new HashMap<>(); @SuppressWarnings("WeakerAccess") /* synthetic access */ + BluetoothRouteInfo mSelectedRoute = null; + @SuppressWarnings("WeakerAccess") /* synthetic access */ BluetoothA2dp mA2dpProfile; @SuppressWarnings("WeakerAccess") /* synthetic access */ BluetoothHearingAid mHearingAidProfile; @@ -62,8 +63,6 @@ class BluetoothRouteProvider { private final BroadcastReceiver mBroadcastReceiver = new BluetoothBroadcastReceiver(); private final BluetoothProfileListener mProfileListener = new BluetoothProfileListener(); - private BluetoothDevice mActiveDevice = null; - static synchronized BluetoothRouteProvider getInstance(@NonNull Context context, @NonNull BluetoothRoutesUpdatedListener listener) { Objects.requireNonNull(context); @@ -108,27 +107,21 @@ class BluetoothRouteProvider { } /** - * Clears the active device for all known profiles. + * Transfers to a given bluetooth route. + * The dedicated BT device with the route would be activated. + * + * @param routeId the id of the Bluetooth device. {@code null} denotes to clear the use of + * BT routes. */ - public void clearActiveDevices() { - BluetoothA2dp a2dpProfile = mA2dpProfile; - BluetoothHearingAid hearingAidProfile = mHearingAidProfile; - if (a2dpProfile != null) { - a2dpProfile.setActiveDevice(null); + public void transferTo(@Nullable String routeId) { + if (routeId == null) { + clearActiveDevices(); + return; } - if (hearingAidProfile != null) { - hearingAidProfile.setActiveDevice(null); - } - } - /** - * Sets the active device. - * @param deviceId the id of the Bluetooth device - */ - public void setActiveDevice(@NonNull String deviceId) { - BluetoothRouteInfo btRouteInfo = mBluetoothRoutes.get(deviceId); + BluetoothRouteInfo btRouteInfo = mBluetoothRoutes.get(routeId); if (btRouteInfo == null) { - Slog.w(TAG, "setActiveDevice: unknown device id=" + deviceId); + Slog.w(TAG, "setActiveDevice: unknown route id=" + routeId); return; } BluetoothA2dp a2dpProfile = mA2dpProfile; @@ -144,6 +137,20 @@ class BluetoothRouteProvider { } } + /** + * Clears the active device for all known profiles. + */ + private void clearActiveDevices() { + BluetoothA2dp a2dpProfile = mA2dpProfile; + BluetoothHearingAid hearingAidProfile = mHearingAidProfile; + if (a2dpProfile != null) { + a2dpProfile.setActiveDevice(null); + } + if (hearingAidProfile != null) { + hearingAidProfile.setActiveDevice(null); + } + } + private void addEventReceiver(String action, BluetoothEventReceiver eventReceiver) { mEventReceiverMap.put(action, eventReceiver); mIntentFilter.addAction(action); @@ -159,7 +166,8 @@ class BluetoothRouteProvider { } } - @NonNull List getBluetoothRoutes() { + @NonNull + List getAllBluetoothRoutes() { ArrayList routes = new ArrayList<>(); for (BluetoothRouteInfo btRoute : mBluetoothRoutes.values()) { routes.add(btRoute.route); @@ -167,17 +175,14 @@ class BluetoothRouteProvider { return routes; } - @Nullable String getActiveDeviceAddress() { - BluetoothDevice device = mActiveDevice; - if (device == null) { - return null; - } - return device.getAddress(); + @Nullable + String getSelectedRouteId() { + return mSelectedRoute == null ? null : mSelectedRoute.route.getId(); } private void notifyBluetoothRoutesUpdated() { if (mListener != null) { - mListener.onBluetoothRoutesUpdated(getBluetoothRoutes()); + mListener.onBluetoothRoutesUpdated(getAllBluetoothRoutes()); } } @@ -198,15 +203,10 @@ class BluetoothRouteProvider { return newBtRoute; } - private void setRouteConnectionStateForDevice(BluetoothDevice device, + private void setRouteConnectionState(@NonNull BluetoothRouteInfo btRoute, @MediaRoute2Info.ConnectionState int state) { - if (device == null) { - Slog.w(TAG, "setRouteConnectionStateForDevice: device shouldn't be null"); - return; - } - BluetoothRouteInfo btRoute = mBluetoothRoutes.get(device.getAddress()); if (btRoute == null) { - Slog.w(TAG, "setRouteConnectionStateForDevice: route shouldn't be null"); + Slog.w(TAG, "setRouteConnectionState: route shouldn't be null"); return; } if (btRoute.route.getConnectionState() == state) { @@ -260,8 +260,8 @@ class BluetoothRouteProvider { mBluetoothRoutes.put(device.getAddress(), btRoute); } if (activeDevices.contains(device)) { - mActiveDevice = device; - setRouteConnectionStateForDevice(device, + mSelectedRoute = btRoute; + setRouteConnectionState(mSelectedRoute, MediaRoute2Info.CONNECTION_STATE_CONNECTED); } @@ -344,20 +344,18 @@ class BluetoothRouteProvider { public void onReceive(Context context, Intent intent, BluetoothDevice device) { switch (intent.getAction()) { case BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED: - String prevActiveDeviceAddress = - (mActiveDevice == null) ? null : mActiveDevice.getAddress(); - String curActiveDeviceAddress = - (device == null) ? null : device.getAddress(); - if (!TextUtils.equals(prevActiveDeviceAddress, curActiveDeviceAddress)) { - if (mActiveDevice != null) { - setRouteConnectionStateForDevice(mActiveDevice, + if (mSelectedRoute == null + || !mSelectedRoute.btDevice.equals(device)) { + if (mSelectedRoute != null) { + setRouteConnectionState(mSelectedRoute, MediaRoute2Info.CONNECTION_STATE_DISCONNECTED); } - if (device != null) { - setRouteConnectionStateForDevice(device, + mSelectedRoute = (device == null) ? null + : mBluetoothRoutes.get(device.getAddress()); + if (mSelectedRoute != null) { + setRouteConnectionState(mSelectedRoute, MediaRoute2Info.CONNECTION_STATE_CONNECTED); } - mActiveDevice = device; notifyBluetoothRoutesUpdated(); } break; @@ -386,10 +384,8 @@ class BluetoothRouteProvider { btRoute.connectedProfiles.delete(profile); if (btRoute.connectedProfiles.size() == 0) { mBluetoothRoutes.remove(device.getAddress()); - if (mActiveDevice != null - && TextUtils.equals(mActiveDevice.getAddress(), - device.getAddress())) { - mActiveDevice = null; + if (mSelectedRoute != null && mSelectedRoute.btDevice.equals(device)) { + mSelectedRoute = null; } notifyBluetoothRoutesUpdated(); } diff --git a/services/core/java/com/android/server/media/SystemMediaRoute2Provider.java b/services/core/java/com/android/server/media/SystemMediaRoute2Provider.java index 18383c4314792..4b14e104a33f7 100644 --- a/services/core/java/com/android/server/media/SystemMediaRoute2Provider.java +++ b/services/core/java/com/android/server/media/SystemMediaRoute2Provider.java @@ -145,9 +145,9 @@ class SystemMediaRoute2Provider extends MediaRoute2Provider { @Override public void transferToRoute(String sessionId, String routeId) { if (TextUtils.equals(routeId, mDefaultRoute.getId())) { - mBtRouteProvider.clearActiveDevices(); + mBtRouteProvider.transferTo(null); } else { - mBtRouteProvider.setActiveDevice(routeId); + mBtRouteProvider.transferTo(routeId); } } @@ -196,7 +196,7 @@ class SystemMediaRoute2Provider extends MediaRoute2Provider { } private void initializeSessionInfo() { - mBluetoothRoutes = mBtRouteProvider.getBluetoothRoutes(); + mBluetoothRoutes = mBtRouteProvider.getAllBluetoothRoutes(); MediaRoute2ProviderInfo.Builder builder = new MediaRoute2ProviderInfo.Builder(); builder.addRoute(mDefaultRoute); @@ -251,7 +251,7 @@ class SystemMediaRoute2Provider extends MediaRoute2Provider { RoutingSessionInfo.Builder builder = new RoutingSessionInfo.Builder( SYSTEM_SESSION_ID, "" /* clientPackageName */) .setSystemSession(true); - String activeBtDeviceAddress = mBtRouteProvider.getActiveDeviceAddress(); + String activeBtDeviceAddress = mBtRouteProvider.getSelectedRouteId(); mSelectedRouteId = TextUtils.isEmpty(activeBtDeviceAddress) ? mDefaultRoute.getId() : activeBtDeviceAddress; builder.addSelectedRoute(mSelectedRouteId); @@ -311,7 +311,7 @@ class SystemMediaRoute2Provider extends MediaRoute2Provider { AudioManager.EXTRA_PREV_VOLUME_STREAM_VALUE, 0); if (newVolume != oldVolume) { - String activeBtDeviceAddress = mBtRouteProvider.getActiveDeviceAddress(); + String activeBtDeviceAddress = mBtRouteProvider.getSelectedRouteId(); if (!TextUtils.isEmpty(activeBtDeviceAddress)) { for (int i = mBluetoothRoutes.size() - 1; i >= 0; i--) { MediaRoute2Info route = mBluetoothRoutes.get(i);