MediaRouter: Enable MRM.selectRoute()

MediaRouter2 won't ignore session created by others.
(proviers or MediaRouterManager)

testRouterOnSessionCreated is also enabled.

Test: atest mediaroutertest

Change-Id: Ibeac3ae6b725c93c80f5a308d250574cdd21c9be
This commit is contained in:
Kyunglyul Hyun
2020-01-02 18:19:52 +09:00
parent 4003e43525
commit 138246e495
4 changed files with 72 additions and 65 deletions

View File

@@ -547,8 +547,6 @@ public class MediaRouter2 {
* {@link SessionCallback#onSessionCreationFailed}.
* <p>
* Pass {@code null} to sessionInfo for the failure case.
*
* TODO: What should router do when the session is created by manager?
*/
void createControllerOnHandler(@Nullable RouteSessionInfo sessionInfo, int requestId) {
SessionCreationRequest matchingRequest = null;
@@ -559,40 +557,44 @@ public class MediaRouter2 {
}
}
if (matchingRequest == null) {
Log.w(TAG, "Ignoring session creation result for unknown request."
+ " requestId=" + requestId + ", sessionInfo=" + sessionInfo);
return;
if (matchingRequest != null) {
mSessionCreationRequests.remove(matchingRequest);
MediaRoute2Info requestedRoute = matchingRequest.mRoute;
String requestedControlCategory = matchingRequest.mControlCategory;
if (sessionInfo == null) {
// TODO: We may need to distinguish between failure and rejection.
// One way can be introducing 'reason'.
notifySessionCreationFailed(requestedRoute, requestedControlCategory);
return;
} else if (!TextUtils.equals(requestedControlCategory,
sessionInfo.getControlCategory())) {
Log.w(TAG, "The session has different control category from what we requested. "
+ "(requested=" + requestedControlCategory
+ ", actual=" + sessionInfo.getControlCategory()
+ ")");
notifySessionCreationFailed(requestedRoute, requestedControlCategory);
return;
} else if (!sessionInfo.getSelectedRoutes().contains(requestedRoute.getId())) {
Log.w(TAG, "The session does not contain the requested route. "
+ "(requestedRouteId=" + requestedRoute.getId()
+ ", actualRoutes=" + sessionInfo.getSelectedRoutes()
+ ")");
notifySessionCreationFailed(requestedRoute, requestedControlCategory);
return;
} else if (!TextUtils.equals(requestedRoute.getProviderId(),
sessionInfo.getProviderId())) {
Log.w(TAG, "The session's provider ID does not match the requested route's. "
+ "(requested route's providerId=" + requestedRoute.getProviderId()
+ ", actual providerId=" + sessionInfo.getProviderId()
+ ")");
notifySessionCreationFailed(requestedRoute, requestedControlCategory);
return;
}
}
mSessionCreationRequests.remove(matchingRequest);
MediaRoute2Info requestedRoute = matchingRequest.mRoute;
String requestedControlCategory = matchingRequest.mControlCategory;
if (sessionInfo == null) {
// TODO: We may need to distinguish between failure and rejection.
// One way can be introducing 'reason'.
notifySessionCreationFailed(requestedRoute, requestedControlCategory);
} else if (!TextUtils.equals(requestedControlCategory, sessionInfo.getControlCategory())) {
Log.w(TAG, "The session has different control category from what we requested. "
+ "(requested=" + requestedControlCategory
+ ", actual=" + sessionInfo.getControlCategory()
+ ")");
notifySessionCreationFailed(requestedRoute, requestedControlCategory);
} else if (!sessionInfo.getSelectedRoutes().contains(requestedRoute.getId())) {
Log.w(TAG, "The session does not contain the requested route. "
+ "(requestedRouteId=" + requestedRoute.getId()
+ ", actualRoutes=" + sessionInfo.getSelectedRoutes()
+ ")");
notifySessionCreationFailed(requestedRoute, requestedControlCategory);
} else if (!TextUtils.equals(requestedRoute.getProviderId(), sessionInfo.getProviderId())) {
Log.w(TAG, "The session's provider ID does not match the requested route's. "
+ "(requested route's providerId=" + requestedRoute.getProviderId()
+ ", actual providerId=" + sessionInfo.getProviderId()
+ ")");
notifySessionCreationFailed(requestedRoute, requestedControlCategory);
} else {
if (sessionInfo != null) {
RouteSessionController controller = new RouteSessionController(sessionInfo);
mSessionControllers.put(controller.getUniqueSessionId(), controller);
notifySessionCreated(controller);

View File

@@ -29,6 +29,7 @@ import android.content.Intent;
import android.media.MediaRoute2Info;
import android.media.MediaRouter2;
import android.media.MediaRouter2.RouteCallback;
import android.media.MediaRouter2.SessionCallback;
import android.media.MediaRouter2Manager;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
@@ -101,8 +102,8 @@ public class MediaRouterManagerTest {
private String mPackageName;
private final List<MediaRouter2Manager.Callback> mManagerCallbacks = new ArrayList<>();
private final List<RouteCallback> mRouteCallbacks =
new ArrayList<>();
private final List<RouteCallback> mRouteCallbacks = new ArrayList<>();
private final List<SessionCallback> mSessionCallbacks = new ArrayList<>();
public static final List<String> CATEGORIES_ALL = new ArrayList();
public static final List<String> CATEGORIES_SPECIAL = new ArrayList();
@@ -205,27 +206,26 @@ public class MediaRouterManagerTest {
}
/**
* Tests if MR2.Callback.onRouteSelected is called when a route is selected from MR2Manager.
*
* TODO: Change this test so that this test check whether the route is added in a session.
* Until then, temporailiy marking @Ignore
* Tests if MR2.SessionCallback.onSessionCreated is called
* when a route is selected from MR2Manager.
*/
@Test
@Ignore
public void testRouterOnRouteSelected() throws Exception {
public void testRouterOnSessionCreated() throws Exception {
Map<String, MediaRoute2Info> routes = waitAndGetRoutesWithManager(CATEGORIES_ALL);
CountDownLatch latch = new CountDownLatch(1);
addManagerCallback(new MediaRouter2Manager.Callback());
// addRouterCallback(new RouteDiscoveryCallback() {
// @Override
// public void onRouteSelected(MediaRoute2Info route, int reason, Bundle controlHints) {
// if (route != null && TextUtils.equals(route.getId(), ROUTE_ID1)) {
// latch.countDown();
// }
// }
// });
//TODO: remove this when it's not necessary.
addRouterCallback(new MediaRouter2.RouteCallback());
addSessionCallback(new SessionCallback() {
@Override
public void onSessionCreated(MediaRouter2.RouteSessionController controller) {
if (createRouteMap(controller.getSelectedRoutes()).containsKey(ROUTE_ID1)) {
latch.countDown();
}
}
});
MediaRoute2Info routeToSelect = routes.get(ROUTE_ID1);
assertNotNull(routeToSelect);
@@ -446,6 +446,11 @@ public class MediaRouterManagerTest {
mRouter2.registerRouteCallback(mExecutor, routeCallback);
}
private void addSessionCallback(SessionCallback sessionCallback) {
mSessionCallbacks.add(sessionCallback);
mRouter2.registerSessionCallback(mExecutor, sessionCallback);
}
private void clearCallbacks() {
for (MediaRouter2Manager.Callback callback : mManagerCallbacks) {
mManager.unregisterCallback(callback);
@@ -456,5 +461,10 @@ public class MediaRouterManagerTest {
mRouter2.unregisterRouteCallback(routeCallback);
}
mRouteCallbacks.clear();
for (SessionCallback sessionCallback : mSessionCallbacks) {
mRouter2.unregisterSessionCallback(sessionCallback);
}
mSessionCallbacks.clear();
}
}

View File

@@ -286,6 +286,11 @@ final class MediaRoute2ProviderProxy extends MediaRoute2Provider implements Serv
if (mActiveConnection != connection) {
return;
}
if (sessionInfo != null) {
sessionInfo = new RouteSessionInfo.Builder(sessionInfo)
.setProviderId(getUniqueId())
.build();
}
mCallback.onSessionCreated(this, sessionInfo, requestId);
}

View File

@@ -1071,12 +1071,6 @@ class MediaRouter2ServiceImpl {
return;
}
//TODO: remove this when we are sure that request id is properly implemented.
if (matchingRequest.mClientRecord.mClientId != toClientId(requestId)) {
Slog.w(TAG, "Client id is changed. This shouldn't happen.");
return;
}
mSessionCreationRequests.remove(matchingRequest);
if (sessionInfo == null) {
@@ -1086,21 +1080,17 @@ class MediaRouter2ServiceImpl {
return;
}
RouteSessionInfo sessionInfoWithProviderId = new RouteSessionInfo.Builder(sessionInfo)
.setProviderId(provider.getUniqueId())
.build();
String originalRouteId = matchingRequest.mRoute.getId();
String originalCategory = matchingRequest.mControlCategory;
Client2Record client2Record = matchingRequest.mClientRecord;
if (!sessionInfoWithProviderId.getSelectedRoutes().contains(originalRouteId)
if (!sessionInfo.getSelectedRoutes().contains(originalRouteId)
|| !TextUtils.equals(originalCategory,
sessionInfoWithProviderId.getControlCategory())) {
sessionInfo.getControlCategory())) {
Slog.w(TAG, "Created session doesn't match the original request."
+ " originalRouteId=" + originalRouteId
+ ", originalCategory=" + originalCategory + ", requestId=" + requestId
+ ", sessionInfo=" + sessionInfoWithProviderId);
+ ", sessionInfo=" + sessionInfo);
notifySessionCreationFailed(matchingRequest.mClientRecord,
toClientRequestId(requestId));
return;
@@ -1108,8 +1098,8 @@ class MediaRouter2ServiceImpl {
// Succeeded
notifySessionCreated(matchingRequest.mClientRecord,
sessionInfoWithProviderId, toClientRequestId(requestId));
mSessionToClientMap.put(sessionInfoWithProviderId.getUniqueSessionId(), client2Record);
sessionInfo, toClientRequestId(requestId));
mSessionToClientMap.put(sessionInfo.getUniqueSessionId(), client2Record);
// TODO: Tell managers for the session creation
}