The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.os; |
| 18 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Svet Ganov | ddb9488 | 2016-06-23 19:55:24 -0700 | [diff] [blame] | 20 | import android.annotation.Nullable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.text.TextUtils; |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 22 | import android.util.ArrayMap; |
Svet Ganov | ddb9488 | 2016-06-23 19:55:24 -0700 | [diff] [blame] | 23 | import android.util.ArraySet; |
Fyodor Kupolov | 3b946f4 | 2017-11-27 10:40:46 -0800 | [diff] [blame] | 24 | import android.util.ExceptionUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.util.Log; |
Jeff Sharkey | 5ef3398 | 2014-09-04 18:13:39 -0700 | [diff] [blame] | 26 | import android.util.Size; |
| 27 | import android.util.SizeF; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.util.SparseArray; |
| 29 | import android.util.SparseBooleanArray; |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 30 | import android.util.SparseIntArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | |
Makoto Onuki | b148b6c | 2017-06-27 13:38:38 -0700 | [diff] [blame] | 32 | import dalvik.annotation.optimization.CriticalNative; |
Jeff Sharkey | 789a8fc | 2017-04-16 13:18:35 -0600 | [diff] [blame] | 33 | import dalvik.annotation.optimization.FastNative; |
| 34 | import dalvik.system.VMRuntime; |
| 35 | |
Pete Gillin | 60f55a25 | 2018-05-10 15:40:32 +0100 | [diff] [blame] | 36 | import libcore.util.ArrayUtils; |
Jeff Sharkey | e628b7d | 2017-01-17 13:50:20 -0700 | [diff] [blame] | 37 | import libcore.util.SneakyThrow; |
| 38 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import java.io.ByteArrayInputStream; |
| 40 | import java.io.ByteArrayOutputStream; |
| 41 | import java.io.FileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | import java.io.IOException; |
| 43 | import java.io.ObjectInputStream; |
| 44 | import java.io.ObjectOutputStream; |
John Spurlock | 5002b8c | 2014-01-10 13:32:12 -0500 | [diff] [blame] | 45 | import java.io.ObjectStreamClass; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import java.io.Serializable; |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 47 | import java.lang.reflect.Array; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import java.lang.reflect.Field; |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 49 | import java.lang.reflect.Modifier; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | import java.util.ArrayList; |
| 51 | import java.util.HashMap; |
| 52 | import java.util.List; |
| 53 | import java.util.Map; |
| 54 | import java.util.Set; |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 55 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | /** |
| 57 | * Container for a message (data and object references) that can |
| 58 | * be sent through an IBinder. A Parcel can contain both flattened data |
| 59 | * that will be unflattened on the other side of the IPC (using the various |
| 60 | * methods here for writing specific types, or the general |
| 61 | * {@link Parcelable} interface), and references to live {@link IBinder} |
| 62 | * objects that will result in the other side receiving a proxy IBinder |
| 63 | * connected with the original IBinder in the Parcel. |
| 64 | * |
| 65 | * <p class="note">Parcel is <strong>not</strong> a general-purpose |
| 66 | * serialization mechanism. This class (and the corresponding |
| 67 | * {@link Parcelable} API for placing arbitrary objects into a Parcel) is |
| 68 | * designed as a high-performance IPC transport. As such, it is not |
| 69 | * appropriate to place any Parcel data in to persistent storage: changes |
| 70 | * in the underlying implementation of any of the data in the Parcel can |
| 71 | * render older data unreadable.</p> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 72 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | * <p>The bulk of the Parcel API revolves around reading and writing data |
| 74 | * of various types. There are six major classes of such functions available.</p> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 75 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | * <h3>Primitives</h3> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 77 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | * <p>The most basic data functions are for writing and reading primitive |
| 79 | * data types: {@link #writeByte}, {@link #readByte}, {@link #writeDouble}, |
| 80 | * {@link #readDouble}, {@link #writeFloat}, {@link #readFloat}, {@link #writeInt}, |
| 81 | * {@link #readInt}, {@link #writeLong}, {@link #readLong}, |
| 82 | * {@link #writeString}, {@link #readString}. Most other |
| 83 | * data operations are built on top of these. The given data is written and |
| 84 | * read using the endianess of the host CPU.</p> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 85 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | * <h3>Primitive Arrays</h3> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 87 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | * <p>There are a variety of methods for reading and writing raw arrays |
| 89 | * of primitive objects, which generally result in writing a 4-byte length |
| 90 | * followed by the primitive data items. The methods for reading can either |
| 91 | * read the data into an existing array, or create and return a new array. |
| 92 | * These available types are:</p> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 93 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | * <ul> |
| 95 | * <li> {@link #writeBooleanArray(boolean[])}, |
| 96 | * {@link #readBooleanArray(boolean[])}, {@link #createBooleanArray()} |
| 97 | * <li> {@link #writeByteArray(byte[])}, |
| 98 | * {@link #writeByteArray(byte[], int, int)}, {@link #readByteArray(byte[])}, |
| 99 | * {@link #createByteArray()} |
| 100 | * <li> {@link #writeCharArray(char[])}, {@link #readCharArray(char[])}, |
| 101 | * {@link #createCharArray()} |
| 102 | * <li> {@link #writeDoubleArray(double[])}, {@link #readDoubleArray(double[])}, |
| 103 | * {@link #createDoubleArray()} |
| 104 | * <li> {@link #writeFloatArray(float[])}, {@link #readFloatArray(float[])}, |
| 105 | * {@link #createFloatArray()} |
| 106 | * <li> {@link #writeIntArray(int[])}, {@link #readIntArray(int[])}, |
| 107 | * {@link #createIntArray()} |
| 108 | * <li> {@link #writeLongArray(long[])}, {@link #readLongArray(long[])}, |
| 109 | * {@link #createLongArray()} |
| 110 | * <li> {@link #writeStringArray(String[])}, {@link #readStringArray(String[])}, |
| 111 | * {@link #createStringArray()}. |
| 112 | * <li> {@link #writeSparseBooleanArray(SparseBooleanArray)}, |
| 113 | * {@link #readSparseBooleanArray()}. |
| 114 | * </ul> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 115 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | * <h3>Parcelables</h3> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 117 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | * <p>The {@link Parcelable} protocol provides an extremely efficient (but |
| 119 | * low-level) protocol for objects to write and read themselves from Parcels. |
| 120 | * You can use the direct methods {@link #writeParcelable(Parcelable, int)} |
| 121 | * and {@link #readParcelable(ClassLoader)} or |
| 122 | * {@link #writeParcelableArray} and |
| 123 | * {@link #readParcelableArray(ClassLoader)} to write or read. These |
| 124 | * methods write both the class type and its data to the Parcel, allowing |
| 125 | * that class to be reconstructed from the appropriate class loader when |
| 126 | * later reading.</p> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 127 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | * <p>There are also some methods that provide a more efficient way to work |
Jens Ole Lauridsen | 8dea874 | 2015-04-20 11:18:51 -0700 | [diff] [blame] | 129 | * with Parcelables: {@link #writeTypedObject}, {@link #writeTypedArray}, |
| 130 | * {@link #writeTypedList}, {@link #readTypedObject}, |
| 131 | * {@link #createTypedArray} and {@link #createTypedArrayList}. These methods |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | * do not write the class information of the original object: instead, the |
| 133 | * caller of the read function must know what type to expect and pass in the |
| 134 | * appropriate {@link Parcelable.Creator Parcelable.Creator} instead to |
| 135 | * properly construct the new object and read its data. (To more efficient |
Bin Chen | b6b12b5 | 2017-07-11 11:01:44 +0800 | [diff] [blame] | 136 | * write and read a single Parcelable object that is not null, you can directly |
Jens Ole Lauridsen | 8dea874 | 2015-04-20 11:18:51 -0700 | [diff] [blame] | 137 | * call {@link Parcelable#writeToParcel Parcelable.writeToParcel} and |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | * {@link Parcelable.Creator#createFromParcel Parcelable.Creator.createFromParcel} |
| 139 | * yourself.)</p> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 140 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | * <h3>Bundles</h3> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 142 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | * <p>A special type-safe container, called {@link Bundle}, is available |
| 144 | * for key/value maps of heterogeneous values. This has many optimizations |
| 145 | * for improved performance when reading and writing data, and its type-safe |
| 146 | * API avoids difficult to debug type errors when finally marshalling the |
| 147 | * data contents into a Parcel. The methods to use are |
| 148 | * {@link #writeBundle(Bundle)}, {@link #readBundle()}, and |
| 149 | * {@link #readBundle(ClassLoader)}. |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 150 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | * <h3>Active Objects</h3> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 152 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | * <p>An unusual feature of Parcel is the ability to read and write active |
| 154 | * objects. For these objects the actual contents of the object is not |
| 155 | * written, rather a special token referencing the object is written. When |
| 156 | * reading the object back from the Parcel, you do not get a new instance of |
| 157 | * the object, but rather a handle that operates on the exact same object that |
| 158 | * was originally written. There are two forms of active objects available.</p> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 159 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | * <p>{@link Binder} objects are a core facility of Android's general cross-process |
| 161 | * communication system. The {@link IBinder} interface describes an abstract |
| 162 | * protocol with a Binder object. Any such interface can be written in to |
| 163 | * a Parcel, and upon reading you will receive either the original object |
| 164 | * implementing that interface or a special proxy implementation |
| 165 | * that communicates calls back to the original object. The methods to use are |
| 166 | * {@link #writeStrongBinder(IBinder)}, |
| 167 | * {@link #writeStrongInterface(IInterface)}, {@link #readStrongBinder()}, |
| 168 | * {@link #writeBinderArray(IBinder[])}, {@link #readBinderArray(IBinder[])}, |
| 169 | * {@link #createBinderArray()}, |
| 170 | * {@link #writeBinderList(List)}, {@link #readBinderList(List)}, |
| 171 | * {@link #createBinderArrayList()}.</p> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 172 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | * <p>FileDescriptor objects, representing raw Linux file descriptor identifiers, |
| 174 | * can be written and {@link ParcelFileDescriptor} objects returned to operate |
| 175 | * on the original file descriptor. The returned file descriptor is a dup |
| 176 | * of the original file descriptor: the object and fd is different, but |
| 177 | * operating on the same underlying file stream, with the same position, etc. |
| 178 | * The methods to use are {@link #writeFileDescriptor(FileDescriptor)}, |
| 179 | * {@link #readFileDescriptor()}. |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 180 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | * <h3>Untyped Containers</h3> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 182 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | * <p>A final class of methods are for writing and reading standard Java |
| 184 | * containers of arbitrary types. These all revolve around the |
| 185 | * {@link #writeValue(Object)} and {@link #readValue(ClassLoader)} methods |
| 186 | * which define the types of objects allowed. The container methods are |
| 187 | * {@link #writeArray(Object[])}, {@link #readArray(ClassLoader)}, |
| 188 | * {@link #writeList(List)}, {@link #readList(List, ClassLoader)}, |
| 189 | * {@link #readArrayList(ClassLoader)}, |
| 190 | * {@link #writeMap(Map)}, {@link #readMap(Map, ClassLoader)}, |
| 191 | * {@link #writeSparseArray(SparseArray)}, |
| 192 | * {@link #readSparseArray(ClassLoader)}. |
| 193 | */ |
| 194 | public final class Parcel { |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 195 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | private static final boolean DEBUG_RECYCLE = false; |
Dianne Hackborn | e784d1e | 2013-09-20 18:13:52 -0700 | [diff] [blame] | 197 | private static final boolean DEBUG_ARRAY_MAP = false; |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 198 | private static final String TAG = "Parcel"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | |
| 200 | @SuppressWarnings({"UnusedDeclaration"}) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 201 | private long mNativePtr; // used by native code |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 202 | |
| 203 | /** |
| 204 | * Flag indicating if {@link #mNativePtr} was allocated by this object, |
| 205 | * indicating that we're responsible for its lifecycle. |
| 206 | */ |
| 207 | private boolean mOwnsNativeParcelObject; |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 208 | private long mNativeSize; |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 209 | |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 210 | private ArrayMap<Class, Object> mClassCookies; |
| 211 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | private RuntimeException mStack; |
| 213 | |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 214 | /** |
| 215 | * Whether or not to parcel the stack trace of an exception. This has a performance |
| 216 | * impact, so should only be included in specific processes and only on debug builds. |
| 217 | */ |
| 218 | private static boolean sParcelExceptionStackTrace; |
| 219 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | private static final int POOL_SIZE = 6; |
| 221 | private static final Parcel[] sOwnedPool = new Parcel[POOL_SIZE]; |
| 222 | private static final Parcel[] sHolderPool = new Parcel[POOL_SIZE]; |
| 223 | |
Robert Quattlebaum | 9cd7af3 | 2017-01-04 13:28:01 -0800 | [diff] [blame] | 224 | // Keep in sync with frameworks/native/include/private/binder/ParcelValTypes.h. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | private static final int VAL_NULL = -1; |
| 226 | private static final int VAL_STRING = 0; |
| 227 | private static final int VAL_INTEGER = 1; |
| 228 | private static final int VAL_MAP = 2; |
| 229 | private static final int VAL_BUNDLE = 3; |
| 230 | private static final int VAL_PARCELABLE = 4; |
| 231 | private static final int VAL_SHORT = 5; |
| 232 | private static final int VAL_LONG = 6; |
| 233 | private static final int VAL_FLOAT = 7; |
| 234 | private static final int VAL_DOUBLE = 8; |
| 235 | private static final int VAL_BOOLEAN = 9; |
| 236 | private static final int VAL_CHARSEQUENCE = 10; |
| 237 | private static final int VAL_LIST = 11; |
| 238 | private static final int VAL_SPARSEARRAY = 12; |
| 239 | private static final int VAL_BYTEARRAY = 13; |
| 240 | private static final int VAL_STRINGARRAY = 14; |
| 241 | private static final int VAL_IBINDER = 15; |
| 242 | private static final int VAL_PARCELABLEARRAY = 16; |
| 243 | private static final int VAL_OBJECTARRAY = 17; |
| 244 | private static final int VAL_INTARRAY = 18; |
| 245 | private static final int VAL_LONGARRAY = 19; |
| 246 | private static final int VAL_BYTE = 20; |
| 247 | private static final int VAL_SERIALIZABLE = 21; |
| 248 | private static final int VAL_SPARSEBOOLEANARRAY = 22; |
| 249 | private static final int VAL_BOOLEANARRAY = 23; |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 250 | private static final int VAL_CHARSEQUENCEARRAY = 24; |
Craig Mautner | 719e6b1 | 2014-04-04 20:29:41 -0700 | [diff] [blame] | 251 | private static final int VAL_PERSISTABLEBUNDLE = 25; |
Jeff Sharkey | 5ef3398 | 2014-09-04 18:13:39 -0700 | [diff] [blame] | 252 | private static final int VAL_SIZE = 26; |
| 253 | private static final int VAL_SIZEF = 27; |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 254 | private static final int VAL_DOUBLEARRAY = 28; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 256 | // The initial int32 in a Binder call's reply Parcel header: |
Christopher Wiley | 80fd120 | 2015-11-22 17:12:37 -0800 | [diff] [blame] | 257 | // Keep these in sync with libbinder's binder/Status.h. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | private static final int EX_SECURITY = -1; |
| 259 | private static final int EX_BAD_PARCELABLE = -2; |
| 260 | private static final int EX_ILLEGAL_ARGUMENT = -3; |
| 261 | private static final int EX_NULL_POINTER = -4; |
| 262 | private static final int EX_ILLEGAL_STATE = -5; |
Dianne Hackborn | 7e71442 | 2013-09-13 17:32:57 -0700 | [diff] [blame] | 263 | private static final int EX_NETWORK_MAIN_THREAD = -6; |
Dianne Hackborn | 33d738a | 2014-09-12 14:23:58 -0700 | [diff] [blame] | 264 | private static final int EX_UNSUPPORTED_OPERATION = -7; |
Christopher Wiley | 80fd120 | 2015-11-22 17:12:37 -0800 | [diff] [blame] | 265 | private static final int EX_SERVICE_SPECIFIC = -8; |
Jeff Sharkey | e628b7d | 2017-01-17 13:50:20 -0700 | [diff] [blame] | 266 | private static final int EX_PARCELABLE = -9; |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 267 | private static final int EX_HAS_REPLY_HEADER = -128; // special; see below |
Christopher Wiley | 80fd120 | 2015-11-22 17:12:37 -0800 | [diff] [blame] | 268 | // EX_TRANSACTION_FAILED is used exclusively in native code. |
| 269 | // see libbinder's binder/Status.h |
| 270 | private static final int EX_TRANSACTION_FAILED = -129; |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 271 | |
Makoto Onuki | b148b6c | 2017-06-27 13:38:38 -0700 | [diff] [blame] | 272 | @CriticalNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 273 | private static native int nativeDataSize(long nativePtr); |
Makoto Onuki | b148b6c | 2017-06-27 13:38:38 -0700 | [diff] [blame] | 274 | @CriticalNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 275 | private static native int nativeDataAvail(long nativePtr); |
Makoto Onuki | b148b6c | 2017-06-27 13:38:38 -0700 | [diff] [blame] | 276 | @CriticalNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 277 | private static native int nativeDataPosition(long nativePtr); |
Makoto Onuki | b148b6c | 2017-06-27 13:38:38 -0700 | [diff] [blame] | 278 | @CriticalNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 279 | private static native int nativeDataCapacity(long nativePtr); |
John Reck | 71207b5 | 2016-09-28 13:28:09 -0700 | [diff] [blame] | 280 | @FastNative |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 281 | private static native long nativeSetDataSize(long nativePtr, int size); |
Makoto Onuki | b148b6c | 2017-06-27 13:38:38 -0700 | [diff] [blame] | 282 | @CriticalNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 283 | private static native void nativeSetDataPosition(long nativePtr, int pos); |
John Reck | 71207b5 | 2016-09-28 13:28:09 -0700 | [diff] [blame] | 284 | @FastNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 285 | private static native void nativeSetDataCapacity(long nativePtr, int size); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 286 | |
Makoto Onuki | b148b6c | 2017-06-27 13:38:38 -0700 | [diff] [blame] | 287 | @CriticalNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 288 | private static native boolean nativePushAllowFds(long nativePtr, boolean allowFds); |
Makoto Onuki | b148b6c | 2017-06-27 13:38:38 -0700 | [diff] [blame] | 289 | @CriticalNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 290 | private static native void nativeRestoreAllowFds(long nativePtr, boolean lastValue); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 291 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 292 | private static native void nativeWriteByteArray(long nativePtr, byte[] b, int offset, int len); |
Sandeep Siddhartha | 90d7a3e | 2014-07-25 16:19:42 -0700 | [diff] [blame] | 293 | private static native void nativeWriteBlob(long nativePtr, byte[] b, int offset, int len); |
John Reck | 71207b5 | 2016-09-28 13:28:09 -0700 | [diff] [blame] | 294 | @FastNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 295 | private static native void nativeWriteInt(long nativePtr, int val); |
John Reck | 71207b5 | 2016-09-28 13:28:09 -0700 | [diff] [blame] | 296 | @FastNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 297 | private static native void nativeWriteLong(long nativePtr, long val); |
John Reck | 71207b5 | 2016-09-28 13:28:09 -0700 | [diff] [blame] | 298 | @FastNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 299 | private static native void nativeWriteFloat(long nativePtr, float val); |
John Reck | 71207b5 | 2016-09-28 13:28:09 -0700 | [diff] [blame] | 300 | @FastNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 301 | private static native void nativeWriteDouble(long nativePtr, double val); |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 302 | static native void nativeWriteString(long nativePtr, String val); |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 303 | private static native void nativeWriteStrongBinder(long nativePtr, IBinder val); |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 304 | private static native long nativeWriteFileDescriptor(long nativePtr, FileDescriptor val); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 305 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 306 | private static native byte[] nativeCreateByteArray(long nativePtr); |
Jocelyn Dang | 4610044 | 2017-05-05 15:40:49 -0700 | [diff] [blame] | 307 | private static native boolean nativeReadByteArray(long nativePtr, byte[] dest, int destLen); |
Sandeep Siddhartha | 90d7a3e | 2014-07-25 16:19:42 -0700 | [diff] [blame] | 308 | private static native byte[] nativeReadBlob(long nativePtr); |
Makoto Onuki | b148b6c | 2017-06-27 13:38:38 -0700 | [diff] [blame] | 309 | @CriticalNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 310 | private static native int nativeReadInt(long nativePtr); |
Makoto Onuki | b148b6c | 2017-06-27 13:38:38 -0700 | [diff] [blame] | 311 | @CriticalNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 312 | private static native long nativeReadLong(long nativePtr); |
Makoto Onuki | b148b6c | 2017-06-27 13:38:38 -0700 | [diff] [blame] | 313 | @CriticalNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 314 | private static native float nativeReadFloat(long nativePtr); |
Makoto Onuki | b148b6c | 2017-06-27 13:38:38 -0700 | [diff] [blame] | 315 | @CriticalNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 316 | private static native double nativeReadDouble(long nativePtr); |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 317 | static native String nativeReadString(long nativePtr); |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 318 | private static native IBinder nativeReadStrongBinder(long nativePtr); |
| 319 | private static native FileDescriptor nativeReadFileDescriptor(long nativePtr); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 320 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 321 | private static native long nativeCreate(); |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 322 | private static native long nativeFreeBuffer(long nativePtr); |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 323 | private static native void nativeDestroy(long nativePtr); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 324 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 325 | private static native byte[] nativeMarshall(long nativePtr); |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 326 | private static native long nativeUnmarshall( |
John Spurlock | e085236 | 2015-02-04 15:47:40 -0500 | [diff] [blame] | 327 | long nativePtr, byte[] data, int offset, int length); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 328 | private static native int nativeCompareData(long thisNativePtr, long otherNativePtr); |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 329 | private static native long nativeAppendFrom( |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 330 | long thisNativePtr, long otherNativePtr, int offset, int length); |
Makoto Onuki | b148b6c | 2017-06-27 13:38:38 -0700 | [diff] [blame] | 331 | @CriticalNative |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 332 | private static native boolean nativeHasFileDescriptors(long nativePtr); |
| 333 | private static native void nativeWriteInterfaceToken(long nativePtr, String interfaceName); |
| 334 | private static native void nativeEnforceInterface(long nativePtr, String interfaceName); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 335 | |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 336 | /** Last time exception with a stack trace was written */ |
| 337 | private static volatile long sLastWriteExceptionStackTrace; |
| 338 | /** Used for throttling of writing stack trace, which is costly */ |
| 339 | private static final int WRITE_EXCEPTION_STACK_TRACE_THRESHOLD_MS = 1000; |
| 340 | |
Makoto Onuki | b148b6c | 2017-06-27 13:38:38 -0700 | [diff] [blame] | 341 | @CriticalNative |
Dan Sandler | aa86166 | 2015-04-21 10:24:32 -0400 | [diff] [blame] | 342 | private static native long nativeGetBlobAshmemSize(long nativePtr); |
Dan Sandler | 5ce0430 | 2015-04-09 23:50:15 -0400 | [diff] [blame] | 343 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | public final static Parcelable.Creator<String> STRING_CREATOR |
| 345 | = new Parcelable.Creator<String>() { |
| 346 | public String createFromParcel(Parcel source) { |
| 347 | return source.readString(); |
| 348 | } |
| 349 | public String[] newArray(int size) { |
| 350 | return new String[size]; |
| 351 | } |
| 352 | }; |
| 353 | |
| 354 | /** |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 355 | * @hide |
| 356 | */ |
| 357 | public static class ReadWriteHelper { |
| 358 | public static final ReadWriteHelper DEFAULT = new ReadWriteHelper(); |
| 359 | |
| 360 | /** |
| 361 | * Called when writing a string to a parcel. Subclasses wanting to write a string |
| 362 | * must use {@link #writeStringNoHelper(String)} to avoid |
| 363 | * infinity recursive calls. |
| 364 | */ |
| 365 | public void writeString(Parcel p, String s) { |
| 366 | nativeWriteString(p.mNativePtr, s); |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Called when reading a string to a parcel. Subclasses wanting to read a string |
| 371 | * must use {@link #readStringNoHelper()} to avoid |
| 372 | * infinity recursive calls. |
| 373 | */ |
| 374 | public String readString(Parcel p) { |
| 375 | return nativeReadString(p.mNativePtr); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | private ReadWriteHelper mReadWriteHelper = ReadWriteHelper.DEFAULT; |
| 380 | |
| 381 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 382 | * Retrieve a new Parcel object from the pool. |
| 383 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 384 | @NonNull |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 385 | public static Parcel obtain() { |
| 386 | final Parcel[] pool = sOwnedPool; |
| 387 | synchronized (pool) { |
| 388 | Parcel p; |
| 389 | for (int i=0; i<POOL_SIZE; i++) { |
| 390 | p = pool[i]; |
| 391 | if (p != null) { |
| 392 | pool[i] = null; |
| 393 | if (DEBUG_RECYCLE) { |
| 394 | p.mStack = new RuntimeException(); |
| 395 | } |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 396 | p.mReadWriteHelper = ReadWriteHelper.DEFAULT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 397 | return p; |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | return new Parcel(0); |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Put a Parcel object back into the pool. You must not touch |
| 406 | * the object after this call. |
| 407 | */ |
| 408 | public final void recycle() { |
| 409 | if (DEBUG_RECYCLE) mStack = null; |
| 410 | freeBuffer(); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 411 | |
| 412 | final Parcel[] pool; |
| 413 | if (mOwnsNativeParcelObject) { |
| 414 | pool = sOwnedPool; |
| 415 | } else { |
| 416 | mNativePtr = 0; |
| 417 | pool = sHolderPool; |
| 418 | } |
| 419 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 420 | synchronized (pool) { |
| 421 | for (int i=0; i<POOL_SIZE; i++) { |
| 422 | if (pool[i] == null) { |
| 423 | pool[i] = this; |
| 424 | return; |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 430 | /** |
| 431 | * Set a {@link ReadWriteHelper}, which can be used to avoid having duplicate strings, for |
| 432 | * example. |
| 433 | * |
| 434 | * @hide |
| 435 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 436 | public void setReadWriteHelper(@Nullable ReadWriteHelper helper) { |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 437 | mReadWriteHelper = helper != null ? helper : ReadWriteHelper.DEFAULT; |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * @return whether this parcel has a {@link ReadWriteHelper}. |
| 442 | * |
| 443 | * @hide |
| 444 | */ |
| 445 | public boolean hasReadWriteHelper() { |
| 446 | return (mReadWriteHelper != null) && (mReadWriteHelper != ReadWriteHelper.DEFAULT); |
| 447 | } |
| 448 | |
Dianne Hackborn | fabb70b | 2014-11-11 12:22:36 -0800 | [diff] [blame] | 449 | /** @hide */ |
| 450 | public static native long getGlobalAllocSize(); |
| 451 | |
| 452 | /** @hide */ |
| 453 | public static native long getGlobalAllocCount(); |
| 454 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 455 | /** |
| 456 | * Returns the total amount of data contained in the parcel. |
| 457 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 458 | public final int dataSize() { |
| 459 | return nativeDataSize(mNativePtr); |
| 460 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 461 | |
| 462 | /** |
| 463 | * Returns the amount of data remaining to be read from the |
| 464 | * parcel. That is, {@link #dataSize}-{@link #dataPosition}. |
| 465 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 466 | public final int dataAvail() { |
| 467 | return nativeDataAvail(mNativePtr); |
| 468 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | |
| 470 | /** |
| 471 | * Returns the current position in the parcel data. Never |
| 472 | * more than {@link #dataSize}. |
| 473 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 474 | public final int dataPosition() { |
| 475 | return nativeDataPosition(mNativePtr); |
| 476 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 477 | |
| 478 | /** |
| 479 | * Returns the total amount of space in the parcel. This is always |
| 480 | * >= {@link #dataSize}. The difference between it and dataSize() is the |
| 481 | * amount of room left until the parcel needs to re-allocate its |
| 482 | * data buffer. |
| 483 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 484 | public final int dataCapacity() { |
| 485 | return nativeDataCapacity(mNativePtr); |
| 486 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 487 | |
| 488 | /** |
| 489 | * Change the amount of data in the parcel. Can be either smaller or |
| 490 | * larger than the current size. If larger than the current capacity, |
| 491 | * more memory will be allocated. |
| 492 | * |
| 493 | * @param size The new number of bytes in the Parcel. |
| 494 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 495 | public final void setDataSize(int size) { |
Michael Wachenschwanz | 138bebf | 2017-05-18 22:09:18 +0000 | [diff] [blame] | 496 | updateNativeSize(nativeSetDataSize(mNativePtr, size)); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 497 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | |
| 499 | /** |
| 500 | * Move the current read/write position in the parcel. |
| 501 | * @param pos New offset in the parcel; must be between 0 and |
| 502 | * {@link #dataSize}. |
| 503 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 504 | public final void setDataPosition(int pos) { |
| 505 | nativeSetDataPosition(mNativePtr, pos); |
| 506 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 507 | |
| 508 | /** |
| 509 | * Change the capacity (current available space) of the parcel. |
| 510 | * |
| 511 | * @param size The new capacity of the parcel, in bytes. Can not be |
| 512 | * less than {@link #dataSize} -- that is, you can not drop existing data |
| 513 | * with this method. |
| 514 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 515 | public final void setDataCapacity(int size) { |
| 516 | nativeSetDataCapacity(mNativePtr, size); |
| 517 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 518 | |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 519 | /** @hide */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 520 | public final boolean pushAllowFds(boolean allowFds) { |
| 521 | return nativePushAllowFds(mNativePtr, allowFds); |
| 522 | } |
Dianne Hackborn | c04db7e | 2011-10-03 21:09:35 -0700 | [diff] [blame] | 523 | |
| 524 | /** @hide */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 525 | public final void restoreAllowFds(boolean lastValue) { |
| 526 | nativeRestoreAllowFds(mNativePtr, lastValue); |
| 527 | } |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 528 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 529 | /** |
| 530 | * Returns the raw bytes of the parcel. |
| 531 | * |
| 532 | * <p class="note">The data you retrieve here <strong>must not</strong> |
| 533 | * be placed in any kind of persistent storage (on local disk, across |
| 534 | * a network, etc). For that, you should use standard serialization |
| 535 | * or another kind of general serialization mechanism. The Parcel |
| 536 | * marshalled representation is highly optimized for local IPC, and as |
| 537 | * such does not attempt to maintain compatibility with data created |
| 538 | * in different versions of the platform. |
| 539 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 540 | public final byte[] marshall() { |
| 541 | return nativeMarshall(mNativePtr); |
| 542 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 543 | |
| 544 | /** |
| 545 | * Set the bytes in data to be the raw bytes of this Parcel. |
| 546 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 547 | public final void unmarshall(@NonNull byte[] data, int offset, int length) { |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 548 | updateNativeSize(nativeUnmarshall(mNativePtr, data, offset, length)); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 549 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 550 | |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 551 | public final void appendFrom(Parcel parcel, int offset, int length) { |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 552 | updateNativeSize(nativeAppendFrom(mNativePtr, parcel.mNativePtr, offset, length)); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 553 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 554 | |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 555 | /** @hide */ |
| 556 | public final int compareData(Parcel other) { |
| 557 | return nativeCompareData(mNativePtr, other.mNativePtr); |
| 558 | } |
| 559 | |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 560 | /** @hide */ |
| 561 | public final void setClassCookie(Class clz, Object cookie) { |
| 562 | if (mClassCookies == null) { |
| 563 | mClassCookies = new ArrayMap<>(); |
| 564 | } |
| 565 | mClassCookies.put(clz, cookie); |
| 566 | } |
| 567 | |
| 568 | /** @hide */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 569 | @Nullable |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 570 | public final Object getClassCookie(Class clz) { |
| 571 | return mClassCookies != null ? mClassCookies.get(clz) : null; |
| 572 | } |
| 573 | |
| 574 | /** @hide */ |
| 575 | public final void adoptClassCookies(Parcel from) { |
| 576 | mClassCookies = from.mClassCookies; |
| 577 | } |
| 578 | |
Adrian Roos | fb92184 | 2017-10-26 14:49:56 +0200 | [diff] [blame] | 579 | /** @hide */ |
| 580 | public Map<Class, Object> copyClassCookies() { |
| 581 | return new ArrayMap<>(mClassCookies); |
| 582 | } |
| 583 | |
| 584 | /** @hide */ |
| 585 | public void putClassCookies(Map<Class, Object> cookies) { |
| 586 | if (cookies == null) { |
| 587 | return; |
| 588 | } |
| 589 | if (mClassCookies == null) { |
| 590 | mClassCookies = new ArrayMap<>(); |
| 591 | } |
| 592 | mClassCookies.putAll(cookies); |
| 593 | } |
| 594 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | /** |
| 596 | * Report whether the parcel contains any marshalled file descriptors. |
| 597 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 598 | public final boolean hasFileDescriptors() { |
| 599 | return nativeHasFileDescriptors(mNativePtr); |
| 600 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 601 | |
| 602 | /** |
| 603 | * Store or read an IBinder interface token in the parcel at the current |
| 604 | * {@link #dataPosition}. This is used to validate that the marshalled |
| 605 | * transaction is intended for the target interface. |
| 606 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 607 | public final void writeInterfaceToken(String interfaceName) { |
| 608 | nativeWriteInterfaceToken(mNativePtr, interfaceName); |
| 609 | } |
| 610 | |
| 611 | public final void enforceInterface(String interfaceName) { |
| 612 | nativeEnforceInterface(mNativePtr, interfaceName); |
| 613 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 614 | |
| 615 | /** |
Elliott Hughes | a28b83e | 2011-02-28 14:26:13 -0800 | [diff] [blame] | 616 | * Write a byte array into the parcel at the current {@link #dataPosition}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 617 | * growing {@link #dataCapacity} if needed. |
| 618 | * @param b Bytes to place into the parcel. |
| 619 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 620 | public final void writeByteArray(@Nullable byte[] b) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 621 | writeByteArray(b, 0, (b != null) ? b.length : 0); |
| 622 | } |
| 623 | |
| 624 | /** |
Ken Wakasa | f76a50c | 2012-03-09 19:56:35 +0900 | [diff] [blame] | 625 | * Write a byte array into the parcel at the current {@link #dataPosition}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 626 | * growing {@link #dataCapacity} if needed. |
| 627 | * @param b Bytes to place into the parcel. |
| 628 | * @param offset Index of first byte to be written. |
| 629 | * @param len Number of bytes to write. |
| 630 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 631 | public final void writeByteArray(@Nullable byte[] b, int offset, int len) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 632 | if (b == null) { |
| 633 | writeInt(-1); |
| 634 | return; |
| 635 | } |
Pete Gillin | 60f55a25 | 2018-05-10 15:40:32 +0100 | [diff] [blame] | 636 | ArrayUtils.throwsIfOutOfBounds(b.length, offset, len); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 637 | nativeWriteByteArray(mNativePtr, b, offset, len); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 638 | } |
| 639 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 640 | /** |
Sandeep Siddhartha | 90d7a3e | 2014-07-25 16:19:42 -0700 | [diff] [blame] | 641 | * Write a blob of data into the parcel at the current {@link #dataPosition}, |
| 642 | * growing {@link #dataCapacity} if needed. |
| 643 | * @param b Bytes to place into the parcel. |
| 644 | * {@hide} |
Sandeep Siddhartha | 39c12fa | 2014-07-25 18:37:29 -0700 | [diff] [blame] | 645 | * {@SystemApi} |
Sandeep Siddhartha | 90d7a3e | 2014-07-25 16:19:42 -0700 | [diff] [blame] | 646 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 647 | public final void writeBlob(@Nullable byte[] b) { |
Dan Sandler | b9f7aac3 | 2015-03-04 13:08:49 -0500 | [diff] [blame] | 648 | writeBlob(b, 0, (b != null) ? b.length : 0); |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * Write a blob of data into the parcel at the current {@link #dataPosition}, |
| 653 | * growing {@link #dataCapacity} if needed. |
| 654 | * @param b Bytes to place into the parcel. |
| 655 | * @param offset Index of first byte to be written. |
| 656 | * @param len Number of bytes to write. |
| 657 | * {@hide} |
| 658 | * {@SystemApi} |
| 659 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 660 | public final void writeBlob(@Nullable byte[] b, int offset, int len) { |
Dan Sandler | b9f7aac3 | 2015-03-04 13:08:49 -0500 | [diff] [blame] | 661 | if (b == null) { |
| 662 | writeInt(-1); |
| 663 | return; |
| 664 | } |
Pete Gillin | 60f55a25 | 2018-05-10 15:40:32 +0100 | [diff] [blame] | 665 | ArrayUtils.throwsIfOutOfBounds(b.length, offset, len); |
Dan Sandler | b9f7aac3 | 2015-03-04 13:08:49 -0500 | [diff] [blame] | 666 | nativeWriteBlob(mNativePtr, b, offset, len); |
Sandeep Siddhartha | 90d7a3e | 2014-07-25 16:19:42 -0700 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 670 | * Write an integer value into the parcel at the current dataPosition(), |
| 671 | * growing dataCapacity() if needed. |
| 672 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 673 | public final void writeInt(int val) { |
| 674 | nativeWriteInt(mNativePtr, val); |
| 675 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 676 | |
| 677 | /** |
| 678 | * Write a long integer value into the parcel at the current dataPosition(), |
| 679 | * growing dataCapacity() if needed. |
| 680 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 681 | public final void writeLong(long val) { |
| 682 | nativeWriteLong(mNativePtr, val); |
| 683 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 684 | |
| 685 | /** |
| 686 | * Write a floating point value into the parcel at the current |
| 687 | * dataPosition(), growing dataCapacity() if needed. |
| 688 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 689 | public final void writeFloat(float val) { |
| 690 | nativeWriteFloat(mNativePtr, val); |
| 691 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 692 | |
| 693 | /** |
| 694 | * Write a double precision floating point value into the parcel at the |
| 695 | * current dataPosition(), growing dataCapacity() if needed. |
| 696 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 697 | public final void writeDouble(double val) { |
| 698 | nativeWriteDouble(mNativePtr, val); |
| 699 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 700 | |
| 701 | /** |
| 702 | * Write a string value into the parcel at the current dataPosition(), |
| 703 | * growing dataCapacity() if needed. |
| 704 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 705 | public final void writeString(@Nullable String val) { |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 706 | mReadWriteHelper.writeString(this, val); |
| 707 | } |
| 708 | |
| 709 | /** |
| 710 | * Write a string without going though a {@link ReadWriteHelper}. Subclasses of |
| 711 | * {@link ReadWriteHelper} must use this method instead of {@link #writeString} to avoid |
| 712 | * infinity recursive calls. |
| 713 | * |
| 714 | * @hide |
| 715 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 716 | public void writeStringNoHelper(@Nullable String val) { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 717 | nativeWriteString(mNativePtr, val); |
| 718 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 719 | |
Jake Wharton | 2ffa431 | 2018-04-23 17:47:14 -0400 | [diff] [blame] | 720 | /** |
| 721 | * Write a boolean value into the parcel at the current dataPosition(), |
| 722 | * growing dataCapacity() if needed. |
| 723 | * |
| 724 | * <p>Note: This method currently delegates to writeInt with a value of 1 or 0 |
| 725 | * for true or false, respectively, but may change in the future. |
| 726 | */ |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 727 | public final void writeBoolean(boolean val) { |
| 728 | writeInt(val ? 1 : 0); |
| 729 | } |
| 730 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 731 | /** |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 732 | * Write a CharSequence value into the parcel at the current dataPosition(), |
| 733 | * growing dataCapacity() if needed. |
| 734 | * @hide |
| 735 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 736 | public final void writeCharSequence(@Nullable CharSequence val) { |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 737 | TextUtils.writeToParcel(val, this, 0); |
| 738 | } |
| 739 | |
| 740 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 741 | * Write an object into the parcel at the current dataPosition(), |
| 742 | * growing dataCapacity() if needed. |
| 743 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 744 | public final void writeStrongBinder(IBinder val) { |
| 745 | nativeWriteStrongBinder(mNativePtr, val); |
| 746 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 747 | |
| 748 | /** |
| 749 | * Write an object into the parcel at the current dataPosition(), |
| 750 | * growing dataCapacity() if needed. |
| 751 | */ |
| 752 | public final void writeStrongInterface(IInterface val) { |
| 753 | writeStrongBinder(val == null ? null : val.asBinder()); |
| 754 | } |
| 755 | |
| 756 | /** |
| 757 | * Write a FileDescriptor into the parcel at the current dataPosition(), |
| 758 | * growing dataCapacity() if needed. |
Dan Egnor | b3e4ef3 | 2010-07-20 09:03:35 -0700 | [diff] [blame] | 759 | * |
| 760 | * <p class="caution">The file descriptor will not be closed, which may |
| 761 | * result in file descriptor leaks when objects are returned from Binder |
| 762 | * calls. Use {@link ParcelFileDescriptor#writeToParcel} instead, which |
| 763 | * accepts contextual flags and will close the original file descriptor |
| 764 | * if {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE} is set.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 765 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 766 | public final void writeFileDescriptor(@NonNull FileDescriptor val) { |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 767 | updateNativeSize(nativeWriteFileDescriptor(mNativePtr, val)); |
| 768 | } |
| 769 | |
| 770 | private void updateNativeSize(long newNativeSize) { |
| 771 | if (mOwnsNativeParcelObject) { |
| 772 | if (newNativeSize > Integer.MAX_VALUE) { |
| 773 | newNativeSize = Integer.MAX_VALUE; |
| 774 | } |
| 775 | if (newNativeSize != mNativeSize) { |
| 776 | int delta = (int) (newNativeSize - mNativeSize); |
| 777 | if (delta > 0) { |
| 778 | VMRuntime.getRuntime().registerNativeAllocation(delta); |
| 779 | } else { |
| 780 | VMRuntime.getRuntime().registerNativeFree(-delta); |
| 781 | } |
| 782 | mNativeSize = newNativeSize; |
| 783 | } |
| 784 | } |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 785 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 786 | |
| 787 | /** |
Casey Dahlin | 2f974b2 | 2015-11-05 12:19:13 -0800 | [diff] [blame] | 788 | * {@hide} |
| 789 | * This will be the new name for writeFileDescriptor, for consistency. |
| 790 | **/ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 791 | public final void writeRawFileDescriptor(@NonNull FileDescriptor val) { |
Casey Dahlin | 2f974b2 | 2015-11-05 12:19:13 -0800 | [diff] [blame] | 792 | nativeWriteFileDescriptor(mNativePtr, val); |
| 793 | } |
| 794 | |
| 795 | /** |
| 796 | * {@hide} |
| 797 | * Write an array of FileDescriptor objects into the Parcel. |
| 798 | * |
| 799 | * @param value The array of objects to be written. |
| 800 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 801 | public final void writeRawFileDescriptorArray(@Nullable FileDescriptor[] value) { |
Casey Dahlin | 2f974b2 | 2015-11-05 12:19:13 -0800 | [diff] [blame] | 802 | if (value != null) { |
| 803 | int N = value.length; |
| 804 | writeInt(N); |
| 805 | for (int i=0; i<N; i++) { |
| 806 | writeRawFileDescriptor(value[i]); |
| 807 | } |
| 808 | } else { |
| 809 | writeInt(-1); |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | /** |
Ken Wakasa | f76a50c | 2012-03-09 19:56:35 +0900 | [diff] [blame] | 814 | * Write a byte value into the parcel at the current dataPosition(), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 815 | * growing dataCapacity() if needed. |
Jake Wharton | 2ffa431 | 2018-04-23 17:47:14 -0400 | [diff] [blame] | 816 | * |
| 817 | * <p>Note: This method currently delegates to writeInt but may change in |
| 818 | * the future. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 819 | */ |
| 820 | public final void writeByte(byte val) { |
| 821 | writeInt(val); |
| 822 | } |
| 823 | |
| 824 | /** |
| 825 | * Please use {@link #writeBundle} instead. Flattens a Map into the parcel |
| 826 | * at the current dataPosition(), |
| 827 | * growing dataCapacity() if needed. The Map keys must be String objects. |
| 828 | * The Map values are written using {@link #writeValue} and must follow |
| 829 | * the specification there. |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 830 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 831 | * <p>It is strongly recommended to use {@link #writeBundle} instead of |
| 832 | * this method, since the Bundle class provides a type-safe API that |
| 833 | * allows you to avoid mysterious type errors at the point of marshalling. |
| 834 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 835 | public final void writeMap(@Nullable Map val) { |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 836 | writeMapInternal((Map<String, Object>) val); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | /** |
| 840 | * Flatten a Map into the parcel at the current dataPosition(), |
| 841 | * growing dataCapacity() if needed. The Map keys must be String objects. |
| 842 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 843 | /* package */ void writeMapInternal(@Nullable Map<String,Object> val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 844 | if (val == null) { |
| 845 | writeInt(-1); |
| 846 | return; |
| 847 | } |
| 848 | Set<Map.Entry<String,Object>> entries = val.entrySet(); |
| 849 | writeInt(entries.size()); |
| 850 | for (Map.Entry<String,Object> e : entries) { |
| 851 | writeValue(e.getKey()); |
| 852 | writeValue(e.getValue()); |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | /** |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 857 | * Flatten an ArrayMap into the parcel at the current dataPosition(), |
| 858 | * growing dataCapacity() if needed. The Map keys must be String objects. |
| 859 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 860 | /* package */ void writeArrayMapInternal(@Nullable ArrayMap<String, Object> val) { |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 861 | if (val == null) { |
| 862 | writeInt(-1); |
| 863 | return; |
| 864 | } |
Samuel Tan | 3cefe6a | 2015-12-14 13:29:17 -0800 | [diff] [blame] | 865 | // Keep the format of this Parcel in sync with writeToParcelInner() in |
| 866 | // frameworks/native/libs/binder/PersistableBundle.cpp. |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 867 | final int N = val.size(); |
| 868 | writeInt(N); |
Dianne Hackborn | e784d1e | 2013-09-20 18:13:52 -0700 | [diff] [blame] | 869 | if (DEBUG_ARRAY_MAP) { |
| 870 | RuntimeException here = new RuntimeException("here"); |
| 871 | here.fillInStackTrace(); |
| 872 | Log.d(TAG, "Writing " + N + " ArrayMap entries", here); |
| 873 | } |
Dianne Hackborn | 8aee64d | 2013-10-25 10:41:50 -0700 | [diff] [blame] | 874 | int startPos; |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 875 | for (int i=0; i<N; i++) { |
Dianne Hackborn | 8aee64d | 2013-10-25 10:41:50 -0700 | [diff] [blame] | 876 | if (DEBUG_ARRAY_MAP) startPos = dataPosition(); |
Dianne Hackborn | 9c3e74f | 2014-08-13 15:39:50 -0700 | [diff] [blame] | 877 | writeString(val.keyAt(i)); |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 878 | writeValue(val.valueAt(i)); |
Dianne Hackborn | 8aee64d | 2013-10-25 10:41:50 -0700 | [diff] [blame] | 879 | if (DEBUG_ARRAY_MAP) Log.d(TAG, " Write #" + i + " " |
| 880 | + (dataPosition()-startPos) + " bytes: key=0x" |
| 881 | + Integer.toHexString(val.keyAt(i) != null ? val.keyAt(i).hashCode() : 0) |
| 882 | + " " + val.keyAt(i)); |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 883 | } |
| 884 | } |
| 885 | |
| 886 | /** |
Dianne Hackborn | 9c3e74f | 2014-08-13 15:39:50 -0700 | [diff] [blame] | 887 | * @hide For testing only. |
| 888 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 889 | public void writeArrayMap(@Nullable ArrayMap<String, Object> val) { |
Dianne Hackborn | 9c3e74f | 2014-08-13 15:39:50 -0700 | [diff] [blame] | 890 | writeArrayMapInternal(val); |
| 891 | } |
| 892 | |
| 893 | /** |
Svet Ganov | ddb9488 | 2016-06-23 19:55:24 -0700 | [diff] [blame] | 894 | * Write an array set to the parcel. |
| 895 | * |
| 896 | * @param val The array set to write. |
| 897 | * |
| 898 | * @hide |
| 899 | */ |
| 900 | public void writeArraySet(@Nullable ArraySet<? extends Object> val) { |
| 901 | final int size = (val != null) ? val.size() : -1; |
| 902 | writeInt(size); |
| 903 | for (int i = 0; i < size; i++) { |
| 904 | writeValue(val.valueAt(i)); |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 909 | * Flatten a Bundle into the parcel at the current dataPosition(), |
| 910 | * growing dataCapacity() if needed. |
| 911 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 912 | public final void writeBundle(@Nullable Bundle val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 913 | if (val == null) { |
| 914 | writeInt(-1); |
| 915 | return; |
| 916 | } |
| 917 | |
Dianne Hackborn | 6aff905 | 2009-05-22 13:20:23 -0700 | [diff] [blame] | 918 | val.writeToParcel(this, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | /** |
Craig Mautner | 719e6b1 | 2014-04-04 20:29:41 -0700 | [diff] [blame] | 922 | * Flatten a PersistableBundle into the parcel at the current dataPosition(), |
| 923 | * growing dataCapacity() if needed. |
| 924 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 925 | public final void writePersistableBundle(@Nullable PersistableBundle val) { |
Craig Mautner | 719e6b1 | 2014-04-04 20:29:41 -0700 | [diff] [blame] | 926 | if (val == null) { |
| 927 | writeInt(-1); |
| 928 | return; |
| 929 | } |
| 930 | |
| 931 | val.writeToParcel(this, 0); |
| 932 | } |
| 933 | |
| 934 | /** |
Jeff Sharkey | 5ef3398 | 2014-09-04 18:13:39 -0700 | [diff] [blame] | 935 | * Flatten a Size into the parcel at the current dataPosition(), |
| 936 | * growing dataCapacity() if needed. |
| 937 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 938 | public final void writeSize(@NonNull Size val) { |
Jeff Sharkey | 5ef3398 | 2014-09-04 18:13:39 -0700 | [diff] [blame] | 939 | writeInt(val.getWidth()); |
| 940 | writeInt(val.getHeight()); |
| 941 | } |
| 942 | |
| 943 | /** |
| 944 | * Flatten a SizeF into the parcel at the current dataPosition(), |
| 945 | * growing dataCapacity() if needed. |
| 946 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 947 | public final void writeSizeF(@NonNull SizeF val) { |
Jeff Sharkey | 5ef3398 | 2014-09-04 18:13:39 -0700 | [diff] [blame] | 948 | writeFloat(val.getWidth()); |
| 949 | writeFloat(val.getHeight()); |
| 950 | } |
| 951 | |
| 952 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 953 | * Flatten a List into the parcel at the current dataPosition(), growing |
| 954 | * dataCapacity() if needed. The List values are written using |
| 955 | * {@link #writeValue} and must follow the specification there. |
| 956 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 957 | public final void writeList(@Nullable List val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 958 | if (val == null) { |
| 959 | writeInt(-1); |
| 960 | return; |
| 961 | } |
| 962 | int N = val.size(); |
| 963 | int i=0; |
| 964 | writeInt(N); |
| 965 | while (i < N) { |
| 966 | writeValue(val.get(i)); |
| 967 | i++; |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | /** |
| 972 | * Flatten an Object array into the parcel at the current dataPosition(), |
| 973 | * growing dataCapacity() if needed. The array values are written using |
| 974 | * {@link #writeValue} and must follow the specification there. |
| 975 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 976 | public final void writeArray(@Nullable Object[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 977 | if (val == null) { |
| 978 | writeInt(-1); |
| 979 | return; |
| 980 | } |
| 981 | int N = val.length; |
| 982 | int i=0; |
| 983 | writeInt(N); |
| 984 | while (i < N) { |
| 985 | writeValue(val[i]); |
| 986 | i++; |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | /** |
| 991 | * Flatten a generic SparseArray into the parcel at the current |
| 992 | * dataPosition(), growing dataCapacity() if needed. The SparseArray |
| 993 | * values are written using {@link #writeValue} and must follow the |
| 994 | * specification there. |
| 995 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 996 | public final void writeSparseArray(@Nullable SparseArray<Object> val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 997 | if (val == null) { |
| 998 | writeInt(-1); |
| 999 | return; |
| 1000 | } |
| 1001 | int N = val.size(); |
| 1002 | writeInt(N); |
| 1003 | int i=0; |
| 1004 | while (i < N) { |
| 1005 | writeInt(val.keyAt(i)); |
| 1006 | writeValue(val.valueAt(i)); |
| 1007 | i++; |
| 1008 | } |
| 1009 | } |
| 1010 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1011 | public final void writeSparseBooleanArray(@Nullable SparseBooleanArray val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1012 | if (val == null) { |
| 1013 | writeInt(-1); |
| 1014 | return; |
| 1015 | } |
| 1016 | int N = val.size(); |
| 1017 | writeInt(N); |
| 1018 | int i=0; |
| 1019 | while (i < N) { |
| 1020 | writeInt(val.keyAt(i)); |
| 1021 | writeByte((byte)(val.valueAt(i) ? 1 : 0)); |
| 1022 | i++; |
| 1023 | } |
| 1024 | } |
| 1025 | |
Adam Lesinski | 205656d | 2017-03-23 13:38:26 -0700 | [diff] [blame] | 1026 | /** |
| 1027 | * @hide |
| 1028 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1029 | public final void writeSparseIntArray(@Nullable SparseIntArray val) { |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 1030 | if (val == null) { |
| 1031 | writeInt(-1); |
| 1032 | return; |
| 1033 | } |
| 1034 | int N = val.size(); |
| 1035 | writeInt(N); |
| 1036 | int i=0; |
| 1037 | while (i < N) { |
| 1038 | writeInt(val.keyAt(i)); |
| 1039 | writeInt(val.valueAt(i)); |
| 1040 | i++; |
| 1041 | } |
| 1042 | } |
| 1043 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1044 | public final void writeBooleanArray(@Nullable boolean[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1045 | if (val != null) { |
| 1046 | int N = val.length; |
| 1047 | writeInt(N); |
| 1048 | for (int i=0; i<N; i++) { |
| 1049 | writeInt(val[i] ? 1 : 0); |
| 1050 | } |
| 1051 | } else { |
| 1052 | writeInt(-1); |
| 1053 | } |
| 1054 | } |
| 1055 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1056 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1057 | public final boolean[] createBooleanArray() { |
| 1058 | int N = readInt(); |
| 1059 | // >>2 as a fast divide-by-4 works in the create*Array() functions |
| 1060 | // because dataAvail() will never return a negative number. 4 is |
| 1061 | // the size of a stored boolean in the stream. |
| 1062 | if (N >= 0 && N <= (dataAvail() >> 2)) { |
| 1063 | boolean[] val = new boolean[N]; |
| 1064 | for (int i=0; i<N; i++) { |
| 1065 | val[i] = readInt() != 0; |
| 1066 | } |
| 1067 | return val; |
| 1068 | } else { |
| 1069 | return null; |
| 1070 | } |
| 1071 | } |
| 1072 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1073 | public final void readBooleanArray(@NonNull boolean[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1074 | int N = readInt(); |
| 1075 | if (N == val.length) { |
| 1076 | for (int i=0; i<N; i++) { |
| 1077 | val[i] = readInt() != 0; |
| 1078 | } |
| 1079 | } else { |
| 1080 | throw new RuntimeException("bad array lengths"); |
| 1081 | } |
| 1082 | } |
| 1083 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1084 | public final void writeCharArray(@Nullable char[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1085 | if (val != null) { |
| 1086 | int N = val.length; |
| 1087 | writeInt(N); |
| 1088 | for (int i=0; i<N; i++) { |
| 1089 | writeInt((int)val[i]); |
| 1090 | } |
| 1091 | } else { |
| 1092 | writeInt(-1); |
| 1093 | } |
| 1094 | } |
| 1095 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1096 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1097 | public final char[] createCharArray() { |
| 1098 | int N = readInt(); |
| 1099 | if (N >= 0 && N <= (dataAvail() >> 2)) { |
| 1100 | char[] val = new char[N]; |
| 1101 | for (int i=0; i<N; i++) { |
| 1102 | val[i] = (char)readInt(); |
| 1103 | } |
| 1104 | return val; |
| 1105 | } else { |
| 1106 | return null; |
| 1107 | } |
| 1108 | } |
| 1109 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1110 | public final void readCharArray(@NonNull char[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1111 | int N = readInt(); |
| 1112 | if (N == val.length) { |
| 1113 | for (int i=0; i<N; i++) { |
| 1114 | val[i] = (char)readInt(); |
| 1115 | } |
| 1116 | } else { |
| 1117 | throw new RuntimeException("bad array lengths"); |
| 1118 | } |
| 1119 | } |
| 1120 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1121 | public final void writeIntArray(@Nullable int[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1122 | if (val != null) { |
| 1123 | int N = val.length; |
| 1124 | writeInt(N); |
| 1125 | for (int i=0; i<N; i++) { |
| 1126 | writeInt(val[i]); |
| 1127 | } |
| 1128 | } else { |
| 1129 | writeInt(-1); |
| 1130 | } |
| 1131 | } |
| 1132 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1133 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1134 | public final int[] createIntArray() { |
| 1135 | int N = readInt(); |
| 1136 | if (N >= 0 && N <= (dataAvail() >> 2)) { |
| 1137 | int[] val = new int[N]; |
| 1138 | for (int i=0; i<N; i++) { |
| 1139 | val[i] = readInt(); |
| 1140 | } |
| 1141 | return val; |
| 1142 | } else { |
| 1143 | return null; |
| 1144 | } |
| 1145 | } |
| 1146 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1147 | public final void readIntArray(@NonNull int[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1148 | int N = readInt(); |
| 1149 | if (N == val.length) { |
| 1150 | for (int i=0; i<N; i++) { |
| 1151 | val[i] = readInt(); |
| 1152 | } |
| 1153 | } else { |
| 1154 | throw new RuntimeException("bad array lengths"); |
| 1155 | } |
| 1156 | } |
| 1157 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1158 | public final void writeLongArray(@Nullable long[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1159 | if (val != null) { |
| 1160 | int N = val.length; |
| 1161 | writeInt(N); |
| 1162 | for (int i=0; i<N; i++) { |
| 1163 | writeLong(val[i]); |
| 1164 | } |
| 1165 | } else { |
| 1166 | writeInt(-1); |
| 1167 | } |
| 1168 | } |
| 1169 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1170 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1171 | public final long[] createLongArray() { |
| 1172 | int N = readInt(); |
| 1173 | // >>3 because stored longs are 64 bits |
| 1174 | if (N >= 0 && N <= (dataAvail() >> 3)) { |
| 1175 | long[] val = new long[N]; |
| 1176 | for (int i=0; i<N; i++) { |
| 1177 | val[i] = readLong(); |
| 1178 | } |
| 1179 | return val; |
| 1180 | } else { |
| 1181 | return null; |
| 1182 | } |
| 1183 | } |
| 1184 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1185 | public final void readLongArray(@NonNull long[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1186 | int N = readInt(); |
| 1187 | if (N == val.length) { |
| 1188 | for (int i=0; i<N; i++) { |
| 1189 | val[i] = readLong(); |
| 1190 | } |
| 1191 | } else { |
| 1192 | throw new RuntimeException("bad array lengths"); |
| 1193 | } |
| 1194 | } |
| 1195 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1196 | public final void writeFloatArray(@Nullable float[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1197 | if (val != null) { |
| 1198 | int N = val.length; |
| 1199 | writeInt(N); |
| 1200 | for (int i=0; i<N; i++) { |
| 1201 | writeFloat(val[i]); |
| 1202 | } |
| 1203 | } else { |
| 1204 | writeInt(-1); |
| 1205 | } |
| 1206 | } |
| 1207 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1208 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1209 | public final float[] createFloatArray() { |
| 1210 | int N = readInt(); |
| 1211 | // >>2 because stored floats are 4 bytes |
| 1212 | if (N >= 0 && N <= (dataAvail() >> 2)) { |
| 1213 | float[] val = new float[N]; |
| 1214 | for (int i=0; i<N; i++) { |
| 1215 | val[i] = readFloat(); |
| 1216 | } |
| 1217 | return val; |
| 1218 | } else { |
| 1219 | return null; |
| 1220 | } |
| 1221 | } |
| 1222 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1223 | public final void readFloatArray(@NonNull float[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1224 | int N = readInt(); |
| 1225 | if (N == val.length) { |
| 1226 | for (int i=0; i<N; i++) { |
| 1227 | val[i] = readFloat(); |
| 1228 | } |
| 1229 | } else { |
| 1230 | throw new RuntimeException("bad array lengths"); |
| 1231 | } |
| 1232 | } |
| 1233 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1234 | public final void writeDoubleArray(@Nullable double[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1235 | if (val != null) { |
| 1236 | int N = val.length; |
| 1237 | writeInt(N); |
| 1238 | for (int i=0; i<N; i++) { |
| 1239 | writeDouble(val[i]); |
| 1240 | } |
| 1241 | } else { |
| 1242 | writeInt(-1); |
| 1243 | } |
| 1244 | } |
| 1245 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1246 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1247 | public final double[] createDoubleArray() { |
| 1248 | int N = readInt(); |
| 1249 | // >>3 because stored doubles are 8 bytes |
| 1250 | if (N >= 0 && N <= (dataAvail() >> 3)) { |
| 1251 | double[] val = new double[N]; |
| 1252 | for (int i=0; i<N; i++) { |
| 1253 | val[i] = readDouble(); |
| 1254 | } |
| 1255 | return val; |
| 1256 | } else { |
| 1257 | return null; |
| 1258 | } |
| 1259 | } |
| 1260 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1261 | public final void readDoubleArray(@NonNull double[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1262 | int N = readInt(); |
| 1263 | if (N == val.length) { |
| 1264 | for (int i=0; i<N; i++) { |
| 1265 | val[i] = readDouble(); |
| 1266 | } |
| 1267 | } else { |
| 1268 | throw new RuntimeException("bad array lengths"); |
| 1269 | } |
| 1270 | } |
| 1271 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1272 | public final void writeStringArray(@Nullable String[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1273 | if (val != null) { |
| 1274 | int N = val.length; |
| 1275 | writeInt(N); |
| 1276 | for (int i=0; i<N; i++) { |
| 1277 | writeString(val[i]); |
| 1278 | } |
| 1279 | } else { |
| 1280 | writeInt(-1); |
| 1281 | } |
| 1282 | } |
| 1283 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1284 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1285 | public final String[] createStringArray() { |
| 1286 | int N = readInt(); |
| 1287 | if (N >= 0) { |
| 1288 | String[] val = new String[N]; |
| 1289 | for (int i=0; i<N; i++) { |
| 1290 | val[i] = readString(); |
| 1291 | } |
| 1292 | return val; |
| 1293 | } else { |
| 1294 | return null; |
| 1295 | } |
| 1296 | } |
| 1297 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1298 | public final void readStringArray(@NonNull String[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1299 | int N = readInt(); |
| 1300 | if (N == val.length) { |
| 1301 | for (int i=0; i<N; i++) { |
| 1302 | val[i] = readString(); |
| 1303 | } |
| 1304 | } else { |
| 1305 | throw new RuntimeException("bad array lengths"); |
| 1306 | } |
| 1307 | } |
| 1308 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1309 | public final void writeBinderArray(@Nullable IBinder[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1310 | if (val != null) { |
| 1311 | int N = val.length; |
| 1312 | writeInt(N); |
| 1313 | for (int i=0; i<N; i++) { |
| 1314 | writeStrongBinder(val[i]); |
| 1315 | } |
| 1316 | } else { |
| 1317 | writeInt(-1); |
| 1318 | } |
| 1319 | } |
| 1320 | |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 1321 | /** |
| 1322 | * @hide |
| 1323 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1324 | public final void writeCharSequenceArray(@Nullable CharSequence[] val) { |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 1325 | if (val != null) { |
| 1326 | int N = val.length; |
| 1327 | writeInt(N); |
| 1328 | for (int i=0; i<N; i++) { |
| 1329 | writeCharSequence(val[i]); |
| 1330 | } |
| 1331 | } else { |
| 1332 | writeInt(-1); |
| 1333 | } |
| 1334 | } |
| 1335 | |
Dianne Hackborn | 3d07c94 | 2015-03-13 18:02:54 -0700 | [diff] [blame] | 1336 | /** |
| 1337 | * @hide |
| 1338 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1339 | public final void writeCharSequenceList(@Nullable ArrayList<CharSequence> val) { |
Dianne Hackborn | 3d07c94 | 2015-03-13 18:02:54 -0700 | [diff] [blame] | 1340 | if (val != null) { |
| 1341 | int N = val.size(); |
| 1342 | writeInt(N); |
| 1343 | for (int i=0; i<N; i++) { |
| 1344 | writeCharSequence(val.get(i)); |
| 1345 | } |
| 1346 | } else { |
| 1347 | writeInt(-1); |
| 1348 | } |
| 1349 | } |
| 1350 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1351 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1352 | public final IBinder[] createBinderArray() { |
| 1353 | int N = readInt(); |
| 1354 | if (N >= 0) { |
| 1355 | IBinder[] val = new IBinder[N]; |
| 1356 | for (int i=0; i<N; i++) { |
| 1357 | val[i] = readStrongBinder(); |
| 1358 | } |
| 1359 | return val; |
| 1360 | } else { |
| 1361 | return null; |
| 1362 | } |
| 1363 | } |
| 1364 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1365 | public final void readBinderArray(@NonNull IBinder[] val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1366 | int N = readInt(); |
| 1367 | if (N == val.length) { |
| 1368 | for (int i=0; i<N; i++) { |
| 1369 | val[i] = readStrongBinder(); |
| 1370 | } |
| 1371 | } else { |
| 1372 | throw new RuntimeException("bad array lengths"); |
| 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | /** |
| 1377 | * Flatten a List containing a particular object type into the parcel, at |
| 1378 | * the current dataPosition() and growing dataCapacity() if needed. The |
| 1379 | * type of the objects in the list must be one that implements Parcelable. |
| 1380 | * Unlike the generic writeList() method, however, only the raw data of the |
| 1381 | * objects is written and not their type, so you must use the corresponding |
| 1382 | * readTypedList() to unmarshall them. |
| 1383 | * |
| 1384 | * @param val The list of objects to be written. |
| 1385 | * |
| 1386 | * @see #createTypedArrayList |
| 1387 | * @see #readTypedList |
| 1388 | * @see Parcelable |
| 1389 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1390 | public final <T extends Parcelable> void writeTypedList(@Nullable List<T> val) { |
Sunny Goyal | 0e60f22 | 2017-09-21 21:39:20 -0700 | [diff] [blame] | 1391 | writeTypedList(val, 0); |
| 1392 | } |
| 1393 | |
| 1394 | /** |
| 1395 | * @hide |
| 1396 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1397 | public <T extends Parcelable> void writeTypedList(@Nullable List<T> val, int parcelableFlags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1398 | if (val == null) { |
| 1399 | writeInt(-1); |
| 1400 | return; |
| 1401 | } |
| 1402 | int N = val.size(); |
| 1403 | int i=0; |
| 1404 | writeInt(N); |
| 1405 | while (i < N) { |
Sunny Goyal | 0e60f22 | 2017-09-21 21:39:20 -0700 | [diff] [blame] | 1406 | writeTypedObject(val.get(i), parcelableFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1407 | i++; |
| 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | /** |
| 1412 | * Flatten a List containing String objects into the parcel, at |
| 1413 | * the current dataPosition() and growing dataCapacity() if needed. They |
| 1414 | * can later be retrieved with {@link #createStringArrayList} or |
| 1415 | * {@link #readStringList}. |
| 1416 | * |
| 1417 | * @param val The list of strings to be written. |
| 1418 | * |
| 1419 | * @see #createStringArrayList |
| 1420 | * @see #readStringList |
| 1421 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1422 | public final void writeStringList(@Nullable List<String> val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1423 | if (val == null) { |
| 1424 | writeInt(-1); |
| 1425 | return; |
| 1426 | } |
| 1427 | int N = val.size(); |
| 1428 | int i=0; |
| 1429 | writeInt(N); |
| 1430 | while (i < N) { |
| 1431 | writeString(val.get(i)); |
| 1432 | i++; |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | /** |
| 1437 | * Flatten a List containing IBinder objects into the parcel, at |
| 1438 | * the current dataPosition() and growing dataCapacity() if needed. They |
| 1439 | * can later be retrieved with {@link #createBinderArrayList} or |
| 1440 | * {@link #readBinderList}. |
| 1441 | * |
| 1442 | * @param val The list of strings to be written. |
| 1443 | * |
| 1444 | * @see #createBinderArrayList |
| 1445 | * @see #readBinderList |
| 1446 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1447 | public final void writeBinderList(@Nullable List<IBinder> val) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1448 | if (val == null) { |
| 1449 | writeInt(-1); |
| 1450 | return; |
| 1451 | } |
| 1452 | int N = val.size(); |
| 1453 | int i=0; |
| 1454 | writeInt(N); |
| 1455 | while (i < N) { |
| 1456 | writeStrongBinder(val.get(i)); |
| 1457 | i++; |
| 1458 | } |
| 1459 | } |
| 1460 | |
| 1461 | /** |
Narayan Kamath | bea4871 | 2016-12-01 15:38:28 +0000 | [diff] [blame] | 1462 | * Flatten a {@code List} containing arbitrary {@code Parcelable} objects into this parcel |
| 1463 | * at the current position. They can later be retrieved using |
| 1464 | * {@link #readParcelableList(List, ClassLoader)} if required. |
| 1465 | * |
| 1466 | * @see #readParcelableList(List, ClassLoader) |
Narayan Kamath | bea4871 | 2016-12-01 15:38:28 +0000 | [diff] [blame] | 1467 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1468 | public final <T extends Parcelable> void writeParcelableList(@Nullable List<T> val, int flags) { |
Narayan Kamath | bea4871 | 2016-12-01 15:38:28 +0000 | [diff] [blame] | 1469 | if (val == null) { |
| 1470 | writeInt(-1); |
| 1471 | return; |
| 1472 | } |
| 1473 | |
| 1474 | int N = val.size(); |
| 1475 | int i=0; |
| 1476 | writeInt(N); |
| 1477 | while (i < N) { |
| 1478 | writeParcelable(val.get(i), flags); |
| 1479 | i++; |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | /** |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 1484 | * Flatten a homogeneous array containing a particular object type into |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1485 | * the parcel, at |
| 1486 | * the current dataPosition() and growing dataCapacity() if needed. The |
| 1487 | * type of the objects in the array must be one that implements Parcelable. |
| 1488 | * Unlike the {@link #writeParcelableArray} method, however, only the |
| 1489 | * raw data of the objects is written and not their type, so you must use |
| 1490 | * {@link #readTypedArray} with the correct corresponding |
| 1491 | * {@link Parcelable.Creator} implementation to unmarshall them. |
| 1492 | * |
| 1493 | * @param val The array of objects to be written. |
| 1494 | * @param parcelableFlags Contextual flags as per |
| 1495 | * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}. |
| 1496 | * |
| 1497 | * @see #readTypedArray |
| 1498 | * @see #writeParcelableArray |
| 1499 | * @see Parcelable.Creator |
| 1500 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1501 | public final <T extends Parcelable> void writeTypedArray(@Nullable T[] val, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1502 | int parcelableFlags) { |
| 1503 | if (val != null) { |
| 1504 | int N = val.length; |
| 1505 | writeInt(N); |
Svet Ganov | 0f4928f | 2017-02-02 20:02:51 -0800 | [diff] [blame] | 1506 | for (int i = 0; i < N; i++) { |
Sunny Goyal | 0e60f22 | 2017-09-21 21:39:20 -0700 | [diff] [blame] | 1507 | writeTypedObject(val[i], parcelableFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1508 | } |
| 1509 | } else { |
| 1510 | writeInt(-1); |
| 1511 | } |
| 1512 | } |
| 1513 | |
| 1514 | /** |
Jens Ole Lauridsen | 8dea874 | 2015-04-20 11:18:51 -0700 | [diff] [blame] | 1515 | * Flatten the Parcelable object into the parcel. |
| 1516 | * |
| 1517 | * @param val The Parcelable object to be written. |
| 1518 | * @param parcelableFlags Contextual flags as per |
| 1519 | * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}. |
| 1520 | * |
| 1521 | * @see #readTypedObject |
| 1522 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1523 | public final <T extends Parcelable> void writeTypedObject(@Nullable T val, |
| 1524 | int parcelableFlags) { |
Jens Ole Lauridsen | 8dea874 | 2015-04-20 11:18:51 -0700 | [diff] [blame] | 1525 | if (val != null) { |
| 1526 | writeInt(1); |
| 1527 | val.writeToParcel(this, parcelableFlags); |
| 1528 | } else { |
| 1529 | writeInt(0); |
| 1530 | } |
| 1531 | } |
| 1532 | |
| 1533 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1534 | * Flatten a generic object in to a parcel. The given Object value may |
| 1535 | * currently be one of the following types: |
Dan Egnor | b3e4ef3 | 2010-07-20 09:03:35 -0700 | [diff] [blame] | 1536 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1537 | * <ul> |
| 1538 | * <li> null |
| 1539 | * <li> String |
| 1540 | * <li> Byte |
| 1541 | * <li> Short |
| 1542 | * <li> Integer |
| 1543 | * <li> Long |
| 1544 | * <li> Float |
| 1545 | * <li> Double |
| 1546 | * <li> Boolean |
| 1547 | * <li> String[] |
| 1548 | * <li> boolean[] |
| 1549 | * <li> byte[] |
| 1550 | * <li> int[] |
| 1551 | * <li> long[] |
| 1552 | * <li> Object[] (supporting objects of the same type defined here). |
| 1553 | * <li> {@link Bundle} |
| 1554 | * <li> Map (as supported by {@link #writeMap}). |
| 1555 | * <li> Any object that implements the {@link Parcelable} protocol. |
| 1556 | * <li> Parcelable[] |
| 1557 | * <li> CharSequence (as supported by {@link TextUtils#writeToParcel}). |
| 1558 | * <li> List (as supported by {@link #writeList}). |
Dan Egnor | b3e4ef3 | 2010-07-20 09:03:35 -0700 | [diff] [blame] | 1559 | * <li> {@link SparseArray} (as supported by {@link #writeSparseArray(SparseArray)}). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1560 | * <li> {@link IBinder} |
| 1561 | * <li> Any object that implements Serializable (but see |
| 1562 | * {@link #writeSerializable} for caveats). Note that all of the |
| 1563 | * previous types have relatively efficient implementations for |
| 1564 | * writing to a Parcel; having to rely on the generic serialization |
| 1565 | * approach is much less efficient and should be avoided whenever |
| 1566 | * possible. |
| 1567 | * </ul> |
Dan Egnor | b3e4ef3 | 2010-07-20 09:03:35 -0700 | [diff] [blame] | 1568 | * |
| 1569 | * <p class="caution">{@link Parcelable} objects are written with |
| 1570 | * {@link Parcelable#writeToParcel} using contextual flags of 0. When |
| 1571 | * serializing objects containing {@link ParcelFileDescriptor}s, |
| 1572 | * this may result in file descriptor leaks when they are returned from |
| 1573 | * Binder calls (where {@link Parcelable#PARCELABLE_WRITE_RETURN_VALUE} |
| 1574 | * should be used).</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1575 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1576 | public final void writeValue(@Nullable Object v) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1577 | if (v == null) { |
| 1578 | writeInt(VAL_NULL); |
| 1579 | } else if (v instanceof String) { |
| 1580 | writeInt(VAL_STRING); |
| 1581 | writeString((String) v); |
| 1582 | } else if (v instanceof Integer) { |
| 1583 | writeInt(VAL_INTEGER); |
| 1584 | writeInt((Integer) v); |
| 1585 | } else if (v instanceof Map) { |
| 1586 | writeInt(VAL_MAP); |
| 1587 | writeMap((Map) v); |
| 1588 | } else if (v instanceof Bundle) { |
| 1589 | // Must be before Parcelable |
| 1590 | writeInt(VAL_BUNDLE); |
| 1591 | writeBundle((Bundle) v); |
Samuel Tan | ceafe5e | 2015-12-11 16:50:58 -0800 | [diff] [blame] | 1592 | } else if (v instanceof PersistableBundle) { |
| 1593 | writeInt(VAL_PERSISTABLEBUNDLE); |
| 1594 | writePersistableBundle((PersistableBundle) v); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1595 | } else if (v instanceof Parcelable) { |
Samuel Tan | ceafe5e | 2015-12-11 16:50:58 -0800 | [diff] [blame] | 1596 | // IMPOTANT: cases for classes that implement Parcelable must |
| 1597 | // come before the Parcelable case, so that their specific VAL_* |
| 1598 | // types will be written. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1599 | writeInt(VAL_PARCELABLE); |
| 1600 | writeParcelable((Parcelable) v, 0); |
| 1601 | } else if (v instanceof Short) { |
| 1602 | writeInt(VAL_SHORT); |
| 1603 | writeInt(((Short) v).intValue()); |
| 1604 | } else if (v instanceof Long) { |
| 1605 | writeInt(VAL_LONG); |
| 1606 | writeLong((Long) v); |
| 1607 | } else if (v instanceof Float) { |
| 1608 | writeInt(VAL_FLOAT); |
| 1609 | writeFloat((Float) v); |
| 1610 | } else if (v instanceof Double) { |
| 1611 | writeInt(VAL_DOUBLE); |
| 1612 | writeDouble((Double) v); |
| 1613 | } else if (v instanceof Boolean) { |
| 1614 | writeInt(VAL_BOOLEAN); |
| 1615 | writeInt((Boolean) v ? 1 : 0); |
| 1616 | } else if (v instanceof CharSequence) { |
| 1617 | // Must be after String |
| 1618 | writeInt(VAL_CHARSEQUENCE); |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 1619 | writeCharSequence((CharSequence) v); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1620 | } else if (v instanceof List) { |
| 1621 | writeInt(VAL_LIST); |
| 1622 | writeList((List) v); |
| 1623 | } else if (v instanceof SparseArray) { |
| 1624 | writeInt(VAL_SPARSEARRAY); |
| 1625 | writeSparseArray((SparseArray) v); |
| 1626 | } else if (v instanceof boolean[]) { |
| 1627 | writeInt(VAL_BOOLEANARRAY); |
| 1628 | writeBooleanArray((boolean[]) v); |
| 1629 | } else if (v instanceof byte[]) { |
| 1630 | writeInt(VAL_BYTEARRAY); |
| 1631 | writeByteArray((byte[]) v); |
| 1632 | } else if (v instanceof String[]) { |
| 1633 | writeInt(VAL_STRINGARRAY); |
| 1634 | writeStringArray((String[]) v); |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 1635 | } else if (v instanceof CharSequence[]) { |
| 1636 | // Must be after String[] and before Object[] |
| 1637 | writeInt(VAL_CHARSEQUENCEARRAY); |
| 1638 | writeCharSequenceArray((CharSequence[]) v); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1639 | } else if (v instanceof IBinder) { |
| 1640 | writeInt(VAL_IBINDER); |
| 1641 | writeStrongBinder((IBinder) v); |
| 1642 | } else if (v instanceof Parcelable[]) { |
| 1643 | writeInt(VAL_PARCELABLEARRAY); |
| 1644 | writeParcelableArray((Parcelable[]) v, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1645 | } else if (v instanceof int[]) { |
| 1646 | writeInt(VAL_INTARRAY); |
| 1647 | writeIntArray((int[]) v); |
| 1648 | } else if (v instanceof long[]) { |
| 1649 | writeInt(VAL_LONGARRAY); |
| 1650 | writeLongArray((long[]) v); |
| 1651 | } else if (v instanceof Byte) { |
| 1652 | writeInt(VAL_BYTE); |
| 1653 | writeInt((Byte) v); |
Jeff Sharkey | 5ef3398 | 2014-09-04 18:13:39 -0700 | [diff] [blame] | 1654 | } else if (v instanceof Size) { |
| 1655 | writeInt(VAL_SIZE); |
| 1656 | writeSize((Size) v); |
| 1657 | } else if (v instanceof SizeF) { |
| 1658 | writeInt(VAL_SIZEF); |
| 1659 | writeSizeF((SizeF) v); |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 1660 | } else if (v instanceof double[]) { |
| 1661 | writeInt(VAL_DOUBLEARRAY); |
| 1662 | writeDoubleArray((double[]) v); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1663 | } else { |
Paul Duffin | ac5a082 | 2014-02-03 15:02:17 +0000 | [diff] [blame] | 1664 | Class<?> clazz = v.getClass(); |
| 1665 | if (clazz.isArray() && clazz.getComponentType() == Object.class) { |
| 1666 | // Only pure Object[] are written here, Other arrays of non-primitive types are |
| 1667 | // handled by serialization as this does not record the component type. |
| 1668 | writeInt(VAL_OBJECTARRAY); |
| 1669 | writeArray((Object[]) v); |
| 1670 | } else if (v instanceof Serializable) { |
| 1671 | // Must be last |
| 1672 | writeInt(VAL_SERIALIZABLE); |
| 1673 | writeSerializable((Serializable) v); |
| 1674 | } else { |
| 1675 | throw new RuntimeException("Parcel: unable to marshal value " + v); |
| 1676 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | /** |
| 1681 | * Flatten the name of the class of the Parcelable and its contents |
| 1682 | * into the parcel. |
Dan Egnor | b3e4ef3 | 2010-07-20 09:03:35 -0700 | [diff] [blame] | 1683 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1684 | * @param p The Parcelable object to be written. |
| 1685 | * @param parcelableFlags Contextual flags as per |
| 1686 | * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}. |
| 1687 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1688 | public final void writeParcelable(@Nullable Parcelable p, int parcelableFlags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1689 | if (p == null) { |
| 1690 | writeString(null); |
| 1691 | return; |
| 1692 | } |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 1693 | writeParcelableCreator(p); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1694 | p.writeToParcel(this, parcelableFlags); |
| 1695 | } |
| 1696 | |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 1697 | /** @hide */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1698 | public final void writeParcelableCreator(@NonNull Parcelable p) { |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 1699 | String name = p.getClass().getName(); |
| 1700 | writeString(name); |
| 1701 | } |
| 1702 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1703 | /** |
| 1704 | * Write a generic serializable object in to a Parcel. It is strongly |
| 1705 | * recommended that this method be avoided, since the serialization |
| 1706 | * overhead is extremely large, and this approach will be much slower than |
| 1707 | * using the other approaches to writing data in to a Parcel. |
| 1708 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1709 | public final void writeSerializable(@Nullable Serializable s) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1710 | if (s == null) { |
| 1711 | writeString(null); |
| 1712 | return; |
| 1713 | } |
| 1714 | String name = s.getClass().getName(); |
| 1715 | writeString(name); |
| 1716 | |
| 1717 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 1718 | try { |
| 1719 | ObjectOutputStream oos = new ObjectOutputStream(baos); |
| 1720 | oos.writeObject(s); |
| 1721 | oos.close(); |
| 1722 | |
| 1723 | writeByteArray(baos.toByteArray()); |
| 1724 | } catch (IOException ioe) { |
| 1725 | throw new RuntimeException("Parcelable encountered " + |
| 1726 | "IOException writing serializable object (name = " + name + |
| 1727 | ")", ioe); |
| 1728 | } |
| 1729 | } |
| 1730 | |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 1731 | /** @hide For debugging purposes */ |
| 1732 | public static void setStackTraceParceling(boolean enabled) { |
| 1733 | sParcelExceptionStackTrace = enabled; |
| 1734 | } |
| 1735 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1736 | /** |
| 1737 | * Special function for writing an exception result at the header of |
| 1738 | * a parcel, to be used when returning an exception from a transaction. |
| 1739 | * Note that this currently only supports a few exception types; any other |
| 1740 | * exception will be re-thrown by this function as a RuntimeException |
| 1741 | * (to be caught by the system's last-resort exception handling when |
| 1742 | * dispatching a transaction). |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 1743 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1744 | * <p>The supported exception types are: |
| 1745 | * <ul> |
| 1746 | * <li>{@link BadParcelableException} |
| 1747 | * <li>{@link IllegalArgumentException} |
| 1748 | * <li>{@link IllegalStateException} |
| 1749 | * <li>{@link NullPointerException} |
| 1750 | * <li>{@link SecurityException} |
Dianne Hackborn | 18482ae | 2017-08-01 17:41:00 -0700 | [diff] [blame] | 1751 | * <li>{@link UnsupportedOperationException} |
Dianne Hackborn | 7e71442 | 2013-09-13 17:32:57 -0700 | [diff] [blame] | 1752 | * <li>{@link NetworkOnMainThreadException} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1753 | * </ul> |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 1754 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1755 | * @param e The Exception to be written. |
| 1756 | * |
| 1757 | * @see #writeNoException |
| 1758 | * @see #readException |
| 1759 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 1760 | public final void writeException(@NonNull Exception e) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1761 | int code = 0; |
Jeff Sharkey | e628b7d | 2017-01-17 13:50:20 -0700 | [diff] [blame] | 1762 | if (e instanceof Parcelable |
| 1763 | && (e.getClass().getClassLoader() == Parcelable.class.getClassLoader())) { |
| 1764 | // We only send Parcelable exceptions that are in the |
| 1765 | // BootClassLoader to ensure that the receiver can unpack them |
| 1766 | code = EX_PARCELABLE; |
| 1767 | } else if (e instanceof SecurityException) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1768 | code = EX_SECURITY; |
| 1769 | } else if (e instanceof BadParcelableException) { |
| 1770 | code = EX_BAD_PARCELABLE; |
| 1771 | } else if (e instanceof IllegalArgumentException) { |
| 1772 | code = EX_ILLEGAL_ARGUMENT; |
| 1773 | } else if (e instanceof NullPointerException) { |
| 1774 | code = EX_NULL_POINTER; |
| 1775 | } else if (e instanceof IllegalStateException) { |
| 1776 | code = EX_ILLEGAL_STATE; |
Dianne Hackborn | 7e71442 | 2013-09-13 17:32:57 -0700 | [diff] [blame] | 1777 | } else if (e instanceof NetworkOnMainThreadException) { |
| 1778 | code = EX_NETWORK_MAIN_THREAD; |
Dianne Hackborn | 33d738a | 2014-09-12 14:23:58 -0700 | [diff] [blame] | 1779 | } else if (e instanceof UnsupportedOperationException) { |
| 1780 | code = EX_UNSUPPORTED_OPERATION; |
Christopher Wiley | 80fd120 | 2015-11-22 17:12:37 -0800 | [diff] [blame] | 1781 | } else if (e instanceof ServiceSpecificException) { |
| 1782 | code = EX_SERVICE_SPECIFIC; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1783 | } |
| 1784 | writeInt(code); |
Brad Fitzpatrick | 703e5d3 | 2010-07-15 13:16:41 -0700 | [diff] [blame] | 1785 | StrictMode.clearGatheredViolations(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1786 | if (code == 0) { |
| 1787 | if (e instanceof RuntimeException) { |
| 1788 | throw (RuntimeException) e; |
| 1789 | } |
| 1790 | throw new RuntimeException(e); |
| 1791 | } |
| 1792 | writeString(e.getMessage()); |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 1793 | final long timeNow = sParcelExceptionStackTrace ? SystemClock.elapsedRealtime() : 0; |
| 1794 | if (sParcelExceptionStackTrace && (timeNow - sLastWriteExceptionStackTrace |
| 1795 | > WRITE_EXCEPTION_STACK_TRACE_THRESHOLD_MS)) { |
| 1796 | sLastWriteExceptionStackTrace = timeNow; |
| 1797 | final int sizePosition = dataPosition(); |
| 1798 | writeInt(0); // Header size will be filled in later |
| 1799 | StackTraceElement[] stackTrace = e.getStackTrace(); |
| 1800 | final int truncatedSize = Math.min(stackTrace.length, 5); |
| 1801 | StringBuilder sb = new StringBuilder(); |
| 1802 | for (int i = 0; i < truncatedSize; i++) { |
Fyodor Kupolov | 679d998 | 2017-11-21 10:42:23 -0800 | [diff] [blame] | 1803 | sb.append("\tat ").append(stackTrace[i]).append('\n'); |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 1804 | } |
| 1805 | writeString(sb.toString()); |
| 1806 | final int payloadPosition = dataPosition(); |
| 1807 | setDataPosition(sizePosition); |
| 1808 | // Write stack trace header size. Used in native side to skip the header |
| 1809 | writeInt(payloadPosition - sizePosition); |
| 1810 | setDataPosition(payloadPosition); |
| 1811 | } else { |
| 1812 | writeInt(0); |
| 1813 | } |
Jeff Sharkey | e628b7d | 2017-01-17 13:50:20 -0700 | [diff] [blame] | 1814 | switch (code) { |
| 1815 | case EX_SERVICE_SPECIFIC: |
| 1816 | writeInt(((ServiceSpecificException) e).errorCode); |
| 1817 | break; |
| 1818 | case EX_PARCELABLE: |
| 1819 | // Write parceled exception prefixed by length |
| 1820 | final int sizePosition = dataPosition(); |
| 1821 | writeInt(0); |
| 1822 | writeParcelable((Parcelable) e, Parcelable.PARCELABLE_WRITE_RETURN_VALUE); |
| 1823 | final int payloadPosition = dataPosition(); |
| 1824 | setDataPosition(sizePosition); |
| 1825 | writeInt(payloadPosition - sizePosition); |
| 1826 | setDataPosition(payloadPosition); |
| 1827 | break; |
Christopher Wiley | 80fd120 | 2015-11-22 17:12:37 -0800 | [diff] [blame] | 1828 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1829 | } |
| 1830 | |
| 1831 | /** |
| 1832 | * Special function for writing information at the front of the Parcel |
| 1833 | * indicating that no exception occurred. |
| 1834 | * |
| 1835 | * @see #writeException |
| 1836 | * @see #readException |
| 1837 | */ |
| 1838 | public final void writeNoException() { |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 1839 | // Despite the name of this function ("write no exception"), |
| 1840 | // it should instead be thought of as "write the RPC response |
| 1841 | // header", but because this function name is written out by |
| 1842 | // the AIDL compiler, we're not going to rename it. |
| 1843 | // |
| 1844 | // The response header, in the non-exception case (see also |
| 1845 | // writeException above, also called by the AIDL compiler), is |
| 1846 | // either a 0 (the default case), or EX_HAS_REPLY_HEADER if |
| 1847 | // StrictMode has gathered up violations that have occurred |
| 1848 | // during a Binder call, in which case we write out the number |
| 1849 | // of violations and their details, serialized, before the |
| 1850 | // actual RPC respons data. The receiving end of this is |
| 1851 | // readException(), below. |
| 1852 | if (StrictMode.hasGatheredViolations()) { |
| 1853 | writeInt(EX_HAS_REPLY_HEADER); |
| 1854 | final int sizePosition = dataPosition(); |
| 1855 | writeInt(0); // total size of fat header, to be filled in later |
| 1856 | StrictMode.writeGatheredViolationsToParcel(this); |
| 1857 | final int payloadPosition = dataPosition(); |
| 1858 | setDataPosition(sizePosition); |
| 1859 | writeInt(payloadPosition - sizePosition); // header size |
| 1860 | setDataPosition(payloadPosition); |
| 1861 | } else { |
| 1862 | writeInt(0); |
| 1863 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1864 | } |
| 1865 | |
| 1866 | /** |
| 1867 | * Special function for reading an exception result from the header of |
| 1868 | * a parcel, to be used after receiving the result of a transaction. This |
| 1869 | * will throw the exception for you if it had been written to the Parcel, |
| 1870 | * otherwise return and let you read the normal result data from the Parcel. |
| 1871 | * |
| 1872 | * @see #writeException |
| 1873 | * @see #writeNoException |
| 1874 | */ |
| 1875 | public final void readException() { |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 1876 | int code = readExceptionCode(); |
| 1877 | if (code != 0) { |
| 1878 | String msg = readString(); |
Takamasa Kuramitsu | 2214b82 | 2018-04-02 14:44:59 +0900 | [diff] [blame] | 1879 | readException(code, msg); |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 1880 | } |
| 1881 | } |
| 1882 | |
| 1883 | /** |
| 1884 | * Parses the header of a Binder call's response Parcel and |
| 1885 | * returns the exception code. Deals with lite or fat headers. |
| 1886 | * In the common successful case, this header is generally zero. |
| 1887 | * In less common cases, it's a small negative number and will be |
| 1888 | * followed by an error string. |
| 1889 | * |
| 1890 | * This exists purely for android.database.DatabaseUtils and |
| 1891 | * insulating it from having to handle fat headers as returned by |
| 1892 | * e.g. StrictMode-induced RPC responses. |
| 1893 | * |
| 1894 | * @hide |
| 1895 | */ |
| 1896 | public final int readExceptionCode() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1897 | int code = readInt(); |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 1898 | if (code == EX_HAS_REPLY_HEADER) { |
| 1899 | int headerSize = readInt(); |
| 1900 | if (headerSize == 0) { |
| 1901 | Log.e(TAG, "Unexpected zero-sized Parcel reply header."); |
| 1902 | } else { |
| 1903 | // Currently the only thing in the header is StrictMode stacks, |
| 1904 | // but discussions around event/RPC tracing suggest we might |
| 1905 | // put that here too. If so, switch on sub-header tags here. |
| 1906 | // But for now, just parse out the StrictMode stuff. |
| 1907 | StrictMode.readAndHandleBinderCallViolations(this); |
| 1908 | } |
| 1909 | // And fat response headers are currently only used when |
| 1910 | // there are no exceptions, so return no error: |
| 1911 | return 0; |
| 1912 | } |
| 1913 | return code; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1914 | } |
| 1915 | |
| 1916 | /** |
Mark Doliner | 879ea45 | 2014-01-02 12:38:07 -0800 | [diff] [blame] | 1917 | * Throw an exception with the given message. Not intended for use |
| 1918 | * outside the Parcel class. |
| 1919 | * |
| 1920 | * @param code Used to determine which exception class to throw. |
| 1921 | * @param msg The exception message. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1922 | */ |
| 1923 | public final void readException(int code, String msg) { |
Takamasa Kuramitsu | 2214b82 | 2018-04-02 14:44:59 +0900 | [diff] [blame] | 1924 | String remoteStackTrace = null; |
| 1925 | final int remoteStackPayloadSize = readInt(); |
| 1926 | if (remoteStackPayloadSize > 0) { |
| 1927 | remoteStackTrace = readString(); |
| 1928 | } |
| 1929 | Exception e = createException(code, msg); |
| 1930 | // Attach remote stack trace if availalble |
| 1931 | if (remoteStackTrace != null) { |
| 1932 | RemoteException cause = new RemoteException( |
| 1933 | "Remote stack trace:\n" + remoteStackTrace, null, false, false); |
| 1934 | try { |
| 1935 | Throwable rootCause = ExceptionUtils.getRootCause(e); |
| 1936 | if (rootCause != null) { |
| 1937 | rootCause.initCause(cause); |
| 1938 | } |
| 1939 | } catch (RuntimeException ex) { |
| 1940 | Log.e(TAG, "Cannot set cause " + cause + " for " + e, ex); |
| 1941 | } |
| 1942 | } |
| 1943 | SneakyThrow.sneakyThrow(e); |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 1944 | } |
| 1945 | |
| 1946 | /** |
| 1947 | * Creates an exception with the given message. |
| 1948 | * |
| 1949 | * @param code Used to determine which exception class to throw. |
| 1950 | * @param msg The exception message. |
| 1951 | */ |
| 1952 | private Exception createException(int code, String msg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1953 | switch (code) { |
Jeff Sharkey | e628b7d | 2017-01-17 13:50:20 -0700 | [diff] [blame] | 1954 | case EX_PARCELABLE: |
| 1955 | if (readInt() > 0) { |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 1956 | return (Exception) readParcelable(Parcelable.class.getClassLoader()); |
Jeff Sharkey | e628b7d | 2017-01-17 13:50:20 -0700 | [diff] [blame] | 1957 | } else { |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 1958 | return new RuntimeException(msg + " [missing Parcelable]"); |
Jeff Sharkey | e628b7d | 2017-01-17 13:50:20 -0700 | [diff] [blame] | 1959 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1960 | case EX_SECURITY: |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 1961 | return new SecurityException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1962 | case EX_BAD_PARCELABLE: |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 1963 | return new BadParcelableException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1964 | case EX_ILLEGAL_ARGUMENT: |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 1965 | return new IllegalArgumentException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1966 | case EX_NULL_POINTER: |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 1967 | return new NullPointerException(msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1968 | case EX_ILLEGAL_STATE: |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 1969 | return new IllegalStateException(msg); |
Dianne Hackborn | 7e71442 | 2013-09-13 17:32:57 -0700 | [diff] [blame] | 1970 | case EX_NETWORK_MAIN_THREAD: |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 1971 | return new NetworkOnMainThreadException(); |
Dianne Hackborn | 33d738a | 2014-09-12 14:23:58 -0700 | [diff] [blame] | 1972 | case EX_UNSUPPORTED_OPERATION: |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 1973 | return new UnsupportedOperationException(msg); |
Christopher Wiley | 80fd120 | 2015-11-22 17:12:37 -0800 | [diff] [blame] | 1974 | case EX_SERVICE_SPECIFIC: |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 1975 | return new ServiceSpecificException(readInt(), msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1976 | } |
Fyodor Kupolov | a81b8c0 | 2017-11-13 15:51:03 -0800 | [diff] [blame] | 1977 | return new RuntimeException("Unknown exception code: " + code |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1978 | + " msg " + msg); |
| 1979 | } |
| 1980 | |
| 1981 | /** |
| 1982 | * Read an integer value from the parcel at the current dataPosition(). |
| 1983 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 1984 | public final int readInt() { |
| 1985 | return nativeReadInt(mNativePtr); |
| 1986 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1987 | |
| 1988 | /** |
| 1989 | * Read a long integer value from the parcel at the current dataPosition(). |
| 1990 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 1991 | public final long readLong() { |
| 1992 | return nativeReadLong(mNativePtr); |
| 1993 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1994 | |
| 1995 | /** |
| 1996 | * Read a floating point value from the parcel at the current |
| 1997 | * dataPosition(). |
| 1998 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 1999 | public final float readFloat() { |
| 2000 | return nativeReadFloat(mNativePtr); |
| 2001 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2002 | |
| 2003 | /** |
| 2004 | * Read a double precision floating point value from the parcel at the |
| 2005 | * current dataPosition(). |
| 2006 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 2007 | public final double readDouble() { |
| 2008 | return nativeReadDouble(mNativePtr); |
| 2009 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2010 | |
| 2011 | /** |
| 2012 | * Read a string value from the parcel at the current dataPosition(). |
| 2013 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2014 | @Nullable |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 2015 | public final String readString() { |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 2016 | return mReadWriteHelper.readString(this); |
| 2017 | } |
| 2018 | |
| 2019 | /** |
| 2020 | * Read a string without going though a {@link ReadWriteHelper}. Subclasses of |
| 2021 | * {@link ReadWriteHelper} must use this method instead of {@link #readString} to avoid |
| 2022 | * infinity recursive calls. |
| 2023 | * |
| 2024 | * @hide |
| 2025 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2026 | @Nullable |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 2027 | public String readStringNoHelper() { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 2028 | return nativeReadString(mNativePtr); |
| 2029 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2030 | |
Jake Wharton | 2ffa431 | 2018-04-23 17:47:14 -0400 | [diff] [blame] | 2031 | /** |
| 2032 | * Read a boolean value from the parcel at the current dataPosition(). |
| 2033 | */ |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 2034 | public final boolean readBoolean() { |
| 2035 | return readInt() != 0; |
| 2036 | } |
| 2037 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2038 | /** |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 2039 | * Read a CharSequence value from the parcel at the current dataPosition(). |
| 2040 | * @hide |
| 2041 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2042 | @Nullable |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 2043 | public final CharSequence readCharSequence() { |
| 2044 | return TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(this); |
| 2045 | } |
| 2046 | |
| 2047 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2048 | * Read an object from the parcel at the current dataPosition(). |
| 2049 | */ |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 2050 | public final IBinder readStrongBinder() { |
| 2051 | return nativeReadStrongBinder(mNativePtr); |
| 2052 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2053 | |
| 2054 | /** |
| 2055 | * Read a FileDescriptor from the parcel at the current dataPosition(). |
| 2056 | */ |
| 2057 | public final ParcelFileDescriptor readFileDescriptor() { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 2058 | FileDescriptor fd = nativeReadFileDescriptor(mNativePtr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2059 | return fd != null ? new ParcelFileDescriptor(fd) : null; |
| 2060 | } |
| 2061 | |
Jeff Sharkey | da5a3e1 | 2013-08-11 12:54:42 -0700 | [diff] [blame] | 2062 | /** {@hide} */ |
| 2063 | public final FileDescriptor readRawFileDescriptor() { |
| 2064 | return nativeReadFileDescriptor(mNativePtr); |
| 2065 | } |
| 2066 | |
Casey Dahlin | 2f974b2 | 2015-11-05 12:19:13 -0800 | [diff] [blame] | 2067 | /** |
| 2068 | * {@hide} |
| 2069 | * Read and return a new array of FileDescriptors from the parcel. |
| 2070 | * @return the FileDescriptor array, or null if the array is null. |
| 2071 | **/ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2072 | @Nullable |
Casey Dahlin | 2f974b2 | 2015-11-05 12:19:13 -0800 | [diff] [blame] | 2073 | public final FileDescriptor[] createRawFileDescriptorArray() { |
| 2074 | int N = readInt(); |
| 2075 | if (N < 0) { |
| 2076 | return null; |
| 2077 | } |
| 2078 | FileDescriptor[] f = new FileDescriptor[N]; |
| 2079 | for (int i = 0; i < N; i++) { |
| 2080 | f[i] = readRawFileDescriptor(); |
| 2081 | } |
| 2082 | return f; |
| 2083 | } |
| 2084 | |
| 2085 | /** |
| 2086 | * {@hide} |
| 2087 | * Read an array of FileDescriptors from a parcel. |
| 2088 | * The passed array must be exactly the length of the array in the parcel. |
| 2089 | * @return the FileDescriptor array, or null if the array is null. |
| 2090 | **/ |
| 2091 | public final void readRawFileDescriptorArray(FileDescriptor[] val) { |
| 2092 | int N = readInt(); |
| 2093 | if (N == val.length) { |
| 2094 | for (int i=0; i<N; i++) { |
| 2095 | val[i] = readRawFileDescriptor(); |
| 2096 | } |
| 2097 | } else { |
| 2098 | throw new RuntimeException("bad array lengths"); |
| 2099 | } |
| 2100 | } |
| 2101 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2102 | /** |
| 2103 | * Read a byte value from the parcel at the current dataPosition(). |
| 2104 | */ |
| 2105 | public final byte readByte() { |
| 2106 | return (byte)(readInt() & 0xff); |
| 2107 | } |
| 2108 | |
| 2109 | /** |
| 2110 | * Please use {@link #readBundle(ClassLoader)} instead (whose data must have |
| 2111 | * been written with {@link #writeBundle}. Read into an existing Map object |
| 2112 | * from the parcel at the current dataPosition(). |
| 2113 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2114 | public final void readMap(@NonNull Map outVal, @Nullable ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2115 | int N = readInt(); |
| 2116 | readMapInternal(outVal, N, loader); |
| 2117 | } |
| 2118 | |
| 2119 | /** |
| 2120 | * Read into an existing List object from the parcel at the current |
| 2121 | * dataPosition(), using the given class loader to load any enclosed |
| 2122 | * Parcelables. If it is null, the default class loader is used. |
| 2123 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2124 | public final void readList(@NonNull List outVal, @Nullable ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2125 | int N = readInt(); |
| 2126 | readListInternal(outVal, N, loader); |
| 2127 | } |
| 2128 | |
| 2129 | /** |
| 2130 | * Please use {@link #readBundle(ClassLoader)} instead (whose data must have |
| 2131 | * been written with {@link #writeBundle}. Read and return a new HashMap |
| 2132 | * object from the parcel at the current dataPosition(), using the given |
| 2133 | * class loader to load any enclosed Parcelables. Returns null if |
| 2134 | * the previously written map object was null. |
| 2135 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2136 | @Nullable |
| 2137 | public final HashMap readHashMap(@Nullable ClassLoader loader) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2138 | { |
| 2139 | int N = readInt(); |
| 2140 | if (N < 0) { |
| 2141 | return null; |
| 2142 | } |
| 2143 | HashMap m = new HashMap(N); |
| 2144 | readMapInternal(m, N, loader); |
| 2145 | return m; |
| 2146 | } |
| 2147 | |
| 2148 | /** |
| 2149 | * Read and return a new Bundle object from the parcel at the current |
| 2150 | * dataPosition(). Returns null if the previously written Bundle object was |
| 2151 | * null. |
| 2152 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2153 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2154 | public final Bundle readBundle() { |
| 2155 | return readBundle(null); |
| 2156 | } |
| 2157 | |
| 2158 | /** |
| 2159 | * Read and return a new Bundle object from the parcel at the current |
| 2160 | * dataPosition(), using the given class loader to initialize the class |
| 2161 | * loader of the Bundle for later retrieval of Parcelable objects. |
| 2162 | * Returns null if the previously written Bundle object was null. |
| 2163 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2164 | @Nullable |
| 2165 | public final Bundle readBundle(@Nullable ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2166 | int length = readInt(); |
| 2167 | if (length < 0) { |
Dianne Hackborn | 4a7d824 | 2013-10-03 10:19:20 -0700 | [diff] [blame] | 2168 | if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2169 | return null; |
| 2170 | } |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 2171 | |
Dianne Hackborn | 6aff905 | 2009-05-22 13:20:23 -0700 | [diff] [blame] | 2172 | final Bundle bundle = new Bundle(this, length); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2173 | if (loader != null) { |
| 2174 | bundle.setClassLoader(loader); |
| 2175 | } |
| 2176 | return bundle; |
| 2177 | } |
| 2178 | |
| 2179 | /** |
Craig Mautner | 719e6b1 | 2014-04-04 20:29:41 -0700 | [diff] [blame] | 2180 | * Read and return a new Bundle object from the parcel at the current |
| 2181 | * dataPosition(). Returns null if the previously written Bundle object was |
| 2182 | * null. |
| 2183 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2184 | @Nullable |
Craig Mautner | 719e6b1 | 2014-04-04 20:29:41 -0700 | [diff] [blame] | 2185 | public final PersistableBundle readPersistableBundle() { |
| 2186 | return readPersistableBundle(null); |
| 2187 | } |
| 2188 | |
| 2189 | /** |
| 2190 | * Read and return a new Bundle object from the parcel at the current |
| 2191 | * dataPosition(), using the given class loader to initialize the class |
| 2192 | * loader of the Bundle for later retrieval of Parcelable objects. |
| 2193 | * Returns null if the previously written Bundle object was null. |
| 2194 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2195 | @Nullable |
| 2196 | public final PersistableBundle readPersistableBundle(@Nullable ClassLoader loader) { |
Craig Mautner | 719e6b1 | 2014-04-04 20:29:41 -0700 | [diff] [blame] | 2197 | int length = readInt(); |
| 2198 | if (length < 0) { |
| 2199 | if (Bundle.DEBUG) Log.d(TAG, "null bundle: length=" + length); |
| 2200 | return null; |
| 2201 | } |
| 2202 | |
| 2203 | final PersistableBundle bundle = new PersistableBundle(this, length); |
| 2204 | if (loader != null) { |
| 2205 | bundle.setClassLoader(loader); |
| 2206 | } |
| 2207 | return bundle; |
| 2208 | } |
| 2209 | |
| 2210 | /** |
Jeff Sharkey | 5ef3398 | 2014-09-04 18:13:39 -0700 | [diff] [blame] | 2211 | * Read a Size from the parcel at the current dataPosition(). |
| 2212 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2213 | @NonNull |
Jeff Sharkey | 5ef3398 | 2014-09-04 18:13:39 -0700 | [diff] [blame] | 2214 | public final Size readSize() { |
| 2215 | final int width = readInt(); |
| 2216 | final int height = readInt(); |
| 2217 | return new Size(width, height); |
| 2218 | } |
| 2219 | |
| 2220 | /** |
| 2221 | * Read a SizeF from the parcel at the current dataPosition(). |
| 2222 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2223 | @NonNull |
Jeff Sharkey | 5ef3398 | 2014-09-04 18:13:39 -0700 | [diff] [blame] | 2224 | public final SizeF readSizeF() { |
| 2225 | final float width = readFloat(); |
| 2226 | final float height = readFloat(); |
| 2227 | return new SizeF(width, height); |
| 2228 | } |
| 2229 | |
| 2230 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2231 | * Read and return a byte[] object from the parcel. |
| 2232 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2233 | @Nullable |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 2234 | public final byte[] createByteArray() { |
| 2235 | return nativeCreateByteArray(mNativePtr); |
| 2236 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2237 | |
| 2238 | /** |
| 2239 | * Read a byte[] object from the parcel and copy it into the |
| 2240 | * given byte array. |
| 2241 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2242 | public final void readByteArray(@NonNull byte[] val) { |
Jocelyn Dang | 4610044 | 2017-05-05 15:40:49 -0700 | [diff] [blame] | 2243 | boolean valid = nativeReadByteArray(mNativePtr, val, (val != null) ? val.length : 0); |
| 2244 | if (!valid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2245 | throw new RuntimeException("bad array lengths"); |
| 2246 | } |
| 2247 | } |
| 2248 | |
| 2249 | /** |
Sandeep Siddhartha | 90d7a3e | 2014-07-25 16:19:42 -0700 | [diff] [blame] | 2250 | * Read a blob of data from the parcel and return it as a byte array. |
| 2251 | * {@hide} |
Sandeep Siddhartha | 39c12fa | 2014-07-25 18:37:29 -0700 | [diff] [blame] | 2252 | * {@SystemApi} |
Sandeep Siddhartha | 90d7a3e | 2014-07-25 16:19:42 -0700 | [diff] [blame] | 2253 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2254 | @Nullable |
Sandeep Siddhartha | 90d7a3e | 2014-07-25 16:19:42 -0700 | [diff] [blame] | 2255 | public final byte[] readBlob() { |
| 2256 | return nativeReadBlob(mNativePtr); |
| 2257 | } |
| 2258 | |
| 2259 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2260 | * Read and return a String[] object from the parcel. |
| 2261 | * {@hide} |
| 2262 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2263 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2264 | public final String[] readStringArray() { |
| 2265 | String[] array = null; |
| 2266 | |
| 2267 | int length = readInt(); |
| 2268 | if (length >= 0) |
| 2269 | { |
| 2270 | array = new String[length]; |
| 2271 | |
| 2272 | for (int i = 0 ; i < length ; i++) |
| 2273 | { |
| 2274 | array[i] = readString(); |
| 2275 | } |
| 2276 | } |
| 2277 | |
| 2278 | return array; |
| 2279 | } |
| 2280 | |
| 2281 | /** |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 2282 | * Read and return a CharSequence[] object from the parcel. |
| 2283 | * {@hide} |
| 2284 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2285 | @Nullable |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 2286 | public final CharSequence[] readCharSequenceArray() { |
| 2287 | CharSequence[] array = null; |
| 2288 | |
| 2289 | int length = readInt(); |
| 2290 | if (length >= 0) |
| 2291 | { |
| 2292 | array = new CharSequence[length]; |
| 2293 | |
| 2294 | for (int i = 0 ; i < length ; i++) |
| 2295 | { |
| 2296 | array[i] = readCharSequence(); |
| 2297 | } |
| 2298 | } |
| 2299 | |
| 2300 | return array; |
| 2301 | } |
| 2302 | |
| 2303 | /** |
Dianne Hackborn | 3d07c94 | 2015-03-13 18:02:54 -0700 | [diff] [blame] | 2304 | * Read and return an ArrayList<CharSequence> object from the parcel. |
| 2305 | * {@hide} |
| 2306 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2307 | @Nullable |
Dianne Hackborn | 3d07c94 | 2015-03-13 18:02:54 -0700 | [diff] [blame] | 2308 | public final ArrayList<CharSequence> readCharSequenceList() { |
| 2309 | ArrayList<CharSequence> array = null; |
| 2310 | |
| 2311 | int length = readInt(); |
| 2312 | if (length >= 0) { |
| 2313 | array = new ArrayList<CharSequence>(length); |
| 2314 | |
| 2315 | for (int i = 0 ; i < length ; i++) { |
| 2316 | array.add(readCharSequence()); |
| 2317 | } |
| 2318 | } |
| 2319 | |
| 2320 | return array; |
| 2321 | } |
| 2322 | |
| 2323 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2324 | * Read and return a new ArrayList object from the parcel at the current |
| 2325 | * dataPosition(). Returns null if the previously written list object was |
| 2326 | * null. The given class loader will be used to load any enclosed |
| 2327 | * Parcelables. |
| 2328 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2329 | @Nullable |
| 2330 | public final ArrayList readArrayList(@Nullable ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2331 | int N = readInt(); |
| 2332 | if (N < 0) { |
| 2333 | return null; |
| 2334 | } |
| 2335 | ArrayList l = new ArrayList(N); |
| 2336 | readListInternal(l, N, loader); |
| 2337 | return l; |
| 2338 | } |
| 2339 | |
| 2340 | /** |
| 2341 | * Read and return a new Object array from the parcel at the current |
| 2342 | * dataPosition(). Returns null if the previously written array was |
| 2343 | * null. The given class loader will be used to load any enclosed |
| 2344 | * Parcelables. |
| 2345 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2346 | @Nullable |
| 2347 | public final Object[] readArray(@Nullable ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2348 | int N = readInt(); |
| 2349 | if (N < 0) { |
| 2350 | return null; |
| 2351 | } |
| 2352 | Object[] l = new Object[N]; |
| 2353 | readArrayInternal(l, N, loader); |
| 2354 | return l; |
| 2355 | } |
| 2356 | |
| 2357 | /** |
| 2358 | * Read and return a new SparseArray object from the parcel at the current |
| 2359 | * dataPosition(). Returns null if the previously written list object was |
| 2360 | * null. The given class loader will be used to load any enclosed |
| 2361 | * Parcelables. |
| 2362 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2363 | @Nullable |
| 2364 | public final SparseArray readSparseArray(@Nullable ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2365 | int N = readInt(); |
| 2366 | if (N < 0) { |
| 2367 | return null; |
| 2368 | } |
| 2369 | SparseArray sa = new SparseArray(N); |
| 2370 | readSparseArrayInternal(sa, N, loader); |
| 2371 | return sa; |
| 2372 | } |
| 2373 | |
| 2374 | /** |
| 2375 | * Read and return a new SparseBooleanArray object from the parcel at the current |
| 2376 | * dataPosition(). Returns null if the previously written list object was |
| 2377 | * null. |
| 2378 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2379 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2380 | public final SparseBooleanArray readSparseBooleanArray() { |
| 2381 | int N = readInt(); |
| 2382 | if (N < 0) { |
| 2383 | return null; |
| 2384 | } |
| 2385 | SparseBooleanArray sa = new SparseBooleanArray(N); |
| 2386 | readSparseBooleanArrayInternal(sa, N); |
| 2387 | return sa; |
| 2388 | } |
| 2389 | |
| 2390 | /** |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 2391 | * Read and return a new SparseIntArray object from the parcel at the current |
| 2392 | * dataPosition(). Returns null if the previously written array object was null. |
Adam Lesinski | 205656d | 2017-03-23 13:38:26 -0700 | [diff] [blame] | 2393 | * @hide |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 2394 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2395 | @Nullable |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 2396 | public final SparseIntArray readSparseIntArray() { |
| 2397 | int N = readInt(); |
| 2398 | if (N < 0) { |
| 2399 | return null; |
| 2400 | } |
| 2401 | SparseIntArray sa = new SparseIntArray(N); |
| 2402 | readSparseIntArrayInternal(sa, N); |
| 2403 | return sa; |
| 2404 | } |
| 2405 | |
| 2406 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2407 | * Read and return a new ArrayList containing a particular object type from |
| 2408 | * the parcel that was written with {@link #writeTypedList} at the |
| 2409 | * current dataPosition(). Returns null if the |
| 2410 | * previously written list object was null. The list <em>must</em> have |
| 2411 | * previously been written via {@link #writeTypedList} with the same object |
| 2412 | * type. |
| 2413 | * |
| 2414 | * @return A newly created ArrayList containing objects with the same data |
| 2415 | * as those that were previously written. |
| 2416 | * |
| 2417 | * @see #writeTypedList |
| 2418 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2419 | @Nullable |
| 2420 | public final <T> ArrayList<T> createTypedArrayList(@NonNull Parcelable.Creator<T> c) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2421 | int N = readInt(); |
| 2422 | if (N < 0) { |
| 2423 | return null; |
| 2424 | } |
| 2425 | ArrayList<T> l = new ArrayList<T>(N); |
| 2426 | while (N > 0) { |
Sunny Goyal | 0e60f22 | 2017-09-21 21:39:20 -0700 | [diff] [blame] | 2427 | l.add(readTypedObject(c)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2428 | N--; |
| 2429 | } |
| 2430 | return l; |
| 2431 | } |
| 2432 | |
| 2433 | /** |
| 2434 | * Read into the given List items containing a particular object type |
| 2435 | * that were written with {@link #writeTypedList} at the |
| 2436 | * current dataPosition(). The list <em>must</em> have |
| 2437 | * previously been written via {@link #writeTypedList} with the same object |
| 2438 | * type. |
| 2439 | * |
| 2440 | * @return A newly created ArrayList containing objects with the same data |
| 2441 | * as those that were previously written. |
| 2442 | * |
| 2443 | * @see #writeTypedList |
| 2444 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2445 | public final <T> void readTypedList(@NonNull List<T> list, @NonNull Parcelable.Creator<T> c) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2446 | int M = list.size(); |
| 2447 | int N = readInt(); |
| 2448 | int i = 0; |
| 2449 | for (; i < M && i < N; i++) { |
Sunny Goyal | 0e60f22 | 2017-09-21 21:39:20 -0700 | [diff] [blame] | 2450 | list.set(i, readTypedObject(c)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2451 | } |
| 2452 | for (; i<N; i++) { |
Sunny Goyal | 0e60f22 | 2017-09-21 21:39:20 -0700 | [diff] [blame] | 2453 | list.add(readTypedObject(c)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2454 | } |
| 2455 | for (; i<M; i++) { |
| 2456 | list.remove(N); |
| 2457 | } |
| 2458 | } |
| 2459 | |
| 2460 | /** |
| 2461 | * Read and return a new ArrayList containing String objects from |
| 2462 | * the parcel that was written with {@link #writeStringList} at the |
| 2463 | * current dataPosition(). Returns null if the |
| 2464 | * previously written list object was null. |
| 2465 | * |
| 2466 | * @return A newly created ArrayList containing strings with the same data |
| 2467 | * as those that were previously written. |
| 2468 | * |
| 2469 | * @see #writeStringList |
| 2470 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2471 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2472 | public final ArrayList<String> createStringArrayList() { |
| 2473 | int N = readInt(); |
| 2474 | if (N < 0) { |
| 2475 | return null; |
| 2476 | } |
| 2477 | ArrayList<String> l = new ArrayList<String>(N); |
| 2478 | while (N > 0) { |
| 2479 | l.add(readString()); |
| 2480 | N--; |
| 2481 | } |
| 2482 | return l; |
| 2483 | } |
| 2484 | |
| 2485 | /** |
| 2486 | * Read and return a new ArrayList containing IBinder objects from |
| 2487 | * the parcel that was written with {@link #writeBinderList} at the |
| 2488 | * current dataPosition(). Returns null if the |
| 2489 | * previously written list object was null. |
| 2490 | * |
| 2491 | * @return A newly created ArrayList containing strings with the same data |
| 2492 | * as those that were previously written. |
| 2493 | * |
| 2494 | * @see #writeBinderList |
| 2495 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2496 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2497 | public final ArrayList<IBinder> createBinderArrayList() { |
| 2498 | int N = readInt(); |
| 2499 | if (N < 0) { |
| 2500 | return null; |
| 2501 | } |
| 2502 | ArrayList<IBinder> l = new ArrayList<IBinder>(N); |
| 2503 | while (N > 0) { |
| 2504 | l.add(readStrongBinder()); |
| 2505 | N--; |
| 2506 | } |
| 2507 | return l; |
| 2508 | } |
| 2509 | |
| 2510 | /** |
| 2511 | * Read into the given List items String objects that were written with |
| 2512 | * {@link #writeStringList} at the current dataPosition(). |
| 2513 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2514 | * @see #writeStringList |
| 2515 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2516 | public final void readStringList(@NonNull List<String> list) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2517 | int M = list.size(); |
| 2518 | int N = readInt(); |
| 2519 | int i = 0; |
| 2520 | for (; i < M && i < N; i++) { |
| 2521 | list.set(i, readString()); |
| 2522 | } |
| 2523 | for (; i<N; i++) { |
| 2524 | list.add(readString()); |
| 2525 | } |
| 2526 | for (; i<M; i++) { |
| 2527 | list.remove(N); |
| 2528 | } |
| 2529 | } |
| 2530 | |
| 2531 | /** |
| 2532 | * Read into the given List items IBinder objects that were written with |
| 2533 | * {@link #writeBinderList} at the current dataPosition(). |
| 2534 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2535 | * @see #writeBinderList |
| 2536 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2537 | public final void readBinderList(@NonNull List<IBinder> list) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2538 | int M = list.size(); |
| 2539 | int N = readInt(); |
| 2540 | int i = 0; |
| 2541 | for (; i < M && i < N; i++) { |
| 2542 | list.set(i, readStrongBinder()); |
| 2543 | } |
| 2544 | for (; i<N; i++) { |
| 2545 | list.add(readStrongBinder()); |
| 2546 | } |
| 2547 | for (; i<M; i++) { |
| 2548 | list.remove(N); |
| 2549 | } |
| 2550 | } |
| 2551 | |
| 2552 | /** |
Narayan Kamath | bea4871 | 2016-12-01 15:38:28 +0000 | [diff] [blame] | 2553 | * Read the list of {@code Parcelable} objects at the current data position into the |
| 2554 | * given {@code list}. The contents of the {@code list} are replaced. If the serialized |
| 2555 | * list was {@code null}, {@code list} is cleared. |
| 2556 | * |
| 2557 | * @see #writeParcelableList(List, int) |
Narayan Kamath | bea4871 | 2016-12-01 15:38:28 +0000 | [diff] [blame] | 2558 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2559 | @NonNull |
| 2560 | public final <T extends Parcelable> List<T> readParcelableList(@NonNull List<T> list, |
| 2561 | @Nullable ClassLoader cl) { |
Narayan Kamath | bea4871 | 2016-12-01 15:38:28 +0000 | [diff] [blame] | 2562 | final int N = readInt(); |
| 2563 | if (N == -1) { |
| 2564 | list.clear(); |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 2565 | return list; |
Narayan Kamath | bea4871 | 2016-12-01 15:38:28 +0000 | [diff] [blame] | 2566 | } |
| 2567 | |
| 2568 | final int M = list.size(); |
| 2569 | int i = 0; |
| 2570 | for (; i < M && i < N; i++) { |
| 2571 | list.set(i, (T) readParcelable(cl)); |
| 2572 | } |
| 2573 | for (; i<N; i++) { |
| 2574 | list.add((T) readParcelable(cl)); |
| 2575 | } |
| 2576 | for (; i<M; i++) { |
| 2577 | list.remove(N); |
| 2578 | } |
Eugene Susla | 36e866b | 2017-02-23 18:24:39 -0800 | [diff] [blame] | 2579 | return list; |
Narayan Kamath | bea4871 | 2016-12-01 15:38:28 +0000 | [diff] [blame] | 2580 | } |
| 2581 | |
| 2582 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2583 | * Read and return a new array containing a particular object type from |
| 2584 | * the parcel at the current dataPosition(). Returns null if the |
| 2585 | * previously written array was null. The array <em>must</em> have |
| 2586 | * previously been written via {@link #writeTypedArray} with the same |
| 2587 | * object type. |
| 2588 | * |
| 2589 | * @return A newly created array containing objects with the same data |
| 2590 | * as those that were previously written. |
| 2591 | * |
| 2592 | * @see #writeTypedArray |
| 2593 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2594 | @Nullable |
| 2595 | public final <T> T[] createTypedArray(@NonNull Parcelable.Creator<T> c) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2596 | int N = readInt(); |
| 2597 | if (N < 0) { |
| 2598 | return null; |
| 2599 | } |
| 2600 | T[] l = c.newArray(N); |
| 2601 | for (int i=0; i<N; i++) { |
Sunny Goyal | 0e60f22 | 2017-09-21 21:39:20 -0700 | [diff] [blame] | 2602 | l[i] = readTypedObject(c); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2603 | } |
| 2604 | return l; |
| 2605 | } |
| 2606 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2607 | public final <T> void readTypedArray(@NonNull T[] val, @NonNull Parcelable.Creator<T> c) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2608 | int N = readInt(); |
| 2609 | if (N == val.length) { |
| 2610 | for (int i=0; i<N; i++) { |
Sunny Goyal | 0e60f22 | 2017-09-21 21:39:20 -0700 | [diff] [blame] | 2611 | val[i] = readTypedObject(c); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2612 | } |
| 2613 | } else { |
| 2614 | throw new RuntimeException("bad array lengths"); |
| 2615 | } |
| 2616 | } |
| 2617 | |
| 2618 | /** |
| 2619 | * @deprecated |
| 2620 | * @hide |
| 2621 | */ |
| 2622 | @Deprecated |
| 2623 | public final <T> T[] readTypedArray(Parcelable.Creator<T> c) { |
| 2624 | return createTypedArray(c); |
| 2625 | } |
| 2626 | |
| 2627 | /** |
Jens Ole Lauridsen | 8dea874 | 2015-04-20 11:18:51 -0700 | [diff] [blame] | 2628 | * Read and return a typed Parcelable object from a parcel. |
| 2629 | * Returns null if the previous written object was null. |
| 2630 | * The object <em>must</em> have previous been written via |
| 2631 | * {@link #writeTypedObject} with the same object type. |
| 2632 | * |
| 2633 | * @return A newly created object of the type that was previously |
| 2634 | * written. |
| 2635 | * |
| 2636 | * @see #writeTypedObject |
| 2637 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2638 | @Nullable |
| 2639 | public final <T> T readTypedObject(@NonNull Parcelable.Creator<T> c) { |
Jens Ole Lauridsen | 8dea874 | 2015-04-20 11:18:51 -0700 | [diff] [blame] | 2640 | if (readInt() != 0) { |
| 2641 | return c.createFromParcel(this); |
| 2642 | } else { |
| 2643 | return null; |
| 2644 | } |
| 2645 | } |
| 2646 | |
| 2647 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2648 | * Write a heterogeneous array of Parcelable objects into the Parcel. |
| 2649 | * Each object in the array is written along with its class name, so |
| 2650 | * that the correct class can later be instantiated. As a result, this |
| 2651 | * has significantly more overhead than {@link #writeTypedArray}, but will |
| 2652 | * correctly handle an array containing more than one type of object. |
| 2653 | * |
| 2654 | * @param value The array of objects to be written. |
| 2655 | * @param parcelableFlags Contextual flags as per |
| 2656 | * {@link Parcelable#writeToParcel(Parcel, int) Parcelable.writeToParcel()}. |
| 2657 | * |
| 2658 | * @see #writeTypedArray |
| 2659 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2660 | public final <T extends Parcelable> void writeParcelableArray(@Nullable T[] value, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2661 | int parcelableFlags) { |
| 2662 | if (value != null) { |
| 2663 | int N = value.length; |
| 2664 | writeInt(N); |
| 2665 | for (int i=0; i<N; i++) { |
| 2666 | writeParcelable(value[i], parcelableFlags); |
| 2667 | } |
| 2668 | } else { |
| 2669 | writeInt(-1); |
| 2670 | } |
| 2671 | } |
| 2672 | |
| 2673 | /** |
| 2674 | * Read a typed object from a parcel. The given class loader will be |
| 2675 | * used to load any enclosed Parcelables. If it is null, the default class |
| 2676 | * loader will be used. |
| 2677 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2678 | @Nullable |
| 2679 | public final Object readValue(@Nullable ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2680 | int type = readInt(); |
| 2681 | |
| 2682 | switch (type) { |
| 2683 | case VAL_NULL: |
| 2684 | return null; |
| 2685 | |
| 2686 | case VAL_STRING: |
| 2687 | return readString(); |
| 2688 | |
| 2689 | case VAL_INTEGER: |
| 2690 | return readInt(); |
| 2691 | |
| 2692 | case VAL_MAP: |
| 2693 | return readHashMap(loader); |
| 2694 | |
| 2695 | case VAL_PARCELABLE: |
| 2696 | return readParcelable(loader); |
| 2697 | |
| 2698 | case VAL_SHORT: |
| 2699 | return (short) readInt(); |
| 2700 | |
| 2701 | case VAL_LONG: |
| 2702 | return readLong(); |
| 2703 | |
| 2704 | case VAL_FLOAT: |
| 2705 | return readFloat(); |
| 2706 | |
| 2707 | case VAL_DOUBLE: |
| 2708 | return readDouble(); |
| 2709 | |
| 2710 | case VAL_BOOLEAN: |
| 2711 | return readInt() == 1; |
| 2712 | |
| 2713 | case VAL_CHARSEQUENCE: |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 2714 | return readCharSequence(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2715 | |
| 2716 | case VAL_LIST: |
| 2717 | return readArrayList(loader); |
| 2718 | |
| 2719 | case VAL_BOOLEANARRAY: |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 2720 | return createBooleanArray(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2721 | |
| 2722 | case VAL_BYTEARRAY: |
| 2723 | return createByteArray(); |
| 2724 | |
| 2725 | case VAL_STRINGARRAY: |
| 2726 | return readStringArray(); |
| 2727 | |
Bjorn Bringert | 08bbffb | 2010-02-25 11:16:22 +0000 | [diff] [blame] | 2728 | case VAL_CHARSEQUENCEARRAY: |
| 2729 | return readCharSequenceArray(); |
| 2730 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2731 | case VAL_IBINDER: |
| 2732 | return readStrongBinder(); |
| 2733 | |
| 2734 | case VAL_OBJECTARRAY: |
| 2735 | return readArray(loader); |
| 2736 | |
| 2737 | case VAL_INTARRAY: |
| 2738 | return createIntArray(); |
| 2739 | |
| 2740 | case VAL_LONGARRAY: |
| 2741 | return createLongArray(); |
| 2742 | |
| 2743 | case VAL_BYTE: |
| 2744 | return readByte(); |
| 2745 | |
| 2746 | case VAL_SERIALIZABLE: |
John Spurlock | 5002b8c | 2014-01-10 13:32:12 -0500 | [diff] [blame] | 2747 | return readSerializable(loader); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2748 | |
| 2749 | case VAL_PARCELABLEARRAY: |
| 2750 | return readParcelableArray(loader); |
| 2751 | |
| 2752 | case VAL_SPARSEARRAY: |
| 2753 | return readSparseArray(loader); |
| 2754 | |
| 2755 | case VAL_SPARSEBOOLEANARRAY: |
| 2756 | return readSparseBooleanArray(); |
| 2757 | |
| 2758 | case VAL_BUNDLE: |
| 2759 | return readBundle(loader); // loading will be deferred |
| 2760 | |
Craig Mautner | 719e6b1 | 2014-04-04 20:29:41 -0700 | [diff] [blame] | 2761 | case VAL_PERSISTABLEBUNDLE: |
| 2762 | return readPersistableBundle(loader); |
| 2763 | |
Jeff Sharkey | 5ef3398 | 2014-09-04 18:13:39 -0700 | [diff] [blame] | 2764 | case VAL_SIZE: |
| 2765 | return readSize(); |
| 2766 | |
| 2767 | case VAL_SIZEF: |
| 2768 | return readSizeF(); |
| 2769 | |
Samuel Tan | a803666 | 2015-11-23 14:36:00 -0800 | [diff] [blame] | 2770 | case VAL_DOUBLEARRAY: |
| 2771 | return createDoubleArray(); |
| 2772 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2773 | default: |
| 2774 | int off = dataPosition() - 4; |
| 2775 | throw new RuntimeException( |
| 2776 | "Parcel " + this + ": Unmarshalling unknown type code " + type + " at offset " + off); |
| 2777 | } |
| 2778 | } |
| 2779 | |
| 2780 | /** |
| 2781 | * Read and return a new Parcelable from the parcel. The given class loader |
| 2782 | * will be used to load any enclosed Parcelables. If it is null, the default |
| 2783 | * class loader will be used. |
| 2784 | * @param loader A ClassLoader from which to instantiate the Parcelable |
| 2785 | * object, or null for the default class loader. |
| 2786 | * @return Returns the newly created Parcelable, or null if a null |
| 2787 | * object has been written. |
| 2788 | * @throws BadParcelableException Throws BadParcelableException if there |
| 2789 | * was an error trying to instantiate the Parcelable. |
| 2790 | */ |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 2791 | @SuppressWarnings("unchecked") |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2792 | @Nullable |
| 2793 | public final <T extends Parcelable> T readParcelable(@Nullable ClassLoader loader) { |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 2794 | Parcelable.Creator<?> creator = readParcelableCreator(loader); |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 2795 | if (creator == null) { |
| 2796 | return null; |
| 2797 | } |
| 2798 | if (creator instanceof Parcelable.ClassLoaderCreator<?>) { |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 2799 | Parcelable.ClassLoaderCreator<?> classLoaderCreator = |
| 2800 | (Parcelable.ClassLoaderCreator<?>) creator; |
| 2801 | return (T) classLoaderCreator.createFromParcel(this, loader); |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 2802 | } |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 2803 | return (T) creator.createFromParcel(this); |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 2804 | } |
| 2805 | |
| 2806 | /** @hide */ |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 2807 | @SuppressWarnings("unchecked") |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2808 | @Nullable |
| 2809 | public final <T extends Parcelable> T readCreator(@NonNull Parcelable.Creator<?> creator, |
| 2810 | @Nullable ClassLoader loader) { |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 2811 | if (creator instanceof Parcelable.ClassLoaderCreator<?>) { |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 2812 | Parcelable.ClassLoaderCreator<?> classLoaderCreator = |
| 2813 | (Parcelable.ClassLoaderCreator<?>) creator; |
| 2814 | return (T) classLoaderCreator.createFromParcel(this, loader); |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 2815 | } |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 2816 | return (T) creator.createFromParcel(this); |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 2817 | } |
| 2818 | |
| 2819 | /** @hide */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2820 | @Nullable |
| 2821 | public final Parcelable.Creator<?> readParcelableCreator(@Nullable ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2822 | String name = readString(); |
| 2823 | if (name == null) { |
| 2824 | return null; |
| 2825 | } |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 2826 | Parcelable.Creator<?> creator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2827 | synchronized (mCreators) { |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 2828 | HashMap<String,Parcelable.Creator<?>> map = mCreators.get(loader); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2829 | if (map == null) { |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 2830 | map = new HashMap<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2831 | mCreators.put(loader, map); |
| 2832 | } |
| 2833 | creator = map.get(name); |
| 2834 | if (creator == null) { |
| 2835 | try { |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 2836 | // If loader == null, explicitly emulate Class.forName(String) "caller |
| 2837 | // classloader" behavior. |
| 2838 | ClassLoader parcelableClassLoader = |
| 2839 | (loader == null ? getClass().getClassLoader() : loader); |
| 2840 | // Avoid initializing the Parcelable class until we know it implements |
| 2841 | // Parcelable and has the necessary CREATOR field. http://b/1171613. |
| 2842 | Class<?> parcelableClass = Class.forName(name, false /* initialize */, |
| 2843 | parcelableClassLoader); |
| 2844 | if (!Parcelable.class.isAssignableFrom(parcelableClass)) { |
Dianne Hackborn | ced5439 | 2018-02-26 13:07:42 -0800 | [diff] [blame] | 2845 | throw new BadParcelableException("Parcelable protocol requires subclassing " |
| 2846 | + "from Parcelable on class " + name); |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 2847 | } |
| 2848 | Field f = parcelableClass.getField("CREATOR"); |
| 2849 | if ((f.getModifiers() & Modifier.STATIC) == 0) { |
| 2850 | throw new BadParcelableException("Parcelable protocol requires " |
| 2851 | + "the CREATOR object to be static on class " + name); |
| 2852 | } |
| 2853 | Class<?> creatorType = f.getType(); |
| 2854 | if (!Parcelable.Creator.class.isAssignableFrom(creatorType)) { |
| 2855 | // Fail before calling Field.get(), not after, to avoid initializing |
| 2856 | // parcelableClass unnecessarily. |
| 2857 | throw new BadParcelableException("Parcelable protocol requires a " |
| 2858 | + "Parcelable.Creator object called " |
| 2859 | + "CREATOR on class " + name); |
| 2860 | } |
| 2861 | creator = (Parcelable.Creator<?>) f.get(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2862 | } |
| 2863 | catch (IllegalAccessException e) { |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 2864 | Log.e(TAG, "Illegal access when unmarshalling: " + name, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2865 | throw new BadParcelableException( |
| 2866 | "IllegalAccessException when unmarshalling: " + name); |
| 2867 | } |
| 2868 | catch (ClassNotFoundException e) { |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 2869 | Log.e(TAG, "Class not found when unmarshalling: " + name, e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2870 | throw new BadParcelableException( |
| 2871 | "ClassNotFoundException when unmarshalling: " + name); |
| 2872 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2873 | catch (NoSuchFieldException e) { |
| 2874 | throw new BadParcelableException("Parcelable protocol requires a " |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 2875 | + "Parcelable.Creator object called " |
| 2876 | + "CREATOR on class " + name); |
Irfan Sheriff | 97a72f6 | 2013-01-11 10:45:51 -0800 | [diff] [blame] | 2877 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2878 | if (creator == null) { |
| 2879 | throw new BadParcelableException("Parcelable protocol requires a " |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 2880 | + "non-null Parcelable.Creator object called " |
| 2881 | + "CREATOR on class " + name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2882 | } |
| 2883 | |
| 2884 | map.put(name, creator); |
| 2885 | } |
| 2886 | } |
| 2887 | |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 2888 | return creator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2889 | } |
| 2890 | |
| 2891 | /** |
| 2892 | * Read and return a new Parcelable array from the parcel. |
| 2893 | * The given class loader will be used to load any enclosed |
| 2894 | * Parcelables. |
| 2895 | * @return the Parcelable array, or null if the array is null |
| 2896 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2897 | @Nullable |
| 2898 | public final Parcelable[] readParcelableArray(@Nullable ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2899 | int N = readInt(); |
| 2900 | if (N < 0) { |
| 2901 | return null; |
| 2902 | } |
| 2903 | Parcelable[] p = new Parcelable[N]; |
| 2904 | for (int i = 0; i < N; i++) { |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 2905 | p[i] = readParcelable(loader); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2906 | } |
| 2907 | return p; |
| 2908 | } |
| 2909 | |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 2910 | /** @hide */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2911 | @Nullable |
| 2912 | public final <T extends Parcelable> T[] readParcelableArray(@Nullable ClassLoader loader, |
| 2913 | @NonNull Class<T> clazz) { |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 2914 | int N = readInt(); |
| 2915 | if (N < 0) { |
| 2916 | return null; |
| 2917 | } |
| 2918 | T[] p = (T[]) Array.newInstance(clazz, N); |
| 2919 | for (int i = 0; i < N; i++) { |
| 2920 | p[i] = readParcelable(loader); |
| 2921 | } |
| 2922 | return p; |
| 2923 | } |
| 2924 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2925 | /** |
| 2926 | * Read and return a new Serializable object from the parcel. |
| 2927 | * @return the Serializable object, or null if the Serializable name |
| 2928 | * wasn't found in the parcel. |
| 2929 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2930 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2931 | public final Serializable readSerializable() { |
John Spurlock | 5002b8c | 2014-01-10 13:32:12 -0500 | [diff] [blame] | 2932 | return readSerializable(null); |
| 2933 | } |
| 2934 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 2935 | @Nullable |
| 2936 | private final Serializable readSerializable(@Nullable final ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2937 | String name = readString(); |
| 2938 | if (name == null) { |
| 2939 | // For some reason we were unable to read the name of the Serializable (either there |
| 2940 | // is nothing left in the Parcel to read, or the next value wasn't a String), so |
| 2941 | // return null, which indicates that the name wasn't found in the parcel. |
| 2942 | return null; |
| 2943 | } |
| 2944 | |
| 2945 | byte[] serializedData = createByteArray(); |
| 2946 | ByteArrayInputStream bais = new ByteArrayInputStream(serializedData); |
| 2947 | try { |
John Spurlock | 5002b8c | 2014-01-10 13:32:12 -0500 | [diff] [blame] | 2948 | ObjectInputStream ois = new ObjectInputStream(bais) { |
| 2949 | @Override |
| 2950 | protected Class<?> resolveClass(ObjectStreamClass osClass) |
| 2951 | throws IOException, ClassNotFoundException { |
| 2952 | // try the custom classloader if provided |
| 2953 | if (loader != null) { |
| 2954 | Class<?> c = Class.forName(osClass.getName(), false, loader); |
| 2955 | if (c != null) { |
| 2956 | return c; |
| 2957 | } |
| 2958 | } |
| 2959 | return super.resolveClass(osClass); |
| 2960 | } |
| 2961 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2962 | return (Serializable) ois.readObject(); |
| 2963 | } catch (IOException ioe) { |
| 2964 | throw new RuntimeException("Parcelable encountered " + |
| 2965 | "IOException reading a Serializable object (name = " + name + |
| 2966 | ")", ioe); |
| 2967 | } catch (ClassNotFoundException cnfe) { |
John Spurlock | 5002b8c | 2014-01-10 13:32:12 -0500 | [diff] [blame] | 2968 | throw new RuntimeException("Parcelable encountered " + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2969 | "ClassNotFoundException reading a Serializable object (name = " |
| 2970 | + name + ")", cnfe); |
| 2971 | } |
| 2972 | } |
| 2973 | |
| 2974 | // Cache of previously looked up CREATOR.createFromParcel() methods for |
| 2975 | // particular classes. Keys are the names of the classes, values are |
| 2976 | // Method objects. |
Neil Fuller | 44e440c | 2015-04-20 14:39:00 +0100 | [diff] [blame] | 2977 | private static final HashMap<ClassLoader,HashMap<String,Parcelable.Creator<?>>> |
| 2978 | mCreators = new HashMap<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2979 | |
Narayan Kamath | b34a461 | 2014-01-23 14:17:11 +0000 | [diff] [blame] | 2980 | /** @hide for internal use only. */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2981 | static protected final Parcel obtain(int obj) { |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 2982 | throw new UnsupportedOperationException(); |
| 2983 | } |
| 2984 | |
| 2985 | /** @hide */ |
| 2986 | static protected final Parcel obtain(long obj) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2987 | final Parcel[] pool = sHolderPool; |
| 2988 | synchronized (pool) { |
| 2989 | Parcel p; |
| 2990 | for (int i=0; i<POOL_SIZE; i++) { |
| 2991 | p = pool[i]; |
| 2992 | if (p != null) { |
| 2993 | pool[i] = null; |
| 2994 | if (DEBUG_RECYCLE) { |
| 2995 | p.mStack = new RuntimeException(); |
| 2996 | } |
| 2997 | p.init(obj); |
| 2998 | return p; |
| 2999 | } |
| 3000 | } |
| 3001 | } |
| 3002 | return new Parcel(obj); |
| 3003 | } |
| 3004 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 3005 | private Parcel(long nativePtr) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3006 | if (DEBUG_RECYCLE) { |
| 3007 | mStack = new RuntimeException(); |
| 3008 | } |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 3009 | //Log.i(TAG, "Initializing obj=0x" + Integer.toHexString(obj), mStack); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 3010 | init(nativePtr); |
| 3011 | } |
| 3012 | |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 3013 | private void init(long nativePtr) { |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 3014 | if (nativePtr != 0) { |
| 3015 | mNativePtr = nativePtr; |
| 3016 | mOwnsNativeParcelObject = false; |
| 3017 | } else { |
| 3018 | mNativePtr = nativeCreate(); |
| 3019 | mOwnsNativeParcelObject = true; |
| 3020 | } |
| 3021 | } |
| 3022 | |
| 3023 | private void freeBuffer() { |
| 3024 | if (mOwnsNativeParcelObject) { |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 3025 | updateNativeSize(nativeFreeBuffer(mNativePtr)); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 3026 | } |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 3027 | mReadWriteHelper = ReadWriteHelper.DEFAULT; |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 3028 | } |
| 3029 | |
| 3030 | private void destroy() { |
| 3031 | if (mNativePtr != 0) { |
| 3032 | if (mOwnsNativeParcelObject) { |
| 3033 | nativeDestroy(mNativePtr); |
Adrian Roos | 0450565 | 2015-10-22 16:12:01 -0700 | [diff] [blame] | 3034 | updateNativeSize(0); |
Jeff Sharkey | 047238c | 2012-03-07 16:51:38 -0800 | [diff] [blame] | 3035 | } |
| 3036 | mNativePtr = 0; |
| 3037 | } |
Makoto Onuki | 4501c61d | 2017-07-27 15:56:40 -0700 | [diff] [blame] | 3038 | mReadWriteHelper = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3039 | } |
| 3040 | |
| 3041 | @Override |
| 3042 | protected void finalize() throws Throwable { |
| 3043 | if (DEBUG_RECYCLE) { |
| 3044 | if (mStack != null) { |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 3045 | Log.w(TAG, "Client did not call Parcel.recycle()", mStack); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3046 | } |
| 3047 | } |
| 3048 | destroy(); |
| 3049 | } |
| 3050 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 3051 | /* package */ void readMapInternal(@NonNull Map outVal, int N, |
| 3052 | @Nullable ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3053 | while (N > 0) { |
| 3054 | Object key = readValue(loader); |
| 3055 | Object value = readValue(loader); |
| 3056 | outVal.put(key, value); |
| 3057 | N--; |
| 3058 | } |
| 3059 | } |
| 3060 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 3061 | /* package */ void readArrayMapInternal(@NonNull ArrayMap outVal, int N, |
| 3062 | @Nullable ClassLoader loader) { |
Dianne Hackborn | e784d1e | 2013-09-20 18:13:52 -0700 | [diff] [blame] | 3063 | if (DEBUG_ARRAY_MAP) { |
| 3064 | RuntimeException here = new RuntimeException("here"); |
| 3065 | here.fillInStackTrace(); |
| 3066 | Log.d(TAG, "Reading " + N + " ArrayMap entries", here); |
| 3067 | } |
Dianne Hackborn | 8aee64d | 2013-10-25 10:41:50 -0700 | [diff] [blame] | 3068 | int startPos; |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 3069 | while (N > 0) { |
Dianne Hackborn | 8aee64d | 2013-10-25 10:41:50 -0700 | [diff] [blame] | 3070 | if (DEBUG_ARRAY_MAP) startPos = dataPosition(); |
Dianne Hackborn | 9c3e74f | 2014-08-13 15:39:50 -0700 | [diff] [blame] | 3071 | String key = readString(); |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 3072 | Object value = readValue(loader); |
Dianne Hackborn | 8aee64d | 2013-10-25 10:41:50 -0700 | [diff] [blame] | 3073 | if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read #" + (N-1) + " " |
| 3074 | + (dataPosition()-startPos) + " bytes: key=0x" |
| 3075 | + Integer.toHexString((key != null ? key.hashCode() : 0)) + " " + key); |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 3076 | outVal.append(key, value); |
| 3077 | N--; |
| 3078 | } |
Dianne Hackborn | 9c3e74f | 2014-08-13 15:39:50 -0700 | [diff] [blame] | 3079 | outVal.validate(); |
Dianne Hackborn | b87655b | 2013-07-17 19:06:22 -0700 | [diff] [blame] | 3080 | } |
| 3081 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 3082 | /* package */ void readArrayMapSafelyInternal(@NonNull ArrayMap outVal, int N, |
| 3083 | @Nullable ClassLoader loader) { |
Dianne Hackborn | e784d1e | 2013-09-20 18:13:52 -0700 | [diff] [blame] | 3084 | if (DEBUG_ARRAY_MAP) { |
| 3085 | RuntimeException here = new RuntimeException("here"); |
| 3086 | here.fillInStackTrace(); |
| 3087 | Log.d(TAG, "Reading safely " + N + " ArrayMap entries", here); |
| 3088 | } |
| 3089 | while (N > 0) { |
Dianne Hackborn | 9c3e74f | 2014-08-13 15:39:50 -0700 | [diff] [blame] | 3090 | String key = readString(); |
Dianne Hackborn | e784d1e | 2013-09-20 18:13:52 -0700 | [diff] [blame] | 3091 | if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read safe #" + (N-1) + ": key=0x" |
| 3092 | + (key != null ? key.hashCode() : 0) + " " + key); |
| 3093 | Object value = readValue(loader); |
| 3094 | outVal.put(key, value); |
| 3095 | N--; |
| 3096 | } |
| 3097 | } |
| 3098 | |
Dianne Hackborn | 9c3e74f | 2014-08-13 15:39:50 -0700 | [diff] [blame] | 3099 | /** |
| 3100 | * @hide For testing only. |
| 3101 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 3102 | public void readArrayMap(@NonNull ArrayMap outVal, @Nullable ClassLoader loader) { |
Dianne Hackborn | 9c3e74f | 2014-08-13 15:39:50 -0700 | [diff] [blame] | 3103 | final int N = readInt(); |
| 3104 | if (N < 0) { |
| 3105 | return; |
| 3106 | } |
| 3107 | readArrayMapInternal(outVal, N, loader); |
| 3108 | } |
| 3109 | |
Svet Ganov | ddb9488 | 2016-06-23 19:55:24 -0700 | [diff] [blame] | 3110 | /** |
| 3111 | * Reads an array set. |
| 3112 | * |
| 3113 | * @param loader The class loader to use. |
| 3114 | * |
| 3115 | * @hide |
| 3116 | */ |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 3117 | public @Nullable ArraySet<? extends Object> readArraySet(@Nullable ClassLoader loader) { |
Svet Ganov | ddb9488 | 2016-06-23 19:55:24 -0700 | [diff] [blame] | 3118 | final int size = readInt(); |
| 3119 | if (size < 0) { |
| 3120 | return null; |
| 3121 | } |
| 3122 | ArraySet<Object> result = new ArraySet<>(size); |
| 3123 | for (int i = 0; i < size; i++) { |
| 3124 | Object value = readValue(loader); |
| 3125 | result.append(value); |
| 3126 | } |
| 3127 | return result; |
| 3128 | } |
| 3129 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 3130 | private void readListInternal(@NonNull List outVal, int N, |
| 3131 | @Nullable ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3132 | while (N > 0) { |
| 3133 | Object value = readValue(loader); |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 3134 | //Log.d(TAG, "Unmarshalling value=" + value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3135 | outVal.add(value); |
| 3136 | N--; |
| 3137 | } |
| 3138 | } |
| 3139 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 3140 | private void readArrayInternal(@NonNull Object[] outVal, int N, |
| 3141 | @Nullable ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3142 | for (int i = 0; i < N; i++) { |
| 3143 | Object value = readValue(loader); |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 3144 | //Log.d(TAG, "Unmarshalling value=" + value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3145 | outVal[i] = value; |
| 3146 | } |
| 3147 | } |
| 3148 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 3149 | private void readSparseArrayInternal(@NonNull SparseArray outVal, int N, |
| 3150 | @Nullable ClassLoader loader) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3151 | while (N > 0) { |
| 3152 | int key = readInt(); |
| 3153 | Object value = readValue(loader); |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 3154 | //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3155 | outVal.append(key, value); |
| 3156 | N--; |
| 3157 | } |
| 3158 | } |
| 3159 | |
| 3160 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 3161 | private void readSparseBooleanArrayInternal(@NonNull SparseBooleanArray outVal, int N) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3162 | while (N > 0) { |
| 3163 | int key = readInt(); |
| 3164 | boolean value = this.readByte() == 1; |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 3165 | //Log.i(TAG, "Unmarshalling key=" + key + " value=" + value); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3166 | outVal.append(key, value); |
| 3167 | N--; |
| 3168 | } |
| 3169 | } |
Dan Sandler | 5ce0430 | 2015-04-09 23:50:15 -0400 | [diff] [blame] | 3170 | |
Jake Wharton | b1f474c | 2018-05-30 15:11:13 -0400 | [diff] [blame] | 3171 | private void readSparseIntArrayInternal(@NonNull SparseIntArray outVal, int N) { |
Adam Lesinski | 4e86281 | 2016-11-21 16:02:24 -0800 | [diff] [blame] | 3172 | while (N > 0) { |
| 3173 | int key = readInt(); |
| 3174 | int value = readInt(); |
| 3175 | outVal.append(key, value); |
| 3176 | N--; |
| 3177 | } |
| 3178 | } |
| 3179 | |
Dan Sandler | 5ce0430 | 2015-04-09 23:50:15 -0400 | [diff] [blame] | 3180 | /** |
| 3181 | * @hide For testing |
| 3182 | */ |
| 3183 | public long getBlobAshmemSize() { |
| 3184 | return nativeGetBlobAshmemSize(mNativePtr); |
| 3185 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3186 | } |