blob: 5e8929c6c999a4b71a67fda3424d66c9795f9179 [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 /**
327 * Checks to make sure that the interface name matches the name written by the parcel
328 * sender by writeInterfaceToken
329 *
330 * @throws SecurityException interface doesn't match
331 */
Andreas Huberdab5fc62016-08-15 09:25:02 -0700332 public native final void enforceInterface(String interfaceName);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800333
334 /**
335 * Reads a boolean value from the current location in the parcel.
336 * @return value parsed from the parcel
337 * @throws IllegalArgumentException if the parcel has no more data
338 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700339 @FastNative
Andreas Huber86635bb2016-08-24 16:19:03 -0700340 public native final boolean readBool();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800341 /**
342 * Reads a byte 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 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700346 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700347 public native final byte readInt8();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800348 /**
349 * Reads a short value from the current location in the parcel.
350 * @return value parsed from the parcel
351 * @throws IllegalArgumentException if the parcel has no more data
352 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700353 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700354 public native final short readInt16();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800355 /**
356 * Reads a int value from the current location in the parcel.
357 * @return value parsed from the parcel
358 * @throws IllegalArgumentException if the parcel has no more data
359 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700360 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700361 public native final int readInt32();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800362 /**
363 * Reads a long value from the current location in the parcel.
364 * @return value parsed from the parcel
365 * @throws IllegalArgumentException if the parcel has no more data
366 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700367 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700368 public native final long readInt64();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800369 /**
370 * Reads a float value from the current location in the parcel.
371 * @return value parsed from the parcel
372 * @throws IllegalArgumentException if the parcel has no more data
373 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700374 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700375 public native final float readFloat();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800376 /**
377 * Reads a double value from the current location in the parcel.
378 * @return value parsed from the parcel
379 * @throws IllegalArgumentException if the parcel has no more data
380 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700381 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700382 public native final double readDouble();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800383 /**
384 * Reads a String value from the current location in the parcel.
385 * @return value parsed from the parcel
386 * @throws IllegalArgumentException if the parcel has no more data
387 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700388 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700389 public native final String readString();
Nirav Atre9850dd92018-07-24 17:03:44 -0700390 /**
391 * Reads a native handle (without duplicating the underlying file
392 * descriptors) from the parcel. These file descriptors will only
393 * be open for the duration that the binder window is open. If they
394 * are needed further, you must call {@link NativeHandle#dup()}.
395 *
396 * @return a {@link NativeHandle} instance parsed from the parcel
397 * @throws IllegalArgumentException if the parcel has no more data
398 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700399 @FastNative
Steven Moreland0ff061a2019-03-04 17:56:30 -0800400 public native final @Nullable NativeHandle readNativeHandle();
Nirav Atre9850dd92018-07-24 17:03:44 -0700401 /**
402 * Reads an embedded native handle (without duplicating the underlying
403 * file descriptors) from the parcel. These file descriptors will only
404 * be open for the duration that the binder window is open. If they
405 * are needed further, you must call {@link NativeHandle#dup()}. You
406 * do not need to call close on the NativeHandle returned from this.
407 *
408 * @param parentHandle handle from which to read the embedded object
409 * @param offset offset into parent
410 * @return a {@link NativeHandle} instance parsed from the parcel
411 * @throws IllegalArgumentException if the parcel has no more data
412 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700413 @FastNative
Steven Moreland0ff061a2019-03-04 17:56:30 -0800414 public native final @Nullable NativeHandle readEmbeddedNativeHandle(
Nirav Atre9850dd92018-07-24 17:03:44 -0700415 long parentHandle, long offset);
Andreas Huberdab5fc62016-08-15 09:25:02 -0700416
Steven Moreland4dde8a12018-01-10 15:45:36 -0800417 /**
418 * Reads an array of boolean values from the parcel.
419 * @return array of parsed values
420 * @throws IllegalArgumentException if the parcel has no more data
421 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700422 @FastNative
Andreas Huberef1a5652016-10-18 09:24:04 -0700423 private native final boolean[] readBoolVectorAsArray();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800424 /**
425 * Reads an array of byte values from the parcel.
426 * @return array of parsed values
427 * @throws IllegalArgumentException if the parcel has no more data
428 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700429 @FastNative
Andreas Huberef1a5652016-10-18 09:24:04 -0700430 private native final byte[] readInt8VectorAsArray();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800431 /**
432 * Reads an array of short values from the parcel.
433 * @return array of parsed values
434 * @throws IllegalArgumentException if the parcel has no more data
435 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700436 @FastNative
Andreas Huberef1a5652016-10-18 09:24:04 -0700437 private native final short[] readInt16VectorAsArray();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800438 /**
439 * Reads an array of int values from the parcel.
440 * @return array of parsed values
441 * @throws IllegalArgumentException if the parcel has no more data
442 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700443 @FastNative
Andreas Huberef1a5652016-10-18 09:24:04 -0700444 private native final int[] readInt32VectorAsArray();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800445 /**
446 * Reads an array of long values from the parcel.
447 * @return array of parsed values
448 * @throws IllegalArgumentException if the parcel has no more data
449 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700450 @FastNative
Andreas Huberef1a5652016-10-18 09:24:04 -0700451 private native final long[] readInt64VectorAsArray();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800452 /**
453 * Reads an array of float values from the parcel.
454 * @return array of parsed values
455 * @throws IllegalArgumentException if the parcel has no more data
456 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700457 @FastNative
Andreas Huberef1a5652016-10-18 09:24:04 -0700458 private native final float[] readFloatVectorAsArray();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800459 /**
460 * Reads an array of double values from the parcel.
461 * @return array of parsed values
462 * @throws IllegalArgumentException if the parcel has no more data
463 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700464 @FastNative
Andreas Huberef1a5652016-10-18 09:24:04 -0700465 private native final double[] readDoubleVectorAsArray();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800466 /**
467 * Reads an array of String values from the parcel.
468 * @return array of parsed values
469 * @throws IllegalArgumentException if the parcel has no more data
470 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700471 @FastNative
Andreas Huberef1a5652016-10-18 09:24:04 -0700472 private native final String[] readStringVectorAsArray();
Nirav Atre9850dd92018-07-24 17:03:44 -0700473 /**
474 * Reads an array of native handles from the parcel.
475 * @return array of {@link NativeHandle} objects
476 * @throws IllegalArgumentException if the parcel has no more data
477 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700478 @FastNative
Nirav Atre9850dd92018-07-24 17:03:44 -0700479 private native final NativeHandle[] readNativeHandleAsArray();
Andreas Huberef1a5652016-10-18 09:24:04 -0700480
Steven Moreland4dde8a12018-01-10 15:45:36 -0800481 /**
482 * Convenience method to read a Boolean vector as an ArrayList.
483 * @return array of parsed values.
484 * @throws IllegalArgumentException if the parcel has no more data
485 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700486 public final ArrayList<Boolean> readBoolVector() {
487 Boolean[] array = HwBlob.wrapArray(readBoolVectorAsArray());
488
489 return new ArrayList<Boolean>(Arrays.asList(array));
490 }
491
Steven Moreland4dde8a12018-01-10 15:45:36 -0800492 /**
493 * Convenience method to read a Byte vector as an ArrayList.
494 * @return array of parsed values.
495 * @throws IllegalArgumentException if the parcel has no more data
496 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700497 public final ArrayList<Byte> readInt8Vector() {
498 Byte[] array = HwBlob.wrapArray(readInt8VectorAsArray());
499
500 return new ArrayList<Byte>(Arrays.asList(array));
501 }
502
Steven Moreland4dde8a12018-01-10 15:45:36 -0800503 /**
504 * Convenience method to read a Short vector as an ArrayList.
505 * @return array of parsed values.
506 * @throws IllegalArgumentException if the parcel has no more data
507 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700508 public final ArrayList<Short> readInt16Vector() {
509 Short[] array = HwBlob.wrapArray(readInt16VectorAsArray());
510
511 return new ArrayList<Short>(Arrays.asList(array));
512 }
513
Steven Moreland4dde8a12018-01-10 15:45:36 -0800514 /**
515 * Convenience method to read a Integer vector as an ArrayList.
516 * @return array of parsed values.
517 * @throws IllegalArgumentException if the parcel has no more data
518 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700519 public final ArrayList<Integer> readInt32Vector() {
520 Integer[] array = HwBlob.wrapArray(readInt32VectorAsArray());
521
522 return new ArrayList<Integer>(Arrays.asList(array));
523 }
524
Steven Moreland4dde8a12018-01-10 15:45:36 -0800525 /**
526 * Convenience method to read a Long vector as an ArrayList.
527 * @return array of parsed values.
528 * @throws IllegalArgumentException if the parcel has no more data
529 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700530 public final ArrayList<Long> readInt64Vector() {
531 Long[] array = HwBlob.wrapArray(readInt64VectorAsArray());
532
533 return new ArrayList<Long>(Arrays.asList(array));
534 }
535
Steven Moreland4dde8a12018-01-10 15:45:36 -0800536 /**
537 * Convenience method to read a Float vector as an ArrayList.
538 * @return array of parsed values.
539 * @throws IllegalArgumentException if the parcel has no more data
540 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700541 public final ArrayList<Float> readFloatVector() {
542 Float[] array = HwBlob.wrapArray(readFloatVectorAsArray());
543
544 return new ArrayList<Float>(Arrays.asList(array));
545 }
546
Steven Moreland4dde8a12018-01-10 15:45:36 -0800547 /**
548 * Convenience method to read a Double vector as an ArrayList.
549 * @return array of parsed values.
550 * @throws IllegalArgumentException if the parcel has no more data
551 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700552 public final ArrayList<Double> readDoubleVector() {
553 Double[] array = HwBlob.wrapArray(readDoubleVectorAsArray());
554
555 return new ArrayList<Double>(Arrays.asList(array));
556 }
557
Steven Moreland4dde8a12018-01-10 15:45:36 -0800558 /**
559 * Convenience method to read a String vector as an ArrayList.
560 * @return array of parsed values.
561 * @throws IllegalArgumentException if the parcel has no more data
562 */
Andreas Huberef1a5652016-10-18 09:24:04 -0700563 public final ArrayList<String> readStringVector() {
564 return new ArrayList<String>(Arrays.asList(readStringVectorAsArray()));
565 }
Andreas Huberdab5fc62016-08-15 09:25:02 -0700566
Steven Moreland4dde8a12018-01-10 15:45:36 -0800567 /**
Nirav Atre9850dd92018-07-24 17:03:44 -0700568 * Convenience method to read a vector of native handles as an ArrayList.
569 * @return array of {@link NativeHandle} objects.
570 * @throws IllegalArgumentException if the parcel has no more data
571 */
Steven Moreland0ff061a2019-03-04 17:56:30 -0800572 public final @NonNull ArrayList<NativeHandle> readNativeHandleVector() {
Nirav Atre9850dd92018-07-24 17:03:44 -0700573 return new ArrayList<NativeHandle>(Arrays.asList(readNativeHandleAsArray()));
574 }
575
576 /**
Steven Moreland4dde8a12018-01-10 15:45:36 -0800577 * Reads a strong binder value from the parcel.
578 * @return binder object read from parcel or null if no binder can be read
579 * @throws IllegalArgumentException if the parcel has no more data
580 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700581 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700582 public native final IHwBinder readStrongBinder();
583
Steven Moreland4dde8a12018-01-10 15:45:36 -0800584 /**
585 * Read opaque segment of data as a blob.
586 * @return blob of size expectedSize
587 * @throws IllegalArgumentException if the parcel has no more data
588 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700589 @FastNative
Martijn Coenen932b5042017-04-18 15:54:43 -0700590 public native final HwBlob readBuffer(long expectedSize);
Andreas Huber9266f992016-08-25 11:21:21 -0700591
Steven Moreland4dde8a12018-01-10 15:45:36 -0800592 /**
593 * Read a buffer written using scatter gather.
594 *
595 * @param expectedSize size that buffer should be
596 * @param parentHandle handle from which to read the embedded buffer
597 * @param offset offset into parent
598 * @param nullable whether or not to allow for a null return
599 * @return blob of data with size expectedSize
600 * @throws NoSuchElementException if an embedded buffer is not available to read
601 * @throws IllegalArgumentException if expectedSize < 0
602 * @throws NullPointerException if the transaction specified the blob to be null
603 * but nullable is false
604 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700605 @FastNative
Andreas Huber9266f992016-08-25 11:21:21 -0700606 public native final HwBlob readEmbeddedBuffer(
Martijn Coenen932b5042017-04-18 15:54:43 -0700607 long expectedSize, long parentHandle, long offset,
608 boolean nullable);
Andreas Huber9266f992016-08-25 11:21:21 -0700609
Steven Moreland4dde8a12018-01-10 15:45:36 -0800610 /**
611 * Write a buffer into the transaction.
612 * @param blob blob to write into the parcel.
613 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700614 @FastNative
Andreas Huber9266f992016-08-25 11:21:21 -0700615 public native final void writeBuffer(HwBlob blob);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800616 /**
617 * Write a status value into the blob.
618 * @param status value to write
619 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700620 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700621 public native final void writeStatus(int status);
Steven Moreland4dde8a12018-01-10 15:45:36 -0800622 /**
623 * @throws IllegalArgumentException if a success vaue cannot be read
624 * @throws RemoteException if success value indicates a transaction error
625 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700626 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700627 public native final void verifySuccess();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800628 /**
629 * Should be called to reduce memory pressure when this object no longer needs
630 * to be written to.
631 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700632 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700633 public native final void releaseTemporaryStorage();
Steven Moreland4dde8a12018-01-10 15:45:36 -0800634 /**
635 * Should be called when object is no longer needed to reduce possible memory
636 * pressure if the Java GC does not get to this object in time.
637 */
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700638 @FastNative
Martijn Coenen3d726d12017-03-16 18:46:42 +0100639 public native final void release();
Andreas Huberdab5fc62016-08-15 09:25:02 -0700640
Steven Moreland4dde8a12018-01-10 15:45:36 -0800641 /**
642 * Sends the parcel to the specified destination.
643 */
Andreas Huberdab5fc62016-08-15 09:25:02 -0700644 public native final void send();
645
646 // Returns address of the "freeFunction".
647 private static native final long native_init();
648
Daniel Colascionec94b8c32019-10-08 11:47:41 -0700649 @FastNative
Andreas Huberdab5fc62016-08-15 09:25:02 -0700650 private native final void native_setup(boolean allocate);
651
652 static {
653 long freeFunction = native_init();
654
655 sNativeRegistry = new NativeAllocationRegistry(
656 HwParcel.class.getClassLoader(),
657 freeFunction,
658 128 /* size */);
659 }
660
661 private long mNativeContext;
662}
663