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