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.testapp.wrappers; 17 18 import android.os.Parcel; 19 import android.os.Parcelable; 20 import com.google.android.enterprise.connectedapps.annotations.CustomParcelableWrapper; 21 import com.google.android.enterprise.connectedapps.internal.Bundler; 22 import com.google.android.enterprise.connectedapps.internal.BundlerType; 23 import com.google.android.enterprise.connectedapps.testapp.CustomWrapper; 24 import com.google.android.enterprise.connectedapps.testapp.CustomWrapper2; 25 26 @CustomParcelableWrapper(originalType = CustomWrapper2.class) 27 public class ParcelableCustomWrapper2<E> implements Parcelable { 28 29 private static final int NULL = -1; 30 private static final int NOT_NULL = 1; 31 32 private final Bundler bundler; 33 private final BundlerType type; 34 private final CustomWrapper2<E> customWrapper; 35 36 /** 37 * Create a wrapper for a given {@link CustomWrapper}. 38 * 39 * <p>The passed in {@link Bundler} must be capable of bundling {@code F}. 40 */ of( Bundler bundler, BundlerType type, CustomWrapper2<F> customWrapper)41 public static <F> ParcelableCustomWrapper2<F> of( 42 Bundler bundler, BundlerType type, CustomWrapper2<F> customWrapper) { 43 return new ParcelableCustomWrapper2<>(bundler, type, customWrapper); 44 } 45 get()46 public CustomWrapper2<E> get() { 47 return customWrapper; 48 } 49 ParcelableCustomWrapper2( Bundler bundler, BundlerType type, CustomWrapper2<E> customWrapper)50 private ParcelableCustomWrapper2( 51 Bundler bundler, BundlerType type, CustomWrapper2<E> customWrapper) { 52 if (bundler == null || type == null) { 53 throw new NullPointerException(); 54 } 55 this.bundler = bundler; 56 this.type = type; 57 this.customWrapper = customWrapper; 58 } 59 ParcelableCustomWrapper2(Parcel in)60 private ParcelableCustomWrapper2(Parcel in) { 61 bundler = in.readParcelable(Bundler.class.getClassLoader()); 62 63 int presentValue = in.readInt(); 64 65 if (presentValue == NULL) { 66 type = null; 67 customWrapper = null; 68 return; 69 } 70 71 type = (BundlerType) in.readParcelable(Bundler.class.getClassLoader()); 72 BundlerType valueType = type.typeArguments().get(0); 73 74 @SuppressWarnings("unchecked") 75 E value = (E) bundler.readFromParcel(in, valueType); 76 77 customWrapper = new CustomWrapper2<>(value); 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 (customWrapper == null) { 85 dest.writeInt(NULL); 86 return; 87 } 88 89 dest.writeInt(NOT_NULL); 90 dest.writeParcelable(type, flags); 91 BundlerType valueType = type.typeArguments().get(0); 92 bundler.writeToParcel(dest, customWrapper.value(), valueType, flags); 93 } 94 95 @Override describeContents()96 public int describeContents() { 97 return 0; 98 } 99 100 @SuppressWarnings("rawtypes") 101 public static final Creator<ParcelableCustomWrapper2> CREATOR = 102 new Creator<ParcelableCustomWrapper2>() { 103 @Override 104 public ParcelableCustomWrapper2 createFromParcel(Parcel in) { 105 return new ParcelableCustomWrapper2(in); 106 } 107 108 @Override 109 public ParcelableCustomWrapper2[] newArray(int size) { 110 return new ParcelableCustomWrapper2[size]; 111 } 112 }; 113 } 114