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 int length() { 69 return mWrapped.length(); 70 } 71 72 public int getIndexCount() { 73 return mWrapped.getIndexCount(); 74 } 75 76 public int getIndex(int at) { 77 return mWrapped.getIndex(at); 78 } 79 80 public Resources getResources() { 81 return mWrapped.getResources(); 82 } 83 84 public CharSequence getText(int index) { 85 return mWrapped.getText(index); 86 } 87 88 public String getString(int index) { 89 return mWrapped.getString(index); 90 } 91 92 public String getNonResourceString(int index) { 93 return mWrapped.getNonResourceString(index); 94 } 95 96 public boolean getBoolean(int index, boolean defValue) { 97 return mWrapped.getBoolean(index, defValue); 98 } 99 100 public int getInt(int index, int defValue) { 101 return mWrapped.getInt(index, defValue); 102 } 103 104 public float getFloat(int index, float defValue) { 105 return mWrapped.getFloat(index, defValue); 106 } 107 108 public int getColor(int index, int defValue) { 109 return mWrapped.getColor(index, defValue); 110 } 111 112 public ColorStateList getColorStateList(int index) { 113 return mWrapped.getColorStateList(index); 114 } 115 116 public int getInteger(int index, int defValue) { 117 return mWrapped.getInteger(index, defValue); 118 } 119 120 public float getDimension(int index, float defValue) { 121 return mWrapped.getDimension(index, defValue); 122 } 123 124 public int getDimensionPixelOffset(int index, int defValue) { 125 return mWrapped.getDimensionPixelOffset(index, defValue); 126 } 127 128 public int getDimensionPixelSize(int index, int defValue) { 129 return mWrapped.getDimensionPixelSize(index, defValue); 130 } 131 132 public int getLayoutDimension(int index, String name) { 133 return mWrapped.getLayoutDimension(index, name); 134 } 135 136 public int getLayoutDimension(int index, int defValue) { 137 return mWrapped.getLayoutDimension(index, defValue); 138 } 139 140 public float getFraction(int index, int base, int pbase, float defValue) { 141 return mWrapped.getFraction(index, base, pbase, defValue); 142 } 143 144 public int getResourceId(int index, int defValue) { 145 return mWrapped.getResourceId(index, defValue); 146 } 147 148 public CharSequence[] getTextArray(int index) { 149 return mWrapped.getTextArray(index); 150 } 151 152 public boolean getValue(int index, TypedValue outValue) { 153 return mWrapped.getValue(index, outValue); 154 } 155 156 public int getType(int index) { 157 return mWrapped.getType(index); 158 } 159 160 public boolean hasValue(int index) { 161 return mWrapped.hasValue(index); 162 } 163 164 public TypedValue peekValue(int index) { 165 return mWrapped.peekValue(index); 166 } 167 168 public String getPositionDescription() { 169 return mWrapped.getPositionDescription(); 170 } 171 172 public void recycle() { 173 mWrapped.recycle(); 174 } 175 176 public int getChangingConfigurations() { 177 return mWrapped.getChangingConfigurations(); 178 } 179 180 public TintManager getTintManager() { 181 if (mTintManager == null) { 182 mTintManager = new TintManager(mContext); 183 } 184 return mTintManager; 185 } 186 187} 188