Search in sources :

Example 51 with StyleResourceValue

use of com.android.ide.common.rendering.api.StyleResourceValue in project android_frameworks_base by DirtyUnicorns.

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);
    }
    // (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);
        boolean create = value.startsWith("@+");
        boolean isFrameworkId = mPlatformFile || value.startsWith("@android") || value.startsWith("@+android");
        // Look for the idName in project or android R class depending on isPlatform.
        if (create) {
            Integer idValue;
            if (isFrameworkId) {
                idValue = Bridge.getResourceId(ResourceType.ID, idName);
            } else {
                idValue = mContext.getLayoutlibCallback().getResourceId(ResourceType.ID, idName);
            }
            return idValue == null ? defValue : idValue;
        }
        // one is not found.
        if (isFrameworkId) {
            return mContext.getFrameworkResourceValue(ResourceType.ID, idName, defValue);
        } else {
            return mContext.getProjectResourceValue(ResourceType.ID, idName, defValue);
        }
    }
    // not a direct id valid reference? resolve it
    Integer idValue;
    if (resValue.isFramework()) {
        idValue = Bridge.getResourceId(resValue.getResourceType(), resValue.getName());
    } else {
        idValue = mContext.getLayoutlibCallback().getResourceId(resValue.getResourceType(), resValue.getName());
    }
    if (idValue != null) {
        return idValue;
    }
    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) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) AttrResourceValue(com.android.ide.common.rendering.api.AttrResourceValue) ArrayResourceValue(com.android.ide.common.rendering.api.ArrayResourceValue)

Example 52 with StyleResourceValue

use of com.android.ide.common.rendering.api.StyleResourceValue in project android_frameworks_base by DirtyUnicorns.

the class Resources_Theme_Delegate method resolveStyle.

@Nullable
private static StyleResourceValue resolveStyle(int nativeResid) {
    if (nativeResid == 0) {
        return null;
    }
    BridgeContext context = RenderSessionImpl.getCurrentContext();
    ResourceReference theme = context.resolveId(nativeResid);
    if (theme.isFramework()) {
        return (StyleResourceValue) context.getRenderResources().getFrameworkResource(ResourceType.STYLE, theme.getName());
    } else {
        return (StyleResourceValue) context.getRenderResources().getProjectResource(ResourceType.STYLE, theme.getName());
    }
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) ResourceReference(com.android.ide.common.rendering.api.ResourceReference) Nullable(android.annotation.Nullable)

Example 53 with StyleResourceValue

use of com.android.ide.common.rendering.api.StyleResourceValue in project android_frameworks_base by DirtyUnicorns.

the class AppCompatActionBar method getInflater.

@Override
protected LayoutInflater getInflater(BridgeContext context) {
    // Other than the resource resolution part, the code has been taken from the support
    // library. see code from line 269 onwards in
    // https://android.googlesource.com/platform/frameworks/support/+/android-5.1.0_r1/v7/appcompat/src/android/support/v7/app/ActionBarActivityDelegateBase.java
    Context themedContext = context;
    RenderResources resources = context.getRenderResources();
    ResourceValue actionBarTheme = resources.findItemInTheme("actionBarTheme", false);
    if (actionBarTheme != null) {
        // resolve it, if needed.
        actionBarTheme = resources.resolveResValue(actionBarTheme);
    }
    if (actionBarTheme instanceof StyleResourceValue) {
        int styleId = context.getDynamicIdByStyle(((StyleResourceValue) actionBarTheme));
        if (styleId != 0) {
            themedContext = new ContextThemeWrapper(context, styleId);
        }
    }
    return LayoutInflater.from(themedContext);
}
Also used : Context(android.content.Context) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ContextThemeWrapper(android.view.ContextThemeWrapper) RenderResources(com.android.ide.common.rendering.api.RenderResources) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue)

Example 54 with StyleResourceValue

use of com.android.ide.common.rendering.api.StyleResourceValue in project android by JetBrains.

the class StyleFilterTest method assertStylesSorted.

private void assertStylesSorted(@NotNull String context, @NotNull List<StyleResourceValue> styles, int minUserCount, int minLibraryCount, int minFrameworkCount, List<String> userSample, List<String> librarySample, List<String> frameworkSample) {
    boolean success = false;
    try {
        Set<String> userSampleStyles = new HashSet<>(userSample);
        Set<String> librarySampleStyles = new HashSet<>(librarySample);
        Set<String> frameworkSampleStyles = new HashSet<>(frameworkSample);
        String previousStyleName = "";
        int index = 0;
        // All user styles should be sorted
        for (; index < styles.size() && styles.get(index).isUserDefined(); index++) {
            StyleResourceValue style = styles.get(index);
            assertStyle(styles.get(index), style.getName(), !FRAMEWORK, USER_DEFINED, IS_DERIVED_STYLE, false);
            assertTrue(previousStyleName.compareTo(style.getName()) < 0);
            userSampleStyles.remove(style.getName());
            previousStyleName = style.getName();
        }
        int actualUserCount = index;
        assertAtLeast(context + " user style count", minUserCount, actualUserCount);
        assertEmpty(context, userSampleStyles);
        // All library styles should be sorted
        int previousIndex = index;
        previousStyleName = "";
        for (; index < styles.size() && !styles.get(index).isFramework(); index++) {
            StyleResourceValue style = styles.get(index);
            assertStyle(styles.get(index), style.getName(), !FRAMEWORK, !USER_DEFINED, IS_DERIVED_STYLE, false);
            assertTrue(previousStyleName.compareTo(style.getName()) < 0);
            librarySampleStyles.remove(style.getName());
            previousStyleName = style.getName();
        }
        int actualLibraryCount = index - previousIndex;
        assertAtLeast(context + " library style count", minLibraryCount, actualLibraryCount);
        assertEmpty(context, librarySampleStyles);
        // All framework styles should be sorted
        previousIndex = index;
        previousStyleName = "";
        for (; index < styles.size(); index++) {
            StyleResourceValue style = styles.get(index);
            assertStyle(styles.get(index), style.getName(), FRAMEWORK, !USER_DEFINED, IS_DERIVED_STYLE, false);
            assertTrue(previousStyleName.compareTo(style.getName()) < 0);
            frameworkSampleStyles.remove(style.getName());
            previousStyleName = style.getName();
        }
        int actualFrameworkCount = index - previousIndex;
        assertAtLeast(context + " framework style count", minFrameworkCount, actualFrameworkCount);
        assertEmpty(context, frameworkSampleStyles);
        assertEquals("All styles should be in 3 sections", styles.size(), index);
        success = true;
    } finally {
        if (!success) {
            dumpStyles(styles);
        }
    }
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) HashSet(java.util.HashSet)

Example 55 with StyleResourceValue

use of com.android.ide.common.rendering.api.StyleResourceValue in project android by JetBrains.

the class ResolutionUtilsTest method testGetQualifiedName.

/*
   * The test SDK only includes some resources. It only includes a few incomplete styles.
   */
public void testGetQualifiedName() {
    StyleResourceValue styleResourceValue = new StyleResourceValue(ResourceType.STYLE, "myStyle", true, null);
    assertEquals("android:myStyle", ResolutionUtils.getQualifiedStyleName(styleResourceValue));
    styleResourceValue = new StyleResourceValue(ResourceType.STYLE, "myStyle", false, null);
    assertEquals("myStyle", ResolutionUtils.getQualifiedStyleName(styleResourceValue));
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue)

Aggregations

StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)69 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)41 BridgeTypedArray (android.content.res.BridgeTypedArray)18 BridgeContext (com.android.layoutlib.bridge.android.BridgeContext)15 PropertiesMap (com.android.util.PropertiesMap)15 Pair (com.android.util.Pair)12 RenderResources (com.android.ide.common.rendering.api.RenderResources)11 Property (com.android.util.PropertiesMap.Property)10 ResourceReference (com.android.ide.common.rendering.api.ResourceReference)9 ResourceItem (com.android.ide.common.res2.ResourceItem)7 ColorStateList (android.content.res.ColorStateList)6 BitmapDrawable (android.graphics.drawable.BitmapDrawable)6 Drawable (android.graphics.drawable.Drawable)6 TypedValue (android.util.TypedValue)6 TextView (android.widget.TextView)6 ResourceType (com.android.resources.ResourceType)6 FileNotFoundException (java.io.FileNotFoundException)6 NotNull (org.jetbrains.annotations.NotNull)6 Context (android.content.Context)5 ContextThemeWrapper (android.view.ContextThemeWrapper)5