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