1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package android.support.graphics.drawable;
16 
17 import android.content.res.TypedArray;
18 import org.xmlpull.v1.XmlPullParser;
19 
20 class TypedArrayUtils {
21     private static final String NAMESPACE = "http://schemas.android.com/apk/res/android";
22 
hasAttribute(XmlPullParser parser, String attrName)23     public static boolean hasAttribute(XmlPullParser parser, String attrName) {
24         return parser.getAttributeValue(NAMESPACE, attrName) != null;
25     }
26 
getNamedFloat(TypedArray a, XmlPullParser parser, String attrName, int resId, float defaultValue)27     public static float getNamedFloat(TypedArray a, XmlPullParser parser, String attrName,
28                                       int resId, float defaultValue) {
29         final boolean hasAttr = hasAttribute(parser, attrName);
30         if (!hasAttr) {
31             return defaultValue;
32         } else {
33             return a.getFloat(resId, defaultValue);
34         }
35     }
36 
getNamedBoolean(TypedArray a, XmlPullParser parser, String attrName, int resId, boolean defaultValue)37     public static boolean getNamedBoolean(TypedArray a, XmlPullParser parser, String attrName,
38                                           int resId, boolean defaultValue) {
39         final boolean hasAttr = hasAttribute(parser, attrName);
40         if (!hasAttr) {
41             return defaultValue;
42         } else {
43             return a.getBoolean(resId, defaultValue);
44         }
45     }
46 
getNamedInt(TypedArray a, XmlPullParser parser, String attrName, int resId, int defaultValue)47     public static int getNamedInt(TypedArray a, XmlPullParser parser, String attrName,
48                                   int resId, int defaultValue) {
49         final boolean hasAttr = hasAttribute(parser, attrName);
50         if (!hasAttr) {
51             return defaultValue;
52         } else {
53             return a.getInt(resId, defaultValue);
54         }
55     }
56 
getNamedColor(TypedArray a, XmlPullParser parser, String attrName, int resId, int defaultValue)57     public static int getNamedColor(TypedArray a, XmlPullParser parser, String attrName,
58                                     int resId, int defaultValue) {
59         final boolean hasAttr = hasAttribute(parser, attrName);
60         if (!hasAttr) {
61             return defaultValue;
62         } else {
63             return a.getColor(resId, defaultValue);
64         }
65     }
66 }
67