• 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.StringWrapper;
24 
25 @CustomParcelableWrapper(originalType = StringWrapper.class)
26 public class ParcelableStringWrapper implements Parcelable {
27 
28   private static final int NULL = -1;
29   private static final int NOT_NULL = 1;
30 
31   private final StringWrapper stringWrapper;
32 
33   /**
34    * Create a wrapper for a given {@link StringWrapper}.
35    *
36    * <p>The passed in {@link Bundler} and {@link BundlerType} are ignored.
37    */
of( Bundler bundler, BundlerType type, StringWrapper stringWrapper)38   public static ParcelableStringWrapper of(
39       Bundler bundler, BundlerType type, StringWrapper stringWrapper) {
40     return new ParcelableStringWrapper(bundler, type, stringWrapper);
41   }
42 
get()43   public StringWrapper get() {
44     return stringWrapper;
45   }
46 
ParcelableStringWrapper(Bundler bundler, BundlerType type, StringWrapper stringWrapper)47   private ParcelableStringWrapper(Bundler bundler, BundlerType type, StringWrapper stringWrapper) {
48     // Ignore bundler and type as we aren't generic
49     this.stringWrapper = stringWrapper;
50   }
51 
ParcelableStringWrapper(Parcel in)52   private ParcelableStringWrapper(Parcel in) {
53     int presentValue = in.readInt();
54 
55     if (presentValue == NULL) {
56       stringWrapper = null;
57       return;
58     }
59 
60     String value = in.readString();
61 
62     stringWrapper = new StringWrapper(value);
63   }
64 
65   @Override
writeToParcel(Parcel dest, int flags)66   public void writeToParcel(Parcel dest, int flags) {
67     if (stringWrapper == null) {
68       dest.writeInt(NULL);
69       return;
70     }
71 
72     dest.writeInt(NOT_NULL);
73     dest.writeString(stringWrapper.value());
74   }
75 
76   @Override
describeContents()77   public int describeContents() {
78     return 0;
79   }
80 
81   public static final Creator<ParcelableStringWrapper> CREATOR =
82       new Creator<ParcelableStringWrapper>() {
83         @Override
84         public ParcelableStringWrapper createFromParcel(Parcel in) {
85           return new ParcelableStringWrapper(in);
86         }
87 
88         @Override
89         public ParcelableStringWrapper[] newArray(int size) {
90           return new ParcelableStringWrapper[size];
91         }
92       };
93 }
94