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