1 /* 2 * Copyright 2021 Google LLC 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 * https://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 package com.google.android.enterprise.connectedapps.parcelablewrappers; 17 18 import android.os.Parcel; 19 import android.os.Parcelable; 20 import com.google.android.enterprise.connectedapps.internal.Bundler; 21 import com.google.android.enterprise.connectedapps.internal.BundlerType; 22 import java.util.ArrayList; 23 import java.util.Collection; 24 25 /** 26 * Wrapper for reading & writing {@link Collection} instances from and to {@link Parcel} instances. 27 */ 28 public class ParcelableCollection<E> implements Parcelable { 29 30 private static final int NULL_SIZE = -1; 31 32 private final Bundler bundler; 33 private final BundlerType type; 34 private final Collection<E> collection; 35 36 /** 37 * Create a wrapper for a given collection. 38 * 39 * <p>The passed in {@link Bundler} must be capable of bundling {@code F}. 40 */ of( Bundler bundler, BundlerType type, Collection<F> collection)41 public static <F> ParcelableCollection<F> of( 42 Bundler bundler, BundlerType type, Collection<F> collection) { 43 return new ParcelableCollection<>(bundler, type, collection); 44 } 45 get()46 public Collection<E> get() { 47 return collection; 48 } 49 ParcelableCollection(Bundler bundler, BundlerType type, Collection<E> collection)50 private ParcelableCollection(Bundler bundler, BundlerType type, Collection<E> collection) { 51 if (bundler == null || type == null) { 52 throw new NullPointerException(); 53 } 54 this.bundler = bundler; 55 this.type = type; 56 this.collection = collection; 57 } 58 ParcelableCollection(Parcel in)59 private ParcelableCollection(Parcel in) { 60 bundler = in.readParcelable(Bundler.class.getClassLoader()); 61 int size = in.readInt(); 62 if (size == NULL_SIZE) { 63 type = null; 64 collection = null; 65 return; 66 } 67 68 collection = new ArrayList<>(); 69 type = (BundlerType) in.readParcelable(Bundler.class.getClassLoader()); 70 if (size > 0) { 71 BundlerType valueType = type.typeArguments().get(0); 72 for (int i = 0; i < size; i++) { 73 @SuppressWarnings("unchecked") 74 E value = (E) bundler.readFromParcel(in, valueType); 75 collection.add(value); 76 } 77 } 78 } 79 80 @Override writeToParcel(Parcel dest, int flags)81 public void writeToParcel(Parcel dest, int flags) { 82 dest.writeParcelable(bundler, flags); 83 84 if (collection == null) { 85 dest.writeInt(NULL_SIZE); 86 return; 87 } 88 89 dest.writeInt(collection.size()); 90 dest.writeParcelable(type, flags); 91 if (!collection.isEmpty()) { 92 BundlerType valueType = type.typeArguments().get(0); 93 for (E value : collection) { 94 bundler.writeToParcel(dest, value, valueType, flags); 95 } 96 } 97 } 98 99 @Override describeContents()100 public int describeContents() { 101 return 0; 102 } 103 104 @SuppressWarnings("rawtypes") 105 public static final Creator<ParcelableCollection> CREATOR = 106 new Creator<ParcelableCollection>() { 107 @Override 108 public ParcelableCollection createFromParcel(Parcel in) { 109 return new ParcelableCollection(in); 110 } 111 112 @Override 113 public ParcelableCollection[] newArray(int size) { 114 return new ParcelableCollection[size]; 115 } 116 }; 117 } 118