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 package android.adservices.adid;
17 
18 import android.annotation.NonNull;
19 
20 import java.util.Objects;
21 
22 /**
23  * A unique, user-resettable, device-wide, per-profile ID for advertising.
24  *
25  * <p>Ad networks may use {@code AdId} to monetize for Interest Based Advertising (IBA), i.e.
26  * targeting and remarketing ads. The user may limit availability of this identifier.
27  *
28  * @see AdIdManager#getAdId(Executor, OutcomeReceiver)
29  */
30 public class AdId {
31     @NonNull private final String mAdId;
32     private final boolean mLimitAdTrackingEnabled;
33 
34     /**
35      * A zeroed-out {@link #getAdId ad id} that is returned when the user has {@link
36      * #isLimitAdTrackingEnabled limited ad tracking}.
37      */
38     public static final String ZERO_OUT = "00000000-0000-0000-0000-000000000000";
39 
40     /**
41      * Creates an instance of {@link AdId}
42      *
43      * @param adId obtained from the provider service.
44      * @param limitAdTrackingEnabled value from the provider service which determines the value of
45      *     adId.
46      */
AdId(@onNull String adId, boolean limitAdTrackingEnabled)47     public AdId(@NonNull String adId, boolean limitAdTrackingEnabled) {
48         mAdId = adId;
49         mLimitAdTrackingEnabled = limitAdTrackingEnabled;
50     }
51 
52     /**
53      * The advertising ID.
54      *
55      * <p>The value of advertising Id depends on a combination of {@link
56      * #isLimitAdTrackingEnabled()} and {@link
57      * android.adservices.common.AdServicesPermissions#ACCESS_ADSERVICES_AD_ID}.
58      *
59      * <p>When the user is {@link #isLimitAdTrackingEnabled limiting ad tracking}, the API returns
60      * {@link #ZERO_OUT}. This disallows a caller to track the user for monetization purposes.
61      *
62      * <p>Otherwise, a string unique to the device and user is returned, which can be used to track
63      * users for advertising.
64      */
getAdId()65     public @NonNull String getAdId() {
66         return mAdId;
67     }
68 
69     /**
70      * Retrieves the limit ad tracking enabled setting.
71      *
72      * <p>This value is true if user has limit ad tracking enabled, {@code false} otherwise.
73      */
isLimitAdTrackingEnabled()74     public boolean isLimitAdTrackingEnabled() {
75         return mLimitAdTrackingEnabled;
76     }
77 
78     @Override
equals(Object o)79     public boolean equals(Object o) {
80         if (this == o) {
81             return true;
82         }
83         if (!(o instanceof AdId)) {
84             return false;
85         }
86         AdId that = (AdId) o;
87         return mAdId.equals(that.mAdId)
88                 && (mLimitAdTrackingEnabled == that.mLimitAdTrackingEnabled);
89     }
90 
91     @Override
hashCode()92     public int hashCode() {
93         return Objects.hash(mAdId, mLimitAdTrackingEnabled);
94     }
95 
96     @Override
toString()97     public String toString() {
98         return "AdId{"
99                 + "mAdId="
100                 + mAdId
101                 + ", mLimitAdTrackingEnabled='"
102                 + mLimitAdTrackingEnabled
103                 + '}';
104     }
105 }
106