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