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

Bridge

public final class Bridge extends Object implements com.android.layoutlib.api.ILayoutBridge
Main entry point of the LayoutLib Bridge.

To use this bridge, simply instantiate an object of type {@link Bridge} and call {@link #computeLayout(IXmlPullParser, Object, int, int, String, boolean, Map, Map, IProjectCallback, ILayoutLog)}.

Fields Summary
private static final int
DEFAULT_TITLE_BAR_HEIGHT
private static final int
DEFAULT_STATUS_BAR_HEIGHT
private static final Map
sRMap
Maps from id to resource name/type.
private static final Map
sRArrayMap
Same as sRMap except for int[] instead of int resources.
private static final Map
sRFullMap
Reverse map compared to sRMap, resource type -> (resource name -> id)
private static final Map
sProjectBitmapCache
private static final Map
sProject9PatchCache
private static final Map
sFrameworkBitmapCache
private static final Map
sFramework9PatchCache
private static Map
sEnumValueMap
private static final com.android.layoutlib.api.ILayoutLog
sDefaultLogger
A default logger than prints to stdout/stderr.
private static com.android.layoutlib.api.ILayoutLog
sLogger
Logger defined during a compute layout operation.

This logger is generally set to {@link #sDefaultLogger} except during rendering operations when it might be set to a specific provided logger.

To change this value, use a block synchronized on {@link #sDefaultLogger}.

Constructors Summary
Methods Summary
public voidclearCaches(java.lang.Object projectKey)

        if (projectKey != null) {
            sProjectBitmapCache.remove(projectKey);
            sProject9PatchCache.remove(projectKey);
        }
    
public com.android.layoutlib.api.ILayoutResultcomputeLayout(com.android.layoutlib.api.IXmlPullParser layoutDescription, java.lang.Object projectKey, int screenWidth, int screenHeight, java.lang.String themeName, java.util.Map projectResources, java.util.Map frameworkResources, com.android.layoutlib.api.IProjectCallback customViewLoader, com.android.layoutlib.api.ILayoutLog logger)

        boolean isProjectTheme = false;
        if (themeName.charAt(0) == '*") {
            themeName = themeName.substring(1);
            isProjectTheme = true;
        }
        
        return computeLayout(layoutDescription, projectKey,
                screenWidth, screenHeight, DisplayMetrics.DEFAULT_DENSITY,
                DisplayMetrics.DEFAULT_DENSITY, DisplayMetrics.DEFAULT_DENSITY,
                themeName, isProjectTheme,
                projectResources, frameworkResources, customViewLoader, logger);
    
public com.android.layoutlib.api.ILayoutResultcomputeLayout(com.android.layoutlib.api.IXmlPullParser layoutDescription, java.lang.Object projectKey, int screenWidth, int screenHeight, java.lang.String themeName, boolean isProjectTheme, java.util.Map projectResources, java.util.Map frameworkResources, com.android.layoutlib.api.IProjectCallback customViewLoader, com.android.layoutlib.api.ILayoutLog logger)

        return computeLayout(layoutDescription, projectKey,
                screenWidth, screenHeight, DisplayMetrics.DEFAULT_DENSITY,
                DisplayMetrics.DEFAULT_DENSITY, DisplayMetrics.DEFAULT_DENSITY,
                themeName, isProjectTheme,
                projectResources, frameworkResources, customViewLoader, logger);
    
public com.android.layoutlib.api.ILayoutResultcomputeLayout(com.android.layoutlib.api.IXmlPullParser layoutDescription, java.lang.Object projectKey, int screenWidth, int screenHeight, int density, float xdpi, float ydpi, java.lang.String themeName, boolean isProjectTheme, java.util.Map projectResources, java.util.Map frameworkResources, com.android.layoutlib.api.IProjectCallback customViewLoader, com.android.layoutlib.api.ILayoutLog logger)

        if (logger == null) {
            logger = sDefaultLogger;
        }
        
        synchronized (sDefaultLogger) {
            sLogger = logger;
        }

        // find the current theme and compute the style inheritance map
        Map<IStyleResourceValue, IStyleResourceValue> styleParentMap =
            new HashMap<IStyleResourceValue, IStyleResourceValue>();
        
        IStyleResourceValue currentTheme = computeStyleMaps(themeName, isProjectTheme,
                projectResources.get(BridgeConstants.RES_STYLE),
                frameworkResources.get(BridgeConstants.RES_STYLE), styleParentMap);
        
        BridgeContext context = null; 
        try {
            // setup the display Metrics.
            DisplayMetrics metrics = new DisplayMetrics();
            metrics.density = density / (float) DisplayMetrics.DEFAULT_DENSITY;
            metrics.scaledDensity = metrics.density;
            metrics.widthPixels = screenWidth;
            metrics.heightPixels = screenHeight;
            metrics.xdpi = xdpi;
            metrics.ydpi = ydpi;

            context = new BridgeContext(projectKey, metrics, currentTheme, projectResources,
                    frameworkResources, styleParentMap, customViewLoader, logger);
            BridgeInflater inflater = new BridgeInflater(context, customViewLoader);
            context.setBridgeInflater(inflater);
            
            IResourceValue windowBackground = null;
            int screenOffset = 0;
            if (currentTheme != null) {
                windowBackground = context.findItemInStyle(currentTheme, "windowBackground");
                windowBackground = context.resolveResValue(windowBackground);
    
                screenOffset = getScreenOffset(currentTheme, context);
            }
            
            // we need to make sure the Looper has been initialized for this thread.
            // this is required for View that creates Handler objects.
            if (Looper.myLooper() == null) {
                Looper.prepare();
            }
            
            BridgeXmlBlockParser parser = new BridgeXmlBlockParser(layoutDescription,
                    context, false /* platformResourceFlag */);
            
            ViewGroup root = new FrameLayout(context);
        
            View view = inflater.inflate(parser, root);
            
            // set the AttachInfo on the root view.
            AttachInfo info = new AttachInfo(new WindowSession(), new Window(),
                    new Handler(), null);
            info.mHasWindowFocus = true;
            info.mWindowVisibility = View.VISIBLE;
            info.mInTouchMode = false; // this is so that we can display selections.
            root.dispatchAttachedToWindow(info, 0);

            // get the background drawable
            if (windowBackground != null) {
                Drawable d = ResourceHelper.getDrawable(windowBackground.getValue(),
                        context, true /* isFramework */);
                root.setBackgroundDrawable(d);
            }

            int w_spec = MeasureSpec.makeMeasureSpec(screenWidth, MeasureSpec.EXACTLY);
            int h_spec = MeasureSpec.makeMeasureSpec(screenHeight - screenOffset,
                    MeasureSpec.EXACTLY);

            // measure the views
            view.measure(w_spec, h_spec);
            view.layout(0, screenOffset, screenWidth, screenHeight);
            
            // draw them
            BridgeCanvas canvas = new BridgeCanvas(screenWidth, screenHeight - screenOffset,
                    logger);
            
            root.draw(canvas);
            canvas.dispose();
            
            return new LayoutResult(visit(((ViewGroup)view).getChildAt(0), context),
                    canvas.getImage());
        } catch (Throwable e) {
            // get the real cause of the exception.
            Throwable t = e;
            while (t.getCause() != null) {
                t = t.getCause();
            }

            // log it
            logger.error(t);

            // then return with an ERROR status and the message from the real exception
            return new LayoutResult(ILayoutResult.ERROR,
                    t.getClass().getSimpleName() + ": " + t.getMessage());
        } finally {
            // Make sure to remove static references, otherwise we could not unload the lib
            BridgeResources.clearSystem();
            BridgeAssetManager.clearSystem();
            
            // Remove the global logger
            synchronized (sDefaultLogger) {
                sLogger = sDefaultLogger;
            }
        }
    
private voidcomputeStyleInheritance(java.util.Collection styles, java.util.Map inProjectStyleMap, java.util.Map inFrameworkStyleMap, java.util.Map outInheritanceMap)
Compute the parent style for all the styles in a given list.

param
styles the styles for which we compute the parent.
param
inProjectStyleMap the map of project styles.
param
inFrameworkStyleMap the map of framework styles.
param
outInheritanceMap the map of style inheritance. This is filled by the method.

        for (IResourceValue value : styles) {
            if (value instanceof IStyleResourceValue) {
                IStyleResourceValue style = (IStyleResourceValue)value;
                IStyleResourceValue parentStyle = null;

                // first look for a specified parent.
                String parentName = style.getParentStyle();
                
                // no specified parent? try to infer it from the name of the style.
                if (parentName == null) {
                    parentName = getParentName(value.getName());
                }

                if (parentName != null) {
                    parentStyle = getStyle(parentName, inProjectStyleMap, inFrameworkStyleMap);
                    
                    if (parentStyle != null) {
                        outInheritanceMap.put(style, parentStyle);
                    }
                }
            }
        }
    
private com.android.layoutlib.api.IStyleResourceValuecomputeStyleMaps(java.lang.String themeName, boolean isProjectTheme, java.util.Map inProjectStyleMap, java.util.Map inFrameworkStyleMap, java.util.Map outInheritanceMap)
Compute style information from the given list of style for the project and framework.

param
themeName the name of the current theme. In order to differentiate project and platform themes sharing the same name, all project themes must be prepended with a '*' character.
param
isProjectTheme Is this a project theme
param
inProjectStyleMap the project style map
param
inFrameworkStyleMap the framework style map
param
outInheritanceMap the map of style inheritance. This is filled by the method
return
the {@link IStyleResourceValue} matching themeName

        
        if (inProjectStyleMap != null && inFrameworkStyleMap != null) {
            // first, get the theme
            IResourceValue theme = null;
            
            // project theme names have been prepended with a *
            if (isProjectTheme) {
                theme = inProjectStyleMap.get(themeName);
            } else {
                theme = inFrameworkStyleMap.get(themeName);
            }
            
            if (theme instanceof IStyleResourceValue) {
                // compute the inheritance map for both the project and framework styles
                computeStyleInheritance(inProjectStyleMap.values(), inProjectStyleMap,
                        inFrameworkStyleMap, outInheritanceMap);
    
                // Compute the style inheritance for the framework styles/themes.
                // Since, for those, the style parent values do not contain 'android:'
                // we want to force looking in the framework style only to avoid using
                // similarly named styles from the project.
                // To do this, we pass null in lieu of the project style map.
                computeStyleInheritance(inFrameworkStyleMap.values(), null /*inProjectStyleMap */,
                        inFrameworkStyleMap, outInheritanceMap);
    
                return (IStyleResourceValue)theme;
            }
        }
        
        return null;
    
public intgetApiLevel()


    /*
     * (non-Javadoc)
     * @see com.android.layoutlib.api.ILayoutBridge#getApiLevel()
     */
       
        return API_CURRENT;
    
static com.android.ninepatch.NinePatchgetCached9Patch(java.lang.String value, java.lang.Object projectKey)
Returns the 9 patch for a specific path, from a specific project cache, or from the framework cache.

param
value the path of the 9 patch
param
projectKey the key of the project, or null to query the framework cache.
return
the cached 9 patch or null if not found.

        if (projectKey != null) {
            Map<String, SoftReference<NinePatch>> map = sProject9PatchCache.get(projectKey);
            
            if (map != null) {
                SoftReference<NinePatch> ref = map.get(value);
                if (ref != null) {
                    return ref.get();
                }
            }
        } else {
            SoftReference<NinePatch> ref = sFramework9PatchCache.get(value);
            if (ref != null) {
                return ref.get();
            }
        }
        
        return null;
    
static android.graphics.BitmapgetCachedBitmap(java.lang.String value, java.lang.Object projectKey)
Returns the bitmap for a specific path, from a specific project cache, or from the framework cache.

param
value the path of the bitmap
param
projectKey the key of the project, or null to query the framework cache.
return
the cached Bitmap or null if not found.

        if (projectKey != null) {
            Map<String, SoftReference<Bitmap>> map = sProjectBitmapCache.get(projectKey);
            if (map != null) {
                SoftReference<Bitmap> ref = map.get(value);
                if (ref != null) {
                    return ref.get();
                }
            }
        } else {
            SoftReference<Bitmap> ref = sFrameworkBitmapCache.get(value);
            if (ref != null) {
                return ref.get();
            }
        }

        return null;
    
static java.util.MapgetEnumValues(java.lang.String attributeName)

        if (sEnumValueMap != null) {
            return sEnumValueMap.get(attributeName);
        }
        
        return null;
    
private java.lang.StringgetParentName(java.lang.String styleName)
Computes the name of the parent style, or null if the style is a root style.

        int index = styleName.lastIndexOf('.");
        if (index != -1) {
            return styleName.substring(0, index);
        }
        
        return null;
    
public static java.lang.IntegergetResourceValue(java.lang.String type, java.lang.String name)
Returns the integer id of a framework resource, from a given resource type and resource name.

param
type the type of the resource
param
name the name of the resource.
return
an {@link Integer} containing the resource id, or null if no resource were found.

        Map<String, Integer> map = sRFullMap.get(type);
        if (map != null) {
            return map.get(name);
        }
        
        return null;
    
private intgetScreenOffset(com.android.layoutlib.api.IStyleResourceValue currentTheme, BridgeContext context)
Returns the top screen offset. This depends on whether the current theme defines the user of the title and status bars.

return
the pixel height offset

        int offset = 0;

        // get the title bar flag from the current theme.
        IResourceValue value = context.findItemInStyle(currentTheme, "windowNoTitle");
        
        // because it may reference something else, we resolve it.
        value = context.resolveResValue(value);

        // if there's a value and it's true (default is false)
        if (value == null || value.getValue() == null ||
                XmlUtils.convertValueToBoolean(value.getValue(), false /* defValue */) == false) {
            // get value from the theme.
            value = context.findItemInStyle(currentTheme, "windowTitleSize");
            
            // resolve it
            value = context.resolveResValue(value);
            
            // default value
            offset = DEFAULT_TITLE_BAR_HEIGHT;

            // get the real value;
            if (value != null) {
                TypedValue typedValue = ResourceHelper.getValue(value.getValue());
                if (typedValue != null) {
                    offset = (int)typedValue.getDimension(context.getResources().mMetrics);   
                }
            }
        }
        
        // get the fullscreen flag from the current theme.
        value = context.findItemInStyle(currentTheme, "windowFullscreen");
        
        // because it may reference something else, we resolve it.
        value = context.resolveResValue(value);
        
        if (value == null || value.getValue() == null ||
                XmlUtils.convertValueToBoolean(value.getValue(), false /* defValue */) == false) {
            // FIXME: Right now this is hard-coded in the platform, but once there's a constant, we'll need to use it.
            offset += DEFAULT_STATUS_BAR_HEIGHT;
        }

        return offset;
    
private com.android.layoutlib.api.IStyleResourceValuegetStyle(java.lang.String parentName, java.util.Map inProjectStyleMap, java.util.Map inFrameworkStyleMap)
Searches for and returns the {@link IStyleResourceValue} from a given name.

The format of the name can be:

  • [android:]<name>
  • [android:]style/<name>
  • @[android:]style/<name>

param
parentName the name of the style.
param
inProjectStyleMap the project style map. Can be null
param
inFrameworkStyleMap the framework style map.
return
The matching {@link IStyleResourceValue} object or null if not found.

        boolean frameworkOnly = false;
        
        String name = parentName;
        
        // remove the useless @ if it's there
        if (name.startsWith(BridgeConstants.PREFIX_RESOURCE_REF)) {
            name = name.substring(BridgeConstants.PREFIX_RESOURCE_REF.length());
        }
        
        // check for framework identifier.
        if (name.startsWith(BridgeConstants.PREFIX_ANDROID)) {
            frameworkOnly = true;
            name = name.substring(BridgeConstants.PREFIX_ANDROID.length());
        }
        
        // at this point we could have the format style/<name>. we want only the name
        if (name.startsWith(BridgeConstants.REFERENCE_STYLE)) {
            name = name.substring(BridgeConstants.REFERENCE_STYLE.length());
        }

        IResourceValue parent = null;
        
        // if allowed, search in the project resources.
        if (frameworkOnly == false && inProjectStyleMap != null) {
            parent = inProjectStyleMap.get(name);
        }

        // if not found, then look in the framework resources.
        if (parent == null) {
            parent = inFrameworkStyleMap.get(name);
        }
        
        // make sure the result is the proper class type and return it.
        if (parent instanceof IStyleResourceValue) {
            return (IStyleResourceValue)parent;
        }
        
        sLogger.error(String.format("Unable to resolve parent style name: ", parentName));
        
        return null;
    
public booleaninit(java.lang.String fontOsLocation, java.util.Map enumValueMap)


        return sinit(fontOsLocation, enumValueMap);
    
public static java.lang.String[]resolveResourceValue(int value)
Returns details of a framework resource from its integer value.

param
value the integer value
return
an array of 2 strings containing the resource name and type, or null if the id does not match any resource.

        return sRMap.get(value);
        
    
public static java.lang.StringresolveResourceValue(int[] array)
Returns the name of a framework resource whose value is an int array.

param
array

        return sRArrayMap.get(array);
    
static voidsetCached9Patch(java.lang.String value, com.android.ninepatch.NinePatch ninePatch, java.lang.Object projectKey)
Sets a 9 patch in a project cache or in the framework cache.

param
value the path of the 9 patch
param
ninePatch the 9 patch object
param
projectKey the key of the project, or null to put the bitmap in the framework cache.

        if (projectKey != null) {
            Map<String, SoftReference<NinePatch>> map = sProject9PatchCache.get(projectKey);

            if (map == null) {
                map = new HashMap<String, SoftReference<NinePatch>>();
                sProject9PatchCache.put(projectKey, map);
            }
            
            map.put(value, new SoftReference<NinePatch>(ninePatch));
        } else {
            sFramework9PatchCache.put(value, new SoftReference<NinePatch>(ninePatch));
        }
    
static voidsetCachedBitmap(java.lang.String value, android.graphics.Bitmap bmp, java.lang.Object projectKey)
Sets a bitmap in a project cache or in the framework cache.

param
value the path of the bitmap
param
bmp the Bitmap object
param
projectKey the key of the project, or null to put the bitmap in the framework cache.

        if (projectKey != null) {
            Map<String, SoftReference<Bitmap>> map = sProjectBitmapCache.get(projectKey);

            if (map == null) {
                map = new HashMap<String, SoftReference<Bitmap>>();
                sProjectBitmapCache.put(projectKey, map);
            }
            
            map.put(value, new SoftReference<Bitmap>(bmp));
        } else {
            sFrameworkBitmapCache.put(value, new SoftReference<Bitmap>(bmp));
        }
    
private static synchronized booleansinit(java.lang.String fontOsLocation, java.util.Map enumValueMap)


        // When DEBUG_LAYOUT is set and is not 0 or false, setup a default listener
        // on static (native) methods which prints the signature on the console and
        // throws an exception.
        // This is useful when testing the rendering in ADT to identify static native 
        // methods that are ignored -- layoutlib_create makes them returns 0/false/null
        // which is generally OK yet might be a problem, so this is how you'd find out.
        //
        // Currently layoutlib_create only overrides static native method.
        // Static non-natives are not overridden and thus do not get here.
        final String debug = System.getenv("DEBUG_LAYOUT");
        if (debug != null && !debug.equals("0") && !debug.equals("false")) {

            OverrideMethod.setDefaultListener(new MethodAdapter() {
                @Override
                public void onInvokeV(String signature, boolean isNative, Object caller) {
                    if (sLogger != null) {
                        synchronized (sDefaultLogger) {
                            sLogger.error("Missing Stub: " + signature +
                                    (isNative ? " (native)" : ""));
                        }
                    }

                    if (debug.equalsIgnoreCase("throw")) {
                        // Throwing this exception doesn't seem that useful. It breaks
                        // the layout editor yet doesn't display anything meaningful to the
                        // user. Having the error in the console is just as useful. We'll
                        // throw it only if the environment variable is "throw" or "THROW".
                        throw new StaticMethodNotImplementedException(signature);
                    }
                }
            });
        }

        // Override View.isInEditMode to return true.
        //
        // This allows custom views that are drawn in the Graphical Layout Editor to adapt their
        // rendering for preview. Most important this let custom views know that they can't expect
        // the rest of their activities to be alive.
        OverrideMethod.setMethodListener("android.view.View#isInEditMode()Z",
            new MethodAdapter() {
                @Override
                public int onInvokeI(String signature, boolean isNative, Object caller) {
                    return 1;
                }
            }
        );

        // load the fonts.
        FontLoader fontLoader = FontLoader.create(fontOsLocation);
        if (fontLoader != null) {
            Typeface.init(fontLoader);
        } else {
            return false;
        }
        
        sEnumValueMap = enumValueMap;

        // now parse com.android.internal.R (and only this one as android.R is a subset of
        // the internal version), and put the content in the maps.
        try {
            // WARNING: this only works because the class is already loaded, and therefore
            // the objects returned by Field.get() are the same as the ones used by
            // the code accessing the R class.
            // int[] does not implement equals/hashCode, and if the parsing used a different class
            // loader for the R class, this would NOT work.
            Class<?> r = com.android.internal.R.class;
            
            for (Class<?> inner : r.getDeclaredClasses()) {
                String resType = inner.getSimpleName();

                Map<String, Integer> fullMap = new HashMap<String, Integer>();
                sRFullMap.put(resType, fullMap);
                
                for (Field f : inner.getDeclaredFields()) {
                    // only process static final fields. Since the final attribute may have
                    // been altered by layoutlib_create, we only check static
                    int modifiers = f.getModifiers();
                    if (Modifier.isStatic(modifiers)) {
                        Class<?> type = f.getType();
                        if (type.isArray() && type.getComponentType() == int.class) {
                            // if the object is an int[] we put it in sRArrayMap
                            sRArrayMap.put((int[]) f.get(null), f.getName());
                        } else if (type == int.class) {
                            Integer value = (Integer) f.get(null); 
                            sRMap.put(value, new String[] { f.getName(), resType });
                            fullMap.put(f.getName(), value);
                        } else {
                            assert false;
                        }
                    }
                }
            }
        } catch (IllegalArgumentException e) {
            // FIXME: log/return the error (there's no logger object at this point!)
            e.printStackTrace();
            return false;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return false;
        }

        return true;
    
private com.android.layoutlib.api.ILayoutResult.ILayoutViewInfovisit(android.view.View view, BridgeContext context)
Visits a View and its children and generate a {@link ILayoutViewInfo} containing the bounds of all the views.

param
view the root View
param
context the context.

        if (view == null) {
            return null;
        }

        LayoutViewInfo result = new LayoutViewInfo(view.getClass().getName(),
                context.getViewKey(view),
                view.getLeft(), view.getTop(), view.getRight(), view.getBottom());

        if (view instanceof ViewGroup) {
            ViewGroup group = ((ViewGroup) view);
            int n = group.getChildCount();
            ILayoutViewInfo[] children = new ILayoutViewInfo[n];
            for (int i = 0; i < group.getChildCount(); i++) {
                children[i] = visit(group.getChildAt(i), context);
            }
            result.setChildren(children);
        }

        return result;