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.adservices.adid;
18 
19 import android.adservices.common.AdServicesResponse;
20 import android.adservices.common.AdServicesStatusUtils;
21 import android.annotation.NonNull;
22 import android.annotation.Nullable;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 
26 import java.util.Objects;
27 
28 /**
29  * Represent the result from the getAdId API.
30  *
31  * @hide
32  */
33 public final class GetAdIdResult extends AdServicesResponse {
34     @NonNull private final String mAdId;
35     private final boolean mLimitAdTrackingEnabled;
36 
GetAdIdResult( @dServicesStatusUtils.StatusCode int resultCode, @Nullable String errorMessage, @NonNull String adId, boolean isLimitAdTrackingEnabled)37     private GetAdIdResult(
38             @AdServicesStatusUtils.StatusCode int resultCode,
39             @Nullable String errorMessage,
40             @NonNull String adId,
41             boolean isLimitAdTrackingEnabled) {
42         super(resultCode, errorMessage);
43         mAdId = adId;
44         mLimitAdTrackingEnabled = isLimitAdTrackingEnabled;
45     }
46 
GetAdIdResult(@onNull Parcel in)47     private GetAdIdResult(@NonNull Parcel in) {
48         super(in);
49         Objects.requireNonNull(in);
50 
51         mAdId = in.readString();
52         mLimitAdTrackingEnabled = in.readBoolean();
53     }
54 
55     public static final @NonNull Creator<GetAdIdResult> CREATOR =
56             new Parcelable.Creator<GetAdIdResult>() {
57                 @Override
58                 public GetAdIdResult createFromParcel(@NonNull Parcel in) {
59                     Objects.requireNonNull(in);
60                     return new GetAdIdResult(in);
61                 }
62 
63                 @Override
64                 public GetAdIdResult[] newArray(int size) {
65                     return new GetAdIdResult[size];
66                 }
67             };
68 
69     /** @hide */
70     @Override
describeContents()71     public int describeContents() {
72         return 0;
73     }
74 
75     /** @hide */
76     @Override
writeToParcel(@onNull Parcel out, int flags)77     public void writeToParcel(@NonNull Parcel out, int flags) {
78         out.writeInt(mStatusCode);
79         out.writeString(mErrorMessage);
80         out.writeString(mAdId);
81         out.writeBoolean(mLimitAdTrackingEnabled);
82     }
83 
84     /**
85      * Returns the error message associated with this result.
86      *
87      * <p>If {@link #isSuccess} is {@code true}, the error message is always {@code null}. The error
88      * message may be {@code null} even if {@link #isSuccess} is {@code false}.
89      */
90     @Nullable
getErrorMessage()91     public String getErrorMessage() {
92         return mErrorMessage;
93     }
94 
95     /** Returns the advertising ID associated with this result. */
96     @NonNull
getAdId()97     public String getAdId() {
98         return mAdId;
99     }
100 
101     /** Returns the Limited adtracking field associated with this result. */
isLatEnabled()102     public boolean isLatEnabled() {
103         return mLimitAdTrackingEnabled;
104     }
105 
106     @Override
toString()107     public String toString() {
108         return "GetAdIdResult{"
109                 + "mResultCode="
110                 + mStatusCode
111                 + ", mErrorMessage='"
112                 + mErrorMessage
113                 + '\''
114                 + ", mAdId="
115                 + mAdId
116                 + ", mLimitAdTrackingEnabled="
117                 + mLimitAdTrackingEnabled
118                 + '}';
119     }
120 
121     @Override
equals(Object o)122     public boolean equals(Object o) {
123         if (this == o) {
124             return true;
125         }
126 
127         if (!(o instanceof GetAdIdResult)) {
128             return false;
129         }
130 
131         GetAdIdResult that = (GetAdIdResult) o;
132 
133         return mStatusCode == that.mStatusCode
134                 && Objects.equals(mErrorMessage, that.mErrorMessage)
135                 && Objects.equals(mAdId, that.mAdId)
136                 && (mLimitAdTrackingEnabled == that.mLimitAdTrackingEnabled);
137     }
138 
139     @Override
hashCode()140     public int hashCode() {
141         return Objects.hash(mStatusCode, mErrorMessage, mAdId, mLimitAdTrackingEnabled);
142     }
143 
144     /**
145      * Builder for {@link GetAdIdResult} objects.
146      *
147      * @hide
148      */
149     public static final class Builder {
150         private @AdServicesStatusUtils.StatusCode int mStatusCode;
151         @Nullable private String mErrorMessage;
152         @NonNull private String mAdId;
153         private boolean mLimitAdTrackingEnabled;
154 
Builder()155         public Builder() {}
156 
157         /** Set the Result Code. */
setStatusCode(@dServicesStatusUtils.StatusCode int statusCode)158         public @NonNull Builder setStatusCode(@AdServicesStatusUtils.StatusCode int statusCode) {
159             mStatusCode = statusCode;
160             return this;
161         }
162 
163         /** Set the Error Message. */
setErrorMessage(@ullable String errorMessage)164         public @NonNull Builder setErrorMessage(@Nullable String errorMessage) {
165             mErrorMessage = errorMessage;
166             return this;
167         }
168 
169         /** Set the adid. */
setAdId(@onNull String adId)170         public @NonNull Builder setAdId(@NonNull String adId) {
171             mAdId = adId;
172             return this;
173         }
174 
175         /** Set the Limited AdTracking enabled field. */
setLatEnabled(boolean isLimitAdTrackingEnabled)176         public @NonNull Builder setLatEnabled(boolean isLimitAdTrackingEnabled) {
177             mLimitAdTrackingEnabled = isLimitAdTrackingEnabled;
178             return this;
179         }
180 
181         /** Builds a {@link GetAdIdResult} instance. */
build()182         public @NonNull GetAdIdResult build() {
183             if (mAdId == null) {
184                 throw new IllegalArgumentException("adId is null");
185             }
186 
187             return new GetAdIdResult(mStatusCode, mErrorMessage, mAdId, mLimitAdTrackingEnabled);
188         }
189     }
190 }
191