• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.adselection;
18 
19 import static android.adservices.adselection.AdSelectionOutcome.UNSET_AD_SELECTION_ID;
20 import static android.adservices.adselection.AdSelectionOutcome.UNSET_AD_SELECTION_ID_MESSAGE;
21 
22 import android.annotation.Nullable;
23 import android.content.res.AssetFileDescriptor;
24 import android.os.Parcel;
25 import android.os.Parcelable;
26 
27 import androidx.annotation.NonNull;
28 
29 import com.android.adservices.AdServicesParcelableUtil;
30 import com.android.internal.util.Preconditions;
31 
32 import java.util.Arrays;
33 import java.util.Objects;
34 
35 /**
36  * Represents ad selection data collected from device for ad selection.
37  *
38  * @hide
39  */
40 public final class GetAdSelectionDataResponse implements Parcelable {
41     private final long mAdSelectionId;
42     @Nullable private final byte[] mAdSelectionData;
43     @Nullable private final AssetFileDescriptor mAssetFileDescriptor;
44 
45     public static final Creator<GetAdSelectionDataResponse> CREATOR =
46             new Creator<>() {
47                 @Override
48                 public GetAdSelectionDataResponse createFromParcel(Parcel in) {
49                     Objects.requireNonNull(in);
50 
51                     return new GetAdSelectionDataResponse(in);
52                 }
53 
54                 @Override
55                 public GetAdSelectionDataResponse[] newArray(int size) {
56                     return new GetAdSelectionDataResponse[size];
57                 }
58             };
59 
GetAdSelectionDataResponse( long adSelectionId, byte[] adSelectionData, AssetFileDescriptor assetFileDescriptor)60     private GetAdSelectionDataResponse(
61             long adSelectionId, byte[] adSelectionData, AssetFileDescriptor assetFileDescriptor) {
62         this.mAdSelectionId = adSelectionId;
63         this.mAdSelectionData = adSelectionData;
64         this.mAssetFileDescriptor = assetFileDescriptor;
65     }
66 
GetAdSelectionDataResponse(@onNull Parcel in)67     private GetAdSelectionDataResponse(@NonNull Parcel in) {
68         Objects.requireNonNull(in);
69 
70         this.mAdSelectionId = in.readLong();
71         this.mAdSelectionData = in.createByteArray();
72         this.mAssetFileDescriptor =
73                 AdServicesParcelableUtil.readNullableFromParcel(
74                         in, AssetFileDescriptor.CREATOR::createFromParcel);
75     }
76 
77     @Override
equals(Object o)78     public boolean equals(Object o) {
79         if (o instanceof GetAdSelectionDataResponse) {
80             GetAdSelectionDataResponse response = (GetAdSelectionDataResponse) o;
81             return mAdSelectionId == response.mAdSelectionId
82                     && Arrays.equals(mAdSelectionData, response.mAdSelectionData)
83                     && Objects.equals(mAssetFileDescriptor, response.mAssetFileDescriptor);
84         }
85         return false;
86     }
87 
88     @Override
hashCode()89     public int hashCode() {
90         return Objects.hash(
91                 mAdSelectionId, Arrays.hashCode(mAdSelectionData), mAssetFileDescriptor);
92     }
93 
94     @Override
describeContents()95     public int describeContents() {
96         return 0;
97     }
98 
99     /** Returns the adSelectionId that identifies the AdSelection. */
getAdSelectionId()100     public long getAdSelectionId() {
101         return mAdSelectionId;
102     }
103 
104     /** Returns the adSelectionData that is collected from device. */
105     @Nullable
getAdSelectionData()106     public byte[] getAdSelectionData() {
107         if (Objects.isNull(mAdSelectionData)) {
108             return null;
109         } else {
110             return Arrays.copyOf(mAdSelectionData, mAdSelectionData.length);
111         }
112     }
113 
114     /**
115      * Returns the {@link AssetFileDescriptor} that points to a piece of memory where the
116      * adSelectionData is stored
117      */
118     @Nullable
getAssetFileDescriptor()119     public AssetFileDescriptor getAssetFileDescriptor() {
120         return mAssetFileDescriptor;
121     }
122 
123     @Override
writeToParcel(Parcel dest, int flags)124     public void writeToParcel(Parcel dest, int flags) {
125         Objects.requireNonNull(dest);
126 
127         dest.writeLong(mAdSelectionId);
128         dest.writeByteArray(mAdSelectionData);
129         AdServicesParcelableUtil.writeNullableToParcel(
130                 dest,
131                 mAssetFileDescriptor,
132                 (targetParcel, sourceSignals) -> sourceSignals.writeToParcel(targetParcel, flags));
133     }
134 
135     /**
136      * Builder for {@link GetAdSelectionDataResponse} objects.
137      *
138      * @hide
139      */
140     public static final class Builder {
141         private long mAdSelectionId;
142         @Nullable private byte[] mAdSelectionData;
143         @Nullable private AssetFileDescriptor mAssetFileDescriptor;
144 
Builder()145         public Builder() {}
146 
147         /** Sets the adSelectionId. */
148         @NonNull
setAdSelectionId(long adSelectionId)149         public GetAdSelectionDataResponse.Builder setAdSelectionId(long adSelectionId) {
150             this.mAdSelectionId = adSelectionId;
151             return this;
152         }
153 
154         /** Sets the adSelectionData. */
155         @NonNull
setAdSelectionData( @ullable byte[] adSelectionData)156         public GetAdSelectionDataResponse.Builder setAdSelectionData(
157                 @Nullable byte[] adSelectionData) {
158             if (!Objects.isNull(adSelectionData)) {
159                 this.mAdSelectionData = Arrays.copyOf(adSelectionData, adSelectionData.length);
160             } else {
161                 this.mAdSelectionData = null;
162             }
163             return this;
164         }
165 
166         /** Sets the assetFileDescriptor */
167         @NonNull
setAssetFileDescriptor( @ullable AssetFileDescriptor assetFileDescriptor)168         public GetAdSelectionDataResponse.Builder setAssetFileDescriptor(
169                 @Nullable AssetFileDescriptor assetFileDescriptor) {
170             this.mAssetFileDescriptor = assetFileDescriptor;
171             return this;
172         }
173 
174         /**
175          * Builds a {@link GetAdSelectionDataResponse} instance.
176          *
177          * @throws IllegalArgumentException if the adSelectionId is not set
178          */
179         @NonNull
build()180         public GetAdSelectionDataResponse build() {
181             Preconditions.checkArgument(
182                     mAdSelectionId != UNSET_AD_SELECTION_ID, UNSET_AD_SELECTION_ID_MESSAGE);
183 
184             return new GetAdSelectionDataResponse(
185                     mAdSelectionId, mAdSelectionData, mAssetFileDescriptor);
186         }
187     }
188 }
189