1 /* 2 * Copyright (C) 2015 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 package android.support.v4.content.res; 17 18 import android.content.res.TypedArray; 19 import android.graphics.drawable.Drawable; 20 import android.support.annotation.AnyRes; 21 import android.support.annotation.StyleableRes; 22 23 /** 24 * Compat methods for accessing TypedArray values. 25 * 26 * @hide 27 */ 28 public class TypedArrayUtils { getBoolean(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex, boolean defaultValue)29 public static boolean getBoolean(TypedArray a, @StyleableRes int index, 30 @StyleableRes int fallbackIndex, boolean defaultValue) { 31 boolean val = a.getBoolean(fallbackIndex, defaultValue); 32 return a.getBoolean(index, val); 33 } 34 getDrawable(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex)35 public static Drawable getDrawable(TypedArray a, @StyleableRes int index, 36 @StyleableRes int fallbackIndex) { 37 Drawable val = a.getDrawable(index); 38 if (val == null) { 39 val = a.getDrawable(fallbackIndex); 40 } 41 return val; 42 } 43 getInt(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex, int defaultValue)44 public static int getInt(TypedArray a, @StyleableRes int index, 45 @StyleableRes int fallbackIndex, int defaultValue) { 46 int val = a.getInt(fallbackIndex, defaultValue); 47 return a.getInt(index, val); 48 } 49 getResourceId(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex, @AnyRes int defaultValue)50 public static @AnyRes int getResourceId(TypedArray a, @StyleableRes int index, 51 @StyleableRes int fallbackIndex, @AnyRes int defaultValue) { 52 int val = a.getResourceId(fallbackIndex, defaultValue); 53 return a.getResourceId(index, val); 54 } 55 getString(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex)56 public static String getString(TypedArray a, @StyleableRes int index, 57 @StyleableRes int fallbackIndex) { 58 String val = a.getString(index); 59 if (val == null) { 60 val = a.getString(fallbackIndex); 61 } 62 return val; 63 } 64 getTextArray(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex)65 public static CharSequence[] getTextArray(TypedArray a, @StyleableRes int index, 66 @StyleableRes int fallbackIndex) { 67 CharSequence[] val = a.getTextArray(index); 68 if (val == null) { 69 val = a.getTextArray(fallbackIndex); 70 } 71 return val; 72 } 73 } 74