blob: 50487e9e7a9918699bce720ad574058e1d7af3fe [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);
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700308 static native void nativeWriteString(long nativePtr, String val);
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000309 private static native void nativeWriteStrongBinder(long nativePtr, IBinder val);
Adrian Roos04505652015-10-22 16:12:01 -0700310 private static native long nativeWriteFileDescriptor(long nativePtr, FileDescriptor val);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800311
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000312 private static native byte[] nativeCreateByteArray(long nativePtr);
Jocelyn Dang46100442017-05-05 15:40:49 -0700313 private static native boolean nativeReadByteArray(long nativePtr, byte[] dest, int destLen);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700314 private static native byte[] nativeReadBlob(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700315 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000316 private static native int nativeReadInt(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700317 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000318 private static native long nativeReadLong(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700319 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000320 private static native float nativeReadFloat(long nativePtr);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700321 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000322 private static native double nativeReadDouble(long nativePtr);
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700323 static native String nativeReadString(long nativePtr);
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000324 private static native IBinder nativeReadStrongBinder(long nativePtr);
325 private static native FileDescriptor nativeReadFileDescriptor(long nativePtr);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800326
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000327 private static native long nativeCreate();
Adrian Roos04505652015-10-22 16:12:01 -0700328 private static native long nativeFreeBuffer(long nativePtr);
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000329 private static native void nativeDestroy(long nativePtr);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800330
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000331 private static native byte[] nativeMarshall(long nativePtr);
Adrian Roos04505652015-10-22 16:12:01 -0700332 private static native long nativeUnmarshall(
John Spurlocke0852362015-02-04 15:47:40 -0500333 long nativePtr, byte[] data, int offset, int length);
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700334 private static native int nativeCompareData(long thisNativePtr, long otherNativePtr);
Adrian Roos04505652015-10-22 16:12:01 -0700335 private static native long nativeAppendFrom(
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000336 long thisNativePtr, long otherNativePtr, int offset, int length);
Makoto Onukib148b6c2017-06-27 13:38:38 -0700337 @CriticalNative
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000338 private static native boolean nativeHasFileDescriptors(long nativePtr);
339 private static native void nativeWriteInterfaceToken(long nativePtr, String interfaceName);
340 private static native void nativeEnforceInterface(long nativePtr, String interfaceName);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800341
Olivier Gaillardbab444a2019-01-30 17:11:40 +0000342 @CriticalNative
343 private static native boolean nativeReplaceCallingWorkSourceUid(
344 long nativePtr, int workSourceUid);
345 @CriticalNative
346 private static native int nativeReadCallingWorkSourceUid(long nativePtr);
347
Fyodor Kupolova81b8c02017-11-13 15:51:03 -0800348 /** Last time exception with a stack trace was written */
349 private static volatile long sLastWriteExceptionStackTrace;
350 /** Used for throttling of writing stack trace, which is costly */
351 private static final int WRITE_EXCEPTION_STACK_TRACE_THRESHOLD_MS = 1000;
352
Makoto Onukib148b6c2017-06-27 13:38:38 -0700353 @CriticalNative
Dan Sandleraa861662015-04-21 10:24:32 -0400354 private static native long nativeGetBlobAshmemSize(long nativePtr);
Dan Sandler5ce04302015-04-09 23:50:15 -0400355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 public final static Parcelable.Creator<String> STRING_CREATOR
357 = new Parcelable.Creator<String>() {
358 public String createFromParcel(Parcel source) {
359 return source.readString();
360 }
361 public String[] newArray(int size) {
362 return new String[size];
363 }
364 };
365
366 /**
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700367 * @hide
368 */
369 public static class ReadWriteHelper {
370 public static final ReadWriteHelper DEFAULT = new ReadWriteHelper();
371
372 /**
373 * Called when writing a string to a parcel. Subclasses wanting to write a string
374 * must use {@link #writeStringNoHelper(String)} to avoid
375 * infinity recursive calls.
376 */
377 public void writeString(Parcel p, String s) {
378 nativeWriteString(p.mNativePtr, s);
379 }
380
381 /**
382 * Called when reading a string to a parcel. Subclasses wanting to read a string
383 * must use {@link #readStringNoHelper()} to avoid
384 * infinity recursive calls.
385 */
386 public String readString(Parcel p) {
387 return nativeReadString(p.mNativePtr);
388 }
389 }
390
391 private ReadWriteHelper mReadWriteHelper = ReadWriteHelper.DEFAULT;
392
393 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 * Retrieve a new Parcel object from the pool.
395 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400396 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 public static Parcel obtain() {
398 final Parcel[] pool = sOwnedPool;
399 synchronized (pool) {
400 Parcel p;
401 for (int i=0; i<POOL_SIZE; i++) {
402 p = pool[i];
403 if (p != null) {
404 pool[i] = null;
405 if (DEBUG_RECYCLE) {
406 p.mStack = new RuntimeException();
407 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700408 p.mReadWriteHelper = ReadWriteHelper.DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 return p;
410 }
411 }
412 }
413 return new Parcel(0);
414 }
415
416 /**
417 * Put a Parcel object back into the pool. You must not touch
418 * the object after this call.
419 */
420 public final void recycle() {
421 if (DEBUG_RECYCLE) mStack = null;
422 freeBuffer();
Jeff Sharkey047238c2012-03-07 16:51:38 -0800423
424 final Parcel[] pool;
425 if (mOwnsNativeParcelObject) {
426 pool = sOwnedPool;
427 } else {
428 mNativePtr = 0;
429 pool = sHolderPool;
430 }
431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 synchronized (pool) {
433 for (int i=0; i<POOL_SIZE; i++) {
434 if (pool[i] == null) {
435 pool[i] = this;
436 return;
437 }
438 }
439 }
440 }
441
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700442 /**
443 * Set a {@link ReadWriteHelper}, which can be used to avoid having duplicate strings, for
444 * example.
445 *
446 * @hide
447 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400448 public void setReadWriteHelper(@Nullable ReadWriteHelper helper) {
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700449 mReadWriteHelper = helper != null ? helper : ReadWriteHelper.DEFAULT;
450 }
451
452 /**
453 * @return whether this parcel has a {@link ReadWriteHelper}.
454 *
455 * @hide
456 */
457 public boolean hasReadWriteHelper() {
458 return (mReadWriteHelper != null) && (mReadWriteHelper != ReadWriteHelper.DEFAULT);
459 }
460
Dianne Hackbornfabb70b2014-11-11 12:22:36 -0800461 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000462 @UnsupportedAppUsage
Dianne Hackbornfabb70b2014-11-11 12:22:36 -0800463 public static native long getGlobalAllocSize();
464
465 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000466 @UnsupportedAppUsage
Dianne Hackbornfabb70b2014-11-11 12:22:36 -0800467 public static native long getGlobalAllocCount();
468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 /**
470 * Returns the total amount of data contained in the parcel.
471 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800472 public final int dataSize() {
473 return nativeDataSize(mNativePtr);
474 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475
476 /**
477 * Returns the amount of data remaining to be read from the
478 * parcel. That is, {@link #dataSize}-{@link #dataPosition}.
479 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800480 public final int dataAvail() {
481 return nativeDataAvail(mNativePtr);
482 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483
484 /**
485 * Returns the current position in the parcel data. Never
486 * more than {@link #dataSize}.
487 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800488 public final int dataPosition() {
489 return nativeDataPosition(mNativePtr);
490 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491
492 /**
493 * Returns the total amount of space in the parcel. This is always
494 * >= {@link #dataSize}. The difference between it and dataSize() is the
495 * amount of room left until the parcel needs to re-allocate its
496 * data buffer.
497 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800498 public final int dataCapacity() {
499 return nativeDataCapacity(mNativePtr);
500 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501
502 /**
503 * Change the amount of data in the parcel. Can be either smaller or
504 * larger than the current size. If larger than the current capacity,
505 * more memory will be allocated.
506 *
507 * @param size The new number of bytes in the Parcel.
508 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800509 public final void setDataSize(int size) {
Michael Wachenschwanz138bebf2017-05-18 22:09:18 +0000510 updateNativeSize(nativeSetDataSize(mNativePtr, size));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800511 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512
513 /**
514 * Move the current read/write position in the parcel.
515 * @param pos New offset in the parcel; must be between 0 and
516 * {@link #dataSize}.
517 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800518 public final void setDataPosition(int pos) {
519 nativeSetDataPosition(mNativePtr, pos);
520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521
522 /**
523 * Change the capacity (current available space) of the parcel.
524 *
525 * @param size The new capacity of the parcel, in bytes. Can not be
526 * less than {@link #dataSize} -- that is, you can not drop existing data
527 * with this method.
528 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800529 public final void setDataCapacity(int size) {
530 nativeSetDataCapacity(mNativePtr, size);
531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400533 /** @hide */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800534 public final boolean pushAllowFds(boolean allowFds) {
535 return nativePushAllowFds(mNativePtr, allowFds);
536 }
Dianne Hackbornc04db7e2011-10-03 21:09:35 -0700537
538 /** @hide */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800539 public final void restoreAllowFds(boolean lastValue) {
540 nativeRestoreAllowFds(mNativePtr, lastValue);
541 }
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 /**
544 * Returns the raw bytes of the parcel.
545 *
546 * <p class="note">The data you retrieve here <strong>must not</strong>
547 * be placed in any kind of persistent storage (on local disk, across
548 * a network, etc). For that, you should use standard serialization
549 * or another kind of general serialization mechanism. The Parcel
550 * marshalled representation is highly optimized for local IPC, and as
551 * such does not attempt to maintain compatibility with data created
552 * in different versions of the platform.
553 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800554 public final byte[] marshall() {
555 return nativeMarshall(mNativePtr);
556 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557
558 /**
559 * Set the bytes in data to be the raw bytes of this Parcel.
560 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400561 public final void unmarshall(@NonNull byte[] data, int offset, int length) {
Adrian Roos04505652015-10-22 16:12:01 -0700562 updateNativeSize(nativeUnmarshall(mNativePtr, data, offset, length));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800563 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564
Jeff Sharkey047238c2012-03-07 16:51:38 -0800565 public final void appendFrom(Parcel parcel, int offset, int length) {
Adrian Roos04505652015-10-22 16:12:01 -0700566 updateNativeSize(nativeAppendFrom(mNativePtr, parcel.mNativePtr, offset, length));
Jeff Sharkey047238c2012-03-07 16:51:38 -0800567 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700569 /** @hide */
570 public final int compareData(Parcel other) {
571 return nativeCompareData(mNativePtr, other.mNativePtr);
572 }
573
Dianne Hackborn98305522017-05-05 17:53:53 -0700574 /** @hide */
575 public final void setClassCookie(Class clz, Object cookie) {
576 if (mClassCookies == null) {
577 mClassCookies = new ArrayMap<>();
578 }
579 mClassCookies.put(clz, cookie);
580 }
581
582 /** @hide */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400583 @Nullable
Dianne Hackborn98305522017-05-05 17:53:53 -0700584 public final Object getClassCookie(Class clz) {
585 return mClassCookies != null ? mClassCookies.get(clz) : null;
586 }
587
588 /** @hide */
589 public final void adoptClassCookies(Parcel from) {
590 mClassCookies = from.mClassCookies;
591 }
592
Adrian Roosfb921842017-10-26 14:49:56 +0200593 /** @hide */
594 public Map<Class, Object> copyClassCookies() {
595 return new ArrayMap<>(mClassCookies);
596 }
597
598 /** @hide */
599 public void putClassCookies(Map<Class, Object> cookies) {
600 if (cookies == null) {
601 return;
602 }
603 if (mClassCookies == null) {
604 mClassCookies = new ArrayMap<>();
605 }
606 mClassCookies.putAll(cookies);
607 }
608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 /**
610 * Report whether the parcel contains any marshalled file descriptors.
611 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800612 public final boolean hasFileDescriptors() {
613 return nativeHasFileDescriptors(mNativePtr);
614 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615
616 /**
617 * Store or read an IBinder interface token in the parcel at the current
618 * {@link #dataPosition}. This is used to validate that the marshalled
619 * transaction is intended for the target interface.
620 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800621 public final void writeInterfaceToken(String interfaceName) {
622 nativeWriteInterfaceToken(mNativePtr, interfaceName);
623 }
624
625 public final void enforceInterface(String interfaceName) {
626 nativeEnforceInterface(mNativePtr, interfaceName);
627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628
629 /**
Olivier Gaillardbab444a2019-01-30 17:11:40 +0000630 * Writes the work source uid to the request headers.
631 *
632 * <p>It requires the headers to have been written/read already to replace the work source.
633 *
634 * @return true if the request headers have been updated.
635 *
636 * @hide
637 */
638 public boolean replaceCallingWorkSourceUid(int workSourceUid) {
639 return nativeReplaceCallingWorkSourceUid(mNativePtr, workSourceUid);
640 }
641
642 /**
643 * Reads the work source uid from the request headers.
644 *
645 * <p>Unlike other read methods, this method does not read the parcel at the current
646 * {@link #dataPosition}. It will set the {@link #dataPosition} before the read and restore the
647 * position after reading the request header.
648 *
649 * @return the work source uid or {@link Binder#UNSET_WORKSOURCE} if headers have not been
650 * written/parsed yet.
651 *
652 * @hide
653 */
654 public int readCallingWorkSourceUid() {
655 return nativeReadCallingWorkSourceUid(mNativePtr);
656 }
657
658 /**
Elliott Hughesa28b83e2011-02-28 14:26:13 -0800659 * Write a byte array into the parcel at the current {@link #dataPosition},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 * growing {@link #dataCapacity} if needed.
661 * @param b Bytes to place into the parcel.
662 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400663 public final void writeByteArray(@Nullable byte[] b) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 writeByteArray(b, 0, (b != null) ? b.length : 0);
665 }
666
667 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900668 * Write a byte array into the parcel at the current {@link #dataPosition},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 * growing {@link #dataCapacity} if needed.
670 * @param b Bytes to place into the parcel.
671 * @param offset Index of first byte to be written.
672 * @param len Number of bytes to write.
673 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400674 public final void writeByteArray(@Nullable byte[] b, int offset, int len) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 if (b == null) {
676 writeInt(-1);
677 return;
678 }
Pete Gillin60f55a252018-05-10 15:40:32 +0100679 ArrayUtils.throwsIfOutOfBounds(b.length, offset, len);
Jeff Sharkey047238c2012-03-07 16:51:38 -0800680 nativeWriteByteArray(mNativePtr, b, offset, len);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 }
682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 /**
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700684 * Write a blob of data into the parcel at the current {@link #dataPosition},
685 * growing {@link #dataCapacity} if needed.
686 * @param b Bytes to place into the parcel.
687 * {@hide}
Sandeep Siddhartha39c12fa2014-07-25 18:37:29 -0700688 * {@SystemApi}
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700689 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000690 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -0400691 public final void writeBlob(@Nullable byte[] b) {
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500692 writeBlob(b, 0, (b != null) ? b.length : 0);
693 }
694
695 /**
696 * Write a blob of data into the parcel at the current {@link #dataPosition},
697 * growing {@link #dataCapacity} if needed.
698 * @param b Bytes to place into the parcel.
699 * @param offset Index of first byte to be written.
700 * @param len Number of bytes to write.
701 * {@hide}
702 * {@SystemApi}
703 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400704 public final void writeBlob(@Nullable byte[] b, int offset, int len) {
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500705 if (b == null) {
706 writeInt(-1);
707 return;
708 }
Pete Gillin60f55a252018-05-10 15:40:32 +0100709 ArrayUtils.throwsIfOutOfBounds(b.length, offset, len);
Dan Sandlerb9f7aac32015-03-04 13:08:49 -0500710 nativeWriteBlob(mNativePtr, b, offset, len);
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -0700711 }
712
713 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 * Write an integer value into the parcel at the current dataPosition(),
715 * growing dataCapacity() if needed.
716 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800717 public final void writeInt(int val) {
718 nativeWriteInt(mNativePtr, val);
719 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720
721 /**
722 * Write a long integer value into the parcel at the current dataPosition(),
723 * growing dataCapacity() if needed.
724 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800725 public final void writeLong(long val) {
726 nativeWriteLong(mNativePtr, val);
727 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728
729 /**
730 * Write a floating point value into the parcel at the current
731 * dataPosition(), growing dataCapacity() if needed.
732 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800733 public final void writeFloat(float val) {
734 nativeWriteFloat(mNativePtr, val);
735 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736
737 /**
738 * Write a double precision floating point value into the parcel at the
739 * current dataPosition(), growing dataCapacity() if needed.
740 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800741 public final void writeDouble(double val) {
742 nativeWriteDouble(mNativePtr, val);
743 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744
745 /**
746 * Write a string value into the parcel at the current dataPosition(),
747 * growing dataCapacity() if needed.
748 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400749 public final void writeString(@Nullable String val) {
Makoto Onuki4501c61d2017-07-27 15:56:40 -0700750 mReadWriteHelper.writeString(this, val);
751 }
752
753 /**
754 * Write a string without going though a {@link ReadWriteHelper}. Subclasses of
755 * {@link ReadWriteHelper} must use this method instead of {@link #writeString} to avoid
756 * infinity recursive calls.
757 *
758 * @hide
759 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400760 public void writeStringNoHelper(@Nullable String val) {
Jeff Sharkey047238c2012-03-07 16:51:38 -0800761 nativeWriteString(mNativePtr, val);
762 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763
Jake Wharton2ffa4312018-04-23 17:47:14 -0400764 /**
765 * Write a boolean value into the parcel at the current dataPosition(),
766 * growing dataCapacity() if needed.
767 *
768 * <p>Note: This method currently delegates to writeInt with a value of 1 or 0
769 * for true or false, respectively, but may change in the future.
770 */
Eugene Susla36e866b2017-02-23 18:24:39 -0800771 public final void writeBoolean(boolean val) {
772 writeInt(val ? 1 : 0);
773 }
774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +0000776 * Write a CharSequence value into the parcel at the current dataPosition(),
777 * growing dataCapacity() if needed.
778 * @hide
779 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000780 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -0400781 public final void writeCharSequence(@Nullable CharSequence val) {
Bjorn Bringert08bbffb2010-02-25 11:16:22 +0000782 TextUtils.writeToParcel(val, this, 0);
783 }
784
785 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 * Write an object into the parcel at the current dataPosition(),
787 * growing dataCapacity() if needed.
788 */
Jeff Sharkey047238c2012-03-07 16:51:38 -0800789 public final void writeStrongBinder(IBinder val) {
790 nativeWriteStrongBinder(mNativePtr, val);
791 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792
793 /**
794 * Write an object into the parcel at the current dataPosition(),
795 * growing dataCapacity() if needed.
796 */
797 public final void writeStrongInterface(IInterface val) {
798 writeStrongBinder(val == null ? null : val.asBinder());
799 }
800
801 /**
802 * Write a FileDescriptor into the parcel at the current dataPosition(),
803 * growing dataCapacity() if needed.
Dan Egnorb3e4ef32010-07-20 09:03:35 -0700804 *
805 * <p class="caution">The file descriptor will not be closed, which may
806 * result in file descriptor leaks when objects are returned from Binder
807 * calls. Use {@link ParcelFileDescriptor#writeToParcel} instead, which
808 * accepts contextual flags and will close the original file descriptor
809 * if {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE} is set.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400811 public final void writeFileDescriptor(@NonNull FileDescriptor val) {
Adrian Roos04505652015-10-22 16:12:01 -0700812 updateNativeSize(nativeWriteFileDescriptor(mNativePtr, val));
813 }
814
815 private void updateNativeSize(long newNativeSize) {
816 if (mOwnsNativeParcelObject) {
817 if (newNativeSize > Integer.MAX_VALUE) {
818 newNativeSize = Integer.MAX_VALUE;
819 }
820 if (newNativeSize != mNativeSize) {
821 int delta = (int) (newNativeSize - mNativeSize);
822 if (delta > 0) {
823 VMRuntime.getRuntime().registerNativeAllocation(delta);
824 } else {
825 VMRuntime.getRuntime().registerNativeFree(-delta);
826 }
827 mNativeSize = newNativeSize;
828 }
829 }
Jeff Sharkey047238c2012-03-07 16:51:38 -0800830 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831
832 /**
Casey Dahlin2f974b22015-11-05 12:19:13 -0800833 * {@hide}
834 * This will be the new name for writeFileDescriptor, for consistency.
835 **/
Jake Whartonb1f474c2018-05-30 15:11:13 -0400836 public final void writeRawFileDescriptor(@NonNull FileDescriptor val) {
Casey Dahlin2f974b22015-11-05 12:19:13 -0800837 nativeWriteFileDescriptor(mNativePtr, val);
838 }
839
840 /**
841 * {@hide}
842 * Write an array of FileDescriptor objects into the Parcel.
843 *
844 * @param value The array of objects to be written.
845 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400846 public final void writeRawFileDescriptorArray(@Nullable FileDescriptor[] value) {
Casey Dahlin2f974b22015-11-05 12:19:13 -0800847 if (value != null) {
848 int N = value.length;
849 writeInt(N);
850 for (int i=0; i<N; i++) {
851 writeRawFileDescriptor(value[i]);
852 }
853 } else {
854 writeInt(-1);
855 }
856 }
857
858 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900859 * Write a byte value into the parcel at the current dataPosition(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 * growing dataCapacity() if needed.
Jake Wharton2ffa4312018-04-23 17:47:14 -0400861 *
862 * <p>Note: This method currently delegates to writeInt but may change in
863 * the future.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 */
865 public final void writeByte(byte val) {
866 writeInt(val);
867 }
868
869 /**
870 * Please use {@link #writeBundle} instead. Flattens a Map into the parcel
871 * at the current dataPosition(),
872 * growing dataCapacity() if needed. The Map keys must be String objects.
873 * The Map values are written using {@link #writeValue} and must follow
874 * the specification there.
Samuel Tana8036662015-11-23 14:36:00 -0800875 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 * <p>It is strongly recommended to use {@link #writeBundle} instead of
877 * this method, since the Bundle class provides a type-safe API that
878 * allows you to avoid mysterious type errors at the point of marshalling.
879 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400880 public final void writeMap(@Nullable Map val) {
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700881 writeMapInternal((Map<String, Object>) val);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 }
883
884 /**
885 * Flatten a Map into the parcel at the current dataPosition(),
886 * growing dataCapacity() if needed. The Map keys must be String objects.
887 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400888 /* package */ void writeMapInternal(@Nullable Map<String,Object> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 if (val == null) {
890 writeInt(-1);
891 return;
892 }
893 Set<Map.Entry<String,Object>> entries = val.entrySet();
Michael Wachenschwanz80b9d692018-08-24 21:50:35 -0700894 int size = entries.size();
895 writeInt(size);
896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 for (Map.Entry<String,Object> e : entries) {
898 writeValue(e.getKey());
899 writeValue(e.getValue());
Michael Wachenschwanz80b9d692018-08-24 21:50:35 -0700900 size--;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 }
Michael Wachenschwanz80b9d692018-08-24 21:50:35 -0700902
903 if (size != 0) {
904 throw new BadParcelableException("Map size does not match number of entries!");
905 }
906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 }
908
909 /**
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700910 * Flatten an ArrayMap into the parcel at the current dataPosition(),
911 * growing dataCapacity() if needed. The Map keys must be String objects.
912 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400913 /* package */ void writeArrayMapInternal(@Nullable ArrayMap<String, Object> val) {
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700914 if (val == null) {
915 writeInt(-1);
916 return;
917 }
Samuel Tan3cefe6a2015-12-14 13:29:17 -0800918 // Keep the format of this Parcel in sync with writeToParcelInner() in
919 // frameworks/native/libs/binder/PersistableBundle.cpp.
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700920 final int N = val.size();
921 writeInt(N);
Dianne Hackborne784d1e2013-09-20 18:13:52 -0700922 if (DEBUG_ARRAY_MAP) {
923 RuntimeException here = new RuntimeException("here");
924 here.fillInStackTrace();
925 Log.d(TAG, "Writing " + N + " ArrayMap entries", here);
926 }
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700927 int startPos;
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700928 for (int i=0; i<N; i++) {
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700929 if (DEBUG_ARRAY_MAP) startPos = dataPosition();
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700930 writeString(val.keyAt(i));
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700931 writeValue(val.valueAt(i));
Dianne Hackborn8aee64d2013-10-25 10:41:50 -0700932 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Write #" + i + " "
933 + (dataPosition()-startPos) + " bytes: key=0x"
934 + Integer.toHexString(val.keyAt(i) != null ? val.keyAt(i).hashCode() : 0)
935 + " " + val.keyAt(i));
Dianne Hackbornb87655b2013-07-17 19:06:22 -0700936 }
937 }
938
939 /**
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700940 * @hide For testing only.
941 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000942 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -0400943 public void writeArrayMap(@Nullable ArrayMap<String, Object> val) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -0700944 writeArrayMapInternal(val);
945 }
946
947 /**
Svet Ganov8455ba22019-01-02 13:05:56 -0800948 * Flatten an {@link ArrayMap} with string keys containing a particular object
949 * type into the parcel at the current dataPosition() and growing dataCapacity()
950 * if needed. The type of the objects in the array must be one that implements
951 * Parcelable. Only the raw data of the objects is written and not their type,
952 * so you must use the corresponding {@link #createTypedArrayMap(Parcelable.Creator)}
953 *
954 * @param val The map of objects to be written.
955 * @param parcelableFlags The parcelable flags to use.
956 *
957 * @see #createTypedArrayMap(Parcelable.Creator)
958 * @see Parcelable
959 */
960 public <T extends Parcelable> void writeTypedArrayMap(@Nullable ArrayMap<String, T> val,
961 int parcelableFlags) {
962 if (val == null) {
963 writeInt(-1);
964 return;
965 }
966 final int count = val.size();
967 writeInt(count);
968 for (int i = 0; i < count; i++) {
969 writeString(val.keyAt(i));
970 writeTypedObject(val.valueAt(i), parcelableFlags);
971 }
972 }
973
974 /**
Svet Ganovddb94882016-06-23 19:55:24 -0700975 * Write an array set to the parcel.
976 *
977 * @param val The array set to write.
978 *
979 * @hide
980 */
Andrei Onea24ec3212019-03-15 17:35:05 +0000981 @UnsupportedAppUsage
Svet Ganovddb94882016-06-23 19:55:24 -0700982 public void writeArraySet(@Nullable ArraySet<? extends Object> val) {
983 final int size = (val != null) ? val.size() : -1;
984 writeInt(size);
985 for (int i = 0; i < size; i++) {
986 writeValue(val.valueAt(i));
987 }
988 }
989
990 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 * Flatten a Bundle into the parcel at the current dataPosition(),
992 * growing dataCapacity() if needed.
993 */
Jake Whartonb1f474c2018-05-30 15:11:13 -0400994 public final void writeBundle(@Nullable Bundle val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 if (val == null) {
996 writeInt(-1);
997 return;
998 }
999
Dianne Hackborn6aff9052009-05-22 13:20:23 -07001000 val.writeToParcel(this, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 }
1002
1003 /**
Craig Mautner719e6b12014-04-04 20:29:41 -07001004 * Flatten a PersistableBundle into the parcel at the current dataPosition(),
1005 * growing dataCapacity() if needed.
1006 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001007 public final void writePersistableBundle(@Nullable PersistableBundle val) {
Craig Mautner719e6b12014-04-04 20:29:41 -07001008 if (val == null) {
1009 writeInt(-1);
1010 return;
1011 }
1012
1013 val.writeToParcel(this, 0);
1014 }
1015
1016 /**
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001017 * Flatten a Size into the parcel at the current dataPosition(),
1018 * growing dataCapacity() if needed.
1019 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001020 public final void writeSize(@NonNull Size val) {
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001021 writeInt(val.getWidth());
1022 writeInt(val.getHeight());
1023 }
1024
1025 /**
1026 * Flatten a SizeF into the parcel at the current dataPosition(),
1027 * growing dataCapacity() if needed.
1028 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001029 public final void writeSizeF(@NonNull SizeF val) {
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001030 writeFloat(val.getWidth());
1031 writeFloat(val.getHeight());
1032 }
1033
1034 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 * Flatten a List into the parcel at the current dataPosition(), growing
1036 * dataCapacity() if needed. The List values are written using
1037 * {@link #writeValue} and must follow the specification there.
1038 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001039 public final void writeList(@Nullable List val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 if (val == null) {
1041 writeInt(-1);
1042 return;
1043 }
1044 int N = val.size();
1045 int i=0;
1046 writeInt(N);
1047 while (i < N) {
1048 writeValue(val.get(i));
1049 i++;
1050 }
1051 }
1052
1053 /**
1054 * Flatten an Object array into the parcel at the current dataPosition(),
1055 * growing dataCapacity() if needed. The array values are written using
1056 * {@link #writeValue} and must follow the specification there.
1057 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001058 public final void writeArray(@Nullable Object[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 if (val == null) {
1060 writeInt(-1);
1061 return;
1062 }
1063 int N = val.length;
1064 int i=0;
1065 writeInt(N);
1066 while (i < N) {
1067 writeValue(val[i]);
1068 i++;
1069 }
1070 }
1071
1072 /**
1073 * Flatten a generic SparseArray into the parcel at the current
1074 * dataPosition(), growing dataCapacity() if needed. The SparseArray
1075 * values are written using {@link #writeValue} and must follow the
1076 * specification there.
1077 */
Svet Ganov8455ba22019-01-02 13:05:56 -08001078 public final <T> void writeSparseArray(@Nullable SparseArray<T> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 if (val == null) {
1080 writeInt(-1);
1081 return;
1082 }
1083 int N = val.size();
1084 writeInt(N);
1085 int i=0;
1086 while (i < N) {
1087 writeInt(val.keyAt(i));
1088 writeValue(val.valueAt(i));
1089 i++;
1090 }
1091 }
1092
Jake Whartonb1f474c2018-05-30 15:11:13 -04001093 public final void writeSparseBooleanArray(@Nullable SparseBooleanArray val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 if (val == null) {
1095 writeInt(-1);
1096 return;
1097 }
1098 int N = val.size();
1099 writeInt(N);
1100 int i=0;
1101 while (i < N) {
1102 writeInt(val.keyAt(i));
1103 writeByte((byte)(val.valueAt(i) ? 1 : 0));
1104 i++;
1105 }
1106 }
1107
Adam Lesinski205656d2017-03-23 13:38:26 -07001108 /**
1109 * @hide
1110 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001111 public final void writeSparseIntArray(@Nullable SparseIntArray val) {
Adam Lesinski4e862812016-11-21 16:02:24 -08001112 if (val == null) {
1113 writeInt(-1);
1114 return;
1115 }
1116 int N = val.size();
1117 writeInt(N);
1118 int i=0;
1119 while (i < N) {
1120 writeInt(val.keyAt(i));
1121 writeInt(val.valueAt(i));
1122 i++;
1123 }
1124 }
1125
Jake Whartonb1f474c2018-05-30 15:11:13 -04001126 public final void writeBooleanArray(@Nullable boolean[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 if (val != null) {
1128 int N = val.length;
1129 writeInt(N);
1130 for (int i=0; i<N; i++) {
1131 writeInt(val[i] ? 1 : 0);
1132 }
1133 } else {
1134 writeInt(-1);
1135 }
1136 }
1137
Jake Whartonb1f474c2018-05-30 15:11:13 -04001138 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 public final boolean[] createBooleanArray() {
1140 int N = readInt();
1141 // >>2 as a fast divide-by-4 works in the create*Array() functions
1142 // because dataAvail() will never return a negative number. 4 is
1143 // the size of a stored boolean in the stream.
1144 if (N >= 0 && N <= (dataAvail() >> 2)) {
1145 boolean[] val = new boolean[N];
1146 for (int i=0; i<N; i++) {
1147 val[i] = readInt() != 0;
1148 }
1149 return val;
1150 } else {
1151 return null;
1152 }
1153 }
1154
Jake Whartonb1f474c2018-05-30 15:11:13 -04001155 public final void readBooleanArray(@NonNull boolean[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 int N = readInt();
1157 if (N == val.length) {
1158 for (int i=0; i<N; i++) {
1159 val[i] = readInt() != 0;
1160 }
1161 } else {
1162 throw new RuntimeException("bad array lengths");
1163 }
1164 }
1165
Jake Whartonb1f474c2018-05-30 15:11:13 -04001166 public final void writeCharArray(@Nullable char[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 if (val != null) {
1168 int N = val.length;
1169 writeInt(N);
1170 for (int i=0; i<N; i++) {
1171 writeInt((int)val[i]);
1172 }
1173 } else {
1174 writeInt(-1);
1175 }
1176 }
1177
Jake Whartonb1f474c2018-05-30 15:11:13 -04001178 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 public final char[] createCharArray() {
1180 int N = readInt();
1181 if (N >= 0 && N <= (dataAvail() >> 2)) {
1182 char[] val = new char[N];
1183 for (int i=0; i<N; i++) {
1184 val[i] = (char)readInt();
1185 }
1186 return val;
1187 } else {
1188 return null;
1189 }
1190 }
1191
Jake Whartonb1f474c2018-05-30 15:11:13 -04001192 public final void readCharArray(@NonNull char[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 int N = readInt();
1194 if (N == val.length) {
1195 for (int i=0; i<N; i++) {
1196 val[i] = (char)readInt();
1197 }
1198 } else {
1199 throw new RuntimeException("bad array lengths");
1200 }
1201 }
1202
Jake Whartonb1f474c2018-05-30 15:11:13 -04001203 public final void writeIntArray(@Nullable int[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 if (val != null) {
1205 int N = val.length;
1206 writeInt(N);
1207 for (int i=0; i<N; i++) {
1208 writeInt(val[i]);
1209 }
1210 } else {
1211 writeInt(-1);
1212 }
1213 }
1214
Jake Whartonb1f474c2018-05-30 15:11:13 -04001215 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 public final int[] createIntArray() {
1217 int N = readInt();
1218 if (N >= 0 && N <= (dataAvail() >> 2)) {
1219 int[] val = new int[N];
1220 for (int i=0; i<N; i++) {
1221 val[i] = readInt();
1222 }
1223 return val;
1224 } else {
1225 return null;
1226 }
1227 }
1228
Jake Whartonb1f474c2018-05-30 15:11:13 -04001229 public final void readIntArray(@NonNull int[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 int N = readInt();
1231 if (N == val.length) {
1232 for (int i=0; i<N; i++) {
1233 val[i] = readInt();
1234 }
1235 } else {
1236 throw new RuntimeException("bad array lengths");
1237 }
1238 }
1239
Jake Whartonb1f474c2018-05-30 15:11:13 -04001240 public final void writeLongArray(@Nullable long[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 if (val != null) {
1242 int N = val.length;
1243 writeInt(N);
1244 for (int i=0; i<N; i++) {
1245 writeLong(val[i]);
1246 }
1247 } else {
1248 writeInt(-1);
1249 }
1250 }
1251
Jake Whartonb1f474c2018-05-30 15:11:13 -04001252 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 public final long[] createLongArray() {
1254 int N = readInt();
1255 // >>3 because stored longs are 64 bits
1256 if (N >= 0 && N <= (dataAvail() >> 3)) {
1257 long[] val = new long[N];
1258 for (int i=0; i<N; i++) {
1259 val[i] = readLong();
1260 }
1261 return val;
1262 } else {
1263 return null;
1264 }
1265 }
1266
Jake Whartonb1f474c2018-05-30 15:11:13 -04001267 public final void readLongArray(@NonNull long[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 int N = readInt();
1269 if (N == val.length) {
1270 for (int i=0; i<N; i++) {
1271 val[i] = readLong();
1272 }
1273 } else {
1274 throw new RuntimeException("bad array lengths");
1275 }
1276 }
1277
Jake Whartonb1f474c2018-05-30 15:11:13 -04001278 public final void writeFloatArray(@Nullable float[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 if (val != null) {
1280 int N = val.length;
1281 writeInt(N);
1282 for (int i=0; i<N; i++) {
1283 writeFloat(val[i]);
1284 }
1285 } else {
1286 writeInt(-1);
1287 }
1288 }
1289
Jake Whartonb1f474c2018-05-30 15:11:13 -04001290 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 public final float[] createFloatArray() {
1292 int N = readInt();
1293 // >>2 because stored floats are 4 bytes
1294 if (N >= 0 && N <= (dataAvail() >> 2)) {
1295 float[] val = new float[N];
1296 for (int i=0; i<N; i++) {
1297 val[i] = readFloat();
1298 }
1299 return val;
1300 } else {
1301 return null;
1302 }
1303 }
1304
Jake Whartonb1f474c2018-05-30 15:11:13 -04001305 public final void readFloatArray(@NonNull float[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 int N = readInt();
1307 if (N == val.length) {
1308 for (int i=0; i<N; i++) {
1309 val[i] = readFloat();
1310 }
1311 } else {
1312 throw new RuntimeException("bad array lengths");
1313 }
1314 }
1315
Jake Whartonb1f474c2018-05-30 15:11:13 -04001316 public final void writeDoubleArray(@Nullable double[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 if (val != null) {
1318 int N = val.length;
1319 writeInt(N);
1320 for (int i=0; i<N; i++) {
1321 writeDouble(val[i]);
1322 }
1323 } else {
1324 writeInt(-1);
1325 }
1326 }
1327
Jake Whartonb1f474c2018-05-30 15:11:13 -04001328 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 public final double[] createDoubleArray() {
1330 int N = readInt();
1331 // >>3 because stored doubles are 8 bytes
1332 if (N >= 0 && N <= (dataAvail() >> 3)) {
1333 double[] val = new double[N];
1334 for (int i=0; i<N; i++) {
1335 val[i] = readDouble();
1336 }
1337 return val;
1338 } else {
1339 return null;
1340 }
1341 }
1342
Jake Whartonb1f474c2018-05-30 15:11:13 -04001343 public final void readDoubleArray(@NonNull double[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 int N = readInt();
1345 if (N == val.length) {
1346 for (int i=0; i<N; i++) {
1347 val[i] = readDouble();
1348 }
1349 } else {
1350 throw new RuntimeException("bad array lengths");
1351 }
1352 }
1353
Jake Whartonb1f474c2018-05-30 15:11:13 -04001354 public final void writeStringArray(@Nullable String[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 if (val != null) {
1356 int N = val.length;
1357 writeInt(N);
1358 for (int i=0; i<N; i++) {
1359 writeString(val[i]);
1360 }
1361 } else {
1362 writeInt(-1);
1363 }
1364 }
1365
Jake Whartonb1f474c2018-05-30 15:11:13 -04001366 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 public final String[] createStringArray() {
1368 int N = readInt();
1369 if (N >= 0) {
1370 String[] val = new String[N];
1371 for (int i=0; i<N; i++) {
1372 val[i] = readString();
1373 }
1374 return val;
1375 } else {
1376 return null;
1377 }
1378 }
1379
Jake Whartonb1f474c2018-05-30 15:11:13 -04001380 public final void readStringArray(@NonNull String[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 int N = readInt();
1382 if (N == val.length) {
1383 for (int i=0; i<N; i++) {
1384 val[i] = readString();
1385 }
1386 } else {
1387 throw new RuntimeException("bad array lengths");
1388 }
1389 }
1390
Jake Whartonb1f474c2018-05-30 15:11:13 -04001391 public final void writeBinderArray(@Nullable IBinder[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 if (val != null) {
1393 int N = val.length;
1394 writeInt(N);
1395 for (int i=0; i<N; i++) {
1396 writeStrongBinder(val[i]);
1397 }
1398 } else {
1399 writeInt(-1);
1400 }
1401 }
1402
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001403 /**
1404 * @hide
1405 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001406 public final void writeCharSequenceArray(@Nullable CharSequence[] val) {
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001407 if (val != null) {
1408 int N = val.length;
1409 writeInt(N);
1410 for (int i=0; i<N; i++) {
1411 writeCharSequence(val[i]);
1412 }
1413 } else {
1414 writeInt(-1);
1415 }
1416 }
1417
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001418 /**
1419 * @hide
1420 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001421 public final void writeCharSequenceList(@Nullable ArrayList<CharSequence> val) {
Dianne Hackborn3d07c942015-03-13 18:02:54 -07001422 if (val != null) {
1423 int N = val.size();
1424 writeInt(N);
1425 for (int i=0; i<N; i++) {
1426 writeCharSequence(val.get(i));
1427 }
1428 } else {
1429 writeInt(-1);
1430 }
1431 }
1432
Jake Whartonb1f474c2018-05-30 15:11:13 -04001433 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 public final IBinder[] createBinderArray() {
1435 int N = readInt();
1436 if (N >= 0) {
1437 IBinder[] val = new IBinder[N];
1438 for (int i=0; i<N; i++) {
1439 val[i] = readStrongBinder();
1440 }
1441 return val;
1442 } else {
1443 return null;
1444 }
1445 }
1446
Jake Whartonb1f474c2018-05-30 15:11:13 -04001447 public final void readBinderArray(@NonNull IBinder[] val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 int N = readInt();
1449 if (N == val.length) {
1450 for (int i=0; i<N; i++) {
1451 val[i] = readStrongBinder();
1452 }
1453 } else {
1454 throw new RuntimeException("bad array lengths");
1455 }
1456 }
1457
1458 /**
1459 * Flatten a List containing a particular object type into the parcel, at
1460 * the current dataPosition() and growing dataCapacity() if needed. The
1461 * type of the objects in the list must be one that implements Parcelable.
1462 * Unlike the generic writeList() method, however, only the raw data of the
1463 * objects is written and not their type, so you must use the corresponding
1464 * readTypedList() to unmarshall them.
1465 *
1466 * @param val The list of objects to be written.
1467 *
1468 * @see #createTypedArrayList
1469 * @see #readTypedList
1470 * @see Parcelable
1471 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001472 public final <T extends Parcelable> void writeTypedList(@Nullable List<T> val) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07001473 writeTypedList(val, 0);
1474 }
1475
1476 /**
Svet Ganov8455ba22019-01-02 13:05:56 -08001477 * Flatten a {@link SparseArray} containing a particular object type into the parcel
1478 * at the current dataPosition() and growing dataCapacity() if needed. The
1479 * type of the objects in the array must be one that implements Parcelable.
1480 * Unlike the generic {@link #writeSparseArray(SparseArray)} method, however, only
1481 * the raw data of the objects is written and not their type, so you must use the
1482 * corresponding {@link #createTypedSparseArray(Parcelable.Creator)}.
1483 *
1484 * @param val The list of objects to be written.
1485 * @param parcelableFlags The parcelable flags to use.
1486 *
1487 * @see #createTypedSparseArray(Parcelable.Creator)
1488 * @see Parcelable
1489 */
1490 public final <T extends Parcelable> void writeTypedSparseArray(@Nullable SparseArray<T> val,
1491 int parcelableFlags) {
1492 if (val == null) {
1493 writeInt(-1);
1494 return;
1495 }
1496 final int count = val.size();
1497 writeInt(count);
1498 for (int i = 0; i < count; i++) {
1499 writeInt(val.keyAt(i));
1500 writeTypedObject(val.valueAt(i), parcelableFlags);
1501 }
1502 }
1503
1504 /**
Sunny Goyal0e60f222017-09-21 21:39:20 -07001505 * @hide
1506 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001507 public <T extends Parcelable> void writeTypedList(@Nullable List<T> val, int parcelableFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 if (val == null) {
1509 writeInt(-1);
1510 return;
1511 }
1512 int N = val.size();
1513 int i=0;
1514 writeInt(N);
1515 while (i < N) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07001516 writeTypedObject(val.get(i), parcelableFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 i++;
1518 }
1519 }
1520
1521 /**
1522 * Flatten a List containing String objects into the parcel, at
1523 * the current dataPosition() and growing dataCapacity() if needed. They
1524 * can later be retrieved with {@link #createStringArrayList} or
1525 * {@link #readStringList}.
1526 *
1527 * @param val The list of strings to be written.
1528 *
1529 * @see #createStringArrayList
1530 * @see #readStringList
1531 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001532 public final void writeStringList(@Nullable List<String> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 if (val == null) {
1534 writeInt(-1);
1535 return;
1536 }
1537 int N = val.size();
1538 int i=0;
1539 writeInt(N);
1540 while (i < N) {
1541 writeString(val.get(i));
1542 i++;
1543 }
1544 }
1545
1546 /**
1547 * Flatten a List containing IBinder objects into the parcel, at
1548 * the current dataPosition() and growing dataCapacity() if needed. They
1549 * can later be retrieved with {@link #createBinderArrayList} or
1550 * {@link #readBinderList}.
1551 *
1552 * @param val The list of strings to be written.
1553 *
1554 * @see #createBinderArrayList
1555 * @see #readBinderList
1556 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001557 public final void writeBinderList(@Nullable List<IBinder> val) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 if (val == null) {
1559 writeInt(-1);
1560 return;
1561 }
1562 int N = val.size();
1563 int i=0;
1564 writeInt(N);
1565 while (i < N) {
1566 writeStrongBinder(val.get(i));
1567 i++;
1568 }
1569 }
1570
1571 /**
Narayan Kamathbea48712016-12-01 15:38:28 +00001572 * Flatten a {@code List} containing arbitrary {@code Parcelable} objects into this parcel
1573 * at the current position. They can later be retrieved using
1574 * {@link #readParcelableList(List, ClassLoader)} if required.
1575 *
1576 * @see #readParcelableList(List, ClassLoader)
Narayan Kamathbea48712016-12-01 15:38:28 +00001577 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001578 public final <T extends Parcelable> void writeParcelableList(@Nullable List<T> val, int flags) {
Narayan Kamathbea48712016-12-01 15:38:28 +00001579 if (val == null) {
1580 writeInt(-1);
1581 return;
1582 }
1583
1584 int N = val.size();
1585 int i=0;
1586 writeInt(N);
1587 while (i < N) {
1588 writeParcelable(val.get(i), flags);
1589 i++;
1590 }
1591 }
1592
1593 /**
Svet Ganov0f4928f2017-02-02 20:02:51 -08001594 * Flatten a homogeneous array containing a particular object type into
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 * the parcel, at
1596 * the current dataPosition() and growing dataCapacity() if needed. The
1597 * type of the objects in the array must be one that implements Parcelable.
1598 * Unlike the {@link #writeParcelableArray} method, however, only the
1599 * raw data of the objects is written and not their type, so you must use
1600 * {@link #readTypedArray} with the correct corresponding
1601 * {@link Parcelable.Creator} implementation to unmarshall them.
1602 *
1603 * @param val The array of objects to be written.
1604 * @param parcelableFlags Contextual flags as per
1605 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1606 *
1607 * @see #readTypedArray
1608 * @see #writeParcelableArray
1609 * @see Parcelable.Creator
1610 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001611 public final <T extends Parcelable> void writeTypedArray(@Nullable T[] val,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 int parcelableFlags) {
1613 if (val != null) {
1614 int N = val.length;
1615 writeInt(N);
Svet Ganov0f4928f2017-02-02 20:02:51 -08001616 for (int i = 0; i < N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07001617 writeTypedObject(val[i], parcelableFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 }
1619 } else {
1620 writeInt(-1);
1621 }
1622 }
1623
1624 /**
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07001625 * Flatten the Parcelable object into the parcel.
1626 *
1627 * @param val The Parcelable object to be written.
1628 * @param parcelableFlags Contextual flags as per
1629 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1630 *
1631 * @see #readTypedObject
1632 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001633 public final <T extends Parcelable> void writeTypedObject(@Nullable T val,
1634 int parcelableFlags) {
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07001635 if (val != null) {
1636 writeInt(1);
1637 val.writeToParcel(this, parcelableFlags);
1638 } else {
1639 writeInt(0);
1640 }
1641 }
1642
1643 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 * Flatten a generic object in to a parcel. The given Object value may
1645 * currently be one of the following types:
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001646 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 * <ul>
1648 * <li> null
1649 * <li> String
1650 * <li> Byte
1651 * <li> Short
1652 * <li> Integer
1653 * <li> Long
1654 * <li> Float
1655 * <li> Double
1656 * <li> Boolean
1657 * <li> String[]
1658 * <li> boolean[]
1659 * <li> byte[]
1660 * <li> int[]
1661 * <li> long[]
1662 * <li> Object[] (supporting objects of the same type defined here).
1663 * <li> {@link Bundle}
1664 * <li> Map (as supported by {@link #writeMap}).
1665 * <li> Any object that implements the {@link Parcelable} protocol.
1666 * <li> Parcelable[]
1667 * <li> CharSequence (as supported by {@link TextUtils#writeToParcel}).
1668 * <li> List (as supported by {@link #writeList}).
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001669 * <li> {@link SparseArray} (as supported by {@link #writeSparseArray(SparseArray)}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 * <li> {@link IBinder}
1671 * <li> Any object that implements Serializable (but see
1672 * {@link #writeSerializable} for caveats). Note that all of the
1673 * previous types have relatively efficient implementations for
1674 * writing to a Parcel; having to rely on the generic serialization
1675 * approach is much less efficient and should be avoided whenever
1676 * possible.
1677 * </ul>
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001678 *
1679 * <p class="caution">{@link Parcelable} objects are written with
1680 * {@link Parcelable#writeToParcel} using contextual flags of 0. When
1681 * serializing objects containing {@link ParcelFileDescriptor}s,
1682 * this may result in file descriptor leaks when they are returned from
1683 * Binder calls (where {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE}
1684 * should be used).</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001686 public final void writeValue(@Nullable Object v) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 if (v == null) {
1688 writeInt(VAL_NULL);
1689 } else if (v instanceof String) {
1690 writeInt(VAL_STRING);
1691 writeString((String) v);
1692 } else if (v instanceof Integer) {
1693 writeInt(VAL_INTEGER);
1694 writeInt((Integer) v);
1695 } else if (v instanceof Map) {
1696 writeInt(VAL_MAP);
1697 writeMap((Map) v);
1698 } else if (v instanceof Bundle) {
1699 // Must be before Parcelable
1700 writeInt(VAL_BUNDLE);
1701 writeBundle((Bundle) v);
Samuel Tanceafe5e2015-12-11 16:50:58 -08001702 } else if (v instanceof PersistableBundle) {
1703 writeInt(VAL_PERSISTABLEBUNDLE);
1704 writePersistableBundle((PersistableBundle) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 } else if (v instanceof Parcelable) {
Samuel Tanceafe5e2015-12-11 16:50:58 -08001706 // IMPOTANT: cases for classes that implement Parcelable must
1707 // come before the Parcelable case, so that their specific VAL_*
1708 // types will be written.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 writeInt(VAL_PARCELABLE);
1710 writeParcelable((Parcelable) v, 0);
1711 } else if (v instanceof Short) {
1712 writeInt(VAL_SHORT);
1713 writeInt(((Short) v).intValue());
1714 } else if (v instanceof Long) {
1715 writeInt(VAL_LONG);
1716 writeLong((Long) v);
1717 } else if (v instanceof Float) {
1718 writeInt(VAL_FLOAT);
1719 writeFloat((Float) v);
1720 } else if (v instanceof Double) {
1721 writeInt(VAL_DOUBLE);
1722 writeDouble((Double) v);
1723 } else if (v instanceof Boolean) {
1724 writeInt(VAL_BOOLEAN);
1725 writeInt((Boolean) v ? 1 : 0);
1726 } else if (v instanceof CharSequence) {
1727 // Must be after String
1728 writeInt(VAL_CHARSEQUENCE);
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001729 writeCharSequence((CharSequence) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 } else if (v instanceof List) {
1731 writeInt(VAL_LIST);
1732 writeList((List) v);
1733 } else if (v instanceof SparseArray) {
1734 writeInt(VAL_SPARSEARRAY);
1735 writeSparseArray((SparseArray) v);
1736 } else if (v instanceof boolean[]) {
1737 writeInt(VAL_BOOLEANARRAY);
1738 writeBooleanArray((boolean[]) v);
1739 } else if (v instanceof byte[]) {
1740 writeInt(VAL_BYTEARRAY);
1741 writeByteArray((byte[]) v);
1742 } else if (v instanceof String[]) {
1743 writeInt(VAL_STRINGARRAY);
1744 writeStringArray((String[]) v);
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00001745 } else if (v instanceof CharSequence[]) {
1746 // Must be after String[] and before Object[]
1747 writeInt(VAL_CHARSEQUENCEARRAY);
1748 writeCharSequenceArray((CharSequence[]) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 } else if (v instanceof IBinder) {
1750 writeInt(VAL_IBINDER);
1751 writeStrongBinder((IBinder) v);
1752 } else if (v instanceof Parcelable[]) {
1753 writeInt(VAL_PARCELABLEARRAY);
1754 writeParcelableArray((Parcelable[]) v, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 } else if (v instanceof int[]) {
1756 writeInt(VAL_INTARRAY);
1757 writeIntArray((int[]) v);
1758 } else if (v instanceof long[]) {
1759 writeInt(VAL_LONGARRAY);
1760 writeLongArray((long[]) v);
1761 } else if (v instanceof Byte) {
1762 writeInt(VAL_BYTE);
1763 writeInt((Byte) v);
Jeff Sharkey5ef33982014-09-04 18:13:39 -07001764 } else if (v instanceof Size) {
1765 writeInt(VAL_SIZE);
1766 writeSize((Size) v);
1767 } else if (v instanceof SizeF) {
1768 writeInt(VAL_SIZEF);
1769 writeSizeF((SizeF) v);
Samuel Tana8036662015-11-23 14:36:00 -08001770 } else if (v instanceof double[]) {
1771 writeInt(VAL_DOUBLEARRAY);
1772 writeDoubleArray((double[]) v);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 } else {
Paul Duffinac5a0822014-02-03 15:02:17 +00001774 Class<?> clazz = v.getClass();
1775 if (clazz.isArray() && clazz.getComponentType() == Object.class) {
1776 // Only pure Object[] are written here, Other arrays of non-primitive types are
1777 // handled by serialization as this does not record the component type.
1778 writeInt(VAL_OBJECTARRAY);
1779 writeArray((Object[]) v);
1780 } else if (v instanceof Serializable) {
1781 // Must be last
1782 writeInt(VAL_SERIALIZABLE);
1783 writeSerializable((Serializable) v);
1784 } else {
1785 throw new RuntimeException("Parcel: unable to marshal value " + v);
1786 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 }
1788 }
1789
1790 /**
1791 * Flatten the name of the class of the Parcelable and its contents
1792 * into the parcel.
Dan Egnorb3e4ef32010-07-20 09:03:35 -07001793 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 * @param p The Parcelable object to be written.
1795 * @param parcelableFlags Contextual flags as per
1796 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
1797 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001798 public final void writeParcelable(@Nullable Parcelable p, int parcelableFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 if (p == null) {
1800 writeString(null);
1801 return;
1802 }
Neil Fuller44e440c2015-04-20 14:39:00 +01001803 writeParcelableCreator(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 p.writeToParcel(this, parcelableFlags);
1805 }
1806
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08001807 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00001808 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04001809 public final void writeParcelableCreator(@NonNull Parcelable p) {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08001810 String name = p.getClass().getName();
1811 writeString(name);
1812 }
1813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 /**
1815 * Write a generic serializable object in to a Parcel. It is strongly
1816 * recommended that this method be avoided, since the serialization
1817 * overhead is extremely large, and this approach will be much slower than
1818 * using the other approaches to writing data in to a Parcel.
1819 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001820 public final void writeSerializable(@Nullable Serializable s) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 if (s == null) {
1822 writeString(null);
1823 return;
1824 }
1825 String name = s.getClass().getName();
1826 writeString(name);
1827
1828 ByteArrayOutputStream baos = new ByteArrayOutputStream();
1829 try {
1830 ObjectOutputStream oos = new ObjectOutputStream(baos);
1831 oos.writeObject(s);
1832 oos.close();
1833
1834 writeByteArray(baos.toByteArray());
1835 } catch (IOException ioe) {
1836 throw new RuntimeException("Parcelable encountered " +
1837 "IOException writing serializable object (name = " + name +
1838 ")", ioe);
1839 }
1840 }
1841
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001842 /** @hide For debugging purposes */
1843 public static void setStackTraceParceling(boolean enabled) {
1844 sParcelExceptionStackTrace = enabled;
1845 }
1846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 /**
1848 * Special function for writing an exception result at the header of
1849 * a parcel, to be used when returning an exception from a transaction.
1850 * Note that this currently only supports a few exception types; any other
1851 * exception will be re-thrown by this function as a RuntimeException
1852 * (to be caught by the system's last-resort exception handling when
1853 * dispatching a transaction).
Samuel Tana8036662015-11-23 14:36:00 -08001854 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 * <p>The supported exception types are:
1856 * <ul>
1857 * <li>{@link BadParcelableException}
1858 * <li>{@link IllegalArgumentException}
1859 * <li>{@link IllegalStateException}
1860 * <li>{@link NullPointerException}
1861 * <li>{@link SecurityException}
Dianne Hackborn18482ae2017-08-01 17:41:00 -07001862 * <li>{@link UnsupportedOperationException}
Dianne Hackborn7e714422013-09-13 17:32:57 -07001863 * <li>{@link NetworkOnMainThreadException}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 * </ul>
Samuel Tana8036662015-11-23 14:36:00 -08001865 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001866 * @param e The Exception to be written.
1867 *
1868 * @see #writeNoException
1869 * @see #readException
1870 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04001871 public final void writeException(@NonNull Exception e) {
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07001872 AppOpsManager.prefixParcelWithAppOpsIfNeeded(this);
1873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001874 int code = 0;
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001875 if (e instanceof Parcelable
1876 && (e.getClass().getClassLoader() == Parcelable.class.getClassLoader())) {
1877 // We only send Parcelable exceptions that are in the
1878 // BootClassLoader to ensure that the receiver can unpack them
1879 code = EX_PARCELABLE;
1880 } else if (e instanceof SecurityException) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 code = EX_SECURITY;
1882 } else if (e instanceof BadParcelableException) {
1883 code = EX_BAD_PARCELABLE;
1884 } else if (e instanceof IllegalArgumentException) {
1885 code = EX_ILLEGAL_ARGUMENT;
1886 } else if (e instanceof NullPointerException) {
1887 code = EX_NULL_POINTER;
1888 } else if (e instanceof IllegalStateException) {
1889 code = EX_ILLEGAL_STATE;
Dianne Hackborn7e714422013-09-13 17:32:57 -07001890 } else if (e instanceof NetworkOnMainThreadException) {
1891 code = EX_NETWORK_MAIN_THREAD;
Dianne Hackborn33d738a2014-09-12 14:23:58 -07001892 } else if (e instanceof UnsupportedOperationException) {
1893 code = EX_UNSUPPORTED_OPERATION;
Christopher Wiley80fd1202015-11-22 17:12:37 -08001894 } else if (e instanceof ServiceSpecificException) {
1895 code = EX_SERVICE_SPECIFIC;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 }
1897 writeInt(code);
Brad Fitzpatrick703e5d32010-07-15 13:16:41 -07001898 StrictMode.clearGatheredViolations();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001899 if (code == 0) {
1900 if (e instanceof RuntimeException) {
1901 throw (RuntimeException) e;
1902 }
1903 throw new RuntimeException(e);
1904 }
1905 writeString(e.getMessage());
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001906 final long timeNow = sParcelExceptionStackTrace ? SystemClock.elapsedRealtime() : 0;
1907 if (sParcelExceptionStackTrace && (timeNow - sLastWriteExceptionStackTrace
1908 > WRITE_EXCEPTION_STACK_TRACE_THRESHOLD_MS)) {
1909 sLastWriteExceptionStackTrace = timeNow;
1910 final int sizePosition = dataPosition();
1911 writeInt(0); // Header size will be filled in later
1912 StackTraceElement[] stackTrace = e.getStackTrace();
1913 final int truncatedSize = Math.min(stackTrace.length, 5);
1914 StringBuilder sb = new StringBuilder();
1915 for (int i = 0; i < truncatedSize; i++) {
Fyodor Kupolov679d9982017-11-21 10:42:23 -08001916 sb.append("\tat ").append(stackTrace[i]).append('\n');
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08001917 }
1918 writeString(sb.toString());
1919 final int payloadPosition = dataPosition();
1920 setDataPosition(sizePosition);
1921 // Write stack trace header size. Used in native side to skip the header
1922 writeInt(payloadPosition - sizePosition);
1923 setDataPosition(payloadPosition);
1924 } else {
1925 writeInt(0);
1926 }
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07001927 switch (code) {
1928 case EX_SERVICE_SPECIFIC:
1929 writeInt(((ServiceSpecificException) e).errorCode);
1930 break;
1931 case EX_PARCELABLE:
1932 // Write parceled exception prefixed by length
1933 final int sizePosition = dataPosition();
1934 writeInt(0);
1935 writeParcelable((Parcelable) e, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
1936 final int payloadPosition = dataPosition();
1937 setDataPosition(sizePosition);
1938 writeInt(payloadPosition - sizePosition);
1939 setDataPosition(payloadPosition);
1940 break;
Christopher Wiley80fd1202015-11-22 17:12:37 -08001941 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 }
1943
1944 /**
1945 * Special function for writing information at the front of the Parcel
1946 * indicating that no exception occurred.
1947 *
1948 * @see #writeException
1949 * @see #readException
1950 */
1951 public final void writeNoException() {
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07001952 AppOpsManager.prefixParcelWithAppOpsIfNeeded(this);
1953
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001954 // Despite the name of this function ("write no exception"),
1955 // it should instead be thought of as "write the RPC response
1956 // header", but because this function name is written out by
1957 // the AIDL compiler, we're not going to rename it.
1958 //
1959 // The response header, in the non-exception case (see also
1960 // writeException above, also called by the AIDL compiler), is
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07001961 // either a 0 (the default case), or EX_HAS_STRICTMODE_REPLY_HEADER if
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001962 // StrictMode has gathered up violations that have occurred
1963 // during a Binder call, in which case we write out the number
1964 // of violations and their details, serialized, before the
1965 // actual RPC respons data. The receiving end of this is
1966 // readException(), below.
1967 if (StrictMode.hasGatheredViolations()) {
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07001968 writeInt(EX_HAS_STRICTMODE_REPLY_HEADER);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001969 final int sizePosition = dataPosition();
1970 writeInt(0); // total size of fat header, to be filled in later
1971 StrictMode.writeGatheredViolationsToParcel(this);
1972 final int payloadPosition = dataPosition();
1973 setDataPosition(sizePosition);
1974 writeInt(payloadPosition - sizePosition); // header size
1975 setDataPosition(payloadPosition);
1976 } else {
1977 writeInt(0);
1978 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 }
1980
1981 /**
1982 * Special function for reading an exception result from the header of
1983 * a parcel, to be used after receiving the result of a transaction. This
1984 * will throw the exception for you if it had been written to the Parcel,
1985 * otherwise return and let you read the normal result data from the Parcel.
1986 *
1987 * @see #writeException
1988 * @see #writeNoException
1989 */
1990 public final void readException() {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001991 int code = readExceptionCode();
1992 if (code != 0) {
1993 String msg = readString();
Takamasa Kuramitsu2214b822018-04-02 14:44:59 +09001994 readException(code, msg);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07001995 }
1996 }
1997
1998 /**
1999 * Parses the header of a Binder call's response Parcel and
2000 * returns the exception code. Deals with lite or fat headers.
2001 * In the common successful case, this header is generally zero.
2002 * In less common cases, it's a small negative number and will be
2003 * followed by an error string.
2004 *
2005 * This exists purely for android.database.DatabaseUtils and
2006 * insulating it from having to handle fat headers as returned by
2007 * e.g. StrictMode-induced RPC responses.
2008 *
2009 * @hide
2010 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002011 @UnsupportedAppUsage
Howard Chen40abbb02019-05-24 02:04:50 +08002012 @TestApi
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07002013 public final int readExceptionCode() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 int code = readInt();
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -07002015 if (code == EX_HAS_NOTED_APPOPS_REPLY_HEADER) {
2016 AppOpsManager.readAndLogNotedAppops(this);
2017 // Read next header or real exception if there is no more header
2018 code = readInt();
2019 }
2020
2021 if (code == EX_HAS_STRICTMODE_REPLY_HEADER) {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07002022 int headerSize = readInt();
2023 if (headerSize == 0) {
2024 Log.e(TAG, "Unexpected zero-sized Parcel reply header.");
2025 } else {
2026 // Currently the only thing in the header is StrictMode stacks,
2027 // but discussions around event/RPC tracing suggest we might
2028 // put that here too. If so, switch on sub-header tags here.
2029 // But for now, just parse out the StrictMode stuff.
2030 StrictMode.readAndHandleBinderCallViolations(this);
2031 }
2032 // And fat response headers are currently only used when
2033 // there are no exceptions, so return no error:
2034 return 0;
2035 }
2036 return code;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 }
2038
2039 /**
Mark Doliner879ea452014-01-02 12:38:07 -08002040 * Throw an exception with the given message. Not intended for use
2041 * outside the Parcel class.
2042 *
2043 * @param code Used to determine which exception class to throw.
2044 * @param msg The exception message.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 */
2046 public final void readException(int code, String msg) {
Takamasa Kuramitsu2214b822018-04-02 14:44:59 +09002047 String remoteStackTrace = null;
2048 final int remoteStackPayloadSize = readInt();
2049 if (remoteStackPayloadSize > 0) {
2050 remoteStackTrace = readString();
2051 }
2052 Exception e = createException(code, msg);
2053 // Attach remote stack trace if availalble
2054 if (remoteStackTrace != null) {
2055 RemoteException cause = new RemoteException(
2056 "Remote stack trace:\n" + remoteStackTrace, null, false, false);
2057 try {
2058 Throwable rootCause = ExceptionUtils.getRootCause(e);
2059 if (rootCause != null) {
2060 rootCause.initCause(cause);
2061 }
2062 } catch (RuntimeException ex) {
2063 Log.e(TAG, "Cannot set cause " + cause + " for " + e, ex);
2064 }
2065 }
2066 SneakyThrow.sneakyThrow(e);
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002067 }
2068
2069 /**
2070 * Creates an exception with the given message.
2071 *
2072 * @param code Used to determine which exception class to throw.
2073 * @param msg The exception message.
2074 */
2075 private Exception createException(int code, String msg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 switch (code) {
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07002077 case EX_PARCELABLE:
2078 if (readInt() > 0) {
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002079 return (Exception) readParcelable(Parcelable.class.getClassLoader());
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07002080 } else {
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002081 return new RuntimeException(msg + " [missing Parcelable]");
Jeff Sharkeye628b7d2017-01-17 13:50:20 -07002082 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 case EX_SECURITY:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002084 return new SecurityException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 case EX_BAD_PARCELABLE:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002086 return new BadParcelableException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 case EX_ILLEGAL_ARGUMENT:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002088 return new IllegalArgumentException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 case EX_NULL_POINTER:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002090 return new NullPointerException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 case EX_ILLEGAL_STATE:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002092 return new IllegalStateException(msg);
Dianne Hackborn7e714422013-09-13 17:32:57 -07002093 case EX_NETWORK_MAIN_THREAD:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002094 return new NetworkOnMainThreadException();
Dianne Hackborn33d738a2014-09-12 14:23:58 -07002095 case EX_UNSUPPORTED_OPERATION:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002096 return new UnsupportedOperationException(msg);
Christopher Wiley80fd1202015-11-22 17:12:37 -08002097 case EX_SERVICE_SPECIFIC:
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002098 return new ServiceSpecificException(readInt(), msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 }
Fyodor Kupolova81b8c02017-11-13 15:51:03 -08002100 return new RuntimeException("Unknown exception code: " + code
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002101 + " msg " + msg);
2102 }
2103
2104 /**
2105 * Read an integer value from the parcel at the current dataPosition().
2106 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002107 public final int readInt() {
2108 return nativeReadInt(mNativePtr);
2109 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110
2111 /**
2112 * Read a long integer value from the parcel at the current dataPosition().
2113 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002114 public final long readLong() {
2115 return nativeReadLong(mNativePtr);
2116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117
2118 /**
2119 * Read a floating point value from the parcel at the current
2120 * dataPosition().
2121 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002122 public final float readFloat() {
2123 return nativeReadFloat(mNativePtr);
2124 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125
2126 /**
2127 * Read a double precision floating point value from the parcel at the
2128 * current dataPosition().
2129 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002130 public final double readDouble() {
2131 return nativeReadDouble(mNativePtr);
2132 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133
2134 /**
2135 * Read a string value from the parcel at the current dataPosition().
2136 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002137 @Nullable
Jeff Sharkey047238c2012-03-07 16:51:38 -08002138 public final String readString() {
Makoto Onuki4501c61d2017-07-27 15:56:40 -07002139 return mReadWriteHelper.readString(this);
2140 }
2141
2142 /**
2143 * Read a string without going though a {@link ReadWriteHelper}. Subclasses of
2144 * {@link ReadWriteHelper} must use this method instead of {@link #readString} to avoid
2145 * infinity recursive calls.
2146 *
2147 * @hide
2148 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002149 @Nullable
Makoto Onuki4501c61d2017-07-27 15:56:40 -07002150 public String readStringNoHelper() {
Jeff Sharkey047238c2012-03-07 16:51:38 -08002151 return nativeReadString(mNativePtr);
2152 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153
Jake Wharton2ffa4312018-04-23 17:47:14 -04002154 /**
2155 * Read a boolean value from the parcel at the current dataPosition().
2156 */
Eugene Susla36e866b2017-02-23 18:24:39 -08002157 public final boolean readBoolean() {
2158 return readInt() != 0;
2159 }
2160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002162 * Read a CharSequence value from the parcel at the current dataPosition().
2163 * @hide
2164 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002165 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04002166 @Nullable
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002167 public final CharSequence readCharSequence() {
2168 return TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(this);
2169 }
2170
2171 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 * Read an object from the parcel at the current dataPosition().
2173 */
Jeff Sharkey047238c2012-03-07 16:51:38 -08002174 public final IBinder readStrongBinder() {
2175 return nativeReadStrongBinder(mNativePtr);
2176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177
2178 /**
2179 * Read a FileDescriptor from the parcel at the current dataPosition().
2180 */
2181 public final ParcelFileDescriptor readFileDescriptor() {
Jeff Sharkey047238c2012-03-07 16:51:38 -08002182 FileDescriptor fd = nativeReadFileDescriptor(mNativePtr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 return fd != null ? new ParcelFileDescriptor(fd) : null;
2184 }
2185
Jeff Sharkeyda5a3e12013-08-11 12:54:42 -07002186 /** {@hide} */
Andrei Onea24ec3212019-03-15 17:35:05 +00002187 @UnsupportedAppUsage
Jeff Sharkeyda5a3e12013-08-11 12:54:42 -07002188 public final FileDescriptor readRawFileDescriptor() {
2189 return nativeReadFileDescriptor(mNativePtr);
2190 }
2191
Casey Dahlin2f974b22015-11-05 12:19:13 -08002192 /**
2193 * {@hide}
2194 * Read and return a new array of FileDescriptors from the parcel.
2195 * @return the FileDescriptor array, or null if the array is null.
2196 **/
Jake Whartonb1f474c2018-05-30 15:11:13 -04002197 @Nullable
Casey Dahlin2f974b22015-11-05 12:19:13 -08002198 public final FileDescriptor[] createRawFileDescriptorArray() {
2199 int N = readInt();
2200 if (N < 0) {
2201 return null;
2202 }
2203 FileDescriptor[] f = new FileDescriptor[N];
2204 for (int i = 0; i < N; i++) {
2205 f[i] = readRawFileDescriptor();
2206 }
2207 return f;
2208 }
2209
2210 /**
2211 * {@hide}
2212 * Read an array of FileDescriptors from a parcel.
2213 * The passed array must be exactly the length of the array in the parcel.
2214 * @return the FileDescriptor array, or null if the array is null.
2215 **/
2216 public final void readRawFileDescriptorArray(FileDescriptor[] val) {
2217 int N = readInt();
2218 if (N == val.length) {
2219 for (int i=0; i<N; i++) {
2220 val[i] = readRawFileDescriptor();
2221 }
2222 } else {
2223 throw new RuntimeException("bad array lengths");
2224 }
2225 }
2226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 /**
2228 * Read a byte value from the parcel at the current dataPosition().
2229 */
2230 public final byte readByte() {
2231 return (byte)(readInt() & 0xff);
2232 }
2233
2234 /**
2235 * Please use {@link #readBundle(ClassLoader)} instead (whose data must have
2236 * been written with {@link #writeBundle}. Read into an existing Map object
2237 * from the parcel at the current dataPosition().
2238 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002239 public final void readMap(@NonNull Map outVal, @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 int N = readInt();
2241 readMapInternal(outVal, N, loader);
2242 }
2243
2244 /**
2245 * Read into an existing List object from the parcel at the current
2246 * dataPosition(), using the given class loader to load any enclosed
2247 * Parcelables. If it is null, the default class loader is used.
2248 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002249 public final void readList(@NonNull List outVal, @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 int N = readInt();
2251 readListInternal(outVal, N, loader);
2252 }
2253
2254 /**
2255 * Please use {@link #readBundle(ClassLoader)} instead (whose data must have
2256 * been written with {@link #writeBundle}. Read and return a new HashMap
2257 * object from the parcel at the current dataPosition(), using the given
2258 * class loader to load any enclosed Parcelables. Returns null if
2259 * the previously written map object was null.
2260 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002261 @Nullable
2262 public final HashMap readHashMap(@Nullable ClassLoader loader)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263 {
2264 int N = readInt();
2265 if (N < 0) {
2266 return null;
2267 }
2268 HashMap m = new HashMap(N);
2269 readMapInternal(m, N, loader);
2270 return m;
2271 }
2272
2273 /**
2274 * Read and return a new Bundle object from the parcel at the current
2275 * dataPosition(). Returns null if the previously written Bundle object was
2276 * null.
2277 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002278 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002279 public final Bundle readBundle() {
2280 return readBundle(null);
2281 }
2282
2283 /**
2284 * Read and return a new Bundle object from the parcel at the current
2285 * dataPosition(), using the given class loader to initialize the class
2286 * loader of the Bundle for later retrieval of Parcelable objects.
2287 * Returns null if the previously written Bundle object was null.
2288 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002289 @Nullable
2290 public final Bundle readBundle(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 int length = readInt();
2292 if (length < 0) {
Dianne Hackborn4a7d8242013-10-03 10:19:20 -07002293 if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 return null;
2295 }
Samuel Tana8036662015-11-23 14:36:00 -08002296
Dianne Hackborn6aff9052009-05-22 13:20:23 -07002297 final Bundle bundle = new Bundle(this, length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002298 if (loader != null) {
2299 bundle.setClassLoader(loader);
2300 }
2301 return bundle;
2302 }
2303
2304 /**
Craig Mautner719e6b12014-04-04 20:29:41 -07002305 * Read and return a new Bundle object from the parcel at the current
2306 * dataPosition(). Returns null if the previously written Bundle object was
2307 * null.
2308 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002309 @Nullable
Craig Mautner719e6b12014-04-04 20:29:41 -07002310 public final PersistableBundle readPersistableBundle() {
2311 return readPersistableBundle(null);
2312 }
2313
2314 /**
2315 * Read and return a new Bundle object from the parcel at the current
2316 * dataPosition(), using the given class loader to initialize the class
2317 * loader of the Bundle for later retrieval of Parcelable objects.
2318 * Returns null if the previously written Bundle object was null.
2319 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002320 @Nullable
2321 public final PersistableBundle readPersistableBundle(@Nullable ClassLoader loader) {
Craig Mautner719e6b12014-04-04 20:29:41 -07002322 int length = readInt();
2323 if (length < 0) {
2324 if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length);
2325 return null;
2326 }
2327
2328 final PersistableBundle bundle = new PersistableBundle(this, length);
2329 if (loader != null) {
2330 bundle.setClassLoader(loader);
2331 }
2332 return bundle;
2333 }
2334
2335 /**
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002336 * Read a Size from the parcel at the current dataPosition().
2337 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002338 @NonNull
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002339 public final Size readSize() {
2340 final int width = readInt();
2341 final int height = readInt();
2342 return new Size(width, height);
2343 }
2344
2345 /**
2346 * Read a SizeF from the parcel at the current dataPosition().
2347 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002348 @NonNull
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002349 public final SizeF readSizeF() {
2350 final float width = readFloat();
2351 final float height = readFloat();
2352 return new SizeF(width, height);
2353 }
2354
2355 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 * Read and return a byte[] object from the parcel.
2357 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002358 @Nullable
Jeff Sharkey047238c2012-03-07 16:51:38 -08002359 public final byte[] createByteArray() {
2360 return nativeCreateByteArray(mNativePtr);
2361 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362
2363 /**
2364 * Read a byte[] object from the parcel and copy it into the
2365 * given byte array.
2366 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002367 public final void readByteArray(@NonNull byte[] val) {
Jocelyn Dang46100442017-05-05 15:40:49 -07002368 boolean valid = nativeReadByteArray(mNativePtr, val, (val != null) ? val.length : 0);
2369 if (!valid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 throw new RuntimeException("bad array lengths");
2371 }
2372 }
2373
2374 /**
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002375 * Read a blob of data from the parcel and return it as a byte array.
2376 * {@hide}
Sandeep Siddhartha39c12fa2014-07-25 18:37:29 -07002377 * {@SystemApi}
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002378 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002379 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04002380 @Nullable
Sandeep Siddhartha90d7a3e2014-07-25 16:19:42 -07002381 public final byte[] readBlob() {
2382 return nativeReadBlob(mNativePtr);
2383 }
2384
2385 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002386 * Read and return a String[] object from the parcel.
2387 * {@hide}
2388 */
Andrei Onea24ec3212019-03-15 17:35:05 +00002389 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04002390 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 public final String[] readStringArray() {
2392 String[] array = null;
2393
2394 int length = readInt();
2395 if (length >= 0)
2396 {
2397 array = new String[length];
2398
2399 for (int i = 0 ; i < length ; i++)
2400 {
2401 array[i] = readString();
2402 }
2403 }
2404
2405 return array;
2406 }
2407
2408 /**
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002409 * Read and return a CharSequence[] object from the parcel.
2410 * {@hide}
2411 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002412 @Nullable
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002413 public final CharSequence[] readCharSequenceArray() {
2414 CharSequence[] array = null;
2415
2416 int length = readInt();
2417 if (length >= 0)
2418 {
2419 array = new CharSequence[length];
2420
2421 for (int i = 0 ; i < length ; i++)
2422 {
2423 array[i] = readCharSequence();
2424 }
2425 }
2426
2427 return array;
2428 }
2429
2430 /**
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002431 * Read and return an ArrayList&lt;CharSequence&gt; object from the parcel.
2432 * {@hide}
2433 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002434 @Nullable
Dianne Hackborn3d07c942015-03-13 18:02:54 -07002435 public final ArrayList<CharSequence> readCharSequenceList() {
2436 ArrayList<CharSequence> array = null;
2437
2438 int length = readInt();
2439 if (length >= 0) {
2440 array = new ArrayList<CharSequence>(length);
2441
2442 for (int i = 0 ; i < length ; i++) {
2443 array.add(readCharSequence());
2444 }
2445 }
2446
2447 return array;
2448 }
2449
2450 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 * Read and return a new ArrayList object from the parcel at the current
2452 * dataPosition(). Returns null if the previously written list object was
2453 * null. The given class loader will be used to load any enclosed
2454 * Parcelables.
2455 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002456 @Nullable
2457 public final ArrayList readArrayList(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002458 int N = readInt();
2459 if (N < 0) {
2460 return null;
2461 }
2462 ArrayList l = new ArrayList(N);
2463 readListInternal(l, N, loader);
2464 return l;
2465 }
2466
2467 /**
2468 * Read and return a new Object array from the parcel at the current
2469 * dataPosition(). Returns null if the previously written array was
2470 * null. The given class loader will be used to load any enclosed
2471 * Parcelables.
2472 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002473 @Nullable
2474 public final Object[] readArray(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002475 int N = readInt();
2476 if (N < 0) {
2477 return null;
2478 }
2479 Object[] l = new Object[N];
2480 readArrayInternal(l, N, loader);
2481 return l;
2482 }
2483
2484 /**
2485 * Read and return a new SparseArray object from the parcel at the current
2486 * dataPosition(). Returns null if the previously written list object was
2487 * null. The given class loader will be used to load any enclosed
2488 * Parcelables.
2489 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002490 @Nullable
Svet Ganov8455ba22019-01-02 13:05:56 -08002491 public final <T> SparseArray<T> readSparseArray(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002492 int N = readInt();
2493 if (N < 0) {
2494 return null;
2495 }
2496 SparseArray sa = new SparseArray(N);
2497 readSparseArrayInternal(sa, N, loader);
2498 return sa;
2499 }
2500
2501 /**
2502 * Read and return a new SparseBooleanArray object from the parcel at the current
2503 * dataPosition(). Returns null if the previously written list object was
2504 * null.
2505 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002506 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 public final SparseBooleanArray readSparseBooleanArray() {
2508 int N = readInt();
2509 if (N < 0) {
2510 return null;
2511 }
2512 SparseBooleanArray sa = new SparseBooleanArray(N);
2513 readSparseBooleanArrayInternal(sa, N);
2514 return sa;
2515 }
2516
2517 /**
Adam Lesinski4e862812016-11-21 16:02:24 -08002518 * Read and return a new SparseIntArray object from the parcel at the current
2519 * dataPosition(). Returns null if the previously written array object was null.
Adam Lesinski205656d2017-03-23 13:38:26 -07002520 * @hide
Adam Lesinski4e862812016-11-21 16:02:24 -08002521 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002522 @Nullable
Adam Lesinski4e862812016-11-21 16:02:24 -08002523 public final SparseIntArray readSparseIntArray() {
2524 int N = readInt();
2525 if (N < 0) {
2526 return null;
2527 }
2528 SparseIntArray sa = new SparseIntArray(N);
2529 readSparseIntArrayInternal(sa, N);
2530 return sa;
2531 }
2532
2533 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 * Read and return a new ArrayList containing a particular object type from
2535 * the parcel that was written with {@link #writeTypedList} at the
2536 * current dataPosition(). Returns null if the
2537 * previously written list object was null. The list <em>must</em> have
2538 * previously been written via {@link #writeTypedList} with the same object
2539 * type.
2540 *
2541 * @return A newly created ArrayList containing objects with the same data
2542 * as those that were previously written.
2543 *
2544 * @see #writeTypedList
2545 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002546 @Nullable
2547 public final <T> ArrayList<T> createTypedArrayList(@NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002548 int N = readInt();
2549 if (N < 0) {
2550 return null;
2551 }
2552 ArrayList<T> l = new ArrayList<T>(N);
2553 while (N > 0) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002554 l.add(readTypedObject(c));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555 N--;
2556 }
2557 return l;
2558 }
2559
2560 /**
2561 * Read into the given List items containing a particular object type
2562 * that were written with {@link #writeTypedList} at the
2563 * current dataPosition(). The list <em>must</em> have
2564 * previously been written via {@link #writeTypedList} with the same object
2565 * type.
2566 *
2567 * @return A newly created ArrayList containing objects with the same data
2568 * as those that were previously written.
2569 *
2570 * @see #writeTypedList
2571 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002572 public final <T> void readTypedList(@NonNull List<T> list, @NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573 int M = list.size();
2574 int N = readInt();
2575 int i = 0;
2576 for (; i < M && i < N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002577 list.set(i, readTypedObject(c));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 }
2579 for (; i<N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002580 list.add(readTypedObject(c));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 }
2582 for (; i<M; i++) {
2583 list.remove(N);
2584 }
2585 }
2586
2587 /**
Svet Ganov8455ba22019-01-02 13:05:56 -08002588 * Read into a new {@link SparseArray} items containing a particular object type
2589 * that were written with {@link #writeTypedSparseArray(SparseArray, int)} at the
2590 * current dataPosition(). The list <em>must</em> have previously been written
2591 * via {@link #writeTypedSparseArray(SparseArray, int)} with the same object type.
2592 *
2593 * @param creator The creator to use when for instantiation.
2594 *
2595 * @return A newly created {@link SparseArray} containing objects with the same data
2596 * as those that were previously written.
2597 *
2598 * @see #writeTypedSparseArray(SparseArray, int)
2599 */
2600 public final @Nullable <T extends Parcelable> SparseArray<T> createTypedSparseArray(
2601 @NonNull Parcelable.Creator<T> creator) {
2602 final int count = readInt();
2603 if (count < 0) {
2604 return null;
2605 }
2606 final SparseArray<T> array = new SparseArray<>(count);
2607 for (int i = 0; i < count; i++) {
2608 final int index = readInt();
2609 final T value = readTypedObject(creator);
2610 array.append(index, value);
2611 }
2612 return array;
2613 }
2614
2615 /**
2616 * Read into a new {@link ArrayMap} with string keys items containing a particular
2617 * object type that were written with {@link #writeTypedArrayMap(ArrayMap, int)} at the
2618 * current dataPosition(). The list <em>must</em> have previously been written
2619 * via {@link #writeTypedArrayMap(ArrayMap, int)} with the same object type.
2620 *
2621 * @param creator The creator to use when for instantiation.
2622 *
2623 * @return A newly created {@link ArrayMap} containing objects with the same data
2624 * as those that were previously written.
2625 *
2626 * @see #writeTypedArrayMap(ArrayMap, int)
2627 */
2628 public final @Nullable <T extends Parcelable> ArrayMap<String, T> createTypedArrayMap(
2629 @NonNull Parcelable.Creator<T> creator) {
2630 final int count = readInt();
2631 if (count < 0) {
2632 return null;
2633 }
2634 final ArrayMap<String, T> map = new ArrayMap<>(count);
2635 for (int i = 0; i < count; i++) {
2636 final String key = readString();
2637 final T value = readTypedObject(creator);
2638 map.append(key, value);
2639 }
2640 return map;
2641 }
2642
2643 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002644 * Read and return a new ArrayList containing String objects from
2645 * the parcel that was written with {@link #writeStringList} at the
2646 * current dataPosition(). Returns null if the
2647 * previously written list object was null.
2648 *
2649 * @return A newly created ArrayList containing strings with the same data
2650 * as those that were previously written.
2651 *
2652 * @see #writeStringList
2653 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002654 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655 public final ArrayList<String> createStringArrayList() {
2656 int N = readInt();
2657 if (N < 0) {
2658 return null;
2659 }
2660 ArrayList<String> l = new ArrayList<String>(N);
2661 while (N > 0) {
2662 l.add(readString());
2663 N--;
2664 }
2665 return l;
2666 }
2667
2668 /**
2669 * Read and return a new ArrayList containing IBinder objects from
2670 * the parcel that was written with {@link #writeBinderList} at the
2671 * current dataPosition(). Returns null if the
2672 * previously written list object was null.
2673 *
2674 * @return A newly created ArrayList containing strings with the same data
2675 * as those that were previously written.
2676 *
2677 * @see #writeBinderList
2678 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002679 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680 public final ArrayList<IBinder> createBinderArrayList() {
2681 int N = readInt();
2682 if (N < 0) {
2683 return null;
2684 }
2685 ArrayList<IBinder> l = new ArrayList<IBinder>(N);
2686 while (N > 0) {
2687 l.add(readStrongBinder());
2688 N--;
2689 }
2690 return l;
2691 }
2692
2693 /**
2694 * Read into the given List items String objects that were written with
2695 * {@link #writeStringList} at the current dataPosition().
2696 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002697 * @see #writeStringList
2698 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002699 public final void readStringList(@NonNull List<String> list) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 int M = list.size();
2701 int N = readInt();
2702 int i = 0;
2703 for (; i < M && i < N; i++) {
2704 list.set(i, readString());
2705 }
2706 for (; i<N; i++) {
2707 list.add(readString());
2708 }
2709 for (; i<M; i++) {
2710 list.remove(N);
2711 }
2712 }
2713
2714 /**
2715 * Read into the given List items IBinder objects that were written with
2716 * {@link #writeBinderList} at the current dataPosition().
2717 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 * @see #writeBinderList
2719 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002720 public final void readBinderList(@NonNull List<IBinder> list) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721 int M = list.size();
2722 int N = readInt();
2723 int i = 0;
2724 for (; i < M && i < N; i++) {
2725 list.set(i, readStrongBinder());
2726 }
2727 for (; i<N; i++) {
2728 list.add(readStrongBinder());
2729 }
2730 for (; i<M; i++) {
2731 list.remove(N);
2732 }
2733 }
2734
2735 /**
Narayan Kamathbea48712016-12-01 15:38:28 +00002736 * Read the list of {@code Parcelable} objects at the current data position into the
2737 * given {@code list}. The contents of the {@code list} are replaced. If the serialized
2738 * list was {@code null}, {@code list} is cleared.
2739 *
2740 * @see #writeParcelableList(List, int)
Narayan Kamathbea48712016-12-01 15:38:28 +00002741 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002742 @NonNull
2743 public final <T extends Parcelable> List<T> readParcelableList(@NonNull List<T> list,
2744 @Nullable ClassLoader cl) {
Narayan Kamathbea48712016-12-01 15:38:28 +00002745 final int N = readInt();
2746 if (N == -1) {
2747 list.clear();
Eugene Susla36e866b2017-02-23 18:24:39 -08002748 return list;
Narayan Kamathbea48712016-12-01 15:38:28 +00002749 }
2750
2751 final int M = list.size();
2752 int i = 0;
2753 for (; i < M && i < N; i++) {
2754 list.set(i, (T) readParcelable(cl));
2755 }
2756 for (; i<N; i++) {
2757 list.add((T) readParcelable(cl));
2758 }
2759 for (; i<M; i++) {
2760 list.remove(N);
2761 }
Eugene Susla36e866b2017-02-23 18:24:39 -08002762 return list;
Narayan Kamathbea48712016-12-01 15:38:28 +00002763 }
2764
2765 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002766 * Read and return a new array containing a particular object type from
2767 * the parcel at the current dataPosition(). Returns null if the
2768 * previously written array was null. The array <em>must</em> have
2769 * previously been written via {@link #writeTypedArray} with the same
2770 * object type.
2771 *
2772 * @return A newly created array containing objects with the same data
2773 * as those that were previously written.
2774 *
2775 * @see #writeTypedArray
2776 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002777 @Nullable
2778 public final <T> T[] createTypedArray(@NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 int N = readInt();
2780 if (N < 0) {
2781 return null;
2782 }
2783 T[] l = c.newArray(N);
2784 for (int i=0; i<N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002785 l[i] = readTypedObject(c);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002786 }
2787 return l;
2788 }
2789
Jake Whartonb1f474c2018-05-30 15:11:13 -04002790 public final <T> void readTypedArray(@NonNull T[] val, @NonNull Parcelable.Creator<T> c) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 int N = readInt();
2792 if (N == val.length) {
2793 for (int i=0; i<N; i++) {
Sunny Goyal0e60f222017-09-21 21:39:20 -07002794 val[i] = readTypedObject(c);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002795 }
2796 } else {
2797 throw new RuntimeException("bad array lengths");
2798 }
2799 }
2800
2801 /**
2802 * @deprecated
2803 * @hide
2804 */
2805 @Deprecated
2806 public final <T> T[] readTypedArray(Parcelable.Creator<T> c) {
2807 return createTypedArray(c);
2808 }
2809
2810 /**
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07002811 * Read and return a typed Parcelable object from a parcel.
2812 * Returns null if the previous written object was null.
2813 * The object <em>must</em> have previous been written via
2814 * {@link #writeTypedObject} with the same object type.
2815 *
2816 * @return A newly created object of the type that was previously
2817 * written.
2818 *
2819 * @see #writeTypedObject
2820 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002821 @Nullable
2822 public final <T> T readTypedObject(@NonNull Parcelable.Creator<T> c) {
Jens Ole Lauridsen8dea8742015-04-20 11:18:51 -07002823 if (readInt() != 0) {
2824 return c.createFromParcel(this);
2825 } else {
2826 return null;
2827 }
2828 }
2829
2830 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831 * Write a heterogeneous array of Parcelable objects into the Parcel.
2832 * Each object in the array is written along with its class name, so
2833 * that the correct class can later be instantiated. As a result, this
2834 * has significantly more overhead than {@link #writeTypedArray}, but will
2835 * correctly handle an array containing more than one type of object.
2836 *
2837 * @param value The array of objects to be written.
2838 * @param parcelableFlags Contextual flags as per
2839 * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}.
2840 *
2841 * @see #writeTypedArray
2842 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002843 public final <T extends Parcelable> void writeParcelableArray(@Nullable T[] value,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 int parcelableFlags) {
2845 if (value != null) {
2846 int N = value.length;
2847 writeInt(N);
2848 for (int i=0; i<N; i++) {
2849 writeParcelable(value[i], parcelableFlags);
2850 }
2851 } else {
2852 writeInt(-1);
2853 }
2854 }
2855
2856 /**
2857 * Read a typed object from a parcel. The given class loader will be
2858 * used to load any enclosed Parcelables. If it is null, the default class
2859 * loader will be used.
2860 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04002861 @Nullable
2862 public final Object readValue(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 int type = readInt();
2864
2865 switch (type) {
2866 case VAL_NULL:
2867 return null;
2868
2869 case VAL_STRING:
2870 return readString();
2871
2872 case VAL_INTEGER:
2873 return readInt();
2874
2875 case VAL_MAP:
2876 return readHashMap(loader);
2877
2878 case VAL_PARCELABLE:
2879 return readParcelable(loader);
2880
2881 case VAL_SHORT:
2882 return (short) readInt();
2883
2884 case VAL_LONG:
2885 return readLong();
2886
2887 case VAL_FLOAT:
2888 return readFloat();
2889
2890 case VAL_DOUBLE:
2891 return readDouble();
2892
2893 case VAL_BOOLEAN:
2894 return readInt() == 1;
2895
2896 case VAL_CHARSEQUENCE:
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002897 return readCharSequence();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898
2899 case VAL_LIST:
2900 return readArrayList(loader);
2901
2902 case VAL_BOOLEANARRAY:
Samuel Tana8036662015-11-23 14:36:00 -08002903 return createBooleanArray();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904
2905 case VAL_BYTEARRAY:
2906 return createByteArray();
2907
2908 case VAL_STRINGARRAY:
2909 return readStringArray();
2910
Bjorn Bringert08bbffb2010-02-25 11:16:22 +00002911 case VAL_CHARSEQUENCEARRAY:
2912 return readCharSequenceArray();
2913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 case VAL_IBINDER:
2915 return readStrongBinder();
2916
2917 case VAL_OBJECTARRAY:
2918 return readArray(loader);
2919
2920 case VAL_INTARRAY:
2921 return createIntArray();
2922
2923 case VAL_LONGARRAY:
2924 return createLongArray();
2925
2926 case VAL_BYTE:
2927 return readByte();
2928
2929 case VAL_SERIALIZABLE:
John Spurlock5002b8c2014-01-10 13:32:12 -05002930 return readSerializable(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002931
2932 case VAL_PARCELABLEARRAY:
2933 return readParcelableArray(loader);
2934
2935 case VAL_SPARSEARRAY:
2936 return readSparseArray(loader);
2937
2938 case VAL_SPARSEBOOLEANARRAY:
2939 return readSparseBooleanArray();
2940
2941 case VAL_BUNDLE:
2942 return readBundle(loader); // loading will be deferred
2943
Craig Mautner719e6b12014-04-04 20:29:41 -07002944 case VAL_PERSISTABLEBUNDLE:
2945 return readPersistableBundle(loader);
2946
Jeff Sharkey5ef33982014-09-04 18:13:39 -07002947 case VAL_SIZE:
2948 return readSize();
2949
2950 case VAL_SIZEF:
2951 return readSizeF();
2952
Samuel Tana8036662015-11-23 14:36:00 -08002953 case VAL_DOUBLEARRAY:
2954 return createDoubleArray();
2955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956 default:
2957 int off = dataPosition() - 4;
2958 throw new RuntimeException(
2959 "Parcel " + this + ": Unmarshalling unknown type code " + type + " at offset " + off);
2960 }
2961 }
2962
2963 /**
2964 * Read and return a new Parcelable from the parcel. The given class loader
2965 * will be used to load any enclosed Parcelables. If it is null, the default
2966 * class loader will be used.
2967 * @param loader A ClassLoader from which to instantiate the Parcelable
2968 * object, or null for the default class loader.
2969 * @return Returns the newly created Parcelable, or null if a null
2970 * object has been written.
2971 * @throws BadParcelableException Throws BadParcelableException if there
2972 * was an error trying to instantiate the Parcelable.
2973 */
Neil Fuller44e440c2015-04-20 14:39:00 +01002974 @SuppressWarnings("unchecked")
Jake Whartonb1f474c2018-05-30 15:11:13 -04002975 @Nullable
2976 public final <T extends Parcelable> T readParcelable(@Nullable ClassLoader loader) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002977 Parcelable.Creator<?> creator = readParcelableCreator(loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002978 if (creator == null) {
2979 return null;
2980 }
2981 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002982 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
2983 (Parcelable.ClassLoaderCreator<?>) creator;
2984 return (T) classLoaderCreator.createFromParcel(this, loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002985 }
Neil Fuller44e440c2015-04-20 14:39:00 +01002986 return (T) creator.createFromParcel(this);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002987 }
2988
2989 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00002990 @UnsupportedAppUsage
Neil Fuller44e440c2015-04-20 14:39:00 +01002991 @SuppressWarnings("unchecked")
Jake Whartonb1f474c2018-05-30 15:11:13 -04002992 @Nullable
2993 public final <T extends Parcelable> T readCreator(@NonNull Parcelable.Creator<?> creator,
2994 @Nullable ClassLoader loader) {
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002995 if (creator instanceof Parcelable.ClassLoaderCreator<?>) {
Neil Fuller44e440c2015-04-20 14:39:00 +01002996 Parcelable.ClassLoaderCreator<?> classLoaderCreator =
2997 (Parcelable.ClassLoaderCreator<?>) creator;
2998 return (T) classLoaderCreator.createFromParcel(this, loader);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08002999 }
Neil Fuller44e440c2015-04-20 14:39:00 +01003000 return (T) creator.createFromParcel(this);
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08003001 }
3002
3003 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +00003004 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04003005 @Nullable
3006 public final Parcelable.Creator<?> readParcelableCreator(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007 String name = readString();
3008 if (name == null) {
3009 return null;
3010 }
Neil Fuller44e440c2015-04-20 14:39:00 +01003011 Parcelable.Creator<?> creator;
Andreas Gampe4c0df5c2019-05-30 14:38:43 -07003012 HashMap<String, Parcelable.Creator<?>> map;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 synchronized (mCreators) {
Andreas Gampe4c0df5c2019-05-30 14:38:43 -07003014 map = mCreators.get(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003015 if (map == null) {
Neil Fuller44e440c2015-04-20 14:39:00 +01003016 map = new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003017 mCreators.put(loader, map);
3018 }
3019 creator = map.get(name);
Andreas Gampe4c0df5c2019-05-30 14:38:43 -07003020 }
3021 if (creator != null) {
3022 return creator;
3023 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003024
Andreas Gampe4c0df5c2019-05-30 14:38:43 -07003025 try {
3026 // If loader == null, explicitly emulate Class.forName(String) "caller
3027 // classloader" behavior.
3028 ClassLoader parcelableClassLoader =
3029 (loader == null ? getClass().getClassLoader() : loader);
3030 // Avoid initializing the Parcelable class until we know it implements
3031 // Parcelable and has the necessary CREATOR field. http://b/1171613.
3032 Class<?> parcelableClass = Class.forName(name, false /* initialize */,
3033 parcelableClassLoader);
3034 if (!Parcelable.class.isAssignableFrom(parcelableClass)) {
3035 throw new BadParcelableException("Parcelable protocol requires subclassing "
3036 + "from Parcelable on class " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003037 }
Andreas Gampe4c0df5c2019-05-30 14:38:43 -07003038 Field f = parcelableClass.getField("CREATOR");
3039 if ((f.getModifiers() & Modifier.STATIC) == 0) {
3040 throw new BadParcelableException("Parcelable protocol requires "
3041 + "the CREATOR object to be static on class " + name);
3042 }
3043 Class<?> creatorType = f.getType();
3044 if (!Parcelable.Creator.class.isAssignableFrom(creatorType)) {
3045 // Fail before calling Field.get(), not after, to avoid initializing
3046 // parcelableClass unnecessarily.
3047 throw new BadParcelableException("Parcelable protocol requires a "
3048 + "Parcelable.Creator object called "
3049 + "CREATOR on class " + name);
3050 }
3051 creator = (Parcelable.Creator<?>) f.get(null);
3052 } catch (IllegalAccessException e) {
3053 Log.e(TAG, "Illegal access when unmarshalling: " + name, e);
3054 throw new BadParcelableException(
3055 "IllegalAccessException when unmarshalling: " + name);
3056 } catch (ClassNotFoundException e) {
3057 Log.e(TAG, "Class not found when unmarshalling: " + name, e);
3058 throw new BadParcelableException(
3059 "ClassNotFoundException when unmarshalling: " + name);
3060 } catch (NoSuchFieldException e) {
3061 throw new BadParcelableException("Parcelable protocol requires a "
3062 + "Parcelable.Creator object called "
3063 + "CREATOR on class " + name);
3064 }
3065 if (creator == null) {
3066 throw new BadParcelableException("Parcelable protocol requires a "
3067 + "non-null Parcelable.Creator object called "
3068 + "CREATOR on class " + name);
3069 }
3070
3071 synchronized (mCreators) {
3072 map.put(name, creator);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003073 }
3074
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -08003075 return creator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076 }
3077
3078 /**
3079 * Read and return a new Parcelable array from the parcel.
3080 * The given class loader will be used to load any enclosed
3081 * Parcelables.
3082 * @return the Parcelable array, or null if the array is null
3083 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04003084 @Nullable
3085 public final Parcelable[] readParcelableArray(@Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003086 int N = readInt();
3087 if (N < 0) {
3088 return null;
3089 }
3090 Parcelable[] p = new Parcelable[N];
3091 for (int i = 0; i < N; i++) {
Neil Fuller44e440c2015-04-20 14:39:00 +01003092 p[i] = readParcelable(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003093 }
3094 return p;
3095 }
3096
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003097 /** @hide */
Jake Whartonb1f474c2018-05-30 15:11:13 -04003098 @Nullable
3099 public final <T extends Parcelable> T[] readParcelableArray(@Nullable ClassLoader loader,
3100 @NonNull Class<T> clazz) {
Makoto Onuki440a1ea2016-07-20 14:21:18 -07003101 int N = readInt();
3102 if (N < 0) {
3103 return null;
3104 }
3105 T[] p = (T[]) Array.newInstance(clazz, N);
3106 for (int i = 0; i < N; i++) {
3107 p[i] = readParcelable(loader);
3108 }
3109 return p;
3110 }
3111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003112 /**
3113 * Read and return a new Serializable object from the parcel.
3114 * @return the Serializable object, or null if the Serializable name
3115 * wasn't found in the parcel.
3116 */
Jake Whartonb1f474c2018-05-30 15:11:13 -04003117 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003118 public final Serializable readSerializable() {
John Spurlock5002b8c2014-01-10 13:32:12 -05003119 return readSerializable(null);
3120 }
3121
Jake Whartonb1f474c2018-05-30 15:11:13 -04003122 @Nullable
3123 private final Serializable readSerializable(@Nullable final ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003124 String name = readString();
3125 if (name == null) {
3126 // For some reason we were unable to read the name of the Serializable (either there
3127 // is nothing left in the Parcel to read, or the next value wasn't a String), so
3128 // return null, which indicates that the name wasn't found in the parcel.
3129 return null;
3130 }
3131
3132 byte[] serializedData = createByteArray();
3133 ByteArrayInputStream bais = new ByteArrayInputStream(serializedData);
3134 try {
John Spurlock5002b8c2014-01-10 13:32:12 -05003135 ObjectInputStream ois = new ObjectInputStream(bais) {
3136 @Override
3137 protected Class<?> resolveClass(ObjectStreamClass osClass)
3138 throws IOException, ClassNotFoundException {
3139 // try the custom classloader if provided
3140 if (loader != null) {
3141 Class<?> c = Class.forName(osClass.getName(), false, loader);
3142 if (c != null) {
3143 return c;
3144 }
3145 }
3146 return super.resolveClass(osClass);
3147 }
3148 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003149 return (Serializable) ois.readObject();
3150 } catch (IOException ioe) {
3151 throw new RuntimeException("Parcelable encountered " +
3152 "IOException reading a Serializable object (name = " + name +
3153 ")", ioe);
3154 } catch (ClassNotFoundException cnfe) {
John Spurlock5002b8c2014-01-10 13:32:12 -05003155 throw new RuntimeException("Parcelable encountered " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003156 "ClassNotFoundException reading a Serializable object (name = "
3157 + name + ")", cnfe);
3158 }
3159 }
3160
3161 // Cache of previously looked up CREATOR.createFromParcel() methods for
3162 // particular classes. Keys are the names of the classes, values are
3163 // Method objects.
Artur Satayev70507ed2019-07-29 13:18:27 +01003164 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
Neil Fuller44e440c2015-04-20 14:39:00 +01003165 private static final HashMap<ClassLoader,HashMap<String,Parcelable.Creator<?>>>
3166 mCreators = new HashMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167
Narayan Kamathb34a4612014-01-23 14:17:11 +00003168 /** @hide for internal use only. */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003169 static protected final Parcel obtain(int obj) {
Ashok Bhat8ab665d2014-01-22 16:00:20 +00003170 throw new UnsupportedOperationException();
3171 }
3172
3173 /** @hide */
3174 static protected final Parcel obtain(long obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003175 final Parcel[] pool = sHolderPool;
3176 synchronized (pool) {
3177 Parcel p;
3178 for (int i=0; i<POOL_SIZE; i++) {
3179 p = pool[i];
3180 if (p != null) {
3181 pool[i] = null;
3182 if (DEBUG_RECYCLE) {
3183 p.mStack = new RuntimeException();
3184 }
3185 p.init(obj);
3186 return p;
3187 }
3188 }
3189 }
3190 return new Parcel(obj);
3191 }
3192
Ashok Bhat8ab665d2014-01-22 16:00:20 +00003193 private Parcel(long nativePtr) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003194 if (DEBUG_RECYCLE) {
3195 mStack = new RuntimeException();
3196 }
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003197 //Log.i(TAG, "Initializing obj=0x" + Integer.toHexString(obj), mStack);
Jeff Sharkey047238c2012-03-07 16:51:38 -08003198 init(nativePtr);
3199 }
3200
Ashok Bhat8ab665d2014-01-22 16:00:20 +00003201 private void init(long nativePtr) {
Jeff Sharkey047238c2012-03-07 16:51:38 -08003202 if (nativePtr != 0) {
3203 mNativePtr = nativePtr;
3204 mOwnsNativeParcelObject = false;
3205 } else {
3206 mNativePtr = nativeCreate();
3207 mOwnsNativeParcelObject = true;
3208 }
3209 }
3210
3211 private void freeBuffer() {
3212 if (mOwnsNativeParcelObject) {
Adrian Roos04505652015-10-22 16:12:01 -07003213 updateNativeSize(nativeFreeBuffer(mNativePtr));
Jeff Sharkey047238c2012-03-07 16:51:38 -08003214 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -07003215 mReadWriteHelper = ReadWriteHelper.DEFAULT;
Jeff Sharkey047238c2012-03-07 16:51:38 -08003216 }
3217
3218 private void destroy() {
3219 if (mNativePtr != 0) {
3220 if (mOwnsNativeParcelObject) {
3221 nativeDestroy(mNativePtr);
Adrian Roos04505652015-10-22 16:12:01 -07003222 updateNativeSize(0);
Jeff Sharkey047238c2012-03-07 16:51:38 -08003223 }
3224 mNativePtr = 0;
3225 }
Makoto Onuki4501c61d2017-07-27 15:56:40 -07003226 mReadWriteHelper = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 }
3228
3229 @Override
3230 protected void finalize() throws Throwable {
3231 if (DEBUG_RECYCLE) {
3232 if (mStack != null) {
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003233 Log.w(TAG, "Client did not call Parcel.recycle()", mStack);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003234 }
3235 }
3236 destroy();
3237 }
3238
Jake Whartonb1f474c2018-05-30 15:11:13 -04003239 /* package */ void readMapInternal(@NonNull Map outVal, int N,
3240 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003241 while (N > 0) {
3242 Object key = readValue(loader);
3243 Object value = readValue(loader);
3244 outVal.put(key, value);
3245 N--;
3246 }
3247 }
3248
Jake Whartonb1f474c2018-05-30 15:11:13 -04003249 /* package */ void readArrayMapInternal(@NonNull ArrayMap outVal, int N,
3250 @Nullable ClassLoader loader) {
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003251 if (DEBUG_ARRAY_MAP) {
3252 RuntimeException here = new RuntimeException("here");
3253 here.fillInStackTrace();
3254 Log.d(TAG, "Reading " + N + " ArrayMap entries", here);
3255 }
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003256 int startPos;
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003257 while (N > 0) {
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003258 if (DEBUG_ARRAY_MAP) startPos = dataPosition();
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003259 String key = readString();
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003260 Object value = readValue(loader);
Dianne Hackborn8aee64d2013-10-25 10:41:50 -07003261 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read #" + (N-1) + " "
3262 + (dataPosition()-startPos) + " bytes: key=0x"
3263 + Integer.toHexString((key != null ? key.hashCode() : 0)) + " " + key);
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003264 outVal.append(key, value);
3265 N--;
3266 }
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003267 outVal.validate();
Dianne Hackbornb87655b2013-07-17 19:06:22 -07003268 }
3269
Jake Whartonb1f474c2018-05-30 15:11:13 -04003270 /* package */ void readArrayMapSafelyInternal(@NonNull ArrayMap outVal, int N,
3271 @Nullable ClassLoader loader) {
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003272 if (DEBUG_ARRAY_MAP) {
3273 RuntimeException here = new RuntimeException("here");
3274 here.fillInStackTrace();
3275 Log.d(TAG, "Reading safely " + N + " ArrayMap entries", here);
3276 }
3277 while (N > 0) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003278 String key = readString();
Dianne Hackborne784d1e2013-09-20 18:13:52 -07003279 if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read safe #" + (N-1) + ": key=0x"
3280 + (key != null ? key.hashCode() : 0) + " " + key);
3281 Object value = readValue(loader);
3282 outVal.put(key, value);
3283 N--;
3284 }
3285 }
3286
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003287 /**
3288 * @hide For testing only.
3289 */
Andrei Onea24ec3212019-03-15 17:35:05 +00003290 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04003291 public void readArrayMap(@NonNull ArrayMap outVal, @Nullable ClassLoader loader) {
Dianne Hackborn9c3e74f2014-08-13 15:39:50 -07003292 final int N = readInt();
3293 if (N < 0) {
3294 return;
3295 }
3296 readArrayMapInternal(outVal, N, loader);
3297 }
3298
Svet Ganovddb94882016-06-23 19:55:24 -07003299 /**
3300 * Reads an array set.
3301 *
3302 * @param loader The class loader to use.
3303 *
3304 * @hide
3305 */
Andrei Onea24ec3212019-03-15 17:35:05 +00003306 @UnsupportedAppUsage
Jake Whartonb1f474c2018-05-30 15:11:13 -04003307 public @Nullable ArraySet<? extends Object> readArraySet(@Nullable ClassLoader loader) {
Svet Ganovddb94882016-06-23 19:55:24 -07003308 final int size = readInt();
3309 if (size < 0) {
3310 return null;
3311 }
3312 ArraySet<Object> result = new ArraySet<>(size);
3313 for (int i = 0; i < size; i++) {
3314 Object value = readValue(loader);
3315 result.append(value);
3316 }
3317 return result;
3318 }
3319
Jake Whartonb1f474c2018-05-30 15:11:13 -04003320 private void readListInternal(@NonNull List outVal, int N,
3321 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 while (N > 0) {
3323 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003324 //Log.d(TAG, "Unmarshalling value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003325 outVal.add(value);
3326 N--;
3327 }
3328 }
3329
Jake Whartonb1f474c2018-05-30 15:11:13 -04003330 private void readArrayInternal(@NonNull Object[] outVal, int N,
3331 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003332 for (int i = 0; i < N; i++) {
3333 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003334 //Log.d(TAG, "Unmarshalling value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003335 outVal[i] = value;
3336 }
3337 }
3338
Jake Whartonb1f474c2018-05-30 15:11:13 -04003339 private void readSparseArrayInternal(@NonNull SparseArray outVal, int N,
3340 @Nullable ClassLoader loader) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003341 while (N > 0) {
3342 int key = readInt();
3343 Object value = readValue(loader);
Brad Fitzpatrick5b747192010-07-12 11:05:38 -07003344 //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003345 outVal.append(key, value);
3346 N--;
3347 }
3348 }
3349
3350
Jake Whartonb1f474c2018-05-30 15:11:13 -04003351 private void readSparseBooleanArrayInternal(@NonNull SparseBooleanArray outVal, int N) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003352 while (N > 0) {
3353 int key = readInt();
3354 boolean value = this.readByte() == 1;
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 }
Dan Sandler5ce04302015-04-09 23:50:15 -04003360
Jake Whartonb1f474c2018-05-30 15:11:13 -04003361 private void readSparseIntArrayInternal(@NonNull SparseIntArray outVal, int N) {
Adam Lesinski4e862812016-11-21 16:02:24 -08003362 while (N > 0) {
3363 int key = readInt();
3364 int value = readInt();
3365 outVal.append(key, value);
3366 N--;
3367 }
3368 }
3369
Dan Sandler5ce04302015-04-09 23:50:15 -04003370 /**
3371 * @hide For testing
3372 */
3373 public long getBlobAshmemSize() {
3374 return nativeGetBlobAshmemSize(mNativePtr);
3375 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003376}