MediaRouter: make volume control on child routes work

Bug: 23118003
Change-Id: Ib1ce2fb2b7a5eee5a835b717b0d679837e2dbf88
diff --git a/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java b/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java
index 5fc8458..a444ee1 100644
--- a/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java
+++ b/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java
@@ -538,7 +538,6 @@
             public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                 if (fromUser) {
                     int position = (int) seekBar.getTag();
-                    // TODO: Verify
                     getGroup().getRouteAt(position).requestSetVolume(progress);
                 }
             }
diff --git a/v7/mediarouter/src/android/support/v7/media/MediaRouter.java b/v7/mediarouter/src/android/support/v7/media/MediaRouter.java
index 614dade..e1ebd94 100644
--- a/v7/mediarouter/src/android/support/v7/media/MediaRouter.java
+++ b/v7/mediarouter/src/android/support/v7/media/MediaRouter.java
@@ -39,6 +39,7 @@
 import android.support.v4.media.session.MediaSessionCompat;
 import android.support.v4.util.Pair;
 import android.support.v7.media.MediaRouteProvider.ProviderMetadata;
+import android.support.v7.media.MediaRouteProvider.RouteController;
 import android.util.Log;
 import android.view.Display;
 
@@ -1782,7 +1783,8 @@
         private RegisteredMediaRouteProviderWatcher mRegisteredProviderWatcher;
         private RouteInfo mDefaultRoute;
         private RouteInfo mSelectedRoute;
-        private MediaRouteProvider.RouteController mSelectedRouteController;
+        private RouteController mSelectedRouteController;
+        private Map<String, RouteController> mChildControllers;
         private MediaRouteDiscoveryRequest mDiscoveryRequest;
         private MediaSessionRecord mMediaSession;
         private MediaSessionCompat mRccMediaSession;
@@ -1873,6 +1875,11 @@
         public void requestSetVolume(RouteInfo route, int volume) {
             if (route == mSelectedRoute && mSelectedRouteController != null) {
                 mSelectedRouteController.onSetVolume(volume);
+            } else if (mChildControllers != null) {
+                RouteController controller = mChildControllers.get(route.mDescriptorId);
+                if (controller != null) {
+                    controller.onSetVolume(volume);
+                }
             }
         }
 
@@ -2355,6 +2362,13 @@
                         mSelectedRouteController.onRelease();
                         mSelectedRouteController = null;
                     }
+                    if (mChildControllers != null) {
+                        for (RouteController ctrl : mChildControllers.values()) {
+                            ctrl.onUnselect();
+                            ctrl.onRelease();
+                        }
+                        mChildControllers = null;
+                    }
                 }
 
                 mSelectedRoute = route;
@@ -2369,6 +2383,17 @@
                         Log.d(TAG, "Route selected: " + mSelectedRoute);
                     }
                     mCallbackHandler.post(CallbackHandler.MSG_ROUTE_SELECTED, mSelectedRoute);
+
+                    if (mSelectedRoute instanceof RouteGroup) {
+                        mChildControllers = new HashMap<>();
+                        RouteGroup group = (RouteGroup) mSelectedRoute;
+                        for (RouteInfo child : group.getRoutes()) {
+                            RouteController controller = child.getProviderInstance()
+                                    .onCreateRouteController(child.mDescriptorId);
+                            controller.onSelect();
+                            mChildControllers.put(child.mDescriptorId, controller);
+                        }
+                    }
                 }
 
                 updatePlaybackInfoFromSelectedRoute();