MediaRouter: Make getThemeColor() work properly

If an app uses its own style for MRCD, the primary color in the style
was not applied properly when creating theme context in the
constructor of MRCD.

Bug: 25731047
Change-Id: I9744c76373ac9b3d400f7be753a589aa520c78e6
diff --git a/v7/mediarouter/src/android/support/v7/app/MediaRouteButton.java b/v7/mediarouter/src/android/support/v7/app/MediaRouteButton.java
index 9fdd56c..c44e8d2 100644
--- a/v7/mediarouter/src/android/support/v7/app/MediaRouteButton.java
+++ b/v7/mediarouter/src/android/support/v7/app/MediaRouteButton.java
@@ -120,7 +120,8 @@
     }
 
     public MediaRouteButton(Context context, AttributeSet attrs, int defStyleAttr) {
-        super(MediaRouterThemeHelper.createThemedContext(context), attrs, defStyleAttr);
+        super(MediaRouterThemeHelper.createThemedContext(context, defStyleAttr), attrs,
+                defStyleAttr);
         context = getContext();
 
         mRouter = MediaRouter.getInstance(context);
diff --git a/v7/mediarouter/src/android/support/v7/app/MediaRouteChooserDialog.java b/v7/mediarouter/src/android/support/v7/app/MediaRouteChooserDialog.java
index 8e5e2c6..5856f38 100644
--- a/v7/mediarouter/src/android/support/v7/app/MediaRouteChooserDialog.java
+++ b/v7/mediarouter/src/android/support/v7/app/MediaRouteChooserDialog.java
@@ -78,7 +78,7 @@
     }
 
     public MediaRouteChooserDialog(Context context, int theme) {
-        super(MediaRouterThemeHelper.createThemedContext(context), theme);
+        super(MediaRouterThemeHelper.createThemedContext(context, theme), theme);
         context = getContext();
 
         mRouter = MediaRouter.getInstance(context);
diff --git a/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java b/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java
index b7ee5ab..1f375a0 100644
--- a/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java
+++ b/v7/mediarouter/src/android/support/v7/app/MediaRouteControllerDialog.java
@@ -151,18 +151,18 @@
     }
 
     public MediaRouteControllerDialog(Context context, int theme) {
-        super(MediaRouterThemeHelper.createThemedContext(context), theme);
+        super(MediaRouterThemeHelper.createThemedContext(context, theme), theme);
         mContext = getContext();
 
         mControllerCallback = new MediaControllerCallback();
-        mRouter = MediaRouter.getInstance(context);
+        mRouter = MediaRouter.getInstance(mContext);
         mCallback = new MediaRouterCallback();
         mRoute = mRouter.getSelectedRoute();
         setMediaSession(mRouter.getMediaSessionToken());
-        mVolumeGroupListPaddingTop = context.getResources().getDimensionPixelSize(
+        mVolumeGroupListPaddingTop = mContext.getResources().getDimensionPixelSize(
                 R.dimen.mr_controller_volume_group_list_padding_top);
         mAccessibilityManager =
-                (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
+                (AccessibilityManager) mContext.getSystemService(Context.ACCESSIBILITY_SERVICE);
     }
 
     /**
diff --git a/v7/mediarouter/src/android/support/v7/app/MediaRouteExpandCollapseButton.java b/v7/mediarouter/src/android/support/v7/app/MediaRouteExpandCollapseButton.java
index 6a68846..6ccfaa8 100644
--- a/v7/mediarouter/src/android/support/v7/app/MediaRouteExpandCollapseButton.java
+++ b/v7/mediarouter/src/android/support/v7/app/MediaRouteExpandCollapseButton.java
@@ -54,7 +54,8 @@
                 context, R.drawable.ic_collapse);
 
         ColorFilter filter = new PorterDuffColorFilter(
-                MediaRouterThemeHelper.getControllerColor(context), PorterDuff.Mode.SRC_IN);
+                MediaRouterThemeHelper.getControllerColor(context, defStyleAttr),
+                PorterDuff.Mode.SRC_IN);
         mExpandAnimationDrawable.setColorFilter(filter);
         mCollapseAnimationDrawable.setColorFilter(filter);
 
diff --git a/v7/mediarouter/src/android/support/v7/app/MediaRouterThemeHelper.java b/v7/mediarouter/src/android/support/v7/app/MediaRouterThemeHelper.java
index bd5ae91..33ab7bc 100644
--- a/v7/mediarouter/src/android/support/v7/app/MediaRouterThemeHelper.java
+++ b/v7/mediarouter/src/android/support/v7/app/MediaRouterThemeHelper.java
@@ -17,6 +17,7 @@
 package android.support.v7.app;
 
 import android.content.Context;
+import android.content.res.TypedArray;
 import android.graphics.Color;
 import android.support.annotation.IntDef;
 import android.support.v4.graphics.ColorUtils;
@@ -41,22 +42,34 @@
     private MediaRouterThemeHelper() {
     }
 
-    public static Context createThemedContext(Context context) {
-        int style;
+    /**
+     * Creates a themed context based on the explicit style resource or the parent context's default
+     * theme.
+     * <p>
+     * The theme which will be applied on top of the parent {@code context}'s theme is determined
+     * by the primary color defined in the given {@code style}, or in the parent {@code context}.
+     *
+     * @param context the parent context
+     * @param style the resource ID of the style against which to inflate this context, or
+     *              {@code 0} to use the parent {@code context}'s default theme.
+     * @return The themed context.
+     */
+    public static Context createThemedContext(Context context, int style) {
+        int theme;
         if (isLightTheme(context)) {
-            if (getControllerColor(context) == COLOR_DARK_ON_LIGHT_BACKGROUND) {
-                style = R.style.Theme_MediaRouter_Light;
+            if (getControllerColor(context, style) == COLOR_DARK_ON_LIGHT_BACKGROUND) {
+                theme = R.style.Theme_MediaRouter_Light;
             } else {
-                style = R.style.Theme_MediaRouter_Light_DarkControlPanel;
+                theme = R.style.Theme_MediaRouter_Light_DarkControlPanel;
             }
         } else {
-            if (getControllerColor(context) == COLOR_DARK_ON_LIGHT_BACKGROUND) {
-                style = R.style.Theme_MediaRouter_LightControlPanel;
+            if (getControllerColor(context, style) == COLOR_DARK_ON_LIGHT_BACKGROUND) {
+                theme = R.style.Theme_MediaRouter_LightControlPanel;
             } else {
-                style = R.style.Theme_MediaRouter;
+                theme = R.style.Theme_MediaRouter;
             }
         }
-        return new ContextThemeWrapper(context, style);
+        return new ContextThemeWrapper(context, theme);
     }
 
     public static int getThemeResource(Context context, int attr) {
@@ -70,8 +83,8 @@
                 ? value.getFloat() : 0.5f;
     }
 
-    public static @ControllerColorType int getControllerColor(Context context) {
-        int primaryColor = getThemeColor(context, R.attr.colorPrimary);
+    public static @ControllerColorType int getControllerColor(Context context, int style) {
+        int primaryColor = getThemeColor(context, style, R.attr.colorPrimary);
         if (ColorUtils.calculateContrast(COLOR_WHITE_ON_DARK_BACKGROUND, primaryColor)
                 >= MIN_CONTRAST) {
             return COLOR_WHITE_ON_DARK_BACKGROUND;
@@ -80,21 +93,21 @@
     }
 
     public static int getButtonTextColor(Context context) {
-        int primaryColor = getThemeColor(context, R.attr.colorPrimary);
-        int backgroundColor = getThemeColor(context, android.R.attr.colorBackground);
+        int primaryColor = getThemeColor(context, 0, R.attr.colorPrimary);
+        int backgroundColor = getThemeColor(context, 0, android.R.attr.colorBackground);
 
         if (ColorUtils.calculateContrast(primaryColor, backgroundColor) < MIN_CONTRAST) {
             // Default to colorAccent if the contrast ratio is low.
-            return getThemeColor(context, R.attr.colorAccent);
+            return getThemeColor(context, 0, R.attr.colorAccent);
         }
         return primaryColor;
     }
 
     public static void setMediaControlsBackgroundColor(
             Context context, View mainControls, View groupControls, boolean hasGroup) {
-        int primaryColor = getThemeColor(context, R.attr.colorPrimary);
-        int primaryDarkColor = getThemeColor(context, R.attr.colorPrimaryDark);
-        int controllerColor = getControllerColor(context);
+        int primaryColor = getThemeColor(context, 0, R.attr.colorPrimary);
+        int primaryDarkColor = getThemeColor(context, 0, R.attr.colorPrimaryDark);
+        int controllerColor = getControllerColor(context, 0);
         if (hasGroup && controllerColor == COLOR_DARK_ON_LIGHT_BACKGROUND
                 && ColorUtils.calculateContrast(controllerColor, primaryDarkColor) < MIN_CONTRAST) {
             // Instead of showing dark controls in a possibly dark (i.e. the primary dark), model
@@ -112,7 +125,7 @@
 
     public static void setVolumeSliderColor(
             Context context, MediaRouteVolumeSlider volumeSlider, View backgroundView) {
-        int controllerColor = getControllerColor(context);
+        int controllerColor = getControllerColor(context, 0);
         if (Color.alpha(controllerColor) != 0xFF) {
             // Composite with the background in order not to show the underlying progress bar
             // through the thumb.
@@ -128,7 +141,16 @@
                 && value.data != 0;
     }
 
-    private static int getThemeColor(Context context, int attr) {
+    private static int getThemeColor(Context context, int style, int attr) {
+        if (style != 0) {
+            int[] attrs = { attr };
+            TypedArray ta = context.obtainStyledAttributes(style, attrs);
+            int color = ta.getColor(0, 0);
+            ta.recycle();
+            if (color != 0) {
+                return color;
+            }
+        }
         TypedValue value = new TypedValue();
         context.getTheme().resolveAttribute(attr, value, true);
         if (value.resourceId != 0) {