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