1/*
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.os;
18
19/**
20 * Interface for classes whose instances can be written to
21 * and restored from a {@link Parcel}.  Classes implementing the Parcelable
22 * interface must also have a non-null static field called <code>CREATOR</code>
23 * of a type that implements the {@link Parcelable.Creator} interface.
24 *
25 * <p>A typical implementation of Parcelable is:</p>
26 *
27 * <pre>
28 * public class MyParcelable implements Parcelable {
29 *     private int mData;
30 *
31 *     public int describeContents() {
32 *         return 0;
33 *     }
34 *
35 *     public void writeToParcel(Parcel out, int flags) {
36 *         out.writeInt(mData);
37 *     }
38 *
39 *     public static final Parcelable.Creator&lt;MyParcelable&gt; CREATOR
40 *             = new Parcelable.Creator&lt;MyParcelable&gt;() {
41 *         public MyParcelable createFromParcel(Parcel in) {
42 *             return new MyParcelable(in);
43 *         }
44 *
45 *         public MyParcelable[] newArray(int size) {
46 *             return new MyParcelable[size];
47 *         }
48 *     };
49 *
50 *     private MyParcelable(Parcel in) {
51 *         mData = in.readInt();
52 *     }
53 * }</pre>
54 */
55public interface Parcelable {
56    /**
57     * Flag for use with {@link #writeToParcel}: the object being written
58     * is a return value, that is the result of a function such as
59     * "<code>Parcelable someFunction()</code>",
60     * "<code>void someFunction(out Parcelable)</code>", or
61     * "<code>void someFunction(inout Parcelable)</code>".  Some implementations
62     * may want to release resources at this point.
63     */
64    public static final int PARCELABLE_WRITE_RETURN_VALUE = 0x0001;
65
66    /**
67     * Bit masks for use with {@link #describeContents}: each bit represents a
68     * kind of object considered to have potential special significance when
69     * marshalled.
70     */
71    public static final int CONTENTS_FILE_DESCRIPTOR = 0x0001;
72
73    /**
74     * Describe the kinds of special objects contained in this Parcelable's
75     * marshalled representation.
76     *
77     * @return a bitmask indicating the set of special object types marshalled
78     * by the Parcelable.
79     */
80    public int describeContents();
81
82    /**
83     * Flatten this object in to a Parcel.
84     *
85     * @param dest The Parcel in which the object should be written.
86     * @param flags Additional flags about how the object should be written.
87     * May be 0 or {@link #PARCELABLE_WRITE_RETURN_VALUE}.
88     */
89    public void writeToParcel(Parcel dest, int flags);
90
91    /**
92     * Interface that must be implemented and provided as a public CREATOR
93     * field that generates instances of your Parcelable class from a Parcel.
94     */
95    public interface Creator<T> {
96        /**
97         * Create a new instance of the Parcelable class, instantiating it
98         * from the given Parcel whose data had previously been written by
99         * {@link Parcelable#writeToParcel Parcelable.writeToParcel()}.
100         *
101         * @param source The Parcel to read the object's data from.
102         * @return Returns a new instance of the Parcelable class.
103         */
104        public T createFromParcel(Parcel source);
105
106        /**
107         * Create a new array of the Parcelable class.
108         *
109         * @param size Size of the array.
110         * @return Returns an array of the Parcelable class, with every entry
111         * initialized to null.
112         */
113        public T[] newArray(int size);
114    }
115
116    /**
117     * Specialization of {@link Creator} that allows you to receive the
118     * ClassLoader the object is being created in.
119     */
120    public interface ClassLoaderCreator<T> extends Creator<T> {
121        /**
122         * Create a new instance of the Parcelable class, instantiating it
123         * from the given Parcel whose data had previously been written by
124         * {@link Parcelable#writeToParcel Parcelable.writeToParcel()} and
125         * using the given ClassLoader.
126         *
127         * @param source The Parcel to read the object's data from.
128         * @param loader The ClassLoader that this object is being created in.
129         * @return Returns a new instance of the Parcelable class.
130         */
131        public T createFromParcel(Parcel source, ClassLoader loader);
132    }
133}
134