blob: 9eb6445d5931ca7b765e13289d41b3d7c332f93a [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 {
Artur Satayev751e5512019-11-15 19:12:49 +0000376
377 @UnsupportedAppUsage
378 public ReadWriteHelper() {
379 }
380
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700381 public static final ReadWriteHelper DEFAULT = new ReadWriteHelper();
382
383 /**
384 * Called when writing a string to a parcel. Subclasses wanting to write a string
385 * must use {@link #writeStringNoHelper(String)} to avoid
386 * infinity recursive calls.
387 */
388 public void writeString(Parcel p, String s) {
389 nativeWriteString(p.mNativePtr, s);
390 }
391
392 /**
393 * Called when reading a string to a parcel. Subclasses wanting to read a string
394 * must use {@link #readStringNoHelper()} to avoid
395 * infinity recursive calls.
396 */
397 public String readString(Parcel p) {
398 return nativeReadString(p.mNativePtr);
399 }
400 }
401
402 private ReadWriteHelper mReadWriteHelper = ReadWriteHelper.DEFAULT;
403
404 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 * Retrieve a new Parcel object from the pool.
406 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400407 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 public static Parcel obtain() {
409 final Parcel[] pool = sOwnedPool;
410 synchronized (pool) {
411 Parcel p;
412 for (int i=0; i<POOL_SIZE; i++) {
413 p = pool[i];
414 if (p != null) {
415 pool[i] = null;
416 if (DEBUG_RECYCLE) {
417 p.mStack = new RuntimeException();
418 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700419 p.mReadWriteHelper = ReadWriteHelper.DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 return p;
421 }
422 }
423 }
424 return new Parcel(0);
425 }
426
427 /**
428 * Put a Parcel object back into the pool. You must not touch
429 * the object after this call.
430 */
431 public final void recycle() {
432 if (DEBUG_RECYCLE) mStack = null;
433 freeBuffer();
Jeff Sharkey047238c2012-03-07 16:51:38 -0800434
435 final Parcel[] pool;
436 if (mOwnsNativeParcelObject) {
437 pool = sOwnedPool;
438 } else {
439 mNativePtr = 0;
440 pool = sHolderPool;
441 }
442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 synchronized (pool) {
444 for (int i=0; i<POOL_SIZE; i++) {
445 if (pool[i] == null) {
446 pool[i] = this;
447 return;
448 }
449 }
450 }
451 }
452
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700453 /**
454 * Set a {@link ReadWriteHelper}, which can be used to avoid having duplicate strings, for
455 * example.
456 *
457 * @hide
458 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400459 public void setReadWriteHelper(@Nullable ReadWriteHelper helper) {
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700460 mReadWriteHelper = helper != null ? helper : ReadWriteHelper.DEFAULT;
461 }
462
463 /**
464 * @return whether this parcel has a {@link ReadWriteHelper}.
465 *
466 * @hide
467 */
468 public boolean hasReadWriteHelper() {
469 return (mReadWriteHelper != null) && (mReadWriteHelper != ReadWriteHelper.DEFAULT);
470 }
471
Dianne Hackbornfabb70b2014-11-11 12:22:36 -0800472 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000473 @UnsupportedAppUsage
Dianne Hackbornfabb70b2014-11-11 12:22:36 -0800474 public static native long getGlobalAllocSize();
475
476 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000477 @UnsupportedAppUsage
Dianne Hackbornfabb70b2014-11-11 12:22:36 -0800478 public static native long getGlobalAllocCount();
479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 /**
481 * Returns the total amount of data contained in the parcel.
482 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800483 public final int dataSize() {
484 return nativeDataSize(mNativePtr);
485 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486
487 /**
488 * Returns the amount of data remaining to be read from the
489 * parcel. That is, {@link #dataSize}-{@link #dataPosition}.
490 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800491 public final int dataAvail() {
492 return nativeDataAvail(mNativePtr);
493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494
495 /**
496 * Returns the current position in the parcel data. Never
497 * more than {@link #dataSize}.
498 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800499 public final int dataPosition() {
500 return nativeDataPosition(mNativePtr);
501 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502
503 /**
504 * Returns the total amount of space in the parcel. This is always
505 * >= {@link #dataSize}. The difference between it and dataSize() is the
506 * amount of room left until the parcel needs to re-allocate its
507 * data buffer.
508 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800509 public final int dataCapacity() {
510 return nativeDataCapacity(mNativePtr);
511 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512
513 /**
514 * Change the amount of data in the parcel. Can be either smaller or
515 * larger than the current size. If larger than the current capacity,
516 * more memory will be allocated.
517 *
518 * @param size The new number of bytes in the Parcel.
519 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800520 public final void setDataSize(int size) {
Michael Wachenschwanz138bebf2017-05-18 22:09:18 +0000521 updateNativeSize(nativeSetDataSize(mNativePtr, size));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800522 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523
524 /**
525 * Move the current read/write position in the parcel.
526 * @param pos New offset in the parcel; must be between 0 and
527 * {@link #dataSize}.
528 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800529 public final void setDataPosition(int pos) {
530 nativeSetDataPosition(mNativePtr, pos);
531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532
533 /**
534 * Change the capacity (current available space) of the parcel.
535 *
536 * @param size The new capacity of the parcel, in bytes. Can not be
537 * less than {@link #dataSize} -- that is, you can not drop existing data
538 * with this method.
539 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800540 public final void setDataCapacity(int size) {
541 nativeSetDataCapacity(mNativePtr, size);
542 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400544 /** @hide */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800545 public final boolean pushAllowFds(boolean allowFds) {
546 return nativePushAllowFds(mNativePtr, allowFds);
547 }
Dianne Hackbornc04db7e2011-10-03 21:09:35 -0700548
549 /** @hide */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800550 public final void restoreAllowFds(boolean lastValue) {
551 nativeRestoreAllowFds(mNativePtr, lastValue);
552 }
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 /**
555 * Returns the raw bytes of the parcel.
556 *
557 * <p class="note">The data you retrieve here <strong>must not</strong>
558 * be placed in any kind of persistent storage (on local disk, across
559 * a network, etc). For that, you should use standard serialization
560 * or another kind of general serialization mechanism. The Parcel
561 * marshalled representation is highly optimized for local IPC, and as
562 * such does not attempt to maintain compatibility with data created
563 * in different versions of the platform.
564 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800565 public final byte[] marshall() {
566 return nativeMarshall(mNativePtr);
567 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568
569 /**
570 * Set the bytes in data to be the raw bytes of this Parcel.
571 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400572 public final void unmarshall(@NonNull byte[] data, int offset, int length) {
Adrian Roos04505652015-10-22 16:12:01 -0700573 updateNativeSize(nativeUnmarshall(mNativePtr, data, offset, length));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800574 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575
Jeff Sharkey047238c2012-03-07 16:51:38 -0800576 public final void appendFrom(Parcel parcel, int offset, int length) {
Adrian Roos04505652015-10-22 16:12:01 -0700577 updateNativeSize(nativeAppendFrom(mNativePtr, parcel.mNativePtr, offset, length));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800578 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700580 /** @hide */
581 public final int compareData(Parcel other) {
582 return nativeCompareData(mNativePtr, other.mNativePtr);
583 }
584
Dianne Hackborn98305522017-05-05 17:53:53 -0700585 /** @hide */
586 public final void setClassCookie(Class clz, Object cookie) {
587 if (mClassCookies == null) {
588 mClassCookies = new ArrayMap<>();
589 }
590 mClassCookies.put(clz, cookie);
591 }
592
593 /** @hide */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400594 @Nullable
Dianne Hackborn98305522017-05-05 17:53:53 -0700595 public final Object getClassCookie(Class clz) {
596 return mClassCookies != null ? mClassCookies.get(clz) : null;
597 }
598
599 /** @hide */
600 public final void adoptClassCookies(Parcel from) {
601 mClassCookies = from.mClassCookies;
602 }
603
Adrian Roosfb921842017-10-26 14:49:56 +0200604 /** @hide */
605 public Map<Class, Object> copyClassCookies() {
606 return new ArrayMap<>(mClassCookies);
607 }
608
609 /** @hide */
610 public void putClassCookies(Map<Class, Object> cookies) {
611 if (cookies == null) {
612 return;
613 }
614 if (mClassCookies == null) {
615 mClassCookies = new ArrayMap<>();
616 }
617 mClassCookies.putAll(cookies);
618 }
619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 /**
621 * Report whether the parcel contains any marshalled file descriptors.
622 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800623 public final boolean hasFileDescriptors() {
624 return nativeHasFileDescriptors(mNativePtr);
625 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626
627 /**
628 * Store or read an IBinder interface token in the parcel at the current
629 * {@link #dataPosition}. This is used to validate that the marshalled
630 * transaction is intended for the target interface.
631 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800632 public final void writeInterfaceToken(String interfaceName) {
633 nativeWriteInterfaceToken(mNativePtr, interfaceName);
634 }
635
636 public final void enforceInterface(String interfaceName) {
637 nativeEnforceInterface(mNativePtr, interfaceName);
638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639
640 /**
Olivier Gaillardbab444a2019-01-30 17:11:40 +0000641 * Writes the work source uid to the request headers.
642 *
643 * <p>It requires the headers to have been written/read already to replace the work source.
644 *
645 * @return true if the request headers have been updated.
646 *
647 * @hide
648 */
649 public boolean replaceCallingWorkSourceUid(int workSourceUid) {
650 return nativeReplaceCallingWorkSourceUid(mNativePtr, workSourceUid);
651 }
652
653 /**
654 * Reads the work source uid from the request headers.
655 *
656 * <p>Unlike other read methods, this method does not read the parcel at the current
657 * {@link #dataPosition}. It will set the {@link #dataPosition} before the read and restore the
658 * position after reading the request header.
659 *
660 * @return the work source uid or {@link Binder#UNSET_WORKSOURCE} if headers have not been
661 * written/parsed yet.
662 *
663 * @hide
664 */
665 public int readCallingWorkSourceUid() {
666 return nativeReadCallingWorkSourceUid(mNativePtr);
667 }
668
669 /**
Elliott Hughesa28b83e2011-02-28 14:26:13 -0800670 * Write a byte array into the parcel at the current {@link #dataPosition},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 * growing {@link #dataCapacity} if needed.
672 * @param b Bytes to place into the parcel.
673 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400674 public final void writeByteArray(@Nullable byte[] b) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 writeByteArray(b, 0, (b != null) ? b.length : 0);
676 }
677
678 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900679 * Write a byte array into the parcel at the current {@link #dataPosition},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 * growing {@link #dataCapacity} if needed.
681 * @param b Bytes to place into the parcel.
682 * @param offset Index of first byte to be written.
683 * @param len Number of bytes to write.
684 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400685 public final void writeByteArray(@Nullable byte[] b, int offset, int len) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 if (b == null) {
687 writeInt(-1);
688 return;
689 }
Pete Gillin60f55a252018-05-10 15:40:32 +0100690 ArrayUtils.throwsIfOutOfBounds(b.length, offset, len);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800691 nativeWriteByteArray(mNativePtr, b, offset, len);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 }
693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 /**
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700695 * Write a blob of data into the parcel at the current {@link #dataPosition},
696 * growing {@link #dataCapacity} if needed.
697 * @param b Bytes to place into the parcel.
698 * {@hide}
Sandeep Siddhartha39c12fa2014-07-25 18:37:29 -0700699 * {@SystemApi}
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700700 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000701 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -0400702 public final void writeBlob(@Nullable byte[] b) {
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500703 writeBlob(b, 0, (b != null) ? b.length : 0);
704 }
705
706 /**
707 * Write a blob of data into the parcel at the current {@link #dataPosition},
708 * growing {@link #dataCapacity} if needed.
709 * @param b Bytes to place into the parcel.
710 * @param offset Index of first byte to be written.
711 * @param len Number of bytes to write.
712 * {@hide}
713 * {@SystemApi}
714 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400715 public final void writeBlob(@Nullable byte[] b, int offset, int len) {
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500716 if (b == null) {
717 writeInt(-1);
718 return;
719 }
Pete Gillin60f55a252018-05-10 15:40:32 +0100720 ArrayUtils.throwsIfOutOfBounds(b.length, offset, len);
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500721 nativeWriteBlob(mNativePtr, b, offset, len);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700722 }
723
724 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 * Write an integer value into the parcel at the current dataPosition(),
726 * growing dataCapacity() if needed.
727 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800728 public final void writeInt(int val) {
729 nativeWriteInt(mNativePtr, val);
730 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731
732 /**
733 * Write a long integer value into the parcel at the current dataPosition(),
734 * growing dataCapacity() if needed.
735 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800736 public final void writeLong(long val) {
737 nativeWriteLong(mNativePtr, val);
738 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739
740 /**
741 * Write a floating point value into the parcel at the current
742 * dataPosition(), growing dataCapacity() if needed.
743 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800744 public final void writeFloat(float val) {
745 nativeWriteFloat(mNativePtr, val);
746 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747
748 /**
749 * Write a double precision floating point value into the parcel at the
750 * current dataPosition(), growing dataCapacity() if needed.
751 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800752 public final void writeDouble(double val) {
753 nativeWriteDouble(mNativePtr, val);
754 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755
756 /**
757 * Write a string value into the parcel at the current dataPosition(),
758 * growing dataCapacity() if needed.
759 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400760 public final void writeString(@Nullable String val) {
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700761 mReadWriteHelper.writeString(this, val);
762 }
763
764 /**
765 * Write a string without going though a {@link ReadWriteHelper}. Subclasses of
766 * {@link ReadWriteHelper} must use this method instead of {@link #writeString} to avoid
767 * infinity recursive calls.
768 *
769 * @hide
770 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400771 public void writeStringNoHelper(@Nullable String val) {
Jeff Sharkey047238c2012-03-07 16:51:38 -0800772 nativeWriteString(mNativePtr, val);
773 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774
Jake Wharton2ffa4312018-04-23 17:47:14 -0400775 /**
776 * Write a boolean value into the parcel at the current dataPosition(),
777 * growing dataCapacity() if needed.
778 *
779 * <p>Note: This method currently delegates to writeInt with a value of 1 or 0
780 * for true or false, respectively, but may change in the future.
781 */
Eugene Susla36e866b2017-02-23 18:24:39 -0800782 public final void writeBoolean(boolean val) {
783 writeInt(val ? 1 : 0);
784 }
785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +0000787 * Write a CharSequence value into the parcel at the current dataPosition(),
788 * growing dataCapacity() if needed.
789 * @hide
790 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000791 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -0400792 public final void writeCharSequence(@Nullable CharSequence val) {
Bjorn Bringert08bbffb2010-02-25 11:16:22 +0000793 TextUtils.writeToParcel(val, this, 0);
794 }
795
796 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 * Write an object into the parcel at the current dataPosition(),
798 * growing dataCapacity() if needed.
799 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800800 public final void writeStrongBinder(IBinder val) {
801 nativeWriteStrongBinder(mNativePtr, val);
802 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803
804 /**
805 * Write an object into the parcel at the current dataPosition(),
806 * growing dataCapacity() if needed.
807 */
808 public final void writeStrongInterface(IInterface val) {
809 writeStrongBinder(val == null ? null : val.asBinder());
810 }
811
812 /**
813 * Write a FileDescriptor into the parcel at the current dataPosition(),
814 * growing dataCapacity() if needed.
Dan Egnorb3e4ef32010-07-20 09:03:35 -0700815 *
816 * <p class="caution">The file descriptor will not be closed, which may
817 * result in file descriptor leaks when objects are returned from Binder
818 * calls. Use {@link ParcelFileDescriptor#writeToParcel} instead, which
819 * accepts contextual flags and will close the original file descriptor
820 * if {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE} is set.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400822 public final void writeFileDescriptor(@NonNull FileDescriptor val) {
Adrian Roos04505652015-10-22 16:12:01 -0700823 updateNativeSize(nativeWriteFileDescriptor(mNativePtr, val));
824 }
825
826 private void updateNativeSize(long newNativeSize) {
827 if (mOwnsNativeParcelObject) {
828 if (newNativeSize > Integer.MAX_VALUE) {
829 newNativeSize = Integer.MAX_VALUE;
830 }
831 if (newNativeSize != mNativeSize) {
832 int delta = (int) (newNativeSize - mNativeSize);
833 if (delta > 0) {
834 VMRuntime.getRuntime().registerNativeAllocation(delta);
835 } else {
836 VMRuntime.getRuntime().registerNativeFree(-delta);
837 }
838 mNativeSize = newNativeSize;
839 }
840 }
Jeff Sharkey047238c2012-03-07 16:51:38 -0800841 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842
843 /**
Casey Dahlin2f974b22015-11-05 12:19:13 -0800844 * {@hide}
845 * This will be the new name for writeFileDescriptor, for consistency.
846 **/
Jake Whartonb1f474c2018-05-30 15:11:13 -0400847 public final void writeRawFileDescriptor(@NonNull FileDescriptor val) {
Casey Dahlin2f974b22015-11-05 12:19:13 -0800848 nativeWriteFileDescriptor(mNativePtr, val);
849 }
850
851 /**
852 * {@hide}
853 * Write an array of FileDescriptor objects into the Parcel.
854 *
855 * @param value The array of objects to be written.
856 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400857 public final void writeRawFileDescriptorArray(@Nullable FileDescriptor[] value) {
Casey Dahlin2f974b22015-11-05 12:19:13 -0800858 if (value != null) {
859 int N = value.length;
860 writeInt(N);
861 for (int i=0; i<N; i++) {
862 writeRawFileDescriptor(value[i]);
863 }
864 } else {
865 writeInt(-1);
866 }
867 }
868
869 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900870 * Write a byte value into the parcel at the current dataPosition(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 * growing dataCapacity() if needed.
Jake Wharton2ffa4312018-04-23 17:47:14 -0400872 *
873 * <p>Note: This method currently delegates to writeInt but may change in
874 * the future.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 */
876 public final void writeByte(byte val) {
877 writeInt(val);
878 }
879
880 /**
881 * Please use {@link #writeBundle} instead. Flattens a Map into the parcel
882 * at the current dataPosition(),
883 * growing dataCapacity() if needed. The Map keys must be String objects.
884 * The Map values are written using {@link #writeValue} and must follow
885 * the specification there.
Samuel Tana8036662015-11-23 14:36:00 -0800886 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 * <p>It is strongly recommended to use {@link #writeBundle} instead of
888 * this method, since the Bundle class provides a type-safe API that
889 * allows you to avoid mysterious type errors at the point of marshalling.
890 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400891 public final void writeMap(@Nullable Map val) {
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700892 writeMapInternal((Map<String, Object>) val);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 }
894
895 /**
896 * Flatten a Map into the parcel at the current dataPosition(),
897 * growing dataCapacity() if needed. The Map keys must be String objects.
898 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400899 /* package */ void writeMapInternal(@Nullable Map<String,Object> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 if (val == null) {
901 writeInt(-1);
902 return;
903 }
904 Set<Map.Entry<String,Object>> entries = val.entrySet();
Michael Wachenschwanz80b9d692018-08-24 21:50:35 -0700905 int size = entries.size();
906 writeInt(size);
907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 for (Map.Entry<String,Object> e : entries) {
909 writeValue(e.getKey());
910 writeValue(e.getValue());
Michael Wachenschwanz80b9d692018-08-24 21:50:35 -0700911 size--;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 }
Michael Wachenschwanz80b9d692018-08-24 21:50:35 -0700913
914 if (size != 0) {
915 throw new BadParcelableException("Map size does not match number of entries!");
916 }
917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 }
919
920 /**
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700921 * Flatten an ArrayMap into the parcel at the current dataPosition(),
922 * growing dataCapacity() if needed. The Map keys must be String objects.
923 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400924 /* package */ void writeArrayMapInternal(@Nullable ArrayMap<String, Object> val) {
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700925 if (val == null) {
926 writeInt(-1);
927 return;
928 }
Samuel Tan3cefe6a2015-12-14 13:29:17 -0800929 // Keep the format of this Parcel in sync with writeToParcelInner() in
930 // frameworks/native/libs/binder/PersistableBundle.cpp.
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700931 final int N = val.size();
932 writeInt(N);
Dianne Hackborne784d1e2013-09-20 18:13:52 -0700933 if (DEBUG_ARRAY_MAP) {
934 RuntimeException here = new RuntimeException("here");
935 here.fillInStackTrace();
936 Log.d(TAG, "Writing " + N + " ArrayMap entries", here);
937 }
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700938 int startPos;
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700939 for (int i=0; i<N; i++) {
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700940 if (DEBUG_ARRAY_MAP) startPos = dataPosition();
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700941 writeString(val.keyAt(i));
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700942 writeValue(val.valueAt(i));
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700943 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Write #" + i + " "
944 + (dataPosition()-startPos) + " bytes: key=0x"
945 + Integer.toHexString(val.keyAt(i) != null ? val.keyAt(i).hashCode() : 0)
946 + " " + val.keyAt(i));
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700947 }
948 }
949
950 /**
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700951 * @hide For testing only.
952 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000953 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -0400954 public void writeArrayMap(@Nullable ArrayMap<String, Object> val) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700955 writeArrayMapInternal(val);
956 }
957
958 /**
Svet Ganov8455ba22019-01-02 13:05:56 -0800959 * Flatten an {@link ArrayMap} with string keys containing a particular object
960 * type into the parcel at the current dataPosition() and growing dataCapacity()
961 * if needed. The type of the objects in the array must be one that implements
962 * Parcelable. Only the raw data of the objects is written and not their type,
963 * so you must use the corresponding {@link #createTypedArrayMap(Parcelable.Creator)}
964 *
965 * @param val The map of objects to be written.
966 * @param parcelableFlags The parcelable flags to use.
967 *
968 * @see #createTypedArrayMap(Parcelable.Creator)
969 * @see Parcelable
970 */
971 public <T extends Parcelable> void writeTypedArrayMap(@Nullable ArrayMap<String, T> val,
972 int parcelableFlags) {
973 if (val == null) {
974 writeInt(-1);
975 return;
976 }
977 final int count = val.size();
978 writeInt(count);
979 for (int i = 0; i < count; i++) {
980 writeString(val.keyAt(i));
981 writeTypedObject(val.valueAt(i), parcelableFlags);
982 }
983 }
984
985 /**
Svet Ganovddb94882016-06-23 19:55:24 -0700986 * Write an array set to the parcel.
987 *
988 * @param val The array set to write.
989 *
990 * @hide
991 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000992 @UnsupportedAppUsage
Svet Ganovddb94882016-06-23 19:55:24 -0700993 public void writeArraySet(@Nullable ArraySet<? extends Object> val) {
994 final int size = (val != null) ? val.size() : -1;
995 writeInt(size);
996 for (int i = 0; i < size; i++) {
997 writeValue(val.valueAt(i));
998 }
999 }
1000
1001 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 * Flatten a Bundle into the parcel at the current dataPosition(),
1003 * growing dataCapacity() if needed.
1004 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001005 public final void writeBundle(@Nullable Bundle val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 if (val == null) {
1007 writeInt(-1);
1008 return;
1009 }
1010
Dianne Hackborn6aff9052009-05-22 13:20:23 -07001011 val.writeToParcel(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 }
1013
1014 /**
Craig Mautner719e6b12014-04-04 20:29:41 -07001015 * Flatten a PersistableBundle into the parcel at the current dataPosition(),
1016 * growing dataCapacity() if needed.
1017 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001018 public final void writePersistableBundle(@Nullable PersistableBundle val) {
Craig Mautner719e6b12014-04-04 20:29:41 -07001019 if (val == null) {
1020 writeInt(-1);
1021 return;
1022 }
1023
1024 val.writeToParcel(this, 0);
1025 }
1026
1027 /**
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001028 * Flatten a Size into the parcel at the current dataPosition(),
1029 * growing dataCapacity() if needed.
1030 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001031 public final void writeSize(@NonNull Size val) {
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001032 writeInt(val.getWidth());
1033 writeInt(val.getHeight());
1034 }
1035
1036 /**
1037 * Flatten a SizeF into the parcel at the current dataPosition(),
1038 * growing dataCapacity() if needed.
1039 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001040 public final void writeSizeF(@NonNull SizeF val) {
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001041 writeFloat(val.getWidth());
1042 writeFloat(val.getHeight());
1043 }
1044
1045 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 * Flatten a List into the parcel at the current dataPosition(), growing
1047 * dataCapacity() if needed. The List values are written using
1048 * {@link #writeValue} and must follow the specification there.
1049 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001050 public final void writeList(@Nullable List val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 if (val == null) {
1052 writeInt(-1);
1053 return;
1054 }
1055 int N = val.size();
1056 int i=0;
1057 writeInt(N);
1058 while (i < N) {
1059 writeValue(val.get(i));
1060 i++;
1061 }
1062 }
1063
1064 /**
1065 * Flatten an Object array into the parcel at the current dataPosition(),
1066 * growing dataCapacity() if needed. The array values are written using
1067 * {@link #writeValue} and must follow the specification there.
1068 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001069 public final void writeArray(@Nullable Object[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 if (val == null) {
1071 writeInt(-1);
1072 return;
1073 }
1074 int N = val.length;
1075 int i=0;
1076 writeInt(N);
1077 while (i < N) {
1078 writeValue(val[i]);
1079 i++;
1080 }
1081 }
1082
1083 /**
1084 * Flatten a generic SparseArray into the parcel at the current
1085 * dataPosition(), growing dataCapacity() if needed. The SparseArray
1086 * values are written using {@link #writeValue} and must follow the
1087 * specification there.
1088 */
Svet Ganov8455ba22019-01-02 13:05:56 -08001089 public final <T> void writeSparseArray(@Nullable SparseArray<T> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 if (val == null) {
1091 writeInt(-1);
1092 return;
1093 }
1094 int N = val.size();
1095 writeInt(N);
1096 int i=0;
1097 while (i < N) {
1098 writeInt(val.keyAt(i));
1099 writeValue(val.valueAt(i));
1100 i++;
1101 }
1102 }
1103
Jake Whartonb1f474c2018-05-30 15:11:13 -04001104 public final void writeSparseBooleanArray(@Nullable SparseBooleanArray val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 if (val == null) {
1106 writeInt(-1);
1107 return;
1108 }
1109 int N = val.size();
1110 writeInt(N);
1111 int i=0;
1112 while (i < N) {
1113 writeInt(val.keyAt(i));
1114 writeByte((byte)(val.valueAt(i) ? 1 : 0));
1115 i++;
1116 }
1117 }
1118
Adam Lesinski205656d2017-03-23 13:38:26 -07001119 /**
1120 * @hide
1121 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001122 public final void writeSparseIntArray(@Nullable SparseIntArray val) {
Adam Lesinski4e862812016-11-21 16:02:24 -08001123 if (val == null) {
1124 writeInt(-1);
1125 return;
1126 }
1127 int N = val.size();
1128 writeInt(N);
1129 int i=0;
1130 while (i < N) {
1131 writeInt(val.keyAt(i));
1132 writeInt(val.valueAt(i));
1133 i++;
1134 }
1135 }
1136
Jake Whartonb1f474c2018-05-30 15:11:13 -04001137 public final void writeBooleanArray(@Nullable boolean[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 if (val != null) {
1139 int N = val.length;
1140 writeInt(N);
1141 for (int i=0; i<N; i++) {
1142 writeInt(val[i] ? 1 : 0);
1143 }
1144 } else {
1145 writeInt(-1);
1146 }
1147 }
1148
Jake Whartonb1f474c2018-05-30 15:11:13 -04001149 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 public final boolean[] createBooleanArray() {
1151 int N = readInt();
1152 // >>2 as a fast divide-by-4 works in the create*Array() functions
1153 // because dataAvail() will never return a negative number. 4 is
1154 // the size of a stored boolean in the stream.
1155 if (N >= 0 && N <= (dataAvail() >> 2)) {
1156 boolean[] val = new boolean[N];
1157 for (int i=0; i<N; i++) {
1158 val[i] = readInt() != 0;
1159 }
1160 return val;
1161 } else {
1162 return null;
1163 }
1164 }
1165
Jake Whartonb1f474c2018-05-30 15:11:13 -04001166 public final void readBooleanArray(@NonNull boolean[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 int N = readInt();
1168 if (N == val.length) {
1169 for (int i=0; i<N; i++) {
1170 val[i] = readInt() != 0;
1171 }
1172 } else {
1173 throw new RuntimeException("bad array lengths");
1174 }
1175 }
1176
Jake Whartonb1f474c2018-05-30 15:11:13 -04001177 public final void writeCharArray(@Nullable char[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 if (val != null) {
1179 int N = val.length;
1180 writeInt(N);
1181 for (int i=0; i<N; i++) {
1182 writeInt((int)val[i]);
1183 }
1184 } else {
1185 writeInt(-1);
1186 }
1187 }
1188
Jake Whartonb1f474c2018-05-30 15:11:13 -04001189 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 public final char[] createCharArray() {
1191 int N = readInt();
1192 if (N >= 0 && N <= (dataAvail() >> 2)) {
1193 char[] val = new char[N];
1194 for (int i=0; i<N; i++) {
1195 val[i] = (char)readInt();
1196 }
1197 return val;
1198 } else {
1199 return null;
1200 }
1201 }
1202
Jake Whartonb1f474c2018-05-30 15:11:13 -04001203 public final void readCharArray(@NonNull char[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 int N = readInt();
1205 if (N == val.length) {
1206 for (int i=0; i<N; i++) {
1207 val[i] = (char)readInt();
1208 }
1209 } else {
1210 throw new RuntimeException("bad array lengths");
1211 }
1212 }
1213
Jake Whartonb1f474c2018-05-30 15:11:13 -04001214 public final void writeIntArray(@Nullable int[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 if (val != null) {
1216 int N = val.length;
1217 writeInt(N);
1218 for (int i=0; i<N; i++) {
1219 writeInt(val[i]);
1220 }
1221 } else {
1222 writeInt(-1);
1223 }
1224 }
1225
Jake Whartonb1f474c2018-05-30 15:11:13 -04001226 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 public final int[] createIntArray() {
1228 int N = readInt();
1229 if (N >= 0 && N <= (dataAvail() >> 2)) {
1230 int[] val = new int[N];
1231 for (int i=0; i<N; i++) {
1232 val[i] = readInt();
1233 }
1234 return val;
1235 } else {
1236 return null;
1237 }
1238 }
1239
Jake Whartonb1f474c2018-05-30 15:11:13 -04001240 public final void readIntArray(@NonNull int[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 int N = readInt();
1242 if (N == val.length) {
1243 for (int i=0; i<N; i++) {
1244 val[i] = readInt();
1245 }
1246 } else {
1247 throw new RuntimeException("bad array lengths");
1248 }
1249 }
1250
Jake Whartonb1f474c2018-05-30 15:11:13 -04001251 public final void writeLongArray(@Nullable long[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 if (val != null) {
1253 int N = val.length;
1254 writeInt(N);
1255 for (int i=0; i<N; i++) {
1256 writeLong(val[i]);
1257 }
1258 } else {
1259 writeInt(-1);
1260 }
1261 }
1262
Jake Whartonb1f474c2018-05-30 15:11:13 -04001263 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 public final long[] createLongArray() {
1265 int N = readInt();
1266 // >>3 because stored longs are 64 bits
1267 if (N >= 0 && N <= (dataAvail() >> 3)) {
1268 long[] val = new long[N];
1269 for (int i=0; i<N; i++) {
1270 val[i] = readLong();
1271 }
1272 return val;
1273 } else {
1274 return null;
1275 }
1276 }
1277
Jake Whartonb1f474c2018-05-30 15:11:13 -04001278 public final void readLongArray(@NonNull long[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 int N = readInt();
1280 if (N == val.length) {
1281 for (int i=0; i<N; i++) {
1282 val[i] = readLong();
1283 }
1284 } else {
1285 throw new RuntimeException("bad array lengths");
1286 }
1287 }
1288
Jake Whartonb1f474c2018-05-30 15:11:13 -04001289 public final void writeFloatArray(@Nullable float[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 if (val != null) {
1291 int N = val.length;
1292 writeInt(N);
1293 for (int i=0; i<N; i++) {
1294 writeFloat(val[i]);
1295 }
1296 } else {
1297 writeInt(-1);
1298 }
1299 }
1300
Jake Whartonb1f474c2018-05-30 15:11:13 -04001301 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 public final float[] createFloatArray() {
1303 int N = readInt();
1304 // >>2 because stored floats are 4 bytes
1305 if (N >= 0 && N <= (dataAvail() >> 2)) {
1306 float[] val = new float[N];
1307 for (int i=0; i<N; i++) {
1308 val[i] = readFloat();
1309 }
1310 return val;
1311 } else {
1312 return null;
1313 }
1314 }
1315
Jake Whartonb1f474c2018-05-30 15:11:13 -04001316 public final void readFloatArray(@NonNull float[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 int N = readInt();
1318 if (N == val.length) {
1319 for (int i=0; i<N; i++) {
1320 val[i] = readFloat();
1321 }
1322 } else {
1323 throw new RuntimeException("bad array lengths");
1324 }
1325 }
1326
Jake Whartonb1f474c2018-05-30 15:11:13 -04001327 public final void writeDoubleArray(@Nullable double[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 if (val != null) {
1329 int N = val.length;
1330 writeInt(N);
1331 for (int i=0; i<N; i++) {
1332 writeDouble(val[i]);
1333 }
1334 } else {
1335 writeInt(-1);
1336 }
1337 }
1338
Jake Whartonb1f474c2018-05-30 15:11:13 -04001339 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 public final double[] createDoubleArray() {
1341 int N = readInt();
1342 // >>3 because stored doubles are 8 bytes
1343 if (N >= 0 && N <= (dataAvail() >> 3)) {
1344 double[] val = new double[N];
1345 for (int i=0; i<N; i++) {
1346 val[i] = readDouble();
1347 }
1348 return val;
1349 } else {
1350 return null;
1351 }
1352 }
1353
Jake Whartonb1f474c2018-05-30 15:11:13 -04001354 public final void readDoubleArray(@NonNull double[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 int N = readInt();
1356 if (N == val.length) {
1357 for (int i=0; i<N; i++) {
1358 val[i] = readDouble();
1359 }
1360 } else {
1361 throw new RuntimeException("bad array lengths");
1362 }
1363 }
1364
Jake Whartonb1f474c2018-05-30 15:11:13 -04001365 public final void writeStringArray(@Nullable String[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 if (val != null) {
1367 int N = val.length;
1368 writeInt(N);
1369 for (int i=0; i<N; i++) {
1370 writeString(val[i]);
1371 }
1372 } else {
1373 writeInt(-1);
1374 }
1375 }
1376
Jake Whartonb1f474c2018-05-30 15:11:13 -04001377 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 public final String[] createStringArray() {
1379 int N = readInt();
1380 if (N >= 0) {
1381 String[] val = new String[N];
1382 for (int i=0; i<N; i++) {
1383 val[i] = readString();
1384 }
1385 return val;
1386 } else {
1387 return null;
1388 }
1389 }
1390
Jake Whartonb1f474c2018-05-30 15:11:13 -04001391 public final void readStringArray(@NonNull String[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 int N = readInt();
1393 if (N == val.length) {
1394 for (int i=0; i<N; i++) {
1395 val[i] = readString();
1396 }
1397 } else {
1398 throw new RuntimeException("bad array lengths");
1399 }
1400 }
1401
Jake Whartonb1f474c2018-05-30 15:11:13 -04001402 public final void writeBinderArray(@Nullable IBinder[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 if (val != null) {
1404 int N = val.length;
1405 writeInt(N);
1406 for (int i=0; i<N; i++) {
1407 writeStrongBinder(val[i]);
1408 }
1409 } else {
1410 writeInt(-1);
1411 }
1412 }
1413
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001414 /**
1415 * @hide
1416 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001417 public final void writeCharSequenceArray(@Nullable CharSequence[] val) {
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001418 if (val != null) {
1419 int N = val.length;
1420 writeInt(N);
1421 for (int i=0; i<N; i++) {
1422 writeCharSequence(val[i]);
1423 }
1424 } else {
1425 writeInt(-1);
1426 }
1427 }
1428
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001429 /**
1430 * @hide
1431 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001432 public final void writeCharSequenceList(@Nullable ArrayList<CharSequence> val) {
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001433 if (val != null) {
1434 int N = val.size();
1435 writeInt(N);
1436 for (int i=0; i<N; i++) {
1437 writeCharSequence(val.get(i));
1438 }
1439 } else {
1440 writeInt(-1);
1441 }
1442 }
1443
Jake Whartonb1f474c2018-05-30 15:11:13 -04001444 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 public final IBinder[] createBinderArray() {
1446 int N = readInt();
1447 if (N >= 0) {
1448 IBinder[] val = new IBinder[N];
1449 for (int i=0; i<N; i++) {
1450 val[i] = readStrongBinder();
1451 }
1452 return val;
1453 } else {
1454 return null;
1455 }
1456 }
1457
Jake Whartonb1f474c2018-05-30 15:11:13 -04001458 public final void readBinderArray(@NonNull IBinder[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 int N = readInt();
1460 if (N == val.length) {
1461 for (int i=0; i<N; i++) {
1462 val[i] = readStrongBinder();
1463 }
1464 } else {
1465 throw new RuntimeException("bad array lengths");
1466 }
1467 }
1468
1469 /**
1470 * Flatten a List containing a particular object type into the parcel, at
1471 * the current dataPosition() and growing dataCapacity() if needed. The
1472 * type of the objects in the list must be one that implements Parcelable.
1473 * Unlike the generic writeList() method, however, only the raw data of the
1474 * objects is written and not their type, so you must use the corresponding
1475 * readTypedList() to unmarshall them.
1476 *
1477 * @param val The list of objects to be written.
1478 *
1479 * @see #createTypedArrayList
1480 * @see #readTypedList
1481 * @see Parcelable
1482 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001483 public final <T extends Parcelable> void writeTypedList(@Nullable List<T> val) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07001484 writeTypedList(val, 0);
1485 }
1486
1487 /**
Svet Ganov8455ba22019-01-02 13:05:56 -08001488 * Flatten a {@link SparseArray} containing a particular object type into the parcel
1489 * at the current dataPosition() and growing dataCapacity() if needed. The
1490 * type of the objects in the array must be one that implements Parcelable.
1491 * Unlike the generic {@link #writeSparseArray(SparseArray)} method, however, only
1492 * the raw data of the objects is written and not their type, so you must use the
1493 * corresponding {@link #createTypedSparseArray(Parcelable.Creator)}.
1494 *
1495 * @param val The list of objects to be written.
1496 * @param parcelableFlags The parcelable flags to use.
1497 *
1498 * @see #createTypedSparseArray(Parcelable.Creator)
1499 * @see Parcelable
1500 */
1501 public final <T extends Parcelable> void writeTypedSparseArray(@Nullable SparseArray<T> val,
1502 int parcelableFlags) {
1503 if (val == null) {
1504 writeInt(-1);
1505 return;
1506 }
1507 final int count = val.size();
1508 writeInt(count);
1509 for (int i = 0; i < count; i++) {
1510 writeInt(val.keyAt(i));
1511 writeTypedObject(val.valueAt(i), parcelableFlags);
1512 }
1513 }
1514
1515 /**
Sunny Goyal0e60f222017-09-21 21:39:20 -07001516 * @hide
1517 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001518 public <T extends Parcelable> void writeTypedList(@Nullable List<T> val, int parcelableFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 if (val == null) {
1520 writeInt(-1);
1521 return;
1522 }
1523 int N = val.size();
1524 int i=0;
1525 writeInt(N);
1526 while (i < N) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07001527 writeTypedObject(val.get(i), parcelableFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 i++;
1529 }
1530 }
1531
1532 /**
1533 * Flatten a List containing String objects into the parcel, at
1534 * the current dataPosition() and growing dataCapacity() if needed. They
1535 * can later be retrieved with {@link #createStringArrayList} or
1536 * {@link #readStringList}.
1537 *
1538 * @param val The list of strings to be written.
1539 *
1540 * @see #createStringArrayList
1541 * @see #readStringList
1542 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001543 public final void writeStringList(@Nullable List<String> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 if (val == null) {
1545 writeInt(-1);
1546 return;
1547 }
1548 int N = val.size();
1549 int i=0;
1550 writeInt(N);
1551 while (i < N) {
1552 writeString(val.get(i));
1553 i++;
1554 }
1555 }
1556
1557 /**
1558 * Flatten a List containing IBinder objects into the parcel, at
1559 * the current dataPosition() and growing dataCapacity() if needed. They
1560 * can later be retrieved with {@link #createBinderArrayList} or
1561 * {@link #readBinderList}.
1562 *
1563 * @param val The list of strings to be written.
1564 *
1565 * @see #createBinderArrayList
1566 * @see #readBinderList
1567 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001568 public final void writeBinderList(@Nullable List<IBinder> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 if (val == null) {
1570 writeInt(-1);
1571 return;
1572 }
1573 int N = val.size();
1574 int i=0;
1575 writeInt(N);
1576 while (i < N) {
1577 writeStrongBinder(val.get(i));
1578 i++;
1579 }
1580 }
1581
1582 /**
Narayan Kamathbea48712016-12-01 15:38:28 +00001583 * Flatten a {@code List} containing arbitrary {@code Parcelable} objects into this parcel
1584 * at the current position. They can later be retrieved using
1585 * {@link #readParcelableList(List, ClassLoader)} if required.
1586 *
1587 * @see #readParcelableList(List, ClassLoader)
Narayan Kamathbea48712016-12-01 15:38:28 +00001588 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001589 public final <T extends Parcelable> void writeParcelableList(@Nullable List<T> val, int flags) {
Narayan Kamathbea48712016-12-01 15:38:28 +00001590 if (val == null) {
1591 writeInt(-1);
1592 return;
1593 }
1594
1595 int N = val.size();
1596 int i=0;
1597 writeInt(N);
1598 while (i < N) {
1599 writeParcelable(val.get(i), flags);
1600 i++;
1601 }
1602 }
1603
1604 /**
Svet Ganov0f4928f2017-02-02 20:02:51 -08001605 * Flatten a homogeneous array containing a particular object type into
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 * the parcel, at
1607 * the current dataPosition() and growing dataCapacity() if needed. The
1608 * type of the objects in the array must be one that implements Parcelable.
1609 * Unlike the {@link #writeParcelableArray} method, however, only the
1610 * raw data of the objects is written and not their type, so you must use
1611 * {@link #readTypedArray} with the correct corresponding
1612 * {@link Parcelable.Creator} implementation to unmarshall them.
1613 *
1614 * @param val The array of objects to be written.
1615 * @param parcelableFlags Contextual flags as per
1616 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1617 *
1618 * @see #readTypedArray
1619 * @see #writeParcelableArray
1620 * @see Parcelable.Creator
1621 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001622 public final <T extends Parcelable> void writeTypedArray(@Nullable T[] val,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 int parcelableFlags) {
1624 if (val != null) {
1625 int N = val.length;
1626 writeInt(N);
Svet Ganov0f4928f2017-02-02 20:02:51 -08001627 for (int i = 0; i < N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07001628 writeTypedObject(val[i], parcelableFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 }
1630 } else {
1631 writeInt(-1);
1632 }
1633 }
1634
1635 /**
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07001636 * Flatten the Parcelable object into the parcel.
1637 *
1638 * @param val The Parcelable object to be written.
1639 * @param parcelableFlags Contextual flags as per
1640 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1641 *
1642 * @see #readTypedObject
1643 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001644 public final <T extends Parcelable> void writeTypedObject(@Nullable T val,
1645 int parcelableFlags) {
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07001646 if (val != null) {
1647 writeInt(1);
1648 val.writeToParcel(this, parcelableFlags);
1649 } else {
1650 writeInt(0);
1651 }
1652 }
1653
1654 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 * Flatten a generic object in to a parcel. The given Object value may
1656 * currently be one of the following types:
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001657 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 * <ul>
1659 * <li> null
1660 * <li> String
1661 * <li> Byte
1662 * <li> Short
1663 * <li> Integer
1664 * <li> Long
1665 * <li> Float
1666 * <li> Double
1667 * <li> Boolean
1668 * <li> String[]
1669 * <li> boolean[]
1670 * <li> byte[]
1671 * <li> int[]
1672 * <li> long[]
1673 * <li> Object[] (supporting objects of the same type defined here).
1674 * <li> {@link Bundle}
1675 * <li> Map (as supported by {@link #writeMap}).
1676 * <li> Any object that implements the {@link Parcelable} protocol.
1677 * <li> Parcelable[]
1678 * <li> CharSequence (as supported by {@link TextUtils#writeToParcel}).
1679 * <li> List (as supported by {@link #writeList}).
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001680 * <li> {@link SparseArray} (as supported by {@link #writeSparseArray(SparseArray)}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 * <li> {@link IBinder}
1682 * <li> Any object that implements Serializable (but see
1683 * {@link #writeSerializable} for caveats). Note that all of the
1684 * previous types have relatively efficient implementations for
1685 * writing to a Parcel; having to rely on the generic serialization
1686 * approach is much less efficient and should be avoided whenever
1687 * possible.
1688 * </ul>
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001689 *
1690 * <p class="caution">{@link Parcelable} objects are written with
1691 * {@link Parcelable#writeToParcel} using contextual flags of 0. When
1692 * serializing objects containing {@link ParcelFileDescriptor}s,
1693 * this may result in file descriptor leaks when they are returned from
1694 * Binder calls (where {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE}
1695 * should be used).</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001697 public final void writeValue(@Nullable Object v) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 if (v == null) {
1699 writeInt(VAL_NULL);
1700 } else if (v instanceof String) {
1701 writeInt(VAL_STRING);
1702 writeString((String) v);
1703 } else if (v instanceof Integer) {
1704 writeInt(VAL_INTEGER);
1705 writeInt((Integer) v);
1706 } else if (v instanceof Map) {
1707 writeInt(VAL_MAP);
1708 writeMap((Map) v);
1709 } else if (v instanceof Bundle) {
1710 // Must be before Parcelable
1711 writeInt(VAL_BUNDLE);
1712 writeBundle((Bundle) v);
Samuel Tanceafe5e2015-12-11 16:50:58 -08001713 } else if (v instanceof PersistableBundle) {
1714 writeInt(VAL_PERSISTABLEBUNDLE);
1715 writePersistableBundle((PersistableBundle) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 } else if (v instanceof Parcelable) {
Samuel Tanceafe5e2015-12-11 16:50:58 -08001717 // IMPOTANT: cases for classes that implement Parcelable must
1718 // come before the Parcelable case, so that their specific VAL_*
1719 // types will be written.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 writeInt(VAL_PARCELABLE);
1721 writeParcelable((Parcelable) v, 0);
1722 } else if (v instanceof Short) {
1723 writeInt(VAL_SHORT);
1724 writeInt(((Short) v).intValue());
1725 } else if (v instanceof Long) {
1726 writeInt(VAL_LONG);
1727 writeLong((Long) v);
1728 } else if (v instanceof Float) {
1729 writeInt(VAL_FLOAT);
1730 writeFloat((Float) v);
1731 } else if (v instanceof Double) {
1732 writeInt(VAL_DOUBLE);
1733 writeDouble((Double) v);
1734 } else if (v instanceof Boolean) {
1735 writeInt(VAL_BOOLEAN);
1736 writeInt((Boolean) v ? 1 : 0);
1737 } else if (v instanceof CharSequence) {
1738 // Must be after String
1739 writeInt(VAL_CHARSEQUENCE);
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001740 writeCharSequence((CharSequence) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 } else if (v instanceof List) {
1742 writeInt(VAL_LIST);
1743 writeList((List) v);
1744 } else if (v instanceof SparseArray) {
1745 writeInt(VAL_SPARSEARRAY);
1746 writeSparseArray((SparseArray) v);
1747 } else if (v instanceof boolean[]) {
1748 writeInt(VAL_BOOLEANARRAY);
1749 writeBooleanArray((boolean[]) v);
1750 } else if (v instanceof byte[]) {
1751 writeInt(VAL_BYTEARRAY);
1752 writeByteArray((byte[]) v);
1753 } else if (v instanceof String[]) {
1754 writeInt(VAL_STRINGARRAY);
1755 writeStringArray((String[]) v);
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001756 } else if (v instanceof CharSequence[]) {
1757 // Must be after String[] and before Object[]
1758 writeInt(VAL_CHARSEQUENCEARRAY);
1759 writeCharSequenceArray((CharSequence[]) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 } else if (v instanceof IBinder) {
1761 writeInt(VAL_IBINDER);
1762 writeStrongBinder((IBinder) v);
1763 } else if (v instanceof Parcelable[]) {
1764 writeInt(VAL_PARCELABLEARRAY);
1765 writeParcelableArray((Parcelable[]) v, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 } else if (v instanceof int[]) {
1767 writeInt(VAL_INTARRAY);
1768 writeIntArray((int[]) v);
1769 } else if (v instanceof long[]) {
1770 writeInt(VAL_LONGARRAY);
1771 writeLongArray((long[]) v);
1772 } else if (v instanceof Byte) {
1773 writeInt(VAL_BYTE);
1774 writeInt((Byte) v);
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001775 } else if (v instanceof Size) {
1776 writeInt(VAL_SIZE);
1777 writeSize((Size) v);
1778 } else if (v instanceof SizeF) {
1779 writeInt(VAL_SIZEF);
1780 writeSizeF((SizeF) v);
Samuel Tana8036662015-11-23 14:36:00 -08001781 } else if (v instanceof double[]) {
1782 writeInt(VAL_DOUBLEARRAY);
1783 writeDoubleArray((double[]) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 } else {
Paul Duffinac5a0822014-02-03 15:02:17 +00001785 Class<?> clazz = v.getClass();
1786 if (clazz.isArray() && clazz.getComponentType() == Object.class) {
1787 // Only pure Object[] are written here, Other arrays of non-primitive types are
1788 // handled by serialization as this does not record the component type.
1789 writeInt(VAL_OBJECTARRAY);
1790 writeArray((Object[]) v);
1791 } else if (v instanceof Serializable) {
1792 // Must be last
1793 writeInt(VAL_SERIALIZABLE);
1794 writeSerializable((Serializable) v);
1795 } else {
1796 throw new RuntimeException("Parcel: unable to marshal value " + v);
1797 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 }
1799 }
1800
1801 /**
1802 * Flatten the name of the class of the Parcelable and its contents
1803 * into the parcel.
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001804 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 * @param p The Parcelable object to be written.
1806 * @param parcelableFlags Contextual flags as per
1807 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1808 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001809 public final void writeParcelable(@Nullable Parcelable p, int parcelableFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 if (p == null) {
1811 writeString(null);
1812 return;
1813 }
Neil Fuller44e440c2015-04-20 14:39:00 +01001814 writeParcelableCreator(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 p.writeToParcel(this, parcelableFlags);
1816 }
1817
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08001818 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00001819 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04001820 public final void writeParcelableCreator(@NonNull Parcelable p) {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08001821 String name = p.getClass().getName();
1822 writeString(name);
1823 }
1824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 /**
1826 * Write a generic serializable object in to a Parcel. It is strongly
1827 * recommended that this method be avoided, since the serialization
1828 * overhead is extremely large, and this approach will be much slower than
1829 * using the other approaches to writing data in to a Parcel.
1830 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001831 public final void writeSerializable(@Nullable Serializable s) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 if (s == null) {
1833 writeString(null);
1834 return;
1835 }
1836 String name = s.getClass().getName();
1837 writeString(name);
1838
1839 ByteArrayOutputStream baos = new ByteArrayOutputStream();
1840 try {
1841 ObjectOutputStream oos = new ObjectOutputStream(baos);
1842 oos.writeObject(s);
1843 oos.close();
1844
1845 writeByteArray(baos.toByteArray());
1846 } catch (IOException ioe) {
1847 throw new RuntimeException("Parcelable encountered " +
1848 "IOException writing serializable object (name = " + name +
1849 ")", ioe);
1850 }
1851 }
1852
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001853 /** @hide For debugging purposes */
1854 public static void setStackTraceParceling(boolean enabled) {
1855 sParcelExceptionStackTrace = enabled;
1856 }
1857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 /**
1859 * Special function for writing an exception result at the header of
1860 * a parcel, to be used when returning an exception from a transaction.
1861 * Note that this currently only supports a few exception types; any other
1862 * exception will be re-thrown by this function as a RuntimeException
1863 * (to be caught by the system's last-resort exception handling when
1864 * dispatching a transaction).
Samuel Tana8036662015-11-23 14:36:00 -08001865 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001866 * <p>The supported exception types are:
1867 * <ul>
1868 * <li>{@link BadParcelableException}
1869 * <li>{@link IllegalArgumentException}
1870 * <li>{@link IllegalStateException}
1871 * <li>{@link NullPointerException}
1872 * <li>{@link SecurityException}
Dianne Hackborn18482ae2017-08-01 17:41:00 -07001873 * <li>{@link UnsupportedOperationException}
Dianne Hackborn7e714422013-09-13 17:32:57 -07001874 * <li>{@link NetworkOnMainThreadException}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 * </ul>
Samuel Tana8036662015-11-23 14:36:00 -08001876 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 * @param e The Exception to be written.
1878 *
1879 * @see #writeNoException
1880 * @see #readException
1881 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001882 public final void writeException(@NonNull Exception e) {
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07001883 AppOpsManager.prefixParcelWithAppOpsIfNeeded(this);
1884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 int code = 0;
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001886 if (e instanceof Parcelable
1887 && (e.getClass().getClassLoader() == Parcelable.class.getClassLoader())) {
1888 // We only send Parcelable exceptions that are in the
1889 // BootClassLoader to ensure that the receiver can unpack them
1890 code = EX_PARCELABLE;
1891 } else if (e instanceof SecurityException) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 code = EX_SECURITY;
1893 } else if (e instanceof BadParcelableException) {
1894 code = EX_BAD_PARCELABLE;
1895 } else if (e instanceof IllegalArgumentException) {
1896 code = EX_ILLEGAL_ARGUMENT;
1897 } else if (e instanceof NullPointerException) {
1898 code = EX_NULL_POINTER;
1899 } else if (e instanceof IllegalStateException) {
1900 code = EX_ILLEGAL_STATE;
Dianne Hackborn7e714422013-09-13 17:32:57 -07001901 } else if (e instanceof NetworkOnMainThreadException) {
1902 code = EX_NETWORK_MAIN_THREAD;
Dianne Hackborn33d738a2014-09-12 14:23:58 -07001903 } else if (e instanceof UnsupportedOperationException) {
1904 code = EX_UNSUPPORTED_OPERATION;
Christopher Wiley80fd1202015-11-22 17:12:37 -08001905 } else if (e instanceof ServiceSpecificException) {
1906 code = EX_SERVICE_SPECIFIC;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 }
1908 writeInt(code);
Brad Fitzpatrick703e5d32010-07-15 13:16:41 -07001909 StrictMode.clearGatheredViolations();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 if (code == 0) {
1911 if (e instanceof RuntimeException) {
1912 throw (RuntimeException) e;
1913 }
1914 throw new RuntimeException(e);
1915 }
1916 writeString(e.getMessage());
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001917 final long timeNow = sParcelExceptionStackTrace ? SystemClock.elapsedRealtime() : 0;
1918 if (sParcelExceptionStackTrace && (timeNow - sLastWriteExceptionStackTrace
1919 > WRITE_EXCEPTION_STACK_TRACE_THRESHOLD_MS)) {
1920 sLastWriteExceptionStackTrace = timeNow;
1921 final int sizePosition = dataPosition();
1922 writeInt(0); // Header size will be filled in later
1923 StackTraceElement[] stackTrace = e.getStackTrace();
1924 final int truncatedSize = Math.min(stackTrace.length, 5);
1925 StringBuilder sb = new StringBuilder();
1926 for (int i = 0; i < truncatedSize; i++) {
Fyodor Kupolov679d9982017-11-21 10:42:23 -08001927 sb.append("\tat ").append(stackTrace[i]).append('\n');
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001928 }
1929 writeString(sb.toString());
1930 final int payloadPosition = dataPosition();
1931 setDataPosition(sizePosition);
1932 // Write stack trace header size. Used in native side to skip the header
1933 writeInt(payloadPosition - sizePosition);
1934 setDataPosition(payloadPosition);
1935 } else {
1936 writeInt(0);
1937 }
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001938 switch (code) {
1939 case EX_SERVICE_SPECIFIC:
1940 writeInt(((ServiceSpecificException) e).errorCode);
1941 break;
1942 case EX_PARCELABLE:
1943 // Write parceled exception prefixed by length
1944 final int sizePosition = dataPosition();
1945 writeInt(0);
1946 writeParcelable((Parcelable) e, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
1947 final int payloadPosition = dataPosition();
1948 setDataPosition(sizePosition);
1949 writeInt(payloadPosition - sizePosition);
1950 setDataPosition(payloadPosition);
1951 break;
Christopher Wiley80fd1202015-11-22 17:12:37 -08001952 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 }
1954
1955 /**
1956 * Special function for writing information at the front of the Parcel
1957 * indicating that no exception occurred.
1958 *
1959 * @see #writeException
1960 * @see #readException
1961 */
1962 public final void writeNoException() {
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07001963 AppOpsManager.prefixParcelWithAppOpsIfNeeded(this);
1964
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001965 // Despite the name of this function ("write no exception"),
1966 // it should instead be thought of as "write the RPC response
1967 // header", but because this function name is written out by
1968 // the AIDL compiler, we're not going to rename it.
1969 //
1970 // The response header, in the non-exception case (see also
1971 // writeException above, also called by the AIDL compiler), is
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07001972 // either a 0 (the default case), or EX_HAS_STRICTMODE_REPLY_HEADER if
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001973 // StrictMode has gathered up violations that have occurred
1974 // during a Binder call, in which case we write out the number
1975 // of violations and their details, serialized, before the
1976 // actual RPC respons data. The receiving end of this is
1977 // readException(), below.
1978 if (StrictMode.hasGatheredViolations()) {
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07001979 writeInt(EX_HAS_STRICTMODE_REPLY_HEADER);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001980 final int sizePosition = dataPosition();
1981 writeInt(0); // total size of fat header, to be filled in later
1982 StrictMode.writeGatheredViolationsToParcel(this);
1983 final int payloadPosition = dataPosition();
1984 setDataPosition(sizePosition);
1985 writeInt(payloadPosition - sizePosition); // header size
1986 setDataPosition(payloadPosition);
1987 } else {
1988 writeInt(0);
1989 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 }
1991
1992 /**
1993 * Special function for reading an exception result from the header of
1994 * a parcel, to be used after receiving the result of a transaction. This
1995 * will throw the exception for you if it had been written to the Parcel,
1996 * otherwise return and let you read the normal result data from the Parcel.
1997 *
1998 * @see #writeException
1999 * @see #writeNoException
2000 */
2001 public final void readException() {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07002002 int code = readExceptionCode();
2003 if (code != 0) {
2004 String msg = readString();
Takamasa Kuramitsu2214b822018-04-02 14:44:59 +09002005 readException(code, msg);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07002006 }
2007 }
2008
2009 /**
2010 * Parses the header of a Binder call's response Parcel and
2011 * returns the exception code. Deals with lite or fat headers.
2012 * In the common successful case, this header is generally zero.
2013 * In less common cases, it's a small negative number and will be
2014 * followed by an error string.
2015 *
2016 * This exists purely for android.database.DatabaseUtils and
2017 * insulating it from having to handle fat headers as returned by
2018 * e.g. StrictMode-induced RPC responses.
2019 *
2020 * @hide
2021 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002022 @UnsupportedAppUsage
Howard Chen40abbb02019-05-24 02:04:50 +08002023 @TestApi
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07002024 public final int readExceptionCode() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 int code = readInt();
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07002026 if (code == EX_HAS_NOTED_APPOPS_REPLY_HEADER) {
2027 AppOpsManager.readAndLogNotedAppops(this);
2028 // Read next header or real exception if there is no more header
2029 code = readInt();
2030 }
2031
2032 if (code == EX_HAS_STRICTMODE_REPLY_HEADER) {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07002033 int headerSize = readInt();
2034 if (headerSize == 0) {
2035 Log.e(TAG, "Unexpected zero-sized Parcel reply header.");
2036 } else {
2037 // Currently the only thing in the header is StrictMode stacks,
2038 // but discussions around event/RPC tracing suggest we might
2039 // put that here too. If so, switch on sub-header tags here.
2040 // But for now, just parse out the StrictMode stuff.
2041 StrictMode.readAndHandleBinderCallViolations(this);
2042 }
2043 // And fat response headers are currently only used when
2044 // there are no exceptions, so return no error:
2045 return 0;
2046 }
2047 return code;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 }
2049
2050 /**
Mark Doliner879ea452014-01-02 12:38:07 -08002051 * Throw an exception with the given message. Not intended for use
2052 * outside the Parcel class.
2053 *
2054 * @param code Used to determine which exception class to throw.
2055 * @param msg The exception message.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 */
2057 public final void readException(int code, String msg) {
Takamasa Kuramitsu2214b822018-04-02 14:44:59 +09002058 String remoteStackTrace = null;
2059 final int remoteStackPayloadSize = readInt();
2060 if (remoteStackPayloadSize > 0) {
2061 remoteStackTrace = readString();
2062 }
2063 Exception e = createException(code, msg);
2064 // Attach remote stack trace if availalble
2065 if (remoteStackTrace != null) {
2066 RemoteException cause = new RemoteException(
2067 "Remote stack trace:\n" + remoteStackTrace, null, false, false);
2068 try {
2069 Throwable rootCause = ExceptionUtils.getRootCause(e);
2070 if (rootCause != null) {
2071 rootCause.initCause(cause);
2072 }
2073 } catch (RuntimeException ex) {
2074 Log.e(TAG, "Cannot set cause " + cause + " for " + e, ex);
2075 }
2076 }
2077 SneakyThrow.sneakyThrow(e);
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002078 }
2079
2080 /**
2081 * Creates an exception with the given message.
2082 *
2083 * @param code Used to determine which exception class to throw.
2084 * @param msg The exception message.
2085 */
2086 private Exception createException(int code, String msg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 switch (code) {
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07002088 case EX_PARCELABLE:
2089 if (readInt() > 0) {
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002090 return (Exception) readParcelable(Parcelable.class.getClassLoader());
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07002091 } else {
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002092 return new RuntimeException(msg + " [missing Parcelable]");
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07002093 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 case EX_SECURITY:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002095 return new SecurityException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 case EX_BAD_PARCELABLE:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002097 return new BadParcelableException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002098 case EX_ILLEGAL_ARGUMENT:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002099 return new IllegalArgumentException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 case EX_NULL_POINTER:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002101 return new NullPointerException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 case EX_ILLEGAL_STATE:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002103 return new IllegalStateException(msg);
Dianne Hackborn7e714422013-09-13 17:32:57 -07002104 case EX_NETWORK_MAIN_THREAD:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002105 return new NetworkOnMainThreadException();
Dianne Hackborn33d738a2014-09-12 14:23:58 -07002106 case EX_UNSUPPORTED_OPERATION:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002107 return new UnsupportedOperationException(msg);
Christopher Wiley80fd1202015-11-22 17:12:37 -08002108 case EX_SERVICE_SPECIFIC:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002109 return new ServiceSpecificException(readInt(), msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 }
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002111 return new RuntimeException("Unknown exception code: " + code
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 + " msg " + msg);
2113 }
2114
2115 /**
2116 * Read an integer value from the parcel at the current dataPosition().
2117 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002118 public final int readInt() {
2119 return nativeReadInt(mNativePtr);
2120 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121
2122 /**
2123 * Read a long integer value from the parcel at the current dataPosition().
2124 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002125 public final long readLong() {
2126 return nativeReadLong(mNativePtr);
2127 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128
2129 /**
2130 * Read a floating point value from the parcel at the current
2131 * dataPosition().
2132 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002133 public final float readFloat() {
2134 return nativeReadFloat(mNativePtr);
2135 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136
2137 /**
2138 * Read a double precision floating point value from the parcel at the
2139 * current dataPosition().
2140 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002141 public final double readDouble() {
2142 return nativeReadDouble(mNativePtr);
2143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144
2145 /**
2146 * Read a string value from the parcel at the current dataPosition().
2147 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002148 @Nullable
Jeff Sharkey047238c2012-03-07 16:51:38 -08002149 public final String readString() {
Makoto Onuki4501c61d2017-07-27 15:56:40 -07002150 return mReadWriteHelper.readString(this);
2151 }
2152
2153 /**
2154 * Read a string without going though a {@link ReadWriteHelper}. Subclasses of
2155 * {@link ReadWriteHelper} must use this method instead of {@link #readString} to avoid
2156 * infinity recursive calls.
2157 *
2158 * @hide
2159 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002160 @Nullable
Makoto Onuki4501c61d2017-07-27 15:56:40 -07002161 public String readStringNoHelper() {
Jeff Sharkey047238c2012-03-07 16:51:38 -08002162 return nativeReadString(mNativePtr);
2163 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164
Jake Wharton2ffa4312018-04-23 17:47:14 -04002165 /**
2166 * Read a boolean value from the parcel at the current dataPosition().
2167 */
Eugene Susla36e866b2017-02-23 18:24:39 -08002168 public final boolean readBoolean() {
2169 return readInt() != 0;
2170 }
2171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002173 * Read a CharSequence value from the parcel at the current dataPosition().
2174 * @hide
2175 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002176 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04002177 @Nullable
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002178 public final CharSequence readCharSequence() {
2179 return TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(this);
2180 }
2181
2182 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 * Read an object from the parcel at the current dataPosition().
2184 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002185 public final IBinder readStrongBinder() {
2186 return nativeReadStrongBinder(mNativePtr);
2187 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188
2189 /**
2190 * Read a FileDescriptor from the parcel at the current dataPosition().
2191 */
2192 public final ParcelFileDescriptor readFileDescriptor() {
Jeff Sharkey047238c2012-03-07 16:51:38 -08002193 FileDescriptor fd = nativeReadFileDescriptor(mNativePtr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 return fd != null ? new ParcelFileDescriptor(fd) : null;
2195 }
2196
Jeff Sharkeyda5a3e12013-08-11 12:54:42 -07002197 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +00002198 @UnsupportedAppUsage
Jeff Sharkeyda5a3e12013-08-11 12:54:42 -07002199 public final FileDescriptor readRawFileDescriptor() {
2200 return nativeReadFileDescriptor(mNativePtr);
2201 }
2202
Casey Dahlin2f974b22015-11-05 12:19:13 -08002203 /**
2204 * {@hide}
2205 * Read and return a new array of FileDescriptors from the parcel.
2206 * @return the FileDescriptor array, or null if the array is null.
2207 **/
Jake Whartonb1f474c2018-05-30 15:11:13 -04002208 @Nullable
Casey Dahlin2f974b22015-11-05 12:19:13 -08002209 public final FileDescriptor[] createRawFileDescriptorArray() {
2210 int N = readInt();
2211 if (N < 0) {
2212 return null;
2213 }
2214 FileDescriptor[] f = new FileDescriptor[N];
2215 for (int i = 0; i < N; i++) {
2216 f[i] = readRawFileDescriptor();
2217 }
2218 return f;
2219 }
2220
2221 /**
2222 * {@hide}
2223 * Read an array of FileDescriptors from a parcel.
2224 * The passed array must be exactly the length of the array in the parcel.
2225 * @return the FileDescriptor array, or null if the array is null.
2226 **/
2227 public final void readRawFileDescriptorArray(FileDescriptor[] val) {
2228 int N = readInt();
2229 if (N == val.length) {
2230 for (int i=0; i<N; i++) {
2231 val[i] = readRawFileDescriptor();
2232 }
2233 } else {
2234 throw new RuntimeException("bad array lengths");
2235 }
2236 }
2237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 /**
2239 * Read a byte value from the parcel at the current dataPosition().
2240 */
2241 public final byte readByte() {
2242 return (byte)(readInt() & 0xff);
2243 }
2244
2245 /**
2246 * Please use {@link #readBundle(ClassLoader)} instead (whose data must have
2247 * been written with {@link #writeBundle}. Read into an existing Map object
2248 * from the parcel at the current dataPosition().
2249 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002250 public final void readMap(@NonNull Map outVal, @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002251 int N = readInt();
2252 readMapInternal(outVal, N, loader);
2253 }
2254
2255 /**
2256 * Read into an existing List object from the parcel at the current
2257 * dataPosition(), using the given class loader to load any enclosed
2258 * Parcelables. If it is null, the default class loader is used.
2259 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002260 public final void readList(@NonNull List outVal, @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261 int N = readInt();
2262 readListInternal(outVal, N, loader);
2263 }
2264
2265 /**
2266 * Please use {@link #readBundle(ClassLoader)} instead (whose data must have
2267 * been written with {@link #writeBundle}. Read and return a new HashMap
2268 * object from the parcel at the current dataPosition(), using the given
2269 * class loader to load any enclosed Parcelables. Returns null if
2270 * the previously written map object was null.
2271 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002272 @Nullable
2273 public final HashMap readHashMap(@Nullable ClassLoader loader)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 {
2275 int N = readInt();
2276 if (N < 0) {
2277 return null;
2278 }
2279 HashMap m = new HashMap(N);
2280 readMapInternal(m, N, loader);
2281 return m;
2282 }
2283
2284 /**
2285 * Read and return a new Bundle object from the parcel at the current
2286 * dataPosition(). Returns null if the previously written Bundle object was
2287 * null.
2288 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002289 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002290 public final Bundle readBundle() {
2291 return readBundle(null);
2292 }
2293
2294 /**
2295 * Read and return a new Bundle object from the parcel at the current
2296 * dataPosition(), using the given class loader to initialize the class
2297 * loader of the Bundle for later retrieval of Parcelable objects.
2298 * Returns null if the previously written Bundle object was null.
2299 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002300 @Nullable
2301 public final Bundle readBundle(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002302 int length = readInt();
2303 if (length < 0) {
Dianne Hackborn4a7d8242013-10-03 10:19:20 -07002304 if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 return null;
2306 }
Samuel Tana8036662015-11-23 14:36:00 -08002307
Dianne Hackborn6aff9052009-05-22 13:20:23 -07002308 final Bundle bundle = new Bundle(this, length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 if (loader != null) {
2310 bundle.setClassLoader(loader);
2311 }
2312 return bundle;
2313 }
2314
2315 /**
Craig Mautner719e6b12014-04-04 20:29:41 -07002316 * Read and return a new Bundle object from the parcel at the current
2317 * dataPosition(). Returns null if the previously written Bundle object was
2318 * null.
2319 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002320 @Nullable
Craig Mautner719e6b12014-04-04 20:29:41 -07002321 public final PersistableBundle readPersistableBundle() {
2322 return readPersistableBundle(null);
2323 }
2324
2325 /**
2326 * Read and return a new Bundle object from the parcel at the current
2327 * dataPosition(), using the given class loader to initialize the class
2328 * loader of the Bundle for later retrieval of Parcelable objects.
2329 * Returns null if the previously written Bundle object was null.
2330 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002331 @Nullable
2332 public final PersistableBundle readPersistableBundle(@Nullable ClassLoader loader) {
Craig Mautner719e6b12014-04-04 20:29:41 -07002333 int length = readInt();
2334 if (length < 0) {
2335 if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length);
2336 return null;
2337 }
2338
2339 final PersistableBundle bundle = new PersistableBundle(this, length);
2340 if (loader != null) {
2341 bundle.setClassLoader(loader);
2342 }
2343 return bundle;
2344 }
2345
2346 /**
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002347 * Read a Size from the parcel at the current dataPosition().
2348 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002349 @NonNull
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002350 public final Size readSize() {
2351 final int width = readInt();
2352 final int height = readInt();
2353 return new Size(width, height);
2354 }
2355
2356 /**
2357 * Read a SizeF from the parcel at the current dataPosition().
2358 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002359 @NonNull
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002360 public final SizeF readSizeF() {
2361 final float width = readFloat();
2362 final float height = readFloat();
2363 return new SizeF(width, height);
2364 }
2365
2366 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 * Read and return a byte[] object from the parcel.
2368 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002369 @Nullable
Jeff Sharkey047238c2012-03-07 16:51:38 -08002370 public final byte[] createByteArray() {
2371 return nativeCreateByteArray(mNativePtr);
2372 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373
2374 /**
2375 * Read a byte[] object from the parcel and copy it into the
2376 * given byte array.
2377 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002378 public final void readByteArray(@NonNull byte[] val) {
Jocelyn Dang46100442017-05-05 15:40:49 -07002379 boolean valid = nativeReadByteArray(mNativePtr, val, (val != null) ? val.length : 0);
2380 if (!valid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381 throw new RuntimeException("bad array lengths");
2382 }
2383 }
2384
2385 /**
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002386 * Read a blob of data from the parcel and return it as a byte array.
2387 * {@hide}
Sandeep Siddhartha39c12fa2014-07-25 18:37:29 -07002388 * {@SystemApi}
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002389 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002390 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04002391 @Nullable
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002392 public final byte[] readBlob() {
2393 return nativeReadBlob(mNativePtr);
2394 }
2395
2396 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 * Read and return a String[] object from the parcel.
2398 * {@hide}
2399 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002400 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04002401 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 public final String[] readStringArray() {
2403 String[] array = null;
2404
2405 int length = readInt();
2406 if (length >= 0)
2407 {
2408 array = new String[length];
2409
2410 for (int i = 0 ; i < length ; i++)
2411 {
2412 array[i] = readString();
2413 }
2414 }
2415
2416 return array;
2417 }
2418
2419 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002420 * Read and return a CharSequence[] object from the parcel.
2421 * {@hide}
2422 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002423 @Nullable
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002424 public final CharSequence[] readCharSequenceArray() {
2425 CharSequence[] array = null;
2426
2427 int length = readInt();
2428 if (length >= 0)
2429 {
2430 array = new CharSequence[length];
2431
2432 for (int i = 0 ; i < length ; i++)
2433 {
2434 array[i] = readCharSequence();
2435 }
2436 }
2437
2438 return array;
2439 }
2440
2441 /**
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002442 * Read and return an ArrayList&lt;CharSequence&gt; object from the parcel.
2443 * {@hide}
2444 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002445 @Nullable
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002446 public final ArrayList<CharSequence> readCharSequenceList() {
2447 ArrayList<CharSequence> array = null;
2448
2449 int length = readInt();
2450 if (length >= 0) {
2451 array = new ArrayList<CharSequence>(length);
2452
2453 for (int i = 0 ; i < length ; i++) {
2454 array.add(readCharSequence());
2455 }
2456 }
2457
2458 return array;
2459 }
2460
2461 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 * Read and return a new ArrayList object from the parcel at the current
2463 * dataPosition(). Returns null if the previously written list object was
2464 * null. The given class loader will be used to load any enclosed
2465 * Parcelables.
2466 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002467 @Nullable
2468 public final ArrayList readArrayList(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 int N = readInt();
2470 if (N < 0) {
2471 return null;
2472 }
2473 ArrayList l = new ArrayList(N);
2474 readListInternal(l, N, loader);
2475 return l;
2476 }
2477
2478 /**
2479 * Read and return a new Object array from the parcel at the current
2480 * dataPosition(). Returns null if the previously written array was
2481 * null. The given class loader will be used to load any enclosed
2482 * Parcelables.
2483 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002484 @Nullable
2485 public final Object[] readArray(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 int N = readInt();
2487 if (N < 0) {
2488 return null;
2489 }
2490 Object[] l = new Object[N];
2491 readArrayInternal(l, N, loader);
2492 return l;
2493 }
2494
2495 /**
2496 * Read and return a new SparseArray object from the parcel at the current
2497 * dataPosition(). Returns null if the previously written list object was
2498 * null. The given class loader will be used to load any enclosed
2499 * Parcelables.
2500 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002501 @Nullable
Svet Ganov8455ba22019-01-02 13:05:56 -08002502 public final <T> SparseArray<T> readSparseArray(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002503 int N = readInt();
2504 if (N < 0) {
2505 return null;
2506 }
2507 SparseArray sa = new SparseArray(N);
2508 readSparseArrayInternal(sa, N, loader);
2509 return sa;
2510 }
2511
2512 /**
2513 * Read and return a new SparseBooleanArray object from the parcel at the current
2514 * dataPosition(). Returns null if the previously written list object was
2515 * null.
2516 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002517 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 public final SparseBooleanArray readSparseBooleanArray() {
2519 int N = readInt();
2520 if (N < 0) {
2521 return null;
2522 }
2523 SparseBooleanArray sa = new SparseBooleanArray(N);
2524 readSparseBooleanArrayInternal(sa, N);
2525 return sa;
2526 }
2527
2528 /**
Adam Lesinski4e862812016-11-21 16:02:24 -08002529 * Read and return a new SparseIntArray object from the parcel at the current
2530 * dataPosition(). Returns null if the previously written array object was null.
Adam Lesinski205656d2017-03-23 13:38:26 -07002531 * @hide
Adam Lesinski4e862812016-11-21 16:02:24 -08002532 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002533 @Nullable
Adam Lesinski4e862812016-11-21 16:02:24 -08002534 public final SparseIntArray readSparseIntArray() {
2535 int N = readInt();
2536 if (N < 0) {
2537 return null;
2538 }
2539 SparseIntArray sa = new SparseIntArray(N);
2540 readSparseIntArrayInternal(sa, N);
2541 return sa;
2542 }
2543
2544 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545 * Read and return a new ArrayList containing a particular object type from
2546 * the parcel that was written with {@link #writeTypedList} at the
2547 * current dataPosition(). Returns null if the
2548 * previously written list object was null. The list <em>must</em> have
2549 * previously been written via {@link #writeTypedList} with the same object
2550 * type.
2551 *
2552 * @return A newly created ArrayList containing objects with the same data
2553 * as those that were previously written.
2554 *
2555 * @see #writeTypedList
2556 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002557 @Nullable
2558 public final <T> ArrayList<T> createTypedArrayList(@NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002559 int N = readInt();
2560 if (N < 0) {
2561 return null;
2562 }
2563 ArrayList<T> l = new ArrayList<T>(N);
2564 while (N > 0) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002565 l.add(readTypedObject(c));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002566 N--;
2567 }
2568 return l;
2569 }
2570
2571 /**
2572 * Read into the given List items containing a particular object type
2573 * that were written with {@link #writeTypedList} at the
2574 * current dataPosition(). The list <em>must</em> have
2575 * previously been written via {@link #writeTypedList} with the same object
2576 * type.
2577 *
2578 * @return A newly created ArrayList containing objects with the same data
2579 * as those that were previously written.
2580 *
2581 * @see #writeTypedList
2582 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002583 public final <T> void readTypedList(@NonNull List<T> list, @NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002584 int M = list.size();
2585 int N = readInt();
2586 int i = 0;
2587 for (; i < M && i < N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002588 list.set(i, readTypedObject(c));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002589 }
2590 for (; i<N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002591 list.add(readTypedObject(c));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 }
2593 for (; i<M; i++) {
2594 list.remove(N);
2595 }
2596 }
2597
2598 /**
Svet Ganov8455ba22019-01-02 13:05:56 -08002599 * Read into a new {@link SparseArray} items containing a particular object type
2600 * that were written with {@link #writeTypedSparseArray(SparseArray, int)} at the
2601 * current dataPosition(). The list <em>must</em> have previously been written
2602 * via {@link #writeTypedSparseArray(SparseArray, int)} with the same object type.
2603 *
2604 * @param creator The creator to use when for instantiation.
2605 *
2606 * @return A newly created {@link SparseArray} containing objects with the same data
2607 * as those that were previously written.
2608 *
2609 * @see #writeTypedSparseArray(SparseArray, int)
2610 */
2611 public final @Nullable <T extends Parcelable> SparseArray<T> createTypedSparseArray(
2612 @NonNull Parcelable.Creator<T> creator) {
2613 final int count = readInt();
2614 if (count < 0) {
2615 return null;
2616 }
2617 final SparseArray<T> array = new SparseArray<>(count);
2618 for (int i = 0; i < count; i++) {
2619 final int index = readInt();
2620 final T value = readTypedObject(creator);
2621 array.append(index, value);
2622 }
2623 return array;
2624 }
2625
2626 /**
2627 * Read into a new {@link ArrayMap} with string keys items containing a particular
2628 * object type that were written with {@link #writeTypedArrayMap(ArrayMap, int)} at the
2629 * current dataPosition(). The list <em>must</em> have previously been written
2630 * via {@link #writeTypedArrayMap(ArrayMap, int)} with the same object type.
2631 *
2632 * @param creator The creator to use when for instantiation.
2633 *
2634 * @return A newly created {@link ArrayMap} containing objects with the same data
2635 * as those that were previously written.
2636 *
2637 * @see #writeTypedArrayMap(ArrayMap, int)
2638 */
2639 public final @Nullable <T extends Parcelable> ArrayMap<String, T> createTypedArrayMap(
2640 @NonNull Parcelable.Creator<T> creator) {
2641 final int count = readInt();
2642 if (count < 0) {
2643 return null;
2644 }
2645 final ArrayMap<String, T> map = new ArrayMap<>(count);
2646 for (int i = 0; i < count; i++) {
2647 final String key = readString();
2648 final T value = readTypedObject(creator);
2649 map.append(key, value);
2650 }
2651 return map;
2652 }
2653
2654 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655 * Read and return a new ArrayList containing String objects from
2656 * the parcel that was written with {@link #writeStringList} at the
2657 * current dataPosition(). Returns null if the
2658 * previously written list object was null.
2659 *
2660 * @return A newly created ArrayList containing strings with the same data
2661 * as those that were previously written.
2662 *
2663 * @see #writeStringList
2664 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002665 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 public final ArrayList<String> createStringArrayList() {
2667 int N = readInt();
2668 if (N < 0) {
2669 return null;
2670 }
2671 ArrayList<String> l = new ArrayList<String>(N);
2672 while (N > 0) {
2673 l.add(readString());
2674 N--;
2675 }
2676 return l;
2677 }
2678
2679 /**
2680 * Read and return a new ArrayList containing IBinder objects from
2681 * the parcel that was written with {@link #writeBinderList} at the
2682 * current dataPosition(). Returns null if the
2683 * previously written list object was null.
2684 *
2685 * @return A newly created ArrayList containing strings with the same data
2686 * as those that were previously written.
2687 *
2688 * @see #writeBinderList
2689 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002690 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 public final ArrayList<IBinder> createBinderArrayList() {
2692 int N = readInt();
2693 if (N < 0) {
2694 return null;
2695 }
2696 ArrayList<IBinder> l = new ArrayList<IBinder>(N);
2697 while (N > 0) {
2698 l.add(readStrongBinder());
2699 N--;
2700 }
2701 return l;
2702 }
2703
2704 /**
2705 * Read into the given List items String objects that were written with
2706 * {@link #writeStringList} at the current dataPosition().
2707 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 * @see #writeStringList
2709 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002710 public final void readStringList(@NonNull List<String> list) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 int M = list.size();
2712 int N = readInt();
2713 int i = 0;
2714 for (; i < M && i < N; i++) {
2715 list.set(i, readString());
2716 }
2717 for (; i<N; i++) {
2718 list.add(readString());
2719 }
2720 for (; i<M; i++) {
2721 list.remove(N);
2722 }
2723 }
2724
2725 /**
2726 * Read into the given List items IBinder objects that were written with
2727 * {@link #writeBinderList} at the current dataPosition().
2728 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002729 * @see #writeBinderList
2730 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002731 public final void readBinderList(@NonNull List<IBinder> list) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 int M = list.size();
2733 int N = readInt();
2734 int i = 0;
2735 for (; i < M && i < N; i++) {
2736 list.set(i, readStrongBinder());
2737 }
2738 for (; i<N; i++) {
2739 list.add(readStrongBinder());
2740 }
2741 for (; i<M; i++) {
2742 list.remove(N);
2743 }
2744 }
2745
2746 /**
Narayan Kamathbea48712016-12-01 15:38:28 +00002747 * Read the list of {@code Parcelable} objects at the current data position into the
2748 * given {@code list}. The contents of the {@code list} are replaced. If the serialized
2749 * list was {@code null}, {@code list} is cleared.
2750 *
2751 * @see #writeParcelableList(List, int)
Narayan Kamathbea48712016-12-01 15:38:28 +00002752 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002753 @NonNull
2754 public final <T extends Parcelable> List<T> readParcelableList(@NonNull List<T> list,
2755 @Nullable ClassLoader cl) {
Narayan Kamathbea48712016-12-01 15:38:28 +00002756 final int N = readInt();
2757 if (N == -1) {
2758 list.clear();
Eugene Susla36e866b2017-02-23 18:24:39 -08002759 return list;
Narayan Kamathbea48712016-12-01 15:38:28 +00002760 }
2761
2762 final int M = list.size();
2763 int i = 0;
2764 for (; i < M && i < N; i++) {
2765 list.set(i, (T) readParcelable(cl));
2766 }
2767 for (; i<N; i++) {
2768 list.add((T) readParcelable(cl));
2769 }
2770 for (; i<M; i++) {
2771 list.remove(N);
2772 }
Eugene Susla36e866b2017-02-23 18:24:39 -08002773 return list;
Narayan Kamathbea48712016-12-01 15:38:28 +00002774 }
2775
2776 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 * Read and return a new array containing a particular object type from
2778 * the parcel at the current dataPosition(). Returns null if the
2779 * previously written array was null. The array <em>must</em> have
2780 * previously been written via {@link #writeTypedArray} with the same
2781 * object type.
2782 *
2783 * @return A newly created array containing objects with the same data
2784 * as those that were previously written.
2785 *
2786 * @see #writeTypedArray
2787 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002788 @Nullable
2789 public final <T> T[] createTypedArray(@NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 int N = readInt();
2791 if (N < 0) {
2792 return null;
2793 }
2794 T[] l = c.newArray(N);
2795 for (int i=0; i<N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002796 l[i] = readTypedObject(c);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 }
2798 return l;
2799 }
2800
Jake Whartonb1f474c2018-05-30 15:11:13 -04002801 public final <T> void readTypedArray(@NonNull T[] val, @NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 int N = readInt();
2803 if (N == val.length) {
2804 for (int i=0; i<N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002805 val[i] = readTypedObject(c);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806 }
2807 } else {
2808 throw new RuntimeException("bad array lengths");
2809 }
2810 }
2811
2812 /**
2813 * @deprecated
2814 * @hide
2815 */
2816 @Deprecated
2817 public final <T> T[] readTypedArray(Parcelable.Creator<T> c) {
2818 return createTypedArray(c);
2819 }
2820
2821 /**
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07002822 * Read and return a typed Parcelable object from a parcel.
2823 * Returns null if the previous written object was null.
2824 * The object <em>must</em> have previous been written via
2825 * {@link #writeTypedObject} with the same object type.
2826 *
2827 * @return A newly created object of the type that was previously
2828 * written.
2829 *
2830 * @see #writeTypedObject
2831 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002832 @Nullable
2833 public final <T> T readTypedObject(@NonNull Parcelable.Creator<T> c) {
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07002834 if (readInt() != 0) {
2835 return c.createFromParcel(this);
2836 } else {
2837 return null;
2838 }
2839 }
2840
2841 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 * Write a heterogeneous array of Parcelable objects into the Parcel.
2843 * Each object in the array is written along with its class name, so
2844 * that the correct class can later be instantiated. As a result, this
2845 * has significantly more overhead than {@link #writeTypedArray}, but will
2846 * correctly handle an array containing more than one type of object.
2847 *
2848 * @param value The array of objects to be written.
2849 * @param parcelableFlags Contextual flags as per
2850 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
2851 *
2852 * @see #writeTypedArray
2853 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002854 public final <T extends Parcelable> void writeParcelableArray(@Nullable T[] value,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 int parcelableFlags) {
2856 if (value != null) {
2857 int N = value.length;
2858 writeInt(N);
2859 for (int i=0; i<N; i++) {
2860 writeParcelable(value[i], parcelableFlags);
2861 }
2862 } else {
2863 writeInt(-1);
2864 }
2865 }
2866
2867 /**
2868 * Read a typed object from a parcel. The given class loader will be
2869 * used to load any enclosed Parcelables. If it is null, the default class
2870 * loader will be used.
2871 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002872 @Nullable
2873 public final Object readValue(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874 int type = readInt();
2875
2876 switch (type) {
2877 case VAL_NULL:
2878 return null;
2879
2880 case VAL_STRING:
2881 return readString();
2882
2883 case VAL_INTEGER:
2884 return readInt();
2885
2886 case VAL_MAP:
2887 return readHashMap(loader);
2888
2889 case VAL_PARCELABLE:
2890 return readParcelable(loader);
2891
2892 case VAL_SHORT:
2893 return (short) readInt();
2894
2895 case VAL_LONG:
2896 return readLong();
2897
2898 case VAL_FLOAT:
2899 return readFloat();
2900
2901 case VAL_DOUBLE:
2902 return readDouble();
2903
2904 case VAL_BOOLEAN:
2905 return readInt() == 1;
2906
2907 case VAL_CHARSEQUENCE:
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002908 return readCharSequence();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909
2910 case VAL_LIST:
2911 return readArrayList(loader);
2912
2913 case VAL_BOOLEANARRAY:
Samuel Tana8036662015-11-23 14:36:00 -08002914 return createBooleanArray();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915
2916 case VAL_BYTEARRAY:
2917 return createByteArray();
2918
2919 case VAL_STRINGARRAY:
2920 return readStringArray();
2921
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002922 case VAL_CHARSEQUENCEARRAY:
2923 return readCharSequenceArray();
2924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 case VAL_IBINDER:
2926 return readStrongBinder();
2927
2928 case VAL_OBJECTARRAY:
2929 return readArray(loader);
2930
2931 case VAL_INTARRAY:
2932 return createIntArray();
2933
2934 case VAL_LONGARRAY:
2935 return createLongArray();
2936
2937 case VAL_BYTE:
2938 return readByte();
2939
2940 case VAL_SERIALIZABLE:
John Spurlock5002b8c2014-01-10 13:32:12 -05002941 return readSerializable(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002942
2943 case VAL_PARCELABLEARRAY:
2944 return readParcelableArray(loader);
2945
2946 case VAL_SPARSEARRAY:
2947 return readSparseArray(loader);
2948
2949 case VAL_SPARSEBOOLEANARRAY:
2950 return readSparseBooleanArray();
2951
2952 case VAL_BUNDLE:
2953 return readBundle(loader); // loading will be deferred
2954
Craig Mautner719e6b12014-04-04 20:29:41 -07002955 case VAL_PERSISTABLEBUNDLE:
2956 return readPersistableBundle(loader);
2957
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002958 case VAL_SIZE:
2959 return readSize();
2960
2961 case VAL_SIZEF:
2962 return readSizeF();
2963
Samuel Tana8036662015-11-23 14:36:00 -08002964 case VAL_DOUBLEARRAY:
2965 return createDoubleArray();
2966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 default:
2968 int off = dataPosition() - 4;
2969 throw new RuntimeException(
2970 "Parcel " + this + ": Unmarshalling unknown type code " + type + " at offset " + off);
2971 }
2972 }
2973
2974 /**
2975 * Read and return a new Parcelable from the parcel. The given class loader
2976 * will be used to load any enclosed Parcelables. If it is null, the default
2977 * class loader will be used.
2978 * @param loader A ClassLoader from which to instantiate the Parcelable
2979 * object, or null for the default class loader.
2980 * @return Returns the newly created Parcelable, or null if a null
2981 * object has been written.
2982 * @throws BadParcelableException Throws BadParcelableException if there
2983 * was an error trying to instantiate the Parcelable.
2984 */
Neil Fuller44e440c2015-04-20 14:39:00 +01002985 @SuppressWarnings("unchecked")
Jake Whartonb1f474c2018-05-30 15:11:13 -04002986 @Nullable
2987 public final <T extends Parcelable> T readParcelable(@Nullable ClassLoader loader) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002988 Parcelable.Creator<?> creator = readParcelableCreator(loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002989 if (creator == null) {
2990 return null;
2991 }
2992 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002993 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
2994 (Parcelable.ClassLoaderCreator<?>) creator;
2995 return (T) classLoaderCreator.createFromParcel(this, loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002996 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002997 return (T) creator.createFromParcel(this);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002998 }
2999
3000 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00003001 @UnsupportedAppUsage
Neil Fuller44e440c2015-04-20 14:39:00 +01003002 @SuppressWarnings("unchecked")
Jake Whartonb1f474c2018-05-30 15:11:13 -04003003 @Nullable
3004 public final <T extends Parcelable> T readCreator(@NonNull Parcelable.Creator<?> creator,
3005 @Nullable ClassLoader loader) {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08003006 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
Neil Fuller44e440c2015-04-20 14:39:00 +01003007 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
3008 (Parcelable.ClassLoaderCreator<?>) creator;
3009 return (T) classLoaderCreator.createFromParcel(this, loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08003010 }
Neil Fuller44e440c2015-04-20 14:39:00 +01003011 return (T) creator.createFromParcel(this);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08003012 }
3013
3014 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00003015 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04003016 @Nullable
3017 public final Parcelable.Creator<?> readParcelableCreator(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003018 String name = readString();
3019 if (name == null) {
3020 return null;
3021 }
Neil Fuller44e440c2015-04-20 14:39:00 +01003022 Parcelable.Creator<?> creator;
Andreas Gampe4c0df5c2019-05-30 14:38:43 -07003023 HashMap<String, Parcelable.Creator<?>> map;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003024 synchronized (mCreators) {
Andreas Gampe4c0df5c2019-05-30 14:38:43 -07003025 map = mCreators.get(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003026 if (map == null) {
Neil Fuller44e440c2015-04-20 14:39:00 +01003027 map = new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003028 mCreators.put(loader, map);
3029 }
3030 creator = map.get(name);
Andreas Gampe4c0df5c2019-05-30 14:38:43 -07003031 }
3032 if (creator != null) {
3033 return creator;
3034 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035
Andreas Gampe4c0df5c2019-05-30 14:38:43 -07003036 try {
3037 // If loader == null, explicitly emulate Class.forName(String) "caller
3038 // classloader" behavior.
3039 ClassLoader parcelableClassLoader =
3040 (loader == null ? getClass().getClassLoader() : loader);
3041 // Avoid initializing the Parcelable class until we know it implements
3042 // Parcelable and has the necessary CREATOR field. http://b/1171613.
3043 Class<?> parcelableClass = Class.forName(name, false /* initialize */,
3044 parcelableClassLoader);
3045 if (!Parcelable.class.isAssignableFrom(parcelableClass)) {
3046 throw new BadParcelableException("Parcelable protocol requires subclassing "
3047 + "from Parcelable on class " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003048 }
Andreas Gampe4c0df5c2019-05-30 14:38:43 -07003049 Field f = parcelableClass.getField("CREATOR");
3050 if ((f.getModifiers() & Modifier.STATIC) == 0) {
3051 throw new BadParcelableException("Parcelable protocol requires "
3052 + "the CREATOR object to be static on class " + name);
3053 }
3054 Class<?> creatorType = f.getType();
3055 if (!Parcelable.Creator.class.isAssignableFrom(creatorType)) {
3056 // Fail before calling Field.get(), not after, to avoid initializing
3057 // parcelableClass unnecessarily.
3058 throw new BadParcelableException("Parcelable protocol requires a "
3059 + "Parcelable.Creator object called "
3060 + "CREATOR on class " + name);
3061 }
3062 creator = (Parcelable.Creator<?>) f.get(null);
3063 } catch (IllegalAccessException e) {
3064 Log.e(TAG, "Illegal access when unmarshalling: " + name, e);
3065 throw new BadParcelableException(
3066 "IllegalAccessException when unmarshalling: " + name);
3067 } catch (ClassNotFoundException e) {
3068 Log.e(TAG, "Class not found when unmarshalling: " + name, e);
3069 throw new BadParcelableException(
3070 "ClassNotFoundException when unmarshalling: " + name);
3071 } catch (NoSuchFieldException e) {
3072 throw new BadParcelableException("Parcelable protocol requires a "
3073 + "Parcelable.Creator object called "
3074 + "CREATOR on class " + name);
3075 }
3076 if (creator == null) {
3077 throw new BadParcelableException("Parcelable protocol requires a "
3078 + "non-null Parcelable.Creator object called "
3079 + "CREATOR on class " + name);
3080 }
3081
3082 synchronized (mCreators) {
3083 map.put(name, creator);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003084 }
3085
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08003086 return creator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 }
3088
3089 /**
3090 * Read and return a new Parcelable array from the parcel.
3091 * The given class loader will be used to load any enclosed
3092 * Parcelables.
3093 * @return the Parcelable array, or null if the array is null
3094 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04003095 @Nullable
3096 public final Parcelable[] readParcelableArray(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003097 int N = readInt();
3098 if (N < 0) {
3099 return null;
3100 }
3101 Parcelable[] p = new Parcelable[N];
3102 for (int i = 0; i < N; i++) {
Neil Fuller44e440c2015-04-20 14:39:00 +01003103 p[i] = readParcelable(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 }
3105 return p;
3106 }
3107
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003108 /** @hide */
Jake Whartonb1f474c2018-05-30 15:11:13 -04003109 @Nullable
3110 public final <T extends Parcelable> T[] readParcelableArray(@Nullable ClassLoader loader,
3111 @NonNull Class<T> clazz) {
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003112 int N = readInt();
3113 if (N < 0) {
3114 return null;
3115 }
3116 T[] p = (T[]) Array.newInstance(clazz, N);
3117 for (int i = 0; i < N; i++) {
3118 p[i] = readParcelable(loader);
3119 }
3120 return p;
3121 }
3122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003123 /**
3124 * Read and return a new Serializable object from the parcel.
3125 * @return the Serializable object, or null if the Serializable name
3126 * wasn't found in the parcel.
3127 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04003128 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003129 public final Serializable readSerializable() {
John Spurlock5002b8c2014-01-10 13:32:12 -05003130 return readSerializable(null);
3131 }
3132
Jake Whartonb1f474c2018-05-30 15:11:13 -04003133 @Nullable
3134 private final Serializable readSerializable(@Nullable final ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003135 String name = readString();
3136 if (name == null) {
3137 // For some reason we were unable to read the name of the Serializable (either there
3138 // is nothing left in the Parcel to read, or the next value wasn't a String), so
3139 // return null, which indicates that the name wasn't found in the parcel.
3140 return null;
3141 }
3142
3143 byte[] serializedData = createByteArray();
3144 ByteArrayInputStream bais = new ByteArrayInputStream(serializedData);
3145 try {
John Spurlock5002b8c2014-01-10 13:32:12 -05003146 ObjectInputStream ois = new ObjectInputStream(bais) {
3147 @Override
3148 protected Class<?> resolveClass(ObjectStreamClass osClass)
3149 throws IOException, ClassNotFoundException {
3150 // try the custom classloader if provided
3151 if (loader != null) {
3152 Class<?> c = Class.forName(osClass.getName(), false, loader);
3153 if (c != null) {
3154 return c;
3155 }
3156 }
3157 return super.resolveClass(osClass);
3158 }
3159 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003160 return (Serializable) ois.readObject();
3161 } catch (IOException ioe) {
3162 throw new RuntimeException("Parcelable encountered " +
3163 "IOException reading a Serializable object (name = " + name +
3164 ")", ioe);
3165 } catch (ClassNotFoundException cnfe) {
John Spurlock5002b8c2014-01-10 13:32:12 -05003166 throw new RuntimeException("Parcelable encountered " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 "ClassNotFoundException reading a Serializable object (name = "
3168 + name + ")", cnfe);
3169 }
3170 }
3171
3172 // Cache of previously looked up CREATOR.createFromParcel() methods for
3173 // particular classes. Keys are the names of the classes, values are
3174 // Method objects.
Artur Satayev70507ed2019-07-29 13:18:27 +01003175 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
Neil Fuller44e440c2015-04-20 14:39:00 +01003176 private static final HashMap<ClassLoader,HashMap<String,Parcelable.Creator<?>>>
3177 mCreators = new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003178
Narayan Kamathb34a4612014-01-23 14:17:11 +00003179 /** @hide for internal use only. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003180 static protected final Parcel obtain(int obj) {
Ashok Bhat8ab665d2014-01-22 16:00:20 +00003181 throw new UnsupportedOperationException();
3182 }
3183
3184 /** @hide */
3185 static protected final Parcel obtain(long obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003186 final Parcel[] pool = sHolderPool;
3187 synchronized (pool) {
3188 Parcel p;
3189 for (int i=0; i<POOL_SIZE; i++) {
3190 p = pool[i];
3191 if (p != null) {
3192 pool[i] = null;
3193 if (DEBUG_RECYCLE) {
3194 p.mStack = new RuntimeException();
3195 }
3196 p.init(obj);
3197 return p;
3198 }
3199 }
3200 }
3201 return new Parcel(obj);
3202 }
3203
Ashok Bhat8ab665d2014-01-22 16:00:20 +00003204 private Parcel(long nativePtr) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003205 if (DEBUG_RECYCLE) {
3206 mStack = new RuntimeException();
3207 }
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003208 //Log.i(TAG, "Initializing obj=0x" + Integer.toHexString(obj), mStack);
Jeff Sharkey047238c2012-03-07 16:51:38 -08003209 init(nativePtr);
3210 }
3211
Ashok Bhat8ab665d2014-01-22 16:00:20 +00003212 private void init(long nativePtr) {
Jeff Sharkey047238c2012-03-07 16:51:38 -08003213 if (nativePtr != 0) {
3214 mNativePtr = nativePtr;
3215 mOwnsNativeParcelObject = false;
3216 } else {
3217 mNativePtr = nativeCreate();
3218 mOwnsNativeParcelObject = true;
3219 }
3220 }
3221
3222 private void freeBuffer() {
3223 if (mOwnsNativeParcelObject) {
Adrian Roos04505652015-10-22 16:12:01 -07003224 updateNativeSize(nativeFreeBuffer(mNativePtr));
Jeff Sharkey047238c2012-03-07 16:51:38 -08003225 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -07003226 mReadWriteHelper = ReadWriteHelper.DEFAULT;
Jeff Sharkey047238c2012-03-07 16:51:38 -08003227 }
3228
3229 private void destroy() {
3230 if (mNativePtr != 0) {
3231 if (mOwnsNativeParcelObject) {
3232 nativeDestroy(mNativePtr);
Adrian Roos04505652015-10-22 16:12:01 -07003233 updateNativeSize(0);
Jeff Sharkey047238c2012-03-07 16:51:38 -08003234 }
3235 mNativePtr = 0;
3236 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -07003237 mReadWriteHelper = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 }
3239
3240 @Override
3241 protected void finalize() throws Throwable {
3242 if (DEBUG_RECYCLE) {
3243 if (mStack != null) {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003244 Log.w(TAG, "Client did not call Parcel.recycle()", mStack);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003245 }
3246 }
3247 destroy();
3248 }
3249
Jake Whartonb1f474c2018-05-30 15:11:13 -04003250 /* package */ void readMapInternal(@NonNull Map outVal, int N,
3251 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 while (N > 0) {
3253 Object key = readValue(loader);
3254 Object value = readValue(loader);
3255 outVal.put(key, value);
3256 N--;
3257 }
3258 }
3259
Jake Whartonb1f474c2018-05-30 15:11:13 -04003260 /* package */ void readArrayMapInternal(@NonNull ArrayMap outVal, int N,
3261 @Nullable ClassLoader loader) {
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003262 if (DEBUG_ARRAY_MAP) {
3263 RuntimeException here = new RuntimeException("here");
3264 here.fillInStackTrace();
3265 Log.d(TAG, "Reading " + N + " ArrayMap entries", here);
3266 }
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003267 int startPos;
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003268 while (N > 0) {
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003269 if (DEBUG_ARRAY_MAP) startPos = dataPosition();
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003270 String key = readString();
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003271 Object value = readValue(loader);
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003272 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read #" + (N-1) + " "
3273 + (dataPosition()-startPos) + " bytes: key=0x"
3274 + Integer.toHexString((key != null ? key.hashCode() : 0)) + " " + key);
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003275 outVal.append(key, value);
3276 N--;
3277 }
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003278 outVal.validate();
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003279 }
3280
Jake Whartonb1f474c2018-05-30 15:11:13 -04003281 /* package */ void readArrayMapSafelyInternal(@NonNull ArrayMap outVal, int N,
3282 @Nullable ClassLoader loader) {
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003283 if (DEBUG_ARRAY_MAP) {
3284 RuntimeException here = new RuntimeException("here");
3285 here.fillInStackTrace();
3286 Log.d(TAG, "Reading safely " + N + " ArrayMap entries", here);
3287 }
3288 while (N > 0) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003289 String key = readString();
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003290 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read safe #" + (N-1) + ": key=0x"
3291 + (key != null ? key.hashCode() : 0) + " " + key);
3292 Object value = readValue(loader);
3293 outVal.put(key, value);
3294 N--;
3295 }
3296 }
3297
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003298 /**
3299 * @hide For testing only.
3300 */
Andrei Onea24ec3212019-03-15 17:35:05 +00003301 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04003302 public void readArrayMap(@NonNull ArrayMap outVal, @Nullable ClassLoader loader) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003303 final int N = readInt();
3304 if (N < 0) {
3305 return;
3306 }
3307 readArrayMapInternal(outVal, N, loader);
3308 }
3309
Svet Ganovddb94882016-06-23 19:55:24 -07003310 /**
3311 * Reads an array set.
3312 *
3313 * @param loader The class loader to use.
3314 *
3315 * @hide
3316 */
Andrei Onea24ec3212019-03-15 17:35:05 +00003317 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04003318 public @Nullable ArraySet<? extends Object> readArraySet(@Nullable ClassLoader loader) {
Svet Ganovddb94882016-06-23 19:55:24 -07003319 final int size = readInt();
3320 if (size < 0) {
3321 return null;
3322 }
3323 ArraySet<Object> result = new ArraySet<>(size);
3324 for (int i = 0; i < size; i++) {
3325 Object value = readValue(loader);
3326 result.append(value);
3327 }
3328 return result;
3329 }
3330
Jake Whartonb1f474c2018-05-30 15:11:13 -04003331 private void readListInternal(@NonNull List outVal, int N,
3332 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003333 while (N > 0) {
3334 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003335 //Log.d(TAG, "Unmarshalling value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003336 outVal.add(value);
3337 N--;
3338 }
3339 }
3340
Jake Whartonb1f474c2018-05-30 15:11:13 -04003341 private void readArrayInternal(@NonNull Object[] outVal, int N,
3342 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003343 for (int i = 0; i < N; i++) {
3344 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003345 //Log.d(TAG, "Unmarshalling value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003346 outVal[i] = value;
3347 }
3348 }
3349
Jake Whartonb1f474c2018-05-30 15:11:13 -04003350 private void readSparseArrayInternal(@NonNull SparseArray outVal, int N,
3351 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003352 while (N > 0) {
3353 int key = readInt();
3354 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003355 //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003356 outVal.append(key, value);
3357 N--;
3358 }
3359 }
3360
3361
Jake Whartonb1f474c2018-05-30 15:11:13 -04003362 private void readSparseBooleanArrayInternal(@NonNull SparseBooleanArray outVal, int N) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003363 while (N > 0) {
3364 int key = readInt();
3365 boolean value = this.readByte() == 1;
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003366 //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003367 outVal.append(key, value);
3368 N--;
3369 }
3370 }
Dan Sandler5ce04302015-04-09 23:50:15 -04003371
Jake Whartonb1f474c2018-05-30 15:11:13 -04003372 private void readSparseIntArrayInternal(@NonNull SparseIntArray outVal, int N) {
Adam Lesinski4e862812016-11-21 16:02:24 -08003373 while (N > 0) {
3374 int key = readInt();
3375 int value = readInt();
3376 outVal.append(key, value);
3377 N--;
3378 }
3379 }
3380
Dan Sandler5ce04302015-04-09 23:50:15 -04003381 /**
3382 * @hide For testing
3383 */
3384 public long getBlobAshmemSize() {
3385 return nativeGetBlobAshmemSize(mNativePtr);
3386 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003387}