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 19 import android.annotation.UnsupportedAppUsage; 20 import android.util.MathUtils; 21 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 30 @UnsupportedAppUsage 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(); 40 if (size < 0) { 41 throw new IllegalArgumentException("Negative size read from parcel"); 42 } 43 44 int pos = src.dataPosition(); 45 src.setDataPosition(MathUtils.addOrThrow(pos, size)); 46 mParcel.appendFrom(src, pos, size); 47 } 48 49 @UnsupportedAppUsage 50 public Parcel getParcel() { 51 mParcel.setDataPosition(0); 52 return mParcel; 53 } 54 55 @UnsupportedAppUsage 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 71 @UnsupportedAppUsage 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 } 87