1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.support.v7.app; 18 19 import android.content.Context; 20 import android.content.res.TypedArray; 21 import android.graphics.Color; 22 import android.support.annotation.IntDef; 23 import android.support.v4.graphics.ColorUtils; 24 import android.support.v7.mediarouter.R; 25 import android.util.TypedValue; 26 import android.view.ContextThemeWrapper; 27 import android.view.View; 28 29 import java.lang.annotation.Retention; 30 import java.lang.annotation.RetentionPolicy; 31 32 final class MediaRouterThemeHelper { 33 private static final float MIN_CONTRAST = 3.0f; 34 35 @IntDef({COLOR_DARK_ON_LIGHT_BACKGROUND, COLOR_WHITE_ON_DARK_BACKGROUND}) 36 @Retention(RetentionPolicy.SOURCE) 37 private @interface ControllerColorType {} 38 39 private static final int COLOR_DARK_ON_LIGHT_BACKGROUND = 0xDE000000; /* Opacity of 87% */ 40 private static final int COLOR_WHITE_ON_DARK_BACKGROUND = Color.WHITE; 41 MediaRouterThemeHelper()42 private MediaRouterThemeHelper() { 43 } 44 45 /** 46 * Creates a themed context based on the explicit style resource or the parent context's default 47 * theme. 48 * <p> 49 * The theme which will be applied on top of the parent {@code context}'s theme is determined 50 * by the primary color defined in the given {@code style}, or in the parent {@code context}. 51 * 52 * @param context the parent context 53 * @param style the resource ID of the style against which to inflate this context, or 54 * {@code 0} to use the parent {@code context}'s default theme. 55 * @return The themed context. 56 */ createThemedContext(Context context, int style)57 public static Context createThemedContext(Context context, int style) { 58 int theme; 59 if (isLightTheme(context)) { 60 if (getControllerColor(context, style) == COLOR_DARK_ON_LIGHT_BACKGROUND) { 61 theme = R.style.Theme_MediaRouter_Light; 62 } else { 63 theme = R.style.Theme_MediaRouter_Light_DarkControlPanel; 64 } 65 } else { 66 if (getControllerColor(context, style) == COLOR_DARK_ON_LIGHT_BACKGROUND) { 67 theme = R.style.Theme_MediaRouter_LightControlPanel; 68 } else { 69 theme = R.style.Theme_MediaRouter; 70 } 71 } 72 return new ContextThemeWrapper(context, theme); 73 } 74 getThemeResource(Context context, int attr)75 public static int getThemeResource(Context context, int attr) { 76 TypedValue value = new TypedValue(); 77 return context.getTheme().resolveAttribute(attr, value, true) ? value.resourceId : 0; 78 } 79 getDisabledAlpha(Context context)80 public static float getDisabledAlpha(Context context) { 81 TypedValue value = new TypedValue(); 82 return context.getTheme().resolveAttribute(android.R.attr.disabledAlpha, value, true) 83 ? value.getFloat() : 0.5f; 84 } 85 getControllerColor(Context context, int style)86 public static @ControllerColorType int getControllerColor(Context context, int style) { 87 int primaryColor = getThemeColor(context, style, 88 android.support.v7.appcompat.R.attr.colorPrimary); 89 if (ColorUtils.calculateContrast(COLOR_WHITE_ON_DARK_BACKGROUND, primaryColor) 90 >= MIN_CONTRAST) { 91 return COLOR_WHITE_ON_DARK_BACKGROUND; 92 } 93 return COLOR_DARK_ON_LIGHT_BACKGROUND; 94 } 95 getButtonTextColor(Context context)96 public static int getButtonTextColor(Context context) { 97 int primaryColor = getThemeColor(context, 0, 98 android.support.v7.appcompat.R.attr.colorPrimary); 99 int backgroundColor = getThemeColor(context, 0, android.R.attr.colorBackground); 100 101 if (ColorUtils.calculateContrast(primaryColor, backgroundColor) < MIN_CONTRAST) { 102 // Default to colorAccent if the contrast ratio is low. 103 return getThemeColor(context, 0, android.support.v7.appcompat.R.attr.colorAccent); 104 } 105 return primaryColor; 106 } 107 setMediaControlsBackgroundColor( Context context, View mainControls, View groupControls, boolean hasGroup)108 public static void setMediaControlsBackgroundColor( 109 Context context, View mainControls, View groupControls, boolean hasGroup) { 110 int primaryColor = getThemeColor(context, 0, 111 android.support.v7.appcompat.R.attr.colorPrimary); 112 int primaryDarkColor = getThemeColor(context, 0, 113 android.support.v7.appcompat.R.attr.colorPrimaryDark); 114 if (hasGroup && getControllerColor(context, 0) == COLOR_DARK_ON_LIGHT_BACKGROUND) { 115 // Instead of showing dark controls in a possibly dark (i.e. the primary dark), model 116 // the white dialog and use the primary color for the group controls. 117 primaryDarkColor = primaryColor; 118 primaryColor = Color.WHITE; 119 } 120 mainControls.setBackgroundColor(primaryColor); 121 groupControls.setBackgroundColor(primaryDarkColor); 122 // Also store the background colors to the view tags. They are used in 123 // setVolumeSliderColor() below. 124 mainControls.setTag(primaryColor); 125 groupControls.setTag(primaryDarkColor); 126 } 127 setVolumeSliderColor( Context context, MediaRouteVolumeSlider volumeSlider, View backgroundView)128 public static void setVolumeSliderColor( 129 Context context, MediaRouteVolumeSlider volumeSlider, View backgroundView) { 130 int controllerColor = getControllerColor(context, 0); 131 if (Color.alpha(controllerColor) != 0xFF) { 132 // Composite with the background in order not to show the underlying progress bar 133 // through the thumb. 134 int backgroundColor = (int) backgroundView.getTag(); 135 controllerColor = ColorUtils.compositeColors(controllerColor, backgroundColor); 136 } 137 volumeSlider.setColor(controllerColor); 138 } 139 140 // This is copied from {@link AlertDialog#resolveDialogTheme} to pre-evaluate theme in advance. getAlertDialogResolvedTheme(Context context, int themeResId)141 public static int getAlertDialogResolvedTheme(Context context, int themeResId) { 142 if (themeResId >= 0x01000000) { // start of real resource IDs. 143 return themeResId; 144 } else { 145 TypedValue outValue = new TypedValue(); 146 context.getTheme().resolveAttribute( 147 android.support.v7.appcompat.R.attr.alertDialogTheme, outValue, true); 148 return outValue.resourceId; 149 } 150 } 151 isLightTheme(Context context)152 private static boolean isLightTheme(Context context) { 153 TypedValue value = new TypedValue(); 154 return context.getTheme().resolveAttribute( 155 android.support.v7.appcompat.R.attr.isLightTheme, value, true) 156 && value.data != 0; 157 } 158 getThemeColor(Context context, int style, int attr)159 private static int getThemeColor(Context context, int style, int attr) { 160 if (style != 0) { 161 int[] attrs = { attr }; 162 TypedArray ta = context.obtainStyledAttributes(style, attrs); 163 int color = ta.getColor(0, 0); 164 ta.recycle(); 165 if (color != 0) { 166 return color; 167 } 168 } 169 TypedValue value = new TypedValue(); 170 context.getTheme().resolveAttribute(attr, value, true); 171 if (value.resourceId != 0) { 172 return context.getResources().getColor(value.resourceId); 173 } 174 return value.data; 175 } 176 } 177