1 /*
2  * Copyright (C) 2016 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 
17 package com.android.tv.data;
18 
19 import android.os.Parcel;
20 import android.os.Parcelable;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.List;
24 
25 /** A convenience class for the list of {@link Parcelable}s. */
26 public final class ParcelableList<T extends Parcelable> implements Parcelable {
27     /** Create instance from {@link Parcel}. */
fromParcel(Parcel in)28     public static ParcelableList fromParcel(Parcel in) {
29         ParcelableList list = new ParcelableList();
30         int length = in.readInt();
31         if (length > 0) {
32             for (int i = 0; i < length; ++i) {
33                 list.mList.add(in.readParcelable(Thread.currentThread().getContextClassLoader()));
34             }
35         }
36         return list;
37     }
38 
39     /** A creator for {@link ParcelableList}. */
40     public static final Creator<ParcelableList> CREATOR =
41             new Creator<ParcelableList>() {
42                 @Override
43                 public ParcelableList createFromParcel(Parcel in) {
44                     return ParcelableList.fromParcel(in);
45                 }
46 
47                 @Override
48                 public ParcelableList[] newArray(int size) {
49                     return new ParcelableList[size];
50                 }
51             };
52 
53     private final List<T> mList = new ArrayList<>();
54 
ParcelableList()55     private ParcelableList() {}
56 
ParcelableList(Collection<T> initialList)57     public ParcelableList(Collection<T> initialList) {
58         mList.addAll(initialList);
59     }
60 
61     /** Returns the list. */
getList()62     public List<T> getList() {
63         return new ArrayList<T>(mList);
64     }
65 
66     @Override
describeContents()67     public int describeContents() {
68         return 0;
69     }
70 
71     @Override
writeToParcel(Parcel out, int paramInt)72     public void writeToParcel(Parcel out, int paramInt) {
73         out.writeInt(mList.size());
74         for (T data : mList) {
75             out.writeParcelable(data, 0);
76         }
77     }
78 }
79