• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.res.android;
2 
3 import com.google.common.base.Preconditions;
4 import com.google.common.collect.ImmutableMap;
5 import com.google.common.collect.ImmutableMap.Builder;
6 import com.google.common.primitives.UnsignedBytes;
7 import java.util.Map;
8 
9 /** Resource type codes. */
10 public enum DataType {
11   /** {@code data} is either 0 (undefined) or 1 (empty). */
12   NULL(0x00),
13   /** {@code data} holds a {@link ResourceTableChunk} entry reference. */
14   REFERENCE(0x01),
15   /** {@code data} holds an attribute resource identifier. */
16   ATTRIBUTE(0x02),
17   /** {@code data} holds an index into the containing resource table's string pool. */
18   STRING(0x03),
19   /** {@code data} holds a single-precision floating point number. */
20   FLOAT(0x04),
21   /** {@code data} holds a complex number encoding a dimension value, such as "100in". */
22   DIMENSION(0x05),
23   /** {@code data} holds a complex number encoding a fraction of a container. */
24   FRACTION(0x06),
25   /** {@code data} holds a dynamic {@link ResourceTableChunk} entry reference. */
26   DYNAMIC_REFERENCE(0x07),
27   /** {@code data} holds an attribute resource identifier, which needs to be resolved
28     * before it can be used like a TYPE_ATTRIBUTE.
29     */
30   DYNAMIC_ATTRIBUTE(0x08),
31   /** {@code data} is a raw integer value of the form n..n. */
32   INT_DEC(0x10),
33   /** {@code data} is a raw integer value of the form 0xn..n. */
34   INT_HEX(0x11),
35   /** {@code data} is either 0 (false) or 1 (true). */
36   INT_BOOLEAN(0x12),
37   /** {@code data} is a raw integer value of the form #aarrggbb. */
38   INT_COLOR_ARGB8(0x1c),
39   /** {@code data} is a raw integer value of the form #rrggbb. */
40   INT_COLOR_RGB8(0x1d),
41   /** {@code data} is a raw integer value of the form #argb. */
42   INT_COLOR_ARGB4(0x1e),
43   /** {@code data} is a raw integer value of the form #rgb. */
44   INT_COLOR_RGB4(0x1f);
45 
46   public static final int TYPE_FIRST_INT = INT_DEC.code();
47   public static final int TYPE_LAST_INT = INT_COLOR_RGB4.code();
48 
49   private final byte code;
50 
51   private static final Map<Byte, DataType> FROM_BYTE;
52 
53   static {
54     Builder<Byte, DataType> builder = ImmutableMap.builder();
55     for (DataType type : values()) {
type.code()56       builder.put(type.code(), type);
57     }
58     FROM_BYTE = builder.build();
59   }
60 
DataType(int code)61   DataType(int code) {
62     this.code = UnsignedBytes.checkedCast(code);
63   }
64 
code()65   public byte code() {
66     return code;
67   }
68 
fromCode(int code)69   public static DataType fromCode(int code) {
70     return fromCode((byte) code);
71   }
72 
fromCode(byte code)73   public static DataType fromCode(byte code) {
74     return Preconditions.checkNotNull(FROM_BYTE.get(code), "Unknown resource type: %s", code);
75   }
76 }
77