1 /*
2  * Copyright (C) 2024 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 package android.adservices.common;
17 
18 import static android.adservices.common.ConsentStatus.ConsentStatusCode;
19 
20 import android.annotation.FlaggedApi;
21 import android.annotation.NonNull;
22 import android.annotation.SystemApi;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 
26 import com.android.adservices.flags.Flags;
27 
28 import java.util.Objects;
29 
30 /**
31  * Represent the common states from the getAdservicesCommonStates API.
32  *
33  * @hide
34  */
35 @SystemApi
36 @FlaggedApi(Flags.FLAG_GET_ADSERVICES_COMMON_STATES_API_ENABLED)
37 public final class AdServicesCommonStates implements Parcelable {
38     @ConsentStatusCode private final int mMeasurementState;
39     @ConsentStatusCode private final int mPaState;
40 
41     /**
42      * Creates an object which represents the result from the getAdservicesCommonStates API.
43      *
44      * @param measurementState a {@link ConsentStatusCode} int indicating whether meansurement is
45      *     allowed
46      * @param paState a {@link ConsentStatusCode} indicating whether fledge is allowed
47      */
AdServicesCommonStates( @onsentStatusCode int measurementState, @ConsentStatusCode int paState)48     private AdServicesCommonStates(
49             @ConsentStatusCode int measurementState, @ConsentStatusCode int paState) {
50         this.mMeasurementState = measurementState;
51         this.mPaState = paState;
52     }
53 
AdServicesCommonStates(@onNull Parcel in)54     private AdServicesCommonStates(@NonNull Parcel in) {
55         this.mMeasurementState = in.readInt();
56         this.mPaState = in.readInt();
57     }
58 
59     @NonNull
60     public static final Creator<AdServicesCommonStates> CREATOR =
61             new Creator<AdServicesCommonStates>() {
62                 @Override
63                 public AdServicesCommonStates createFromParcel(Parcel in) {
64                     Objects.requireNonNull(in);
65                     return new AdServicesCommonStates(in);
66                 }
67 
68                 @Override
69                 public AdServicesCommonStates[] newArray(int size) {
70                     return new AdServicesCommonStates[size];
71                 }
72             };
73 
74     /** describe contents for parcel */
describeContents()75     public int describeContents() {
76         return 0;
77     }
78 
79     /** write contents for parcel */
writeToParcel(@onNull Parcel out, int flags)80     public void writeToParcel(@NonNull Parcel out, int flags) {
81         out.writeInt(mMeasurementState);
82         out.writeInt(mPaState);
83     }
84 
85     /** Get the measurement allowed state. */
86     @ConsentStatusCode
getMeasurementState()87     public int getMeasurementState() {
88         return mMeasurementState;
89     }
90 
91     /** Get the fledge allowed state. */
92     @ConsentStatusCode
getPaState()93     public int getPaState() {
94         return mPaState;
95     }
96 
97     @Override
equals(Object object)98     public boolean equals(Object object) {
99         if (this == object) return true;
100         if (!(object instanceof AdServicesCommonStates)) return false;
101         AdServicesCommonStates adservicesCommonStates = (AdServicesCommonStates) object;
102         return getMeasurementState() == adservicesCommonStates.getMeasurementState()
103                 && getPaState() == adservicesCommonStates.getPaState();
104     }
105 
106     @Override
hashCode()107     public int hashCode() {
108         return Objects.hash(getMeasurementState(), getPaState());
109     }
110 
111     @Override
toString()112     public String toString() {
113         return "AdservicesCommonStates{"
114                 + "mMeasurementState="
115                 + mMeasurementState
116                 + ", mPaState="
117                 + mPaState
118                 + '}';
119     }
120 
121     /**
122      * Builder for {@link AdServicesCommonStates} objects.
123      *
124      * @hide
125      */
126     public static final class Builder {
127         @ConsentStatusCode private int mMeasurementState;
128         @ConsentStatusCode private int mPaState;
129 
Builder()130         public Builder() {}
131 
132         /** Set the measurement allowed by the getAdServicesCommonStates API */
133         @NonNull
setMeasurementState( @onsentStatusCode int measurementState)134         public AdServicesCommonStates.Builder setMeasurementState(
135                 @ConsentStatusCode int measurementState) {
136             mMeasurementState = measurementState;
137             return this;
138         }
139 
140         /** Set the pa allowed by the getAdServicesCommonStates API. */
141         @NonNull
setPaState(@onsentStatusCode int paState)142         public AdServicesCommonStates.Builder setPaState(@ConsentStatusCode int paState) {
143             mPaState = paState;
144             return this;
145         }
146 
147         /** Builds a {@link AdServicesCommonStates} instance. */
148         @NonNull
build()149         public AdServicesCommonStates build() {
150             return new AdServicesCommonStates(mMeasurementState, mPaState);
151         }
152     }
153 }
154