• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Context;
19 import android.content.res.TypedArray;
20 import android.graphics.drawable.Drawable;
21 import android.support.annotation.AnyRes;
22 import android.support.annotation.StyleableRes;
23 import android.util.TypedValue;
24 
25 /**
26  * Compat methods for accessing TypedArray values.
27  *
28  * @hide
29  */
30 public class TypedArrayUtils {
getBoolean(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex, boolean defaultValue)31     public static boolean getBoolean(TypedArray a, @StyleableRes int index,
32             @StyleableRes int fallbackIndex, boolean defaultValue) {
33         boolean val = a.getBoolean(fallbackIndex, defaultValue);
34         return a.getBoolean(index, val);
35     }
36 
getDrawable(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex)37     public static Drawable getDrawable(TypedArray a, @StyleableRes int index,
38             @StyleableRes int fallbackIndex) {
39         Drawable val = a.getDrawable(index);
40         if (val == null) {
41             val = a.getDrawable(fallbackIndex);
42         }
43         return val;
44     }
45 
getInt(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex, int defaultValue)46     public static int getInt(TypedArray a, @StyleableRes int index,
47             @StyleableRes int fallbackIndex, int defaultValue) {
48         int val = a.getInt(fallbackIndex, defaultValue);
49         return a.getInt(index, val);
50     }
51 
getResourceId(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex, @AnyRes int defaultValue)52     public static @AnyRes int getResourceId(TypedArray a, @StyleableRes int index,
53             @StyleableRes int fallbackIndex, @AnyRes int defaultValue) {
54         int val = a.getResourceId(fallbackIndex, defaultValue);
55         return a.getResourceId(index, val);
56     }
57 
getString(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex)58     public static String getString(TypedArray a, @StyleableRes int index,
59             @StyleableRes int fallbackIndex) {
60         String val = a.getString(index);
61         if (val == null) {
62             val = a.getString(fallbackIndex);
63         }
64         return val;
65     }
66 
getTextArray(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex)67     public static CharSequence[] getTextArray(TypedArray a, @StyleableRes int index,
68             @StyleableRes int fallbackIndex) {
69         CharSequence[] val = a.getTextArray(index);
70         if (val == null) {
71             val = a.getTextArray(fallbackIndex);
72         }
73         return val;
74     }
75 
getAttr(Context context, int attr, int fallbackAttr)76     public static int getAttr(Context context, int attr, int fallbackAttr) {
77         TypedValue value = new TypedValue();
78         context.getTheme().resolveAttribute(attr, value, true);
79         if (value.resourceId != 0) {
80             return attr;
81         }
82         return fallbackAttr;
83     }
84 }
85