diff options
| author | 2024-05-09 16:54:24 +0000 | |
|---|---|---|
| committer | 2024-05-09 16:54:24 +0000 | |
| commit | c0f4718d00bc636de0bb08041d3e185fcef06db6 (patch) | |
| tree | 27fc5bd1b24aacf216d3f856c8c50ef572fa7694 | |
| parent | 9270a9fb6674d24870f4b7b4bbe9dcf7f91e3030 (diff) | |
| parent | 122c718f94194fa938be19b1936ff142be51018c (diff) | |
Merge changes from topic "transfer_initiator_fix" into main
* changes:
Fix output switcher initiator data
Fix comparison of route id with original route id
3 files changed, 92 insertions, 95 deletions
diff --git a/media/java/android/media/MediaRouter2.java b/media/java/android/media/MediaRouter2.java index b2838c809854..7ddf11e41a41 100644 --- a/media/java/android/media/MediaRouter2.java +++ b/media/java/android/media/MediaRouter2.java @@ -50,6 +50,7 @@ import android.util.Log; import android.util.SparseArray; import com.android.internal.annotations.GuardedBy; +import com.android.media.flags.Flags; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -984,37 +985,7 @@ public final class MediaRouter2 { @SystemApi @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void transfer(@NonNull RoutingController controller, @NonNull MediaRoute2Info route) { - mImpl.transfer( - controller.getRoutingSessionInfo(), - route, - Process.myUserHandle(), - mContext.getPackageName()); - } - - /** - * Transfers the media of a routing controller to the given route. - * - * <p>This will be no-op for non-system media routers. - * - * @param controller a routing controller controlling media routing. - * @param route the route you want to transfer the media to. - * @param transferInitiatorUserHandle the user handle of the app that initiated the transfer - * request. - * @param transferInitiatorPackageName the package name of the app that initiated the transfer. - * This value is used with the user handle to populate {@link - * RoutingController#wasTransferInitiatedBySelf()}. - * @hide - */ - public void transfer( - @NonNull RoutingController controller, - @NonNull MediaRoute2Info route, - @NonNull UserHandle transferInitiatorUserHandle, - @NonNull String transferInitiatorPackageName) { - mImpl.transfer( - controller.getRoutingSessionInfo(), - route, - transferInitiatorUserHandle, - transferInitiatorPackageName); + mImpl.transfer(controller.getRoutingSessionInfo(), route); } void requestCreateController( @@ -1913,13 +1884,7 @@ public final class MediaRouter2 { */ @FlaggedApi(FLAG_ENABLE_BUILT_IN_SPEAKER_ROUTE_SUITABILITY_STATUSES) public boolean wasTransferInitiatedBySelf() { - RoutingSessionInfo sessionInfo = getRoutingSessionInfo(); - - UserHandle transferInitiatorUserHandle = sessionInfo.getTransferInitiatorUserHandle(); - String transferInitiatorPackageName = sessionInfo.getTransferInitiatorPackageName(); - - return Objects.equals(Process.myUserHandle(), transferInitiatorUserHandle) - && Objects.equals(mContext.getPackageName(), transferInitiatorPackageName); + return mImpl.wasTransferredBySelf(getRoutingSessionInfo()); } /** @@ -2082,12 +2047,28 @@ public final class MediaRouter2 { Objects.requireNonNull(route, "route must not be null"); synchronized (mControllerLock) { if (isReleased()) { - Log.w(TAG, "transferToRoute: Called on released controller. Ignoring."); + Log.w( + TAG, + "tryTransferWithinProvider: Called on released controller. Ignoring."); return true; } - if (!mSessionInfo.getTransferableRoutes().contains(route.getId())) { - Log.w(TAG, "Ignoring transferring to a non-transferable route=" + route); + // If this call is trying to transfer to a selected system route, we let them + // through as a provider driven transfer in order to update the transfer reason and + // initiator data. + boolean isSystemRouteReselection = + Flags.enableBuiltInSpeakerRouteSuitabilityStatuses() + && mSessionInfo.isSystemSession() + && route.isSystemRoute() + && mSessionInfo.getSelectedRoutes().contains(route.getId()); + if (!isSystemRouteReselection + && !mSessionInfo.getTransferableRoutes().contains(route.getId())) { + Log.i( + TAG, + "Transferring to a non-transferable route=" + + route + + " session= " + + mSessionInfo.getId()); return false; } } @@ -2498,11 +2479,7 @@ public final class MediaRouter2 { void stop(); - void transfer( - @NonNull RoutingSessionInfo sessionInfo, - @NonNull MediaRoute2Info route, - @NonNull UserHandle transferInitiatorUserHandle, - @NonNull String transferInitiatorPackageName); + void transfer(@NonNull RoutingSessionInfo sessionInfo, @NonNull MediaRoute2Info route); List<RoutingController> getControllers(); @@ -2523,6 +2500,11 @@ public final class MediaRouter2 { boolean shouldNotifyStop, RoutingController controller); + /** + * Returns the value of {@link RoutingController#wasTransferInitiatedBySelf()} for the app + * associated with this router. + */ + boolean wasTransferredBySelf(RoutingSessionInfo sessionInfo); } /** @@ -2723,7 +2705,7 @@ public final class MediaRouter2 { List<RoutingSessionInfo> sessionInfos = getRoutingSessions(); RoutingSessionInfo targetSession = sessionInfos.get(sessionInfos.size() - 1); - transfer(targetSession, route, mClientUser, mContext.getPackageName()); + transfer(targetSession, route); } @Override @@ -2746,24 +2728,15 @@ public final class MediaRouter2 { * * @param sessionInfo The {@link RoutingSessionInfo routing session} to transfer. * @param route The {@link MediaRoute2Info route} to transfer to. - * @param transferInitiatorUserHandle The user handle of the app that initiated the - * transfer. - * @param transferInitiatorPackageName The package name if of the app that initiated the - * transfer. * @see #transferToRoute(RoutingSessionInfo, MediaRoute2Info, UserHandle, String) * @see #requestCreateSession(RoutingSessionInfo, MediaRoute2Info) */ @Override @SuppressWarnings("AndroidFrameworkRequiresPermission") public void transfer( - @NonNull RoutingSessionInfo sessionInfo, - @NonNull MediaRoute2Info route, - @NonNull UserHandle transferInitiatorUserHandle, - @NonNull String transferInitiatorPackageName) { + @NonNull RoutingSessionInfo sessionInfo, @NonNull MediaRoute2Info route) { Objects.requireNonNull(sessionInfo, "sessionInfo must not be null"); Objects.requireNonNull(route, "route must not be null"); - Objects.requireNonNull(transferInitiatorUserHandle); - Objects.requireNonNull(transferInitiatorPackageName); Log.v( TAG, @@ -2780,15 +2753,19 @@ public final class MediaRouter2 { return; } - if (sessionInfo.getTransferableRoutes().contains(route.getId())) { - transferToRoute( - sessionInfo, - route, - transferInitiatorUserHandle, - transferInitiatorPackageName); + // If this call is trying to transfer to a selected system route, we let them + // through as a provider driven transfer in order to update the transfer reason and + // initiator data. + boolean isSystemRouteReselection = + Flags.enableBuiltInSpeakerRouteSuitabilityStatuses() + && sessionInfo.isSystemSession() + && route.isSystemRoute() + && sessionInfo.getSelectedRoutes().contains(route.getId()); + if (sessionInfo.getTransferableRoutes().contains(route.getId()) + || isSystemRouteReselection) { + transferToRoute(sessionInfo, route, mClientUser, mClientPackageName); } else { - requestCreateSession(sessionInfo, route, transferInitiatorUserHandle, - transferInitiatorPackageName); + requestCreateSession(sessionInfo, route, mClientUser, mClientPackageName); } } @@ -3043,6 +3020,14 @@ public final class MediaRouter2 { releaseSession(controller.getRoutingSessionInfo()); } + @Override + public boolean wasTransferredBySelf(RoutingSessionInfo sessionInfo) { + UserHandle transferInitiatorUserHandle = sessionInfo.getTransferInitiatorUserHandle(); + String transferInitiatorPackageName = sessionInfo.getTransferInitiatorPackageName(); + return Objects.equals(mClientUser, transferInitiatorUserHandle) + && Objects.equals(mClientPackageName, transferInitiatorPackageName); + } + /** * Retrieves the system session info for the given package. * @@ -3619,10 +3604,7 @@ public final class MediaRouter2 { */ @Override public void transfer( - @NonNull RoutingSessionInfo sessionInfo, - @NonNull MediaRoute2Info route, - @NonNull UserHandle transferInitiatorUserHandle, - @NonNull String transferInitiatorPackageName) { + @NonNull RoutingSessionInfo sessionInfo, @NonNull MediaRoute2Info route) { // Do nothing. } @@ -3741,6 +3723,14 @@ public final class MediaRouter2 { } } + @Override + public boolean wasTransferredBySelf(RoutingSessionInfo sessionInfo) { + UserHandle transferInitiatorUserHandle = sessionInfo.getTransferInitiatorUserHandle(); + String transferInitiatorPackageName = sessionInfo.getTransferInitiatorPackageName(); + return Objects.equals(Process.myUserHandle(), transferInitiatorUserHandle) + && Objects.equals(mContext.getPackageName(), transferInitiatorPackageName); + } + @GuardedBy("mLock") private void registerRouterStubIfNeededLocked() throws RemoteException { if (mStub == null) { diff --git a/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java b/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java index 869b89a6670c..73647dbbe978 100644 --- a/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java +++ b/services/core/java/com/android/server/media/MediaRouter2ServiceImpl.java @@ -1305,22 +1305,20 @@ class MediaRouter2ServiceImpl { route.getId(), requestId)); + UserHandler userHandler = routerRecord.mUserRecord.mHandler; if (managerRequestId != MediaRoute2ProviderService.REQUEST_ID_NONE) { - ManagerRecord manager = routerRecord.mUserRecord.mHandler.findManagerWithId( - toRequesterId(managerRequestId)); + ManagerRecord manager = userHandler.findManagerWithId(toRequesterId(managerRequestId)); if (manager == null || manager.mLastSessionCreationRequest == null) { Slog.w(TAG, "requestCreateSessionWithRouter2Locked: " + "Ignoring unknown request."); - routerRecord.mUserRecord.mHandler.notifySessionCreationFailedToRouter( - routerRecord, requestId); + userHandler.notifySessionCreationFailedToRouter(routerRecord, requestId); return; } if (!TextUtils.equals(manager.mLastSessionCreationRequest.mOldSession.getId(), oldSession.getId())) { Slog.w(TAG, "requestCreateSessionWithRouter2Locked: " + "Ignoring unmatched routing session."); - routerRecord.mUserRecord.mHandler.notifySessionCreationFailedToRouter( - routerRecord, requestId); + userHandler.notifySessionCreationFailedToRouter(routerRecord, requestId); return; } if (!TextUtils.equals(manager.mLastSessionCreationRequest.mRoute.getId(), @@ -1333,29 +1331,28 @@ class MediaRouter2ServiceImpl { } else { Slog.w(TAG, "requestCreateSessionWithRouter2Locked: " + "Ignoring unmatched route."); - routerRecord.mUserRecord.mHandler.notifySessionCreationFailedToRouter( - routerRecord, requestId); + userHandler.notifySessionCreationFailedToRouter(routerRecord, requestId); return; } } manager.mLastSessionCreationRequest = null; } else { + String defaultRouteId = userHandler.mSystemProvider.getDefaultRoute().getId(); if (route.isSystemRoute() && !routerRecord.hasSystemRoutingPermission() - && !TextUtils.equals(route.getId(), MediaRoute2Info.ROUTE_ID_DEFAULT)) { + && !TextUtils.equals(route.getId(), defaultRouteId)) { Slog.w(TAG, "MODIFY_AUDIO_ROUTING permission is required to transfer to" + route); - routerRecord.mUserRecord.mHandler.notifySessionCreationFailedToRouter( - routerRecord, requestId); + userHandler.notifySessionCreationFailedToRouter(routerRecord, requestId); return; } } long uniqueRequestId = toUniqueRequestId(routerRecord.mRouterId, requestId); - routerRecord.mUserRecord.mHandler.sendMessage( + userHandler.sendMessage( obtainMessage( UserHandler::requestCreateSessionWithRouter2OnHandler, - routerRecord.mUserRecord.mHandler, + userHandler, uniqueRequestId, managerRequestId, transferInitiatorUserHandle, @@ -1429,18 +1426,22 @@ class MediaRouter2ServiceImpl { "transferToRouteWithRouter2 | router: %s(id: %d), route: %s", routerRecord.mPackageName, routerRecord.mRouterId, route.getId())); + UserHandler userHandler = routerRecord.mUserRecord.mHandler; + String defaultRouteId = userHandler.mSystemProvider.getDefaultRoute().getId(); if (route.isSystemRoute() && !routerRecord.hasSystemRoutingPermission() - && !TextUtils.equals(route.getId(), MediaRoute2Info.ROUTE_ID_DEFAULT)) { - routerRecord.mUserRecord.mHandler.sendMessage( - obtainMessage(UserHandler::notifySessionCreationFailedToRouter, - routerRecord.mUserRecord.mHandler, - routerRecord, toOriginalRequestId(DUMMY_REQUEST_ID))); + && !TextUtils.equals(route.getId(), defaultRouteId)) { + userHandler.sendMessage( + obtainMessage( + UserHandler::notifySessionCreationFailedToRouter, + userHandler, + routerRecord, + toOriginalRequestId(DUMMY_REQUEST_ID))); } else { - routerRecord.mUserRecord.mHandler.sendMessage( + userHandler.sendMessage( obtainMessage( UserHandler::transferToRouteOnHandler, - routerRecord.mUserRecord.mHandler, + userHandler, DUMMY_REQUEST_ID, transferInitiatorUserHandle, routerRecord.mPackageName, diff --git a/services/core/java/com/android/server/media/SystemMediaRoute2Provider.java b/services/core/java/com/android/server/media/SystemMediaRoute2Provider.java index c105b9c37026..6ce3ab4b2d65 100644 --- a/services/core/java/com/android/server/media/SystemMediaRoute2Provider.java +++ b/services/core/java/com/android/server/media/SystemMediaRoute2Provider.java @@ -232,10 +232,16 @@ class SystemMediaRoute2Provider extends MediaRoute2Provider { String sessionId, String routeId, @RoutingSessionInfo.TransferReason int transferReason) { + String selectedDeviceRouteId = mDeviceRouteController.getSelectedRoute().getId(); if (TextUtils.equals(routeId, MediaRoute2Info.ROUTE_ID_DEFAULT)) { - // The currently selected route is the default route. - Log.w(TAG, "Ignoring transfer to " + MediaRoute2Info.ROUTE_ID_DEFAULT); - return; + if (Flags.enableBuiltInSpeakerRouteSuitabilityStatuses()) { + // Transfer to the default route (which is the selected route). We replace the id to + // be the selected route id so that the transfer reason gets updated. + routeId = selectedDeviceRouteId; + } else { + Log.w(TAG, "Ignoring transfer to " + MediaRoute2Info.ROUTE_ID_DEFAULT); + return; + } } if (Flags.enableBuiltInSpeakerRouteSuitabilityStatuses()) { @@ -250,11 +256,11 @@ class SystemMediaRoute2Provider extends MediaRoute2Provider { } } - MediaRoute2Info selectedDeviceRoute = mDeviceRouteController.getSelectedRoute(); + String finalRouteId = routeId; // Make a final copy to use it in the lambda. boolean isAvailableDeviceRoute = mDeviceRouteController.getAvailableRoutes().stream() - .anyMatch(it -> it.getId().equals(routeId)); - boolean isSelectedDeviceRoute = TextUtils.equals(routeId, selectedDeviceRoute.getId()); + .anyMatch(it -> it.getId().equals(finalRouteId)); + boolean isSelectedDeviceRoute = TextUtils.equals(routeId, selectedDeviceRouteId); if (isSelectedDeviceRoute || isAvailableDeviceRoute) { // The requested route is managed by the device route controller. Note that the selected |