com.android.ide.common.rendering.api
Class RenderResources

java.lang.Object
  extended by com.android.ide.common.rendering.api.RenderResources

public class RenderResources
extends java.lang.Object

A class containing all the resources needed to do a rendering.

This contains both the project specific resources and the framework resources, and provide convenience methods to resolve resource and theme references.


Nested Class Summary
static class RenderResources.FrameworkResourceIdProvider
           
 
Field Summary
static java.lang.String REFERENCE_NULL
           
 
Constructor Summary
RenderResources()
           
 
Method Summary
 void applyStyle(StyleResourceValue theme, boolean useAsPrimary)
          Use this theme to resolve resources.
 void clearStyles()
          Clear all the themes applied with applyStyle(StyleResourceValue, boolean)
 ResourceValue findItemInStyle(StyleResourceValue style, java.lang.String attrName)
          Deprecated. Use findItemInStyle(StyleResourceValue, String, boolean) since this method doesn't know the item namespace.
 ResourceValue findItemInStyle(StyleResourceValue style, java.lang.String attrName, boolean isFrameworkAttr)
          Returns the ResourceValue matching a given attribute in a given style.
 ResourceValue findItemInTheme(java.lang.String itemName)
          Deprecated. Use findItemInTheme(String, boolean)
 ResourceValue findItemInTheme(java.lang.String attrName, boolean isFrameworkAttr)
          Returns the ResourceValue matching a given name in the all themes returned by getAllThemes().
 ResourceValue findResValue(java.lang.String reference, boolean forceFrameworkOnly)
          Searches for, and returns a ResourceValue by its reference.
 java.util.List<StyleResourceValue> getAllThemes()
          Returns a list of StyleResourceValue containing a list of themes to be used for resolving resources.
 StyleResourceValue getCurrentTheme()
          Deprecated. Use getDefaultTheme() or getAllThemes()
 StyleResourceValue getDefaultTheme()
          Returns the StyleResourceValue representing the default theme.
 ResourceValue getFrameworkResource(ResourceType resourceType, java.lang.String resourceName)
          Returns a framework resource by type and name.
 StyleResourceValue getParent(StyleResourceValue style)
          Returns the parent style of the given style, if any
 ResourceValue getProjectResource(ResourceType resourceType, java.lang.String resourceName)
          Returns a project resource by type and name.
 StyleResourceValue getStyle(java.lang.String styleName, boolean isFramework)
          Returns the style matching the given name.
 StyleResourceValue getTheme(java.lang.String name, boolean frameworkTheme)
          Returns a theme by its name.
 java.lang.String getXmlName(ResourceType type, java.lang.String name, boolean isFramework)
          Returns the name of the resources as written in the XML.
 ResourceValue resolveResValue(ResourceValue value)
          Returns the ResourceValue referenced by the value of value.
 ResourceValue resolveValue(ResourceType type, java.lang.String name, java.lang.String value, boolean isFrameworkValue)
          Resolves the value of a resource, if the value references a theme or resource value.
 void setFrameworkResourceIdProvider(RenderResources.FrameworkResourceIdProvider provider)
           
 void setLogger(LayoutLog logger)
           
 boolean themeIsParentOf(StyleResourceValue parentTheme, StyleResourceValue childTheme)
          Returns whether a theme is a parent of a given theme.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REFERENCE_NULL

public static final java.lang.String REFERENCE_NULL
See Also:
Constant Field Values
Constructor Detail

RenderResources

public RenderResources()
Method Detail

setFrameworkResourceIdProvider

public void setFrameworkResourceIdProvider(RenderResources.FrameworkResourceIdProvider provider)

setLogger

public void setLogger(LayoutLog logger)

getCurrentTheme

@Deprecated
public StyleResourceValue getCurrentTheme()
Deprecated. Use getDefaultTheme() or getAllThemes()

Returns the StyleResourceValue representing the current theme.

Returns:
the theme or null if there is no current theme.

getDefaultTheme

public StyleResourceValue getDefaultTheme()
Returns the StyleResourceValue representing the default theme.


applyStyle

public void applyStyle(StyleResourceValue theme,
                       boolean useAsPrimary)
Use this theme to resolve resources.

Remember to call clearStyles() to clear the applied styles, so the default theme may be restored.

Parameters:
theme - The style to use for resource resolution in addition to the the default theme and the styles applied earlier. If null, the operation is a no-op.
useAsPrimary - If true, the theme is used first to resolve attributes. If false, the theme is used if the resource cannot be resolved using the default theme and all the themes that have been applied prior to this call.

clearStyles

public void clearStyles()
Clear all the themes applied with applyStyle(StyleResourceValue, boolean)


getAllThemes

public java.util.List<StyleResourceValue> getAllThemes()
Returns a list of StyleResourceValue containing a list of themes to be used for resolving resources. The order of the themes in the list specifies the order in which they should be used to resolve resources.


getTheme

public StyleResourceValue getTheme(java.lang.String name,
                                   boolean frameworkTheme)
Returns a theme by its name.

Parameters:
name - the name of the theme
frameworkTheme - whether the theme is a framework theme.
Returns:
the theme or null if there's no match

themeIsParentOf

public boolean themeIsParentOf(StyleResourceValue parentTheme,
                               StyleResourceValue childTheme)
Returns whether a theme is a parent of a given theme.

Parameters:
parentTheme - the parent theme
childTheme - the child theme.
Returns:
true if the parent theme is indeed a parent theme of the child theme.

getFrameworkResource

public ResourceValue getFrameworkResource(ResourceType resourceType,
                                          java.lang.String resourceName)
Returns a framework resource by type and name. The returned resource is resolved.

Parameters:
resourceType - the type of the resource
resourceName - the name of the resource

getProjectResource

public ResourceValue getProjectResource(ResourceType resourceType,
                                        java.lang.String resourceName)
Returns a project resource by type and name. The returned resource is resolved.

Parameters:
resourceType - the type of the resource
resourceName - the name of the resource

findItemInTheme

@Deprecated
public ResourceValue findItemInTheme(java.lang.String itemName)
Deprecated. Use findItemInTheme(String, boolean)

Returns the ResourceValue matching a given name in the all themes returned by getAllThemes(). If the item is not directly available in the a theme, its parent theme is used before checking the next theme from the list.

Parameters:
itemName - the name of the item to search for.
Returns:
the ResourceValue object or null

findItemInTheme

public ResourceValue findItemInTheme(java.lang.String attrName,
                                     boolean isFrameworkAttr)
Returns the ResourceValue matching a given name in the all themes returned by getAllThemes(). If the item is not directly available in the a theme, its parent theme is used before checking the next theme from the list.

Parameters:
attrName - the name of the attribute to search for.
isFrameworkAttr - whether the attribute is a framework attribute
Returns:
the ResourceValue object or null

findItemInStyle

@Deprecated
public ResourceValue findItemInStyle(StyleResourceValue style,
                                                java.lang.String attrName)
Deprecated. Use findItemInStyle(StyleResourceValue, String, boolean) since this method doesn't know the item namespace.

Returns the ResourceValue matching a given name in a given style. If the item is not directly available in the style, the method looks in its parent style. This version of doesn't support providing the namespace of the attribute so it'll search in both the project's namespace and then in the android namespace.

Parameters:
style - the style to search in
attrName - the name of the attribute to search for.
Returns:
the ResourceValue object or null

findItemInStyle

public ResourceValue findItemInStyle(StyleResourceValue style,
                                     java.lang.String attrName,
                                     boolean isFrameworkAttr)
Returns the ResourceValue matching a given attribute in a given style. If the item is not directly available in the style, the method looks in its parent style.

Parameters:
style - the style to search in
attrName - the name of the attribute to search for.
isFrameworkAttr - whether the attribute is a framework attribute
Returns:
the ResourceValue object or null

findResValue

public ResourceValue findResValue(java.lang.String reference,
                                  boolean forceFrameworkOnly)
Searches for, and returns a ResourceValue by its reference.

The reference format can be:

@resType/resName
@android:resType/resName
@resType/android:resName
?resType/resName
?android:resType/resName
?resType/android:resName
Any other string format will return null.

The actual format of a reference is

@[namespace:]resType/resName
but this method only support the android namespace.

Parameters:
reference - the resource reference to search for.
forceFrameworkOnly - if true all references are considered to be toward framework resource even if the reference does not include the android: prefix.
Returns:
a ResourceValue or null.

resolveValue

public ResourceValue resolveValue(ResourceType type,
                                  java.lang.String name,
                                  java.lang.String value,
                                  boolean isFrameworkValue)
Resolves the value of a resource, if the value references a theme or resource value.

This method ensures that it returns a ResourceValue object that does not reference another resource. If the resource cannot be resolved, it returns null.

If a value that does not need to be resolved is given, the method will return a new instance of ResourceValue that contains the input value.

Parameters:
type - the type of the resource
name - the name of the attribute containing this value.
value - the resource value, or reference to resolve
isFrameworkValue - whether the value is a framework value.
Returns:
the resolved resource value or null if it failed to resolve it.

resolveResValue

public ResourceValue resolveResValue(ResourceValue value)
Returns the ResourceValue referenced by the value of value.

This method ensures that it returns a ResourceValue object that does not reference another resource. If the resource cannot be resolved, it returns null.

If a value that does not need to be resolved is given, the method will return the input value.

Parameters:
value - the value containing the reference to resolve.
Returns:
a ResourceValue object or null

getParent

public StyleResourceValue getParent(StyleResourceValue style)
Returns the parent style of the given style, if any

Parameters:
style - the style to look up
Returns:
the parent style, or null

getStyle

public StyleResourceValue getStyle(java.lang.String styleName,
                                   boolean isFramework)
Returns the style matching the given name. The name should not contain any namespace prefix.

Parameters:
styleName - Name of the style. For example, "Widget.ListView.DropDown".
Returns:
the {link StyleResourceValue} for the style, or null if not found.

getXmlName

public java.lang.String getXmlName(ResourceType type,
                                   java.lang.String name,
                                   boolean isFramework)
Returns the name of the resources as written in the XML. This undos the flattening of some characters to '_' as done by aapt.

The method is not guaranteed to be implemented on the IDE side. In such a case, the method will return null.

Parameters:
type - the type of the resource
name - the name of the resource that may have been obtained from the R class.
isFramework - whether the resource is a framework resource.
Returns:
the name as written in the XML or null if not implemented for the resource type.