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