Ashley Rose | 041d90b | 2018-11-07 17:35:19 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | |
| 17 | package android.view.inspector; |
| 18 | |
Ashley Rose | 89d6bce | 2019-03-01 19:24:50 -0500 | [diff] [blame] | 19 | import static java.lang.annotation.ElementType.FIELD; |
Ashley Rose | 041d90b | 2018-11-07 17:35:19 -0500 | [diff] [blame] | 20 | import static java.lang.annotation.ElementType.METHOD; |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 21 | import static java.lang.annotation.ElementType.TYPE; |
Ashley Rose | 041d90b | 2018-11-07 17:35:19 -0500 | [diff] [blame] | 22 | import static java.lang.annotation.RetentionPolicy.SOURCE; |
| 23 | |
Ashley Rose | c1a4dec | 2018-12-13 18:06:30 -0500 | [diff] [blame] | 24 | import android.annotation.TestApi; |
Aurimas Liutikas | d8ebfef | 2019-01-16 12:46:42 -0800 | [diff] [blame] | 25 | import android.content.res.Resources; |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 26 | |
Ashley Rose | 041d90b | 2018-11-07 17:35:19 -0500 | [diff] [blame] | 27 | import java.lang.annotation.Retention; |
| 28 | import java.lang.annotation.Target; |
| 29 | |
| 30 | /** |
| 31 | * Marks a getter of a property on an inspectable node. |
| 32 | * |
| 33 | * This annotation is inherited by default. If a child class doesn't add it to a getter, but a |
| 34 | * parent class does, the property will be inspected, even if the child overrides the definition |
| 35 | * of the getter. If a child class defines a property of the same name of a property on the parent |
| 36 | * but on a different getter, the inspector will use the child's getter when inspecting instances |
| 37 | * of the child, and the parent's otherwise. |
| 38 | * |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 39 | * @see InspectionCompanion#mapProperties(PropertyMapper) |
| 40 | * @see InspectionCompanion#readProperties(Object, PropertyReader) |
Ashley Rose | 041d90b | 2018-11-07 17:35:19 -0500 | [diff] [blame] | 41 | * @hide |
| 42 | */ |
Ashley Rose | 89d6bce | 2019-03-01 19:24:50 -0500 | [diff] [blame] | 43 | @Target({METHOD, FIELD}) |
Ashley Rose | 041d90b | 2018-11-07 17:35:19 -0500 | [diff] [blame] | 44 | @Retention(SOURCE) |
Ashley Rose | c1a4dec | 2018-12-13 18:06:30 -0500 | [diff] [blame] | 45 | @TestApi |
Ashley Rose | 041d90b | 2018-11-07 17:35:19 -0500 | [diff] [blame] | 46 | public @interface InspectableProperty { |
| 47 | /** |
| 48 | * The name of the property. |
| 49 | * |
| 50 | * If left empty (the default), the property name will be inferred from the name of the getter |
| 51 | * method. |
| 52 | * |
| 53 | * @return The name of the property. |
| 54 | */ |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 55 | String name() default ""; |
| 56 | |
| 57 | /** |
| 58 | * If the property is inflated from XML, the resource ID of its XML attribute. |
| 59 | * |
| 60 | * If left as {ID_NULL}, and {@link #hasAttributeId()} is true, the attribute ID will be |
| 61 | * inferred from {@link #name()}. |
| 62 | * |
Aurimas Liutikas | d8ebfef | 2019-01-16 12:46:42 -0800 | [diff] [blame] | 63 | * @return The attribute ID of the property or {@link Resources#ID_NULL} |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 64 | */ |
Aurimas Liutikas | d8ebfef | 2019-01-16 12:46:42 -0800 | [diff] [blame] | 65 | int attributeId() default Resources.ID_NULL; |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * If this property has an attribute ID. |
| 69 | * |
| 70 | * Set to false if the annotated property does not have an attribute ID, that is, it is not |
| 71 | * inflated from an XML attribute. This will prevent the automatic inference of the attribute |
Aurimas Liutikas | d8ebfef | 2019-01-16 12:46:42 -0800 | [diff] [blame] | 72 | * ID if {@link #attributeId()} is set to {@link Resources#ID_NULL}. |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 73 | * |
| 74 | * @return Whether to infer an attribute ID if not supplied |
| 75 | */ |
| 76 | boolean hasAttributeId() default true; |
| 77 | |
| 78 | /** |
| 79 | * Specify how to interpret a value type packed into a primitive integer. |
| 80 | * |
| 81 | * @return A {@link ValueType} |
| 82 | */ |
| 83 | ValueType valueType() default ValueType.INFERRED; |
| 84 | |
| 85 | /** |
| 86 | * For enumerations packed into primitive {int} properties, map the values to string names. |
| 87 | * |
| 88 | * Note that {@link #enumMapping()} cannot be used simultaneously with {@link #flagMapping()}. |
| 89 | * |
| 90 | * @return An array of {@link EnumMap}, empty if not applicable |
| 91 | * @see android.annotation.IntDef |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 92 | */ |
| 93 | EnumMap[] enumMapping() default {}; |
| 94 | |
| 95 | /** |
| 96 | * For flags packed into primitive {int} properties, model the string names of the flags. |
| 97 | * |
| 98 | * Note that {@link #flagMapping()} cannot be used simultaneously with {@link #enumMapping()}. |
| 99 | * |
| 100 | * @return An array of {@link FlagMap}, empty if not applicable |
| 101 | * @see android.annotation.IntDef |
| 102 | * @see IntFlagMapping |
| 103 | */ |
| 104 | FlagMap[] flagMapping() default {}; |
| 105 | |
| 106 | |
| 107 | /** |
| 108 | * One entry in an enumeration packed into a primitive {int}. |
| 109 | * |
Ashley Rose | a61e7cd | 2019-01-23 14:37:42 -0500 | [diff] [blame] | 110 | * @see IntEnumMapping |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 111 | * @hide |
| 112 | */ |
| 113 | @Target({TYPE}) |
| 114 | @Retention(SOURCE) |
Ashley Rose | c1a4dec | 2018-12-13 18:06:30 -0500 | [diff] [blame] | 115 | @TestApi |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 116 | @interface EnumMap { |
| 117 | /** |
| 118 | * The string name of this enumeration value. |
| 119 | * |
| 120 | * @return A string name |
| 121 | */ |
| 122 | String name(); |
| 123 | |
| 124 | /** |
| 125 | * The integer value of this enumeration value. |
| 126 | * |
| 127 | * @return An integer value |
| 128 | */ |
| 129 | int value(); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * One flag value of many that may be packed into a primitive {int}. |
| 134 | * |
| 135 | * @see IntFlagMapping |
| 136 | * @hide |
| 137 | */ |
| 138 | @Target({TYPE}) |
| 139 | @Retention(SOURCE) |
Ashley Rose | c1a4dec | 2018-12-13 18:06:30 -0500 | [diff] [blame] | 140 | @TestApi |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 141 | @interface FlagMap { |
| 142 | /** |
| 143 | * The string name of this flag. |
| 144 | * |
| 145 | * @return A string name |
| 146 | */ |
| 147 | String name(); |
| 148 | |
| 149 | /** |
| 150 | * A target value that the property's value must equal after masking. |
| 151 | * |
| 152 | * If a mask is not supplied (i.e., {@link #mask()} is 0), the target will be reused as the |
| 153 | * mask. This handles the common case where no flags mutually exclude each other. |
| 154 | * |
| 155 | * @return The target value to compare against |
| 156 | */ |
| 157 | int target(); |
| 158 | |
| 159 | /** |
| 160 | * A mask that the property will be bitwise anded with before comparing to the target. |
| 161 | * |
| 162 | * If set to 0 (the default), the value of {@link #target()} will be used as a mask. Zero |
| 163 | * was chosen as the default since bitwise and with zero is always zero. |
| 164 | * |
| 165 | * @return A mask, or 0 to use the target as a mask |
| 166 | */ |
| 167 | int mask() default 0; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * The type of value packed into a primitive {int}. |
| 172 | * |
| 173 | * @hide |
| 174 | */ |
Ashley Rose | c1a4dec | 2018-12-13 18:06:30 -0500 | [diff] [blame] | 175 | @TestApi |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 176 | enum ValueType { |
| 177 | /** |
| 178 | * No special handling, property is considered to be a numeric value. |
Ashley Rose | c1a4dec | 2018-12-13 18:06:30 -0500 | [diff] [blame] | 179 | * |
| 180 | * @hide |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 181 | */ |
Ashley Rose | c1a4dec | 2018-12-13 18:06:30 -0500 | [diff] [blame] | 182 | @TestApi |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 183 | NONE, |
| 184 | |
| 185 | /** |
| 186 | * The default the annotation processor infers the value type from context. |
Ashley Rose | c1a4dec | 2018-12-13 18:06:30 -0500 | [diff] [blame] | 187 | * |
| 188 | * @hide |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 189 | */ |
Ashley Rose | c1a4dec | 2018-12-13 18:06:30 -0500 | [diff] [blame] | 190 | @TestApi |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 191 | INFERRED, |
| 192 | |
| 193 | /** |
| 194 | * Value packs an enumeration. |
| 195 | * |
| 196 | * This is inferred if {@link #enumMapping()} is specified. |
| 197 | * |
| 198 | * @see EnumMap |
Ashley Rose | c1a4dec | 2018-12-13 18:06:30 -0500 | [diff] [blame] | 199 | * @hide |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 200 | */ |
Ashley Rose | c1a4dec | 2018-12-13 18:06:30 -0500 | [diff] [blame] | 201 | @TestApi |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 202 | INT_ENUM, |
| 203 | |
| 204 | /** |
| 205 | * Value packs flags, of which many may be enabled at once. |
| 206 | * |
| 207 | * This is inferred if {@link #flagMapping()} is specified. |
| 208 | * |
| 209 | * @see FlagMap |
Ashley Rose | c1a4dec | 2018-12-13 18:06:30 -0500 | [diff] [blame] | 210 | * @hide |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 211 | */ |
Ashley Rose | c1a4dec | 2018-12-13 18:06:30 -0500 | [diff] [blame] | 212 | @TestApi |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 213 | INT_FLAG, |
| 214 | |
| 215 | /** |
| 216 | * Value packs color information. |
| 217 | * |
| 218 | * This is inferred from {@link android.annotation.ColorInt}, or |
| 219 | * {@link android.annotation.ColorLong} on the getter method. |
| 220 | * |
| 221 | * @see android.graphics.Color |
Ashley Rose | c1a4dec | 2018-12-13 18:06:30 -0500 | [diff] [blame] | 222 | * @hide |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 223 | */ |
Ashley Rose | c1a4dec | 2018-12-13 18:06:30 -0500 | [diff] [blame] | 224 | @TestApi |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 225 | COLOR, |
| 226 | |
| 227 | /** |
| 228 | * Value packs gravity information. |
| 229 | * |
| 230 | * This type is not inferred, and is non-trivial to represent using {@link FlagMap}. |
| 231 | * |
| 232 | * @see android.view.Gravity |
Ashley Rose | c1a4dec | 2018-12-13 18:06:30 -0500 | [diff] [blame] | 233 | * @hide |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 234 | */ |
Ashley Rose | c1a4dec | 2018-12-13 18:06:30 -0500 | [diff] [blame] | 235 | @TestApi |
Ashley Rose | e891481 | 2019-03-05 17:12:00 -0500 | [diff] [blame] | 236 | GRAVITY, |
| 237 | |
| 238 | /** |
| 239 | * Value is a resource ID |
| 240 | * |
| 241 | * This type is inferred from the presence of a resource ID annotation such as |
| 242 | * {@link android.annotation.AnyRes}. |
| 243 | * |
| 244 | * @hide |
| 245 | */ |
| 246 | @TestApi |
| 247 | RESOURCE_ID |
Ashley Rose | d2c5f45 | 2018-11-29 15:40:10 -0500 | [diff] [blame] | 248 | } |
Ashley Rose | 041d90b | 2018-11-07 17:35:19 -0500 | [diff] [blame] | 249 | } |