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