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