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.appsetid;
18 
19 import android.adservices.common.AdServicesResponse;
20 import android.adservices.common.AdServicesStatusUtils;
21 import android.annotation.IntDef;
22 import android.annotation.NonNull;
23 import android.annotation.Nullable;
24 import android.os.Parcel;
25 import android.os.Parcelable;
26 
27 import java.lang.annotation.Retention;
28 import java.lang.annotation.RetentionPolicy;
29 import java.util.Objects;
30 
31 /**
32  * Represent the result from the getAppSetId API.
33  *
34  * @hide
35  */
36 public final class GetAppSetIdResult extends AdServicesResponse {
37     @NonNull private final String mAppSetId;
38 
39     /** @hide */
40     @Retention(RetentionPolicy.SOURCE)
41     @IntDef({
42         SCOPE_APP,
43         SCOPE_DEVELOPER,
44     })
45     public @interface AppSetIdScope {}
46     /** The appSetId is scoped to an app. All apps on a device will have a different appSetId. */
47     public static final int SCOPE_APP = 1;
48 
49     /**
50      * The appSetId is scoped to a developer account on an app store. All apps from the same
51      * developer on a device will have the same developer scoped appSetId.
52      */
53     public static final int SCOPE_DEVELOPER = 2;
54 
55     private final @AppSetIdScope int mAppSetIdScope;
56 
GetAppSetIdResult( @dServicesStatusUtils.StatusCode int resultCode, @Nullable String errorMessage, @NonNull String appSetId, @AppSetIdScope int appSetIdScope)57     private GetAppSetIdResult(
58             @AdServicesStatusUtils.StatusCode int resultCode,
59             @Nullable String errorMessage,
60             @NonNull String appSetId,
61             @AppSetIdScope int appSetIdScope) {
62         super(resultCode, errorMessage);
63         mAppSetId = appSetId;
64         mAppSetIdScope = appSetIdScope;
65     }
66 
GetAppSetIdResult(@onNull Parcel in)67     private GetAppSetIdResult(@NonNull Parcel in) {
68         super(in);
69         Objects.requireNonNull(in);
70 
71         mAppSetId = in.readString();
72         mAppSetIdScope = in.readInt();
73     }
74 
75     public static final @NonNull Creator<GetAppSetIdResult> CREATOR =
76             new Parcelable.Creator<GetAppSetIdResult>() {
77                 @Override
78                 public GetAppSetIdResult createFromParcel(@NonNull Parcel in) {
79                     Objects.requireNonNull(in);
80                     return new GetAppSetIdResult(in);
81                 }
82 
83                 @Override
84                 public GetAppSetIdResult[] newArray(int size) {
85                     return new GetAppSetIdResult[size];
86                 }
87             };
88 
89     /** @hide */
90     @Override
describeContents()91     public int describeContents() {
92         return 0;
93     }
94 
95     /** @hide */
96     @Override
writeToParcel(@onNull Parcel out, int flags)97     public void writeToParcel(@NonNull Parcel out, int flags) {
98         out.writeInt(mStatusCode);
99         out.writeString(mErrorMessage);
100         out.writeString(mAppSetId);
101         out.writeInt(mAppSetIdScope);
102     }
103 
104     /**
105      * Returns the error message associated with this result.
106      *
107      * <p>If {@link #isSuccess} is {@code true}, the error message is always {@code null}. The error
108      * message may be {@code null} even if {@link #isSuccess} is {@code false}.
109      */
110     @Nullable
getErrorMessage()111     public String getErrorMessage() {
112         return mErrorMessage;
113     }
114 
115     /** Returns the AppSetId associated with this result. */
116     @NonNull
getAppSetId()117     public String getAppSetId() {
118         return mAppSetId;
119     }
120 
121     /** Returns the AppSetId scope associated with this result. */
getAppSetIdScope()122     public @AppSetIdScope int getAppSetIdScope() {
123         return mAppSetIdScope;
124     }
125 
126     @Override
toString()127     public String toString() {
128         return "GetAppSetIdResult{"
129                 + "mResultCode="
130                 + mStatusCode
131                 + ", mErrorMessage='"
132                 + mErrorMessage
133                 + '\''
134                 + ", mAppSetId="
135                 + mAppSetId
136                 + ", mAppSetIdScope="
137                 + mAppSetIdScope
138                 + '}';
139     }
140 
141     @Override
equals(Object o)142     public boolean equals(Object o) {
143         if (this == o) {
144             return true;
145         }
146 
147         if (!(o instanceof GetAppSetIdResult)) {
148             return false;
149         }
150 
151         GetAppSetIdResult that = (GetAppSetIdResult) o;
152 
153         return mStatusCode == that.mStatusCode
154                 && Objects.equals(mErrorMessage, that.mErrorMessage)
155                 && Objects.equals(mAppSetId, that.mAppSetId)
156                 && (mAppSetIdScope == that.mAppSetIdScope);
157     }
158 
159     @Override
hashCode()160     public int hashCode() {
161         return Objects.hash(mStatusCode, mErrorMessage, mAppSetId, mAppSetIdScope);
162     }
163 
164     /**
165      * Builder for {@link GetAppSetIdResult} objects.
166      *
167      * @hide
168      */
169     public static final class Builder {
170         private @AdServicesStatusUtils.StatusCode int mStatusCode;
171         @Nullable private String mErrorMessage;
172         @NonNull private String mAppSetId;
173         private @AppSetIdScope int mAppSetIdScope;
174 
Builder()175         public Builder() {}
176 
177         /** Set the Result Code. */
setStatusCode(@dServicesStatusUtils.StatusCode int statusCode)178         public @NonNull Builder setStatusCode(@AdServicesStatusUtils.StatusCode int statusCode) {
179             mStatusCode = statusCode;
180             return this;
181         }
182 
183         /** Set the Error Message. */
setErrorMessage(@ullable String errorMessage)184         public @NonNull Builder setErrorMessage(@Nullable String errorMessage) {
185             mErrorMessage = errorMessage;
186             return this;
187         }
188 
189         /** Set the appSetId. */
setAppSetId(@onNull String appSetId)190         public @NonNull Builder setAppSetId(@NonNull String appSetId) {
191             mAppSetId = appSetId;
192             return this;
193         }
194 
195         /** Set the appSetId scope field. */
setAppSetIdScope(@ppSetIdScope int scope)196         public @NonNull Builder setAppSetIdScope(@AppSetIdScope int scope) {
197             mAppSetIdScope = scope;
198             return this;
199         }
200 
201         /** Builds a {@link GetAppSetIdResult} instance. */
build()202         public @NonNull GetAppSetIdResult build() {
203             if (mAppSetId == null) {
204                 throw new IllegalArgumentException("appSetId is null");
205             }
206 
207             return new GetAppSetIdResult(mStatusCode, mErrorMessage, mAppSetId, mAppSetIdScope);
208         }
209     }
210 }
211