1 /* 2 * Copyright (C) 2006 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 17 package android.util; 18 19 import android.compat.annotation.UnsupportedAppUsage; 20 21 import com.android.internal.util.XmlUtils; 22 23 import org.xmlpull.v1.XmlPullParser; 24 25 /** 26 * Provides an implementation of AttributeSet on top of an XmlPullParser. 27 */ 28 class XmlPullAttributes implements AttributeSet { 29 @UnsupportedAppUsage XmlPullAttributes(XmlPullParser parser)30 public XmlPullAttributes(XmlPullParser parser) { 31 mParser = parser; 32 } 33 getAttributeCount()34 public int getAttributeCount() { 35 return mParser.getAttributeCount(); 36 } 37 getAttributeNamespace(int index)38 public String getAttributeNamespace (int index) { 39 return mParser.getAttributeNamespace(index); 40 } 41 getAttributeName(int index)42 public String getAttributeName(int index) { 43 return mParser.getAttributeName(index); 44 } 45 getAttributeValue(int index)46 public String getAttributeValue(int index) { 47 return mParser.getAttributeValue(index); 48 } 49 getAttributeValue(String namespace, String name)50 public String getAttributeValue(String namespace, String name) { 51 return mParser.getAttributeValue(namespace, name); 52 } 53 getPositionDescription()54 public String getPositionDescription() { 55 return mParser.getPositionDescription(); 56 } 57 getAttributeNameResource(int index)58 public int getAttributeNameResource(int index) { 59 return 0; 60 } 61 getAttributeListValue(String namespace, String attribute, String[] options, int defaultValue)62 public int getAttributeListValue(String namespace, String attribute, 63 String[] options, int defaultValue) { 64 return XmlUtils.convertValueToList( 65 getAttributeValue(namespace, attribute), options, defaultValue); 66 } 67 getAttributeBooleanValue(String namespace, String attribute, boolean defaultValue)68 public boolean getAttributeBooleanValue(String namespace, String attribute, 69 boolean defaultValue) { 70 return XmlUtils.convertValueToBoolean( 71 getAttributeValue(namespace, attribute), defaultValue); 72 } 73 getAttributeResourceValue(String namespace, String attribute, int defaultValue)74 public int getAttributeResourceValue(String namespace, String attribute, 75 int defaultValue) { 76 return XmlUtils.convertValueToInt( 77 getAttributeValue(namespace, attribute), defaultValue); 78 } 79 getAttributeIntValue(String namespace, String attribute, int defaultValue)80 public int getAttributeIntValue(String namespace, String attribute, 81 int defaultValue) { 82 return XmlUtils.convertValueToInt( 83 getAttributeValue(namespace, attribute), defaultValue); 84 } 85 getAttributeUnsignedIntValue(String namespace, String attribute, int defaultValue)86 public int getAttributeUnsignedIntValue(String namespace, String attribute, 87 int defaultValue) { 88 return XmlUtils.convertValueToUnsignedInt( 89 getAttributeValue(namespace, attribute), defaultValue); 90 } 91 getAttributeFloatValue(String namespace, String attribute, float defaultValue)92 public float getAttributeFloatValue(String namespace, String attribute, 93 float defaultValue) { 94 String s = getAttributeValue(namespace, attribute); 95 if (s != null) { 96 return Float.parseFloat(s); 97 } 98 return defaultValue; 99 } 100 getAttributeListValue(int index, String[] options, int defaultValue)101 public int getAttributeListValue(int index, 102 String[] options, int defaultValue) { 103 return XmlUtils.convertValueToList( 104 getAttributeValue(index), options, defaultValue); 105 } 106 getAttributeBooleanValue(int index, boolean defaultValue)107 public boolean getAttributeBooleanValue(int index, boolean defaultValue) { 108 return XmlUtils.convertValueToBoolean( 109 getAttributeValue(index), defaultValue); 110 } 111 getAttributeResourceValue(int index, int defaultValue)112 public int getAttributeResourceValue(int index, int defaultValue) { 113 return XmlUtils.convertValueToInt( 114 getAttributeValue(index), defaultValue); 115 } 116 getAttributeIntValue(int index, int defaultValue)117 public int getAttributeIntValue(int index, int defaultValue) { 118 return XmlUtils.convertValueToInt( 119 getAttributeValue(index), defaultValue); 120 } 121 getAttributeUnsignedIntValue(int index, int defaultValue)122 public int getAttributeUnsignedIntValue(int index, int defaultValue) { 123 return XmlUtils.convertValueToUnsignedInt( 124 getAttributeValue(index), defaultValue); 125 } 126 getAttributeFloatValue(int index, float defaultValue)127 public float getAttributeFloatValue(int index, float defaultValue) { 128 String s = getAttributeValue(index); 129 if (s != null) { 130 return Float.parseFloat(s); 131 } 132 return defaultValue; 133 } 134 getIdAttribute()135 public String getIdAttribute() { 136 return getAttributeValue(null, "id"); 137 } 138 getClassAttribute()139 public String getClassAttribute() { 140 return getAttributeValue(null, "class"); 141 } 142 getIdAttributeResourceValue(int defaultValue)143 public int getIdAttributeResourceValue(int defaultValue) { 144 return getAttributeResourceValue(null, "id", defaultValue); 145 } 146 getStyleAttribute()147 public int getStyleAttribute() { 148 return getAttributeResourceValue(null, "style", 0); 149 } 150 151 @UnsupportedAppUsage 152 /*package*/ XmlPullParser mParser; 153 } 154