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.common;
18 
19 import android.annotation.NonNull;
20 import android.annotation.SystemApi;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 
24 /**
25  * AdServicesStates exposed to system apps/services through the enableAdServices API. The bits
26  * stored in this parcel can change frequently based on user interaction with the Ads settings page.
27  *
28  * @hide
29  */
30 @SystemApi
31 public final class AdServicesStates implements Parcelable {
32 
33     public static final @NonNull Creator<AdServicesStates> CREATOR =
34             new Parcelable.Creator<AdServicesStates>() {
35                 @Override
36                 public AdServicesStates createFromParcel(Parcel in) {
37                     return new AdServicesStates(in);
38                 }
39 
40                 @Override
41                 public AdServicesStates[] newArray(int size) {
42                     return new AdServicesStates[size];
43                 }
44             };
45 
46     private boolean mIsPrivacySandboxUiEnabled;
47     private boolean mIsPrivacySandboxUiRequest;
48     private boolean mIsU18Account;
49     private boolean mIsAdultAccount;
50     private boolean mIsAdIdEnabled;
51 
AdServicesStates( boolean isPrivacySandboxUiEnabled, boolean isPrivacySandboxUiRequest, boolean isU18Account, boolean isAdultAccount, boolean isAdIdEnabled)52     private AdServicesStates(
53             boolean isPrivacySandboxUiEnabled,
54             boolean isPrivacySandboxUiRequest,
55             boolean isU18Account,
56             boolean isAdultAccount,
57             boolean isAdIdEnabled) {
58         mIsPrivacySandboxUiEnabled = isPrivacySandboxUiEnabled;
59         mIsPrivacySandboxUiRequest = isPrivacySandboxUiRequest;
60         mIsU18Account = isU18Account;
61         mIsAdultAccount = isAdultAccount;
62         mIsAdIdEnabled = isAdIdEnabled;
63     }
64 
AdServicesStates(@onNull Parcel in)65     private AdServicesStates(@NonNull Parcel in) {
66         mIsPrivacySandboxUiEnabled = in.readBoolean();
67         mIsPrivacySandboxUiRequest = in.readBoolean();
68         mIsU18Account = in.readBoolean();
69         mIsAdultAccount = in.readBoolean();
70         mIsAdIdEnabled = in.readBoolean();
71     }
72 
73     @Override
describeContents()74     public int describeContents() {
75         return 0;
76     }
77 
78     @Override
writeToParcel(@onNull Parcel out, int flags)79     public void writeToParcel(@NonNull Parcel out, int flags) {
80         out.writeBoolean(mIsPrivacySandboxUiEnabled);
81         out.writeBoolean(mIsPrivacySandboxUiRequest);
82         out.writeBoolean(mIsU18Account);
83         out.writeBoolean(mIsAdultAccount);
84         out.writeBoolean(mIsAdIdEnabled);
85     }
86 
87     /** Returns whether the privacy sandbox UI is visible from the settings app. */
88     @NonNull
isPrivacySandboxUiEnabled()89     public boolean isPrivacySandboxUiEnabled() {
90         return mIsPrivacySandboxUiEnabled;
91     }
92 
93     /**
94      * Returns whether the API call was the byproduct of a privacy sandbox UI request from the
95      * settings app.
96      */
97     @NonNull
isPrivacySandboxUiRequest()98     public boolean isPrivacySandboxUiRequest() {
99         return mIsPrivacySandboxUiRequest;
100     }
101 
102     /** Returns whether Advertising ID is enabled. */
103     @NonNull
isAdIdEnabled()104     public boolean isAdIdEnabled() {
105         return mIsAdIdEnabled;
106     }
107 
108     /**
109      * Determines whether the user account is eligible for the U18 (under 18) privacy sandbox, in
110      * which all ads relevancepersonalized Ads APIs are * permanently disabled and the ad
111      * measurement API can be enabled/disabled by the user. An account is considered a U18 account
112      * if privacy sandbox has received signals that the user is a minor.
113      */
114     @NonNull
isU18Account()115     public boolean isU18Account() {
116         return mIsU18Account;
117     }
118 
119     /**
120      * Determines whether the user account is eligible for the adult or full-fledged privacy
121      * sandbox, in which all Ads APIs can be * enabled/disabled by the user. An account is
122      * considered an adult account if privacy sandbox has received signals that the user is an
123      * adult.
124      */
125     @NonNull
isAdultAccount()126     public boolean isAdultAccount() {
127         return mIsAdultAccount;
128     }
129 
130     /** Builder for {@link AdServicesStates} objects. */
131     public static final class Builder {
132         private boolean mIsPrivacySandboxUiEnabled;
133         private boolean mIsPrivacySandboxUiRequest;
134         private boolean mIsU18Account;
135         private boolean mIsAdultAccount;
136         private boolean mIsAdIdEnabled;
137 
Builder()138         public Builder() {
139         }
140 
141         /** Set if the privacy sandbox UX entry point is enabled. */
setPrivacySandboxUiEnabled(boolean isPrivacySandboxUiEnabled)142         public @NonNull Builder setPrivacySandboxUiEnabled(boolean isPrivacySandboxUiEnabled) {
143             mIsPrivacySandboxUiEnabled = isPrivacySandboxUiEnabled;
144             return this;
145         }
146 
147         /** Set if the API call was the result of a privacy sandbox UX entry point request. */
setPrivacySandboxUiRequest(boolean isPrivacySandboxUiRequest)148         public @NonNull Builder setPrivacySandboxUiRequest(boolean isPrivacySandboxUiRequest) {
149             mIsPrivacySandboxUiRequest = isPrivacySandboxUiRequest;
150             return this;
151         }
152 
153         /** Set if the device is currently running under an U18 account. */
setU18Account(boolean isU18Account)154         public @NonNull Builder setU18Account(boolean isU18Account) {
155             mIsU18Account = isU18Account;
156             return this;
157         }
158 
159         /** Set if the device is currently running under an adult account. */
setAdultAccount(boolean isAdultAccount)160         public @NonNull Builder setAdultAccount(boolean isAdultAccount) {
161             mIsAdultAccount = isAdultAccount;
162             return this;
163         }
164 
165         /** Set if user has opt-in/out of Advertising ID. */
setAdIdEnabled(boolean isAdIdEnabled)166         public @NonNull Builder setAdIdEnabled(boolean isAdIdEnabled) {
167             mIsAdIdEnabled = isAdIdEnabled;
168             return this;
169         }
170 
171         /** Builds a {@link AdServicesStates} instance. */
build()172         public @NonNull AdServicesStates build() {
173             return new AdServicesStates(
174                     mIsPrivacySandboxUiEnabled,
175                     mIsPrivacySandboxUiRequest,
176                     mIsU18Account,
177                     mIsAdultAccount,
178                     mIsAdIdEnabled);
179         }
180     }
181 }
182