blob: 009898ee303e2430d4e929cdf3b76da60a024d7a [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.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
Chris Banes02e81a02016-04-22 13:15:36 +010040 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041 * @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.
Chris Banes02e81a02016-04-22 13:15:36 +010052 *
53 * @param source parcel to read from
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 */
55 protected AbsSavedState(Parcel source) {
Chris Banes02e81a02016-04-22 13:15:36 +010056 this(source, null);
57 }
58
59 /**
60 * Constructor used when reading from a parcel using a given class loader.
61 * Reads the state of the superclass.
62 *
63 * @param source parcel to read from
64 * @param loader ClassLoader to use for reading
65 */
66 protected AbsSavedState(Parcel source, ClassLoader loader) {
67 Parcelable superState = source.readParcelable(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 mSuperState = superState != null ? superState : EMPTY_STATE;
69 }
70
71 final public Parcelable getSuperState() {
72 return mSuperState;
73 }
74
75 public int describeContents() {
76 return 0;
77 }
78
79 public void writeToParcel(Parcel dest, int flags) {
Chris Banes02e81a02016-04-22 13:15:36 +010080 dest.writeParcelable(mSuperState, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 }
82
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -070083 public static final @android.annotation.NonNull Parcelable.Creator<AbsSavedState> CREATOR
Chris Banes02e81a02016-04-22 13:15:36 +010084 = new Parcelable.ClassLoaderCreator<AbsSavedState>() {
85
86 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 public AbsSavedState createFromParcel(Parcel in) {
Chris Banes02e81a02016-04-22 13:15:36 +010088 return createFromParcel(in, null);
89 }
90
91 @Override
92 public AbsSavedState createFromParcel(Parcel in, ClassLoader loader) {
93 Parcelable superState = in.readParcelable(loader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 if (superState != null) {
95 throw new IllegalStateException("superState must be null");
96 }
97 return EMPTY_STATE;
98 }
99
Chris Banes02e81a02016-04-22 13:15:36 +0100100 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 public AbsSavedState[] newArray(int size) {
102 return new AbsSavedState[size];
103 }
104 };
105}