1/*
2 * Copyright (C) 2015 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.databinding;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22import java.io.Serializable;
23
24/**
25 * An observable class that holds a parcelable object.
26 * <p>
27 * Observable field classes may be used instead of creating an Observable object:
28 * <pre><code>public class MyDataObject {
29 *     public final ObservableParcelable&lt;String> name = new ObservableParcelable&lt;String>();
30 *     public final ObservableInt age = new ObservableInt();
31 * }</code></pre>
32 * Fields of this type should be declared final because bindings only detect changes in the
33 * field's value, not of the field itself.
34 * <p>
35 * This class is parcelable but you should keep in mind that listeners are ignored when the object
36 * is parcelled. Unless you add custom observers, this should not be an issue because data binding
37 * framework always re-registers observers when the view is bound.
38 */
39public class ObservableParcelable<T extends Parcelable> extends ObservableField<T>
40        implements Parcelable, Serializable {
41    static final long serialVersionUID = 1L;
42    /**
43     * Wraps the given object and creates an observable object
44     *
45     * @param value The value to be wrapped as an observable.
46     */
47    public ObservableParcelable(T value) {
48        super(value);
49    }
50
51    /**
52     * Creates an empty observable object
53     */
54    public ObservableParcelable() {
55        super();
56    }
57
58    @Override
59    public int describeContents() {
60        return 0;
61    }
62
63    @Override
64    public void writeToParcel(Parcel dest, int flags) {
65        dest.writeParcelable(get(), 0);
66    }
67
68    public static final Parcelable.Creator<ObservableParcelable> CREATOR
69            = new Parcelable.Creator<ObservableParcelable>() {
70
71        @Override
72        public ObservableParcelable createFromParcel(Parcel source) {
73            //noinspection unchecked
74            return new ObservableParcelable(source.readParcelable(getClass().getClassLoader()));
75        }
76
77        @Override
78        public ObservableParcelable[] newArray(int size) {
79            return new ObservableParcelable[size];
80        }
81    };
82}
83