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.support.design.internal;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21import android.support.v4.os.ParcelableCompat;
22import android.support.v4.os.ParcelableCompatCreatorCallbacks;
23import android.util.SparseArray;
24
25/**
26 * @hide
27 */
28public class ParcelableSparseArray extends SparseArray<Parcelable> implements Parcelable {
29
30    public ParcelableSparseArray() {
31        super();
32    }
33
34    public ParcelableSparseArray(Parcel source, ClassLoader loader) {
35        super();
36        int size = source.readInt();
37        int[] keys = new int[size];
38        source.readIntArray(keys);
39        Parcelable[] values = source.readParcelableArray(loader);
40        for (int i = 0; i < size; ++i) {
41            put(keys[i], values[i]);
42        }
43    }
44
45    @Override
46    public int describeContents() {
47        return 0;
48    }
49
50    @Override
51    public void writeToParcel(Parcel parcel, int flags) {
52        int size = size();
53        int[] keys = new int[size];
54        Parcelable[] values = new Parcelable[size];
55        for (int i = 0; i < size; ++i) {
56            keys[i] = keyAt(i);
57            values[i] = valueAt(i);
58        }
59        parcel.writeInt(size);
60        parcel.writeIntArray(keys);
61        parcel.writeParcelableArray(values, flags);
62    }
63
64    public static final Parcelable.Creator<ParcelableSparseArray> CREATOR =
65            ParcelableCompat
66                    .newCreator(new ParcelableCompatCreatorCallbacks<ParcelableSparseArray>() {
67                        @Override
68                        public ParcelableSparseArray createFromParcel(Parcel source,
69                                ClassLoader loader) {
70                            return new ParcelableSparseArray(source, loader);
71                        }
72
73                        @Override
74                        public ParcelableSparseArray[] newArray(int size) {
75                            return new ParcelableSparseArray[size];
76                        }
77                    });
78}
79