TintTypedArray.java revision 66698bb15ba0f873aa1c2290cc50d6bb839a474a
1/*
2 * Copyright (C) 2014 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
17package android.support.v7.widget;
18
19import android.content.Context;
20import android.content.res.ColorStateList;
21import android.content.res.Resources;
22import android.content.res.TypedArray;
23import android.graphics.drawable.Drawable;
24import android.util.AttributeSet;
25import android.util.TypedValue;
26
27/**
28 * A class that wraps a {@link android.content.res.TypedArray} and provides the same public API
29 * surface. The purpose of this class is so that we can intercept the {@link #getDrawable(int)}
30 * call and tint the result.
31 *
32 * @hide
33 */
34public class TintTypedArray {
35
36    private final Context mContext;
37    private final TypedArray mWrapped;
38
39    public static TintTypedArray obtainStyledAttributes(Context context, AttributeSet set,
40            int[] attrs) {
41        TypedArray array = context.obtainStyledAttributes(set, attrs);
42        return new TintTypedArray(context, array);
43    }
44
45    public static TintTypedArray obtainStyledAttributes(Context context, AttributeSet set,
46            int[] attrs, int defStyleAttr, int defStyleRes) {
47        TypedArray array = context.obtainStyledAttributes(set, attrs, defStyleAttr, defStyleRes);
48        return new TintTypedArray(context, array);
49    }
50
51    private TintTypedArray(Context context, TypedArray array) {
52        mContext = context;
53        mWrapped = array;
54    }
55
56    public Drawable getDrawable(int index) {
57        if (mWrapped.hasValue(index)) {
58            final int resourceId = mWrapped.getResourceId(index, 0);
59            if (resourceId != 0) {
60                return AppCompatDrawableManager.get().getDrawable(mContext, resourceId);
61            }
62        }
63        return mWrapped.getDrawable(index);
64    }
65
66    public Drawable getDrawableIfKnown(int index) {
67        if (mWrapped.hasValue(index)) {
68            final int resourceId = mWrapped.getResourceId(index, 0);
69            if (resourceId != 0) {
70                return AppCompatDrawableManager.get().getDrawable(mContext, resourceId, true);
71            }
72        }
73        return null;
74    }
75
76    public int length() {
77        return mWrapped.length();
78    }
79
80    public int getIndexCount() {
81        return mWrapped.getIndexCount();
82    }
83
84    public int getIndex(int at) {
85        return mWrapped.getIndex(at);
86    }
87
88    public Resources getResources() {
89        return mWrapped.getResources();
90    }
91
92    public CharSequence getText(int index) {
93        return mWrapped.getText(index);
94    }
95
96    public String getString(int index) {
97        return mWrapped.getString(index);
98    }
99
100    public String getNonResourceString(int index) {
101        return mWrapped.getNonResourceString(index);
102    }
103
104    public boolean getBoolean(int index, boolean defValue) {
105        return mWrapped.getBoolean(index, defValue);
106    }
107
108    public int getInt(int index, int defValue) {
109        return mWrapped.getInt(index, defValue);
110    }
111
112    public float getFloat(int index, float defValue) {
113        return mWrapped.getFloat(index, defValue);
114    }
115
116    public int getColor(int index, int defValue) {
117        return mWrapped.getColor(index, defValue);
118    }
119
120    public ColorStateList getColorStateList(int index) {
121        return mWrapped.getColorStateList(index);
122    }
123
124    public int getInteger(int index, int defValue) {
125        return mWrapped.getInteger(index, defValue);
126    }
127
128    public float getDimension(int index, float defValue) {
129        return mWrapped.getDimension(index, defValue);
130    }
131
132    public int getDimensionPixelOffset(int index, int defValue) {
133        return mWrapped.getDimensionPixelOffset(index, defValue);
134    }
135
136    public int getDimensionPixelSize(int index, int defValue) {
137        return mWrapped.getDimensionPixelSize(index, defValue);
138    }
139
140    public int getLayoutDimension(int index, String name) {
141        return mWrapped.getLayoutDimension(index, name);
142    }
143
144    public int getLayoutDimension(int index, int defValue) {
145        return mWrapped.getLayoutDimension(index, defValue);
146    }
147
148    public float getFraction(int index, int base, int pbase, float defValue) {
149        return mWrapped.getFraction(index, base, pbase, defValue);
150    }
151
152    public int getResourceId(int index, int defValue) {
153        return mWrapped.getResourceId(index, defValue);
154    }
155
156    public CharSequence[] getTextArray(int index) {
157        return mWrapped.getTextArray(index);
158    }
159
160    public boolean getValue(int index, TypedValue outValue) {
161        return mWrapped.getValue(index, outValue);
162    }
163
164    public int getType(int index) {
165        return mWrapped.getType(index);
166    }
167
168    public boolean hasValue(int index) {
169        return mWrapped.hasValue(index);
170    }
171
172    public TypedValue peekValue(int index) {
173        return mWrapped.peekValue(index);
174    }
175
176    public String getPositionDescription() {
177        return mWrapped.getPositionDescription();
178    }
179
180    public void recycle() {
181        mWrapped.recycle();
182    }
183
184    public int getChangingConfigurations() {
185        return mWrapped.getChangingConfigurations();
186    }
187
188}
189