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