liyujie
2025-08-28 b3810562527858a3b3d98ffa6e9c9c5b0f4a9a8e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package android.view.inspector;
 
import android.annotation.AnyRes;
import android.annotation.ColorInt;
import android.annotation.ColorLong;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.graphics.Color;
 
/**
 * An interface for reading the properties of an inspectable object.
 *
 * {@code PropertyReader} is defined as an interface that will be called by
 * {@link InspectionCompanion#readProperties(Object, PropertyReader)}. This approach allows a
 * client inspector to read the values of primitive properties without the overhead of
 * instantiating a class to hold the property values for each inspection pass. If an inspectable
 * remains unchanged between reading passes, it should be possible for a {@code PropertyReader} to
 * avoid new allocations for subsequent reading passes.
 *
 * It has separate methods for all primitive types to avoid autoboxing overhead if a concrete
 * implementation is able to work with primitives. Implementations should be prepared to accept
 * {null} as the value of {@link PropertyReader#readObject(int, Object)}.
 *
 * @see InspectionCompanion#readProperties(Object, PropertyReader)
 */
public interface PropertyReader {
    /**
     * Read a primitive boolean property.
     *
     * @param id Identifier of the property from a {@link PropertyMapper}
     * @param value Value of the property
     * @throws PropertyTypeMismatchException If the property ID is not mapped as a {@code boolean}
     */
    void readBoolean(int id, boolean value);
 
    /**
     * Read a primitive byte property.
     *
     * @param id Identifier of the property from a {@link PropertyMapper}
     * @param value Value of the property
     * @throws PropertyTypeMismatchException If the property ID is not mapped as a {@code byte}
     */
    void readByte(int id, byte value);
 
    /**
     * Read a primitive character property.
     *
     * @param id Identifier of the property from a {@link PropertyMapper}
     * @param value Value of the property
     * @throws PropertyTypeMismatchException If the property ID is not mapped as a {@code char}
     */
    void readChar(int id, char value);
 
    /**
     * Read a read a primitive double property.
     *
     * @param id Identifier of the property from a {@link PropertyMapper}
     * @param value Value of the property
     * @throws PropertyTypeMismatchException If the property ID is not mapped as a {@code double}
     */
    void readDouble(int id, double value);
 
    /**
     * Read a primitive float property.
     *
     * @param id Identifier of the property from a {@link PropertyMapper}
     * @param value Value of the property
     * @throws PropertyTypeMismatchException If the property ID is not mapped as a {@code float}
     */
    void readFloat(int id, float value);
 
    /**
     * Read a primitive integer property.
     *
     * @param id Identifier of the property from a {@link PropertyMapper}
     * @param value Value of the property
     * @throws PropertyTypeMismatchException If the property ID is not mapped as an {@code int}
     */
    void readInt(int id, int value);
 
    /**
     * Read a primitive long property.
     *
     * @param id Identifier of the property from a {@link PropertyMapper}
     * @param value Value of the property
     * @throws PropertyTypeMismatchException If the property ID is not mapped as a {@code long}
     */
    void readLong(int id, long value);
 
    /**
     * Read a primitive short property.
     *
     * @param id Identifier of the property from a {@link PropertyMapper}
     * @param value Value of the property
     * @throws PropertyTypeMismatchException If the property ID is not mapped as a {@code short}
     */
    void readShort(int id, short value);
 
    /**
     * Read any object as a property.
     *
     * If value is null, the property is marked as empty.
     *
     * @param id Identifier of the property from a {@link PropertyMapper}
     * @param value Value of the property
     * @throws PropertyTypeMismatchException If the property ID is not mapped as an object
     */
    void readObject(int id, @Nullable Object value);
 
    /**
     * Read a color packed into a {@link ColorInt} as a property.
     *
     * @param id Identifier of the property from a {@link PropertyMapper}
     * @param value Value of the property
     * @throws PropertyTypeMismatchException If the property ID is not mapped as a color
     */
    void readColor(int id, @ColorInt int value);
 
    /**
     * Read a color packed into a {@code ColorLong} as a property.
     *
     * @param id Identifier of the property from a {@link PropertyMapper}
     * @param value Value of the property packed as a {@code ColorLong}. See the
     *              {@link Color} class for details of the packing.
     * @throws PropertyTypeMismatchException If the property ID is not mapped as a color
     */
    void readColor(int id, @ColorLong long value);
 
    /**
     * Read a {@link Color} object as a property.
     *
     * @param id Identifier of the property from a {@link PropertyMapper}
     * @param value Value of the property
     * @throws PropertyTypeMismatchException If the property ID is not mapped as a color
     */
    void readColor(int id, @Nullable Color value);
 
    /**
     * Read {@link android.view.Gravity} packed into an primitive {@code int}.
     *
     * @param id Identifier of the property from a {@link PropertyMapper}
     * @param value Value of the property
     * @throws PropertyTypeMismatchException If the property ID is not mapped as a gravity property
     */
    void readGravity(int id, int value);
 
    /**
     * Read an enumeration packed into a primitive {@code int}.
     *
     * @param id Identifier of the property from a {@link PropertyMapper}
     * @param value Value of the property
     * @throws PropertyTypeMismatchException If the property ID is not mapped as an object
     */
    void readIntEnum(int id, int value);
 
    /**
     * Read a flag packed into a primitive {@code int}.
     *
     * @param id Identifier of the property from a {@link PropertyMapper}
     * @param value Value of the property
     * @throws PropertyTypeMismatchException If the property ID is not mapped as an object
     */
    void readIntFlag(int id, int value);
 
    /**
     * Read an integer that contains a resource ID.
     *
     * @param id Identifier of the property from a {@link PropertyMapper}
     * @param value Value of the property
     * @throws PropertyTypeMismatchException If the property ID is not mapped as a resource ID.
     */
    void readResourceId(int id, @AnyRes int value);
 
    /**
     * Thrown if a client calls a typed read method for a property of a different type.
     */
    class PropertyTypeMismatchException extends RuntimeException {
        public PropertyTypeMismatchException(
                int id,
                @NonNull String expectedPropertyType,
                @NonNull String actualPropertyType,
                @Nullable String propertyName) {
            super(formatMessage(id, expectedPropertyType, actualPropertyType, propertyName));
        }
 
        public PropertyTypeMismatchException(
                int id,
                @NonNull String expectedPropertyType,
                @NonNull String actualPropertyType) {
            super(formatMessage(id, expectedPropertyType, actualPropertyType, null));
        }
 
        private static @NonNull String formatMessage(
                int id,
                @NonNull String expectedPropertyType,
                @NonNull String actualPropertyType,
                @Nullable String propertyName) {
 
            if (propertyName == null) {
                return String.format(
                        "Attempted to read property with ID 0x%08X as type %s, "
                            + "but the ID is of type %s.",
                        id,
                        expectedPropertyType,
                        actualPropertyType
                );
            } else {
                return String.format(
                        "Attempted to read property \"%s\" with ID 0x%08X as type %s, "
                            + "but the ID is of type %s.",
                        propertyName,
                        id,
                        expectedPropertyType,
                        actualPropertyType
                );
            }
        }
    }
}