1282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski/* 2282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Copyright (C) 2008 The Android Open Source Project 3282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * 4282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Licensed under the Apache License, Version 2.0 (the "License"); 5282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * you may not use this file except in compliance with the License. 6282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * You may obtain a copy of the License at 7282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * 8282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * http://www.apache.org/licenses/LICENSE-2.0 9282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * 10282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Unless required by applicable law or agreed to in writing, software 11282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * distributed under the License is distributed on an "AS IS" BASIS, 12282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * See the License for the specific language governing permissions and 14282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * limitations under the License. 15282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski */ 16282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 17282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskipackage android.util; 18282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 19abb080d749638802cef92e2d40faa8b6d9553755Diego Perezimport com.android.ide.common.rendering.api.AttrResourceValue; 20282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.ide.common.rendering.api.RenderResources; 21282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.ide.common.rendering.api.ResourceValue; 22282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.internal.util.XmlUtils; 23282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.layoutlib.bridge.Bridge; 24282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.layoutlib.bridge.BridgeConstants; 25282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.layoutlib.bridge.android.BridgeContext; 2699d90a621084ec0f799dc7840cc05565a0589031Deepanshu Guptaimport com.android.layoutlib.bridge.impl.ResourceHelper; 27282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.resources.ResourceType; 28282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 29282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport org.xmlpull.v1.XmlPullParser; 30282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 31abb080d749638802cef92e2d40faa8b6d9553755Diego Perezimport android.annotation.NonNull; 32abb080d749638802cef92e2d40faa8b6d9553755Diego Perez 33abb080d749638802cef92e2d40faa8b6d9553755Diego Perezimport java.util.Map; 34abb080d749638802cef92e2d40faa8b6d9553755Diego Perezimport java.util.function.Function; 35abb080d749638802cef92e2d40faa8b6d9553755Diego Perez 36282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski/** 37282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * A correct implementation of the {@link AttributeSet} interface on top of a XmlPullParser 38282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski */ 39282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskipublic class BridgeXmlPullAttributes extends XmlPullAttributes { 40282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 41282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski private final BridgeContext mContext; 42282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski private final boolean mPlatformFile; 43abb080d749638802cef92e2d40faa8b6d9553755Diego Perez private final Function<String, Map<String, Integer>> mFrameworkEnumValueSupplier; 44abb080d749638802cef92e2d40faa8b6d9553755Diego Perez private final Function<String, Map<String, Integer>> mProjectEnumValueSupplier; 45abb080d749638802cef92e2d40faa8b6d9553755Diego Perez 46abb080d749638802cef92e2d40faa8b6d9553755Diego Perez // VisibleForTesting 47abb080d749638802cef92e2d40faa8b6d9553755Diego Perez BridgeXmlPullAttributes(@NonNull XmlPullParser parser, @NonNull BridgeContext context, 48abb080d749638802cef92e2d40faa8b6d9553755Diego Perez boolean platformFile, 49abb080d749638802cef92e2d40faa8b6d9553755Diego Perez @NonNull Function<String, Map<String, Integer>> frameworkEnumValueSupplier, 50abb080d749638802cef92e2d40faa8b6d9553755Diego Perez @NonNull Function<String, Map<String, Integer>> projectEnumValueSupplier) { 51282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski super(parser); 52282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski mContext = context; 53282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski mPlatformFile = platformFile; 54abb080d749638802cef92e2d40faa8b6d9553755Diego Perez mFrameworkEnumValueSupplier = frameworkEnumValueSupplier; 55abb080d749638802cef92e2d40faa8b6d9553755Diego Perez mProjectEnumValueSupplier = projectEnumValueSupplier; 56abb080d749638802cef92e2d40faa8b6d9553755Diego Perez } 57abb080d749638802cef92e2d40faa8b6d9553755Diego Perez 58abb080d749638802cef92e2d40faa8b6d9553755Diego Perez public BridgeXmlPullAttributes(@NonNull XmlPullParser parser, @NonNull BridgeContext context, 59abb080d749638802cef92e2d40faa8b6d9553755Diego Perez boolean platformFile) { 60abb080d749638802cef92e2d40faa8b6d9553755Diego Perez this(parser, context, platformFile, Bridge::getEnumValues, attrName -> { 61abb080d749638802cef92e2d40faa8b6d9553755Diego Perez // get the styleable matching the resolved name 62abb080d749638802cef92e2d40faa8b6d9553755Diego Perez RenderResources res = context.getRenderResources(); 63abb080d749638802cef92e2d40faa8b6d9553755Diego Perez ResourceValue attr = res.getProjectResource(ResourceType.ATTR, attrName); 64abb080d749638802cef92e2d40faa8b6d9553755Diego Perez return attr instanceof AttrResourceValue ? 65abb080d749638802cef92e2d40faa8b6d9553755Diego Perez ((AttrResourceValue) attr).getAttributeValues() : null; 66abb080d749638802cef92e2d40faa8b6d9553755Diego Perez }); 67282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 68282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 69282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski /* 70282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * (non-Javadoc) 71282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @see android.util.XmlPullAttributes#getAttributeNameResource(int) 72282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * 73282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * This methods must return com.android.internal.R.attr.<name> matching 74282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * the name of the attribute. 75282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * It returns 0 if it doesn't find anything. 76282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski */ 77282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @Override 78282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski public int getAttributeNameResource(int index) { 79282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // get the attribute name. 80282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski String name = getAttributeName(index); 81282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 82282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // get the attribute namespace 83282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski String ns = mParser.getAttributeNamespace(index); 84282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 85282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (BridgeConstants.NS_RESOURCES.equals(ns)) { 86abb080d749638802cef92e2d40faa8b6d9553755Diego Perez return Bridge.getResourceId(ResourceType.ATTR, name); 87282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 88282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 89282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 90282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // this is not an attribute in the android namespace, we query the customviewloader, if 91282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // the namespaces match. 9237dbb8b7f3c069196040eed3a03006647db7fa5bDeepanshu Gupta if (mContext.getLayoutlibCallback().getNamespace().equals(ns)) { 9337dbb8b7f3c069196040eed3a03006647db7fa5bDeepanshu Gupta Integer v = mContext.getLayoutlibCallback().getResourceId(ResourceType.ATTR, name); 94282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (v != null) { 95abb080d749638802cef92e2d40faa8b6d9553755Diego Perez return v; 96282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 97282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 98282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 99282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return 0; 100282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 101282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 102282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @Override 103282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski public int getAttributeListValue(String namespace, String attribute, 104282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski String[] options, int defaultValue) { 105282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski String value = getAttributeValue(namespace, attribute); 106282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (value != null) { 107282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski ResourceValue r = getResourceValue(value); 108282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 109282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (r != null) { 110282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski value = r.getValue(); 111282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 112282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 113282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return XmlUtils.convertValueToList(value, options, defaultValue); 114282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 115282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 116282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return defaultValue; 117282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 118282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 119282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @Override 120282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski public boolean getAttributeBooleanValue(String namespace, String attribute, 121282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski boolean defaultValue) { 122282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski String value = getAttributeValue(namespace, attribute); 123282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (value != null) { 124282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski ResourceValue r = getResourceValue(value); 125282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 126282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (r != null) { 127282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski value = r.getValue(); 128282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 129282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 130282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return XmlUtils.convertValueToBoolean(value, defaultValue); 131282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 132282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 133282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return defaultValue; 134282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 135282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 136282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @Override 137282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski public int getAttributeResourceValue(String namespace, String attribute, int defaultValue) { 138282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski String value = getAttributeValue(namespace, attribute); 139282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 140282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return resolveResourceValue(value, defaultValue); 141282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 142282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 143282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @Override 144abb080d749638802cef92e2d40faa8b6d9553755Diego Perez public int getAttributeIntValue(String namespace, String attribute, int defaultValue) { 145282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski String value = getAttributeValue(namespace, attribute); 146abb080d749638802cef92e2d40faa8b6d9553755Diego Perez if (value == null) { 147abb080d749638802cef92e2d40faa8b6d9553755Diego Perez return defaultValue; 148abb080d749638802cef92e2d40faa8b6d9553755Diego Perez } 149282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 150abb080d749638802cef92e2d40faa8b6d9553755Diego Perez ResourceValue r = getResourceValue(value); 151282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 152abb080d749638802cef92e2d40faa8b6d9553755Diego Perez if (r != null) { 153abb080d749638802cef92e2d40faa8b6d9553755Diego Perez value = r.getValue(); 154282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 155282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 156abb080d749638802cef92e2d40faa8b6d9553755Diego Perez if (value.charAt(0) == '#') { 157abb080d749638802cef92e2d40faa8b6d9553755Diego Perez return ResourceHelper.getColor(value); 158abb080d749638802cef92e2d40faa8b6d9553755Diego Perez } 159abb080d749638802cef92e2d40faa8b6d9553755Diego Perez 160abb080d749638802cef92e2d40faa8b6d9553755Diego Perez try { 161abb080d749638802cef92e2d40faa8b6d9553755Diego Perez return XmlUtils.convertValueToInt(value, defaultValue); 162abb080d749638802cef92e2d40faa8b6d9553755Diego Perez } catch (NumberFormatException e) { 163abb080d749638802cef92e2d40faa8b6d9553755Diego Perez // This is probably an enum 164abb080d749638802cef92e2d40faa8b6d9553755Diego Perez Map<String, Integer> enumValues = BridgeConstants.NS_RESOURCES.equals(namespace) ? 165abb080d749638802cef92e2d40faa8b6d9553755Diego Perez mFrameworkEnumValueSupplier.apply(attribute) : 166abb080d749638802cef92e2d40faa8b6d9553755Diego Perez mProjectEnumValueSupplier.apply(attribute); 167abb080d749638802cef92e2d40faa8b6d9553755Diego Perez 168abb080d749638802cef92e2d40faa8b6d9553755Diego Perez Integer enumValue = enumValues != null ? enumValues.get(value) : null; 169abb080d749638802cef92e2d40faa8b6d9553755Diego Perez if (enumValue != null) { 170abb080d749638802cef92e2d40faa8b6d9553755Diego Perez return enumValue; 171abb080d749638802cef92e2d40faa8b6d9553755Diego Perez } 172abb080d749638802cef92e2d40faa8b6d9553755Diego Perez 173abb080d749638802cef92e2d40faa8b6d9553755Diego Perez // We weren't able to find the enum int value 174abb080d749638802cef92e2d40faa8b6d9553755Diego Perez throw e; 175abb080d749638802cef92e2d40faa8b6d9553755Diego Perez } 176282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 177282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 178282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @Override 179282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski public int getAttributeUnsignedIntValue(String namespace, String attribute, 180282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski int defaultValue) { 181282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski String value = getAttributeValue(namespace, attribute); 182282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (value != null) { 183282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski ResourceValue r = getResourceValue(value); 184282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 185282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (r != null) { 186282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski value = r.getValue(); 187282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 188282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 189282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return XmlUtils.convertValueToUnsignedInt(value, defaultValue); 190282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 191282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 192282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return defaultValue; 193282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 194282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 195282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @Override 196282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski public float getAttributeFloatValue(String namespace, String attribute, 197282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski float defaultValue) { 198282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski String s = getAttributeValue(namespace, attribute); 199282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (s != null) { 200282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski ResourceValue r = getResourceValue(s); 201282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 202282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (r != null) { 203282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski s = r.getValue(); 204282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 205282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 206282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return Float.parseFloat(s); 207282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 208282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 209282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return defaultValue; 210282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 211282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 212282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @Override 213282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski public int getAttributeListValue(int index, 214282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski String[] options, int defaultValue) { 215282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return XmlUtils.convertValueToList( 216282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski getAttributeValue(index), options, defaultValue); 217282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 218282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 219282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @Override 220282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski public boolean getAttributeBooleanValue(int index, boolean defaultValue) { 221282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski String value = getAttributeValue(index); 222282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (value != null) { 223282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski ResourceValue r = getResourceValue(value); 224282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 225282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (r != null) { 226282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski value = r.getValue(); 227282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 228282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 229282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return XmlUtils.convertValueToBoolean(value, defaultValue); 230282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 231282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 232282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return defaultValue; 233282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 234282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 235282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @Override 236282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski public int getAttributeResourceValue(int index, int defaultValue) { 237282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski String value = getAttributeValue(index); 238282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 239282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return resolveResourceValue(value, defaultValue); 240282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 241282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 242282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @Override 243282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski public int getAttributeIntValue(int index, int defaultValue) { 244abb080d749638802cef92e2d40faa8b6d9553755Diego Perez return getAttributeIntValue(mParser.getAttributeNamespace(index), 245abb080d749638802cef92e2d40faa8b6d9553755Diego Perez getAttributeName(index) 246abb080d749638802cef92e2d40faa8b6d9553755Diego Perez , defaultValue); 247282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 248282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 249282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @Override 250282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski public int getAttributeUnsignedIntValue(int index, int defaultValue) { 251282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski String value = getAttributeValue(index); 252282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (value != null) { 253282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski ResourceValue r = getResourceValue(value); 254282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 255282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (r != null) { 256282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski value = r.getValue(); 257282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 258282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 259282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return XmlUtils.convertValueToUnsignedInt(value, defaultValue); 260282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 261282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 262282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return defaultValue; 263282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 264282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 265282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski @Override 266282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski public float getAttributeFloatValue(int index, float defaultValue) { 267282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski String s = getAttributeValue(index); 268282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (s != null) { 269282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski ResourceValue r = getResourceValue(s); 270282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 271282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (r != null) { 272282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski s = r.getValue(); 273282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 274282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 275282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return Float.parseFloat(s); 276282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 277282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 278282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return defaultValue; 279282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 280282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 281282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // -- private helper methods 282282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 283282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski /** 284282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Returns a resolved {@link ResourceValue} from a given value. 285282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski */ 286282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski private ResourceValue getResourceValue(String value) { 287282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // now look for this particular value 288282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski RenderResources resources = mContext.getRenderResources(); 289282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return resources.resolveResValue(resources.findResValue(value, mPlatformFile)); 290282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 291282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 292282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski /** 293282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Resolves and return a value to its associated integer. 294282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski */ 295282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski private int resolveResourceValue(String value, int defaultValue) { 296282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski ResourceValue resource = getResourceValue(value); 297282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (resource != null) { 298282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Integer id = null; 299282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (mPlatformFile || resource.isFramework()) { 300282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski id = Bridge.getResourceId(resource.getResourceType(), resource.getName()); 301282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } else { 30237dbb8b7f3c069196040eed3a03006647db7fa5bDeepanshu Gupta id = mContext.getLayoutlibCallback().getResourceId( 303282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski resource.getResourceType(), resource.getName()); 304282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 305282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 306282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (id != null) { 307282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return id; 308282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 309282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 310282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 311282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return defaultValue; 312282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 313282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski} 314