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.internal.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 private TintManager mTintManager; 40 41 public static TintTypedArray obtainStyledAttributes(Context context, AttributeSet set, 42 int[] attrs) { 43 TypedArray array = context.obtainStyledAttributes(set, attrs); 44 return new TintTypedArray(context, array); 45 } 46 47 public static TintTypedArray obtainStyledAttributes(Context context, AttributeSet set, 48 int[] attrs, int defStyleAttr, int defStyleRes) { 49 TypedArray array = context.obtainStyledAttributes(set, attrs, defStyleAttr, defStyleRes); 50 return new TintTypedArray(context, array); 51 } 52 53 private TintTypedArray(Context context, TypedArray array) { 54 mContext = context; 55 mWrapped = array; 56 } 57 58 public Drawable getDrawable(int index) { 59 if (mWrapped.hasValue(index)) { 60 final int resourceId = mWrapped.getResourceId(index, 0); 61 if (resourceId != 0) { 62 return getTintManager().getDrawable(resourceId); 63 } 64 } 65 return mWrapped.getDrawable(index); 66 } 67 68 public Drawable getDrawableIfKnown(int index) { 69 if (mWrapped.hasValue(index)) { 70 final int resourceId = mWrapped.getResourceId(index, 0); 71 if (resourceId != 0) { 72 return getTintManager().getDrawable(resourceId, true); 73 } 74 } 75 return null; 76 } 77 78 public int length() { 79 return mWrapped.length(); 80 } 81 82 public int getIndexCount() { 83 return mWrapped.getIndexCount(); 84 } 85 86 public int getIndex(int at) { 87 return mWrapped.getIndex(at); 88 } 89 90 public Resources getResources() { 91 return mWrapped.getResources(); 92 } 93 94 public CharSequence getText(int index) { 95 return mWrapped.getText(index); 96 } 97 98 public String getString(int index) { 99 return mWrapped.getString(index); 100 } 101 102 public String getNonResourceString(int index) { 103 return mWrapped.getNonResourceString(index); 104 } 105 106 public boolean getBoolean(int index, boolean defValue) { 107 return mWrapped.getBoolean(index, defValue); 108 } 109 110 public int getInt(int index, int defValue) { 111 return mWrapped.getInt(index, defValue); 112 } 113 114 public float getFloat(int index, float defValue) { 115 return mWrapped.getFloat(index, defValue); 116 } 117 118 public int getColor(int index, int defValue) { 119 return mWrapped.getColor(index, defValue); 120 } 121 122 public ColorStateList getColorStateList(int index) { 123 return mWrapped.getColorStateList(index); 124 } 125 126 public int getInteger(int index, int defValue) { 127 return mWrapped.getInteger(index, defValue); 128 } 129 130 public float getDimension(int index, float defValue) { 131 return mWrapped.getDimension(index, defValue); 132 } 133 134 public int getDimensionPixelOffset(int index, int defValue) { 135 return mWrapped.getDimensionPixelOffset(index, defValue); 136 } 137 138 public int getDimensionPixelSize(int index, int defValue) { 139 return mWrapped.getDimensionPixelSize(index, defValue); 140 } 141 142 public int getLayoutDimension(int index, String name) { 143 return mWrapped.getLayoutDimension(index, name); 144 } 145 146 public int getLayoutDimension(int index, int defValue) { 147 return mWrapped.getLayoutDimension(index, defValue); 148 } 149 150 public float getFraction(int index, int base, int pbase, float defValue) { 151 return mWrapped.getFraction(index, base, pbase, defValue); 152 } 153 154 public int getResourceId(int index, int defValue) { 155 return mWrapped.getResourceId(index, defValue); 156 } 157 158 public CharSequence[] getTextArray(int index) { 159 return mWrapped.getTextArray(index); 160 } 161 162 public boolean getValue(int index, TypedValue outValue) { 163 return mWrapped.getValue(index, outValue); 164 } 165 166 public int getType(int index) { 167 return mWrapped.getType(index); 168 } 169 170 public boolean hasValue(int index) { 171 return mWrapped.hasValue(index); 172 } 173 174 public TypedValue peekValue(int index) { 175 return mWrapped.peekValue(index); 176 } 177 178 public String getPositionDescription() { 179 return mWrapped.getPositionDescription(); 180 } 181 182 public void recycle() { 183 mWrapped.recycle(); 184 } 185 186 public int getChangingConfigurations() { 187 return mWrapped.getChangingConfigurations(); 188 } 189 190 public TintManager getTintManager() { 191 if (mTintManager == null) { 192 mTintManager = TintManager.get(mContext); 193 } 194 return mTintManager; 195 } 196 197} 198