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; |
Steven Moreland | 14b9eb6 | 2019-01-11 10:19:51 -0800 | [diff] [blame] | 21 | import android.annotation.TestApi; |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 22 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 23 | import libcore.util.NativeAllocationRegistry; |
| 24 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 25 | import java.lang.annotation.Retention; |
| 26 | import java.lang.annotation.RetentionPolicy; |
| 27 | import java.util.ArrayList; |
| 28 | import java.util.Arrays; |
| 29 | |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 30 | /** @hide */ |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 31 | @SystemApi |
Steven Moreland | 14b9eb6 | 2019-01-11 10:19:51 -0800 | [diff] [blame] | 32 | @TestApi |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 33 | public class HwParcel { |
| 34 | private static final String TAG = "HwParcel"; |
| 35 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 36 | @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 Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 46 | public static final int STATUS_SUCCESS = 0; |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 47 | |
| 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 Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 58 | /** |
| 59 | * Creates an initialized and empty parcel. |
| 60 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 61 | public HwParcel() { |
| 62 | native_setup(true /* allocate */); |
| 63 | |
| 64 | sNativeRegistry.registerNativeAllocation( |
| 65 | this, |
| 66 | mNativeContext); |
| 67 | } |
| 68 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 69 | /** |
| 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 Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 76 | public native final void writeInterfaceToken(String interfaceName); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 77 | /** |
| 78 | * Writes a boolean value to the end of the parcel. |
| 79 | * @param val to write |
| 80 | */ |
Andreas Huber | 86635bb | 2016-08-24 16:19:03 -0700 | [diff] [blame] | 81 | public native final void writeBool(boolean val); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 82 | /** |
| 83 | * Writes a byte value to the end of the parcel. |
| 84 | * @param val to write |
| 85 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 86 | public native final void writeInt8(byte val); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 87 | /** |
| 88 | * Writes a short value to the end of the parcel. |
| 89 | * @param val to write |
| 90 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 91 | public native final void writeInt16(short val); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 92 | /** |
| 93 | * Writes a int value to the end of the parcel. |
| 94 | * @param val to write |
| 95 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 96 | public native final void writeInt32(int val); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 97 | /** |
| 98 | * Writes a long value to the end of the parcel. |
| 99 | * @param val to write |
| 100 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 101 | public native final void writeInt64(long val); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 102 | /** |
| 103 | * Writes a float value to the end of the parcel. |
| 104 | * @param val to write |
| 105 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 106 | public native final void writeFloat(float val); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 107 | /** |
| 108 | * Writes a double value to the end of the parcel. |
| 109 | * @param val to write |
| 110 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 111 | public native final void writeDouble(double val); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 112 | /** |
| 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 Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 119 | public native final void writeString(String val); |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame] | 120 | /** |
| 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 Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 127 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 128 | /** |
| 129 | * Writes an array of boolean values to the end of the parcel. |
| 130 | * @param val to write |
| 131 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 132 | private native final void writeBoolVector(boolean[] val); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 133 | /** |
| 134 | * Writes an array of byte values to the end of the parcel. |
| 135 | * @param val to write |
| 136 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 137 | private native final void writeInt8Vector(byte[] val); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 138 | /** |
| 139 | * Writes an array of short values to the end of the parcel. |
| 140 | * @param val to write |
| 141 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 142 | private native final void writeInt16Vector(short[] val); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 143 | /** |
| 144 | * Writes an array of int values to the end of the parcel. |
| 145 | * @param val to write |
| 146 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 147 | private native final void writeInt32Vector(int[] val); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 148 | /** |
| 149 | * Writes an array of long values to the end of the parcel. |
| 150 | * @param val to write |
| 151 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 152 | private native final void writeInt64Vector(long[] val); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 153 | /** |
| 154 | * Writes an array of float values to the end of the parcel. |
| 155 | * @param val to write |
| 156 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 157 | private native final void writeFloatVector(float[] val); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 158 | /** |
| 159 | * Writes an array of double values to the end of the parcel. |
| 160 | * @param val to write |
| 161 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 162 | private native final void writeDoubleVector(double[] val); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 163 | /** |
| 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 Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 170 | private native final void writeStringVector(String[] val); |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame] | 171 | /** |
| 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 Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 176 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 177 | /** |
| 178 | * Helper method to write a list of Booleans to val. |
| 179 | * @param val list to write |
| 180 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 181 | 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 Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 191 | /** |
| 192 | * Helper method to write a list of Booleans to the end of the parcel. |
| 193 | * @param val list to write |
| 194 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 195 | 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 Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 205 | /** |
| 206 | * Helper method to write a list of Shorts to the end of the parcel. |
| 207 | * @param val list to write |
| 208 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 209 | 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 Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 219 | /** |
| 220 | * Helper method to write a list of Integers to the end of the parcel. |
| 221 | * @param val list to write |
| 222 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 223 | 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 Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 233 | /** |
| 234 | * Helper method to write a list of Longs to the end of the parcel. |
| 235 | * @param val list to write |
| 236 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 237 | 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 Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 247 | /** |
| 248 | * Helper method to write a list of Floats to the end of the parcel. |
| 249 | * @param val list to write |
| 250 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 251 | 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 Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 261 | /** |
| 262 | * Helper method to write a list of Doubles to the end of the parcel. |
| 263 | * @param val list to write |
| 264 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 265 | 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 Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 275 | /** |
| 276 | * Helper method to write a list of Strings to the end of the parcel. |
| 277 | * @param val list to write |
| 278 | */ |
Andreas Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 279 | public final void writeStringVector(ArrayList<String> val) { |
| 280 | writeStringVector(val.toArray(new String[val.size()])); |
| 281 | } |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 282 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 283 | /** |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame] | 284 | * 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 Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 292 | * Write a hwbinder object to the end of the parcel. |
| 293 | * @param binder value to write |
| 294 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 295 | public native final void writeStrongBinder(IHwBinder binder); |
| 296 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 297 | /** |
| 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 Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 303 | public native final void enforceInterface(String interfaceName); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 304 | |
| 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 Huber | 86635bb | 2016-08-24 16:19:03 -0700 | [diff] [blame] | 310 | public native final boolean readBool(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 311 | /** |
| 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 Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 316 | public native final byte readInt8(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 317 | /** |
| 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 Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 322 | public native final short readInt16(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 323 | /** |
| 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 Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 328 | public native final int readInt32(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 329 | /** |
| 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 Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 334 | public native final long readInt64(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 335 | /** |
| 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 Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 340 | public native final float readFloat(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 341 | /** |
| 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 Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 346 | public native final double readDouble(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 347 | /** |
| 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 Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 352 | public native final String readString(); |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame] | 353 | /** |
| 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 Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 377 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 378 | /** |
| 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 Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 383 | private native final boolean[] readBoolVectorAsArray(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 384 | /** |
| 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 Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 389 | private native final byte[] readInt8VectorAsArray(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 390 | /** |
| 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 Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 395 | private native final short[] readInt16VectorAsArray(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 396 | /** |
| 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 Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 401 | private native final int[] readInt32VectorAsArray(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 402 | /** |
| 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 Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 407 | private native final long[] readInt64VectorAsArray(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 408 | /** |
| 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 Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 413 | private native final float[] readFloatVectorAsArray(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 414 | /** |
| 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 Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 419 | private native final double[] readDoubleVectorAsArray(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 420 | /** |
| 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 Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 425 | private native final String[] readStringVectorAsArray(); |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame] | 426 | /** |
| 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 Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 432 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 433 | /** |
| 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 Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 438 | public final ArrayList<Boolean> readBoolVector() { |
| 439 | Boolean[] array = HwBlob.wrapArray(readBoolVectorAsArray()); |
| 440 | |
| 441 | return new ArrayList<Boolean>(Arrays.asList(array)); |
| 442 | } |
| 443 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 444 | /** |
| 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 Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 449 | public final ArrayList<Byte> readInt8Vector() { |
| 450 | Byte[] array = HwBlob.wrapArray(readInt8VectorAsArray()); |
| 451 | |
| 452 | return new ArrayList<Byte>(Arrays.asList(array)); |
| 453 | } |
| 454 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 455 | /** |
| 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 Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 460 | public final ArrayList<Short> readInt16Vector() { |
| 461 | Short[] array = HwBlob.wrapArray(readInt16VectorAsArray()); |
| 462 | |
| 463 | return new ArrayList<Short>(Arrays.asList(array)); |
| 464 | } |
| 465 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 466 | /** |
| 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 Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 471 | public final ArrayList<Integer> readInt32Vector() { |
| 472 | Integer[] array = HwBlob.wrapArray(readInt32VectorAsArray()); |
| 473 | |
| 474 | return new ArrayList<Integer>(Arrays.asList(array)); |
| 475 | } |
| 476 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 477 | /** |
| 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 Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 482 | public final ArrayList<Long> readInt64Vector() { |
| 483 | Long[] array = HwBlob.wrapArray(readInt64VectorAsArray()); |
| 484 | |
| 485 | return new ArrayList<Long>(Arrays.asList(array)); |
| 486 | } |
| 487 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 488 | /** |
| 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 Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 493 | public final ArrayList<Float> readFloatVector() { |
| 494 | Float[] array = HwBlob.wrapArray(readFloatVectorAsArray()); |
| 495 | |
| 496 | return new ArrayList<Float>(Arrays.asList(array)); |
| 497 | } |
| 498 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 499 | /** |
| 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 Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 504 | public final ArrayList<Double> readDoubleVector() { |
| 505 | Double[] array = HwBlob.wrapArray(readDoubleVectorAsArray()); |
| 506 | |
| 507 | return new ArrayList<Double>(Arrays.asList(array)); |
| 508 | } |
| 509 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 510 | /** |
| 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 Huber | ef1a565 | 2016-10-18 09:24:04 -0700 | [diff] [blame] | 515 | public final ArrayList<String> readStringVector() { |
| 516 | return new ArrayList<String>(Arrays.asList(readStringVectorAsArray())); |
| 517 | } |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 518 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 519 | /** |
Nirav Atre | 9850dd9 | 2018-07-24 17:03:44 -0700 | [diff] [blame] | 520 | * 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 Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 529 | * 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 Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 533 | public native final IHwBinder readStrongBinder(); |
| 534 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 535 | /** |
| 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 Coenen | 932b504 | 2017-04-18 15:54:43 -0700 | [diff] [blame] | 540 | public native final HwBlob readBuffer(long expectedSize); |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 541 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 542 | /** |
| 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 Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 555 | public native final HwBlob readEmbeddedBuffer( |
Martijn Coenen | 932b504 | 2017-04-18 15:54:43 -0700 | [diff] [blame] | 556 | long expectedSize, long parentHandle, long offset, |
| 557 | boolean nullable); |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 558 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 559 | /** |
| 560 | * Write a buffer into the transaction. |
| 561 | * @param blob blob to write into the parcel. |
| 562 | */ |
Andreas Huber | 9266f99 | 2016-08-25 11:21:21 -0700 | [diff] [blame] | 563 | public native final void writeBuffer(HwBlob blob); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 564 | /** |
| 565 | * Write a status value into the blob. |
| 566 | * @param status value to write |
| 567 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 568 | public native final void writeStatus(int status); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 569 | /** |
| 570 | * @throws IllegalArgumentException if a success vaue cannot be read |
| 571 | * @throws RemoteException if success value indicates a transaction error |
| 572 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 573 | public native final void verifySuccess(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 574 | /** |
| 575 | * Should be called to reduce memory pressure when this object no longer needs |
| 576 | * to be written to. |
| 577 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 578 | public native final void releaseTemporaryStorage(); |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 579 | /** |
| 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 Coenen | 3d726d1 | 2017-03-16 18:46:42 +0100 | [diff] [blame] | 583 | public native final void release(); |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 584 | |
Steven Moreland | 4dde8a1 | 2018-01-10 15:45:36 -0800 | [diff] [blame] | 585 | /** |
| 586 | * Sends the parcel to the specified destination. |
| 587 | */ |
Andreas Huber | dab5fc6 | 2016-08-15 09:25:02 -0700 | [diff] [blame] | 588 | 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 | |