Search in sources :

Example 71 with ResourceType

use of com.android.resources.ResourceType in project bazel by bazelbuild.

the class AndroidResourceClassWriter method writeAsJava.

private void writeAsJava(Map<ResourceType, List<FieldInitializer>> initializers) throws IOException {
    String packageDir = packageName.replace('.', '/');
    Path packagePath = outputBasePath.resolve(packageDir);
    Path rJavaPath = packagePath.resolve(SdkConstants.FN_RESOURCE_CLASS);
    Files.createDirectories(rJavaPath.getParent());
    try (BufferedWriter writer = Files.newBufferedWriter(rJavaPath, UTF_8)) {
        writer.write("/* AUTO-GENERATED FILE.  DO NOT MODIFY.\n");
        writer.write(" *\n");
        writer.write(" * This class was automatically generated by the\n");
        writer.write(" * bazel tool from the resource data it found.  It\n");
        writer.write(" * should not be modified by hand.\n");
        writer.write(" */\n");
        writer.write(String.format("package %s;\n", packageName));
        writer.write("public final class R {\n");
        for (Map.Entry<ResourceType, Set<String>> fieldEntries : innerClasses.entrySet()) {
            ResourceType type = fieldEntries.getKey();
            writer.write(String.format("    public static final class %s {\n", type.getName()));
            for (FieldInitializer field : initializers.get(type)) {
                field.writeInitSource(writer);
            }
            writer.write("    }\n");
        }
        writer.write("}");
    }
}
Also used : Path(java.nio.file.Path) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ResourceType(com.android.resources.ResourceType) FieldInitializer(com.google.devtools.build.android.resources.FieldInitializer) IntArrayFieldInitializer(com.google.devtools.build.android.resources.IntArrayFieldInitializer) IntFieldInitializer(com.google.devtools.build.android.resources.IntFieldInitializer) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) EnumMap(java.util.EnumMap) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) BufferedWriter(java.io.BufferedWriter)

Example 72 with ResourceType

use of com.android.resources.ResourceType in project bazel by bazelbuild.

the class AndroidResourceClassWriter method writeStyleableResource.

public void writeStyleableResource(FullyQualifiedName key, Map<FullyQualifiedName, Boolean> attrs) {
    ResourceType type = ResourceType.STYLEABLE;
    // The configuration can play a role in sorting, but that isn't modeled yet.
    String normalizedStyleableName = normalizeName(key.name());
    writeSimpleResource(type, normalizedStyleableName);
    // We should have merged styleables, so there should only be one definition per configuration.
    // However, we don't combine across configurations, so there can be a pre-existing definition.
    Map<String, Boolean> normalizedAttrs = styleableAttrs.get(normalizedStyleableName);
    if (normalizedAttrs == null) {
        // We need to maintain the original order of the attrs.
        normalizedAttrs = new LinkedHashMap<>();
        styleableAttrs.put(normalizedStyleableName, normalizedAttrs);
    }
    for (Map.Entry<FullyQualifiedName, Boolean> attrEntry : attrs.entrySet()) {
        String normalizedAttrName = normalizeAttrName(attrEntry.getKey().name());
        normalizedAttrs.put(normalizedAttrName, attrEntry.getValue());
    }
}
Also used : ResourceType(com.android.resources.ResourceType) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) EnumMap(java.util.EnumMap) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Example 73 with ResourceType

use of com.android.resources.ResourceType in project bazel by bazelbuild.

the class AndroidResourceClassWriter method chooseTypeIds.

private Map<ResourceType, Integer> chooseTypeIds() {
    // Go through public entries. Those may have forced certain type assignments, so take those
    // into account first.
    Map<ResourceType, Integer> allocatedTypeIds = assignTypeIdsForPublic();
    Set<Integer> reservedTypeSlots = ImmutableSet.copyOf(allocatedTypeIds.values());
    // ATTR always takes up slot #1, even if it isn't present.
    allocatedTypeIds.put(ResourceType.ATTR, ATTR_TYPE_ID);
    // The rest are packed after that.
    int nextTypeId = nextFreeId(ATTR_TYPE_ID + 1, reservedTypeSlots);
    for (ResourceType t : AAPT_TYPE_ORDERING) {
        if (innerClasses.containsKey(t) && !allocatedTypeIds.containsKey(t)) {
            allocatedTypeIds.put(t, nextTypeId);
            nextTypeId = nextFreeId(nextTypeId + 1, reservedTypeSlots);
        }
    }
    // We will need to update the list if there is a new resource type.
    for (ResourceType t : innerClasses.keySet()) {
        Preconditions.checkState(t == ResourceType.STYLEABLE || allocatedTypeIds.containsKey(t), "Resource type %s is not allocated a type ID", t);
    }
    return allocatedTypeIds;
}
Also used : ResourceType(com.android.resources.ResourceType)

Example 74 with ResourceType

use of com.android.resources.ResourceType in project paraphrase by JakeWharton.

the class ValueResourceParser method getResource.

/**
   * Returns a new ResourceItem object for a given node.
   * @param node the node representing the resource.
   * @return a ResourceItem object or null.
   */
static ResourceItem getResource(Node node) {
    ResourceType type = getType(node);
    String name = getName(node);
    if (type != null && name != null) {
        return new ResourceItem(name, type, node);
    }
    return null;
}
Also used : ResourceType(com.android.resources.ResourceType) ResourceItem(com.android.ide.common.res2.ResourceItem)

Example 75 with ResourceType

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

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;
    // don't use EnumSet.allOf(), because the bridge doesn't come with its specific version
    // of layoutlib_api. It is provided by the client which could have a more recent version
    // with newer, unsupported capabilities.
    mCapabilities = EnumSet.of(Capability.UNBOUND_RENDERING, Capability.CUSTOM_BACKGROUND_COLOR, Capability.RENDER, Capability.LAYOUT_ONLY, Capability.EMBEDDED_LAYOUT, Capability.VIEW_MANIPULATION, Capability.PLAY_ANIMATION, Capability.ANIMATED_VIEW_MANIPULATION, Capability.ADAPTER_BINDING, Capability.EXTENDED_VIEWINFO, Capability.FIXED_SCALABLE_NINE_PATCH);
    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.
    FontLoader fontLoader = FontLoader.create(fontLocation.getAbsolutePath());
    if (fontLoader != null) {
        Typeface_Delegate.init(fontLoader);
    } else {
        log.error(LayoutLog.TAG_BROKEN, "Failed create FontLoader in layout lib.", null);
        return false;
    }
    // the internal version), and put the content in the maps.
    try {
        Class<?> r = com.android.internal.R.class;
        for (Class<?> inner : r.getDeclaredClasses()) {
            String resTypeName = inner.getSimpleName();
            ResourceType resType = ResourceType.getEnum(resTypeName);
            if (resType != null) {
                Map<String, Integer> fullMap = new HashMap<String, Integer>();
                sRevRMap.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 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 if (type == int.class) {
                            Integer value = (Integer) f.get(null);
                            sRMap.put(value, Pair.of(resType, f.getName()));
                            fullMap.put(f.getName(), value);
                        } else {
                            assert false;
                        }
                    }
                }
            }
        }
    } catch (Throwable throwable) {
        if (log != null) {
            log.error(LayoutLog.TAG_BROKEN, "Failed to load com.android.internal.R from the layout library jar", throwable);
        }
        return false;
    }
    return true;
}
Also used : HashMap(java.util.HashMap) ResourceType(com.android.resources.ResourceType) FontLoader(com.android.layoutlib.bridge.impl.FontLoader) Field(java.lang.reflect.Field) MethodAdapter(com.android.tools.layoutlib.create.MethodAdapter)

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