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