AbsSavedState.java revision 4b25d63ff53d4676150ac54897851e4ff9f1049f
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;
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 */
26public 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     */
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     */
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 parcel to read from
54     */
55    protected AbsSavedState(Parcel source) {
56        this(source, null);
57    }
58
59    /**
60     * Constructor used when reading from a parcel. Reads the state of the superclass.
61     *
62     * @param source parcel to read from
63     * @param loader ClassLoader to use for reading
64     */
65    protected AbsSavedState(Parcel source, ClassLoader loader) {
66        Parcelable superState = source.readParcelable(loader);
67        mSuperState = superState != null ? superState : EMPTY_STATE;
68    }
69
70    public final Parcelable getSuperState() {
71        return mSuperState;
72    }
73
74    @Override
75    public int describeContents() {
76        return 0;
77    }
78
79    @Override
80    public void writeToParcel(Parcel dest, int flags) {
81        dest.writeParcelable(mSuperState, flags);
82    }
83
84    public static final Creator<AbsSavedState> CREATOR = new ClassLoaderCreator<AbsSavedState>() {
85        @Override
86        public AbsSavedState createFromParcel(Parcel in, ClassLoader loader) {
87            Parcelable superState = in.readParcelable(loader);
88            if (superState != null) {
89                throw new IllegalStateException("superState must be null");
90            }
91            return EMPTY_STATE;
92        }
93
94        @Override
95        public AbsSavedState createFromParcel(Parcel in) {
96            return createFromParcel(in, null);
97        }
98
99        @Override
100        public AbsSavedState[] newArray(int size) {
101            return new AbsSavedState[size];
102        }
103    };
104}
105