• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.widget;
18 
19 import android.content.Context;
20 import android.content.res.ColorStateList;
21 import android.content.res.TypedArray;
22 import android.os.Build;
23 import android.support.v7.appcompat.R;
24 import android.support.v7.internal.text.AllCapsTransformationMethod;
25 import android.support.v7.internal.widget.ThemeUtils;
26 import android.util.AttributeSet;
27 import android.widget.TextView;
28 
29 class AppCompatTextHelper {
30 
31     private static final int[] VIEW_ATTRS = {android.R.attr.textAppearance};
32     private static final int[] TEXT_APPEARANCE_ATTRS = {R.attr.textAllCaps};
33 
34     private final TextView mView;
35 
AppCompatTextHelper(TextView view)36     AppCompatTextHelper(TextView view) {
37         mView = view;
38     }
39 
loadFromAttributes(AttributeSet attrs, int defStyleAttr)40     void loadFromAttributes(AttributeSet attrs, int defStyleAttr) {
41         Context context = mView.getContext();
42 
43         // First read the TextAppearance style id
44         TypedArray a = context.obtainStyledAttributes(attrs, VIEW_ATTRS, defStyleAttr, 0);
45         final int ap = a.getResourceId(0, -1);
46         a.recycle();
47 
48         // Now check TextAppearance's textAllCaps value
49         if (ap != -1) {
50             TypedArray appearance = context.obtainStyledAttributes(ap, R.styleable.TextAppearance);
51             if (appearance.hasValue(R.styleable.TextAppearance_textAllCaps)) {
52                 setAllCaps(appearance.getBoolean(R.styleable.TextAppearance_textAllCaps, false));
53             }
54             appearance.recycle();
55         }
56 
57         // Now read the style's value
58         a = context.obtainStyledAttributes(attrs, TEXT_APPEARANCE_ATTRS, defStyleAttr, 0);
59         if (a.hasValue(0)) {
60             setAllCaps(a.getBoolean(0, false));
61         }
62         a.recycle();
63 
64         final ColorStateList textColors = mView.getTextColors();
65         if (textColors != null && !textColors.isStateful()) {
66             // If we have a ColorStateList which isn't stateful, create one which includes
67             // a disabled state
68 
69             final int disabledTextColor;
70             if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
71                 // Pre-Lollipop, we will use textColorSecondary with android:disabledAlpha
72                 // applied
73                 disabledTextColor = ThemeUtils.getDisabledThemeAttrColor(context,
74                         android.R.attr.textColorSecondary);
75             } else {
76                 // With certain styles on Lollipop, there is a StateListAnimator which sets
77                 // an alpha on the whole view, so we don't need to apply disabledAlpha to
78                 // textColorSecondary
79                 disabledTextColor = ThemeUtils.getThemeAttrColor(context,
80                         android.R.attr.textColorSecondary);
81             }
82 
83             mView.setTextColor(ThemeUtils.createDisabledStateList(
84                     textColors.getDefaultColor(), disabledTextColor));
85         }
86     }
87 
onSetTextAppearance(Context context, int resId)88     void onSetTextAppearance(Context context, int resId) {
89         TypedArray appearance = context.obtainStyledAttributes(resId, TEXT_APPEARANCE_ATTRS);
90         if (appearance.hasValue(0)) {
91             setAllCaps(appearance.getBoolean(0, false));
92         }
93         appearance.recycle();
94     }
95 
setAllCaps(boolean allCaps)96     void setAllCaps(boolean allCaps) {
97         mView.setTransformationMethod(allCaps
98                 ? new AllCapsTransformationMethod(mView.getContext())
99                 : null);
100     }
101 }
102