Search in sources :

Example 21 with ResourceType

use of com.android.resources.ResourceType in project android_frameworks_base by AOSPA.

the class Bridge method init.

@Override
public boolean init(Map<String, String> platformProperties, File fontLocation, Map<String, Map<String, Integer>> enumValueMap, LayoutLog log) {
    sPlatformProperties = platformProperties;
    sEnumValueMap = enumValueMap;
    BridgeAssetManager.initSystem();
    // 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) {
                sDefaultLog.error(null, "Missing Stub: " + signature + (isNative ? " (native)" : ""), null);
                if (debug.equalsIgnoreCase("throw")) {
                    // throw it only if the environment variable is "throw" or "THROW".
                    throw new StaticMethodNotImplementedException(signature);
                }
            }
        });
    }
    // load the fonts.
    FontFamily_Delegate.setFontLocation(fontLocation.getAbsolutePath());
    MemoryMappedFile_Delegate.setDataDir(fontLocation.getAbsoluteFile().getParentFile());
    // the internal version), and put the content in the maps.
    try {
        Class<?> r = com.android.internal.R.class;
        // Parse the styleable class first, since it may contribute to attr values.
        parseStyleable();
        for (Class<?> inner : r.getDeclaredClasses()) {
            if (inner == com.android.internal.R.styleable.class) {
                // that are not referenced from styleables.
                continue;
            }
            String resTypeName = inner.getSimpleName();
            ResourceType resType = ResourceType.getEnum(resTypeName);
            if (resType != null) {
                Map<String, Integer> fullMap = null;
                switch(resType) {
                    case ATTR:
                        fullMap = sRevRMap.get(ResourceType.ATTR);
                        break;
                    case STRING:
                    case STYLE:
                        // Slightly less than thousand entries in each.
                        fullMap = new HashMap<String, Integer>(1280);
                    // no break.
                    default:
                        if (fullMap == null) {
                            fullMap = new HashMap<String, Integer>();
                        }
                        sRevRMap.put(resType, fullMap);
                }
                for (Field f : inner.getDeclaredFields()) {
                    // been altered by layoutlib_create, we only check static
                    if (!isValidRField(f)) {
                        continue;
                    }
                    Class<?> type = f.getType();
                    if (type.isArray()) {
                        // if the object is an int[] we put it in sRArrayMap using an IntArray
                        // wrapper that properly implements equals and hashcode for the array
                        // objects, as required by the map contract.
                        sRArrayMap.put(new IntArray((int[]) f.get(null)), f.getName());
                    } else {
                        Integer value = (Integer) f.get(null);
                        sRMap.put(value, Pair.of(resType, f.getName()));
                        fullMap.put(f.getName(), value);
                    }
                }
            }
        }
    } catch (Exception throwable) {
        if (log != null) {
            log.error(LayoutLog.TAG_BROKEN, "Failed to load com.android.internal.R from the layout library jar", throwable, null);
        }
        return false;
    }
    return true;
}
Also used : ResourceType(com.android.resources.ResourceType) Field(java.lang.reflect.Field) MethodAdapter(com.android.tools.layoutlib.create.MethodAdapter)

Example 22 with ResourceType

use of com.android.resources.ResourceType in project android_frameworks_base by AOSPA.

the class RenderDrawable method render.

public Result render() {
    checkLock();
    // get the drawable resource value
    DrawableParams params = getParams();
    HardwareConfig hardwareConfig = params.getHardwareConfig();
    ResourceValue drawableResource = params.getDrawable();
    // resolve it
    BridgeContext context = getContext();
    drawableResource = context.getRenderResources().resolveResValue(drawableResource);
    if (drawableResource == null) {
        return Status.ERROR_NOT_A_DRAWABLE.createResult();
    }
    ResourceType resourceType = drawableResource.getResourceType();
    if (resourceType != ResourceType.DRAWABLE && resourceType != ResourceType.MIPMAP) {
        return Status.ERROR_NOT_A_DRAWABLE.createResult();
    }
    Drawable d = ResourceHelper.getDrawable(drawableResource, context);
    final Boolean allStates = params.getFlag(RenderParamsFlags.FLAG_KEY_RENDER_ALL_DRAWABLE_STATES);
    if (allStates == Boolean.TRUE) {
        final List<BufferedImage> result;
        if (d instanceof StateListDrawable) {
            result = new ArrayList<BufferedImage>();
            final StateListDrawable stateList = (StateListDrawable) d;
            for (int i = 0; i < stateList.getStateCount(); i++) {
                final Drawable stateDrawable = stateList.getStateDrawable(i);
                result.add(renderImage(hardwareConfig, stateDrawable, context));
            }
        } else {
            result = Collections.singletonList(renderImage(hardwareConfig, d, context));
        }
        return Status.SUCCESS.createResult(result);
    } else {
        BufferedImage image = renderImage(hardwareConfig, d, context);
        return Status.SUCCESS.createResult(image);
    }
}
Also used : HardwareConfig(com.android.ide.common.rendering.api.HardwareConfig) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) Drawable(android.graphics.drawable.Drawable) StateListDrawable(android.graphics.drawable.StateListDrawable) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) ResourceType(com.android.resources.ResourceType) StateListDrawable(android.graphics.drawable.StateListDrawable) BufferedImage(java.awt.image.BufferedImage) DrawableParams(com.android.ide.common.rendering.api.DrawableParams)

Example 23 with ResourceType

use of com.android.resources.ResourceType in project android_frameworks_base by AOSPA.

the class RenderSessionImpl method setupTabHost.

/**
     * Sets up a {@link TabHost} object.
     * @param tabHost the TabHost to setup.
     * @param layoutlibCallback The project callback object to access the project R class.
     * @throws PostInflateException
     */
private void setupTabHost(TabHost tabHost, LayoutlibCallback layoutlibCallback) throws PostInflateException {
    // look for the TabWidget, and the FrameLayout. They have their own specific names
    View v = tabHost.findViewById(android.R.id.tabs);
    if (v == null) {
        throw new PostInflateException("TabHost requires a TabWidget with id \"android:id/tabs\".\n");
    }
    if (!(v instanceof TabWidget)) {
        throw new PostInflateException(String.format("TabHost requires a TabWidget with id \"android:id/tabs\".\n" + "View found with id 'tabs' is '%s'", v.getClass().getCanonicalName()));
    }
    v = tabHost.findViewById(android.R.id.tabcontent);
    if (v == null) {
        //noinspection SpellCheckingInspection
        throw new PostInflateException("TabHost requires a FrameLayout with id \"android:id/tabcontent\".");
    }
    if (!(v instanceof FrameLayout)) {
        //noinspection SpellCheckingInspection
        throw new PostInflateException(String.format("TabHost requires a FrameLayout with id \"android:id/tabcontent\".\n" + "View found with id 'tabcontent' is '%s'", v.getClass().getCanonicalName()));
    }
    FrameLayout content = (FrameLayout) v;
    // now process the content of the frameLayout and dynamically create tabs for it.
    final int count = content.getChildCount();
    // this must be called before addTab() so that the TabHost searches its TabWidget
    // and FrameLayout.
    tabHost.setup();
    if (count == 0) {
        // Create a dummy child to get a single tab
        TabSpec spec = tabHost.newTabSpec("tag").setIndicator("Tab Label", tabHost.getResources().getDrawable(android.R.drawable.ic_menu_info_details, null)).setContent(new TabHost.TabContentFactory() {

            @Override
            public View createTabContent(String tag) {
                return new LinearLayout(getContext());
            }
        });
        tabHost.addTab(spec);
    } else {
        // for each child of the frameLayout, add a new TabSpec
        for (int i = 0; i < count; i++) {
            View child = content.getChildAt(i);
            String tabSpec = String.format("tab_spec%d", i + 1);
            // child cannot be null.
            @SuppressWarnings("ConstantConditions") int id = child.getId();
            @SuppressWarnings("deprecation") Pair<ResourceType, String> resource = layoutlibCallback.resolveResourceId(id);
            String name;
            if (resource != null) {
                name = resource.getSecond();
            } else {
                // default name if id is unresolved.
                name = String.format("Tab %d", i + 1);
            }
            tabHost.addTab(tabHost.newTabSpec(tabSpec).setIndicator(name).setContent(id));
        }
    }
}
Also used : TabHost(android.widget.TabHost) ResourceType(com.android.resources.ResourceType) TabWidget(android.widget.TabWidget) MenuView(com.android.internal.view.menu.MenuView) View(android.view.View) AdapterView(android.widget.AdapterView) ActionMenuItemView(com.android.internal.view.menu.ActionMenuItemView) IconMenuItemView(com.android.internal.view.menu.IconMenuItemView) ListView(android.widget.ListView) ListMenuItemView(com.android.internal.view.menu.ListMenuItemView) AbsListView(android.widget.AbsListView) ActionMenuView(android.widget.ActionMenuView) ExpandableListView(android.widget.ExpandableListView) TabSpec(android.widget.TabHost.TabSpec) FrameLayout(android.widget.FrameLayout) LinearLayout(android.widget.LinearLayout)

Example 24 with ResourceType

use of com.android.resources.ResourceType in project android_frameworks_base by ResurrectionRemix.

the class Bridge method init.

@Override
public boolean init(Map<String, String> platformProperties, File fontLocation, Map<String, Map<String, Integer>> enumValueMap, LayoutLog log) {
    sPlatformProperties = platformProperties;
    sEnumValueMap = enumValueMap;
    BridgeAssetManager.initSystem();
    // 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) {
                sDefaultLog.error(null, "Missing Stub: " + signature + (isNative ? " (native)" : ""), null);
                if (debug.equalsIgnoreCase("throw")) {
                    // throw it only if the environment variable is "throw" or "THROW".
                    throw new StaticMethodNotImplementedException(signature);
                }
            }
        });
    }
    // load the fonts.
    FontFamily_Delegate.setFontLocation(fontLocation.getAbsolutePath());
    MemoryMappedFile_Delegate.setDataDir(fontLocation.getAbsoluteFile().getParentFile());
    // the internal version), and put the content in the maps.
    try {
        Class<?> r = com.android.internal.R.class;
        // Parse the styleable class first, since it may contribute to attr values.
        parseStyleable();
        for (Class<?> inner : r.getDeclaredClasses()) {
            if (inner == com.android.internal.R.styleable.class) {
                // that are not referenced from styleables.
                continue;
            }
            String resTypeName = inner.getSimpleName();
            ResourceType resType = ResourceType.getEnum(resTypeName);
            if (resType != null) {
                Map<String, Integer> fullMap = null;
                switch(resType) {
                    case ATTR:
                        fullMap = sRevRMap.get(ResourceType.ATTR);
                        break;
                    case STRING:
                    case STYLE:
                        // Slightly less than thousand entries in each.
                        fullMap = new HashMap<String, Integer>(1280);
                    // no break.
                    default:
                        if (fullMap == null) {
                            fullMap = new HashMap<String, Integer>();
                        }
                        sRevRMap.put(resType, fullMap);
                }
                for (Field f : inner.getDeclaredFields()) {
                    // been altered by layoutlib_create, we only check static
                    if (!isValidRField(f)) {
                        continue;
                    }
                    Class<?> type = f.getType();
                    if (type.isArray()) {
                        // if the object is an int[] we put it in sRArrayMap using an IntArray
                        // wrapper that properly implements equals and hashcode for the array
                        // objects, as required by the map contract.
                        sRArrayMap.put(new IntArray((int[]) f.get(null)), f.getName());
                    } else {
                        Integer value = (Integer) f.get(null);
                        sRMap.put(value, Pair.of(resType, f.getName()));
                        fullMap.put(f.getName(), value);
                    }
                }
            }
        }
    } catch (Exception throwable) {
        if (log != null) {
            log.error(LayoutLog.TAG_BROKEN, "Failed to load com.android.internal.R from the layout library jar", throwable, null);
        }
        return false;
    }
    return true;
}
Also used : ResourceType(com.android.resources.ResourceType) Field(java.lang.reflect.Field) MethodAdapter(com.android.tools.layoutlib.create.MethodAdapter)

Example 25 with ResourceType

use of com.android.resources.ResourceType in project android_frameworks_base by ResurrectionRemix.

the class CustomBar method getColor.

private static int getColor(RenderResources renderResources, String attr) {
    // From ?attr/foo to @color/bar. This is most likely an ItemResourceValue.
    ResourceValue resource = renderResources.findItemInTheme(attr, true);
    // Form @color/bar to the #AARRGGBB
    resource = renderResources.resolveResValue(resource);
    if (resource != null) {
        ResourceType type = resource.getResourceType();
        if (type == null || type == ResourceType.COLOR) {
            // file, rather than referencing a color resource value.
            try {
                return ResourceHelper.getColor(resource.getValue());
            } catch (NumberFormatException e) {
                // Conversion failed.
                Bridge.getLog().warning(LayoutLog.TAG_RESOURCES_FORMAT, "Theme attribute @android:" + attr + " does not reference a color, instead is '" + resource.getValue() + "'.", resource);
            }
        }
    }
    return 0;
}
Also used : StyleResourceValue(com.android.ide.common.rendering.api.StyleResourceValue) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) ResourceType(com.android.resources.ResourceType)

Aggregations

ResourceType (com.android.resources.ResourceType)137 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)31 NotNull (org.jetbrains.annotations.NotNull)16 Field (java.lang.reflect.Field)13 StyleResourceValue (com.android.ide.common.rendering.api.StyleResourceValue)12 BridgeContext (com.android.layoutlib.bridge.android.BridgeContext)11 VirtualFile (com.intellij.openapi.vfs.VirtualFile)10 Map (java.util.Map)10 Nullable (org.jetbrains.annotations.Nullable)10 Nullable (com.android.annotations.Nullable)8 ResourceFolderType (com.android.resources.ResourceFolderType)8 File (java.io.File)8 EnumMap (java.util.EnumMap)7 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)7 Context (android.content.Context)6 View (android.view.View)6 AbsListView (android.widget.AbsListView)6 AdapterView (android.widget.AdapterView)6 ExpandableListView (android.widget.ExpandableListView)6 FrameLayout (android.widget.FrameLayout)6