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

Commit 34cab6d6 authored by Chung Tang's avatar Chung Tang Committed by Android (Google) Code Review
Browse files

Merge "[OutputSwitcher] Fix AudioManagerRouteController deadlock when calling...

Merge "[OutputSwitcher] Fix AudioManagerRouteController deadlock when calling mAudioManager" into main
parents a6c62b35 6cc1477f
Loading
Loading
Loading
Loading
+16 −29
Original line number Diff line number Diff line
@@ -289,23 +289,31 @@ import java.util.concurrent.CopyOnWriteArrayList;

    @Override
    @NonNull
    public synchronized List<MediaRoute2Info> getSelectableRoutes() {
        if (currentOutputIsBLEBroadcast()) {
    public List<MediaRoute2Info> getSelectableRoutes() {
        List<MediaRoute2Info> selectedRoutes;
        List<MediaRoute2Info> availableRoutes;
        synchronized (this) {
            selectedRoutes = getSelectedRoutes();
            availableRoutes = getAvailableRoutes();
        }
        if (!mBluetoothRouteController.isLEAudioBroadcastSupported()
                || currentOutputIsBLEBroadcast()
                || selectedRoutes.getFirst().getType() != MediaRoute2Info.TYPE_BLE_HEADSET
                || selectedRoutes.size() >= mBroadcastingMaxSinks) {
            return Collections.emptyList();
        } else {
            return getAvailableRoutes().stream()
            return availableRoutes.stream()
                    .filter(
                            route ->
                                    !maxBroadcastDeviceReached()
                                            && !getSelectedRoutes().contains(route)
                                            && isRouteSelectable(route))
                                    !selectedRoutes.contains(route)
                                            && route.getType() == MediaRoute2Info.TYPE_BLE_HEADSET)
                    .toList();
        }
    }

    @Override
    @NonNull
    public synchronized List<MediaRoute2Info> getDeselectableRoutes() {
    public List<MediaRoute2Info> getDeselectableRoutes() {
        if (currentOutputIsBLEBroadcast()) {
            // When broadcasting, all selected routes are deselectable.
            return getSelectedRoutes();
@@ -388,7 +396,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
    }

    @Override
    public synchronized void deselectRoute(long requestId, @NonNull String routeId) {
    public void deselectRoute(long requestId, @NonNull String routeId) {
        if (!currentOutputIsBLEBroadcast()) {
            // Unexpected result.
            Slog.e(TAG, "Unable to deselect route: " + routeId + " ,requestId: " + requestId);
@@ -804,27 +812,6 @@ import java.util.concurrent.CopyOnWriteArrayList;
                && devicesForMedia.getFirst().getType() == AudioDeviceInfo.TYPE_BLE_BROADCAST;
    }

    /**
     * @return true if maximum number of broadcast devices reached
     */
    private boolean maxBroadcastDeviceReached() {
        return getSelectedRoutes().size() >= mBroadcastingMaxSinks;
    }

    /**
     * Checks if a given route should be displayed as selectable route. This function checks: 1) if
     * LE Audio broadcast is support for the device, 2) if the current selected route is a BLE
     * headset and 3) the target route is a BLE headset.
     *
     * @param targetRoute the route for checking
     * @return true if the target route should be displayed as a selectable route
     */
    private synchronized boolean isRouteSelectable(MediaRoute2Info targetRoute) {
        return mBluetoothRouteController.isLEAudioBroadcastSupported()
                && getSelectedRoutes().getFirst().getType() == MediaRoute2Info.TYPE_BLE_HEADSET
                && targetRoute.getType() == MediaRoute2Info.TYPE_BLE_HEADSET;
    }

    /**
     * Holds a {@link MediaRoute2Info} and associated information that we don't want to put in the
     * {@link MediaRoute2Info} class because it's solely necessary for the implementation of this