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