1 /*
2  * Copyright (C) 2023 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.adservices.extdata;
18 
19 import android.annotation.FlaggedApi;
20 import android.annotation.IntDef;
21 import android.annotation.NonNull;
22 import android.annotation.SuppressLint;
23 import android.annotation.SystemApi;
24 import android.os.Parcel;
25 import android.os.Parcelable;
26 
27 import com.android.adservices.flags.Flags;
28 
29 import java.lang.annotation.Retention;
30 import java.lang.annotation.RetentionPolicy;
31 
32 /**
33  * Container for the data fields handled by {@link AdServicesExtDataStorageService}.
34  *
35  * @hide
36  */
37 @SystemApi
38 @FlaggedApi(Flags.FLAG_ADEXT_DATA_SERVICE_APIS_ENABLED)
39 public final class AdServicesExtDataParams implements Parcelable {
40     /**
41      * Custom tri-state boolean type to represent true, false, and unknown
42      *
43      * @hide
44      */
45     @Retention(RetentionPolicy.SOURCE)
46     @IntDef(
47             prefix = "BOOLEAN_",
48             value = {BOOLEAN_TRUE, BOOLEAN_FALSE, BOOLEAN_UNKNOWN})
49     public @interface TriStateBoolean {}
50 
51     /**
52      * Int value to represent true.
53      *
54      * @hide
55      */
56     public static final int BOOLEAN_TRUE = 1;
57 
58     /**
59      * Int value to represent false.
60      *
61      * @hide
62      */
63     public static final int BOOLEAN_FALSE = 0;
64 
65     /**
66      * Int value to represent unknown.
67      *
68      * @hide
69      */
70     public static final int BOOLEAN_UNKNOWN = -1;
71 
72     /**
73      * Type to represent user manual interaction state.
74      *
75      * @hide
76      */
77     @IntDef(
78             prefix = "STATE_",
79             value = {
80                 STATE_NO_MANUAL_INTERACTIONS_RECORDED,
81                 STATE_UNKNOWN,
82                 STATE_MANUAL_INTERACTIONS_RECORDED
83             })
84     @Retention(RetentionPolicy.SOURCE)
85     public @interface UserManualInteraction {}
86 
87     /**
88      * Int value to represent no manual interaction recorded state.
89      *
90      * @hide
91      */
92     public static final int STATE_NO_MANUAL_INTERACTIONS_RECORDED = -1;
93 
94     /**
95      * Int value to represent unknown manual interaction state.
96      *
97      * @hide
98      */
99     public static final int STATE_UNKNOWN = 0;
100 
101     /**
102      * Int value to represent manual interaction reported state.
103      *
104      * @hide
105      */
106     public static final int STATE_MANUAL_INTERACTIONS_RECORDED = 1;
107 
108     @TriStateBoolean private final int mIsNotificationDisplayed;
109     @TriStateBoolean private final int mIsMeasurementConsented;
110     @TriStateBoolean private final int mIsU18Account;
111     @TriStateBoolean private final int mIsAdultAccount;
112     @UserManualInteraction private final int mManualInteractionWithConsentStatus;
113     private final long mMeasurementRollbackApexVersion;
114 
115     /**
116      * Init AdServicesExtDataParams.
117      *
118      * @param isNotificationDisplayed 1 if notification is displayed, 0 if notification not
119      *     displayed, -1 to represent no data.
120      * @param isMeasurementConsented 1 if measurement consented, 0 if not, -1 to represent no data.
121      * @param isU18Account 1 if account is U18, 0 if not, -1 if no data.
122      * @param isAdultAccount 1 if adult account, 0 if not, -1 if no data.
123      * @param manualInteractionWithConsentStatus 1 if user interacted, -1 if not, 0 if unknown.
124      * @param measurementRollbackApexVersion ExtServices apex version for measurement rollback
125      *     handling. -1 if no data.
126      */
AdServicesExtDataParams( @riStateBoolean int isNotificationDisplayed, @TriStateBoolean int isMeasurementConsented, @TriStateBoolean int isU18Account, @TriStateBoolean int isAdultAccount, @UserManualInteraction int manualInteractionWithConsentStatus, long measurementRollbackApexVersion)127     public AdServicesExtDataParams(
128             @TriStateBoolean int isNotificationDisplayed,
129             @TriStateBoolean int isMeasurementConsented,
130             @TriStateBoolean int isU18Account,
131             @TriStateBoolean int isAdultAccount,
132             @UserManualInteraction int manualInteractionWithConsentStatus,
133             long measurementRollbackApexVersion) {
134         mIsNotificationDisplayed = isNotificationDisplayed;
135         mIsMeasurementConsented = isMeasurementConsented;
136         mIsU18Account = isU18Account;
137         mIsAdultAccount = isAdultAccount;
138         mManualInteractionWithConsentStatus = manualInteractionWithConsentStatus;
139         mMeasurementRollbackApexVersion = measurementRollbackApexVersion;
140     }
141 
AdServicesExtDataParams(@onNull Parcel in)142     private AdServicesExtDataParams(@NonNull Parcel in) {
143         mIsNotificationDisplayed = in.readInt();
144         mIsMeasurementConsented = in.readInt();
145         mIsU18Account = in.readInt();
146         mIsAdultAccount = in.readInt();
147         mManualInteractionWithConsentStatus = in.readInt();
148         mMeasurementRollbackApexVersion = in.readLong();
149     }
150 
151     /** Creator for Parcelable. */
152     @NonNull
153     public static final Creator<AdServicesExtDataParams> CREATOR =
154             new Creator<AdServicesExtDataParams>() {
155                 @Override
156                 public AdServicesExtDataParams createFromParcel(Parcel in) {
157                     return new AdServicesExtDataParams(in);
158                 }
159 
160                 @Override
161                 public AdServicesExtDataParams[] newArray(int size) {
162                     return new AdServicesExtDataParams[size];
163                 }
164             };
165 
166     @Override
describeContents()167     public int describeContents() {
168         return 0;
169     }
170 
171     @Override
writeToParcel(@onNull Parcel out, int flags)172     public void writeToParcel(@NonNull Parcel out, int flags) {
173         out.writeInt(mIsNotificationDisplayed);
174         out.writeInt(mIsMeasurementConsented);
175         out.writeInt(mIsU18Account);
176         out.writeInt(mIsAdultAccount);
177         out.writeInt(mManualInteractionWithConsentStatus);
178         out.writeLong(mMeasurementRollbackApexVersion);
179     }
180 
181     /** Returns 1 if notification was shown on R, 0 if not shown, -1 if unknown. */
182     @TriStateBoolean
getIsNotificationDisplayed()183     public int getIsNotificationDisplayed() {
184         return mIsNotificationDisplayed;
185     }
186 
187     /** Returns 1 if measurement was consented, 0 if not, -1 if unknown. */
188     @TriStateBoolean
getIsMeasurementConsented()189     public int getIsMeasurementConsented() {
190         return mIsMeasurementConsented;
191     }
192 
193     /** Returns 1 if account is U18 account, 0 if not, -1 if unknown. */
194     @TriStateBoolean
getIsU18Account()195     public int getIsU18Account() {
196         return mIsU18Account;
197     }
198 
199     /** Returns 1 if account is adult account, 0 if not, -1 if unknown. */
200     @TriStateBoolean
getIsAdultAccount()201     public int getIsAdultAccount() {
202         return mIsAdultAccount;
203     }
204 
205     /** Returns 1 if user interacted, -1 if not, 0 if unknown. */
206     @UserManualInteraction
getManualInteractionWithConsentStatus()207     public int getManualInteractionWithConsentStatus() {
208         return mManualInteractionWithConsentStatus;
209     }
210 
211     /**
212      * Returns ExtServices apex version for handling measurement rollback. -1 is returned if no data
213      * is available.
214      */
getMeasurementRollbackApexVersion()215     public long getMeasurementRollbackApexVersion() {
216         return mMeasurementRollbackApexVersion;
217     }
218 
219     @SuppressLint("DefaultLocale")
220     @Override
toString()221     public String toString() {
222         return String.format(
223                 "AdServicesExtDataParams{"
224                         + "mIsNotificationDisplayed=%d, "
225                         + "mIsMsmtConsented=%d, "
226                         + "mIsU18Account=%d, "
227                         + "mIsAdultAccount=%d, "
228                         + "mManualInteractionWithConsentStatus=%d, "
229                         + "mMsmtRollbackApexVersion=%d}",
230                 mIsNotificationDisplayed,
231                 mIsMeasurementConsented,
232                 mIsU18Account,
233                 mIsAdultAccount,
234                 mManualInteractionWithConsentStatus,
235                 mMeasurementRollbackApexVersion);
236     }
237 
238     /**
239      * Builder for {@link AdServicesExtDataParams} objects.
240      *
241      * @hide
242      */
243     public static final class Builder {
244         @TriStateBoolean private int mNotificationDisplayed;
245         @TriStateBoolean private int mMsmtConsent;
246         @TriStateBoolean private int mIsU18Account;
247         @TriStateBoolean private int mIsAdultAccount;
248         @UserManualInteraction private int mManualInteractionWithConsentStatus;
249         private long mMsmtRollbackApexVersion;
250 
251         /** Set the isNotificationDisplayed. */
252         @NonNull
setNotificationDisplayed( @riStateBoolean int notificationDisplayed)253         public AdServicesExtDataParams.Builder setNotificationDisplayed(
254                 @TriStateBoolean int notificationDisplayed) {
255             mNotificationDisplayed = notificationDisplayed;
256             return this;
257         }
258 
259         /** Set the isMeasurementConsented. */
260         @NonNull
setMsmtConsent(@riStateBoolean int msmtConsent)261         public AdServicesExtDataParams.Builder setMsmtConsent(@TriStateBoolean int msmtConsent) {
262             mMsmtConsent = msmtConsent;
263             return this;
264         }
265 
266         /** Set the isU18Account. */
267         @NonNull
setIsU18Account(@riStateBoolean int isU18Account)268         public AdServicesExtDataParams.Builder setIsU18Account(@TriStateBoolean int isU18Account) {
269             mIsU18Account = isU18Account;
270             return this;
271         }
272 
273         /** Set the isAdultAccount. */
274         @NonNull
setIsAdultAccount( @riStateBoolean int isAdultAccount)275         public AdServicesExtDataParams.Builder setIsAdultAccount(
276                 @TriStateBoolean int isAdultAccount) {
277             mIsAdultAccount = isAdultAccount;
278             return this;
279         }
280 
281         /** Set the manualInteractionWithConsentStatus. */
282         @NonNull
setManualInteractionWithConsentStatus( @serManualInteraction int manualInteractionWithConsentStatus)283         public AdServicesExtDataParams.Builder setManualInteractionWithConsentStatus(
284                 @UserManualInteraction int manualInteractionWithConsentStatus) {
285             mManualInteractionWithConsentStatus = manualInteractionWithConsentStatus;
286             return this;
287         }
288 
289         /** Set the msmtRollbackApexVersion. */
290         @NonNull
setMsmtRollbackApexVersion( long msmtRollbackApexVersion)291         public AdServicesExtDataParams.Builder setMsmtRollbackApexVersion(
292                 long msmtRollbackApexVersion) {
293             mMsmtRollbackApexVersion = msmtRollbackApexVersion;
294             return this;
295         }
296 
Builder()297         public Builder() {}
298 
299         /** Builds a {@link AdServicesExtDataParams} instance. */
300         @NonNull
build()301         public AdServicesExtDataParams build() {
302             return new AdServicesExtDataParams(
303                     mNotificationDisplayed,
304                     mMsmtConsent,
305                     mIsU18Account,
306                     mIsAdultAccount,
307                     mManualInteractionWithConsentStatus,
308                     mMsmtRollbackApexVersion);
309         }
310     }
311 }
312