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 android.adservices.common.AdTechIdentifier; 20 import android.annotation.NonNull; 21 import android.annotation.Nullable; 22 import android.net.Uri; 23 import android.os.Parcel; 24 import android.os.Parcelable; 25 26 import java.util.List; 27 import java.util.Objects; 28 29 /** 30 * Contains Ads supplied by Seller for the Contextual Path 31 * 32 * <p>Instances of this class are created by SDKs to be injected as part of {@link 33 * AdSelectionConfig} and passed to {@link AdSelectionManager#selectAds} 34 * 35 * @hide 36 */ 37 public final class ContextualAds implements Parcelable { 38 @NonNull private final AdTechIdentifier mBuyer; 39 @NonNull private final Uri mDecisionLogicUri; 40 @NonNull private final List<AdWithBid> mAdsWithBid; 41 42 @NonNull 43 public static final Creator<ContextualAds> CREATOR = 44 new Creator<ContextualAds>() { 45 @Override 46 public ContextualAds createFromParcel(@NonNull Parcel in) { 47 Objects.requireNonNull(in); 48 return new ContextualAds(in); 49 } 50 51 @Override 52 public ContextualAds[] newArray(int size) { 53 return new ContextualAds[0]; 54 } 55 }; 56 ContextualAds( @onNull AdTechIdentifier buyer, @NonNull Uri decisionLogicUri, @NonNull List<AdWithBid> adsWithBid)57 private ContextualAds( 58 @NonNull AdTechIdentifier buyer, 59 @NonNull Uri decisionLogicUri, 60 @NonNull List<AdWithBid> adsWithBid) { 61 this.mBuyer = buyer; 62 this.mDecisionLogicUri = decisionLogicUri; 63 this.mAdsWithBid = adsWithBid; 64 } 65 ContextualAds(@onNull Parcel in)66 private ContextualAds(@NonNull Parcel in) { 67 Objects.requireNonNull(in); 68 mBuyer = AdTechIdentifier.CREATOR.createFromParcel(in); 69 mDecisionLogicUri = Uri.CREATOR.createFromParcel(in); 70 mAdsWithBid = in.createTypedArrayList(AdWithBid.CREATOR); 71 } 72 73 @Override describeContents()74 public int describeContents() { 75 return 0; 76 } 77 78 @Override writeToParcel(@onNull Parcel dest, int flags)79 public void writeToParcel(@NonNull Parcel dest, int flags) { 80 Objects.requireNonNull(dest); 81 82 mBuyer.writeToParcel(dest, flags); 83 mDecisionLogicUri.writeToParcel(dest, flags); 84 dest.writeTypedList(mAdsWithBid); 85 } 86 87 @Override equals(Object o)88 public boolean equals(Object o) { 89 if (this == o) return true; 90 if (!(o instanceof ContextualAds)) return false; 91 ContextualAds that = (ContextualAds) o; 92 return Objects.equals(mBuyer, that.mBuyer) 93 && Objects.equals(mDecisionLogicUri, that.mDecisionLogicUri) 94 && Objects.equals(mAdsWithBid, that.mAdsWithBid); 95 } 96 97 @Override hashCode()98 public int hashCode() { 99 return Objects.hash(mBuyer, mDecisionLogicUri, mAdsWithBid); 100 } 101 102 /** @return the Ad tech identifier from which this contextual Ad would have been downloaded */ 103 @NonNull getBuyer()104 public AdTechIdentifier getBuyer() { 105 return mBuyer; 106 } 107 108 /** 109 * @return the URI used for to retrieve the updateBid() and reportWin() function used during the 110 * ad selection and reporting process 111 */ 112 @NonNull getDecisionLogicUri()113 public Uri getDecisionLogicUri() { 114 return mDecisionLogicUri; 115 } 116 117 /** @return the Ad data with bid value associated with this ad */ 118 @NonNull getAdsWithBid()119 public List<AdWithBid> getAdsWithBid() { 120 return mAdsWithBid; 121 } 122 123 /** Builder for {@link ContextualAds} object */ 124 public static final class Builder { 125 @Nullable private AdTechIdentifier mBuyer; 126 @Nullable private Uri mDecisionLogicUri; 127 @Nullable private List<AdWithBid> mAdsWithBid; 128 Builder()129 public Builder() {} 130 131 /** 132 * Sets the buyer Ad tech Identifier 133 * 134 * <p>See {@link #getBuyer()} for more details 135 */ 136 @NonNull setBuyer(@onNull AdTechIdentifier buyer)137 public ContextualAds.Builder setBuyer(@NonNull AdTechIdentifier buyer) { 138 Objects.requireNonNull(buyer); 139 140 this.mBuyer = buyer; 141 return this; 142 } 143 144 /** 145 * Sets the URI to fetch the decision logic used in ad selection and reporting 146 * 147 * <p>See {@link #getDecisionLogicUri()} for more details 148 */ 149 @NonNull setDecisionLogicUri(@onNull Uri decisionLogicUri)150 public ContextualAds.Builder setDecisionLogicUri(@NonNull Uri decisionLogicUri) { 151 Objects.requireNonNull(decisionLogicUri); 152 153 this.mDecisionLogicUri = decisionLogicUri; 154 return this; 155 } 156 157 /** 158 * Sets the Ads with pre-defined bid values 159 * 160 * <p>See {@link #getAdsWithBid()} for more details 161 */ 162 @NonNull setAdsWithBid(@onNull List<AdWithBid> adsWithBid)163 public ContextualAds.Builder setAdsWithBid(@NonNull List<AdWithBid> adsWithBid) { 164 Objects.requireNonNull(adsWithBid); 165 166 this.mAdsWithBid = adsWithBid; 167 return this; 168 } 169 170 /** 171 * Builds a {@link ContextualAds} instance. 172 * 173 * @throws NullPointerException if any required params are null 174 */ 175 @NonNull build()176 public ContextualAds build() { 177 Objects.requireNonNull(mBuyer); 178 Objects.requireNonNull(mDecisionLogicUri); 179 Objects.requireNonNull(mAdsWithBid); 180 return new ContextualAds(mBuyer, mDecisionLogicUri, mAdsWithBid); 181 } 182 } 183 } 184