• 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.primitives.UnsignedBytes;
6 import java.util.Map;
7 
8 /** Resource type codes. */
9 public enum DataType {
10   /** {@code data} is either 0 (undefined) or 1 (empty). */
11   NULL(0x00),
12   /** {@code data} holds a {@link ResourceTableChunk} entry reference. */
13   REFERENCE(0x01),
14   /** {@code data} holds an attribute resource identifier. */
15   ATTRIBUTE(0x02),
16   /** {@code data} holds an index into the containing resource table's string pool. */
17   STRING(0x03),
18   /** {@code data} holds a single-precision floating point number. */
19   FLOAT(0x04),
20   /** {@code data} holds a complex number encoding a dimension value, such as "100in". */
21   DIMENSION(0x05),
22   /** {@code data} holds a complex number encoding a fraction of a container. */
23   FRACTION(0x06),
24   /** {@code data} holds a dynamic {@link ResourceTableChunk} entry reference. */
25   DYNAMIC_REFERENCE(0x07),
26   /**
27    * {@code data} holds an attribute resource identifier, which needs to be resolved before it can
28    * 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     ImmutableMap.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     DataType type = FROM_BYTE.get(code);
75     Preconditions.checkArgument(type != null, "Unknown resource type: %s", code);
76     return type;
77   }
78 }
79