blob: 783ab44bc4e2f256309a1a29bc429ecf3710d012 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 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
Jake Whartonb1f474c2018-05-30 15:11:13 -040019import android.annotation.NonNull;
Svet Ganovddb94882016-06-23 19:55:24 -070020import android.annotation.Nullable;
Howard Chen40abbb02019-05-24 02:04:50 +080021import android.annotation.TestApi;
Andrei Onea24ec3212019-03-15 17:35:05 +000022import android.annotation.UnsupportedAppUsage;
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -070023import android.app.AppOpsManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.text.TextUtils;
Dianne Hackbornb87655b2013-07-17 19:06:22 -070025import android.util.ArrayMap;
Svet Ganovddb94882016-06-23 19:55:24 -070026import android.util.ArraySet;
Fyodor Kupolov3b946f42017-11-27 10:40:46 -080027import android.util.ExceptionUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.util.Log;
Jeff Sharkey5ef33982014-09-04 18:13:39 -070029import android.util.Size;
30import android.util.SizeF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.util.SparseArray;
32import android.util.SparseBooleanArray;
Adam Lesinski4e862812016-11-21 16:02:24 -080033import android.util.SparseIntArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
Makoto Onukib148b6c2017-06-27 13:38:38 -070035import dalvik.annotation.optimization.CriticalNative;
Jeff Sharkey789a8fc2017-04-16 13:18:35 -060036import dalvik.annotation.optimization.FastNative;
37import dalvik.system.VMRuntime;
38
Pete Gillin60f55a252018-05-10 15:40:32 +010039import libcore.util.ArrayUtils;
Jeff Sharkeye628b7d2017-01-17 13:50:20 -070040import libcore.util.SneakyThrow;
41
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import java.io.ByteArrayInputStream;
43import java.io.ByteArrayOutputStream;
44import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import java.io.IOException;
46import java.io.ObjectInputStream;
47import java.io.ObjectOutputStream;
John Spurlock5002b8c2014-01-10 13:32:12 -050048import java.io.ObjectStreamClass;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import java.io.Serializable;
Makoto Onuki440a1ea2016-07-20 14:21:18 -070050import java.lang.reflect.Array;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import java.lang.reflect.Field;
Neil Fuller44e440c2015-04-20 14:39:00 +010052import java.lang.reflect.Modifier;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import java.util.ArrayList;
54import java.util.HashMap;
55import java.util.List;
56import java.util.Map;
57import java.util.Set;
Adrian Roos04505652015-10-22 16:12:01 -070058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059/**
60 * Container for a message (data and object references) that can
61 * be sent through an IBinder. A Parcel can contain both flattened data
62 * that will be unflattened on the other side of the IPC (using the various
63 * methods here for writing specific types, or the general
64 * {@link Parcelable} interface), and references to live {@link IBinder}
65 * objects that will result in the other side receiving a proxy IBinder
66 * connected with the original IBinder in the Parcel.
67 *
68 * <p class="note">Parcel is <strong>not</strong> a general-purpose
69 * serialization mechanism. This class (and the corresponding
70 * {@link Parcelable} API for placing arbitrary objects into a Parcel) is
71 * designed as a high-performance IPC transport. As such, it is not
72 * appropriate to place any Parcel data in to persistent storage: changes
73 * in the underlying implementation of any of the data in the Parcel can
74 * render older data unreadable.</p>
Samuel Tana8036662015-11-23 14:36:00 -080075 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 * <p>The bulk of the Parcel API revolves around reading and writing data
77 * of various types. There are six major classes of such functions available.</p>
Samuel Tana8036662015-11-23 14:36:00 -080078 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 * <h3>Primitives</h3>
Samuel Tana8036662015-11-23 14:36:00 -080080 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 * <p>The most basic data functions are for writing and reading primitive
82 * data types: {@link #writeByte}, {@link #readByte}, {@link #writeDouble},
83 * {@link #readDouble}, {@link #writeFloat}, {@link #readFloat}, {@link #writeInt},
84 * {@link #readInt}, {@link #writeLong}, {@link #readLong},
85 * {@link #writeString}, {@link #readString}. Most other
86 * data operations are built on top of these. The given data is written and
87 * read using the endianess of the host CPU.</p>
Samuel Tana8036662015-11-23 14:36:00 -080088 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 * <h3>Primitive Arrays</h3>
Samuel Tana8036662015-11-23 14:36:00 -080090 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 * <p>There are a variety of methods for reading and writing raw arrays
92 * of primitive objects, which generally result in writing a 4-byte length
93 * followed by the primitive data items. The methods for reading can either
94 * read the data into an existing array, or create and return a new array.
95 * These available types are:</p>
Samuel Tana8036662015-11-23 14:36:00 -080096 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 * <ul>
98 * <li> {@link #writeBooleanArray(boolean[])},
99 * {@link #readBooleanArray(boolean[])}, {@link #createBooleanArray()}
100 * <li> {@link #writeByteArray(byte[])},
101 * {@link #writeByteArray(byte[], int, int)}, {@link #readByteArray(byte[])},
102 * {@link #createByteArray()}
103 * <li> {@link #writeCharArray(char[])}, {@link #readCharArray(char[])},
104 * {@link #createCharArray()}
105 * <li> {@link #writeDoubleArray(double[])}, {@link #readDoubleArray(double[])},
106 * {@link #createDoubleArray()}
107 * <li> {@link #writeFloatArray(float[])}, {@link #readFloatArray(float[])},
108 * {@link #createFloatArray()}
109 * <li> {@link #writeIntArray(int[])}, {@link #readIntArray(int[])},
110 * {@link #createIntArray()}
111 * <li> {@link #writeLongArray(long[])}, {@link #readLongArray(long[])},
112 * {@link #createLongArray()}
113 * <li> {@link #writeStringArray(String[])}, {@link #readStringArray(String[])},
114 * {@link #createStringArray()}.
115 * <li> {@link #writeSparseBooleanArray(SparseBooleanArray)},
116 * {@link #readSparseBooleanArray()}.
117 * </ul>
Samuel Tana8036662015-11-23 14:36:00 -0800118 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 * <h3>Parcelables</h3>
Samuel Tana8036662015-11-23 14:36:00 -0800120 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 * <p>The {@link Parcelable} protocol provides an extremely efficient (but
122 * low-level) protocol for objects to write and read themselves from Parcels.
123 * You can use the direct methods {@link #writeParcelable(Parcelable, int)}
124 * and {@link #readParcelable(ClassLoader)} or
125 * {@link #writeParcelableArray} and
126 * {@link #readParcelableArray(ClassLoader)} to write or read. These
127 * methods write both the class type and its data to the Parcel, allowing
128 * that class to be reconstructed from the appropriate class loader when
129 * later reading.</p>
Samuel Tana8036662015-11-23 14:36:00 -0800130 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 * <p>There are also some methods that provide a more efficient way to work
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -0700132 * with Parcelables: {@link #writeTypedObject}, {@link #writeTypedArray},
133 * {@link #writeTypedList}, {@link #readTypedObject},
134 * {@link #createTypedArray} and {@link #createTypedArrayList}. These methods
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 * do not write the class information of the original object: instead, the
136 * caller of the read function must know what type to expect and pass in the
137 * appropriate {@link Parcelable.Creator Parcelable.Creator} instead to
138 * properly construct the new object and read its data. (To more efficient
Bin Chenb6b12b52017-07-11 11:01:44 +0800139 * write and read a single Parcelable object that is not null, you can directly
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -0700140 * call {@link Parcelable#writeToParcel Parcelable.writeToParcel} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 * {@link Parcelable.Creator#createFromParcel Parcelable.Creator.createFromParcel}
142 * yourself.)</p>
Samuel Tana8036662015-11-23 14:36:00 -0800143 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 * <h3>Bundles</h3>
Samuel Tana8036662015-11-23 14:36:00 -0800145 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 * <p>A special type-safe container, called {@link Bundle}, is available
147 * for key/value maps of heterogeneous values. This has many optimizations
148 * for improved performance when reading and writing data, and its type-safe
149 * API avoids difficult to debug type errors when finally marshalling the
150 * data contents into a Parcel. The methods to use are
151 * {@link #writeBundle(Bundle)}, {@link #readBundle()}, and
152 * {@link #readBundle(ClassLoader)}.
Samuel Tana8036662015-11-23 14:36:00 -0800153 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 * <h3>Active Objects</h3>
Samuel Tana8036662015-11-23 14:36:00 -0800155 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 * <p>An unusual feature of Parcel is the ability to read and write active
157 * objects. For these objects the actual contents of the object is not
158 * written, rather a special token referencing the object is written. When
159 * reading the object back from the Parcel, you do not get a new instance of
160 * the object, but rather a handle that operates on the exact same object that
161 * was originally written. There are two forms of active objects available.</p>
Samuel Tana8036662015-11-23 14:36:00 -0800162 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 * <p>{@link Binder} objects are a core facility of Android's general cross-process
164 * communication system. The {@link IBinder} interface describes an abstract
165 * protocol with a Binder object. Any such interface can be written in to
166 * a Parcel, and upon reading you will receive either the original object
167 * implementing that interface or a special proxy implementation
168 * that communicates calls back to the original object. The methods to use are
169 * {@link #writeStrongBinder(IBinder)},
170 * {@link #writeStrongInterface(IInterface)}, {@link #readStrongBinder()},
171 * {@link #writeBinderArray(IBinder[])}, {@link #readBinderArray(IBinder[])},
172 * {@link #createBinderArray()},
173 * {@link #writeBinderList(List)}, {@link #readBinderList(List)},
174 * {@link #createBinderArrayList()}.</p>
Samuel Tana8036662015-11-23 14:36:00 -0800175 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 * <p>FileDescriptor objects, representing raw Linux file descriptor identifiers,
177 * can be written and {@link ParcelFileDescriptor} objects returned to operate
178 * on the original file descriptor. The returned file descriptor is a dup
179 * of the original file descriptor: the object and fd is different, but
180 * operating on the same underlying file stream, with the same position, etc.
181 * The methods to use are {@link #writeFileDescriptor(FileDescriptor)},
182 * {@link #readFileDescriptor()}.
Samuel Tana8036662015-11-23 14:36:00 -0800183 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 * <h3>Untyped Containers</h3>
Samuel Tana8036662015-11-23 14:36:00 -0800185 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 * <p>A final class of methods are for writing and reading standard Java
187 * containers of arbitrary types. These all revolve around the
188 * {@link #writeValue(Object)} and {@link #readValue(ClassLoader)} methods
189 * which define the types of objects allowed. The container methods are
190 * {@link #writeArray(Object[])}, {@link #readArray(ClassLoader)},
191 * {@link #writeList(List)}, {@link #readList(List, ClassLoader)},
192 * {@link #readArrayList(ClassLoader)},
193 * {@link #writeMap(Map)}, {@link #readMap(Map, ClassLoader)},
194 * {@link #writeSparseArray(SparseArray)},
195 * {@link #readSparseArray(ClassLoader)}.
196 */
197public final class Parcel {
Fyodor Kupolova81b8c02017-11-13 15:51:03 -0800198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 private static final boolean DEBUG_RECYCLE = false;
Dianne Hackborne784d1e2013-09-20 18:13:52 -0700200 private static final boolean DEBUG_ARRAY_MAP = false;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700201 private static final String TAG = "Parcel";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202
Andrei Onea24ec3212019-03-15 17:35:05 +0000203 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 @SuppressWarnings({"UnusedDeclaration"})
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000205 private long mNativePtr; // used by native code
Jeff Sharkey047238c2012-03-07 16:51:38 -0800206
207 /**
208 * Flag indicating if {@link #mNativePtr} was allocated by this object,
209 * indicating that we're responsible for its lifecycle.
210 */
211 private boolean mOwnsNativeParcelObject;
Adrian Roos04505652015-10-22 16:12:01 -0700212 private long mNativeSize;
Jeff Sharkey047238c2012-03-07 16:51:38 -0800213
Dianne Hackborn98305522017-05-05 17:53:53 -0700214 private ArrayMap<Class, Object> mClassCookies;
215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 private RuntimeException mStack;
217
Fyodor Kupolova81b8c02017-11-13 15:51:03 -0800218 /**
219 * Whether or not to parcel the stack trace of an exception. This has a performance
220 * impact, so should only be included in specific processes and only on debug builds.
221 */
222 private static boolean sParcelExceptionStackTrace;
223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 private static final int POOL_SIZE = 6;
225 private static final Parcel[] sOwnedPool = new Parcel[POOL_SIZE];
226 private static final Parcel[] sHolderPool = new Parcel[POOL_SIZE];
227
Robert Quattlebaum9cd7af32017-01-04 13:28:01 -0800228 // Keep in sync with frameworks/native/include/private/binder/ParcelValTypes.h.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 private static final int VAL_NULL = -1;
230 private static final int VAL_STRING = 0;
231 private static final int VAL_INTEGER = 1;
232 private static final int VAL_MAP = 2;
233 private static final int VAL_BUNDLE = 3;
234 private static final int VAL_PARCELABLE = 4;
235 private static final int VAL_SHORT = 5;
236 private static final int VAL_LONG = 6;
237 private static final int VAL_FLOAT = 7;
238 private static final int VAL_DOUBLE = 8;
239 private static final int VAL_BOOLEAN = 9;
240 private static final int VAL_CHARSEQUENCE = 10;
241 private static final int VAL_LIST = 11;
242 private static final int VAL_SPARSEARRAY = 12;
243 private static final int VAL_BYTEARRAY = 13;
244 private static final int VAL_STRINGARRAY = 14;
245 private static final int VAL_IBINDER = 15;
246 private static final int VAL_PARCELABLEARRAY = 16;
247 private static final int VAL_OBJECTARRAY = 17;
248 private static final int VAL_INTARRAY = 18;
249 private static final int VAL_LONGARRAY = 19;
250 private static final int VAL_BYTE = 20;
251 private static final int VAL_SERIALIZABLE = 21;
252 private static final int VAL_SPARSEBOOLEANARRAY = 22;
253 private static final int VAL_BOOLEANARRAY = 23;
Bjorn Bringert08bbffb2010-02-25 11:16:22 +0000254 private static final int VAL_CHARSEQUENCEARRAY = 24;
Craig Mautner719e6b12014-04-04 20:29:41 -0700255 private static final int VAL_PERSISTABLEBUNDLE = 25;
Jeff Sharkey5ef33982014-09-04 18:13:39 -0700256 private static final int VAL_SIZE = 26;
257 private static final int VAL_SIZEF = 27;
Samuel Tana8036662015-11-23 14:36:00 -0800258 private static final int VAL_DOUBLEARRAY = 28;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700260 // The initial int32 in a Binder call's reply Parcel header:
Christopher Wiley80fd1202015-11-22 17:12:37 -0800261 // Keep these in sync with libbinder's binder/Status.h.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 private static final int EX_SECURITY = -1;
263 private static final int EX_BAD_PARCELABLE = -2;
264 private static final int EX_ILLEGAL_ARGUMENT = -3;
265 private static final int EX_NULL_POINTER = -4;
266 private static final int EX_ILLEGAL_STATE = -5;
Dianne Hackborn7e714422013-09-13 17:32:57 -0700267 private static final int EX_NETWORK_MAIN_THREAD = -6;
Dianne Hackborn33d738a2014-09-12 14:23:58 -0700268 private static final int EX_UNSUPPORTED_OPERATION = -7;
Christopher Wiley80fd1202015-11-22 17:12:37 -0800269 private static final int EX_SERVICE_SPECIFIC = -8;
Jeff Sharkeye628b7d2017-01-17 13:50:20 -0700270 private static final int EX_PARCELABLE = -9;
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -0700271 /** @hide */
272 public static final int EX_HAS_NOTED_APPOPS_REPLY_HEADER = -127; // special; see below
273 private static final int EX_HAS_STRICTMODE_REPLY_HEADER = -128; // special; see below
Christopher Wiley80fd1202015-11-22 17:12:37 -0800274 // EX_TRANSACTION_FAILED is used exclusively in native code.
275 // see libbinder's binder/Status.h
276 private static final int EX_TRANSACTION_FAILED = -129;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -0700277
Makoto Onukib148b6c2017-06-27 13:38:38 -0700278 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000279 private static native int nativeDataSize(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700280 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000281 private static native int nativeDataAvail(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700282 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000283 private static native int nativeDataPosition(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700284 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000285 private static native int nativeDataCapacity(long nativePtr);
John Reck71207b52016-09-28 13:28:09 -0700286 @FastNative
Adrian Roos04505652015-10-22 16:12:01 -0700287 private static native long nativeSetDataSize(long nativePtr, int size);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700288 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000289 private static native void nativeSetDataPosition(long nativePtr, int pos);
John Reck71207b52016-09-28 13:28:09 -0700290 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000291 private static native void nativeSetDataCapacity(long nativePtr, int size);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800292
Makoto Onukib148b6c2017-06-27 13:38:38 -0700293 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000294 private static native boolean nativePushAllowFds(long nativePtr, boolean allowFds);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700295 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000296 private static native void nativeRestoreAllowFds(long nativePtr, boolean lastValue);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800297
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000298 private static native void nativeWriteByteArray(long nativePtr, byte[] b, int offset, int len);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700299 private static native void nativeWriteBlob(long nativePtr, byte[] b, int offset, int len);
John Reck71207b52016-09-28 13:28:09 -0700300 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000301 private static native void nativeWriteInt(long nativePtr, int val);
John Reck71207b52016-09-28 13:28:09 -0700302 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000303 private static native void nativeWriteLong(long nativePtr, long val);
John Reck71207b52016-09-28 13:28:09 -0700304 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000305 private static native void nativeWriteFloat(long nativePtr, float val);
John Reck71207b52016-09-28 13:28:09 -0700306 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000307 private static native void nativeWriteDouble(long nativePtr, double val);
Daniel Colascione012ab8e2019-10-07 14:18:09 -0700308 @FastNative
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700309 static native void nativeWriteString(long nativePtr, String val);
Daniel Colascione012ab8e2019-10-07 14:18:09 -0700310 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000311 private static native void nativeWriteStrongBinder(long nativePtr, IBinder val);
Daniel Colascione012ab8e2019-10-07 14:18:09 -0700312 @FastNative
Adrian Roos04505652015-10-22 16:12:01 -0700313 private static native long nativeWriteFileDescriptor(long nativePtr, FileDescriptor val);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800314
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000315 private static native byte[] nativeCreateByteArray(long nativePtr);
Jocelyn Dang46100442017-05-05 15:40:49 -0700316 private static native boolean nativeReadByteArray(long nativePtr, byte[] dest, int destLen);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700317 private static native byte[] nativeReadBlob(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700318 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000319 private static native int nativeReadInt(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700320 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000321 private static native long nativeReadLong(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700322 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000323 private static native float nativeReadFloat(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700324 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000325 private static native double nativeReadDouble(long nativePtr);
Daniel Colascione012ab8e2019-10-07 14:18:09 -0700326 @FastNative
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700327 static native String nativeReadString(long nativePtr);
Daniel Colascione012ab8e2019-10-07 14:18:09 -0700328 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000329 private static native IBinder nativeReadStrongBinder(long nativePtr);
Daniel Colascione012ab8e2019-10-07 14:18:09 -0700330 @FastNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000331 private static native FileDescriptor nativeReadFileDescriptor(long nativePtr);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800332
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000333 private static native long nativeCreate();
Adrian Roos04505652015-10-22 16:12:01 -0700334 private static native long nativeFreeBuffer(long nativePtr);
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000335 private static native void nativeDestroy(long nativePtr);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800336
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000337 private static native byte[] nativeMarshall(long nativePtr);
Adrian Roos04505652015-10-22 16:12:01 -0700338 private static native long nativeUnmarshall(
John Spurlocke0852362015-02-04 15:47:40 -0500339 long nativePtr, byte[] data, int offset, int length);
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700340 private static native int nativeCompareData(long thisNativePtr, long otherNativePtr);
Adrian Roos04505652015-10-22 16:12:01 -0700341 private static native long nativeAppendFrom(
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000342 long thisNativePtr, long otherNativePtr, int offset, int length);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700343 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000344 private static native boolean nativeHasFileDescriptors(long nativePtr);
345 private static native void nativeWriteInterfaceToken(long nativePtr, String interfaceName);
346 private static native void nativeEnforceInterface(long nativePtr, String interfaceName);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800347
Olivier Gaillardbab444a2019-01-30 17:11:40 +0000348 @CriticalNative
349 private static native boolean nativeReplaceCallingWorkSourceUid(
350 long nativePtr, int workSourceUid);
351 @CriticalNative
352 private static native int nativeReadCallingWorkSourceUid(long nativePtr);
353
Fyodor Kupolova81b8c02017-11-13 15:51:03 -0800354 /** Last time exception with a stack trace was written */
355 private static volatile long sLastWriteExceptionStackTrace;
356 /** Used for throttling of writing stack trace, which is costly */
357 private static final int WRITE_EXCEPTION_STACK_TRACE_THRESHOLD_MS = 1000;
358
Makoto Onukib148b6c2017-06-27 13:38:38 -0700359 @CriticalNative
Dan Sandleraa861662015-04-21 10:24:32 -0400360 private static native long nativeGetBlobAshmemSize(long nativePtr);
Dan Sandler5ce04302015-04-09 23:50:15 -0400361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 public final static Parcelable.Creator<String> STRING_CREATOR
363 = new Parcelable.Creator<String>() {
364 public String createFromParcel(Parcel source) {
365 return source.readString();
366 }
367 public String[] newArray(int size) {
368 return new String[size];
369 }
370 };
371
372 /**
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700373 * @hide
374 */
375 public static class ReadWriteHelper {
376 public static final ReadWriteHelper DEFAULT = new ReadWriteHelper();
377
378 /**
379 * Called when writing a string to a parcel. Subclasses wanting to write a string
380 * must use {@link #writeStringNoHelper(String)} to avoid
381 * infinity recursive calls.
382 */
383 public void writeString(Parcel p, String s) {
384 nativeWriteString(p.mNativePtr, s);
385 }
386
387 /**
388 * Called when reading a string to a parcel. Subclasses wanting to read a string
389 * must use {@link #readStringNoHelper()} to avoid
390 * infinity recursive calls.
391 */
392 public String readString(Parcel p) {
393 return nativeReadString(p.mNativePtr);
394 }
395 }
396
397 private ReadWriteHelper mReadWriteHelper = ReadWriteHelper.DEFAULT;
398
399 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 * Retrieve a new Parcel object from the pool.
401 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400402 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 public static Parcel obtain() {
404 final Parcel[] pool = sOwnedPool;
405 synchronized (pool) {
406 Parcel p;
407 for (int i=0; i<POOL_SIZE; i++) {
408 p = pool[i];
409 if (p != null) {
410 pool[i] = null;
411 if (DEBUG_RECYCLE) {
412 p.mStack = new RuntimeException();
413 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700414 p.mReadWriteHelper = ReadWriteHelper.DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 return p;
416 }
417 }
418 }
419 return new Parcel(0);
420 }
421
422 /**
423 * Put a Parcel object back into the pool. You must not touch
424 * the object after this call.
425 */
426 public final void recycle() {
427 if (DEBUG_RECYCLE) mStack = null;
428 freeBuffer();
Jeff Sharkey047238c2012-03-07 16:51:38 -0800429
430 final Parcel[] pool;
431 if (mOwnsNativeParcelObject) {
432 pool = sOwnedPool;
433 } else {
434 mNativePtr = 0;
435 pool = sHolderPool;
436 }
437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 synchronized (pool) {
439 for (int i=0; i<POOL_SIZE; i++) {
440 if (pool[i] == null) {
441 pool[i] = this;
442 return;
443 }
444 }
445 }
446 }
447
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700448 /**
449 * Set a {@link ReadWriteHelper}, which can be used to avoid having duplicate strings, for
450 * example.
451 *
452 * @hide
453 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400454 public void setReadWriteHelper(@Nullable ReadWriteHelper helper) {
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700455 mReadWriteHelper = helper != null ? helper : ReadWriteHelper.DEFAULT;
456 }
457
458 /**
459 * @return whether this parcel has a {@link ReadWriteHelper}.
460 *
461 * @hide
462 */
463 public boolean hasReadWriteHelper() {
464 return (mReadWriteHelper != null) && (mReadWriteHelper != ReadWriteHelper.DEFAULT);
465 }
466
Dianne Hackbornfabb70b2014-11-11 12:22:36 -0800467 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000468 @UnsupportedAppUsage
Dianne Hackbornfabb70b2014-11-11 12:22:36 -0800469 public static native long getGlobalAllocSize();
470
471 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000472 @UnsupportedAppUsage
Dianne Hackbornfabb70b2014-11-11 12:22:36 -0800473 public static native long getGlobalAllocCount();
474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 /**
476 * Returns the total amount of data contained in the parcel.
477 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800478 public final int dataSize() {
479 return nativeDataSize(mNativePtr);
480 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481
482 /**
483 * Returns the amount of data remaining to be read from the
484 * parcel. That is, {@link #dataSize}-{@link #dataPosition}.
485 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800486 public final int dataAvail() {
487 return nativeDataAvail(mNativePtr);
488 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489
490 /**
491 * Returns the current position in the parcel data. Never
492 * more than {@link #dataSize}.
493 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800494 public final int dataPosition() {
495 return nativeDataPosition(mNativePtr);
496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497
498 /**
499 * Returns the total amount of space in the parcel. This is always
500 * >= {@link #dataSize}. The difference between it and dataSize() is the
501 * amount of room left until the parcel needs to re-allocate its
502 * data buffer.
503 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800504 public final int dataCapacity() {
505 return nativeDataCapacity(mNativePtr);
506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507
508 /**
509 * Change the amount of data in the parcel. Can be either smaller or
510 * larger than the current size. If larger than the current capacity,
511 * more memory will be allocated.
512 *
513 * @param size The new number of bytes in the Parcel.
514 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800515 public final void setDataSize(int size) {
Michael Wachenschwanz138bebf2017-05-18 22:09:18 +0000516 updateNativeSize(nativeSetDataSize(mNativePtr, size));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800517 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518
519 /**
520 * Move the current read/write position in the parcel.
521 * @param pos New offset in the parcel; must be between 0 and
522 * {@link #dataSize}.
523 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800524 public final void setDataPosition(int pos) {
525 nativeSetDataPosition(mNativePtr, pos);
526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527
528 /**
529 * Change the capacity (current available space) of the parcel.
530 *
531 * @param size The new capacity of the parcel, in bytes. Can not be
532 * less than {@link #dataSize} -- that is, you can not drop existing data
533 * with this method.
534 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800535 public final void setDataCapacity(int size) {
536 nativeSetDataCapacity(mNativePtr, size);
537 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400539 /** @hide */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800540 public final boolean pushAllowFds(boolean allowFds) {
541 return nativePushAllowFds(mNativePtr, allowFds);
542 }
Dianne Hackbornc04db7e2011-10-03 21:09:35 -0700543
544 /** @hide */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800545 public final void restoreAllowFds(boolean lastValue) {
546 nativeRestoreAllowFds(mNativePtr, lastValue);
547 }
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 /**
550 * Returns the raw bytes of the parcel.
551 *
552 * <p class="note">The data you retrieve here <strong>must not</strong>
553 * be placed in any kind of persistent storage (on local disk, across
554 * a network, etc). For that, you should use standard serialization
555 * or another kind of general serialization mechanism. The Parcel
556 * marshalled representation is highly optimized for local IPC, and as
557 * such does not attempt to maintain compatibility with data created
558 * in different versions of the platform.
559 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800560 public final byte[] marshall() {
561 return nativeMarshall(mNativePtr);
562 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563
564 /**
565 * Set the bytes in data to be the raw bytes of this Parcel.
566 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400567 public final void unmarshall(@NonNull byte[] data, int offset, int length) {
Adrian Roos04505652015-10-22 16:12:01 -0700568 updateNativeSize(nativeUnmarshall(mNativePtr, data, offset, length));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800569 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570
Jeff Sharkey047238c2012-03-07 16:51:38 -0800571 public final void appendFrom(Parcel parcel, int offset, int length) {
Adrian Roos04505652015-10-22 16:12:01 -0700572 updateNativeSize(nativeAppendFrom(mNativePtr, parcel.mNativePtr, offset, length));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800573 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700575 /** @hide */
576 public final int compareData(Parcel other) {
577 return nativeCompareData(mNativePtr, other.mNativePtr);
578 }
579
Dianne Hackborn98305522017-05-05 17:53:53 -0700580 /** @hide */
581 public final void setClassCookie(Class clz, Object cookie) {
582 if (mClassCookies == null) {
583 mClassCookies = new ArrayMap<>();
584 }
585 mClassCookies.put(clz, cookie);
586 }
587
588 /** @hide */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400589 @Nullable
Dianne Hackborn98305522017-05-05 17:53:53 -0700590 public final Object getClassCookie(Class clz) {
591 return mClassCookies != null ? mClassCookies.get(clz) : null;
592 }
593
594 /** @hide */
595 public final void adoptClassCookies(Parcel from) {
596 mClassCookies = from.mClassCookies;
597 }
598
Adrian Roosfb921842017-10-26 14:49:56 +0200599 /** @hide */
600 public Map<Class, Object> copyClassCookies() {
601 return new ArrayMap<>(mClassCookies);
602 }
603
604 /** @hide */
605 public void putClassCookies(Map<Class, Object> cookies) {
606 if (cookies == null) {
607 return;
608 }
609 if (mClassCookies == null) {
610 mClassCookies = new ArrayMap<>();
611 }
612 mClassCookies.putAll(cookies);
613 }
614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 /**
616 * Report whether the parcel contains any marshalled file descriptors.
617 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800618 public final boolean hasFileDescriptors() {
619 return nativeHasFileDescriptors(mNativePtr);
620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621
622 /**
623 * Store or read an IBinder interface token in the parcel at the current
624 * {@link #dataPosition}. This is used to validate that the marshalled
625 * transaction is intended for the target interface.
626 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800627 public final void writeInterfaceToken(String interfaceName) {
628 nativeWriteInterfaceToken(mNativePtr, interfaceName);
629 }
630
631 public final void enforceInterface(String interfaceName) {
632 nativeEnforceInterface(mNativePtr, interfaceName);
633 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634
635 /**
Olivier Gaillardbab444a2019-01-30 17:11:40 +0000636 * Writes the work source uid to the request headers.
637 *
638 * <p>It requires the headers to have been written/read already to replace the work source.
639 *
640 * @return true if the request headers have been updated.
641 *
642 * @hide
643 */
644 public boolean replaceCallingWorkSourceUid(int workSourceUid) {
645 return nativeReplaceCallingWorkSourceUid(mNativePtr, workSourceUid);
646 }
647
648 /**
649 * Reads the work source uid from the request headers.
650 *
651 * <p>Unlike other read methods, this method does not read the parcel at the current
652 * {@link #dataPosition}. It will set the {@link #dataPosition} before the read and restore the
653 * position after reading the request header.
654 *
655 * @return the work source uid or {@link Binder#UNSET_WORKSOURCE} if headers have not been
656 * written/parsed yet.
657 *
658 * @hide
659 */
660 public int readCallingWorkSourceUid() {
661 return nativeReadCallingWorkSourceUid(mNativePtr);
662 }
663
664 /**
Elliott Hughesa28b83e2011-02-28 14:26:13 -0800665 * Write a byte array into the parcel at the current {@link #dataPosition},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 * growing {@link #dataCapacity} if needed.
667 * @param b Bytes to place into the parcel.
668 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400669 public final void writeByteArray(@Nullable byte[] b) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 writeByteArray(b, 0, (b != null) ? b.length : 0);
671 }
672
673 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900674 * Write a byte array into the parcel at the current {@link #dataPosition},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 * growing {@link #dataCapacity} if needed.
676 * @param b Bytes to place into the parcel.
677 * @param offset Index of first byte to be written.
678 * @param len Number of bytes to write.
679 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400680 public final void writeByteArray(@Nullable byte[] b, int offset, int len) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 if (b == null) {
682 writeInt(-1);
683 return;
684 }
Pete Gillin60f55a252018-05-10 15:40:32 +0100685 ArrayUtils.throwsIfOutOfBounds(b.length, offset, len);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800686 nativeWriteByteArray(mNativePtr, b, offset, len);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 }
688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 /**
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700690 * Write a blob of data into the parcel at the current {@link #dataPosition},
691 * growing {@link #dataCapacity} if needed.
692 * @param b Bytes to place into the parcel.
693 * {@hide}
Sandeep Siddhartha39c12fa2014-07-25 18:37:29 -0700694 * {@SystemApi}
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700695 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000696 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -0400697 public final void writeBlob(@Nullable byte[] b) {
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500698 writeBlob(b, 0, (b != null) ? b.length : 0);
699 }
700
701 /**
702 * Write a blob of data into the parcel at the current {@link #dataPosition},
703 * growing {@link #dataCapacity} if needed.
704 * @param b Bytes to place into the parcel.
705 * @param offset Index of first byte to be written.
706 * @param len Number of bytes to write.
707 * {@hide}
708 * {@SystemApi}
709 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400710 public final void writeBlob(@Nullable byte[] b, int offset, int len) {
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500711 if (b == null) {
712 writeInt(-1);
713 return;
714 }
Pete Gillin60f55a252018-05-10 15:40:32 +0100715 ArrayUtils.throwsIfOutOfBounds(b.length, offset, len);
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500716 nativeWriteBlob(mNativePtr, b, offset, len);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700717 }
718
719 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 * Write an integer value into the parcel at the current dataPosition(),
721 * growing dataCapacity() if needed.
722 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800723 public final void writeInt(int val) {
724 nativeWriteInt(mNativePtr, val);
725 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726
727 /**
728 * Write a long integer value into the parcel at the current dataPosition(),
729 * growing dataCapacity() if needed.
730 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800731 public final void writeLong(long val) {
732 nativeWriteLong(mNativePtr, val);
733 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734
735 /**
736 * Write a floating point value into the parcel at the current
737 * dataPosition(), growing dataCapacity() if needed.
738 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800739 public final void writeFloat(float val) {
740 nativeWriteFloat(mNativePtr, val);
741 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742
743 /**
744 * Write a double precision floating point value into the parcel at the
745 * current dataPosition(), growing dataCapacity() if needed.
746 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800747 public final void writeDouble(double val) {
748 nativeWriteDouble(mNativePtr, val);
749 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750
751 /**
752 * Write a string value into the parcel at the current dataPosition(),
753 * growing dataCapacity() if needed.
754 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400755 public final void writeString(@Nullable String val) {
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700756 mReadWriteHelper.writeString(this, val);
757 }
758
759 /**
760 * Write a string without going though a {@link ReadWriteHelper}. Subclasses of
761 * {@link ReadWriteHelper} must use this method instead of {@link #writeString} to avoid
762 * infinity recursive calls.
763 *
764 * @hide
765 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400766 public void writeStringNoHelper(@Nullable String val) {
Jeff Sharkey047238c2012-03-07 16:51:38 -0800767 nativeWriteString(mNativePtr, val);
768 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769
Jake Wharton2ffa4312018-04-23 17:47:14 -0400770 /**
771 * Write a boolean value into the parcel at the current dataPosition(),
772 * growing dataCapacity() if needed.
773 *
774 * <p>Note: This method currently delegates to writeInt with a value of 1 or 0
775 * for true or false, respectively, but may change in the future.
776 */
Eugene Susla36e866b2017-02-23 18:24:39 -0800777 public final void writeBoolean(boolean val) {
778 writeInt(val ? 1 : 0);
779 }
780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +0000782 * Write a CharSequence value into the parcel at the current dataPosition(),
783 * growing dataCapacity() if needed.
784 * @hide
785 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000786 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -0400787 public final void writeCharSequence(@Nullable CharSequence val) {
Bjorn Bringert08bbffb2010-02-25 11:16:22 +0000788 TextUtils.writeToParcel(val, this, 0);
789 }
790
791 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 * Write an object into the parcel at the current dataPosition(),
793 * growing dataCapacity() if needed.
794 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800795 public final void writeStrongBinder(IBinder val) {
796 nativeWriteStrongBinder(mNativePtr, val);
797 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798
799 /**
800 * Write an object into the parcel at the current dataPosition(),
801 * growing dataCapacity() if needed.
802 */
803 public final void writeStrongInterface(IInterface val) {
804 writeStrongBinder(val == null ? null : val.asBinder());
805 }
806
807 /**
808 * Write a FileDescriptor into the parcel at the current dataPosition(),
809 * growing dataCapacity() if needed.
Dan Egnorb3e4ef32010-07-20 09:03:35 -0700810 *
811 * <p class="caution">The file descriptor will not be closed, which may
812 * result in file descriptor leaks when objects are returned from Binder
813 * calls. Use {@link ParcelFileDescriptor#writeToParcel} instead, which
814 * accepts contextual flags and will close the original file descriptor
815 * if {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE} is set.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400817 public final void writeFileDescriptor(@NonNull FileDescriptor val) {
Adrian Roos04505652015-10-22 16:12:01 -0700818 updateNativeSize(nativeWriteFileDescriptor(mNativePtr, val));
819 }
820
821 private void updateNativeSize(long newNativeSize) {
822 if (mOwnsNativeParcelObject) {
823 if (newNativeSize > Integer.MAX_VALUE) {
824 newNativeSize = Integer.MAX_VALUE;
825 }
826 if (newNativeSize != mNativeSize) {
827 int delta = (int) (newNativeSize - mNativeSize);
828 if (delta > 0) {
829 VMRuntime.getRuntime().registerNativeAllocation(delta);
830 } else {
831 VMRuntime.getRuntime().registerNativeFree(-delta);
832 }
833 mNativeSize = newNativeSize;
834 }
835 }
Jeff Sharkey047238c2012-03-07 16:51:38 -0800836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837
838 /**
Casey Dahlin2f974b22015-11-05 12:19:13 -0800839 * {@hide}
840 * This will be the new name for writeFileDescriptor, for consistency.
841 **/
Jake Whartonb1f474c2018-05-30 15:11:13 -0400842 public final void writeRawFileDescriptor(@NonNull FileDescriptor val) {
Casey Dahlin2f974b22015-11-05 12:19:13 -0800843 nativeWriteFileDescriptor(mNativePtr, val);
844 }
845
846 /**
847 * {@hide}
848 * Write an array of FileDescriptor objects into the Parcel.
849 *
850 * @param value The array of objects to be written.
851 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400852 public final void writeRawFileDescriptorArray(@Nullable FileDescriptor[] value) {
Casey Dahlin2f974b22015-11-05 12:19:13 -0800853 if (value != null) {
854 int N = value.length;
855 writeInt(N);
856 for (int i=0; i<N; i++) {
857 writeRawFileDescriptor(value[i]);
858 }
859 } else {
860 writeInt(-1);
861 }
862 }
863
864 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900865 * Write a byte value into the parcel at the current dataPosition(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 * growing dataCapacity() if needed.
Jake Wharton2ffa4312018-04-23 17:47:14 -0400867 *
868 * <p>Note: This method currently delegates to writeInt but may change in
869 * the future.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 */
871 public final void writeByte(byte val) {
872 writeInt(val);
873 }
874
875 /**
876 * Please use {@link #writeBundle} instead. Flattens a Map into the parcel
877 * at the current dataPosition(),
878 * growing dataCapacity() if needed. The Map keys must be String objects.
879 * The Map values are written using {@link #writeValue} and must follow
880 * the specification there.
Samuel Tana8036662015-11-23 14:36:00 -0800881 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 * <p>It is strongly recommended to use {@link #writeBundle} instead of
883 * this method, since the Bundle class provides a type-safe API that
884 * allows you to avoid mysterious type errors at the point of marshalling.
885 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400886 public final void writeMap(@Nullable Map val) {
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700887 writeMapInternal((Map<String, Object>) val);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 }
889
890 /**
891 * Flatten a Map into the parcel at the current dataPosition(),
892 * growing dataCapacity() if needed. The Map keys must be String objects.
893 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400894 /* package */ void writeMapInternal(@Nullable Map<String,Object> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 if (val == null) {
896 writeInt(-1);
897 return;
898 }
899 Set<Map.Entry<String,Object>> entries = val.entrySet();
Michael Wachenschwanz80b9d692018-08-24 21:50:35 -0700900 int size = entries.size();
901 writeInt(size);
902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 for (Map.Entry<String,Object> e : entries) {
904 writeValue(e.getKey());
905 writeValue(e.getValue());
Michael Wachenschwanz80b9d692018-08-24 21:50:35 -0700906 size--;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 }
Michael Wachenschwanz80b9d692018-08-24 21:50:35 -0700908
909 if (size != 0) {
910 throw new BadParcelableException("Map size does not match number of entries!");
911 }
912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 }
914
915 /**
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700916 * Flatten an ArrayMap into the parcel at the current dataPosition(),
917 * growing dataCapacity() if needed. The Map keys must be String objects.
918 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400919 /* package */ void writeArrayMapInternal(@Nullable ArrayMap<String, Object> val) {
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700920 if (val == null) {
921 writeInt(-1);
922 return;
923 }
Samuel Tan3cefe6a2015-12-14 13:29:17 -0800924 // Keep the format of this Parcel in sync with writeToParcelInner() in
925 // frameworks/native/libs/binder/PersistableBundle.cpp.
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700926 final int N = val.size();
927 writeInt(N);
Dianne Hackborne784d1e2013-09-20 18:13:52 -0700928 if (DEBUG_ARRAY_MAP) {
929 RuntimeException here = new RuntimeException("here");
930 here.fillInStackTrace();
931 Log.d(TAG, "Writing " + N + " ArrayMap entries", here);
932 }
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700933 int startPos;
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700934 for (int i=0; i<N; i++) {
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700935 if (DEBUG_ARRAY_MAP) startPos = dataPosition();
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700936 writeString(val.keyAt(i));
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700937 writeValue(val.valueAt(i));
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700938 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Write #" + i + " "
939 + (dataPosition()-startPos) + " bytes: key=0x"
940 + Integer.toHexString(val.keyAt(i) != null ? val.keyAt(i).hashCode() : 0)
941 + " " + val.keyAt(i));
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700942 }
943 }
944
945 /**
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700946 * @hide For testing only.
947 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000948 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -0400949 public void writeArrayMap(@Nullable ArrayMap<String, Object> val) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700950 writeArrayMapInternal(val);
951 }
952
953 /**
Svet Ganov8455ba22019-01-02 13:05:56 -0800954 * Flatten an {@link ArrayMap} with string keys containing a particular object
955 * type into the parcel at the current dataPosition() and growing dataCapacity()
956 * if needed. The type of the objects in the array must be one that implements
957 * Parcelable. Only the raw data of the objects is written and not their type,
958 * so you must use the corresponding {@link #createTypedArrayMap(Parcelable.Creator)}
959 *
960 * @param val The map of objects to be written.
961 * @param parcelableFlags The parcelable flags to use.
962 *
963 * @see #createTypedArrayMap(Parcelable.Creator)
964 * @see Parcelable
965 */
966 public <T extends Parcelable> void writeTypedArrayMap(@Nullable ArrayMap<String, T> val,
967 int parcelableFlags) {
968 if (val == null) {
969 writeInt(-1);
970 return;
971 }
972 final int count = val.size();
973 writeInt(count);
974 for (int i = 0; i < count; i++) {
975 writeString(val.keyAt(i));
976 writeTypedObject(val.valueAt(i), parcelableFlags);
977 }
978 }
979
980 /**
Svet Ganovddb94882016-06-23 19:55:24 -0700981 * Write an array set to the parcel.
982 *
983 * @param val The array set to write.
984 *
985 * @hide
986 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000987 @UnsupportedAppUsage
Svet Ganovddb94882016-06-23 19:55:24 -0700988 public void writeArraySet(@Nullable ArraySet<? extends Object> val) {
989 final int size = (val != null) ? val.size() : -1;
990 writeInt(size);
991 for (int i = 0; i < size; i++) {
992 writeValue(val.valueAt(i));
993 }
994 }
995
996 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 * Flatten a Bundle into the parcel at the current dataPosition(),
998 * growing dataCapacity() if needed.
999 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001000 public final void writeBundle(@Nullable Bundle val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 if (val == null) {
1002 writeInt(-1);
1003 return;
1004 }
1005
Dianne Hackborn6aff9052009-05-22 13:20:23 -07001006 val.writeToParcel(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 }
1008
1009 /**
Craig Mautner719e6b12014-04-04 20:29:41 -07001010 * Flatten a PersistableBundle into the parcel at the current dataPosition(),
1011 * growing dataCapacity() if needed.
1012 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001013 public final void writePersistableBundle(@Nullable PersistableBundle val) {
Craig Mautner719e6b12014-04-04 20:29:41 -07001014 if (val == null) {
1015 writeInt(-1);
1016 return;
1017 }
1018
1019 val.writeToParcel(this, 0);
1020 }
1021
1022 /**
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001023 * Flatten a Size into the parcel at the current dataPosition(),
1024 * growing dataCapacity() if needed.
1025 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001026 public final void writeSize(@NonNull Size val) {
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001027 writeInt(val.getWidth());
1028 writeInt(val.getHeight());
1029 }
1030
1031 /**
1032 * Flatten a SizeF into the parcel at the current dataPosition(),
1033 * growing dataCapacity() if needed.
1034 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001035 public final void writeSizeF(@NonNull SizeF val) {
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001036 writeFloat(val.getWidth());
1037 writeFloat(val.getHeight());
1038 }
1039
1040 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 * Flatten a List into the parcel at the current dataPosition(), growing
1042 * dataCapacity() if needed. The List values are written using
1043 * {@link #writeValue} and must follow the specification there.
1044 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001045 public final void writeList(@Nullable List val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 if (val == null) {
1047 writeInt(-1);
1048 return;
1049 }
1050 int N = val.size();
1051 int i=0;
1052 writeInt(N);
1053 while (i < N) {
1054 writeValue(val.get(i));
1055 i++;
1056 }
1057 }
1058
1059 /**
1060 * Flatten an Object array into the parcel at the current dataPosition(),
1061 * growing dataCapacity() if needed. The array values are written using
1062 * {@link #writeValue} and must follow the specification there.
1063 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001064 public final void writeArray(@Nullable Object[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 if (val == null) {
1066 writeInt(-1);
1067 return;
1068 }
1069 int N = val.length;
1070 int i=0;
1071 writeInt(N);
1072 while (i < N) {
1073 writeValue(val[i]);
1074 i++;
1075 }
1076 }
1077
1078 /**
1079 * Flatten a generic SparseArray into the parcel at the current
1080 * dataPosition(), growing dataCapacity() if needed. The SparseArray
1081 * values are written using {@link #writeValue} and must follow the
1082 * specification there.
1083 */
Svet Ganov8455ba22019-01-02 13:05:56 -08001084 public final <T> void writeSparseArray(@Nullable SparseArray<T> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 if (val == null) {
1086 writeInt(-1);
1087 return;
1088 }
1089 int N = val.size();
1090 writeInt(N);
1091 int i=0;
1092 while (i < N) {
1093 writeInt(val.keyAt(i));
1094 writeValue(val.valueAt(i));
1095 i++;
1096 }
1097 }
1098
Jake Whartonb1f474c2018-05-30 15:11:13 -04001099 public final void writeSparseBooleanArray(@Nullable SparseBooleanArray val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 if (val == null) {
1101 writeInt(-1);
1102 return;
1103 }
1104 int N = val.size();
1105 writeInt(N);
1106 int i=0;
1107 while (i < N) {
1108 writeInt(val.keyAt(i));
1109 writeByte((byte)(val.valueAt(i) ? 1 : 0));
1110 i++;
1111 }
1112 }
1113
Adam Lesinski205656d2017-03-23 13:38:26 -07001114 /**
1115 * @hide
1116 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001117 public final void writeSparseIntArray(@Nullable SparseIntArray val) {
Adam Lesinski4e862812016-11-21 16:02:24 -08001118 if (val == null) {
1119 writeInt(-1);
1120 return;
1121 }
1122 int N = val.size();
1123 writeInt(N);
1124 int i=0;
1125 while (i < N) {
1126 writeInt(val.keyAt(i));
1127 writeInt(val.valueAt(i));
1128 i++;
1129 }
1130 }
1131
Jake Whartonb1f474c2018-05-30 15:11:13 -04001132 public final void writeBooleanArray(@Nullable boolean[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 if (val != null) {
1134 int N = val.length;
1135 writeInt(N);
1136 for (int i=0; i<N; i++) {
1137 writeInt(val[i] ? 1 : 0);
1138 }
1139 } else {
1140 writeInt(-1);
1141 }
1142 }
1143
Jake Whartonb1f474c2018-05-30 15:11:13 -04001144 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 public final boolean[] createBooleanArray() {
1146 int N = readInt();
1147 // >>2 as a fast divide-by-4 works in the create*Array() functions
1148 // because dataAvail() will never return a negative number. 4 is
1149 // the size of a stored boolean in the stream.
1150 if (N >= 0 && N <= (dataAvail() >> 2)) {
1151 boolean[] val = new boolean[N];
1152 for (int i=0; i<N; i++) {
1153 val[i] = readInt() != 0;
1154 }
1155 return val;
1156 } else {
1157 return null;
1158 }
1159 }
1160
Jake Whartonb1f474c2018-05-30 15:11:13 -04001161 public final void readBooleanArray(@NonNull boolean[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 int N = readInt();
1163 if (N == val.length) {
1164 for (int i=0; i<N; i++) {
1165 val[i] = readInt() != 0;
1166 }
1167 } else {
1168 throw new RuntimeException("bad array lengths");
1169 }
1170 }
1171
Jake Whartonb1f474c2018-05-30 15:11:13 -04001172 public final void writeCharArray(@Nullable char[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 if (val != null) {
1174 int N = val.length;
1175 writeInt(N);
1176 for (int i=0; i<N; i++) {
1177 writeInt((int)val[i]);
1178 }
1179 } else {
1180 writeInt(-1);
1181 }
1182 }
1183
Jake Whartonb1f474c2018-05-30 15:11:13 -04001184 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 public final char[] createCharArray() {
1186 int N = readInt();
1187 if (N >= 0 && N <= (dataAvail() >> 2)) {
1188 char[] val = new char[N];
1189 for (int i=0; i<N; i++) {
1190 val[i] = (char)readInt();
1191 }
1192 return val;
1193 } else {
1194 return null;
1195 }
1196 }
1197
Jake Whartonb1f474c2018-05-30 15:11:13 -04001198 public final void readCharArray(@NonNull char[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 int N = readInt();
1200 if (N == val.length) {
1201 for (int i=0; i<N; i++) {
1202 val[i] = (char)readInt();
1203 }
1204 } else {
1205 throw new RuntimeException("bad array lengths");
1206 }
1207 }
1208
Jake Whartonb1f474c2018-05-30 15:11:13 -04001209 public final void writeIntArray(@Nullable int[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 if (val != null) {
1211 int N = val.length;
1212 writeInt(N);
1213 for (int i=0; i<N; i++) {
1214 writeInt(val[i]);
1215 }
1216 } else {
1217 writeInt(-1);
1218 }
1219 }
1220
Jake Whartonb1f474c2018-05-30 15:11:13 -04001221 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 public final int[] createIntArray() {
1223 int N = readInt();
1224 if (N >= 0 && N <= (dataAvail() >> 2)) {
1225 int[] val = new int[N];
1226 for (int i=0; i<N; i++) {
1227 val[i] = readInt();
1228 }
1229 return val;
1230 } else {
1231 return null;
1232 }
1233 }
1234
Jake Whartonb1f474c2018-05-30 15:11:13 -04001235 public final void readIntArray(@NonNull int[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 int N = readInt();
1237 if (N == val.length) {
1238 for (int i=0; i<N; i++) {
1239 val[i] = readInt();
1240 }
1241 } else {
1242 throw new RuntimeException("bad array lengths");
1243 }
1244 }
1245
Jake Whartonb1f474c2018-05-30 15:11:13 -04001246 public final void writeLongArray(@Nullable long[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 if (val != null) {
1248 int N = val.length;
1249 writeInt(N);
1250 for (int i=0; i<N; i++) {
1251 writeLong(val[i]);
1252 }
1253 } else {
1254 writeInt(-1);
1255 }
1256 }
1257
Jake Whartonb1f474c2018-05-30 15:11:13 -04001258 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 public final long[] createLongArray() {
1260 int N = readInt();
1261 // >>3 because stored longs are 64 bits
1262 if (N >= 0 && N <= (dataAvail() >> 3)) {
1263 long[] val = new long[N];
1264 for (int i=0; i<N; i++) {
1265 val[i] = readLong();
1266 }
1267 return val;
1268 } else {
1269 return null;
1270 }
1271 }
1272
Jake Whartonb1f474c2018-05-30 15:11:13 -04001273 public final void readLongArray(@NonNull long[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 int N = readInt();
1275 if (N == val.length) {
1276 for (int i=0; i<N; i++) {
1277 val[i] = readLong();
1278 }
1279 } else {
1280 throw new RuntimeException("bad array lengths");
1281 }
1282 }
1283
Jake Whartonb1f474c2018-05-30 15:11:13 -04001284 public final void writeFloatArray(@Nullable float[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 if (val != null) {
1286 int N = val.length;
1287 writeInt(N);
1288 for (int i=0; i<N; i++) {
1289 writeFloat(val[i]);
1290 }
1291 } else {
1292 writeInt(-1);
1293 }
1294 }
1295
Jake Whartonb1f474c2018-05-30 15:11:13 -04001296 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 public final float[] createFloatArray() {
1298 int N = readInt();
1299 // >>2 because stored floats are 4 bytes
1300 if (N >= 0 && N <= (dataAvail() >> 2)) {
1301 float[] val = new float[N];
1302 for (int i=0; i<N; i++) {
1303 val[i] = readFloat();
1304 }
1305 return val;
1306 } else {
1307 return null;
1308 }
1309 }
1310
Jake Whartonb1f474c2018-05-30 15:11:13 -04001311 public final void readFloatArray(@NonNull float[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 int N = readInt();
1313 if (N == val.length) {
1314 for (int i=0; i<N; i++) {
1315 val[i] = readFloat();
1316 }
1317 } else {
1318 throw new RuntimeException("bad array lengths");
1319 }
1320 }
1321
Jake Whartonb1f474c2018-05-30 15:11:13 -04001322 public final void writeDoubleArray(@Nullable double[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 if (val != null) {
1324 int N = val.length;
1325 writeInt(N);
1326 for (int i=0; i<N; i++) {
1327 writeDouble(val[i]);
1328 }
1329 } else {
1330 writeInt(-1);
1331 }
1332 }
1333
Jake Whartonb1f474c2018-05-30 15:11:13 -04001334 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 public final double[] createDoubleArray() {
1336 int N = readInt();
1337 // >>3 because stored doubles are 8 bytes
1338 if (N >= 0 && N <= (dataAvail() >> 3)) {
1339 double[] val = new double[N];
1340 for (int i=0; i<N; i++) {
1341 val[i] = readDouble();
1342 }
1343 return val;
1344 } else {
1345 return null;
1346 }
1347 }
1348
Jake Whartonb1f474c2018-05-30 15:11:13 -04001349 public final void readDoubleArray(@NonNull double[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 int N = readInt();
1351 if (N == val.length) {
1352 for (int i=0; i<N; i++) {
1353 val[i] = readDouble();
1354 }
1355 } else {
1356 throw new RuntimeException("bad array lengths");
1357 }
1358 }
1359
Jake Whartonb1f474c2018-05-30 15:11:13 -04001360 public final void writeStringArray(@Nullable String[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 if (val != null) {
1362 int N = val.length;
1363 writeInt(N);
1364 for (int i=0; i<N; i++) {
1365 writeString(val[i]);
1366 }
1367 } else {
1368 writeInt(-1);
1369 }
1370 }
1371
Jake Whartonb1f474c2018-05-30 15:11:13 -04001372 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 public final String[] createStringArray() {
1374 int N = readInt();
1375 if (N >= 0) {
1376 String[] val = new String[N];
1377 for (int i=0; i<N; i++) {
1378 val[i] = readString();
1379 }
1380 return val;
1381 } else {
1382 return null;
1383 }
1384 }
1385
Jake Whartonb1f474c2018-05-30 15:11:13 -04001386 public final void readStringArray(@NonNull String[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 int N = readInt();
1388 if (N == val.length) {
1389 for (int i=0; i<N; i++) {
1390 val[i] = readString();
1391 }
1392 } else {
1393 throw new RuntimeException("bad array lengths");
1394 }
1395 }
1396
Jake Whartonb1f474c2018-05-30 15:11:13 -04001397 public final void writeBinderArray(@Nullable IBinder[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 if (val != null) {
1399 int N = val.length;
1400 writeInt(N);
1401 for (int i=0; i<N; i++) {
1402 writeStrongBinder(val[i]);
1403 }
1404 } else {
1405 writeInt(-1);
1406 }
1407 }
1408
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001409 /**
1410 * @hide
1411 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001412 public final void writeCharSequenceArray(@Nullable CharSequence[] val) {
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001413 if (val != null) {
1414 int N = val.length;
1415 writeInt(N);
1416 for (int i=0; i<N; i++) {
1417 writeCharSequence(val[i]);
1418 }
1419 } else {
1420 writeInt(-1);
1421 }
1422 }
1423
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001424 /**
1425 * @hide
1426 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001427 public final void writeCharSequenceList(@Nullable ArrayList<CharSequence> val) {
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001428 if (val != null) {
1429 int N = val.size();
1430 writeInt(N);
1431 for (int i=0; i<N; i++) {
1432 writeCharSequence(val.get(i));
1433 }
1434 } else {
1435 writeInt(-1);
1436 }
1437 }
1438
Jake Whartonb1f474c2018-05-30 15:11:13 -04001439 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 public final IBinder[] createBinderArray() {
1441 int N = readInt();
1442 if (N >= 0) {
1443 IBinder[] val = new IBinder[N];
1444 for (int i=0; i<N; i++) {
1445 val[i] = readStrongBinder();
1446 }
1447 return val;
1448 } else {
1449 return null;
1450 }
1451 }
1452
Jake Whartonb1f474c2018-05-30 15:11:13 -04001453 public final void readBinderArray(@NonNull IBinder[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 int N = readInt();
1455 if (N == val.length) {
1456 for (int i=0; i<N; i++) {
1457 val[i] = readStrongBinder();
1458 }
1459 } else {
1460 throw new RuntimeException("bad array lengths");
1461 }
1462 }
1463
1464 /**
1465 * Flatten a List containing a particular object type into the parcel, at
1466 * the current dataPosition() and growing dataCapacity() if needed. The
1467 * type of the objects in the list must be one that implements Parcelable.
1468 * Unlike the generic writeList() method, however, only the raw data of the
1469 * objects is written and not their type, so you must use the corresponding
1470 * readTypedList() to unmarshall them.
1471 *
1472 * @param val The list of objects to be written.
1473 *
1474 * @see #createTypedArrayList
1475 * @see #readTypedList
1476 * @see Parcelable
1477 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001478 public final <T extends Parcelable> void writeTypedList(@Nullable List<T> val) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07001479 writeTypedList(val, 0);
1480 }
1481
1482 /**
Svet Ganov8455ba22019-01-02 13:05:56 -08001483 * Flatten a {@link SparseArray} containing a particular object type into the parcel
1484 * at the current dataPosition() and growing dataCapacity() if needed. The
1485 * type of the objects in the array must be one that implements Parcelable.
1486 * Unlike the generic {@link #writeSparseArray(SparseArray)} method, however, only
1487 * the raw data of the objects is written and not their type, so you must use the
1488 * corresponding {@link #createTypedSparseArray(Parcelable.Creator)}.
1489 *
1490 * @param val The list of objects to be written.
1491 * @param parcelableFlags The parcelable flags to use.
1492 *
1493 * @see #createTypedSparseArray(Parcelable.Creator)
1494 * @see Parcelable
1495 */
1496 public final <T extends Parcelable> void writeTypedSparseArray(@Nullable SparseArray<T> val,
1497 int parcelableFlags) {
1498 if (val == null) {
1499 writeInt(-1);
1500 return;
1501 }
1502 final int count = val.size();
1503 writeInt(count);
1504 for (int i = 0; i < count; i++) {
1505 writeInt(val.keyAt(i));
1506 writeTypedObject(val.valueAt(i), parcelableFlags);
1507 }
1508 }
1509
1510 /**
Sunny Goyal0e60f222017-09-21 21:39:20 -07001511 * @hide
1512 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001513 public <T extends Parcelable> void writeTypedList(@Nullable List<T> val, int parcelableFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 if (val == null) {
1515 writeInt(-1);
1516 return;
1517 }
1518 int N = val.size();
1519 int i=0;
1520 writeInt(N);
1521 while (i < N) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07001522 writeTypedObject(val.get(i), parcelableFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 i++;
1524 }
1525 }
1526
1527 /**
1528 * Flatten a List containing String objects into the parcel, at
1529 * the current dataPosition() and growing dataCapacity() if needed. They
1530 * can later be retrieved with {@link #createStringArrayList} or
1531 * {@link #readStringList}.
1532 *
1533 * @param val The list of strings to be written.
1534 *
1535 * @see #createStringArrayList
1536 * @see #readStringList
1537 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001538 public final void writeStringList(@Nullable List<String> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 if (val == null) {
1540 writeInt(-1);
1541 return;
1542 }
1543 int N = val.size();
1544 int i=0;
1545 writeInt(N);
1546 while (i < N) {
1547 writeString(val.get(i));
1548 i++;
1549 }
1550 }
1551
1552 /**
1553 * Flatten a List containing IBinder objects into the parcel, at
1554 * the current dataPosition() and growing dataCapacity() if needed. They
1555 * can later be retrieved with {@link #createBinderArrayList} or
1556 * {@link #readBinderList}.
1557 *
1558 * @param val The list of strings to be written.
1559 *
1560 * @see #createBinderArrayList
1561 * @see #readBinderList
1562 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001563 public final void writeBinderList(@Nullable List<IBinder> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 if (val == null) {
1565 writeInt(-1);
1566 return;
1567 }
1568 int N = val.size();
1569 int i=0;
1570 writeInt(N);
1571 while (i < N) {
1572 writeStrongBinder(val.get(i));
1573 i++;
1574 }
1575 }
1576
1577 /**
Narayan Kamathbea48712016-12-01 15:38:28 +00001578 * Flatten a {@code List} containing arbitrary {@code Parcelable} objects into this parcel
1579 * at the current position. They can later be retrieved using
1580 * {@link #readParcelableList(List, ClassLoader)} if required.
1581 *
1582 * @see #readParcelableList(List, ClassLoader)
Narayan Kamathbea48712016-12-01 15:38:28 +00001583 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001584 public final <T extends Parcelable> void writeParcelableList(@Nullable List<T> val, int flags) {
Narayan Kamathbea48712016-12-01 15:38:28 +00001585 if (val == null) {
1586 writeInt(-1);
1587 return;
1588 }
1589
1590 int N = val.size();
1591 int i=0;
1592 writeInt(N);
1593 while (i < N) {
1594 writeParcelable(val.get(i), flags);
1595 i++;
1596 }
1597 }
1598
1599 /**
Svet Ganov0f4928f2017-02-02 20:02:51 -08001600 * Flatten a homogeneous array containing a particular object type into
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 * the parcel, at
1602 * the current dataPosition() and growing dataCapacity() if needed. The
1603 * type of the objects in the array must be one that implements Parcelable.
1604 * Unlike the {@link #writeParcelableArray} method, however, only the
1605 * raw data of the objects is written and not their type, so you must use
1606 * {@link #readTypedArray} with the correct corresponding
1607 * {@link Parcelable.Creator} implementation to unmarshall them.
1608 *
1609 * @param val The array of objects to be written.
1610 * @param parcelableFlags Contextual flags as per
1611 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1612 *
1613 * @see #readTypedArray
1614 * @see #writeParcelableArray
1615 * @see Parcelable.Creator
1616 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001617 public final <T extends Parcelable> void writeTypedArray(@Nullable T[] val,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 int parcelableFlags) {
1619 if (val != null) {
1620 int N = val.length;
1621 writeInt(N);
Svet Ganov0f4928f2017-02-02 20:02:51 -08001622 for (int i = 0; i < N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07001623 writeTypedObject(val[i], parcelableFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 }
1625 } else {
1626 writeInt(-1);
1627 }
1628 }
1629
1630 /**
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07001631 * Flatten the Parcelable object into the parcel.
1632 *
1633 * @param val The Parcelable object to be written.
1634 * @param parcelableFlags Contextual flags as per
1635 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1636 *
1637 * @see #readTypedObject
1638 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001639 public final <T extends Parcelable> void writeTypedObject(@Nullable T val,
1640 int parcelableFlags) {
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07001641 if (val != null) {
1642 writeInt(1);
1643 val.writeToParcel(this, parcelableFlags);
1644 } else {
1645 writeInt(0);
1646 }
1647 }
1648
1649 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 * Flatten a generic object in to a parcel. The given Object value may
1651 * currently be one of the following types:
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001652 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 * <ul>
1654 * <li> null
1655 * <li> String
1656 * <li> Byte
1657 * <li> Short
1658 * <li> Integer
1659 * <li> Long
1660 * <li> Float
1661 * <li> Double
1662 * <li> Boolean
1663 * <li> String[]
1664 * <li> boolean[]
1665 * <li> byte[]
1666 * <li> int[]
1667 * <li> long[]
1668 * <li> Object[] (supporting objects of the same type defined here).
1669 * <li> {@link Bundle}
1670 * <li> Map (as supported by {@link #writeMap}).
1671 * <li> Any object that implements the {@link Parcelable} protocol.
1672 * <li> Parcelable[]
1673 * <li> CharSequence (as supported by {@link TextUtils#writeToParcel}).
1674 * <li> List (as supported by {@link #writeList}).
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001675 * <li> {@link SparseArray} (as supported by {@link #writeSparseArray(SparseArray)}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 * <li> {@link IBinder}
1677 * <li> Any object that implements Serializable (but see
1678 * {@link #writeSerializable} for caveats). Note that all of the
1679 * previous types have relatively efficient implementations for
1680 * writing to a Parcel; having to rely on the generic serialization
1681 * approach is much less efficient and should be avoided whenever
1682 * possible.
1683 * </ul>
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001684 *
1685 * <p class="caution">{@link Parcelable} objects are written with
1686 * {@link Parcelable#writeToParcel} using contextual flags of 0. When
1687 * serializing objects containing {@link ParcelFileDescriptor}s,
1688 * this may result in file descriptor leaks when they are returned from
1689 * Binder calls (where {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE}
1690 * should be used).</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001692 public final void writeValue(@Nullable Object v) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 if (v == null) {
1694 writeInt(VAL_NULL);
1695 } else if (v instanceof String) {
1696 writeInt(VAL_STRING);
1697 writeString((String) v);
1698 } else if (v instanceof Integer) {
1699 writeInt(VAL_INTEGER);
1700 writeInt((Integer) v);
1701 } else if (v instanceof Map) {
1702 writeInt(VAL_MAP);
1703 writeMap((Map) v);
1704 } else if (v instanceof Bundle) {
1705 // Must be before Parcelable
1706 writeInt(VAL_BUNDLE);
1707 writeBundle((Bundle) v);
Samuel Tanceafe5e2015-12-11 16:50:58 -08001708 } else if (v instanceof PersistableBundle) {
1709 writeInt(VAL_PERSISTABLEBUNDLE);
1710 writePersistableBundle((PersistableBundle) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 } else if (v instanceof Parcelable) {
Samuel Tanceafe5e2015-12-11 16:50:58 -08001712 // IMPOTANT: cases for classes that implement Parcelable must
1713 // come before the Parcelable case, so that their specific VAL_*
1714 // types will be written.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 writeInt(VAL_PARCELABLE);
1716 writeParcelable((Parcelable) v, 0);
1717 } else if (v instanceof Short) {
1718 writeInt(VAL_SHORT);
1719 writeInt(((Short) v).intValue());
1720 } else if (v instanceof Long) {
1721 writeInt(VAL_LONG);
1722 writeLong((Long) v);
1723 } else if (v instanceof Float) {
1724 writeInt(VAL_FLOAT);
1725 writeFloat((Float) v);
1726 } else if (v instanceof Double) {
1727 writeInt(VAL_DOUBLE);
1728 writeDouble((Double) v);
1729 } else if (v instanceof Boolean) {
1730 writeInt(VAL_BOOLEAN);
1731 writeInt((Boolean) v ? 1 : 0);
1732 } else if (v instanceof CharSequence) {
1733 // Must be after String
1734 writeInt(VAL_CHARSEQUENCE);
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001735 writeCharSequence((CharSequence) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 } else if (v instanceof List) {
1737 writeInt(VAL_LIST);
1738 writeList((List) v);
1739 } else if (v instanceof SparseArray) {
1740 writeInt(VAL_SPARSEARRAY);
1741 writeSparseArray((SparseArray) v);
1742 } else if (v instanceof boolean[]) {
1743 writeInt(VAL_BOOLEANARRAY);
1744 writeBooleanArray((boolean[]) v);
1745 } else if (v instanceof byte[]) {
1746 writeInt(VAL_BYTEARRAY);
1747 writeByteArray((byte[]) v);
1748 } else if (v instanceof String[]) {
1749 writeInt(VAL_STRINGARRAY);
1750 writeStringArray((String[]) v);
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001751 } else if (v instanceof CharSequence[]) {
1752 // Must be after String[] and before Object[]
1753 writeInt(VAL_CHARSEQUENCEARRAY);
1754 writeCharSequenceArray((CharSequence[]) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 } else if (v instanceof IBinder) {
1756 writeInt(VAL_IBINDER);
1757 writeStrongBinder((IBinder) v);
1758 } else if (v instanceof Parcelable[]) {
1759 writeInt(VAL_PARCELABLEARRAY);
1760 writeParcelableArray((Parcelable[]) v, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 } else if (v instanceof int[]) {
1762 writeInt(VAL_INTARRAY);
1763 writeIntArray((int[]) v);
1764 } else if (v instanceof long[]) {
1765 writeInt(VAL_LONGARRAY);
1766 writeLongArray((long[]) v);
1767 } else if (v instanceof Byte) {
1768 writeInt(VAL_BYTE);
1769 writeInt((Byte) v);
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001770 } else if (v instanceof Size) {
1771 writeInt(VAL_SIZE);
1772 writeSize((Size) v);
1773 } else if (v instanceof SizeF) {
1774 writeInt(VAL_SIZEF);
1775 writeSizeF((SizeF) v);
Samuel Tana8036662015-11-23 14:36:00 -08001776 } else if (v instanceof double[]) {
1777 writeInt(VAL_DOUBLEARRAY);
1778 writeDoubleArray((double[]) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 } else {
Paul Duffinac5a0822014-02-03 15:02:17 +00001780 Class<?> clazz = v.getClass();
1781 if (clazz.isArray() && clazz.getComponentType() == Object.class) {
1782 // Only pure Object[] are written here, Other arrays of non-primitive types are
1783 // handled by serialization as this does not record the component type.
1784 writeInt(VAL_OBJECTARRAY);
1785 writeArray((Object[]) v);
1786 } else if (v instanceof Serializable) {
1787 // Must be last
1788 writeInt(VAL_SERIALIZABLE);
1789 writeSerializable((Serializable) v);
1790 } else {
1791 throw new RuntimeException("Parcel: unable to marshal value " + v);
1792 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 }
1794 }
1795
1796 /**
1797 * Flatten the name of the class of the Parcelable and its contents
1798 * into the parcel.
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001799 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 * @param p The Parcelable object to be written.
1801 * @param parcelableFlags Contextual flags as per
1802 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1803 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001804 public final void writeParcelable(@Nullable Parcelable p, int parcelableFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 if (p == null) {
1806 writeString(null);
1807 return;
1808 }
Neil Fuller44e440c2015-04-20 14:39:00 +01001809 writeParcelableCreator(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 p.writeToParcel(this, parcelableFlags);
1811 }
1812
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08001813 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00001814 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04001815 public final void writeParcelableCreator(@NonNull Parcelable p) {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08001816 String name = p.getClass().getName();
1817 writeString(name);
1818 }
1819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 /**
1821 * Write a generic serializable object in to a Parcel. It is strongly
1822 * recommended that this method be avoided, since the serialization
1823 * overhead is extremely large, and this approach will be much slower than
1824 * using the other approaches to writing data in to a Parcel.
1825 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001826 public final void writeSerializable(@Nullable Serializable s) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 if (s == null) {
1828 writeString(null);
1829 return;
1830 }
1831 String name = s.getClass().getName();
1832 writeString(name);
1833
1834 ByteArrayOutputStream baos = new ByteArrayOutputStream();
1835 try {
1836 ObjectOutputStream oos = new ObjectOutputStream(baos);
1837 oos.writeObject(s);
1838 oos.close();
1839
1840 writeByteArray(baos.toByteArray());
1841 } catch (IOException ioe) {
1842 throw new RuntimeException("Parcelable encountered " +
1843 "IOException writing serializable object (name = " + name +
1844 ")", ioe);
1845 }
1846 }
1847
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001848 /** @hide For debugging purposes */
1849 public static void setStackTraceParceling(boolean enabled) {
1850 sParcelExceptionStackTrace = enabled;
1851 }
1852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 /**
1854 * Special function for writing an exception result at the header of
1855 * a parcel, to be used when returning an exception from a transaction.
1856 * Note that this currently only supports a few exception types; any other
1857 * exception will be re-thrown by this function as a RuntimeException
1858 * (to be caught by the system's last-resort exception handling when
1859 * dispatching a transaction).
Samuel Tana8036662015-11-23 14:36:00 -08001860 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 * <p>The supported exception types are:
1862 * <ul>
1863 * <li>{@link BadParcelableException}
1864 * <li>{@link IllegalArgumentException}
1865 * <li>{@link IllegalStateException}
1866 * <li>{@link NullPointerException}
1867 * <li>{@link SecurityException}
Dianne Hackborn18482ae2017-08-01 17:41:00 -07001868 * <li>{@link UnsupportedOperationException}
Dianne Hackborn7e714422013-09-13 17:32:57 -07001869 * <li>{@link NetworkOnMainThreadException}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 * </ul>
Samuel Tana8036662015-11-23 14:36:00 -08001871 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 * @param e The Exception to be written.
1873 *
1874 * @see #writeNoException
1875 * @see #readException
1876 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001877 public final void writeException(@NonNull Exception e) {
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07001878 AppOpsManager.prefixParcelWithAppOpsIfNeeded(this);
1879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 int code = 0;
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001881 if (e instanceof Parcelable
1882 && (e.getClass().getClassLoader() == Parcelable.class.getClassLoader())) {
1883 // We only send Parcelable exceptions that are in the
1884 // BootClassLoader to ensure that the receiver can unpack them
1885 code = EX_PARCELABLE;
1886 } else if (e instanceof SecurityException) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887 code = EX_SECURITY;
1888 } else if (e instanceof BadParcelableException) {
1889 code = EX_BAD_PARCELABLE;
1890 } else if (e instanceof IllegalArgumentException) {
1891 code = EX_ILLEGAL_ARGUMENT;
1892 } else if (e instanceof NullPointerException) {
1893 code = EX_NULL_POINTER;
1894 } else if (e instanceof IllegalStateException) {
1895 code = EX_ILLEGAL_STATE;
Dianne Hackborn7e714422013-09-13 17:32:57 -07001896 } else if (e instanceof NetworkOnMainThreadException) {
1897 code = EX_NETWORK_MAIN_THREAD;
Dianne Hackborn33d738a2014-09-12 14:23:58 -07001898 } else if (e instanceof UnsupportedOperationException) {
1899 code = EX_UNSUPPORTED_OPERATION;
Christopher Wiley80fd1202015-11-22 17:12:37 -08001900 } else if (e instanceof ServiceSpecificException) {
1901 code = EX_SERVICE_SPECIFIC;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 }
1903 writeInt(code);
Brad Fitzpatrick703e5d32010-07-15 13:16:41 -07001904 StrictMode.clearGatheredViolations();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 if (code == 0) {
1906 if (e instanceof RuntimeException) {
1907 throw (RuntimeException) e;
1908 }
1909 throw new RuntimeException(e);
1910 }
1911 writeString(e.getMessage());
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001912 final long timeNow = sParcelExceptionStackTrace ? SystemClock.elapsedRealtime() : 0;
1913 if (sParcelExceptionStackTrace && (timeNow - sLastWriteExceptionStackTrace
1914 > WRITE_EXCEPTION_STACK_TRACE_THRESHOLD_MS)) {
1915 sLastWriteExceptionStackTrace = timeNow;
1916 final int sizePosition = dataPosition();
1917 writeInt(0); // Header size will be filled in later
1918 StackTraceElement[] stackTrace = e.getStackTrace();
1919 final int truncatedSize = Math.min(stackTrace.length, 5);
1920 StringBuilder sb = new StringBuilder();
1921 for (int i = 0; i < truncatedSize; i++) {
Fyodor Kupolov679d9982017-11-21 10:42:23 -08001922 sb.append("\tat ").append(stackTrace[i]).append('\n');
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001923 }
1924 writeString(sb.toString());
1925 final int payloadPosition = dataPosition();
1926 setDataPosition(sizePosition);
1927 // Write stack trace header size. Used in native side to skip the header
1928 writeInt(payloadPosition - sizePosition);
1929 setDataPosition(payloadPosition);
1930 } else {
1931 writeInt(0);
1932 }
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001933 switch (code) {
1934 case EX_SERVICE_SPECIFIC:
1935 writeInt(((ServiceSpecificException) e).errorCode);
1936 break;
1937 case EX_PARCELABLE:
1938 // Write parceled exception prefixed by length
1939 final int sizePosition = dataPosition();
1940 writeInt(0);
1941 writeParcelable((Parcelable) e, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
1942 final int payloadPosition = dataPosition();
1943 setDataPosition(sizePosition);
1944 writeInt(payloadPosition - sizePosition);
1945 setDataPosition(payloadPosition);
1946 break;
Christopher Wiley80fd1202015-11-22 17:12:37 -08001947 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 }
1949
1950 /**
1951 * Special function for writing information at the front of the Parcel
1952 * indicating that no exception occurred.
1953 *
1954 * @see #writeException
1955 * @see #readException
1956 */
1957 public final void writeNoException() {
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07001958 AppOpsManager.prefixParcelWithAppOpsIfNeeded(this);
1959
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001960 // Despite the name of this function ("write no exception"),
1961 // it should instead be thought of as "write the RPC response
1962 // header", but because this function name is written out by
1963 // the AIDL compiler, we're not going to rename it.
1964 //
1965 // The response header, in the non-exception case (see also
1966 // writeException above, also called by the AIDL compiler), is
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07001967 // either a 0 (the default case), or EX_HAS_STRICTMODE_REPLY_HEADER if
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001968 // StrictMode has gathered up violations that have occurred
1969 // during a Binder call, in which case we write out the number
1970 // of violations and their details, serialized, before the
1971 // actual RPC respons data. The receiving end of this is
1972 // readException(), below.
1973 if (StrictMode.hasGatheredViolations()) {
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07001974 writeInt(EX_HAS_STRICTMODE_REPLY_HEADER);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001975 final int sizePosition = dataPosition();
1976 writeInt(0); // total size of fat header, to be filled in later
1977 StrictMode.writeGatheredViolationsToParcel(this);
1978 final int payloadPosition = dataPosition();
1979 setDataPosition(sizePosition);
1980 writeInt(payloadPosition - sizePosition); // header size
1981 setDataPosition(payloadPosition);
1982 } else {
1983 writeInt(0);
1984 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 }
1986
1987 /**
1988 * Special function for reading an exception result from the header of
1989 * a parcel, to be used after receiving the result of a transaction. This
1990 * will throw the exception for you if it had been written to the Parcel,
1991 * otherwise return and let you read the normal result data from the Parcel.
1992 *
1993 * @see #writeException
1994 * @see #writeNoException
1995 */
1996 public final void readException() {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001997 int code = readExceptionCode();
1998 if (code != 0) {
1999 String msg = readString();
Takamasa Kuramitsu2214b822018-04-02 14:44:59 +09002000 readException(code, msg);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07002001 }
2002 }
2003
2004 /**
2005 * Parses the header of a Binder call's response Parcel and
2006 * returns the exception code. Deals with lite or fat headers.
2007 * In the common successful case, this header is generally zero.
2008 * In less common cases, it's a small negative number and will be
2009 * followed by an error string.
2010 *
2011 * This exists purely for android.database.DatabaseUtils and
2012 * insulating it from having to handle fat headers as returned by
2013 * e.g. StrictMode-induced RPC responses.
2014 *
2015 * @hide
2016 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002017 @UnsupportedAppUsage
Howard Chen40abbb02019-05-24 02:04:50 +08002018 @TestApi
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07002019 public final int readExceptionCode() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 int code = readInt();
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07002021 if (code == EX_HAS_NOTED_APPOPS_REPLY_HEADER) {
2022 AppOpsManager.readAndLogNotedAppops(this);
2023 // Read next header or real exception if there is no more header
2024 code = readInt();
2025 }
2026
2027 if (code == EX_HAS_STRICTMODE_REPLY_HEADER) {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07002028 int headerSize = readInt();
2029 if (headerSize == 0) {
2030 Log.e(TAG, "Unexpected zero-sized Parcel reply header.");
2031 } else {
2032 // Currently the only thing in the header is StrictMode stacks,
2033 // but discussions around event/RPC tracing suggest we might
2034 // put that here too. If so, switch on sub-header tags here.
2035 // But for now, just parse out the StrictMode stuff.
2036 StrictMode.readAndHandleBinderCallViolations(this);
2037 }
2038 // And fat response headers are currently only used when
2039 // there are no exceptions, so return no error:
2040 return 0;
2041 }
2042 return code;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 }
2044
2045 /**
Mark Doliner879ea452014-01-02 12:38:07 -08002046 * Throw an exception with the given message. Not intended for use
2047 * outside the Parcel class.
2048 *
2049 * @param code Used to determine which exception class to throw.
2050 * @param msg The exception message.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 */
2052 public final void readException(int code, String msg) {
Takamasa Kuramitsu2214b822018-04-02 14:44:59 +09002053 String remoteStackTrace = null;
2054 final int remoteStackPayloadSize = readInt();
2055 if (remoteStackPayloadSize > 0) {
2056 remoteStackTrace = readString();
2057 }
2058 Exception e = createException(code, msg);
2059 // Attach remote stack trace if availalble
2060 if (remoteStackTrace != null) {
2061 RemoteException cause = new RemoteException(
2062 "Remote stack trace:\n" + remoteStackTrace, null, false, false);
2063 try {
2064 Throwable rootCause = ExceptionUtils.getRootCause(e);
2065 if (rootCause != null) {
2066 rootCause.initCause(cause);
2067 }
2068 } catch (RuntimeException ex) {
2069 Log.e(TAG, "Cannot set cause " + cause + " for " + e, ex);
2070 }
2071 }
2072 SneakyThrow.sneakyThrow(e);
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002073 }
2074
2075 /**
2076 * Creates an exception with the given message.
2077 *
2078 * @param code Used to determine which exception class to throw.
2079 * @param msg The exception message.
2080 */
2081 private Exception createException(int code, String msg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 switch (code) {
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07002083 case EX_PARCELABLE:
2084 if (readInt() > 0) {
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002085 return (Exception) readParcelable(Parcelable.class.getClassLoader());
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07002086 } else {
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002087 return new RuntimeException(msg + " [missing Parcelable]");
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07002088 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 case EX_SECURITY:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002090 return new SecurityException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 case EX_BAD_PARCELABLE:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002092 return new BadParcelableException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 case EX_ILLEGAL_ARGUMENT:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002094 return new IllegalArgumentException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 case EX_NULL_POINTER:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002096 return new NullPointerException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 case EX_ILLEGAL_STATE:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002098 return new IllegalStateException(msg);
Dianne Hackborn7e714422013-09-13 17:32:57 -07002099 case EX_NETWORK_MAIN_THREAD:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002100 return new NetworkOnMainThreadException();
Dianne Hackborn33d738a2014-09-12 14:23:58 -07002101 case EX_UNSUPPORTED_OPERATION:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002102 return new UnsupportedOperationException(msg);
Christopher Wiley80fd1202015-11-22 17:12:37 -08002103 case EX_SERVICE_SPECIFIC:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002104 return new ServiceSpecificException(readInt(), msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 }
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002106 return new RuntimeException("Unknown exception code: " + code
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 + " msg " + msg);
2108 }
2109
2110 /**
2111 * Read an integer value from the parcel at the current dataPosition().
2112 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002113 public final int readInt() {
2114 return nativeReadInt(mNativePtr);
2115 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002116
2117 /**
2118 * Read a long integer value from the parcel at the current dataPosition().
2119 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002120 public final long readLong() {
2121 return nativeReadLong(mNativePtr);
2122 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123
2124 /**
2125 * Read a floating point value from the parcel at the current
2126 * dataPosition().
2127 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002128 public final float readFloat() {
2129 return nativeReadFloat(mNativePtr);
2130 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131
2132 /**
2133 * Read a double precision floating point value from the parcel at the
2134 * current dataPosition().
2135 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002136 public final double readDouble() {
2137 return nativeReadDouble(mNativePtr);
2138 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002139
2140 /**
2141 * Read a string value from the parcel at the current dataPosition().
2142 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002143 @Nullable
Jeff Sharkey047238c2012-03-07 16:51:38 -08002144 public final String readString() {
Makoto Onuki4501c61d2017-07-27 15:56:40 -07002145 return mReadWriteHelper.readString(this);
2146 }
2147
2148 /**
2149 * Read a string without going though a {@link ReadWriteHelper}. Subclasses of
2150 * {@link ReadWriteHelper} must use this method instead of {@link #readString} to avoid
2151 * infinity recursive calls.
2152 *
2153 * @hide
2154 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002155 @Nullable
Makoto Onuki4501c61d2017-07-27 15:56:40 -07002156 public String readStringNoHelper() {
Jeff Sharkey047238c2012-03-07 16:51:38 -08002157 return nativeReadString(mNativePtr);
2158 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159
Jake Wharton2ffa4312018-04-23 17:47:14 -04002160 /**
2161 * Read a boolean value from the parcel at the current dataPosition().
2162 */
Eugene Susla36e866b2017-02-23 18:24:39 -08002163 public final boolean readBoolean() {
2164 return readInt() != 0;
2165 }
2166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002168 * Read a CharSequence value from the parcel at the current dataPosition().
2169 * @hide
2170 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002171 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04002172 @Nullable
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002173 public final CharSequence readCharSequence() {
2174 return TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(this);
2175 }
2176
2177 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 * Read an object from the parcel at the current dataPosition().
2179 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002180 public final IBinder readStrongBinder() {
2181 return nativeReadStrongBinder(mNativePtr);
2182 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183
2184 /**
2185 * Read a FileDescriptor from the parcel at the current dataPosition().
2186 */
2187 public final ParcelFileDescriptor readFileDescriptor() {
Jeff Sharkey047238c2012-03-07 16:51:38 -08002188 FileDescriptor fd = nativeReadFileDescriptor(mNativePtr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 return fd != null ? new ParcelFileDescriptor(fd) : null;
2190 }
2191
Jeff Sharkeyda5a3e12013-08-11 12:54:42 -07002192 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +00002193 @UnsupportedAppUsage
Jeff Sharkeyda5a3e12013-08-11 12:54:42 -07002194 public final FileDescriptor readRawFileDescriptor() {
2195 return nativeReadFileDescriptor(mNativePtr);
2196 }
2197
Casey Dahlin2f974b22015-11-05 12:19:13 -08002198 /**
2199 * {@hide}
2200 * Read and return a new array of FileDescriptors from the parcel.
2201 * @return the FileDescriptor array, or null if the array is null.
2202 **/
Jake Whartonb1f474c2018-05-30 15:11:13 -04002203 @Nullable
Casey Dahlin2f974b22015-11-05 12:19:13 -08002204 public final FileDescriptor[] createRawFileDescriptorArray() {
2205 int N = readInt();
2206 if (N < 0) {
2207 return null;
2208 }
2209 FileDescriptor[] f = new FileDescriptor[N];
2210 for (int i = 0; i < N; i++) {
2211 f[i] = readRawFileDescriptor();
2212 }
2213 return f;
2214 }
2215
2216 /**
2217 * {@hide}
2218 * Read an array of FileDescriptors from a parcel.
2219 * The passed array must be exactly the length of the array in the parcel.
2220 * @return the FileDescriptor array, or null if the array is null.
2221 **/
2222 public final void readRawFileDescriptorArray(FileDescriptor[] val) {
2223 int N = readInt();
2224 if (N == val.length) {
2225 for (int i=0; i<N; i++) {
2226 val[i] = readRawFileDescriptor();
2227 }
2228 } else {
2229 throw new RuntimeException("bad array lengths");
2230 }
2231 }
2232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 /**
2234 * Read a byte value from the parcel at the current dataPosition().
2235 */
2236 public final byte readByte() {
2237 return (byte)(readInt() & 0xff);
2238 }
2239
2240 /**
2241 * Please use {@link #readBundle(ClassLoader)} instead (whose data must have
2242 * been written with {@link #writeBundle}. Read into an existing Map object
2243 * from the parcel at the current dataPosition().
2244 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002245 public final void readMap(@NonNull Map outVal, @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002246 int N = readInt();
2247 readMapInternal(outVal, N, loader);
2248 }
2249
2250 /**
2251 * Read into an existing List object from the parcel at the current
2252 * dataPosition(), using the given class loader to load any enclosed
2253 * Parcelables. If it is null, the default class loader is used.
2254 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002255 public final void readList(@NonNull List outVal, @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 int N = readInt();
2257 readListInternal(outVal, N, loader);
2258 }
2259
2260 /**
2261 * Please use {@link #readBundle(ClassLoader)} instead (whose data must have
2262 * been written with {@link #writeBundle}. Read and return a new HashMap
2263 * object from the parcel at the current dataPosition(), using the given
2264 * class loader to load any enclosed Parcelables. Returns null if
2265 * the previously written map object was null.
2266 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002267 @Nullable
2268 public final HashMap readHashMap(@Nullable ClassLoader loader)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 {
2270 int N = readInt();
2271 if (N < 0) {
2272 return null;
2273 }
2274 HashMap m = new HashMap(N);
2275 readMapInternal(m, N, loader);
2276 return m;
2277 }
2278
2279 /**
2280 * Read and return a new Bundle object from the parcel at the current
2281 * dataPosition(). Returns null if the previously written Bundle object was
2282 * null.
2283 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002284 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 public final Bundle readBundle() {
2286 return readBundle(null);
2287 }
2288
2289 /**
2290 * Read and return a new Bundle object from the parcel at the current
2291 * dataPosition(), using the given class loader to initialize the class
2292 * loader of the Bundle for later retrieval of Parcelable objects.
2293 * Returns null if the previously written Bundle object was null.
2294 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002295 @Nullable
2296 public final Bundle readBundle(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 int length = readInt();
2298 if (length < 0) {
Dianne Hackborn4a7d8242013-10-03 10:19:20 -07002299 if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002300 return null;
2301 }
Samuel Tana8036662015-11-23 14:36:00 -08002302
Dianne Hackborn6aff9052009-05-22 13:20:23 -07002303 final Bundle bundle = new Bundle(this, length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002304 if (loader != null) {
2305 bundle.setClassLoader(loader);
2306 }
2307 return bundle;
2308 }
2309
2310 /**
Craig Mautner719e6b12014-04-04 20:29:41 -07002311 * Read and return a new Bundle object from the parcel at the current
2312 * dataPosition(). Returns null if the previously written Bundle object was
2313 * null.
2314 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002315 @Nullable
Craig Mautner719e6b12014-04-04 20:29:41 -07002316 public final PersistableBundle readPersistableBundle() {
2317 return readPersistableBundle(null);
2318 }
2319
2320 /**
2321 * Read and return a new Bundle object from the parcel at the current
2322 * dataPosition(), using the given class loader to initialize the class
2323 * loader of the Bundle for later retrieval of Parcelable objects.
2324 * Returns null if the previously written Bundle object was null.
2325 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002326 @Nullable
2327 public final PersistableBundle readPersistableBundle(@Nullable ClassLoader loader) {
Craig Mautner719e6b12014-04-04 20:29:41 -07002328 int length = readInt();
2329 if (length < 0) {
2330 if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length);
2331 return null;
2332 }
2333
2334 final PersistableBundle bundle = new PersistableBundle(this, length);
2335 if (loader != null) {
2336 bundle.setClassLoader(loader);
2337 }
2338 return bundle;
2339 }
2340
2341 /**
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002342 * Read a Size from the parcel at the current dataPosition().
2343 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002344 @NonNull
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002345 public final Size readSize() {
2346 final int width = readInt();
2347 final int height = readInt();
2348 return new Size(width, height);
2349 }
2350
2351 /**
2352 * Read a SizeF from the parcel at the current dataPosition().
2353 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002354 @NonNull
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002355 public final SizeF readSizeF() {
2356 final float width = readFloat();
2357 final float height = readFloat();
2358 return new SizeF(width, height);
2359 }
2360
2361 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 * Read and return a byte[] object from the parcel.
2363 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002364 @Nullable
Jeff Sharkey047238c2012-03-07 16:51:38 -08002365 public final byte[] createByteArray() {
2366 return nativeCreateByteArray(mNativePtr);
2367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368
2369 /**
2370 * Read a byte[] object from the parcel and copy it into the
2371 * given byte array.
2372 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002373 public final void readByteArray(@NonNull byte[] val) {
Jocelyn Dang46100442017-05-05 15:40:49 -07002374 boolean valid = nativeReadByteArray(mNativePtr, val, (val != null) ? val.length : 0);
2375 if (!valid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 throw new RuntimeException("bad array lengths");
2377 }
2378 }
2379
2380 /**
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002381 * Read a blob of data from the parcel and return it as a byte array.
2382 * {@hide}
Sandeep Siddhartha39c12fa2014-07-25 18:37:29 -07002383 * {@SystemApi}
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002384 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002385 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04002386 @Nullable
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002387 public final byte[] readBlob() {
2388 return nativeReadBlob(mNativePtr);
2389 }
2390
2391 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 * Read and return a String[] object from the parcel.
2393 * {@hide}
2394 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002395 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04002396 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 public final String[] readStringArray() {
2398 String[] array = null;
2399
2400 int length = readInt();
2401 if (length >= 0)
2402 {
2403 array = new String[length];
2404
2405 for (int i = 0 ; i < length ; i++)
2406 {
2407 array[i] = readString();
2408 }
2409 }
2410
2411 return array;
2412 }
2413
2414 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002415 * Read and return a CharSequence[] object from the parcel.
2416 * {@hide}
2417 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002418 @Nullable
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002419 public final CharSequence[] readCharSequenceArray() {
2420 CharSequence[] array = null;
2421
2422 int length = readInt();
2423 if (length >= 0)
2424 {
2425 array = new CharSequence[length];
2426
2427 for (int i = 0 ; i < length ; i++)
2428 {
2429 array[i] = readCharSequence();
2430 }
2431 }
2432
2433 return array;
2434 }
2435
2436 /**
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002437 * Read and return an ArrayList&lt;CharSequence&gt; object from the parcel.
2438 * {@hide}
2439 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002440 @Nullable
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002441 public final ArrayList<CharSequence> readCharSequenceList() {
2442 ArrayList<CharSequence> array = null;
2443
2444 int length = readInt();
2445 if (length >= 0) {
2446 array = new ArrayList<CharSequence>(length);
2447
2448 for (int i = 0 ; i < length ; i++) {
2449 array.add(readCharSequence());
2450 }
2451 }
2452
2453 return array;
2454 }
2455
2456 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 * Read and return a new ArrayList object from the parcel at the current
2458 * dataPosition(). Returns null if the previously written list object was
2459 * null. The given class loader will be used to load any enclosed
2460 * Parcelables.
2461 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002462 @Nullable
2463 public final ArrayList readArrayList(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 int N = readInt();
2465 if (N < 0) {
2466 return null;
2467 }
2468 ArrayList l = new ArrayList(N);
2469 readListInternal(l, N, loader);
2470 return l;
2471 }
2472
2473 /**
2474 * Read and return a new Object array from the parcel at the current
2475 * dataPosition(). Returns null if the previously written array was
2476 * null. The given class loader will be used to load any enclosed
2477 * Parcelables.
2478 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002479 @Nullable
2480 public final Object[] readArray(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 int N = readInt();
2482 if (N < 0) {
2483 return null;
2484 }
2485 Object[] l = new Object[N];
2486 readArrayInternal(l, N, loader);
2487 return l;
2488 }
2489
2490 /**
2491 * Read and return a new SparseArray object from the parcel at the current
2492 * dataPosition(). Returns null if the previously written list object was
2493 * null. The given class loader will be used to load any enclosed
2494 * Parcelables.
2495 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002496 @Nullable
Svet Ganov8455ba22019-01-02 13:05:56 -08002497 public final <T> SparseArray<T> readSparseArray(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 int N = readInt();
2499 if (N < 0) {
2500 return null;
2501 }
2502 SparseArray sa = new SparseArray(N);
2503 readSparseArrayInternal(sa, N, loader);
2504 return sa;
2505 }
2506
2507 /**
2508 * Read and return a new SparseBooleanArray object from the parcel at the current
2509 * dataPosition(). Returns null if the previously written list object was
2510 * null.
2511 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002512 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002513 public final SparseBooleanArray readSparseBooleanArray() {
2514 int N = readInt();
2515 if (N < 0) {
2516 return null;
2517 }
2518 SparseBooleanArray sa = new SparseBooleanArray(N);
2519 readSparseBooleanArrayInternal(sa, N);
2520 return sa;
2521 }
2522
2523 /**
Adam Lesinski4e862812016-11-21 16:02:24 -08002524 * Read and return a new SparseIntArray object from the parcel at the current
2525 * dataPosition(). Returns null if the previously written array object was null.
Adam Lesinski205656d2017-03-23 13:38:26 -07002526 * @hide
Adam Lesinski4e862812016-11-21 16:02:24 -08002527 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002528 @Nullable
Adam Lesinski4e862812016-11-21 16:02:24 -08002529 public final SparseIntArray readSparseIntArray() {
2530 int N = readInt();
2531 if (N < 0) {
2532 return null;
2533 }
2534 SparseIntArray sa = new SparseIntArray(N);
2535 readSparseIntArrayInternal(sa, N);
2536 return sa;
2537 }
2538
2539 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540 * Read and return a new ArrayList containing a particular object type from
2541 * the parcel that was written with {@link #writeTypedList} at the
2542 * current dataPosition(). Returns null if the
2543 * previously written list object was null. The list <em>must</em> have
2544 * previously been written via {@link #writeTypedList} with the same object
2545 * type.
2546 *
2547 * @return A newly created ArrayList containing objects with the same data
2548 * as those that were previously written.
2549 *
2550 * @see #writeTypedList
2551 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002552 @Nullable
2553 public final <T> ArrayList<T> createTypedArrayList(@NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554 int N = readInt();
2555 if (N < 0) {
2556 return null;
2557 }
2558 ArrayList<T> l = new ArrayList<T>(N);
2559 while (N > 0) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002560 l.add(readTypedObject(c));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002561 N--;
2562 }
2563 return l;
2564 }
2565
2566 /**
2567 * Read into the given List items containing a particular object type
2568 * that were written with {@link #writeTypedList} at the
2569 * current dataPosition(). The list <em>must</em> have
2570 * previously been written via {@link #writeTypedList} with the same object
2571 * type.
2572 *
2573 * @return A newly created ArrayList containing objects with the same data
2574 * as those that were previously written.
2575 *
2576 * @see #writeTypedList
2577 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002578 public final <T> void readTypedList(@NonNull List<T> list, @NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579 int M = list.size();
2580 int N = readInt();
2581 int i = 0;
2582 for (; i < M && i < N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002583 list.set(i, readTypedObject(c));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002584 }
2585 for (; i<N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002586 list.add(readTypedObject(c));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 }
2588 for (; i<M; i++) {
2589 list.remove(N);
2590 }
2591 }
2592
2593 /**
Svet Ganov8455ba22019-01-02 13:05:56 -08002594 * Read into a new {@link SparseArray} items containing a particular object type
2595 * that were written with {@link #writeTypedSparseArray(SparseArray, int)} at the
2596 * current dataPosition(). The list <em>must</em> have previously been written
2597 * via {@link #writeTypedSparseArray(SparseArray, int)} with the same object type.
2598 *
2599 * @param creator The creator to use when for instantiation.
2600 *
2601 * @return A newly created {@link SparseArray} containing objects with the same data
2602 * as those that were previously written.
2603 *
2604 * @see #writeTypedSparseArray(SparseArray, int)
2605 */
2606 public final @Nullable <T extends Parcelable> SparseArray<T> createTypedSparseArray(
2607 @NonNull Parcelable.Creator<T> creator) {
2608 final int count = readInt();
2609 if (count < 0) {
2610 return null;
2611 }
2612 final SparseArray<T> array = new SparseArray<>(count);
2613 for (int i = 0; i < count; i++) {
2614 final int index = readInt();
2615 final T value = readTypedObject(creator);
2616 array.append(index, value);
2617 }
2618 return array;
2619 }
2620
2621 /**
2622 * Read into a new {@link ArrayMap} with string keys items containing a particular
2623 * object type that were written with {@link #writeTypedArrayMap(ArrayMap, int)} at the
2624 * current dataPosition(). The list <em>must</em> have previously been written
2625 * via {@link #writeTypedArrayMap(ArrayMap, int)} with the same object type.
2626 *
2627 * @param creator The creator to use when for instantiation.
2628 *
2629 * @return A newly created {@link ArrayMap} containing objects with the same data
2630 * as those that were previously written.
2631 *
2632 * @see #writeTypedArrayMap(ArrayMap, int)
2633 */
2634 public final @Nullable <T extends Parcelable> ArrayMap<String, T> createTypedArrayMap(
2635 @NonNull Parcelable.Creator<T> creator) {
2636 final int count = readInt();
2637 if (count < 0) {
2638 return null;
2639 }
2640 final ArrayMap<String, T> map = new ArrayMap<>(count);
2641 for (int i = 0; i < count; i++) {
2642 final String key = readString();
2643 final T value = readTypedObject(creator);
2644 map.append(key, value);
2645 }
2646 return map;
2647 }
2648
2649 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 * Read and return a new ArrayList containing String objects from
2651 * the parcel that was written with {@link #writeStringList} at the
2652 * current dataPosition(). Returns null if the
2653 * previously written list object was null.
2654 *
2655 * @return A newly created ArrayList containing strings with the same data
2656 * as those that were previously written.
2657 *
2658 * @see #writeStringList
2659 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002660 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661 public final ArrayList<String> createStringArrayList() {
2662 int N = readInt();
2663 if (N < 0) {
2664 return null;
2665 }
2666 ArrayList<String> l = new ArrayList<String>(N);
2667 while (N > 0) {
2668 l.add(readString());
2669 N--;
2670 }
2671 return l;
2672 }
2673
2674 /**
2675 * Read and return a new ArrayList containing IBinder objects from
2676 * the parcel that was written with {@link #writeBinderList} at the
2677 * current dataPosition(). Returns null if the
2678 * previously written list object was null.
2679 *
2680 * @return A newly created ArrayList containing strings with the same data
2681 * as those that were previously written.
2682 *
2683 * @see #writeBinderList
2684 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002685 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 public final ArrayList<IBinder> createBinderArrayList() {
2687 int N = readInt();
2688 if (N < 0) {
2689 return null;
2690 }
2691 ArrayList<IBinder> l = new ArrayList<IBinder>(N);
2692 while (N > 0) {
2693 l.add(readStrongBinder());
2694 N--;
2695 }
2696 return l;
2697 }
2698
2699 /**
2700 * Read into the given List items String objects that were written with
2701 * {@link #writeStringList} at the current dataPosition().
2702 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 * @see #writeStringList
2704 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002705 public final void readStringList(@NonNull List<String> list) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 int M = list.size();
2707 int N = readInt();
2708 int i = 0;
2709 for (; i < M && i < N; i++) {
2710 list.set(i, readString());
2711 }
2712 for (; i<N; i++) {
2713 list.add(readString());
2714 }
2715 for (; i<M; i++) {
2716 list.remove(N);
2717 }
2718 }
2719
2720 /**
2721 * Read into the given List items IBinder objects that were written with
2722 * {@link #writeBinderList} at the current dataPosition().
2723 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 * @see #writeBinderList
2725 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002726 public final void readBinderList(@NonNull List<IBinder> list) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 int M = list.size();
2728 int N = readInt();
2729 int i = 0;
2730 for (; i < M && i < N; i++) {
2731 list.set(i, readStrongBinder());
2732 }
2733 for (; i<N; i++) {
2734 list.add(readStrongBinder());
2735 }
2736 for (; i<M; i++) {
2737 list.remove(N);
2738 }
2739 }
2740
2741 /**
Narayan Kamathbea48712016-12-01 15:38:28 +00002742 * Read the list of {@code Parcelable} objects at the current data position into the
2743 * given {@code list}. The contents of the {@code list} are replaced. If the serialized
2744 * list was {@code null}, {@code list} is cleared.
2745 *
2746 * @see #writeParcelableList(List, int)
Narayan Kamathbea48712016-12-01 15:38:28 +00002747 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002748 @NonNull
2749 public final <T extends Parcelable> List<T> readParcelableList(@NonNull List<T> list,
2750 @Nullable ClassLoader cl) {
Narayan Kamathbea48712016-12-01 15:38:28 +00002751 final int N = readInt();
2752 if (N == -1) {
2753 list.clear();
Eugene Susla36e866b2017-02-23 18:24:39 -08002754 return list;
Narayan Kamathbea48712016-12-01 15:38:28 +00002755 }
2756
2757 final int M = list.size();
2758 int i = 0;
2759 for (; i < M && i < N; i++) {
2760 list.set(i, (T) readParcelable(cl));
2761 }
2762 for (; i<N; i++) {
2763 list.add((T) readParcelable(cl));
2764 }
2765 for (; i<M; i++) {
2766 list.remove(N);
2767 }
Eugene Susla36e866b2017-02-23 18:24:39 -08002768 return list;
Narayan Kamathbea48712016-12-01 15:38:28 +00002769 }
2770
2771 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 * Read and return a new array containing a particular object type from
2773 * the parcel at the current dataPosition(). Returns null if the
2774 * previously written array was null. The array <em>must</em> have
2775 * previously been written via {@link #writeTypedArray} with the same
2776 * object type.
2777 *
2778 * @return A newly created array containing objects with the same data
2779 * as those that were previously written.
2780 *
2781 * @see #writeTypedArray
2782 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002783 @Nullable
2784 public final <T> T[] createTypedArray(@NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 int N = readInt();
2786 if (N < 0) {
2787 return null;
2788 }
2789 T[] l = c.newArray(N);
2790 for (int i=0; i<N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002791 l[i] = readTypedObject(c);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792 }
2793 return l;
2794 }
2795
Jake Whartonb1f474c2018-05-30 15:11:13 -04002796 public final <T> void readTypedArray(@NonNull T[] val, @NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 int N = readInt();
2798 if (N == val.length) {
2799 for (int i=0; i<N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002800 val[i] = readTypedObject(c);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 }
2802 } else {
2803 throw new RuntimeException("bad array lengths");
2804 }
2805 }
2806
2807 /**
2808 * @deprecated
2809 * @hide
2810 */
2811 @Deprecated
2812 public final <T> T[] readTypedArray(Parcelable.Creator<T> c) {
2813 return createTypedArray(c);
2814 }
2815
2816 /**
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07002817 * Read and return a typed Parcelable object from a parcel.
2818 * Returns null if the previous written object was null.
2819 * The object <em>must</em> have previous been written via
2820 * {@link #writeTypedObject} with the same object type.
2821 *
2822 * @return A newly created object of the type that was previously
2823 * written.
2824 *
2825 * @see #writeTypedObject
2826 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002827 @Nullable
2828 public final <T> T readTypedObject(@NonNull Parcelable.Creator<T> c) {
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07002829 if (readInt() != 0) {
2830 return c.createFromParcel(this);
2831 } else {
2832 return null;
2833 }
2834 }
2835
2836 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 * Write a heterogeneous array of Parcelable objects into the Parcel.
2838 * Each object in the array is written along with its class name, so
2839 * that the correct class can later be instantiated. As a result, this
2840 * has significantly more overhead than {@link #writeTypedArray}, but will
2841 * correctly handle an array containing more than one type of object.
2842 *
2843 * @param value The array of objects to be written.
2844 * @param parcelableFlags Contextual flags as per
2845 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
2846 *
2847 * @see #writeTypedArray
2848 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002849 public final <T extends Parcelable> void writeParcelableArray(@Nullable T[] value,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002850 int parcelableFlags) {
2851 if (value != null) {
2852 int N = value.length;
2853 writeInt(N);
2854 for (int i=0; i<N; i++) {
2855 writeParcelable(value[i], parcelableFlags);
2856 }
2857 } else {
2858 writeInt(-1);
2859 }
2860 }
2861
2862 /**
2863 * Read a typed object from a parcel. The given class loader will be
2864 * used to load any enclosed Parcelables. If it is null, the default class
2865 * loader will be used.
2866 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002867 @Nullable
2868 public final Object readValue(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 int type = readInt();
2870
2871 switch (type) {
2872 case VAL_NULL:
2873 return null;
2874
2875 case VAL_STRING:
2876 return readString();
2877
2878 case VAL_INTEGER:
2879 return readInt();
2880
2881 case VAL_MAP:
2882 return readHashMap(loader);
2883
2884 case VAL_PARCELABLE:
2885 return readParcelable(loader);
2886
2887 case VAL_SHORT:
2888 return (short) readInt();
2889
2890 case VAL_LONG:
2891 return readLong();
2892
2893 case VAL_FLOAT:
2894 return readFloat();
2895
2896 case VAL_DOUBLE:
2897 return readDouble();
2898
2899 case VAL_BOOLEAN:
2900 return readInt() == 1;
2901
2902 case VAL_CHARSEQUENCE:
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002903 return readCharSequence();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904
2905 case VAL_LIST:
2906 return readArrayList(loader);
2907
2908 case VAL_BOOLEANARRAY:
Samuel Tana8036662015-11-23 14:36:00 -08002909 return createBooleanArray();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910
2911 case VAL_BYTEARRAY:
2912 return createByteArray();
2913
2914 case VAL_STRINGARRAY:
2915 return readStringArray();
2916
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002917 case VAL_CHARSEQUENCEARRAY:
2918 return readCharSequenceArray();
2919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 case VAL_IBINDER:
2921 return readStrongBinder();
2922
2923 case VAL_OBJECTARRAY:
2924 return readArray(loader);
2925
2926 case VAL_INTARRAY:
2927 return createIntArray();
2928
2929 case VAL_LONGARRAY:
2930 return createLongArray();
2931
2932 case VAL_BYTE:
2933 return readByte();
2934
2935 case VAL_SERIALIZABLE:
John Spurlock5002b8c2014-01-10 13:32:12 -05002936 return readSerializable(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937
2938 case VAL_PARCELABLEARRAY:
2939 return readParcelableArray(loader);
2940
2941 case VAL_SPARSEARRAY:
2942 return readSparseArray(loader);
2943
2944 case VAL_SPARSEBOOLEANARRAY:
2945 return readSparseBooleanArray();
2946
2947 case VAL_BUNDLE:
2948 return readBundle(loader); // loading will be deferred
2949
Craig Mautner719e6b12014-04-04 20:29:41 -07002950 case VAL_PERSISTABLEBUNDLE:
2951 return readPersistableBundle(loader);
2952
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002953 case VAL_SIZE:
2954 return readSize();
2955
2956 case VAL_SIZEF:
2957 return readSizeF();
2958
Samuel Tana8036662015-11-23 14:36:00 -08002959 case VAL_DOUBLEARRAY:
2960 return createDoubleArray();
2961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962 default:
2963 int off = dataPosition() - 4;
2964 throw new RuntimeException(
2965 "Parcel " + this + ": Unmarshalling unknown type code " + type + " at offset " + off);
2966 }
2967 }
2968
2969 /**
2970 * Read and return a new Parcelable from the parcel. The given class loader
2971 * will be used to load any enclosed Parcelables. If it is null, the default
2972 * class loader will be used.
2973 * @param loader A ClassLoader from which to instantiate the Parcelable
2974 * object, or null for the default class loader.
2975 * @return Returns the newly created Parcelable, or null if a null
2976 * object has been written.
2977 * @throws BadParcelableException Throws BadParcelableException if there
2978 * was an error trying to instantiate the Parcelable.
2979 */
Neil Fuller44e440c2015-04-20 14:39:00 +01002980 @SuppressWarnings("unchecked")
Jake Whartonb1f474c2018-05-30 15:11:13 -04002981 @Nullable
2982 public final <T extends Parcelable> T readParcelable(@Nullable ClassLoader loader) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002983 Parcelable.Creator<?> creator = readParcelableCreator(loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002984 if (creator == null) {
2985 return null;
2986 }
2987 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002988 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
2989 (Parcelable.ClassLoaderCreator<?>) creator;
2990 return (T) classLoaderCreator.createFromParcel(this, loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002991 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002992 return (T) creator.createFromParcel(this);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002993 }
2994
2995 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00002996 @UnsupportedAppUsage
Neil Fuller44e440c2015-04-20 14:39:00 +01002997 @SuppressWarnings("unchecked")
Jake Whartonb1f474c2018-05-30 15:11:13 -04002998 @Nullable
2999 public final <T extends Parcelable> T readCreator(@NonNull Parcelable.Creator<?> creator,
3000 @Nullable ClassLoader loader) {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08003001 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
Neil Fuller44e440c2015-04-20 14:39:00 +01003002 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
3003 (Parcelable.ClassLoaderCreator<?>) creator;
3004 return (T) classLoaderCreator.createFromParcel(this, loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08003005 }
Neil Fuller44e440c2015-04-20 14:39:00 +01003006 return (T) creator.createFromParcel(this);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08003007 }
3008
3009 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00003010 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04003011 @Nullable
3012 public final Parcelable.Creator<?> readParcelableCreator(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 String name = readString();
3014 if (name == null) {
3015 return null;
3016 }
Neil Fuller44e440c2015-04-20 14:39:00 +01003017 Parcelable.Creator<?> creator;
Andreas Gampe4c0df5c2019-05-30 14:38:43 -07003018 HashMap<String, Parcelable.Creator<?>> map;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003019 synchronized (mCreators) {
Andreas Gampe4c0df5c2019-05-30 14:38:43 -07003020 map = mCreators.get(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003021 if (map == null) {
Neil Fuller44e440c2015-04-20 14:39:00 +01003022 map = new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 mCreators.put(loader, map);
3024 }
3025 creator = map.get(name);
Andreas Gampe4c0df5c2019-05-30 14:38:43 -07003026 }
3027 if (creator != null) {
3028 return creator;
3029 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003030
Andreas Gampe4c0df5c2019-05-30 14:38:43 -07003031 try {
3032 // If loader == null, explicitly emulate Class.forName(String) "caller
3033 // classloader" behavior.
3034 ClassLoader parcelableClassLoader =
3035 (loader == null ? getClass().getClassLoader() : loader);
3036 // Avoid initializing the Parcelable class until we know it implements
3037 // Parcelable and has the necessary CREATOR field. http://b/1171613.
3038 Class<?> parcelableClass = Class.forName(name, false /* initialize */,
3039 parcelableClassLoader);
3040 if (!Parcelable.class.isAssignableFrom(parcelableClass)) {
3041 throw new BadParcelableException("Parcelable protocol requires subclassing "
3042 + "from Parcelable on class " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003043 }
Andreas Gampe4c0df5c2019-05-30 14:38:43 -07003044 Field f = parcelableClass.getField("CREATOR");
3045 if ((f.getModifiers() & Modifier.STATIC) == 0) {
3046 throw new BadParcelableException("Parcelable protocol requires "
3047 + "the CREATOR object to be static on class " + name);
3048 }
3049 Class<?> creatorType = f.getType();
3050 if (!Parcelable.Creator.class.isAssignableFrom(creatorType)) {
3051 // Fail before calling Field.get(), not after, to avoid initializing
3052 // parcelableClass unnecessarily.
3053 throw new BadParcelableException("Parcelable protocol requires a "
3054 + "Parcelable.Creator object called "
3055 + "CREATOR on class " + name);
3056 }
3057 creator = (Parcelable.Creator<?>) f.get(null);
3058 } catch (IllegalAccessException e) {
3059 Log.e(TAG, "Illegal access when unmarshalling: " + name, e);
3060 throw new BadParcelableException(
3061 "IllegalAccessException when unmarshalling: " + name);
3062 } catch (ClassNotFoundException e) {
3063 Log.e(TAG, "Class not found when unmarshalling: " + name, e);
3064 throw new BadParcelableException(
3065 "ClassNotFoundException when unmarshalling: " + name);
3066 } catch (NoSuchFieldException e) {
3067 throw new BadParcelableException("Parcelable protocol requires a "
3068 + "Parcelable.Creator object called "
3069 + "CREATOR on class " + name);
3070 }
3071 if (creator == null) {
3072 throw new BadParcelableException("Parcelable protocol requires a "
3073 + "non-null Parcelable.Creator object called "
3074 + "CREATOR on class " + name);
3075 }
3076
3077 synchronized (mCreators) {
3078 map.put(name, creator);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003079 }
3080
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08003081 return creator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082 }
3083
3084 /**
3085 * Read and return a new Parcelable array from the parcel.
3086 * The given class loader will be used to load any enclosed
3087 * Parcelables.
3088 * @return the Parcelable array, or null if the array is null
3089 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04003090 @Nullable
3091 public final Parcelable[] readParcelableArray(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092 int N = readInt();
3093 if (N < 0) {
3094 return null;
3095 }
3096 Parcelable[] p = new Parcelable[N];
3097 for (int i = 0; i < N; i++) {
Neil Fuller44e440c2015-04-20 14:39:00 +01003098 p[i] = readParcelable(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003099 }
3100 return p;
3101 }
3102
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003103 /** @hide */
Jake Whartonb1f474c2018-05-30 15:11:13 -04003104 @Nullable
3105 public final <T extends Parcelable> T[] readParcelableArray(@Nullable ClassLoader loader,
3106 @NonNull Class<T> clazz) {
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003107 int N = readInt();
3108 if (N < 0) {
3109 return null;
3110 }
3111 T[] p = (T[]) Array.newInstance(clazz, N);
3112 for (int i = 0; i < N; i++) {
3113 p[i] = readParcelable(loader);
3114 }
3115 return p;
3116 }
3117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003118 /**
3119 * Read and return a new Serializable object from the parcel.
3120 * @return the Serializable object, or null if the Serializable name
3121 * wasn't found in the parcel.
3122 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04003123 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003124 public final Serializable readSerializable() {
John Spurlock5002b8c2014-01-10 13:32:12 -05003125 return readSerializable(null);
3126 }
3127
Jake Whartonb1f474c2018-05-30 15:11:13 -04003128 @Nullable
3129 private final Serializable readSerializable(@Nullable final ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003130 String name = readString();
3131 if (name == null) {
3132 // For some reason we were unable to read the name of the Serializable (either there
3133 // is nothing left in the Parcel to read, or the next value wasn't a String), so
3134 // return null, which indicates that the name wasn't found in the parcel.
3135 return null;
3136 }
3137
3138 byte[] serializedData = createByteArray();
3139 ByteArrayInputStream bais = new ByteArrayInputStream(serializedData);
3140 try {
John Spurlock5002b8c2014-01-10 13:32:12 -05003141 ObjectInputStream ois = new ObjectInputStream(bais) {
3142 @Override
3143 protected Class<?> resolveClass(ObjectStreamClass osClass)
3144 throws IOException, ClassNotFoundException {
3145 // try the custom classloader if provided
3146 if (loader != null) {
3147 Class<?> c = Class.forName(osClass.getName(), false, loader);
3148 if (c != null) {
3149 return c;
3150 }
3151 }
3152 return super.resolveClass(osClass);
3153 }
3154 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003155 return (Serializable) ois.readObject();
3156 } catch (IOException ioe) {
3157 throw new RuntimeException("Parcelable encountered " +
3158 "IOException reading a Serializable object (name = " + name +
3159 ")", ioe);
3160 } catch (ClassNotFoundException cnfe) {
John Spurlock5002b8c2014-01-10 13:32:12 -05003161 throw new RuntimeException("Parcelable encountered " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 "ClassNotFoundException reading a Serializable object (name = "
3163 + name + ")", cnfe);
3164 }
3165 }
3166
3167 // Cache of previously looked up CREATOR.createFromParcel() methods for
3168 // particular classes. Keys are the names of the classes, values are
3169 // Method objects.
Artur Satayev70507ed2019-07-29 13:18:27 +01003170 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
Neil Fuller44e440c2015-04-20 14:39:00 +01003171 private static final HashMap<ClassLoader,HashMap<String,Parcelable.Creator<?>>>
3172 mCreators = new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003173
Narayan Kamathb34a4612014-01-23 14:17:11 +00003174 /** @hide for internal use only. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003175 static protected final Parcel obtain(int obj) {
Ashok Bhat8ab665d2014-01-22 16:00:20 +00003176 throw new UnsupportedOperationException();
3177 }
3178
3179 /** @hide */
3180 static protected final Parcel obtain(long obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003181 final Parcel[] pool = sHolderPool;
3182 synchronized (pool) {
3183 Parcel p;
3184 for (int i=0; i<POOL_SIZE; i++) {
3185 p = pool[i];
3186 if (p != null) {
3187 pool[i] = null;
3188 if (DEBUG_RECYCLE) {
3189 p.mStack = new RuntimeException();
3190 }
3191 p.init(obj);
3192 return p;
3193 }
3194 }
3195 }
3196 return new Parcel(obj);
3197 }
3198
Ashok Bhat8ab665d2014-01-22 16:00:20 +00003199 private Parcel(long nativePtr) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003200 if (DEBUG_RECYCLE) {
3201 mStack = new RuntimeException();
3202 }
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003203 //Log.i(TAG, "Initializing obj=0x" + Integer.toHexString(obj), mStack);
Jeff Sharkey047238c2012-03-07 16:51:38 -08003204 init(nativePtr);
3205 }
3206
Ashok Bhat8ab665d2014-01-22 16:00:20 +00003207 private void init(long nativePtr) {
Jeff Sharkey047238c2012-03-07 16:51:38 -08003208 if (nativePtr != 0) {
3209 mNativePtr = nativePtr;
3210 mOwnsNativeParcelObject = false;
3211 } else {
3212 mNativePtr = nativeCreate();
3213 mOwnsNativeParcelObject = true;
3214 }
3215 }
3216
3217 private void freeBuffer() {
3218 if (mOwnsNativeParcelObject) {
Adrian Roos04505652015-10-22 16:12:01 -07003219 updateNativeSize(nativeFreeBuffer(mNativePtr));
Jeff Sharkey047238c2012-03-07 16:51:38 -08003220 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -07003221 mReadWriteHelper = ReadWriteHelper.DEFAULT;
Jeff Sharkey047238c2012-03-07 16:51:38 -08003222 }
3223
3224 private void destroy() {
3225 if (mNativePtr != 0) {
3226 if (mOwnsNativeParcelObject) {
3227 nativeDestroy(mNativePtr);
Adrian Roos04505652015-10-22 16:12:01 -07003228 updateNativeSize(0);
Jeff Sharkey047238c2012-03-07 16:51:38 -08003229 }
3230 mNativePtr = 0;
3231 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -07003232 mReadWriteHelper = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003233 }
3234
3235 @Override
3236 protected void finalize() throws Throwable {
3237 if (DEBUG_RECYCLE) {
3238 if (mStack != null) {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003239 Log.w(TAG, "Client did not call Parcel.recycle()", mStack);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240 }
3241 }
3242 destroy();
3243 }
3244
Jake Whartonb1f474c2018-05-30 15:11:13 -04003245 /* package */ void readMapInternal(@NonNull Map outVal, int N,
3246 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247 while (N > 0) {
3248 Object key = readValue(loader);
3249 Object value = readValue(loader);
3250 outVal.put(key, value);
3251 N--;
3252 }
3253 }
3254
Jake Whartonb1f474c2018-05-30 15:11:13 -04003255 /* package */ void readArrayMapInternal(@NonNull ArrayMap outVal, int N,
3256 @Nullable ClassLoader loader) {
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003257 if (DEBUG_ARRAY_MAP) {
3258 RuntimeException here = new RuntimeException("here");
3259 here.fillInStackTrace();
3260 Log.d(TAG, "Reading " + N + " ArrayMap entries", here);
3261 }
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003262 int startPos;
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003263 while (N > 0) {
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003264 if (DEBUG_ARRAY_MAP) startPos = dataPosition();
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003265 String key = readString();
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003266 Object value = readValue(loader);
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003267 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read #" + (N-1) + " "
3268 + (dataPosition()-startPos) + " bytes: key=0x"
3269 + Integer.toHexString((key != null ? key.hashCode() : 0)) + " " + key);
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003270 outVal.append(key, value);
3271 N--;
3272 }
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003273 outVal.validate();
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003274 }
3275
Jake Whartonb1f474c2018-05-30 15:11:13 -04003276 /* package */ void readArrayMapSafelyInternal(@NonNull ArrayMap outVal, int N,
3277 @Nullable ClassLoader loader) {
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003278 if (DEBUG_ARRAY_MAP) {
3279 RuntimeException here = new RuntimeException("here");
3280 here.fillInStackTrace();
3281 Log.d(TAG, "Reading safely " + N + " ArrayMap entries", here);
3282 }
3283 while (N > 0) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003284 String key = readString();
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003285 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read safe #" + (N-1) + ": key=0x"
3286 + (key != null ? key.hashCode() : 0) + " " + key);
3287 Object value = readValue(loader);
3288 outVal.put(key, value);
3289 N--;
3290 }
3291 }
3292
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003293 /**
3294 * @hide For testing only.
3295 */
Andrei Onea24ec3212019-03-15 17:35:05 +00003296 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04003297 public void readArrayMap(@NonNull ArrayMap outVal, @Nullable ClassLoader loader) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003298 final int N = readInt();
3299 if (N < 0) {
3300 return;
3301 }
3302 readArrayMapInternal(outVal, N, loader);
3303 }
3304
Svet Ganovddb94882016-06-23 19:55:24 -07003305 /**
3306 * Reads an array set.
3307 *
3308 * @param loader The class loader to use.
3309 *
3310 * @hide
3311 */
Andrei Onea24ec3212019-03-15 17:35:05 +00003312 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04003313 public @Nullable ArraySet<? extends Object> readArraySet(@Nullable ClassLoader loader) {
Svet Ganovddb94882016-06-23 19:55:24 -07003314 final int size = readInt();
3315 if (size < 0) {
3316 return null;
3317 }
3318 ArraySet<Object> result = new ArraySet<>(size);
3319 for (int i = 0; i < size; i++) {
3320 Object value = readValue(loader);
3321 result.append(value);
3322 }
3323 return result;
3324 }
3325
Jake Whartonb1f474c2018-05-30 15:11:13 -04003326 private void readListInternal(@NonNull List outVal, int N,
3327 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003328 while (N > 0) {
3329 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003330 //Log.d(TAG, "Unmarshalling value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003331 outVal.add(value);
3332 N--;
3333 }
3334 }
3335
Jake Whartonb1f474c2018-05-30 15:11:13 -04003336 private void readArrayInternal(@NonNull Object[] outVal, int N,
3337 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003338 for (int i = 0; i < N; i++) {
3339 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003340 //Log.d(TAG, "Unmarshalling value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003341 outVal[i] = value;
3342 }
3343 }
3344
Jake Whartonb1f474c2018-05-30 15:11:13 -04003345 private void readSparseArrayInternal(@NonNull SparseArray outVal, int N,
3346 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003347 while (N > 0) {
3348 int key = readInt();
3349 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003350 //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003351 outVal.append(key, value);
3352 N--;
3353 }
3354 }
3355
3356
Jake Whartonb1f474c2018-05-30 15:11:13 -04003357 private void readSparseBooleanArrayInternal(@NonNull SparseBooleanArray outVal, int N) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003358 while (N > 0) {
3359 int key = readInt();
3360 boolean value = this.readByte() == 1;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003361 //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 outVal.append(key, value);
3363 N--;
3364 }
3365 }
Dan Sandler5ce04302015-04-09 23:50:15 -04003366
Jake Whartonb1f474c2018-05-30 15:11:13 -04003367 private void readSparseIntArrayInternal(@NonNull SparseIntArray outVal, int N) {
Adam Lesinski4e862812016-11-21 16:02:24 -08003368 while (N > 0) {
3369 int key = readInt();
3370 int value = readInt();
3371 outVal.append(key, value);
3372 N--;
3373 }
3374 }
3375
Dan Sandler5ce04302015-04-09 23:50:15 -04003376 /**
3377 * @hide For testing
3378 */
3379 public long getBlobAshmemSize() {
3380 return nativeGetBlobAshmemSize(mNativePtr);
3381 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003382}