1 /* 2 * Copyright (C) 2015 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.internal.widget; 18 19 import android.content.Context; 20 import android.content.res.ColorStateList; 21 import android.content.res.TypedArray; 22 import android.graphics.Color; 23 import android.support.v4.graphics.ColorUtils; 24 import android.util.TypedValue; 25 26 /** 27 * @hide 28 */ 29 public class ThemeUtils { 30 31 private static final ThreadLocal<TypedValue> TL_TYPED_VALUE = new ThreadLocal<>(); 32 33 static final int[] DISABLED_STATE_SET = new int[]{-android.R.attr.state_enabled}; 34 static final int[] FOCUSED_STATE_SET = new int[]{android.R.attr.state_focused}; 35 static final int[] ACTIVATED_STATE_SET = new int[]{android.R.attr.state_activated}; 36 static final int[] PRESSED_STATE_SET = new int[]{android.R.attr.state_pressed}; 37 static final int[] CHECKED_STATE_SET = new int[]{android.R.attr.state_checked}; 38 static final int[] SELECTED_STATE_SET = new int[]{android.R.attr.state_selected}; 39 static final int[] NOT_PRESSED_OR_FOCUSED_STATE_SET = new int[]{ 40 -android.R.attr.state_pressed, -android.R.attr.state_focused}; 41 static final int[] EMPTY_STATE_SET = new int[0]; 42 43 private static final int[] TEMP_ARRAY = new int[1]; 44 createDisabledStateList(int textColor, int disabledTextColor)45 public static ColorStateList createDisabledStateList(int textColor, int disabledTextColor) { 46 // Now create a new ColorStateList with the default color, and the new disabled 47 // color 48 final int[][] states = new int[2][]; 49 final int[] colors = new int[2]; 50 int i = 0; 51 52 // Disabled state 53 states[i] = DISABLED_STATE_SET; 54 colors[i] = disabledTextColor; 55 i++; 56 57 // Default state 58 states[i] = EMPTY_STATE_SET; 59 colors[i] = textColor; 60 i++; 61 62 return new ColorStateList(states, colors); 63 } 64 getThemeAttrColor(Context context, int attr)65 public static int getThemeAttrColor(Context context, int attr) { 66 TEMP_ARRAY[0] = attr; 67 TypedArray a = context.obtainStyledAttributes(null, TEMP_ARRAY); 68 try { 69 return a.getColor(0, 0); 70 } finally { 71 a.recycle(); 72 } 73 } 74 getThemeAttrColorStateList(Context context, int attr)75 public static ColorStateList getThemeAttrColorStateList(Context context, int attr) { 76 TEMP_ARRAY[0] = attr; 77 TypedArray a = context.obtainStyledAttributes(null, TEMP_ARRAY); 78 try { 79 return a.getColorStateList(0); 80 } finally { 81 a.recycle(); 82 } 83 } 84 getDisabledThemeAttrColor(Context context, int attr)85 public static int getDisabledThemeAttrColor(Context context, int attr) { 86 final ColorStateList csl = getThemeAttrColorStateList(context, attr); 87 if (csl != null && csl.isStateful()) { 88 // If the CSL is stateful, we'll assume it has a disabled state and use it 89 return csl.getColorForState(DISABLED_STATE_SET, csl.getDefaultColor()); 90 } else { 91 // Else, we'll generate the color using disabledAlpha from the theme 92 93 final TypedValue tv = getTypedValue(); 94 // Now retrieve the disabledAlpha value from the theme 95 context.getTheme().resolveAttribute(android.R.attr.disabledAlpha, tv, true); 96 final float disabledAlpha = tv.getFloat(); 97 98 return getThemeAttrColor(context, attr, disabledAlpha); 99 } 100 } 101 getTypedValue()102 private static TypedValue getTypedValue() { 103 TypedValue typedValue = TL_TYPED_VALUE.get(); 104 if (typedValue == null) { 105 typedValue = new TypedValue(); 106 TL_TYPED_VALUE.set(typedValue); 107 } 108 return typedValue; 109 } 110 getThemeAttrColor(Context context, int attr, float alpha)111 static int getThemeAttrColor(Context context, int attr, float alpha) { 112 final int color = getThemeAttrColor(context, attr); 113 final int originalAlpha = Color.alpha(color); 114 return ColorUtils.setAlphaComponent(color, Math.round(originalAlpha * alpha)); 115 } 116 117 } 118