blob: 7919a00166bf79dbe52860bc6f115b4ee0d14dfb [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;
20import android.annotation.SystemApi;
Steven Morelandcbba4c72019-01-11 10:19:51 -080021import android.annotation.TestApi;
Andreas Huberef1a5652016-10-18 09:24:04 -070022
Andreas Huberdab5fc62016-08-15 09:25:02 -070023import libcore.util.NativeAllocationRegistry;
24
Steven Moreland4dde8a12018-01-10 15:45:36 -080025import java.lang.annotation.Retention;
26import java.lang.annotation.RetentionPolicy;
27import java.util.ArrayList;
28import java.util.Arrays;
29
Andreas Huberdab5fc62016-08-15 09:25:02 -070030/** @hide */
Steven Moreland4dde8a12018-01-10 15:45:36 -080031@SystemApi
Steven Morelandcbba4c72019-01-11 10:19:51 -080032@TestApi
Andreas Huberdab5fc62016-08-15 09:25:02 -070033public class HwParcel {
34 private static final String TAG = "HwParcel";
35
Steven Moreland4dde8a12018-01-10 15:45:36 -080036 @IntDef(prefix = { "STATUS_" }, value = {
37 STATUS_SUCCESS,
38 })
39 @Retention(RetentionPolicy.SOURCE)
40 public @interface Status {}
41
42 /**
43 * Success return error for a transaction. Written to parcels
44 * using writeStatus.
45 */
Andreas Huberdab5fc62016-08-15 09:25:02 -070046 public static final int STATUS_SUCCESS = 0;
Andreas Huberdab5fc62016-08-15 09:25:02 -070047
48 private static final NativeAllocationRegistry sNativeRegistry;
49
50 private HwParcel(boolean allocate) {
51 native_setup(allocate);
52
53 sNativeRegistry.registerNativeAllocation(
54 this,
55 mNativeContext);
56 }
57
Steven Moreland4dde8a12018-01-10 15:45:36 -080058 /**
59 * Creates an initialized and empty parcel.
60 */
Andreas Huberdab5fc62016-08-15 09:25:02 -070061 public HwParcel() {
62 native_setup(true /* allocate */);
63
64 sNativeRegistry.registerNativeAllocation(
65 this,
66 mNativeContext);
67 }
68
Steven Moreland4dde8a12018-01-10 15:45:36 -080069 /**
70 * Writes an interface token into the parcel used to verify that
71 * a transaction has made it to the write type of interface.
72 *
73 * @param interfaceName fully qualified name of interface message
74 * is being sent to.
75 */
Andreas Huberdab5fc62016-08-15 09:25:02 -070076 public native final void writeInterfaceToken(String interfaceName);
Steven Moreland4dde8a12018-01-10 15:45:36 -080077 /**
78 * Writes a boolean value to the end of the parcel.
79 * @param val to write
80 */
Andreas Huber86635bb2016-08-24 16:19:03 -070081 public native final void writeBool(boolean val);
Steven Moreland4dde8a12018-01-10 15:45:36 -080082 /**
83 * Writes a byte value to the end of the parcel.
84 * @param val to write
85 */
Andreas Huberdab5fc62016-08-15 09:25:02 -070086 public native final void writeInt8(byte val);
Steven Moreland4dde8a12018-01-10 15:45:36 -080087 /**
88 * Writes a short value to the end of the parcel.
89 * @param val to write
90 */
Andreas Huberdab5fc62016-08-15 09:25:02 -070091 public native final void writeInt16(short val);
Steven Moreland4dde8a12018-01-10 15:45:36 -080092 /**
93 * Writes a int value to the end of the parcel.
94 * @param val to write
95 */
Andreas Huberdab5fc62016-08-15 09:25:02 -070096 public native final void writeInt32(int val);
Steven Moreland4dde8a12018-01-10 15:45:36 -080097 /**
98 * Writes a long value to the end of the parcel.
99 * @param val to write
100 */
Andreas Huberdab5fc62016-08-15 09:25:02 -0700101 public native final void writeInt64(long val);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800102 /**
103 * Writes a float value to the end of the parcel.
104 * @param val to write
105 */
Andreas Huberdab5fc62016-08-15 09:25:02 -0700106 public native final void writeFloat(float val);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800107 /**
108 * Writes a double value to the end of the parcel.
109 * @param val to write
110 */
Andreas Huberdab5fc62016-08-15 09:25:02 -0700111 public native final void writeDouble(double val);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800112 /**
113 * Writes a String value to the end of the parcel.
114 *
115 * Note, this will be converted to UTF-8 when it is written.
116 *
117 * @param val to write
118 */
Andreas Huberdab5fc62016-08-15 09:25:02 -0700119 public native final void writeString(String val);
Nirav Atre9850dd92018-07-24 17:03:44 -0700120 /**
121 * Writes a native handle (without duplicating the underlying
122 * file descriptors) to the end of the parcel.
123 *
124 * @param val to write
125 */
126 public native final void writeNativeHandle(NativeHandle val);
Andreas Huberdab5fc62016-08-15 09:25:02 -0700127
Steven Moreland4dde8a12018-01-10 15:45:36 -0800128 /**
129 * Writes an array of boolean values to the end of the parcel.
130 * @param val to write
131 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700132 private native final void writeBoolVector(boolean[] val);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800133 /**
134 * Writes an array of byte values to the end of the parcel.
135 * @param val to write
136 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700137 private native final void writeInt8Vector(byte[] val);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800138 /**
139 * Writes an array of short values to the end of the parcel.
140 * @param val to write
141 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700142 private native final void writeInt16Vector(short[] val);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800143 /**
144 * Writes an array of int values to the end of the parcel.
145 * @param val to write
146 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700147 private native final void writeInt32Vector(int[] val);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800148 /**
149 * Writes an array of long values to the end of the parcel.
150 * @param val to write
151 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700152 private native final void writeInt64Vector(long[] val);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800153 /**
154 * Writes an array of float values to the end of the parcel.
155 * @param val to write
156 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700157 private native final void writeFloatVector(float[] val);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800158 /**
159 * Writes an array of double values to the end of the parcel.
160 * @param val to write
161 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700162 private native final void writeDoubleVector(double[] val);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800163 /**
164 * Writes an array of String values to the end of the parcel.
165 *
166 * Note, these will be converted to UTF-8 as they are written.
167 *
168 * @param val to write
169 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700170 private native final void writeStringVector(String[] val);
Nirav Atre9850dd92018-07-24 17:03:44 -0700171 /**
172 * Writes an array of native handles to the end of the parcel.
173 * @param val array of {@link NativeHandle} objects to write
174 */
175 private native final void writeNativeHandleVector(NativeHandle[] val);
Andreas Huberef1a5652016-10-18 09:24:04 -0700176
Steven Moreland4dde8a12018-01-10 15:45:36 -0800177 /**
178 * Helper method to write a list of Booleans to val.
179 * @param val list to write
180 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700181 public final void writeBoolVector(ArrayList<Boolean> val) {
182 final int n = val.size();
183 boolean[] array = new boolean[n];
184 for (int i = 0; i < n; ++i) {
185 array[i] = val.get(i);
186 }
187
188 writeBoolVector(array);
189 }
190
Steven Moreland4dde8a12018-01-10 15:45:36 -0800191 /**
192 * Helper method to write a list of Booleans to the end of the parcel.
193 * @param val list to write
194 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700195 public final void writeInt8Vector(ArrayList<Byte> val) {
196 final int n = val.size();
197 byte[] array = new byte[n];
198 for (int i = 0; i < n; ++i) {
199 array[i] = val.get(i);
200 }
201
202 writeInt8Vector(array);
203 }
204
Steven Moreland4dde8a12018-01-10 15:45:36 -0800205 /**
206 * Helper method to write a list of Shorts to the end of the parcel.
207 * @param val list to write
208 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700209 public final void writeInt16Vector(ArrayList<Short> val) {
210 final int n = val.size();
211 short[] array = new short[n];
212 for (int i = 0; i < n; ++i) {
213 array[i] = val.get(i);
214 }
215
216 writeInt16Vector(array);
217 }
218
Steven Moreland4dde8a12018-01-10 15:45:36 -0800219 /**
220 * Helper method to write a list of Integers to the end of the parcel.
221 * @param val list to write
222 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700223 public final void writeInt32Vector(ArrayList<Integer> val) {
224 final int n = val.size();
225 int[] array = new int[n];
226 for (int i = 0; i < n; ++i) {
227 array[i] = val.get(i);
228 }
229
230 writeInt32Vector(array);
231 }
232
Steven Moreland4dde8a12018-01-10 15:45:36 -0800233 /**
234 * Helper method to write a list of Longs to the end of the parcel.
235 * @param val list to write
236 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700237 public final void writeInt64Vector(ArrayList<Long> val) {
238 final int n = val.size();
239 long[] array = new long[n];
240 for (int i = 0; i < n; ++i) {
241 array[i] = val.get(i);
242 }
243
244 writeInt64Vector(array);
245 }
246
Steven Moreland4dde8a12018-01-10 15:45:36 -0800247 /**
248 * Helper method to write a list of Floats to the end of the parcel.
249 * @param val list to write
250 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700251 public final void writeFloatVector(ArrayList<Float> val) {
252 final int n = val.size();
253 float[] array = new float[n];
254 for (int i = 0; i < n; ++i) {
255 array[i] = val.get(i);
256 }
257
258 writeFloatVector(array);
259 }
260
Steven Moreland4dde8a12018-01-10 15:45:36 -0800261 /**
262 * Helper method to write a list of Doubles to the end of the parcel.
263 * @param val list to write
264 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700265 public final void writeDoubleVector(ArrayList<Double> val) {
266 final int n = val.size();
267 double[] array = new double[n];
268 for (int i = 0; i < n; ++i) {
269 array[i] = val.get(i);
270 }
271
272 writeDoubleVector(array);
273 }
274
Steven Moreland4dde8a12018-01-10 15:45:36 -0800275 /**
276 * Helper method to write a list of Strings to the end of the parcel.
277 * @param val list to write
278 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700279 public final void writeStringVector(ArrayList<String> val) {
280 writeStringVector(val.toArray(new String[val.size()]));
281 }
Andreas Huberdab5fc62016-08-15 09:25:02 -0700282
Steven Moreland4dde8a12018-01-10 15:45:36 -0800283 /**
Nirav Atre9850dd92018-07-24 17:03:44 -0700284 * Helper method to write a list of native handles to the end of the parcel.
285 * @param val list of {@link NativeHandle} objects to write
286 */
287 public final void writeNativeHandleVector(ArrayList<NativeHandle> val) {
288 writeNativeHandleVector(val.toArray(new NativeHandle[val.size()]));
289 }
290
291 /**
Steven Moreland4dde8a12018-01-10 15:45:36 -0800292 * Write a hwbinder object to the end of the parcel.
293 * @param binder value to write
294 */
Andreas Huberdab5fc62016-08-15 09:25:02 -0700295 public native final void writeStrongBinder(IHwBinder binder);
296
Steven Moreland4dde8a12018-01-10 15:45:36 -0800297 /**
298 * Checks to make sure that the interface name matches the name written by the parcel
299 * sender by writeInterfaceToken
300 *
301 * @throws SecurityException interface doesn't match
302 */
Andreas Huberdab5fc62016-08-15 09:25:02 -0700303 public native final void enforceInterface(String interfaceName);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800304
305 /**
306 * Reads a boolean value from the current location in the parcel.
307 * @return value parsed from the parcel
308 * @throws IllegalArgumentException if the parcel has no more data
309 */
Andreas Huber86635bb2016-08-24 16:19:03 -0700310 public native final boolean readBool();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800311 /**
312 * Reads a byte value from the current location in the parcel.
313 * @return value parsed from the parcel
314 * @throws IllegalArgumentException if the parcel has no more data
315 */
Andreas Huberdab5fc62016-08-15 09:25:02 -0700316 public native final byte readInt8();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800317 /**
318 * Reads a short value from the current location in the parcel.
319 * @return value parsed from the parcel
320 * @throws IllegalArgumentException if the parcel has no more data
321 */
Andreas Huberdab5fc62016-08-15 09:25:02 -0700322 public native final short readInt16();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800323 /**
324 * Reads a int value from the current location in the parcel.
325 * @return value parsed from the parcel
326 * @throws IllegalArgumentException if the parcel has no more data
327 */
Andreas Huberdab5fc62016-08-15 09:25:02 -0700328 public native final int readInt32();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800329 /**
330 * Reads a long value from the current location in the parcel.
331 * @return value parsed from the parcel
332 * @throws IllegalArgumentException if the parcel has no more data
333 */
Andreas Huberdab5fc62016-08-15 09:25:02 -0700334 public native final long readInt64();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800335 /**
336 * Reads a float value from the current location in the parcel.
337 * @return value parsed from the parcel
338 * @throws IllegalArgumentException if the parcel has no more data
339 */
Andreas Huberdab5fc62016-08-15 09:25:02 -0700340 public native final float readFloat();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800341 /**
342 * Reads a double value from the current location in the parcel.
343 * @return value parsed from the parcel
344 * @throws IllegalArgumentException if the parcel has no more data
345 */
Andreas Huberdab5fc62016-08-15 09:25:02 -0700346 public native final double readDouble();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800347 /**
348 * Reads a String value from the current location in the parcel.
349 * @return value parsed from the parcel
350 * @throws IllegalArgumentException if the parcel has no more data
351 */
Andreas Huberdab5fc62016-08-15 09:25:02 -0700352 public native final String readString();
Nirav Atre9850dd92018-07-24 17:03:44 -0700353 /**
354 * Reads a native handle (without duplicating the underlying file
355 * descriptors) from the parcel. These file descriptors will only
356 * be open for the duration that the binder window is open. If they
357 * are needed further, you must call {@link NativeHandle#dup()}.
358 *
359 * @return a {@link NativeHandle} instance parsed from the parcel
360 * @throws IllegalArgumentException if the parcel has no more data
361 */
362 public native final NativeHandle readNativeHandle();
363 /**
364 * Reads an embedded native handle (without duplicating the underlying
365 * file descriptors) from the parcel. These file descriptors will only
366 * be open for the duration that the binder window is open. If they
367 * are needed further, you must call {@link NativeHandle#dup()}. You
368 * do not need to call close on the NativeHandle returned from this.
369 *
370 * @param parentHandle handle from which to read the embedded object
371 * @param offset offset into parent
372 * @return a {@link NativeHandle} instance parsed from the parcel
373 * @throws IllegalArgumentException if the parcel has no more data
374 */
375 public native final NativeHandle readEmbeddedNativeHandle(
376 long parentHandle, long offset);
Andreas Huberdab5fc62016-08-15 09:25:02 -0700377
Steven Moreland4dde8a12018-01-10 15:45:36 -0800378 /**
379 * Reads an array of boolean values from the parcel.
380 * @return array of parsed values
381 * @throws IllegalArgumentException if the parcel has no more data
382 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700383 private native final boolean[] readBoolVectorAsArray();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800384 /**
385 * Reads an array of byte values from the parcel.
386 * @return array of parsed values
387 * @throws IllegalArgumentException if the parcel has no more data
388 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700389 private native final byte[] readInt8VectorAsArray();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800390 /**
391 * Reads an array of short values from the parcel.
392 * @return array of parsed values
393 * @throws IllegalArgumentException if the parcel has no more data
394 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700395 private native final short[] readInt16VectorAsArray();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800396 /**
397 * Reads an array of int values from the parcel.
398 * @return array of parsed values
399 * @throws IllegalArgumentException if the parcel has no more data
400 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700401 private native final int[] readInt32VectorAsArray();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800402 /**
403 * Reads an array of long values from the parcel.
404 * @return array of parsed values
405 * @throws IllegalArgumentException if the parcel has no more data
406 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700407 private native final long[] readInt64VectorAsArray();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800408 /**
409 * Reads an array of float values from the parcel.
410 * @return array of parsed values
411 * @throws IllegalArgumentException if the parcel has no more data
412 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700413 private native final float[] readFloatVectorAsArray();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800414 /**
415 * Reads an array of double values from the parcel.
416 * @return array of parsed values
417 * @throws IllegalArgumentException if the parcel has no more data
418 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700419 private native final double[] readDoubleVectorAsArray();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800420 /**
421 * Reads an array of String values from the parcel.
422 * @return array of parsed values
423 * @throws IllegalArgumentException if the parcel has no more data
424 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700425 private native final String[] readStringVectorAsArray();
Nirav Atre9850dd92018-07-24 17:03:44 -0700426 /**
427 * Reads an array of native handles from the parcel.
428 * @return array of {@link NativeHandle} objects
429 * @throws IllegalArgumentException if the parcel has no more data
430 */
431 private native final NativeHandle[] readNativeHandleAsArray();
Andreas Huberef1a5652016-10-18 09:24:04 -0700432
Steven Moreland4dde8a12018-01-10 15:45:36 -0800433 /**
434 * Convenience method to read a Boolean vector as an ArrayList.
435 * @return array of parsed values.
436 * @throws IllegalArgumentException if the parcel has no more data
437 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700438 public final ArrayList<Boolean> readBoolVector() {
439 Boolean[] array = HwBlob.wrapArray(readBoolVectorAsArray());
440
441 return new ArrayList<Boolean>(Arrays.asList(array));
442 }
443
Steven Moreland4dde8a12018-01-10 15:45:36 -0800444 /**
445 * Convenience method to read a Byte vector as an ArrayList.
446 * @return array of parsed values.
447 * @throws IllegalArgumentException if the parcel has no more data
448 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700449 public final ArrayList<Byte> readInt8Vector() {
450 Byte[] array = HwBlob.wrapArray(readInt8VectorAsArray());
451
452 return new ArrayList<Byte>(Arrays.asList(array));
453 }
454
Steven Moreland4dde8a12018-01-10 15:45:36 -0800455 /**
456 * Convenience method to read a Short vector as an ArrayList.
457 * @return array of parsed values.
458 * @throws IllegalArgumentException if the parcel has no more data
459 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700460 public final ArrayList<Short> readInt16Vector() {
461 Short[] array = HwBlob.wrapArray(readInt16VectorAsArray());
462
463 return new ArrayList<Short>(Arrays.asList(array));
464 }
465
Steven Moreland4dde8a12018-01-10 15:45:36 -0800466 /**
467 * Convenience method to read a Integer vector as an ArrayList.
468 * @return array of parsed values.
469 * @throws IllegalArgumentException if the parcel has no more data
470 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700471 public final ArrayList<Integer> readInt32Vector() {
472 Integer[] array = HwBlob.wrapArray(readInt32VectorAsArray());
473
474 return new ArrayList<Integer>(Arrays.asList(array));
475 }
476
Steven Moreland4dde8a12018-01-10 15:45:36 -0800477 /**
478 * Convenience method to read a Long vector as an ArrayList.
479 * @return array of parsed values.
480 * @throws IllegalArgumentException if the parcel has no more data
481 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700482 public final ArrayList<Long> readInt64Vector() {
483 Long[] array = HwBlob.wrapArray(readInt64VectorAsArray());
484
485 return new ArrayList<Long>(Arrays.asList(array));
486 }
487
Steven Moreland4dde8a12018-01-10 15:45:36 -0800488 /**
489 * Convenience method to read a Float vector as an ArrayList.
490 * @return array of parsed values.
491 * @throws IllegalArgumentException if the parcel has no more data
492 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700493 public final ArrayList<Float> readFloatVector() {
494 Float[] array = HwBlob.wrapArray(readFloatVectorAsArray());
495
496 return new ArrayList<Float>(Arrays.asList(array));
497 }
498
Steven Moreland4dde8a12018-01-10 15:45:36 -0800499 /**
500 * Convenience method to read a Double vector as an ArrayList.
501 * @return array of parsed values.
502 * @throws IllegalArgumentException if the parcel has no more data
503 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700504 public final ArrayList<Double> readDoubleVector() {
505 Double[] array = HwBlob.wrapArray(readDoubleVectorAsArray());
506
507 return new ArrayList<Double>(Arrays.asList(array));
508 }
509
Steven Moreland4dde8a12018-01-10 15:45:36 -0800510 /**
511 * Convenience method to read a String vector as an ArrayList.
512 * @return array of parsed values.
513 * @throws IllegalArgumentException if the parcel has no more data
514 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700515 public final ArrayList<String> readStringVector() {
516 return new ArrayList<String>(Arrays.asList(readStringVectorAsArray()));
517 }
Andreas Huberdab5fc62016-08-15 09:25:02 -0700518
Steven Moreland4dde8a12018-01-10 15:45:36 -0800519 /**
Nirav Atre9850dd92018-07-24 17:03:44 -0700520 * Convenience method to read a vector of native handles as an ArrayList.
521 * @return array of {@link NativeHandle} objects.
522 * @throws IllegalArgumentException if the parcel has no more data
523 */
524 public final ArrayList<NativeHandle> readNativeHandleVector() {
525 return new ArrayList<NativeHandle>(Arrays.asList(readNativeHandleAsArray()));
526 }
527
528 /**
Steven Moreland4dde8a12018-01-10 15:45:36 -0800529 * Reads a strong binder value from the parcel.
530 * @return binder object read from parcel or null if no binder can be read
531 * @throws IllegalArgumentException if the parcel has no more data
532 */
Andreas Huberdab5fc62016-08-15 09:25:02 -0700533 public native final IHwBinder readStrongBinder();
534
Steven Moreland4dde8a12018-01-10 15:45:36 -0800535 /**
536 * Read opaque segment of data as a blob.
537 * @return blob of size expectedSize
538 * @throws IllegalArgumentException if the parcel has no more data
539 */
Martijn Coenen932b5042017-04-18 15:54:43 -0700540 public native final HwBlob readBuffer(long expectedSize);
Andreas Huber9266f992016-08-25 11:21:21 -0700541
Steven Moreland4dde8a12018-01-10 15:45:36 -0800542 /**
543 * Read a buffer written using scatter gather.
544 *
545 * @param expectedSize size that buffer should be
546 * @param parentHandle handle from which to read the embedded buffer
547 * @param offset offset into parent
548 * @param nullable whether or not to allow for a null return
549 * @return blob of data with size expectedSize
550 * @throws NoSuchElementException if an embedded buffer is not available to read
551 * @throws IllegalArgumentException if expectedSize < 0
552 * @throws NullPointerException if the transaction specified the blob to be null
553 * but nullable is false
554 */
Andreas Huber9266f992016-08-25 11:21:21 -0700555 public native final HwBlob readEmbeddedBuffer(
Martijn Coenen932b5042017-04-18 15:54:43 -0700556 long expectedSize, long parentHandle, long offset,
557 boolean nullable);
Andreas Huber9266f992016-08-25 11:21:21 -0700558
Steven Moreland4dde8a12018-01-10 15:45:36 -0800559 /**
560 * Write a buffer into the transaction.
561 * @param blob blob to write into the parcel.
562 */
Andreas Huber9266f992016-08-25 11:21:21 -0700563 public native final void writeBuffer(HwBlob blob);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800564 /**
565 * Write a status value into the blob.
566 * @param status value to write
567 */
Andreas Huberdab5fc62016-08-15 09:25:02 -0700568 public native final void writeStatus(int status);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800569 /**
570 * @throws IllegalArgumentException if a success vaue cannot be read
571 * @throws RemoteException if success value indicates a transaction error
572 */
Andreas Huberdab5fc62016-08-15 09:25:02 -0700573 public native final void verifySuccess();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800574 /**
575 * Should be called to reduce memory pressure when this object no longer needs
576 * to be written to.
577 */
Andreas Huberdab5fc62016-08-15 09:25:02 -0700578 public native final void releaseTemporaryStorage();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800579 /**
580 * Should be called when object is no longer needed to reduce possible memory
581 * pressure if the Java GC does not get to this object in time.
582 */
Martijn Coenen3d726d12017-03-16 18:46:42 +0100583 public native final void release();
Andreas Huberdab5fc62016-08-15 09:25:02 -0700584
Steven Moreland4dde8a12018-01-10 15:45:36 -0800585 /**
586 * Sends the parcel to the specified destination.
587 */
Andreas Huberdab5fc62016-08-15 09:25:02 -0700588 public native final void send();
589
590 // Returns address of the "freeFunction".
591 private static native final long native_init();
592
593 private native final void native_setup(boolean allocate);
594
595 static {
596 long freeFunction = native_init();
597
598 sNativeRegistry = new NativeAllocationRegistry(
599 HwParcel.class.getClassLoader(),
600 freeFunction,
601 128 /* size */);
602 }
603
604 private long mNativeContext;
605}
606