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