diff options
| author | Sungsoo Lim <sungsoo@google.com> | 2015-11-17 16:53:26 +0900 |
|---|---|---|
| committer | Sungsoo Lim <sungsoo@google.com> | 2015-11-20 09:25:20 +0900 |
| commit | 4448a8e5ed20d230cb838042ff73b6409106cf80 (patch) | |
| tree | 652320d26a20a03c89e2566e3095389a6e0efb42 | |
| parent | 27f59164a0d383cb7df89858d457725e39c52ee7 (diff) | |
| download | android_frameworks_support-4448a8e5ed20d230cb838042ff73b6409106cf80.tar.gz android_frameworks_support-4448a8e5ed20d230cb838042ff73b6409106cf80.tar.bz2 android_frameworks_support-4448a8e5ed20d230cb838042ff73b6409106cf80.zip | |
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
5 files changed, 50 insertions, 26 deletions
diff --git a/v7/mediarouter/src/android/support/v7/app/MediaRouteButton.java b/v7/mediarouter/src/android/support/v7/app/MediaRouteButton.java index 9fdd56c360..c44e8d2b02 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 class MediaRouteButton extends View { } 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 8e5e2c6484..5856f38a85 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 class MediaRouteChooserDialog extends Dialog { } 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 b7ee5ab462..1f375a0881 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 class MediaRouteControllerDialog extends AlertDialog { } 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 6a6884634a..6ccfaa8e24 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 @@ class MediaRouteExpandCollapseButton extends ImageButton { 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 bd5ae91c41..33ab7bcc44 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 @@ final class MediaRouterThemeHelper { 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 @@ final class MediaRouterThemeHelper { ? 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 @@ final class MediaRouterThemeHelper { } 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 @@ final class MediaRouterThemeHelper { 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 @@ final class MediaRouterThemeHelper { && 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) { |
