Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | |
Artur Satayev | afdb23a | 2019-12-10 17:47:53 +0000 | [diff] [blame] | 19 | import android.compat.annotation.UnsupportedAppUsage; |
Adam Lesinski | 1619ed4 | 2015-09-22 13:02:09 -0700 | [diff] [blame] | 20 | import android.util.MathUtils; |
| 21 | |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 22 | /** |
| 23 | * Parcelable containing a raw Parcel of data. |
| 24 | * @hide |
| 25 | */ |
| 26 | public class ParcelableParcel implements Parcelable { |
| 27 | final Parcel mParcel; |
| 28 | final ClassLoader mClassLoader; |
| 29 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 30 | @UnsupportedAppUsage |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 31 | public ParcelableParcel(ClassLoader loader) { |
| 32 | mParcel = Parcel.obtain(); |
| 33 | mClassLoader = loader; |
| 34 | } |
| 35 | |
| 36 | public ParcelableParcel(Parcel src, ClassLoader loader) { |
| 37 | mParcel = Parcel.obtain(); |
| 38 | mClassLoader = loader; |
| 39 | int size = src.readInt(); |
Adam Lesinski | 1619ed4 | 2015-09-22 13:02:09 -0700 | [diff] [blame] | 40 | if (size < 0) { |
| 41 | throw new IllegalArgumentException("Negative size read from parcel"); |
| 42 | } |
| 43 | |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 44 | int pos = src.dataPosition(); |
Adam Lesinski | 1619ed4 | 2015-09-22 13:02:09 -0700 | [diff] [blame] | 45 | src.setDataPosition(MathUtils.addOrThrow(pos, size)); |
| 46 | mParcel.appendFrom(src, pos, size); |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 49 | @UnsupportedAppUsage |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 50 | public Parcel getParcel() { |
| 51 | mParcel.setDataPosition(0); |
| 52 | return mParcel; |
| 53 | } |
| 54 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 55 | @UnsupportedAppUsage |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 56 | public ClassLoader getClassLoader() { |
| 57 | return mClassLoader; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public int describeContents() { |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public void writeToParcel(Parcel dest, int flags) { |
| 67 | dest.writeInt(mParcel.dataSize()); |
| 68 | dest.appendFrom(mParcel, 0, mParcel.dataSize()); |
| 69 | } |
| 70 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 71 | @UnsupportedAppUsage |
Dianne Hackborn | 3aa49b6 | 2013-04-26 16:39:17 -0700 | [diff] [blame] | 72 | public static final Parcelable.ClassLoaderCreator<ParcelableParcel> CREATOR |
| 73 | = new Parcelable.ClassLoaderCreator<ParcelableParcel>() { |
| 74 | public ParcelableParcel createFromParcel(Parcel in) { |
| 75 | return new ParcelableParcel(in, null); |
| 76 | } |
| 77 | |
| 78 | public ParcelableParcel createFromParcel(Parcel in, ClassLoader loader) { |
| 79 | return new ParcelableParcel(in, loader); |
| 80 | } |
| 81 | |
| 82 | public ParcelableParcel[] newArray(int size) { |
| 83 | return new ParcelableParcel[size]; |
| 84 | } |
| 85 | }; |
| 86 | } |