blob: 38d980ecb0f18aed4b9c884cd514deae0f9415ff [file] [log] [blame]
Dianne Hackborn3aa49b62013-04-26 16:39:17 -07001/*
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
17package android.os;
18
Artur Satayevafdb23a2019-12-10 17:47:53 +000019import android.compat.annotation.UnsupportedAppUsage;
Adam Lesinski1619ed42015-09-22 13:02:09 -070020import android.util.MathUtils;
21
Dianne Hackborn3aa49b62013-04-26 16:39:17 -070022/**
23 * Parcelable containing a raw Parcel of data.
24 * @hide
25 */
26public class ParcelableParcel implements Parcelable {
27 final Parcel mParcel;
28 final ClassLoader mClassLoader;
29
Andrei Onea24ec3212019-03-15 17:35:05 +000030 @UnsupportedAppUsage
Dianne Hackborn3aa49b62013-04-26 16:39:17 -070031 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 Lesinski1619ed42015-09-22 13:02:09 -070040 if (size < 0) {
41 throw new IllegalArgumentException("Negative size read from parcel");
42 }
43
Dianne Hackborn3aa49b62013-04-26 16:39:17 -070044 int pos = src.dataPosition();
Adam Lesinski1619ed42015-09-22 13:02:09 -070045 src.setDataPosition(MathUtils.addOrThrow(pos, size));
46 mParcel.appendFrom(src, pos, size);
Dianne Hackborn3aa49b62013-04-26 16:39:17 -070047 }
48
Andrei Onea24ec3212019-03-15 17:35:05 +000049 @UnsupportedAppUsage
Dianne Hackborn3aa49b62013-04-26 16:39:17 -070050 public Parcel getParcel() {
51 mParcel.setDataPosition(0);
52 return mParcel;
53 }
54
Andrei Onea24ec3212019-03-15 17:35:05 +000055 @UnsupportedAppUsage
Dianne Hackborn3aa49b62013-04-26 16:39:17 -070056 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 Onea24ec3212019-03-15 17:35:05 +000071 @UnsupportedAppUsage
Dianne Hackborn3aa49b62013-04-26 16:39:17 -070072 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}