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