FileDocCategorySizeDatePackage
BridgeResources.javaAPI DocAndroid 5.1 API25046Thu Mar 12 22:22:44 GMT 2015android.content.res

BridgeResources

public final class BridgeResources extends Resources

Fields Summary
private com.android.layoutlib.bridge.android.BridgeContext
mContext
private com.android.ide.common.rendering.api.IProjectCallback
mProjectCallback
private boolean[]
mPlatformResourceFlag
private android.util.TypedValue
mTmpValue
Constructors Summary
private BridgeResources(com.android.layoutlib.bridge.android.BridgeContext context, AssetManager assets, android.util.DisplayMetrics metrics, Configuration config, com.android.ide.common.rendering.api.IProjectCallback projectCallback)

        super(assets, metrics, config);
        mContext = context;
        mProjectCallback = projectCallback;
    
Methods Summary
public static voiddisposeSystem()
Disposes the static {@link Resources#mSystem} to make sure we don't leave objects around that would prevent us from unloading the library.

        if (Resources.mSystem instanceof BridgeResources) {
            ((BridgeResources)(Resources.mSystem)).mContext = null;
            ((BridgeResources)(Resources.mSystem)).mProjectCallback = null;
        }
        Resources.mSystem = null;
    
public XmlResourceParsergetAnimation(int id)

        Pair<String, ResourceValue> v = getResourceValue(id, mPlatformResourceFlag);

        if (v != null) {
            ResourceValue value = v.getSecond();
            XmlPullParser parser = null;

            try {
                File xml = new File(value.getValue());
                if (xml.isFile()) {
                    // we need to create a pull parser around the layout XML file, and then
                    // give that to our XmlBlockParser
                    parser = ParserFactory.create(xml);

                    return new BridgeXmlBlockParser(parser, mContext, mPlatformResourceFlag[0]);
                }
            } catch (XmlPullParserException e) {
                Bridge.getLog().error(LayoutLog.TAG_BROKEN,
                        "Failed to configure parser for " + value.getValue(), e, null /*data*/);
                // we'll return null below.
            } catch (FileNotFoundException e) {
                // this shouldn't happen since we check above.
            }

        }

        // id was not found or not resolved. Throw a NotFoundException.
        throwException(id);

        // this is not used since the method above always throws
        return null;
    
public booleangetBoolean(int id)

        Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag);

        if (value != null) {
            ResourceValue resValue = value.getSecond();

            assert resValue != null;
            if (resValue != null) {
                String v = resValue.getValue();
                if (v != null) {
                    return Boolean.parseBoolean(v);
                }
            }
        }

        // id was not found or not resolved. Throw a NotFoundException.
        throwException(id);

        // this is not used since the method above always throws
        return false;
    
public intgetColor(int id)

        Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag);

        if (value != null) {
            try {
                return ResourceHelper.getColor(value.getSecond().getValue());
            } catch (NumberFormatException e) {
                Bridge.getLog().error(LayoutLog.TAG_RESOURCES_FORMAT, e.getMessage(), e,
                        null /*data*/);
                return 0;
            }
        }

        // id was not found or not resolved. Throw a NotFoundException.
        throwException(id);

        // this is not used since the method above always throws
        return 0;
    
public ColorStateListgetColorStateList(int id)

        Pair<String, ResourceValue> resValue = getResourceValue(id, mPlatformResourceFlag);

        if (resValue != null) {
            ColorStateList stateList = ResourceHelper.getColorStateList(resValue.getSecond(),
                    mContext);
            if (stateList != null) {
                return stateList;
            }
        }

        // id was not found or not resolved. Throw a NotFoundException.
        throwException(id);

        // this is not used since the method above always throws
        return null;
    
public floatgetDimension(int id)

        Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag);

        if (value != null) {
            ResourceValue resValue = value.getSecond();

            assert resValue != null;
            if (resValue != null) {
                String v = resValue.getValue();
                if (v != null) {
                    if (v.equals(BridgeConstants.MATCH_PARENT) ||
                            v.equals(BridgeConstants.FILL_PARENT)) {
                        return LayoutParams.MATCH_PARENT;
                    } else if (v.equals(BridgeConstants.WRAP_CONTENT)) {
                        return LayoutParams.WRAP_CONTENT;
                    }

                    if (ResourceHelper.parseFloatAttribute(
                            value.getFirst(), v, mTmpValue, true /*requireUnit*/) &&
                            mTmpValue.type == TypedValue.TYPE_DIMENSION) {
                        return mTmpValue.getDimension(getDisplayMetrics());
                    }
                }
            }
        }

        // id was not found or not resolved. Throw a NotFoundException.
        throwException(id);

        // this is not used since the method above always throws
        return 0;
    
public intgetDimensionPixelOffset(int id)

        Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag);

        if (value != null) {
            ResourceValue resValue = value.getSecond();

            assert resValue != null;
            if (resValue != null) {
                String v = resValue.getValue();
                if (v != null) {
                    if (ResourceHelper.parseFloatAttribute(
                            value.getFirst(), v, mTmpValue, true /*requireUnit*/) &&
                            mTmpValue.type == TypedValue.TYPE_DIMENSION) {
                        return TypedValue.complexToDimensionPixelOffset(mTmpValue.data,
                                getDisplayMetrics());
                    }
                }
            }
        }

        // id was not found or not resolved. Throw a NotFoundException.
        throwException(id);

        // this is not used since the method above always throws
        return 0;
    
public intgetDimensionPixelSize(int id)

        Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag);

        if (value != null) {
            ResourceValue resValue = value.getSecond();

            assert resValue != null;
            if (resValue != null) {
                String v = resValue.getValue();
                if (v != null) {
                    if (ResourceHelper.parseFloatAttribute(
                            value.getFirst(), v, mTmpValue, true /*requireUnit*/) &&
                            mTmpValue.type == TypedValue.TYPE_DIMENSION) {
                        return TypedValue.complexToDimensionPixelSize(mTmpValue.data,
                                getDisplayMetrics());
                    }
                }
            }
        }

        // id was not found or not resolved. Throw a NotFoundException.
        throwException(id);

        // this is not used since the method above always throws
        return 0;
    
public android.graphics.drawable.DrawablegetDrawable(int id)

        return getDrawable(id, null);
    
public android.graphics.drawable.DrawablegetDrawable(int id, Theme theme)

        Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag);

        if (value != null) {
            return ResourceHelper.getDrawable(value.getSecond(), mContext, theme);
        }

        // id was not found or not resolved. Throw a NotFoundException.
        throwException(id);

        // this is not used since the method above always throws
        return null;
    
public intgetInteger(int id)

        Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag);

        if (value != null) {
            ResourceValue resValue = value.getSecond();

            assert resValue != null;
            if (resValue != null) {
                String v = resValue.getValue();
                if (v != null) {
                    int radix = 10;
                    if (v.startsWith("0x")) {
                        v = v.substring(2);
                        radix = 16;
                    }
                    try {
                        return Integer.parseInt(v, radix);
                    } catch (NumberFormatException e) {
                        // return exception below
                    }
                }
            }
        }

        // id was not found or not resolved. Throw a NotFoundException.
        throwException(id);

        // this is not used since the method above always throws
        return 0;
    
public XmlResourceParsergetLayout(int id)

        Pair<String, ResourceValue> v = getResourceValue(id, mPlatformResourceFlag);

        if (v != null) {
            ResourceValue value = v.getSecond();
            XmlPullParser parser = null;

            try {
                // check if the current parser can provide us with a custom parser.
                if (mPlatformResourceFlag[0] == false) {
                    parser = mProjectCallback.getParser(value);
                }

                // create a new one manually if needed.
                if (parser == null) {
                    File xml = new File(value.getValue());
                    if (xml.isFile()) {
                        // we need to create a pull parser around the layout XML file, and then
                        // give that to our XmlBlockParser
                        parser = ParserFactory.create(xml);
                    }
                }

                if (parser != null) {
                    return new BridgeXmlBlockParser(parser, mContext, mPlatformResourceFlag[0]);
                }
            } catch (XmlPullParserException e) {
                Bridge.getLog().error(LayoutLog.TAG_BROKEN,
                        "Failed to configure parser for " + value.getValue(), e, null /*data*/);
                // we'll return null below.
            } catch (FileNotFoundException e) {
                // this shouldn't happen since we check above.
            }

        }

        // id was not found or not resolved. Throw a NotFoundException.
        throwException(id);

        // this is not used since the method above always throws
        return null;
    
public java.lang.StringgetResourceEntryName(int resid)

        throw new UnsupportedOperationException();
    
public java.lang.StringgetResourceName(int resid)

        throw new UnsupportedOperationException();
    
public java.lang.StringgetResourceTypeName(int resid)

        throw new UnsupportedOperationException();
    
private com.android.util.PairgetResourceValue(int id, boolean[] platformResFlag_out)

        // first get the String related to this id in the framework
        Pair<ResourceType, String> resourceInfo = Bridge.resolveResourceId(id);

        if (resourceInfo != null) {
            platformResFlag_out[0] = true;
            String attributeName = resourceInfo.getSecond();

            return Pair.of(attributeName, mContext.getRenderResources().getFrameworkResource(
                    resourceInfo.getFirst(), attributeName));
        }

        // didn't find a match in the framework? look in the project.
        if (mProjectCallback != null) {
            resourceInfo = mProjectCallback.resolveResourceId(id);

            if (resourceInfo != null) {
                platformResFlag_out[0] = false;
                String attributeName = resourceInfo.getSecond();

                return Pair.of(attributeName, mContext.getRenderResources().getProjectResource(
                        resourceInfo.getFirst(), attributeName));
            }
        }

        return null;
    
public java.lang.StringgetString(int id, java.lang.Object formatArgs)

        String s = getString(id);
        if (s != null) {
            return String.format(s, formatArgs);

        }

        // id was not found or not resolved. Throw a NotFoundException.
        throwException(id);

        // this is not used since the method above always throws
        return null;
    
public java.lang.StringgetString(int id)

        Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag);

        if (value != null && value.getSecond().getValue() != null) {
            return value.getSecond().getValue();
        }

        // id was not found or not resolved. Throw a NotFoundException.
        throwException(id);

        // this is not used since the method above always throws
        return null;
    
public java.lang.CharSequencegetText(int id)

        Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag);

        if (value != null) {
            ResourceValue resValue = value.getSecond();

            assert resValue != null;
            if (resValue != null) {
                String v = resValue.getValue();
                if (v != null) {
                    return v;
                }
            }
        }

        // id was not found or not resolved. Throw a NotFoundException.
        throwException(id);

        // this is not used since the method above always throws
        return null;
    
public voidgetValue(int id, android.util.TypedValue outValue, boolean resolveRefs)

        Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag);

        if (value != null) {
            String v = value.getSecond().getValue();

            if (v != null) {
                if (ResourceHelper.parseFloatAttribute(value.getFirst(), v, outValue,
                        false /*requireUnit*/)) {
                    return;
                }

                // else it's a string
                outValue.type = TypedValue.TYPE_STRING;
                outValue.string = v;
                return;
            }
        }

        // id was not found or not resolved. Throw a NotFoundException.
        throwException(id);
    
public voidgetValue(java.lang.String name, android.util.TypedValue outValue, boolean resolveRefs)

        throw new UnsupportedOperationException();
    
public XmlResourceParsergetXml(int id)

        Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag);

        if (value != null) {
            String v = value.getSecond().getValue();

            if (v != null) {
                // check this is a file
                File f = new File(v);
                if (f.isFile()) {
                    try {
                        XmlPullParser parser = ParserFactory.create(f);

                        return new BridgeXmlBlockParser(parser, mContext, mPlatformResourceFlag[0]);
                    } catch (XmlPullParserException e) {
                        NotFoundException newE = new NotFoundException();
                        newE.initCause(e);
                        throw newE;
                    } catch (FileNotFoundException e) {
                        NotFoundException newE = new NotFoundException();
                        newE.initCause(e);
                        throw newE;
                    }
                }
            }
        }

        // id was not found or not resolved. Throw a NotFoundException.
        throwException(id);

        // this is not used since the method above always throws
        return null;
    
public static ResourcesinitSystem(com.android.layoutlib.bridge.android.BridgeContext context, AssetManager assets, android.util.DisplayMetrics metrics, Configuration config, com.android.ide.common.rendering.api.IProjectCallback projectCallback)
This initializes the static field {@link Resources#mSystem} which is used by methods who get global resources using {@link Resources#getSystem()}.

They will end up using our bridge resources.

{@link Bridge} calls this method after setting up a new bridge.

        return Resources.mSystem = new BridgeResources(context,
                assets,
                metrics,
                config,
                projectCallback);
    
public XmlResourceParserloadXmlResourceParser(java.lang.String file, int id, int assetCookie, java.lang.String type)

        // even though we know the XML file to load directly, we still need to resolve the
        // id so that we can know if it's a platform or project resource.
        // (mPlatformResouceFlag will get the result and will be used later).
        getResourceValue(id, mPlatformResourceFlag);

        File f = new File(file);
        try {
            XmlPullParser parser = ParserFactory.create(f);

            return new BridgeXmlBlockParser(parser, mContext, mPlatformResourceFlag[0]);
        } catch (XmlPullParserException e) {
            NotFoundException newE = new NotFoundException();
            newE.initCause(e);
            throw newE;
        } catch (FileNotFoundException e) {
            NotFoundException newE = new NotFoundException();
            newE.initCause(e);
            throw newE;
        }
    
public BridgeTypedArraynewTypeArray(int numEntries, boolean platformFile)

        return new BridgeTypedArray(this, mContext, numEntries, platformFile);
    
public TypedArrayobtainAttributes(android.util.AttributeSet set, int[] attrs)

        return mContext.obtainStyledAttributes(set, attrs);
    
public TypedArrayobtainTypedArray(int id)

        throw new UnsupportedOperationException();
    
public java.io.InputStreamopenRawResource(int id)

        Pair<String, ResourceValue> value = getResourceValue(id, mPlatformResourceFlag);

        if (value != null) {
            String path = value.getSecond().getValue();

            if (path != null) {
                // check this is a file
                File f = new File(path);
                if (f.isFile()) {
                    try {
                        // if it's a nine-patch return a custom input stream so that
                        // other methods (mainly bitmap factory) can detect it's a 9-patch
                        // and actually load it as a 9-patch instead of a normal bitmap
                        if (path.toLowerCase().endsWith(NinePatch.EXTENSION_9PATCH)) {
                            return new NinePatchInputStream(f);
                        }
                        return new FileInputStream(f);
                    } catch (FileNotFoundException e) {
                        NotFoundException newE = new NotFoundException();
                        newE.initCause(e);
                        throw newE;
                    }
                }
            }
        }

        // id was not found or not resolved. Throw a NotFoundException.
        throwException(id);

        // this is not used since the method above always throws
        return null;
    
public java.io.InputStreamopenRawResource(int id, android.util.TypedValue value)

        getValue(id, value, true);

        String path = value.string.toString();

        File f = new File(path);
        if (f.isFile()) {
            try {
                // if it's a nine-patch return a custom input stream so that
                // other methods (mainly bitmap factory) can detect it's a 9-patch
                // and actually load it as a 9-patch instead of a normal bitmap
                if (path.toLowerCase().endsWith(NinePatch.EXTENSION_9PATCH)) {
                    return new NinePatchInputStream(f);
                }
                return new FileInputStream(f);
            } catch (FileNotFoundException e) {
                NotFoundException exception = new NotFoundException();
                exception.initCause(e);
                throw exception;
            }
        }

        throw new NotFoundException();
    
public AssetFileDescriptoropenRawResourceFd(int id)

        throw new UnsupportedOperationException();
    
private voidthrowException(int id)
Builds and throws a {@link Resources.NotFoundException} based on a resource id and a resource type.

param
id the id of the resource
throws
NotFoundException

        // first get the String related to this id in the framework
        Pair<ResourceType, String> resourceInfo = Bridge.resolveResourceId(id);

        // if the name is unknown in the framework, get it from the custom view loader.
        if (resourceInfo == null && mProjectCallback != null) {
            resourceInfo = mProjectCallback.resolveResourceId(id);
        }

        String message = null;
        if (resourceInfo != null) {
            message = String.format(
                    "Could not find %1$s resource matching value 0x%2$X (resolved name: %3$s) in current configuration.",
                    resourceInfo.getFirst(), id, resourceInfo.getSecond());
        } else {
            message = String.format(
                    "Could not resolve resource value: 0x%1$X.", id);
        }

        throw new NotFoundException(message);