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.view; 18 19 import android.os.Parcel; 20 import android.os.Parcelable; 21 22 /** 23 * A {@link Parcelable} implementation that should be used by inheritance 24 * hierarchies to ensure the state of all classes along the chain is saved. 25 */ 26 public abstract class AbsSavedState implements Parcelable { 27 public static final AbsSavedState EMPTY_STATE = new AbsSavedState() {}; 28 29 private final Parcelable mSuperState; 30 31 /** 32 * Constructor used to make the EMPTY_STATE singleton 33 */ AbsSavedState()34 private AbsSavedState() { 35 mSuperState = null; 36 } 37 38 /** 39 * Constructor called by derived classes when creating their SavedState objects 40 * 41 * @param superState The state of the superclass of this view 42 */ AbsSavedState(Parcelable superState)43 protected AbsSavedState(Parcelable superState) { 44 if (superState == null) { 45 throw new IllegalArgumentException("superState must not be null"); 46 } 47 mSuperState = superState != EMPTY_STATE ? superState : null; 48 } 49 50 /** 51 * Constructor used when reading from a parcel. Reads the state of the superclass. 52 * 53 * @param source 54 */ AbsSavedState(Parcel source)55 protected AbsSavedState(Parcel source) { 56 // FIXME need class loader 57 Parcelable superState = source.readParcelable(null); 58 59 mSuperState = superState != null ? superState : EMPTY_STATE; 60 } 61 getSuperState()62 final public Parcelable getSuperState() { 63 return mSuperState; 64 } 65 describeContents()66 public int describeContents() { 67 return 0; 68 } 69 writeToParcel(Parcel dest, int flags)70 public void writeToParcel(Parcel dest, int flags) { 71 dest.writeParcelable(mSuperState, flags); 72 } 73 74 public static final Parcelable.Creator<AbsSavedState> CREATOR 75 = new Parcelable.Creator<AbsSavedState>() { 76 77 public AbsSavedState createFromParcel(Parcel in) { 78 Parcelable superState = in.readParcelable(null); 79 if (superState != null) { 80 throw new IllegalStateException("superState must be null"); 81 } 82 return EMPTY_STATE; 83 } 84 85 public AbsSavedState[] newArray(int size) { 86 return new AbsSavedState[size]; 87 } 88 }; 89 } 90