Search in sources :

Example 6 with ResourceValue

use of com.android.ide.common.rendering.api.ResourceValue in project android_frameworks_base by ParanoidAndroid.

the class BridgeTypedArray method getResourceId.

/**
     * Retrieve the resource identifier for the attribute at
     * <var>index</var>.  Note that attribute resource as resolved when
     * the overall {@link TypedArray} object is retrieved.  As a
     * result, this function will return the resource identifier of the
     * final resource value that was found, <em>not</em> necessarily the
     * original resource that was specified by the attribute.
     *
     * @param index Index of attribute to retrieve.
     * @param defValue Value to return if the attribute is not defined or
     *                 not a resource.
     *
     * @return Attribute resource identifier, or defValue if not defined.
     */
@Override
public int getResourceId(int index, int defValue) {
    if (index < 0 || index >= mResourceData.length) {
        return defValue;
    }
    // get the Resource for this index
    ResourceValue resValue = mResourceData[index];
    // no data, return the default value.
    if (resValue == null) {
        return defValue;
    }
    // check if this is a style resource
    if (resValue instanceof StyleResourceValue) {
        // get the id that will represent this style.
        return mContext.getDynamicIdByStyle((StyleResourceValue) resValue);
    }
    if (RenderResources.REFERENCE_NULL.equals(resValue.getValue())) {
        return defValue;
    }
    // (and getValue() returning null!). We need to handle this!
    if (resValue.getResourceType() != null) {
        // if this is a framework id
        if (mPlatformFile || resValue.isFramework()) {
            // look for idName in the android R classes
            return mContext.getFrameworkResourceValue(resValue.getResourceType(), resValue.getName(), defValue);
        }
        // look for idName in the project R class.
        return mContext.getProjectResourceValue(resValue.getResourceType(), resValue.getName(), defValue);
    }
    // else, try to get the value, and resolve it somehow.
    String value = resValue.getValue();
    if (value == null) {
        return defValue;
    }
    // if the value is just an integer, return it.
    try {
        int i = Integer.parseInt(value);
        if (Integer.toString(i).equals(value)) {
            return i;
        }
    } catch (NumberFormatException e) {
    // pass
    }
    // if this is a reference to an id, find it.
    if (value.startsWith("@id/") || value.startsWith("@+") || value.startsWith("@android:id/")) {
        int pos = value.indexOf('/');
        String idName = value.substring(pos + 1);
        // if this is a framework id
        if (mPlatformFile || value.startsWith("@android") || value.startsWith("@+android")) {
            // look for idName in the android R classes
            return mContext.getFrameworkResourceValue(ResourceType.ID, idName, defValue);
        }
        // look for idName in the project R class.
        return mContext.getProjectResourceValue(ResourceType.ID, idName, defValue);
    }
    // not a direct id valid reference? resolve it
    Integer idValue = null;
    if (resValue.isFramework()) {
        idValue = Bridge.getResourceId(resValue.getResourceType(), resValue.getName());
    } else {
        idValue = mContext.getProjectCallback().getResourceId(resValue.getResourceType(), resValue.getName());
    }
    if (idValue != null) {
        return idValue.intValue();
    }
    Bridge.getLog().warning(LayoutLog.TAG_RESOURCES_RESOLVE, String.format("Unable to resolve id \"%1$s\" for attribute \"%2$s\"", value, mNames[index]), resValue);
    return defValue;
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) AttrResourceValue(com.android.ide.common.rendering.api.AttrResourceValue) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue)

Example 7 with ResourceValue

use of com.android.ide.common.rendering.api.ResourceValue in project android_frameworks_base by ParanoidAndroid.

the class RenderSessionImpl method getBooleanThemeValue.

/**
     * Looks for a attribute in the current theme. The attribute is in the android
     * namespace.
     *
     * @param resources the render resources
     * @param name the name of the attribute
     * @param defaultValue the default value.
     * @return the value of the attribute or the default one if not found.
     */
private boolean getBooleanThemeValue(RenderResources resources, String name, boolean defaultValue) {
    // get the title bar flag from the current theme.
    ResourceValue value = resources.findItemInTheme(name, true);
    // because it may reference something else, we resolve it.
    value = resources.resolveResValue(value);
    // if there's no value, return the default.
    if (value == null || value.getValue() == null) {
        return defaultValue;
    }
    return XmlUtils.convertValueToBoolean(value.getValue(), defaultValue);
}
Also used : ResourceValue(com.android.ide.common.rendering.api.ResourceValue)

Example 8 with ResourceValue

use of com.android.ide.common.rendering.api.ResourceValue in project android_frameworks_base by ParanoidAndroid.

the class RenderSessionImpl method animate.

/**
     * Animate an object
     * <p>
     * {@link #acquire(long)} must have been called before this.
     *
     * @throws IllegalStateException if the current context is different than the one owned by
     *      the scene, or if {@link #acquire(long)} was not called.
     *
     * @see RenderSession#animate(Object, String, boolean, IAnimationListener)
     */
public Result animate(Object targetObject, String animationName, boolean isFrameworkAnimation, IAnimationListener listener) {
    checkLock();
    BridgeContext context = getContext();
    // find the animation file.
    ResourceValue animationResource = null;
    int animationId = 0;
    if (isFrameworkAnimation) {
        animationResource = context.getRenderResources().getFrameworkResource(ResourceType.ANIMATOR, animationName);
        if (animationResource != null) {
            animationId = Bridge.getResourceId(ResourceType.ANIMATOR, animationName);
        }
    } else {
        animationResource = context.getRenderResources().getProjectResource(ResourceType.ANIMATOR, animationName);
        if (animationResource != null) {
            animationId = context.getProjectCallback().getResourceId(ResourceType.ANIMATOR, animationName);
        }
    }
    if (animationResource != null) {
        try {
            Animator anim = AnimatorInflater.loadAnimator(context, animationId);
            if (anim != null) {
                anim.setTarget(targetObject);
                new PlayAnimationThread(anim, this, animationName, listener).start();
                return SUCCESS.createResult();
            }
        } catch (Exception e) {
            // get the real cause of the exception.
            Throwable t = e;
            while (t.getCause() != null) {
                t = t.getCause();
            }
            return ERROR_UNKNOWN.createResult(t.getMessage(), t);
        }
    }
    return ERROR_ANIM_NOT_FOUND.createResult();
}
Also used : Animator(android.animation.Animator) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 9 with ResourceValue

use of com.android.ide.common.rendering.api.ResourceValue in project android_frameworks_base by ParanoidAndroid.

the class RenderSessionImpl method findNavigationBar.

private void findNavigationBar(RenderResources resources, DisplayMetrics metrics) {
    if (hasSoftwareButtons() && mWindowIsFloating == false) {
        // default value
        // ??
        mNavigationBarSize = 48;
        HardwareConfig hardwareConfig = getParams().getHardwareConfig();
        boolean barOnBottom = true;
        if (hardwareConfig.getOrientation() == ScreenOrientation.LANDSCAPE) {
            // compute the dp of the screen.
            int shortSize = hardwareConfig.getScreenHeight();
            // compute in dp
            int shortSizeDp = shortSize * DisplayMetrics.DENSITY_DEFAULT / hardwareConfig.getDensity().getDpiValue();
            if (shortSizeDp < 600) {
                // 0-599dp: "phone" UI with bar on the side
                barOnBottom = false;
            } else {
                // 600+dp: "tablet" UI with bar on the bottom
                barOnBottom = true;
            }
        }
        if (barOnBottom) {
            mNavigationBarOrientation = LinearLayout.HORIZONTAL;
        } else {
            mNavigationBarOrientation = LinearLayout.VERTICAL;
        }
        // get the real value
        ResourceValue value = resources.getFrameworkResource(ResourceType.DIMEN, barOnBottom ? "navigation_bar_height" : "navigation_bar_width");
        if (value != null) {
            TypedValue typedValue = ResourceHelper.getValue("navigation_bar_height", value.getValue(), true);
            if (typedValue != null) {
                // compute the pixel value based on the display metrics
                mNavigationBarSize = (int) typedValue.getDimension(metrics);
            }
        }
    }
}
Also used : HardwareConfig(com.android.ide.common.rendering.api.HardwareConfig) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) TypedValue(android.util.TypedValue)

Example 10 with ResourceValue

use of com.android.ide.common.rendering.api.ResourceValue in project android_frameworks_base by ParanoidAndroid.

the class BridgeContext method createStyleBasedTypedArray.

// ------------- private new methods
/**
     * Creates a {@link BridgeTypedArray} by filling the values defined by the int[] with the
     * values found in the given style.
     * @see #obtainStyledAttributes(int, int[])
     */
private BridgeTypedArray createStyleBasedTypedArray(StyleResourceValue style, int[] attrs) throws Resources.NotFoundException {
    List<Pair<String, Boolean>> attributes = searchAttrs(attrs);
    BridgeTypedArray ta = ((BridgeResources) mSystemResources).newTypeArray(attrs.length, false);
    // for each attribute, get its name so that we can search it in the style
    for (int i = 0; i < attrs.length; i++) {
        Pair<String, Boolean> attribute = attributes.get(i);
        if (attribute != null) {
            // look for the value in the given style
            ResourceValue resValue = mRenderResources.findItemInStyle(style, attribute.getFirst(), attribute.getSecond());
            if (resValue != null) {
                // resolve it to make sure there are no references left.
                ta.bridgeSetValue(i, attribute.getFirst(), attribute.getSecond(), mRenderResources.resolveResValue(resValue));
            }
        }
    }
    ta.sealArray();
    return ta;
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) BridgeTypedArray(android.content.res.BridgeTypedArray) Pair(com.android.util.Pair) BridgeResources(android.content.res.BridgeResources)

Aggregations

ResourceValue (com.android.ide.common.rendering.api.ResourceValue)243 StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)82 ArrayResourceValue (com.android.ide.common.rendering.api.ArrayResourceValue)80 DensityBasedResourceValue (com.android.ide.common.rendering.api.DensityBasedResourceValue)69 LayoutlibDelegate (com.android.tools.layoutlib.annotations.LayoutlibDelegate)56 RenderResources (com.android.ide.common.rendering.api.RenderResources)48 File (java.io.File)44 BridgeContext (com.android.layoutlib.bridge.android.BridgeContext)40 ResourceType (com.android.resources.ResourceType)31 XmlPullParser (org.xmlpull.v1.XmlPullParser)28 TypedValue (android.util.TypedValue)26 FileNotFoundException (java.io.FileNotFoundException)26 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)23 AttrResourceValue (com.android.ide.common.rendering.api.AttrResourceValue)22 BridgeXmlBlockParser (com.android.layoutlib.bridge.android.BridgeXmlBlockParser)22 Context (android.content.Context)16 NotNull (org.jetbrains.annotations.NotNull)14 ResourceResolver (com.android.ide.common.resources.ResourceResolver)13 BridgeTypedArray (android.content.res.BridgeTypedArray)12 NotFoundException (android.content.res.Resources.NotFoundException)12