FileDocCategorySizeDatePackage
BridgeResources.javaAPI DocAndroid 1.5 API17107Wed May 06 22:42:02 BST 2009com.android.layoutlib.bridge

BridgeResources

public final class BridgeResources extends android.content.res.Resources

Fields Summary
private BridgeContext
mContext
private com.android.layoutlib.api.IProjectCallback
mProjectCallback
private boolean[]
mPlatformResourceFlag
Constructors Summary
private BridgeResources(BridgeContext context, android.content.res.AssetManager assets, android.util.DisplayMetrics metrics, android.content.res.Configuration config, com.android.layoutlib.api.IProjectCallback projectCallback)

        super(assets, metrics, config);
        mContext = context;
        mProjectCallback = projectCallback;
    
Methods Summary
static voidclearSystem()
Clears 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 intgetColor(int id)

        IResourceValue value = getResourceValue(id, mPlatformResourceFlag);
        
        if (value != null) {
            try {
                return ResourceHelper.getColor(value.getValue());
            } catch (NumberFormatException e) {
                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 android.content.res.ColorStateListgetColorStateList(int id)

        IResourceValue value = getResourceValue(id, mPlatformResourceFlag);
        
        if (value != null) {
            try {
                int color = ResourceHelper.getColor(value.getValue());
                return ColorStateList.valueOf(color);
            } catch (NumberFormatException e) {
                return null;
            }
        }
        
        // 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)

        IResourceValue value = getResourceValue(id, mPlatformResourceFlag);

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

            if (v != null) {
                if (v.equals(BridgeConstants.FILL_PARENT)) {
                    return LayoutParams.FILL_PARENT;
                } else if (v.equals(BridgeConstants.WRAP_CONTENT)) {
                    return LayoutParams.WRAP_CONTENT;
                }
            
                if (ResourceHelper.stringToFloat(v, mTmpValue) &&
                        mTmpValue.type == TypedValue.TYPE_DIMENSION) {
                    return mTmpValue.getDimension(mMetrics);
                }
            }
        }

        // 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)

        IResourceValue value = getResourceValue(id, mPlatformResourceFlag);

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

            if (v != null) {
                if (ResourceHelper.stringToFloat(v, mTmpValue) &&
                        mTmpValue.type == TypedValue.TYPE_DIMENSION) {
                    return TypedValue.complexToDimensionPixelOffset(mTmpValue.data, mMetrics);
                }
            }
        }

        // 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)

        IResourceValue value = getResourceValue(id, mPlatformResourceFlag);

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

            if (v != null) {
                if (ResourceHelper.stringToFloat(v, mTmpValue) &&
                        mTmpValue.type == TypedValue.TYPE_DIMENSION) {
                    return TypedValue.complexToDimensionPixelSize(mTmpValue.data, mMetrics);
                }
            }
        }

        // 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)

        IResourceValue value = getResourceValue(id, mPlatformResourceFlag);
        
        if (value != null) {
            return ResourceHelper.getDrawable(value.getValue(), mContext, value.isFramework());
        }
        
        // 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)

        IResourceValue value = getResourceValue(id, mPlatformResourceFlag);
        
        if (value != null && value.getValue() != null) {
            String v = value.getValue();
            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 android.content.res.XmlResourceParsergetLayout(int id)

        IResourceValue value = getResourceValue(id, mPlatformResourceFlag);
        
        if (value != 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
                try {
                    KXmlParser parser = new KXmlParser();
                    parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
                    parser.setInput(new FileReader(xml));
                    
                    return new BridgeXmlBlockParser(parser, mContext, mPlatformResourceFlag[0]);
                } catch (XmlPullParserException e) {
                    mContext.getLogger().error(e);

                    // 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.layoutlib.api.IResourceValuegetResourceValue(int id, boolean[] platformResFlag_out)

        // first get the String related to this id in the framework
        String[] resourceInfo = Bridge.resolveResourceValue(id);
        
        if (resourceInfo != null) {
            platformResFlag_out[0] = true;
            return mContext.getFrameworkResource(resourceInfo[1], resourceInfo[0]);
        }

        // didn't find a match in the framework? look in the project.
        if (mProjectCallback != null) {
            resourceInfo = mProjectCallback.resolveResourceValue(id);
            
            if (resourceInfo != null) {
                platformResFlag_out[0] = false;
                return mContext.getProjectResource(resourceInfo[1], resourceInfo[0]);
            }
        }

        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)

        IResourceValue value = getResourceValue(id, mPlatformResourceFlag);
        
        if (value != null && value.getValue() != null) {
            return value.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)

        IResourceValue value = getResourceValue(id, mPlatformResourceFlag);
        
        if (value != null) {
            return value.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 voidgetValue(int id, android.util.TypedValue outValue, boolean resolveRefs)

        IResourceValue value = getResourceValue(id, mPlatformResourceFlag);

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

            if (v != null) {
                if (ResourceHelper.stringToFloat(v, outValue)) {
                    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 android.content.res.XmlResourceParsergetXml(int id)

        IResourceValue value = getResourceValue(id, mPlatformResourceFlag);

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

            if (v != null) {
                // check this is a file
                File f = new File(value.getValue());
                if (f.isFile()) {
                    try {
                        KXmlParser parser = new KXmlParser();
                        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
                        parser.setInput(new FileReader(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;
    
static android.content.res.ResourcesinitSystem(BridgeContext context, android.content.res.AssetManager assets, android.util.DisplayMetrics metrics, android.content.res.Configuration config, com.android.layoutlib.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.

    
                                                 
    /*package*/     
             
             
             
              
        if (!(Resources.mSystem instanceof BridgeResources)) {
            Resources.mSystem = new BridgeResources(context,
                    assets,
                    metrics,
                    config,
                    projectCallback);
        }
        return Resources.mSystem;
    
public BridgeTypedArraynewTypeArray(int numEntries, boolean platformFile)

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

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

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

        IResourceValue value = getResourceValue(id, mPlatformResourceFlag);

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

            if (v != null) {
                // check this is a file
                File f = new File(value.getValue());
                if (f.isFile()) {
                    try {
                        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 android.content.res.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
        String[] resourceInfo = Bridge.resolveResourceValue(id);

        // if the name is unknown in the framework, get it from the custom view loader.
        if (resourceInfo == null && mProjectCallback != null) {
            resourceInfo = mProjectCallback.resolveResourceValue(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[1], id, resourceInfo[0]);
        } else {
            message = String.format(
                    "Could not resolve resource value: 0x%1$X.", id);
        }
        
        throw new NotFoundException(message);