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