blob: 9786f16bebaa861b075d30afc30a7bd24ae07322 [file] [log] [blame]
Andreas Huberdab5fc62016-08-15 09:25:02 -07001/*
2 * Copyright (C) 2016 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
17package android.os;
18
Steven Moreland4dde8a12018-01-10 15:45:36 -080019import android.annotation.IntDef;
Steven Moreland0ff061a2019-03-04 17:56:30 -080020import android.annotation.NonNull;
21import android.annotation.Nullable;
Steven Moreland4dde8a12018-01-10 15:45:36 -080022import android.annotation.SystemApi;
Steven Moreland14b9eb62019-01-11 10:19:51 -080023import android.annotation.TestApi;
Andrei Oneadcb67732019-03-18 11:37:25 +000024import android.annotation.UnsupportedAppUsage;
Andreas Huberef1a5652016-10-18 09:24:04 -070025
Daniel Colascionec94b8c32019-10-08 11:47:41 -070026import dalvik.annotation.optimization.FastNative;
27
Andreas Huberdab5fc62016-08-15 09:25:02 -070028import libcore.util.NativeAllocationRegistry;
29
Steven Moreland4dde8a12018-01-10 15:45:36 -080030import java.lang.annotation.Retention;
31import java.lang.annotation.RetentionPolicy;
32import java.util.ArrayList;
33import java.util.Arrays;
34
Andreas Huberdab5fc62016-08-15 09:25:02 -070035/** @hide */
Steven Moreland4dde8a12018-01-10 15:45:36 -080036@SystemApi
Steven Moreland14b9eb62019-01-11 10:19:51 -080037@TestApi
Andreas Huberdab5fc62016-08-15 09:25:02 -070038public class HwParcel {
39 private static final String TAG = "HwParcel";
40
Steven Moreland4dde8a12018-01-10 15:45:36 -080041 @IntDef(prefix = { "STATUS_" }, value = {
42 STATUS_SUCCESS,
43 })
44 @Retention(RetentionPolicy.SOURCE)
45 public @interface Status {}
46
47 /**
48 * Success return error for a transaction. Written to parcels
49 * using writeStatus.
50 */
Andreas Huberdab5fc62016-08-15 09:25:02 -070051 public static final int STATUS_SUCCESS = 0;
Andreas Huberdab5fc62016-08-15 09:25:02 -070052
53 private static final NativeAllocationRegistry sNativeRegistry;
54
Andrei Oneadcb67732019-03-18 11:37:25 +000055 @UnsupportedAppUsage
Andreas Huberdab5fc62016-08-15 09:25:02 -070056 private HwParcel(boolean allocate) {
57 native_setup(allocate);
58
59 sNativeRegistry.registerNativeAllocation(
60 this,
61 mNativeContext);
62 }
63
Steven Moreland4dde8a12018-01-10 15:45:36 -080064 /**
65 * Creates an initialized and empty parcel.
66 */
Andreas Huberdab5fc62016-08-15 09:25:02 -070067 public HwParcel() {
68 native_setup(true /* allocate */);
69
70 sNativeRegistry.registerNativeAllocation(
71 this,
72 mNativeContext);
73 }
74
Steven Moreland4dde8a12018-01-10 15:45:36 -080075 /**
76 * Writes an interface token into the parcel used to verify that
Daniel Colascionec94b8c32019-10-08 11:47:41 -070077 * a transaction has made it to the right type of interface.
Steven Moreland4dde8a12018-01-10 15:45:36 -080078 *
79 * @param interfaceName fully qualified name of interface message
80 * is being sent to.
81 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -070082 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -070083 public native final void writeInterfaceToken(String interfaceName);
Steven Moreland4dde8a12018-01-10 15:45:36 -080084 /**
85 * Writes a boolean value to the end of the parcel.
86 * @param val to write
87 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -070088 @FastNative
Andreas Huber86635bb2016-08-24 16:19:03 -070089 public native final void writeBool(boolean val);
Steven Moreland4dde8a12018-01-10 15:45:36 -080090 /**
91 * Writes a byte value to the end of the parcel.
92 * @param val to write
93 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -070094 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -070095 public native final void writeInt8(byte val);
Steven Moreland4dde8a12018-01-10 15:45:36 -080096 /**
97 * Writes a short value to the end of the parcel.
98 * @param val to write
99 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700100 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700101 public native final void writeInt16(short val);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800102 /**
103 * Writes a int value to the end of the parcel.
104 * @param val to write
105 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700106 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700107 public native final void writeInt32(int val);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800108 /**
109 * Writes a long value to the end of the parcel.
110 * @param val to write
111 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700112 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700113 public native final void writeInt64(long val);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800114 /**
115 * Writes a float value to the end of the parcel.
116 * @param val to write
117 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700118 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700119 public native final void writeFloat(float val);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800120 /**
121 * Writes a double value to the end of the parcel.
122 * @param val to write
123 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700124 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700125 public native final void writeDouble(double val);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800126 /**
127 * Writes a String value to the end of the parcel.
128 *
129 * Note, this will be converted to UTF-8 when it is written.
130 *
131 * @param val to write
132 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700133 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700134 public native final void writeString(String val);
Nirav Atre9850dd92018-07-24 17:03:44 -0700135 /**
136 * Writes a native handle (without duplicating the underlying
137 * file descriptors) to the end of the parcel.
138 *
139 * @param val to write
140 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700141 @FastNative
Steven Moreland0ff061a2019-03-04 17:56:30 -0800142 public native final void writeNativeHandle(@Nullable NativeHandle val);
Andreas Huberdab5fc62016-08-15 09:25:02 -0700143
Steven Moreland4dde8a12018-01-10 15:45:36 -0800144 /**
145 * Writes an array of boolean values to the end of the parcel.
146 * @param val to write
147 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700148 @FastNative
Andreas Huberef1a5652016-10-18 09:24:04 -0700149 private native final void writeBoolVector(boolean[] val);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800150 /**
151 * Writes an array of byte values to the end of the parcel.
152 * @param val to write
153 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700154 @FastNative
Andreas Huberef1a5652016-10-18 09:24:04 -0700155 private native final void writeInt8Vector(byte[] val);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800156 /**
157 * Writes an array of short values to the end of the parcel.
158 * @param val to write
159 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700160 @FastNative
Andreas Huberef1a5652016-10-18 09:24:04 -0700161 private native final void writeInt16Vector(short[] val);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800162 /**
163 * Writes an array of int values to the end of the parcel.
164 * @param val to write
165 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700166 @FastNative
Andreas Huberef1a5652016-10-18 09:24:04 -0700167 private native final void writeInt32Vector(int[] val);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800168 /**
169 * Writes an array of long values to the end of the parcel.
170 * @param val to write
171 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700172 @FastNative
Andreas Huberef1a5652016-10-18 09:24:04 -0700173 private native final void writeInt64Vector(long[] val);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800174 /**
175 * Writes an array of float values to the end of the parcel.
176 * @param val to write
177 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700178 @FastNative
Andreas Huberef1a5652016-10-18 09:24:04 -0700179 private native final void writeFloatVector(float[] val);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800180 /**
181 * Writes an array of double values to the end of the parcel.
182 * @param val to write
183 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700184 @FastNative
Andreas Huberef1a5652016-10-18 09:24:04 -0700185 private native final void writeDoubleVector(double[] val);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800186 /**
187 * Writes an array of String values to the end of the parcel.
188 *
189 * Note, these will be converted to UTF-8 as they are written.
190 *
191 * @param val to write
192 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700193 @FastNative
Andreas Huberef1a5652016-10-18 09:24:04 -0700194 private native final void writeStringVector(String[] val);
Nirav Atre9850dd92018-07-24 17:03:44 -0700195 /**
196 * Writes an array of native handles to the end of the parcel.
Steven Moreland0ff061a2019-03-04 17:56:30 -0800197 *
198 * Individual elements may be null but not the whole array.
199 *
Nirav Atre9850dd92018-07-24 17:03:44 -0700200 * @param val array of {@link NativeHandle} objects to write
201 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700202 @FastNative
Nirav Atre9850dd92018-07-24 17:03:44 -0700203 private native final void writeNativeHandleVector(NativeHandle[] val);
Andreas Huberef1a5652016-10-18 09:24:04 -0700204
Steven Moreland4dde8a12018-01-10 15:45:36 -0800205 /**
206 * Helper method to write a list of Booleans to val.
207 * @param val list to write
208 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700209 public final void writeBoolVector(ArrayList<Boolean> val) {
210 final int n = val.size();
211 boolean[] array = new boolean[n];
212 for (int i = 0; i < n; ++i) {
213 array[i] = val.get(i);
214 }
215
216 writeBoolVector(array);
217 }
218
Steven Moreland4dde8a12018-01-10 15:45:36 -0800219 /**
220 * Helper method to write a list of Booleans to the end of the parcel.
221 * @param val list to write
222 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700223 public final void writeInt8Vector(ArrayList<Byte> val) {
224 final int n = val.size();
225 byte[] array = new byte[n];
226 for (int i = 0; i < n; ++i) {
227 array[i] = val.get(i);
228 }
229
230 writeInt8Vector(array);
231 }
232
Steven Moreland4dde8a12018-01-10 15:45:36 -0800233 /**
234 * Helper method to write a list of Shorts to the end of the parcel.
235 * @param val list to write
236 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700237 public final void writeInt16Vector(ArrayList<Short> val) {
238 final int n = val.size();
239 short[] array = new short[n];
240 for (int i = 0; i < n; ++i) {
241 array[i] = val.get(i);
242 }
243
244 writeInt16Vector(array);
245 }
246
Steven Moreland4dde8a12018-01-10 15:45:36 -0800247 /**
248 * Helper method to write a list of Integers to the end of the parcel.
249 * @param val list to write
250 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700251 public final void writeInt32Vector(ArrayList<Integer> val) {
252 final int n = val.size();
253 int[] array = new int[n];
254 for (int i = 0; i < n; ++i) {
255 array[i] = val.get(i);
256 }
257
258 writeInt32Vector(array);
259 }
260
Steven Moreland4dde8a12018-01-10 15:45:36 -0800261 /**
262 * Helper method to write a list of Longs to the end of the parcel.
263 * @param val list to write
264 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700265 public final void writeInt64Vector(ArrayList<Long> val) {
266 final int n = val.size();
267 long[] array = new long[n];
268 for (int i = 0; i < n; ++i) {
269 array[i] = val.get(i);
270 }
271
272 writeInt64Vector(array);
273 }
274
Steven Moreland4dde8a12018-01-10 15:45:36 -0800275 /**
276 * Helper method to write a list of Floats to the end of the parcel.
277 * @param val list to write
278 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700279 public final void writeFloatVector(ArrayList<Float> val) {
280 final int n = val.size();
281 float[] array = new float[n];
282 for (int i = 0; i < n; ++i) {
283 array[i] = val.get(i);
284 }
285
286 writeFloatVector(array);
287 }
288
Steven Moreland4dde8a12018-01-10 15:45:36 -0800289 /**
290 * Helper method to write a list of Doubles to the end of the parcel.
291 * @param val list to write
292 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700293 public final void writeDoubleVector(ArrayList<Double> val) {
294 final int n = val.size();
295 double[] array = new double[n];
296 for (int i = 0; i < n; ++i) {
297 array[i] = val.get(i);
298 }
299
300 writeDoubleVector(array);
301 }
302
Steven Moreland4dde8a12018-01-10 15:45:36 -0800303 /**
304 * Helper method to write a list of Strings to the end of the parcel.
305 * @param val list to write
306 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700307 public final void writeStringVector(ArrayList<String> val) {
308 writeStringVector(val.toArray(new String[val.size()]));
309 }
Andreas Huberdab5fc62016-08-15 09:25:02 -0700310
Steven Moreland4dde8a12018-01-10 15:45:36 -0800311 /**
Nirav Atre9850dd92018-07-24 17:03:44 -0700312 * Helper method to write a list of native handles to the end of the parcel.
313 * @param val list of {@link NativeHandle} objects to write
314 */
Steven Moreland0ff061a2019-03-04 17:56:30 -0800315 public final void writeNativeHandleVector(@NonNull ArrayList<NativeHandle> val) {
Nirav Atre9850dd92018-07-24 17:03:44 -0700316 writeNativeHandleVector(val.toArray(new NativeHandle[val.size()]));
317 }
318
319 /**
Steven Moreland4dde8a12018-01-10 15:45:36 -0800320 * Write a hwbinder object to the end of the parcel.
321 * @param binder value to write
322 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700323 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700324 public native final void writeStrongBinder(IHwBinder binder);
325
Steven Moreland4dde8a12018-01-10 15:45:36 -0800326 /**
Ytai Ben-Tsvi4659e182019-11-06 09:53:34 -0800327 * Write a HidlMemory object (without duplicating the underlying file descriptors) to the end
328 * of the parcel.
329 *
330 * @param memory value to write
331 */
332 @FastNative
333 public native final void writeHidlMemory(@NonNull HidlMemory memory);
334
335 /**
Steven Moreland4dde8a12018-01-10 15:45:36 -0800336 * Checks to make sure that the interface name matches the name written by the parcel
337 * sender by writeInterfaceToken
338 *
339 * @throws SecurityException interface doesn't match
340 */
Andreas Huberdab5fc62016-08-15 09:25:02 -0700341 public native final void enforceInterface(String interfaceName);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800342
343 /**
344 * Reads a boolean value from the current location in the parcel.
345 * @return value parsed from the parcel
346 * @throws IllegalArgumentException if the parcel has no more data
347 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700348 @FastNative
Andreas Huber86635bb2016-08-24 16:19:03 -0700349 public native final boolean readBool();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800350 /**
351 * Reads a byte value from the current location in the parcel.
352 * @return value parsed from the parcel
353 * @throws IllegalArgumentException if the parcel has no more data
354 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700355 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700356 public native final byte readInt8();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800357 /**
358 * Reads a short value from the current location in the parcel.
359 * @return value parsed from the parcel
360 * @throws IllegalArgumentException if the parcel has no more data
361 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700362 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700363 public native final short readInt16();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800364 /**
365 * Reads a int value from the current location in the parcel.
366 * @return value parsed from the parcel
367 * @throws IllegalArgumentException if the parcel has no more data
368 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700369 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700370 public native final int readInt32();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800371 /**
372 * Reads a long value from the current location in the parcel.
373 * @return value parsed from the parcel
374 * @throws IllegalArgumentException if the parcel has no more data
375 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700376 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700377 public native final long readInt64();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800378 /**
379 * Reads a float value from the current location in the parcel.
380 * @return value parsed from the parcel
381 * @throws IllegalArgumentException if the parcel has no more data
382 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700383 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700384 public native final float readFloat();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800385 /**
386 * Reads a double value from the current location in the parcel.
387 * @return value parsed from the parcel
388 * @throws IllegalArgumentException if the parcel has no more data
389 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700390 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700391 public native final double readDouble();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800392 /**
393 * Reads a String value from the current location in the parcel.
394 * @return value parsed from the parcel
395 * @throws IllegalArgumentException if the parcel has no more data
396 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700397 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700398 public native final String readString();
Nirav Atre9850dd92018-07-24 17:03:44 -0700399 /**
400 * Reads a native handle (without duplicating the underlying file
401 * descriptors) from the parcel. These file descriptors will only
402 * be open for the duration that the binder window is open. If they
403 * are needed further, you must call {@link NativeHandle#dup()}.
404 *
405 * @return a {@link NativeHandle} instance parsed from the parcel
406 * @throws IllegalArgumentException if the parcel has no more data
407 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700408 @FastNative
Steven Moreland0ff061a2019-03-04 17:56:30 -0800409 public native final @Nullable NativeHandle readNativeHandle();
Nirav Atre9850dd92018-07-24 17:03:44 -0700410 /**
411 * Reads an embedded native handle (without duplicating the underlying
412 * file descriptors) from the parcel. These file descriptors will only
413 * be open for the duration that the binder window is open. If they
414 * are needed further, you must call {@link NativeHandle#dup()}. You
415 * do not need to call close on the NativeHandle returned from this.
416 *
417 * @param parentHandle handle from which to read the embedded object
418 * @param offset offset into parent
419 * @return a {@link NativeHandle} instance parsed from the parcel
420 * @throws IllegalArgumentException if the parcel has no more data
421 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700422 @FastNative
Steven Moreland0ff061a2019-03-04 17:56:30 -0800423 public native final @Nullable NativeHandle readEmbeddedNativeHandle(
Nirav Atre9850dd92018-07-24 17:03:44 -0700424 long parentHandle, long offset);
Andreas Huberdab5fc62016-08-15 09:25:02 -0700425
Steven Moreland4dde8a12018-01-10 15:45:36 -0800426 /**
427 * Reads an array of boolean values from the parcel.
428 * @return array of parsed values
429 * @throws IllegalArgumentException if the parcel has no more data
430 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700431 @FastNative
Andreas Huberef1a5652016-10-18 09:24:04 -0700432 private native final boolean[] readBoolVectorAsArray();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800433 /**
434 * Reads an array of byte values from the parcel.
435 * @return array of parsed values
436 * @throws IllegalArgumentException if the parcel has no more data
437 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700438 @FastNative
Andreas Huberef1a5652016-10-18 09:24:04 -0700439 private native final byte[] readInt8VectorAsArray();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800440 /**
441 * Reads an array of short values from the parcel.
442 * @return array of parsed values
443 * @throws IllegalArgumentException if the parcel has no more data
444 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700445 @FastNative
Andreas Huberef1a5652016-10-18 09:24:04 -0700446 private native final short[] readInt16VectorAsArray();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800447 /**
448 * Reads an array of int values from the parcel.
449 * @return array of parsed values
450 * @throws IllegalArgumentException if the parcel has no more data
451 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700452 @FastNative
Andreas Huberef1a5652016-10-18 09:24:04 -0700453 private native final int[] readInt32VectorAsArray();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800454 /**
455 * Reads an array of long values from the parcel.
456 * @return array of parsed values
457 * @throws IllegalArgumentException if the parcel has no more data
458 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700459 @FastNative
Andreas Huberef1a5652016-10-18 09:24:04 -0700460 private native final long[] readInt64VectorAsArray();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800461 /**
462 * Reads an array of float values from the parcel.
463 * @return array of parsed values
464 * @throws IllegalArgumentException if the parcel has no more data
465 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700466 @FastNative
Andreas Huberef1a5652016-10-18 09:24:04 -0700467 private native final float[] readFloatVectorAsArray();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800468 /**
469 * Reads an array of double values from the parcel.
470 * @return array of parsed values
471 * @throws IllegalArgumentException if the parcel has no more data
472 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700473 @FastNative
Andreas Huberef1a5652016-10-18 09:24:04 -0700474 private native final double[] readDoubleVectorAsArray();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800475 /**
476 * Reads an array of String values from the parcel.
477 * @return array of parsed values
478 * @throws IllegalArgumentException if the parcel has no more data
479 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700480 @FastNative
Andreas Huberef1a5652016-10-18 09:24:04 -0700481 private native final String[] readStringVectorAsArray();
Nirav Atre9850dd92018-07-24 17:03:44 -0700482 /**
483 * Reads an array of native handles from the parcel.
484 * @return array of {@link NativeHandle} objects
485 * @throws IllegalArgumentException if the parcel has no more data
486 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700487 @FastNative
Nirav Atre9850dd92018-07-24 17:03:44 -0700488 private native final NativeHandle[] readNativeHandleAsArray();
Andreas Huberef1a5652016-10-18 09:24:04 -0700489
Steven Moreland4dde8a12018-01-10 15:45:36 -0800490 /**
491 * Convenience method to read a Boolean vector as an ArrayList.
492 * @return array of parsed values.
493 * @throws IllegalArgumentException if the parcel has no more data
494 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700495 public final ArrayList<Boolean> readBoolVector() {
496 Boolean[] array = HwBlob.wrapArray(readBoolVectorAsArray());
497
498 return new ArrayList<Boolean>(Arrays.asList(array));
499 }
500
Steven Moreland4dde8a12018-01-10 15:45:36 -0800501 /**
502 * Convenience method to read a Byte vector as an ArrayList.
503 * @return array of parsed values.
504 * @throws IllegalArgumentException if the parcel has no more data
505 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700506 public final ArrayList<Byte> readInt8Vector() {
507 Byte[] array = HwBlob.wrapArray(readInt8VectorAsArray());
508
509 return new ArrayList<Byte>(Arrays.asList(array));
510 }
511
Steven Moreland4dde8a12018-01-10 15:45:36 -0800512 /**
513 * Convenience method to read a Short vector as an ArrayList.
514 * @return array of parsed values.
515 * @throws IllegalArgumentException if the parcel has no more data
516 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700517 public final ArrayList<Short> readInt16Vector() {
518 Short[] array = HwBlob.wrapArray(readInt16VectorAsArray());
519
520 return new ArrayList<Short>(Arrays.asList(array));
521 }
522
Steven Moreland4dde8a12018-01-10 15:45:36 -0800523 /**
524 * Convenience method to read a Integer vector as an ArrayList.
525 * @return array of parsed values.
526 * @throws IllegalArgumentException if the parcel has no more data
527 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700528 public final ArrayList<Integer> readInt32Vector() {
529 Integer[] array = HwBlob.wrapArray(readInt32VectorAsArray());
530
531 return new ArrayList<Integer>(Arrays.asList(array));
532 }
533
Steven Moreland4dde8a12018-01-10 15:45:36 -0800534 /**
535 * Convenience method to read a Long vector as an ArrayList.
536 * @return array of parsed values.
537 * @throws IllegalArgumentException if the parcel has no more data
538 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700539 public final ArrayList<Long> readInt64Vector() {
540 Long[] array = HwBlob.wrapArray(readInt64VectorAsArray());
541
542 return new ArrayList<Long>(Arrays.asList(array));
543 }
544
Steven Moreland4dde8a12018-01-10 15:45:36 -0800545 /**
546 * Convenience method to read a Float vector as an ArrayList.
547 * @return array of parsed values.
548 * @throws IllegalArgumentException if the parcel has no more data
549 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700550 public final ArrayList<Float> readFloatVector() {
551 Float[] array = HwBlob.wrapArray(readFloatVectorAsArray());
552
553 return new ArrayList<Float>(Arrays.asList(array));
554 }
555
Steven Moreland4dde8a12018-01-10 15:45:36 -0800556 /**
557 * Convenience method to read a Double vector as an ArrayList.
558 * @return array of parsed values.
559 * @throws IllegalArgumentException if the parcel has no more data
560 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700561 public final ArrayList<Double> readDoubleVector() {
562 Double[] array = HwBlob.wrapArray(readDoubleVectorAsArray());
563
564 return new ArrayList<Double>(Arrays.asList(array));
565 }
566
Steven Moreland4dde8a12018-01-10 15:45:36 -0800567 /**
568 * Convenience method to read a String vector as an ArrayList.
569 * @return array of parsed values.
570 * @throws IllegalArgumentException if the parcel has no more data
571 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700572 public final ArrayList<String> readStringVector() {
573 return new ArrayList<String>(Arrays.asList(readStringVectorAsArray()));
574 }
Andreas Huberdab5fc62016-08-15 09:25:02 -0700575
Steven Moreland4dde8a12018-01-10 15:45:36 -0800576 /**
Nirav Atre9850dd92018-07-24 17:03:44 -0700577 * Convenience method to read a vector of native handles as an ArrayList.
578 * @return array of {@link NativeHandle} objects.
579 * @throws IllegalArgumentException if the parcel has no more data
580 */
Steven Moreland0ff061a2019-03-04 17:56:30 -0800581 public final @NonNull ArrayList<NativeHandle> readNativeHandleVector() {
Nirav Atre9850dd92018-07-24 17:03:44 -0700582 return new ArrayList<NativeHandle>(Arrays.asList(readNativeHandleAsArray()));
583 }
584
585 /**
Steven Moreland4dde8a12018-01-10 15:45:36 -0800586 * Reads a strong binder value from the parcel.
587 * @return binder object read from parcel or null if no binder can be read
588 * @throws IllegalArgumentException if the parcel has no more data
589 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700590 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700591 public native final IHwBinder readStrongBinder();
592
Steven Moreland4dde8a12018-01-10 15:45:36 -0800593 /**
Ytai Ben-Tsvi4659e182019-11-06 09:53:34 -0800594 * Reads a HidlMemory value (without duplicating the underlying file
595 * descriptors) from the parcel. These file descriptors will only
596 * be open for the duration that the binder window is open. If they
597 * are needed further, you must call {@link HidlMemory#dup()}, which makes you also
598 * responsible for calling {@link HidlMemory#close()}.
599 *
600 * @return HidlMemory object read from parcel.
601 * @throws IllegalArgumentException if the parcel has no more data or is otherwise corrupt.
602 */
603 @FastNative
604 @NonNull
605 public native final HidlMemory readHidlMemory();
606
607 /**
608 * Reads an embedded HidlMemory (without duplicating the underlying
609 * file descriptors) from the parcel. These file descriptors will only
610 * be open for the duration that the binder window is open. If they
611 * are needed further, you must call {@link HidlMemory#dup()}. You
612 * do not need to call close on the HidlMemory returned from this.
613 *
614 * @param fieldHandle handle of the field, obtained from the {@link HwBlob}.
615 * @param parentHandle parentHandle from which to read the embedded object
616 * @param offset offset into parent
617 * @return a {@link HidlMemory} instance parsed from the parcel
618 * @throws IllegalArgumentException if the parcel has no more data
619 */
620 @FastNative
621 @NonNull
622 public native final @Nullable
623 HidlMemory readEmbeddedHidlMemory(long fieldHandle, long parentHandle, long offset);
624
625 /**
Steven Moreland4dde8a12018-01-10 15:45:36 -0800626 * Read opaque segment of data as a blob.
627 * @return blob of size expectedSize
628 * @throws IllegalArgumentException if the parcel has no more data
629 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700630 @FastNative
Martijn Coenen932b5042017-04-18 15:54:43 -0700631 public native final HwBlob readBuffer(long expectedSize);
Andreas Huber9266f992016-08-25 11:21:21 -0700632
Steven Moreland4dde8a12018-01-10 15:45:36 -0800633 /**
634 * Read a buffer written using scatter gather.
635 *
636 * @param expectedSize size that buffer should be
637 * @param parentHandle handle from which to read the embedded buffer
638 * @param offset offset into parent
639 * @param nullable whether or not to allow for a null return
640 * @return blob of data with size expectedSize
641 * @throws NoSuchElementException if an embedded buffer is not available to read
642 * @throws IllegalArgumentException if expectedSize < 0
643 * @throws NullPointerException if the transaction specified the blob to be null
644 * but nullable is false
645 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700646 @FastNative
Andreas Huber9266f992016-08-25 11:21:21 -0700647 public native final HwBlob readEmbeddedBuffer(
Martijn Coenen932b5042017-04-18 15:54:43 -0700648 long expectedSize, long parentHandle, long offset,
649 boolean nullable);
Andreas Huber9266f992016-08-25 11:21:21 -0700650
Steven Moreland4dde8a12018-01-10 15:45:36 -0800651 /**
652 * Write a buffer into the transaction.
653 * @param blob blob to write into the parcel.
654 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700655 @FastNative
Andreas Huber9266f992016-08-25 11:21:21 -0700656 public native final void writeBuffer(HwBlob blob);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800657 /**
658 * Write a status value into the blob.
659 * @param status value to write
660 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700661 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700662 public native final void writeStatus(int status);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800663 /**
664 * @throws IllegalArgumentException if a success vaue cannot be read
665 * @throws RemoteException if success value indicates a transaction error
666 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700667 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700668 public native final void verifySuccess();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800669 /**
670 * Should be called to reduce memory pressure when this object no longer needs
671 * to be written to.
672 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700673 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700674 public native final void releaseTemporaryStorage();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800675 /**
676 * Should be called when object is no longer needed to reduce possible memory
677 * pressure if the Java GC does not get to this object in time.
678 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700679 @FastNative
Martijn Coenen3d726d12017-03-16 18:46:42 +0100680 public native final void release();
Andreas Huberdab5fc62016-08-15 09:25:02 -0700681
Steven Moreland4dde8a12018-01-10 15:45:36 -0800682 /**
683 * Sends the parcel to the specified destination.
684 */
Andreas Huberdab5fc62016-08-15 09:25:02 -0700685 public native final void send();
686
687 // Returns address of the "freeFunction".
688 private static native final long native_init();
689
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700690 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700691 private native final void native_setup(boolean allocate);
692
693 static {
694 long freeFunction = native_init();
695
696 sNativeRegistry = new NativeAllocationRegistry(
697 HwParcel.class.getClassLoader(),
698 freeFunction,
699 128 /* size */);
700 }
701
702 private long mNativeContext;
703}
704