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 com.android.layoutlib.bridge.impl; 18282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 195f1bf496948fb6383ae17e5467a26a00489d7564Deepanshu Guptaimport com.android.SdkConstants; 20282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.ide.common.rendering.api.DensityBasedResourceValue; 21282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.ide.common.rendering.api.LayoutLog; 22282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.ide.common.rendering.api.RenderResources; 23282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.ide.common.rendering.api.ResourceValue; 24fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Guptaimport com.android.internal.util.XmlUtils; 25282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.layoutlib.bridge.Bridge; 26282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.layoutlib.bridge.android.BridgeContext; 27282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.layoutlib.bridge.android.BridgeXmlBlockParser; 28cd6e47e4455cec8daa4570ac8073d93bd567d139Deepanshu Guptaimport com.android.layoutlib.bridge.android.RenderParamsFlags; 29282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.ninepatch.NinePatch; 30282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.ninepatch.NinePatchChunk; 31282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport com.android.resources.Density; 32282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 33282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport org.xmlpull.v1.XmlPullParser; 34282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport org.xmlpull.v1.XmlPullParserException; 35282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 36476e582d2ffdf25102d4c55f8c242baa3d21d37fDeepanshu Guptaimport android.annotation.NonNull; 37566b303365078fac9a454f1595add19e02631db3Diego Perezimport android.annotation.Nullable; 38282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.content.res.ColorStateList; 39566b303365078fac9a454f1595add19e02631db3Diego Perezimport android.content.res.ComplexColor; 40566b303365078fac9a454f1595add19e02631db3Diego Perezimport android.content.res.ComplexColor_Accessor; 41566b303365078fac9a454f1595add19e02631db3Diego Perezimport android.content.res.GradientColor; 424a605c6fa0027ee116beb29fbc9625721f0441f0Deepanshu Guptaimport android.content.res.Resources.Theme; 43282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.graphics.Bitmap; 44282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.graphics.Bitmap_Delegate; 45282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.graphics.NinePatch_Delegate; 46282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.graphics.Rect; 47282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.graphics.drawable.BitmapDrawable; 48282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.graphics.drawable.ColorDrawable; 49282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.graphics.drawable.Drawable; 50282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.graphics.drawable.NinePatchDrawable; 51282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport android.util.TypedValue; 52282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 53282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport java.io.File; 54282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport java.io.FileInputStream; 55566b303365078fac9a454f1595add19e02631db3Diego Perezimport java.io.FileNotFoundException; 56282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport java.io.IOException; 57282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport java.io.InputStream; 58282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport java.net.MalformedURLException; 59282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport java.util.regex.Matcher; 60282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskiimport java.util.regex.Pattern; 61282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 62282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski/** 63282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Helper class to provide various conversion method used in handling android resources. 64282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski */ 65282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinskipublic final class ResourceHelper { 66282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 67282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski private final static Pattern sFloatPattern = Pattern.compile("(-?[0-9]+(?:\\.[0-9]+)?)(.*)"); 68282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski private final static float[] sFloatOut = new float[1]; 69282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 70282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski private final static TypedValue mValue = new TypedValue(); 71282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 72282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski /** 73282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Returns the color value represented by the given string value 74282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @param value the color value 75282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @return the color as an int 761a12b805698c7cba9c2daa78c76c055afbdfa9fcDeepanshu Gupta * @throws NumberFormatException if the conversion failed. 77282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski */ 78282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski public static int getColor(String value) { 79282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (value != null) { 801a12b805698c7cba9c2daa78c76c055afbdfa9fcDeepanshu Gupta if (!value.startsWith("#")) { 815f1bf496948fb6383ae17e5467a26a00489d7564Deepanshu Gupta if (value.startsWith(SdkConstants.PREFIX_THEME_REF)) { 825f1bf496948fb6383ae17e5467a26a00489d7564Deepanshu Gupta throw new NumberFormatException(String.format( 835f1bf496948fb6383ae17e5467a26a00489d7564Deepanshu Gupta "Attribute '%s' not found. Are you using the right theme?", value)); 845f1bf496948fb6383ae17e5467a26a00489d7564Deepanshu Gupta } 85282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski throw new NumberFormatException( 86282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski String.format("Color value '%s' must start with #", value)); 87282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 88282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 89282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski value = value.substring(1); 90282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 91282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // make sure it's not longer than 32bit 92282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (value.length() > 8) { 93282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski throw new NumberFormatException(String.format( 94282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski "Color value '%s' is too long. Format is either" + 95282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski "#AARRGGBB, #RRGGBB, #RGB, or #ARGB", 96282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski value)); 97282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 98282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 99282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (value.length() == 3) { // RGB format 100282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski char[] color = new char[8]; 101282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski color[0] = color[1] = 'F'; 102282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski color[2] = color[3] = value.charAt(0); 103282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski color[4] = color[5] = value.charAt(1); 104282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski color[6] = color[7] = value.charAt(2); 105282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski value = new String(color); 106282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } else if (value.length() == 4) { // ARGB format 107282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski char[] color = new char[8]; 108282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski color[0] = color[1] = value.charAt(0); 109282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski color[2] = color[3] = value.charAt(1); 110282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski color[4] = color[5] = value.charAt(2); 111282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski color[6] = color[7] = value.charAt(3); 112282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski value = new String(color); 113282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } else if (value.length() == 6) { 114282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski value = "FF" + value; 115282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 116282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 117282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // this is a RRGGBB or AARRGGBB value 118282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 119282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // Integer.parseInt will fail to parse strings like "ff191919", so we use 120282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // a Long, but cast the result back into an int, since we know that we're only 121282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // dealing with 32 bit values. 122282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return (int)Long.parseLong(value, 16); 123282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 124282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 125282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski throw new NumberFormatException(); 126282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 127282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 128566b303365078fac9a454f1595add19e02631db3Diego Perez /** 129566b303365078fac9a454f1595add19e02631db3Diego Perez * Returns a {@link ComplexColor} from the given {@link ResourceValue} 130566b303365078fac9a454f1595add19e02631db3Diego Perez * 131566b303365078fac9a454f1595add19e02631db3Diego Perez * @param resValue the value containing a color value or a file path to a complex color 132566b303365078fac9a454f1595add19e02631db3Diego Perez * definition 133566b303365078fac9a454f1595add19e02631db3Diego Perez * @param context the current context 134566b303365078fac9a454f1595add19e02631db3Diego Perez * @param theme the theme to use when resolving the complex color 135566b303365078fac9a454f1595add19e02631db3Diego Perez * @param allowGradients when false, only {@link ColorStateList} will be returned. If a {@link 136566b303365078fac9a454f1595add19e02631db3Diego Perez * GradientColor} is found, null will be returned. 137566b303365078fac9a454f1595add19e02631db3Diego Perez */ 138566b303365078fac9a454f1595add19e02631db3Diego Perez @Nullable 139566b303365078fac9a454f1595add19e02631db3Diego Perez private static ComplexColor getInternalComplexColor(@NonNull ResourceValue resValue, 140566b303365078fac9a454f1595add19e02631db3Diego Perez @NonNull BridgeContext context, @Nullable Theme theme, boolean allowGradients) { 141282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski String value = resValue.getValue(); 142566b303365078fac9a454f1595add19e02631db3Diego Perez if (value == null || RenderResources.REFERENCE_NULL.equals(value)) { 143566b303365078fac9a454f1595add19e02631db3Diego Perez return null; 144566b303365078fac9a454f1595add19e02631db3Diego Perez } 145566b303365078fac9a454f1595add19e02631db3Diego Perez 146cd6e47e4455cec8daa4570ac8073d93bd567d139Deepanshu Gupta XmlPullParser parser = null; 147566b303365078fac9a454f1595add19e02631db3Diego Perez // first check if the value is a file (xml most likely) 148cd6e47e4455cec8daa4570ac8073d93bd567d139Deepanshu Gupta Boolean psiParserSupport = context.getLayoutlibCallback().getFlag( 149cd6e47e4455cec8daa4570ac8073d93bd567d139Deepanshu Gupta RenderParamsFlags.FLAG_KEY_XML_FILE_PARSER_SUPPORT); 150cd6e47e4455cec8daa4570ac8073d93bd567d139Deepanshu Gupta if (psiParserSupport != null && psiParserSupport) { 151cd6e47e4455cec8daa4570ac8073d93bd567d139Deepanshu Gupta parser = context.getLayoutlibCallback().getXmlFileParser(value); 152cd6e47e4455cec8daa4570ac8073d93bd567d139Deepanshu Gupta } 153566b303365078fac9a454f1595add19e02631db3Diego Perez if (parser == null) { 154282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski File f = new File(value); 155282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (f.isFile()) { 156566b303365078fac9a454f1595add19e02631db3Diego Perez // let the framework inflate the color from the XML file, by 157566b303365078fac9a454f1595add19e02631db3Diego Perez // providing an XmlPullParser 158282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski try { 159566b303365078fac9a454f1595add19e02631db3Diego Perez parser = ParserFactory.create(f); 160566b303365078fac9a454f1595add19e02631db3Diego Perez } catch (XmlPullParserException | FileNotFoundException e) { 161282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bridge.getLog().error(LayoutLog.TAG_RESOURCES_READ, 162282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski "Failed to parse file " + value, e, null /*data*/); 163282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 164566b303365078fac9a454f1595add19e02631db3Diego Perez } 165566b303365078fac9a454f1595add19e02631db3Diego Perez } 166566b303365078fac9a454f1595add19e02631db3Diego Perez 167566b303365078fac9a454f1595add19e02631db3Diego Perez if (parser != null) { 168566b303365078fac9a454f1595add19e02631db3Diego Perez try { 169566b303365078fac9a454f1595add19e02631db3Diego Perez BridgeXmlBlockParser blockParser = new BridgeXmlBlockParser( 170566b303365078fac9a454f1595add19e02631db3Diego Perez parser, context, resValue.isFramework()); 171282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski try { 172566b303365078fac9a454f1595add19e02631db3Diego Perez // Advance the parser to the first element so we can detect if it's a 173566b303365078fac9a454f1595add19e02631db3Diego Perez // color list or a gradient color 174566b303365078fac9a454f1595add19e02631db3Diego Perez int type; 175566b303365078fac9a454f1595add19e02631db3Diego Perez //noinspection StatementWithEmptyBody 176566b303365078fac9a454f1595add19e02631db3Diego Perez while ((type = blockParser.next()) != XmlPullParser.START_TAG 177566b303365078fac9a454f1595add19e02631db3Diego Perez && type != XmlPullParser.END_DOCUMENT) { 178566b303365078fac9a454f1595add19e02631db3Diego Perez // Seek parser to start tag. 179566b303365078fac9a454f1595add19e02631db3Diego Perez } 180566b303365078fac9a454f1595add19e02631db3Diego Perez 181566b303365078fac9a454f1595add19e02631db3Diego Perez if (type != XmlPullParser.START_TAG) { 182566b303365078fac9a454f1595add19e02631db3Diego Perez throw new XmlPullParserException("No start tag found"); 183566b303365078fac9a454f1595add19e02631db3Diego Perez } 184566b303365078fac9a454f1595add19e02631db3Diego Perez 185566b303365078fac9a454f1595add19e02631db3Diego Perez final String name = blockParser.getName(); 186566b303365078fac9a454f1595add19e02631db3Diego Perez if (allowGradients && "gradient".equals(name)) { 187566b303365078fac9a454f1595add19e02631db3Diego Perez return ComplexColor_Accessor.createGradientColorFromXmlInner( 188566b303365078fac9a454f1595add19e02631db3Diego Perez context.getResources(), 189566b303365078fac9a454f1595add19e02631db3Diego Perez blockParser, blockParser, 190566b303365078fac9a454f1595add19e02631db3Diego Perez theme); 191566b303365078fac9a454f1595add19e02631db3Diego Perez } else if ("selector".equals(name)) { 192566b303365078fac9a454f1595add19e02631db3Diego Perez return ComplexColor_Accessor.createColorStateListFromXmlInner( 193566b303365078fac9a454f1595add19e02631db3Diego Perez context.getResources(), 194566b303365078fac9a454f1595add19e02631db3Diego Perez blockParser, blockParser, 195566b303365078fac9a454f1595add19e02631db3Diego Perez theme); 196566b303365078fac9a454f1595add19e02631db3Diego Perez } 197566b303365078fac9a454f1595add19e02631db3Diego Perez } finally { 198566b303365078fac9a454f1595add19e02631db3Diego Perez blockParser.ensurePopped(); 199282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 200566b303365078fac9a454f1595add19e02631db3Diego Perez } catch (XmlPullParserException e) { 201566b303365078fac9a454f1595add19e02631db3Diego Perez Bridge.getLog().error(LayoutLog.TAG_BROKEN, 202566b303365078fac9a454f1595add19e02631db3Diego Perez "Failed to configure parser for " + value, e, null /*data*/); 203566b303365078fac9a454f1595add19e02631db3Diego Perez // we'll return null below. 204566b303365078fac9a454f1595add19e02631db3Diego Perez } catch (Exception e) { 205566b303365078fac9a454f1595add19e02631db3Diego Perez // this is an error and not warning since the file existence is 206566b303365078fac9a454f1595add19e02631db3Diego Perez // checked before attempting to parse it. 207566b303365078fac9a454f1595add19e02631db3Diego Perez Bridge.getLog().error(LayoutLog.TAG_RESOURCES_READ, 208566b303365078fac9a454f1595add19e02631db3Diego Perez "Failed to parse file " + value, e, null /*data*/); 209566b303365078fac9a454f1595add19e02631db3Diego Perez 210566b303365078fac9a454f1595add19e02631db3Diego Perez return null; 211566b303365078fac9a454f1595add19e02631db3Diego Perez } 212566b303365078fac9a454f1595add19e02631db3Diego Perez } else { 213566b303365078fac9a454f1595add19e02631db3Diego Perez // try to load the color state list from an int 214566b303365078fac9a454f1595add19e02631db3Diego Perez try { 215566b303365078fac9a454f1595add19e02631db3Diego Perez int color = getColor(value); 216566b303365078fac9a454f1595add19e02631db3Diego Perez return ColorStateList.valueOf(color); 217566b303365078fac9a454f1595add19e02631db3Diego Perez } catch (NumberFormatException e) { 218566b303365078fac9a454f1595add19e02631db3Diego Perez Bridge.getLog().error(LayoutLog.TAG_RESOURCES_FORMAT, 219566b303365078fac9a454f1595add19e02631db3Diego Perez "Failed to convert " + value + " into a ColorStateList", e, 220566b303365078fac9a454f1595add19e02631db3Diego Perez null /*data*/); 221282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 222282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 223282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 224282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return null; 225282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 226282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 227282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski /** 228566b303365078fac9a454f1595add19e02631db3Diego Perez * Returns a {@link ColorStateList} from the given {@link ResourceValue} 229566b303365078fac9a454f1595add19e02631db3Diego Perez * 230566b303365078fac9a454f1595add19e02631db3Diego Perez * @param resValue the value containing a color value or a file path to a complex color 231566b303365078fac9a454f1595add19e02631db3Diego Perez * definition 232566b303365078fac9a454f1595add19e02631db3Diego Perez * @param context the current context 233566b303365078fac9a454f1595add19e02631db3Diego Perez */ 234566b303365078fac9a454f1595add19e02631db3Diego Perez @Nullable 235566b303365078fac9a454f1595add19e02631db3Diego Perez public static ColorStateList getColorStateList(@NonNull ResourceValue resValue, 236566b303365078fac9a454f1595add19e02631db3Diego Perez @NonNull BridgeContext context) { 237566b303365078fac9a454f1595add19e02631db3Diego Perez return (ColorStateList) getInternalComplexColor(resValue, context, context.getTheme(), 238566b303365078fac9a454f1595add19e02631db3Diego Perez false); 239566b303365078fac9a454f1595add19e02631db3Diego Perez } 240566b303365078fac9a454f1595add19e02631db3Diego Perez 241566b303365078fac9a454f1595add19e02631db3Diego Perez /** 242566b303365078fac9a454f1595add19e02631db3Diego Perez * Returns a {@link ComplexColor} from the given {@link ResourceValue} 243566b303365078fac9a454f1595add19e02631db3Diego Perez * 244566b303365078fac9a454f1595add19e02631db3Diego Perez * @param resValue the value containing a color value or a file path to a complex color 245566b303365078fac9a454f1595add19e02631db3Diego Perez * definition 246566b303365078fac9a454f1595add19e02631db3Diego Perez * @param context the current context 247566b303365078fac9a454f1595add19e02631db3Diego Perez */ 248566b303365078fac9a454f1595add19e02631db3Diego Perez @Nullable 249566b303365078fac9a454f1595add19e02631db3Diego Perez public static ComplexColor getComplexColor(@NonNull ResourceValue resValue, 250566b303365078fac9a454f1595add19e02631db3Diego Perez @NonNull BridgeContext context) { 251566b303365078fac9a454f1595add19e02631db3Diego Perez return getInternalComplexColor(resValue, context, context.getTheme(), true); 252566b303365078fac9a454f1595add19e02631db3Diego Perez } 253566b303365078fac9a454f1595add19e02631db3Diego Perez 254566b303365078fac9a454f1595add19e02631db3Diego Perez /** 255282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Returns a drawable from the given value. 256282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @param value The value that contains a path to a 9 patch, a bitmap or a xml based drawable, 257282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * or an hexadecimal color 258282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @param context the current context 259282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski */ 260282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski public static Drawable getDrawable(ResourceValue value, BridgeContext context) { 2614a605c6fa0027ee116beb29fbc9625721f0441f0Deepanshu Gupta return getDrawable(value, context, null); 2624a605c6fa0027ee116beb29fbc9625721f0441f0Deepanshu Gupta } 2634a605c6fa0027ee116beb29fbc9625721f0441f0Deepanshu Gupta 2644a605c6fa0027ee116beb29fbc9625721f0441f0Deepanshu Gupta /** 2654a605c6fa0027ee116beb29fbc9625721f0441f0Deepanshu Gupta * Returns a drawable from the given value. 2664a605c6fa0027ee116beb29fbc9625721f0441f0Deepanshu Gupta * @param value The value that contains a path to a 9 patch, a bitmap or a xml based drawable, 2674a605c6fa0027ee116beb29fbc9625721f0441f0Deepanshu Gupta * or an hexadecimal color 2684a605c6fa0027ee116beb29fbc9625721f0441f0Deepanshu Gupta * @param context the current context 2694a605c6fa0027ee116beb29fbc9625721f0441f0Deepanshu Gupta * @param theme the theme to be used to inflate the drawable. 2704a605c6fa0027ee116beb29fbc9625721f0441f0Deepanshu Gupta */ 2714a605c6fa0027ee116beb29fbc9625721f0441f0Deepanshu Gupta public static Drawable getDrawable(ResourceValue value, BridgeContext context, Theme theme) { 27214bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta if (value == null) { 27314bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta return null; 27414bf0cef7eeed572a67c29a328581afac4decc20Deepanshu Gupta } 275282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski String stringValue = value.getValue(); 276282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (RenderResources.REFERENCE_NULL.equals(stringValue)) { 277282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return null; 278282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 279282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 280282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski String lowerCaseValue = stringValue.toLowerCase(); 281282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 282282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Density density = Density.MEDIUM; 283282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (value instanceof DensityBasedResourceValue) { 284282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski density = 285282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski ((DensityBasedResourceValue)value).getResourceDensity(); 286282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 287282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 288282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 289282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (lowerCaseValue.endsWith(NinePatch.EXTENSION_9PATCH)) { 290282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski File file = new File(stringValue); 291282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (file.isFile()) { 292282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski try { 293282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return getNinePatchDrawable( 294282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski new FileInputStream(file), density, value.isFramework(), 295282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski stringValue, context); 296282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } catch (IOException e) { 297282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // failed to read the file, we'll return null below. 298282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bridge.getLog().error(LayoutLog.TAG_RESOURCES_READ, 299282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski "Failed lot load " + file.getAbsolutePath(), e, null /*data*/); 300282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 301282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 302282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 303282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return null; 304282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } else if (lowerCaseValue.endsWith(".xml")) { 305282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // create a block parser for the file 306282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski File f = new File(stringValue); 307282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (f.isFile()) { 308282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski try { 309282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // let the framework inflate the Drawable from the XML file. 310282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski XmlPullParser parser = ParserFactory.create(f); 311282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 312282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski BridgeXmlBlockParser blockParser = new BridgeXmlBlockParser( 313282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski parser, context, value.isFramework()); 314282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski try { 3154a605c6fa0027ee116beb29fbc9625721f0441f0Deepanshu Gupta return Drawable.createFromXml(context.getResources(), blockParser, theme); 316282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } finally { 317282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski blockParser.ensurePopped(); 318282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 319282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } catch (Exception e) { 320282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // this is an error and not warning since the file existence is checked before 321282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // attempting to parse it. 322282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bridge.getLog().error(null, "Failed to parse file " + stringValue, 323282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski e, null /*data*/); 324282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 325282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } else { 326282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bridge.getLog().error(LayoutLog.TAG_BROKEN, 327282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski String.format("File %s does not exist (or is not a file)", stringValue), 328282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski null /*data*/); 329282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 330282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 331282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return null; 332282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } else { 333282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski File bmpFile = new File(stringValue); 334282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (bmpFile.isFile()) { 335282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski try { 336282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bitmap bitmap = Bridge.getCachedBitmap(stringValue, 337282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski value.isFramework() ? null : context.getProjectKey()); 338282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 339282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (bitmap == null) { 340282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski bitmap = Bitmap_Delegate.createBitmap(bmpFile, false /*isMutable*/, 341282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski density); 342282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bridge.setCachedBitmap(stringValue, bitmap, 343282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski value.isFramework() ? null : context.getProjectKey()); 344282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 345282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 346282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return new BitmapDrawable(context.getResources(), bitmap); 347282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } catch (IOException e) { 348282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // we'll return null below 349282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bridge.getLog().error(LayoutLog.TAG_RESOURCES_READ, 350282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski "Failed lot load " + bmpFile.getAbsolutePath(), e, null /*data*/); 351282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 352282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } else { 353282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // attempt to get a color from the value 354282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski try { 355282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski int color = getColor(stringValue); 356282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return new ColorDrawable(color); 357282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } catch (NumberFormatException e) { 358282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // we'll return null below. 359282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bridge.getLog().error(LayoutLog.TAG_RESOURCES_FORMAT, 360282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski "Failed to convert " + stringValue + " into a drawable", e, 361282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski null /*data*/); 362282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 363282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 364282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 365282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 366282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return null; 367282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 368282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 369282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski private static Drawable getNinePatchDrawable(InputStream inputStream, Density density, 370282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski boolean isFramework, String cacheKey, BridgeContext context) throws IOException { 371282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // see if we still have both the chunk and the bitmap in the caches 372282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski NinePatchChunk chunk = Bridge.getCached9Patch(cacheKey, 373282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski isFramework ? null : context.getProjectKey()); 374282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bitmap bitmap = Bridge.getCachedBitmap(cacheKey, 375282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski isFramework ? null : context.getProjectKey()); 376282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 377282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // if either chunk or bitmap is null, then we reload the 9-patch file. 378282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (chunk == null || bitmap == null) { 379282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski try { 380282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski NinePatch ninePatch = NinePatch.load(inputStream, true /*is9Patch*/, 381282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski false /* convert */); 382282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (ninePatch != null) { 383282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (chunk == null) { 384282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski chunk = ninePatch.getChunk(); 385282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 386282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bridge.setCached9Patch(cacheKey, chunk, 387282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski isFramework ? null : context.getProjectKey()); 388282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 389282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 390282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (bitmap == null) { 391282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski bitmap = Bitmap_Delegate.createBitmap(ninePatch.getImage(), 392282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski false /*isMutable*/, 393282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski density); 394282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 395282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bridge.setCachedBitmap(cacheKey, bitmap, 396282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski isFramework ? null : context.getProjectKey()); 397282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 398282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 399282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } catch (MalformedURLException e) { 400282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // URL is wrong, we'll return null below 401282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 402282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 403282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 404282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (chunk != null && bitmap != null) { 405282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski int[] padding = chunk.getPadding(); 406282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Rect paddingRect = new Rect(padding[0], padding[1], padding[2], padding[3]); 407282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 408282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return new NinePatchDrawable(context.getResources(), bitmap, 409282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski NinePatch_Delegate.serialize(chunk), 410282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski paddingRect, null); 411282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 412282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 413282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return null; 414282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 415282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 416fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta /** 417fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta * Looks for an attribute in the current theme. 418fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta * 419fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta * @param resources the render resources 420fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta * @param name the name of the attribute 421fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta * @param defaultValue the default value. 422fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta * @param isFrameworkAttr if the attribute is in android namespace 423fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta * @return the value of the attribute or the default one if not found. 424fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta */ 425fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta public static boolean getBooleanThemeValue(@NonNull RenderResources resources, String name, 426fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta boolean isFrameworkAttr, boolean defaultValue) { 427fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta ResourceValue value = resources.findItemInTheme(name, isFrameworkAttr); 428fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta value = resources.resolveResValue(value); 429fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta if (value == null) { 430fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta return defaultValue; 431fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta } 432fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta return XmlUtils.convertValueToBoolean(value.getValue(), defaultValue); 433fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta } 434fc8f4aad7395eca11f6d9b82eb266b1f4ee5041bDeepanshu Gupta 435282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // ------- TypedValue stuff 436282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // This is taken from //device/libs/utils/ResourceTypes.cpp 437282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 438282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski private static final class UnitEntry { 439282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski String name; 440282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski int type; 441282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski int unit; 442282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski float scale; 443282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 444282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski UnitEntry(String name, int type, int unit, float scale) { 445282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski this.name = name; 446282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski this.type = type; 447282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski this.unit = unit; 448282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski this.scale = scale; 449282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 450282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 451282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 452282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski private final static UnitEntry[] sUnitNames = new UnitEntry[] { 453282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski new UnitEntry("px", TypedValue.TYPE_DIMENSION, TypedValue.COMPLEX_UNIT_PX, 1.0f), 454282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski new UnitEntry("dip", TypedValue.TYPE_DIMENSION, TypedValue.COMPLEX_UNIT_DIP, 1.0f), 455282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski new UnitEntry("dp", TypedValue.TYPE_DIMENSION, TypedValue.COMPLEX_UNIT_DIP, 1.0f), 456282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski new UnitEntry("sp", TypedValue.TYPE_DIMENSION, TypedValue.COMPLEX_UNIT_SP, 1.0f), 457282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski new UnitEntry("pt", TypedValue.TYPE_DIMENSION, TypedValue.COMPLEX_UNIT_PT, 1.0f), 458282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski new UnitEntry("in", TypedValue.TYPE_DIMENSION, TypedValue.COMPLEX_UNIT_IN, 1.0f), 459282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski new UnitEntry("mm", TypedValue.TYPE_DIMENSION, TypedValue.COMPLEX_UNIT_MM, 1.0f), 460282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski new UnitEntry("%", TypedValue.TYPE_FRACTION, TypedValue.COMPLEX_UNIT_FRACTION, 1.0f/100), 461282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski new UnitEntry("%p", TypedValue.TYPE_FRACTION, TypedValue.COMPLEX_UNIT_FRACTION_PARENT, 1.0f/100), 462282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski }; 463282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 464282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski /** 465282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Returns the raw value from the given attribute float-type value string. 466282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * This object is only valid until the next call on to {@link ResourceHelper}. 467282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski */ 468282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski public static TypedValue getValue(String attribute, String value, boolean requireUnit) { 469282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (parseFloatAttribute(attribute, value, mValue, requireUnit)) { 470282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return mValue; 471282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 472282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 473282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return null; 474282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 475282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 476282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski /** 477282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * Parse a float attribute and return the parsed value into a given TypedValue. 478282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @param attribute the name of the attribute. Can be null if <var>requireUnit</var> is false. 479282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @param value the string value of the attribute 480282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @param outValue the TypedValue to receive the parsed value 481282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @param requireUnit whether the value is expected to contain a unit. 482282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski * @return true if success. 483282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski */ 484171804201dd556f8ce7ee7618661a5c8ee71507aDeepanshu Gupta public static boolean parseFloatAttribute(String attribute, @NonNull String value, 485282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski TypedValue outValue, boolean requireUnit) { 4861a12b805698c7cba9c2daa78c76c055afbdfa9fcDeepanshu Gupta assert !requireUnit || attribute != null; 487282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 488282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // remove the space before and after 489282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski value = value.trim(); 490282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski int len = value.length(); 491282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 492282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (len <= 0) { 493282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return false; 494282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 495282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 496282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // check that there's no non ascii characters. 497282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski char[] buf = value.toCharArray(); 498282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski for (int i = 0 ; i < len ; i++) { 499282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (buf[i] > 255) { 500282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return false; 501282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 502282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 503282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 504282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // check the first character 505458f2a7bbbcc45d12b575fe9f3b2f8e1a20f9b1cDeepanshu Gupta if ((buf[0] < '0' || buf[0] > '9') && buf[0] != '.' && buf[0] != '-' && buf[0] != '+') { 506282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return false; 507282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 508282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 509282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // now look for the string that is after the float... 510282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Matcher m = sFloatPattern.matcher(value); 511282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (m.matches()) { 512282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski String f_str = m.group(1); 513282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski String end = m.group(2); 514282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 515282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski float f; 516282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski try { 517282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski f = Float.parseFloat(f_str); 518282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } catch (NumberFormatException e) { 519282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // this shouldn't happen with the regexp above. 520282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return false; 521282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 522282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 523282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (end.length() > 0 && end.charAt(0) != ' ') { 524282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // Might be a unit... 525282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (parseUnit(end, outValue, sFloatOut)) { 526282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski computeTypedValue(outValue, f, sFloatOut[0]); 527282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return true; 528282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 529282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return false; 530282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 531282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 532282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // make sure it's only spaces at the end. 533282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski end = end.trim(); 534282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 535282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (end.length() == 0) { 536282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (outValue != null) { 5371a12b805698c7cba9c2daa78c76c055afbdfa9fcDeepanshu Gupta if (!requireUnit) { 538282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski outValue.type = TypedValue.TYPE_FLOAT; 539282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski outValue.data = Float.floatToIntBits(f); 540282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } else { 541282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // no unit when required? Use dp and out an error. 542282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski applyUnit(sUnitNames[1], outValue, sFloatOut); 543282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski computeTypedValue(outValue, f, sFloatOut[0]); 544282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 545282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski Bridge.getLog().error(LayoutLog.TAG_RESOURCES_RESOLVE, 546282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski String.format( 547282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski "Dimension \"%1$s\" in attribute \"%2$s\" is missing unit!", 548282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski value, attribute), 549282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski null); 550282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 551282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return true; 552282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 553282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 554282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 555282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 556282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return false; 557282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 558282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 559282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski private static void computeTypedValue(TypedValue outValue, float value, float scale) { 560282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski value *= scale; 561282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski boolean neg = value < 0; 562282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (neg) { 563282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski value = -value; 564282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 565282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski long bits = (long)(value*(1<<23)+.5f); 566282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski int radix; 567282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski int shift; 568282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if ((bits&0x7fffff) == 0) { 569282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // Always use 23p0 if there is no fraction, just to make 570282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // things easier to read. 571282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski radix = TypedValue.COMPLEX_RADIX_23p0; 572282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski shift = 23; 573282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } else if ((bits&0xffffffffff800000L) == 0) { 574282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // Magnitude is zero -- can fit in 0 bits of precision. 575282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski radix = TypedValue.COMPLEX_RADIX_0p23; 576282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski shift = 0; 577282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } else if ((bits&0xffffffff80000000L) == 0) { 578282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // Magnitude can fit in 8 bits of precision. 579282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski radix = TypedValue.COMPLEX_RADIX_8p15; 580282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski shift = 8; 581282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } else if ((bits&0xffffff8000000000L) == 0) { 582282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // Magnitude can fit in 16 bits of precision. 583282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski radix = TypedValue.COMPLEX_RADIX_16p7; 584282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski shift = 16; 585282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } else { 586282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski // Magnitude needs entire range, so no fractional part. 587282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski radix = TypedValue.COMPLEX_RADIX_23p0; 588282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski shift = 23; 589282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 590282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski int mantissa = (int)( 591282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski (bits>>shift) & TypedValue.COMPLEX_MANTISSA_MASK); 592282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (neg) { 593282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski mantissa = (-mantissa) & TypedValue.COMPLEX_MANTISSA_MASK; 594282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 595282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski outValue.data |= 596282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski (radix<<TypedValue.COMPLEX_RADIX_SHIFT) 597282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski | (mantissa<<TypedValue.COMPLEX_MANTISSA_SHIFT); 598282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 599282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 600282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski private static boolean parseUnit(String str, TypedValue outValue, float[] outScale) { 601282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski str = str.trim(); 602282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 603282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski for (UnitEntry unit : sUnitNames) { 604282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski if (unit.name.equals(str)) { 605282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski applyUnit(unit, outValue, outScale); 606282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return true; 607282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 608282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 609282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 610282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski return false; 611282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 612282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 613282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski private static void applyUnit(UnitEntry unit, TypedValue outValue, float[] outScale) { 614282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski outValue.type = unit.type; 6151a12b805698c7cba9c2daa78c76c055afbdfa9fcDeepanshu Gupta // COMPLEX_UNIT_SHIFT is 0 and hence intelliJ complains about it. Suppress the warning. 6161a12b805698c7cba9c2daa78c76c055afbdfa9fcDeepanshu Gupta //noinspection PointlessBitwiseExpression 617282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski outValue.data = unit.unit << TypedValue.COMPLEX_UNIT_SHIFT; 618282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski outScale[0] = unit.scale; 619282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski } 620282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski} 621282e181b58cf72b6ca770dc7ca5f91f135444502Adam Lesinski 622