1 /*
2  * Copyright (C) 2022 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 android.app.adservices.consent;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 
24 import java.lang.annotation.Retention;
25 import java.lang.annotation.RetentionPolicy;
26 
27 /**
28  * Represent a User Consent.
29  *
30  * @hide
31  */
32 public final class ConsentParcel implements Parcelable {
33     /**
34      * Consent Api Types.
35      *
36      * @hide
37      */
38     @IntDef(value = {UNKNOWN, ALL_API, TOPICS, FLEDGE, MEASUREMENT})
39     @Retention(RetentionPolicy.SOURCE)
40     public @interface ConsentApiType {}
41 
42     /** The Consent API Type is not set. */
43     public static final int UNKNOWN = 0;
44 
45     /** The Consent API Type for All API. This is used when there is only 1 consent for all APIs */
46     public static final int ALL_API = 1;
47 
48     /** The Consent API Type for Topics. */
49     public static final int TOPICS = 2;
50 
51     /** The Consent API Type for FLEDGE. */
52     public static final int FLEDGE = 3;
53 
54     /** The Consent API Type for Measurement. */
55     public static final int MEASUREMENT = 4;
56 
57     private final boolean mIsGiven;
58     @ConsentApiType private final int mConsentApiType;
59 
ConsentParcel(@onNull Builder builder)60     private ConsentParcel(@NonNull Builder builder) {
61         mIsGiven = builder.mIsGiven;
62         mConsentApiType = builder.mConsentApiType;
63     }
64 
ConsentParcel(@onNull Parcel in)65     private ConsentParcel(@NonNull Parcel in) {
66         mConsentApiType = in.readInt();
67         mIsGiven = in.readBoolean();
68     }
69 
70     public static final @NonNull Creator<ConsentParcel> CREATOR =
71             new Parcelable.Creator<ConsentParcel>() {
72                 @Override
73                 public ConsentParcel createFromParcel(Parcel in) {
74                     return new ConsentParcel(in);
75                 }
76 
77                 @Override
78                 public ConsentParcel[] newArray(int size) {
79                     return new ConsentParcel[size];
80                 }
81             };
82 
83     @Override
describeContents()84     public int describeContents() {
85         return 0;
86     }
87 
88     @Override
writeToParcel(@onNull Parcel out, int flags)89     public void writeToParcel(@NonNull Parcel out, int flags) {
90         out.writeInt(mConsentApiType);
91         out.writeBoolean(mIsGiven);
92     }
93 
94     /** Get the ConsentApiType. */
95     @ConsentApiType
getConsentApiType()96     public int getConsentApiType() {
97         return mConsentApiType;
98     }
99 
100     /** Get the IsGiven. */
isIsGiven()101     public boolean isIsGiven() {
102         return mIsGiven;
103     }
104 
105     /** Create a REVOKED consent for the consentApiType */
createRevokedConsent(@onsentApiType int consentApiType)106     public static ConsentParcel createRevokedConsent(@ConsentApiType int consentApiType) {
107         return new ConsentParcel.Builder()
108                 .setConsentApiType(consentApiType)
109                 .setIsGiven(false)
110                 .build();
111     }
112 
113     /** Create a GIVEN consent for the consentApiType */
createGivenConsent(@onsentApiType int consentApiType)114     public static ConsentParcel createGivenConsent(@ConsentApiType int consentApiType) {
115         return new ConsentParcel.Builder()
116                 .setConsentApiType(consentApiType)
117                 .setIsGiven(true)
118                 .build();
119     }
120 
121     /** Builder for {@link ConsentParcel} objects. */
122     public static final class Builder {
123         @ConsentApiType private int mConsentApiType = UNKNOWN;
124         private boolean mIsGiven = false;
125 
Builder()126         public Builder() {}
127 
128         /** Set the ConsentApiType for this request */
setConsentApiType(@onsentApiType int consentApiType)129         public @NonNull Builder setConsentApiType(@ConsentApiType int consentApiType) {
130             mConsentApiType = consentApiType;
131             return this;
132         }
133 
134         /** Set the IsGiven */
setIsGiven(Boolean isGiven)135         public @NonNull Builder setIsGiven(Boolean isGiven) {
136             // null input means isGiven = false
137             mIsGiven = isGiven != null ? isGiven : false;
138             return this;
139         }
140 
141         /** Builds a {@link ConsentParcel} instance. */
build()142         public @NonNull ConsentParcel build() {
143 
144             if (mConsentApiType == UNKNOWN) {
145                 throw new IllegalArgumentException("One must set the valid ConsentApiType");
146             }
147 
148             return new ConsentParcel(this);
149         }
150     }
151 }
152