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

Commit 03bf111b authored by Bishoy Gendy's avatar Bishoy Gendy
Browse files

Introduce waiting state for session create in SysMediaRoute2Provider

Bug: 307723189
Bug: 304816922
Test: Manually using the steps in b/304816922
Change-Id: Icc69463d11ce2320b8ce735e94c96076f3ab24ba
parent ddedd72a
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -34,3 +34,10 @@ flag {
    description: "Fallbacks to the default handling for volume adjustment when media session has fixed volume handling and its app is in the foreground and setting a media controller."
    bug: "293743975"
}

flag {
    namespace: "media_solutions"
    name: "enable_waiting_state_for_system_session_creation_request"
    description: "Introduces a waiting state for the session creation request and prevents it from early failing when the selectedRoute from the bluetooth stack doesn't match the pending request route id."
    bug: "307723189"
}
+56 −15
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ import android.util.Log;
import android.util.Slog;

import com.android.internal.annotations.GuardedBy;
import com.android.media.flags.Flags;

import java.util.List;
import java.util.Objects;
@@ -358,21 +359,8 @@ class SystemMediaRoute2Provider extends MediaRoute2Provider {

            RoutingSessionInfo newSessionInfo = builder.setProviderId(mUniqueId).build();

            if (mPendingSessionCreationRequest != null) {
                SessionCreationRequest sessionCreationRequest;
            synchronized (mRequestLock) {
                    sessionCreationRequest = mPendingSessionCreationRequest;
                    mPendingSessionCreationRequest = null;
                }
                if (sessionCreationRequest != null) {
                    if (TextUtils.equals(mSelectedRouteId, sessionCreationRequest.mRouteId)) {
                        mCallback.onSessionCreated(this,
                                sessionCreationRequest.mRequestId, newSessionInfo);
                    } else {
                        mCallback.onRequestFailed(this, sessionCreationRequest.mRequestId,
                                MediaRoute2ProviderService.REASON_UNKNOWN_ERROR);
                    }
                }
                reportPendingSessionRequestResultLockedIfNeeded(newSessionInfo);
            }

            if (Objects.equals(oldSessionInfo, newSessionInfo)) {
@@ -395,6 +383,59 @@ class SystemMediaRoute2Provider extends MediaRoute2Provider {
        }
    }

    @GuardedBy("mRequestLock")
    private void reportPendingSessionRequestResultLockedIfNeeded(
            RoutingSessionInfo newSessionInfo) {
        if (mPendingSessionCreationRequest == null) {
            // No pending request, nothing to report.
            return;
        }

        long pendingRequestId = mPendingSessionCreationRequest.mRequestId;
        if (TextUtils.equals(mSelectedRouteId, mPendingSessionCreationRequest.mRouteId)) {
            if (DEBUG) {
                Slog.w(
                        TAG,
                        "Session creation success to route "
                                + mPendingSessionCreationRequest.mRouteId);
            }
            mPendingSessionCreationRequest = null;
            mCallback.onSessionCreated(this, pendingRequestId, newSessionInfo);
        } else {
            boolean isRequestedRouteConnectedBtRoute = isRequestedRouteConnectedBtRoute();
            if (!Flags.enableWaitingStateForSystemSessionCreationRequest()
                    || !isRequestedRouteConnectedBtRoute) {
                if (DEBUG) {
                    Slog.w(
                            TAG,
                            "Session creation failed to route "
                                    + mPendingSessionCreationRequest.mRouteId);
                }
                mPendingSessionCreationRequest = null;
                mCallback.onRequestFailed(
                        this, pendingRequestId, MediaRoute2ProviderService.REASON_UNKNOWN_ERROR);
            } else if (DEBUG) {
                Slog.w(
                        TAG,
                        "Session creation waiting state to route "
                                + mPendingSessionCreationRequest.mRouteId);
            }
        }
    }

    @GuardedBy("mRequestLock")
    private boolean isRequestedRouteConnectedBtRoute() {
        // Using AllRoutes instead of TransferableRoutes as BT Stack sends an intermediate update
        // where two BT routes are active so the transferable routes list is empty.
        // See b/307723189 for context
        for (MediaRoute2Info btRoute : mBluetoothRouteController.getAllBluetoothRoutes()) {
            if (TextUtils.equals(btRoute.getId(), mPendingSessionCreationRequest.mRouteId)) {
                return true;
            }
        }
        return false;
    }

    void publishProviderState() {
        updateProviderState();
        notifyProviderState();