Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.os; |
| 18 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 19 | import android.annotation.IntDef; |
| 20 | import android.annotation.SystemApi; |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 21 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 22 | import libcore.util.NativeAllocationRegistry; |
| 23 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 24 | import java.lang.annotation.Retention; |
| 25 | import java.lang.annotation.RetentionPolicy; |
| 26 | import java.util.ArrayList; |
| 27 | import java.util.Arrays; |
| 28 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 29 | /** @hide */ |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 30 | @SystemApi |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 31 | public class HwParcel { |
| 32 | private static final String TAG = "HwParcel"; |
| 33 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 34 | @IntDef(prefix = { "STATUS_" }, value = { |
| 35 | STATUS_SUCCESS, |
| 36 | }) |
| 37 | @Retention(RetentionPolicy.SOURCE) |
| 38 | public @interface Status {} |
| 39 | |
| 40 | /** |
| 41 | * Success return error for a transaction. Written to parcels |
| 42 | * using writeStatus. |
| 43 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 44 | public static final int STATUS_SUCCESS = 0; |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 45 | |
| 46 | private static final NativeAllocationRegistry sNativeRegistry; |
| 47 | |
| 48 | private HwParcel(boolean allocate) { |
| 49 | native_setup(allocate); |
| 50 | |
| 51 | sNativeRegistry.registerNativeAllocation( |
| 52 | this, |
| 53 | mNativeContext); |
| 54 | } |
| 55 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 56 | /** |
| 57 | * Creates an initialized and empty parcel. |
| 58 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 59 | public HwParcel() { |
| 60 | native_setup(true /* allocate */); |
| 61 | |
| 62 | sNativeRegistry.registerNativeAllocation( |
| 63 | this, |
| 64 | mNativeContext); |
| 65 | } |
| 66 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 67 | /** |
| 68 | * Writes an interface token into the parcel used to verify that |
| 69 | * a transaction has made it to the write type of interface. |
| 70 | * |
| 71 | * @param interfaceName fully qualified name of interface message |
| 72 | * is being sent to. |
| 73 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 74 | public native final void writeInterfaceToken(String interfaceName); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 75 | /** |
| 76 | * Writes a boolean value to the end of the parcel. |
| 77 | * @param val to write |
| 78 | */ |
Andreas Huber | 86635bb | 2016-08-24 16:19:03 -0700 | [diff] [blame] | 79 | public native final void writeBool(boolean val); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 80 | /** |
| 81 | * Writes a byte value to the end of the parcel. |
| 82 | * @param val to write |
| 83 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 84 | public native final void writeInt8(byte val); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 85 | /** |
| 86 | * Writes a short value to the end of the parcel. |
| 87 | * @param val to write |
| 88 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 89 | public native final void writeInt16(short val); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 90 | /** |
| 91 | * Writes a int value to the end of the parcel. |
| 92 | * @param val to write |
| 93 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 94 | public native final void writeInt32(int val); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 95 | /** |
| 96 | * Writes a long value to the end of the parcel. |
| 97 | * @param val to write |
| 98 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 99 | public native final void writeInt64(long val); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 100 | /** |
| 101 | * Writes a float value to the end of the parcel. |
| 102 | * @param val to write |
| 103 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 104 | public native final void writeFloat(float val); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 105 | /** |
| 106 | * Writes a double value to the end of the parcel. |
| 107 | * @param val to write |
| 108 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 109 | public native final void writeDouble(double val); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 110 | /** |
| 111 | * Writes a String value to the end of the parcel. |
| 112 | * |
| 113 | * Note, this will be converted to UTF-8 when it is written. |
| 114 | * |
| 115 | * @param val to write |
| 116 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 117 | public native final void writeString(String val); |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame^] | 118 | /** |
| 119 | * Writes a native handle (without duplicating the underlying |
| 120 | * file descriptors) to the end of the parcel. |
| 121 | * |
| 122 | * @param val to write |
| 123 | */ |
| 124 | public native final void writeNativeHandle(NativeHandle val); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 125 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 126 | /** |
| 127 | * Writes an array of boolean values to the end of the parcel. |
| 128 | * @param val to write |
| 129 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 130 | private native final void writeBoolVector(boolean[] val); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 131 | /** |
| 132 | * Writes an array of byte values to the end of the parcel. |
| 133 | * @param val to write |
| 134 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 135 | private native final void writeInt8Vector(byte[] val); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 136 | /** |
| 137 | * Writes an array of short values to the end of the parcel. |
| 138 | * @param val to write |
| 139 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 140 | private native final void writeInt16Vector(short[] val); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 141 | /** |
| 142 | * Writes an array of int values to the end of the parcel. |
| 143 | * @param val to write |
| 144 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 145 | private native final void writeInt32Vector(int[] val); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 146 | /** |
| 147 | * Writes an array of long values to the end of the parcel. |
| 148 | * @param val to write |
| 149 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 150 | private native final void writeInt64Vector(long[] val); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 151 | /** |
| 152 | * Writes an array of float values to the end of the parcel. |
| 153 | * @param val to write |
| 154 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 155 | private native final void writeFloatVector(float[] val); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 156 | /** |
| 157 | * Writes an array of double values to the end of the parcel. |
| 158 | * @param val to write |
| 159 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 160 | private native final void writeDoubleVector(double[] val); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 161 | /** |
| 162 | * Writes an array of String values to the end of the parcel. |
| 163 | * |
| 164 | * Note, these will be converted to UTF-8 as they are written. |
| 165 | * |
| 166 | * @param val to write |
| 167 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 168 | private native final void writeStringVector(String[] val); |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame^] | 169 | /** |
| 170 | * Writes an array of native handles to the end of the parcel. |
| 171 | * @param val array of {@link NativeHandle} objects to write |
| 172 | */ |
| 173 | private native final void writeNativeHandleVector(NativeHandle[] val); |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 174 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 175 | /** |
| 176 | * Helper method to write a list of Booleans to val. |
| 177 | * @param val list to write |
| 178 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 179 | public final void writeBoolVector(ArrayList<Boolean> val) { |
| 180 | final int n = val.size(); |
| 181 | boolean[] array = new boolean[n]; |
| 182 | for (int i = 0; i < n; ++i) { |
| 183 | array[i] = val.get(i); |
| 184 | } |
| 185 | |
| 186 | writeBoolVector(array); |
| 187 | } |
| 188 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 189 | /** |
| 190 | * Helper method to write a list of Booleans to the end of the parcel. |
| 191 | * @param val list to write |
| 192 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 193 | public final void writeInt8Vector(ArrayList<Byte> val) { |
| 194 | final int n = val.size(); |
| 195 | byte[] array = new byte[n]; |
| 196 | for (int i = 0; i < n; ++i) { |
| 197 | array[i] = val.get(i); |
| 198 | } |
| 199 | |
| 200 | writeInt8Vector(array); |
| 201 | } |
| 202 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 203 | /** |
| 204 | * Helper method to write a list of Shorts to the end of the parcel. |
| 205 | * @param val list to write |
| 206 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 207 | public final void writeInt16Vector(ArrayList<Short> val) { |
| 208 | final int n = val.size(); |
| 209 | short[] array = new short[n]; |
| 210 | for (int i = 0; i < n; ++i) { |
| 211 | array[i] = val.get(i); |
| 212 | } |
| 213 | |
| 214 | writeInt16Vector(array); |
| 215 | } |
| 216 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 217 | /** |
| 218 | * Helper method to write a list of Integers to the end of the parcel. |
| 219 | * @param val list to write |
| 220 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 221 | public final void writeInt32Vector(ArrayList<Integer> val) { |
| 222 | final int n = val.size(); |
| 223 | int[] array = new int[n]; |
| 224 | for (int i = 0; i < n; ++i) { |
| 225 | array[i] = val.get(i); |
| 226 | } |
| 227 | |
| 228 | writeInt32Vector(array); |
| 229 | } |
| 230 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 231 | /** |
| 232 | * Helper method to write a list of Longs to the end of the parcel. |
| 233 | * @param val list to write |
| 234 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 235 | public final void writeInt64Vector(ArrayList<Long> val) { |
| 236 | final int n = val.size(); |
| 237 | long[] array = new long[n]; |
| 238 | for (int i = 0; i < n; ++i) { |
| 239 | array[i] = val.get(i); |
| 240 | } |
| 241 | |
| 242 | writeInt64Vector(array); |
| 243 | } |
| 244 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 245 | /** |
| 246 | * Helper method to write a list of Floats to the end of the parcel. |
| 247 | * @param val list to write |
| 248 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 249 | public final void writeFloatVector(ArrayList<Float> val) { |
| 250 | final int n = val.size(); |
| 251 | float[] array = new float[n]; |
| 252 | for (int i = 0; i < n; ++i) { |
| 253 | array[i] = val.get(i); |
| 254 | } |
| 255 | |
| 256 | writeFloatVector(array); |
| 257 | } |
| 258 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 259 | /** |
| 260 | * Helper method to write a list of Doubles to the end of the parcel. |
| 261 | * @param val list to write |
| 262 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 263 | public final void writeDoubleVector(ArrayList<Double> val) { |
| 264 | final int n = val.size(); |
| 265 | double[] array = new double[n]; |
| 266 | for (int i = 0; i < n; ++i) { |
| 267 | array[i] = val.get(i); |
| 268 | } |
| 269 | |
| 270 | writeDoubleVector(array); |
| 271 | } |
| 272 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 273 | /** |
| 274 | * Helper method to write a list of Strings to the end of the parcel. |
| 275 | * @param val list to write |
| 276 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 277 | public final void writeStringVector(ArrayList<String> val) { |
| 278 | writeStringVector(val.toArray(new String[val.size()])); |
| 279 | } |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 280 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 281 | /** |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame^] | 282 | * Helper method to write a list of native handles to the end of the parcel. |
| 283 | * @param val list of {@link NativeHandle} objects to write |
| 284 | */ |
| 285 | public final void writeNativeHandleVector(ArrayList<NativeHandle> val) { |
| 286 | writeNativeHandleVector(val.toArray(new NativeHandle[val.size()])); |
| 287 | } |
| 288 | |
| 289 | /** |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 290 | * Write a hwbinder object to the end of the parcel. |
| 291 | * @param binder value to write |
| 292 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 293 | public native final void writeStrongBinder(IHwBinder binder); |
| 294 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 295 | /** |
| 296 | * Checks to make sure that the interface name matches the name written by the parcel |
| 297 | * sender by writeInterfaceToken |
| 298 | * |
| 299 | * @throws SecurityException interface doesn't match |
| 300 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 301 | public native final void enforceInterface(String interfaceName); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 302 | |
| 303 | /** |
| 304 | * Reads a boolean value from the current location in the parcel. |
| 305 | * @return value parsed from the parcel |
| 306 | * @throws IllegalArgumentException if the parcel has no more data |
| 307 | */ |
Andreas Huber | 86635bb | 2016-08-24 16:19:03 -0700 | [diff] [blame] | 308 | public native final boolean readBool(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 309 | /** |
| 310 | * Reads a byte value from the current location in the parcel. |
| 311 | * @return value parsed from the parcel |
| 312 | * @throws IllegalArgumentException if the parcel has no more data |
| 313 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 314 | public native final byte readInt8(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 315 | /** |
| 316 | * Reads a short value from the current location in the parcel. |
| 317 | * @return value parsed from the parcel |
| 318 | * @throws IllegalArgumentException if the parcel has no more data |
| 319 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 320 | public native final short readInt16(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 321 | /** |
| 322 | * Reads a int value from the current location in the parcel. |
| 323 | * @return value parsed from the parcel |
| 324 | * @throws IllegalArgumentException if the parcel has no more data |
| 325 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 326 | public native final int readInt32(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 327 | /** |
| 328 | * Reads a long value from the current location in the parcel. |
| 329 | * @return value parsed from the parcel |
| 330 | * @throws IllegalArgumentException if the parcel has no more data |
| 331 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 332 | public native final long readInt64(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 333 | /** |
| 334 | * Reads a float value from the current location in the parcel. |
| 335 | * @return value parsed from the parcel |
| 336 | * @throws IllegalArgumentException if the parcel has no more data |
| 337 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 338 | public native final float readFloat(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 339 | /** |
| 340 | * Reads a double value from the current location in the parcel. |
| 341 | * @return value parsed from the parcel |
| 342 | * @throws IllegalArgumentException if the parcel has no more data |
| 343 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 344 | public native final double readDouble(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 345 | /** |
| 346 | * Reads a String value from the current location in the parcel. |
| 347 | * @return value parsed from the parcel |
| 348 | * @throws IllegalArgumentException if the parcel has no more data |
| 349 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 350 | public native final String readString(); |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame^] | 351 | /** |
| 352 | * Reads a native handle (without duplicating the underlying file |
| 353 | * descriptors) from the parcel. These file descriptors will only |
| 354 | * be open for the duration that the binder window is open. If they |
| 355 | * are needed further, you must call {@link NativeHandle#dup()}. |
| 356 | * |
| 357 | * @return a {@link NativeHandle} instance parsed from the parcel |
| 358 | * @throws IllegalArgumentException if the parcel has no more data |
| 359 | */ |
| 360 | public native final NativeHandle readNativeHandle(); |
| 361 | /** |
| 362 | * Reads an embedded native handle (without duplicating the underlying |
| 363 | * file descriptors) from the parcel. These file descriptors will only |
| 364 | * be open for the duration that the binder window is open. If they |
| 365 | * are needed further, you must call {@link NativeHandle#dup()}. You |
| 366 | * do not need to call close on the NativeHandle returned from this. |
| 367 | * |
| 368 | * @param parentHandle handle from which to read the embedded object |
| 369 | * @param offset offset into parent |
| 370 | * @return a {@link NativeHandle} instance parsed from the parcel |
| 371 | * @throws IllegalArgumentException if the parcel has no more data |
| 372 | */ |
| 373 | public native final NativeHandle readEmbeddedNativeHandle( |
| 374 | long parentHandle, long offset); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 375 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 376 | /** |
| 377 | * Reads an array of boolean values from the parcel. |
| 378 | * @return array of parsed values |
| 379 | * @throws IllegalArgumentException if the parcel has no more data |
| 380 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 381 | private native final boolean[] readBoolVectorAsArray(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 382 | /** |
| 383 | * Reads an array of byte values from the parcel. |
| 384 | * @return array of parsed values |
| 385 | * @throws IllegalArgumentException if the parcel has no more data |
| 386 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 387 | private native final byte[] readInt8VectorAsArray(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 388 | /** |
| 389 | * Reads an array of short values from the parcel. |
| 390 | * @return array of parsed values |
| 391 | * @throws IllegalArgumentException if the parcel has no more data |
| 392 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 393 | private native final short[] readInt16VectorAsArray(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 394 | /** |
| 395 | * Reads an array of int values from the parcel. |
| 396 | * @return array of parsed values |
| 397 | * @throws IllegalArgumentException if the parcel has no more data |
| 398 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 399 | private native final int[] readInt32VectorAsArray(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 400 | /** |
| 401 | * Reads an array of long values from the parcel. |
| 402 | * @return array of parsed values |
| 403 | * @throws IllegalArgumentException if the parcel has no more data |
| 404 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 405 | private native final long[] readInt64VectorAsArray(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 406 | /** |
| 407 | * Reads an array of float values from the parcel. |
| 408 | * @return array of parsed values |
| 409 | * @throws IllegalArgumentException if the parcel has no more data |
| 410 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 411 | private native final float[] readFloatVectorAsArray(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 412 | /** |
| 413 | * Reads an array of double values from the parcel. |
| 414 | * @return array of parsed values |
| 415 | * @throws IllegalArgumentException if the parcel has no more data |
| 416 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 417 | private native final double[] readDoubleVectorAsArray(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 418 | /** |
| 419 | * Reads an array of String values from the parcel. |
| 420 | * @return array of parsed values |
| 421 | * @throws IllegalArgumentException if the parcel has no more data |
| 422 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 423 | private native final String[] readStringVectorAsArray(); |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame^] | 424 | /** |
| 425 | * Reads an array of native handles from the parcel. |
| 426 | * @return array of {@link NativeHandle} objects |
| 427 | * @throws IllegalArgumentException if the parcel has no more data |
| 428 | */ |
| 429 | private native final NativeHandle[] readNativeHandleAsArray(); |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 430 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 431 | /** |
| 432 | * Convenience method to read a Boolean vector as an ArrayList. |
| 433 | * @return array of parsed values. |
| 434 | * @throws IllegalArgumentException if the parcel has no more data |
| 435 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 436 | public final ArrayList<Boolean> readBoolVector() { |
| 437 | Boolean[] array = HwBlob.wrapArray(readBoolVectorAsArray()); |
| 438 | |
| 439 | return new ArrayList<Boolean>(Arrays.asList(array)); |
| 440 | } |
| 441 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 442 | /** |
| 443 | * Convenience method to read a Byte vector as an ArrayList. |
| 444 | * @return array of parsed values. |
| 445 | * @throws IllegalArgumentException if the parcel has no more data |
| 446 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 447 | public final ArrayList<Byte> readInt8Vector() { |
| 448 | Byte[] array = HwBlob.wrapArray(readInt8VectorAsArray()); |
| 449 | |
| 450 | return new ArrayList<Byte>(Arrays.asList(array)); |
| 451 | } |
| 452 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 453 | /** |
| 454 | * Convenience method to read a Short vector as an ArrayList. |
| 455 | * @return array of parsed values. |
| 456 | * @throws IllegalArgumentException if the parcel has no more data |
| 457 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 458 | public final ArrayList<Short> readInt16Vector() { |
| 459 | Short[] array = HwBlob.wrapArray(readInt16VectorAsArray()); |
| 460 | |
| 461 | return new ArrayList<Short>(Arrays.asList(array)); |
| 462 | } |
| 463 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 464 | /** |
| 465 | * Convenience method to read a Integer vector as an ArrayList. |
| 466 | * @return array of parsed values. |
| 467 | * @throws IllegalArgumentException if the parcel has no more data |
| 468 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 469 | public final ArrayList<Integer> readInt32Vector() { |
| 470 | Integer[] array = HwBlob.wrapArray(readInt32VectorAsArray()); |
| 471 | |
| 472 | return new ArrayList<Integer>(Arrays.asList(array)); |
| 473 | } |
| 474 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 475 | /** |
| 476 | * Convenience method to read a Long vector as an ArrayList. |
| 477 | * @return array of parsed values. |
| 478 | * @throws IllegalArgumentException if the parcel has no more data |
| 479 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 480 | public final ArrayList<Long> readInt64Vector() { |
| 481 | Long[] array = HwBlob.wrapArray(readInt64VectorAsArray()); |
| 482 | |
| 483 | return new ArrayList<Long>(Arrays.asList(array)); |
| 484 | } |
| 485 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 486 | /** |
| 487 | * Convenience method to read a Float vector as an ArrayList. |
| 488 | * @return array of parsed values. |
| 489 | * @throws IllegalArgumentException if the parcel has no more data |
| 490 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 491 | public final ArrayList<Float> readFloatVector() { |
| 492 | Float[] array = HwBlob.wrapArray(readFloatVectorAsArray()); |
| 493 | |
| 494 | return new ArrayList<Float>(Arrays.asList(array)); |
| 495 | } |
| 496 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 497 | /** |
| 498 | * Convenience method to read a Double vector as an ArrayList. |
| 499 | * @return array of parsed values. |
| 500 | * @throws IllegalArgumentException if the parcel has no more data |
| 501 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 502 | public final ArrayList<Double> readDoubleVector() { |
| 503 | Double[] array = HwBlob.wrapArray(readDoubleVectorAsArray()); |
| 504 | |
| 505 | return new ArrayList<Double>(Arrays.asList(array)); |
| 506 | } |
| 507 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 508 | /** |
| 509 | * Convenience method to read a String vector as an ArrayList. |
| 510 | * @return array of parsed values. |
| 511 | * @throws IllegalArgumentException if the parcel has no more data |
| 512 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 513 | public final ArrayList<String> readStringVector() { |
| 514 | return new ArrayList<String>(Arrays.asList(readStringVectorAsArray())); |
| 515 | } |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 516 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 517 | /** |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame^] | 518 | * Convenience method to read a vector of native handles as an ArrayList. |
| 519 | * @return array of {@link NativeHandle} objects. |
| 520 | * @throws IllegalArgumentException if the parcel has no more data |
| 521 | */ |
| 522 | public final ArrayList<NativeHandle> readNativeHandleVector() { |
| 523 | return new ArrayList<NativeHandle>(Arrays.asList(readNativeHandleAsArray())); |
| 524 | } |
| 525 | |
| 526 | /** |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 527 | * Reads a strong binder value from the parcel. |
| 528 | * @return binder object read from parcel or null if no binder can be read |
| 529 | * @throws IllegalArgumentException if the parcel has no more data |
| 530 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 531 | public native final IHwBinder readStrongBinder(); |
| 532 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 533 | /** |
| 534 | * Read opaque segment of data as a blob. |
| 535 | * @return blob of size expectedSize |
| 536 | * @throws IllegalArgumentException if the parcel has no more data |
| 537 | */ |
Martijn Coenen | 932b504 | 2017-04-18 15:54:43 -0700 | [diff] [blame] | 538 | public native final HwBlob readBuffer(long expectedSize); |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 539 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 540 | /** |
| 541 | * Read a buffer written using scatter gather. |
| 542 | * |
| 543 | * @param expectedSize size that buffer should be |
| 544 | * @param parentHandle handle from which to read the embedded buffer |
| 545 | * @param offset offset into parent |
| 546 | * @param nullable whether or not to allow for a null return |
| 547 | * @return blob of data with size expectedSize |
| 548 | * @throws NoSuchElementException if an embedded buffer is not available to read |
| 549 | * @throws IllegalArgumentException if expectedSize < 0 |
| 550 | * @throws NullPointerException if the transaction specified the blob to be null |
| 551 | * but nullable is false |
| 552 | */ |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 553 | public native final HwBlob readEmbeddedBuffer( |
Martijn Coenen | 932b504 | 2017-04-18 15:54:43 -0700 | [diff] [blame] | 554 | long expectedSize, long parentHandle, long offset, |
| 555 | boolean nullable); |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 556 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 557 | /** |
| 558 | * Write a buffer into the transaction. |
| 559 | * @param blob blob to write into the parcel. |
| 560 | */ |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 561 | public native final void writeBuffer(HwBlob blob); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 562 | /** |
| 563 | * Write a status value into the blob. |
| 564 | * @param status value to write |
| 565 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 566 | public native final void writeStatus(int status); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 567 | /** |
| 568 | * @throws IllegalArgumentException if a success vaue cannot be read |
| 569 | * @throws RemoteException if success value indicates a transaction error |
| 570 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 571 | public native final void verifySuccess(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 572 | /** |
| 573 | * Should be called to reduce memory pressure when this object no longer needs |
| 574 | * to be written to. |
| 575 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 576 | public native final void releaseTemporaryStorage(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 577 | /** |
| 578 | * Should be called when object is no longer needed to reduce possible memory |
| 579 | * pressure if the Java GC does not get to this object in time. |
| 580 | */ |
Martijn Coenen | 3d726d1 | 2017-03-16 18:46:42 +0100 | [diff] [blame] | 581 | public native final void release(); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 582 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 583 | /** |
| 584 | * Sends the parcel to the specified destination. |
| 585 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 586 | public native final void send(); |
| 587 | |
| 588 | // Returns address of the "freeFunction". |
| 589 | private static native final long native_init(); |
| 590 | |
| 591 | private native final void native_setup(boolean allocate); |
| 592 | |
| 593 | static { |
| 594 | long freeFunction = native_init(); |
| 595 | |
| 596 | sNativeRegistry = new NativeAllocationRegistry( |
| 597 | HwParcel.class.getClassLoader(), |
| 598 | freeFunction, |
| 599 | 128 /* size */); |
| 600 | } |
| 601 | |
| 602 | private long mNativeContext; |
| 603 | } |
| 604 | |