Aurimas Liutikas | dc3f885 | 2024-07-11 10:07:48 -0700 | [diff] [blame] | 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 |
| 30 | public XmlPullAttributes(XmlPullParser parser) { |
| 31 | mParser = parser; |
| 32 | } |
| 33 | |
| 34 | public int getAttributeCount() { |
| 35 | return mParser.getAttributeCount(); |
| 36 | } |
| 37 | |
| 38 | public String getAttributeNamespace (int index) { |
| 39 | return mParser.getAttributeNamespace(index); |
| 40 | } |
| 41 | |
| 42 | public String getAttributeName(int index) { |
| 43 | return mParser.getAttributeName(index); |
| 44 | } |
| 45 | |
| 46 | public String getAttributeValue(int index) { |
| 47 | return mParser.getAttributeValue(index); |
| 48 | } |
| 49 | |
| 50 | public String getAttributeValue(String namespace, String name) { |
| 51 | return mParser.getAttributeValue(namespace, name); |
| 52 | } |
| 53 | |
| 54 | public String getPositionDescription() { |
| 55 | return mParser.getPositionDescription(); |
| 56 | } |
| 57 | |
| 58 | public int getAttributeNameResource(int index) { |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 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 | |
| 68 | public boolean getAttributeBooleanValue(String namespace, String attribute, |
| 69 | boolean defaultValue) { |
| 70 | return XmlUtils.convertValueToBoolean( |
| 71 | getAttributeValue(namespace, attribute), defaultValue); |
| 72 | } |
| 73 | |
| 74 | public int getAttributeResourceValue(String namespace, String attribute, |
| 75 | int defaultValue) { |
| 76 | return XmlUtils.convertValueToInt( |
| 77 | getAttributeValue(namespace, attribute), defaultValue); |
| 78 | } |
| 79 | |
| 80 | public int getAttributeIntValue(String namespace, String attribute, |
| 81 | int defaultValue) { |
| 82 | return XmlUtils.convertValueToInt( |
| 83 | getAttributeValue(namespace, attribute), defaultValue); |
| 84 | } |
| 85 | |
| 86 | public int getAttributeUnsignedIntValue(String namespace, String attribute, |
| 87 | int defaultValue) { |
| 88 | return XmlUtils.convertValueToUnsignedInt( |
| 89 | getAttributeValue(namespace, attribute), defaultValue); |
| 90 | } |
| 91 | |
| 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 | |
| 101 | public int getAttributeListValue(int index, |
| 102 | String[] options, int defaultValue) { |
| 103 | return XmlUtils.convertValueToList( |
| 104 | getAttributeValue(index), options, defaultValue); |
| 105 | } |
| 106 | |
| 107 | public boolean getAttributeBooleanValue(int index, boolean defaultValue) { |
| 108 | return XmlUtils.convertValueToBoolean( |
| 109 | getAttributeValue(index), defaultValue); |
| 110 | } |
| 111 | |
| 112 | public int getAttributeResourceValue(int index, int defaultValue) { |
| 113 | return XmlUtils.convertValueToInt( |
| 114 | getAttributeValue(index), defaultValue); |
| 115 | } |
| 116 | |
| 117 | public int getAttributeIntValue(int index, int defaultValue) { |
| 118 | return XmlUtils.convertValueToInt( |
| 119 | getAttributeValue(index), defaultValue); |
| 120 | } |
| 121 | |
| 122 | public int getAttributeUnsignedIntValue(int index, int defaultValue) { |
| 123 | return XmlUtils.convertValueToUnsignedInt( |
| 124 | getAttributeValue(index), defaultValue); |
| 125 | } |
| 126 | |
| 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 | |
| 135 | public String getIdAttribute() { |
| 136 | return getAttributeValue(null, "id"); |
| 137 | } |
| 138 | |
| 139 | public String getClassAttribute() { |
| 140 | return getAttributeValue(null, "class"); |
| 141 | } |
| 142 | |
| 143 | public int getIdAttributeResourceValue(int defaultValue) { |
| 144 | return getAttributeResourceValue(null, "id", defaultValue); |
| 145 | } |
| 146 | |
| 147 | public int getStyleAttribute() { |
| 148 | return getAttributeResourceValue(null, "style", 0); |
| 149 | } |
| 150 | |
| 151 | @UnsupportedAppUsage |
| 152 | /*package*/ XmlPullParser mParser; |
| 153 | } |