1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 // Copied from tools/base/layoutlib-api 17 package com.android.tools.metalava.lint 18 19 /** 20 * Enum representing a type of compiled resource. 21 * 22 * See `ResourceType` in aapt2/Resource.h. 23 * 24 * Although the [displayName] and [alternateXmlNames] are currently unused they were kept for a few 25 * reasons: 26 * 1. It's not clear that they will not be needed in metalava. 27 * 2. It makes it easier to copy new resource types from the original when needed. 28 * 3. It has little maintenance cost. 29 */ 30 enum class ResourceType( 31 /** Returns the resource type name, as used by XML files. */ 32 private val xmlName: String, 33 /** Returns a translated display name for the resource type. */ 34 @Suppress("unused") private val displayName: String, 35 private val kind: Kind = Kind.REAL, 36 @Suppress("unused") private val alternateXmlNames: List<String> = emptyList(), 37 ) { 38 ANIM("anim", "Animation"), 39 ANIMATOR("animator", "Animator"), 40 ARRAY("array", "Array", "string-array", "integer-array"), 41 ATTR("attr", "Attr"), 42 BOOL("bool", "Boolean"), 43 COLOR("color", "Color"), 44 DIMEN("dimen", "Dimension"), 45 DRAWABLE("drawable", "Drawable"), 46 FONT("font", "Font"), 47 FRACTION("fraction", "Fraction"), 48 ID("id", "ID"), 49 INTEGER("integer", "Integer"), 50 INTERPOLATOR("interpolator", "Interpolator"), 51 LAYOUT("layout", "Layout"), 52 MENU("menu", "Menu"), 53 MIPMAP("mipmap", "Mip Map"), 54 NAVIGATION("navigation", "Navigation"), 55 PLURALS("plurals", "Plurals"), 56 RAW("raw", "Raw"), 57 STRING("string", "String"), 58 STYLE("style", "Style"), 59 STYLEABLE("styleable", "Styleable", Kind.STYLEABLE), 60 TRANSITION("transition", "Transition"), 61 XML("xml", "XML"), 62 63 /** 64 * This is not actually used. Only there because they get parsed and since we want to detect new 65 * resource type, we need to have this one exist. 66 */ 67 PUBLIC("public", "Public visibility modifier", Kind.SYNTHETIC), 68 69 /** 70 * This type is used for elements dynamically generated by the parsing of aapt:attr nodes. The 71 * "aapt:attr" allow to inline resources as part of a different resource, for example, a 72 * drawable as part of a layout. When the parser encounters one of these nodes, it will generate 73 * a synthetic _aaptattr reference. 74 */ 75 AAPT("_aapt", "Aapt Attribute", Kind.SYNTHETIC), 76 77 /** 78 * This tag is used for marking a resource overlayable, i.e. that it can be overlaid at runtime 79 * by RROs (Runtime Resource Overlays). This is a new feature supported starting Android 10. 80 * This tag (and the content following it in that node) does not define a resource. 81 */ 82 OVERLAYABLE("overlayable", "Overlayable tag", Kind.SYNTHETIC), 83 84 /** Represents item tags inside a style definition. */ 85 STYLE_ITEM("item", "Style Item", Kind.SYNTHETIC), 86 87 /** 88 * Not an actual resource type from AAPT. Used to provide sample data values in the tools 89 * namespace 90 */ 91 SAMPLE_DATA("sample", "Sample data", Kind.SYNTHETIC), 92 93 /** 94 * Not a real resource, but a way of defining a resource reference that will be replaced with 95 * its actual value during linking. Does not exist at runtime, nor does it appear in the R 96 * class. Only present in raw and flat resources. 97 */ 98 MACRO("macro", "Macro resource replacement", Kind.SYNTHETIC); 99 100 private enum class Kind { 101 /** These types are used both in the R and as XML tag names. */ 102 REAL, 103 104 /** 105 * Styleables are handled by aapt but don't end up in the resource table. They have an R 106 * inner class (called `styleable`), are declared in XML (using `declare-styleable`) but 107 * cannot be referenced from XML. 108 */ 109 STYLEABLE, 110 111 /** 112 * Other types that are not known to aapt, but are used by tools to represent some 113 * information in the resources system. 114 */ 115 SYNTHETIC 116 } 117 118 constructor( 119 xmlName: String, 120 displayName: String, 121 vararg alternateXmlNames: String, 122 ) : this( 123 xmlName = xmlName, 124 displayName = displayName, 125 kind = Kind.REAL, 126 alternateXmlNames = alternateXmlNames.toList() 127 ) 128 toStringnull129 override fun toString(): String { 130 // Unfortunately we still have code that relies on toString() returning the aapt name. 131 return xmlName 132 } 133 134 companion object { <lambda>null135 private val CLASS_NAMES: Map<String, ResourceType> = buildMap { 136 put(STYLEABLE.xmlName, STYLEABLE) 137 put(AAPT.xmlName, AAPT) 138 for (type in values()) { 139 if (type.kind != Kind.REAL || type == STYLEABLE) { 140 continue 141 } 142 put(type.xmlName, type) 143 } 144 } 145 146 /** 147 * Returns the enum by its name as it appears in the R class. 148 * 149 * @param className name of the inner class of the R class, e.g. "string" or "styleable". 150 */ fromClassNamenull151 fun fromClassName(className: String): ResourceType? { 152 return CLASS_NAMES[className] 153 } 154 } 155 } 156