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 package androidx.privacysandbox.ads.adservices.adselection
17 
18 import android.os.Build
19 import android.os.ext.SdkExtensions
20 import androidx.annotation.RequiresExtension
21 import androidx.annotation.RestrictTo
22 import androidx.privacysandbox.ads.adservices.common.AdTechIdentifier
23 import androidx.privacysandbox.ads.adservices.common.ExperimentalFeatures
24 import androidx.privacysandbox.ads.adservices.common.FrequencyCapFilters
25 
26 /**
27  * This class represents input to the [AdSelectionManager#updateAdCounterHistogram] in the
28  * [AdSelectionManager].
29  *
30  * Note that the [FrequencyCapFilters.AD_EVENT_TYPE_WIN] event type cannot be updated manually using
31  * the [AdSelectionManager#updateAdCounterHistogram] API.
32  *
33  * @param adSelectionId An ID unique only to a device user that identifies a successful ad
34  *   selection.
35  * @param adEventType A render URL for the winning ad.
36  * @param callerAdTech The caller adtech entity's [AdTechIdentifier].
37  */
38 @ExperimentalFeatures.Ext8OptIn
39 class UpdateAdCounterHistogramRequest
40 public constructor(
41     val adSelectionId: Long,
42     @FrequencyCapFilters.AdEventType val adEventType: Int,
43     val callerAdTech: AdTechIdentifier
44 ) {
45     init {
<lambda>null46         require(adEventType != FrequencyCapFilters.AD_EVENT_TYPE_WIN) {
47             "Win event types cannot be manually updated."
48         }
49         require(
50             adEventType == FrequencyCapFilters.AD_EVENT_TYPE_IMPRESSION ||
51                 adEventType == FrequencyCapFilters.AD_EVENT_TYPE_VIEW ||
52                 adEventType == FrequencyCapFilters.AD_EVENT_TYPE_CLICK
<lambda>null53         ) {
54             "Ad event type must be one of AD_EVENT_TYPE_IMPRESSION, AD_EVENT_TYPE_VIEW, or" +
55                 " AD_EVENT_TYPE_CLICK"
56         }
57     }
58 
59     /**
60      * Checks whether two [UpdateAdCounterHistogramRequest] objects contain the same information.
61      */
equalsnull62     override fun equals(other: Any?): Boolean {
63         if (this === other) return true
64         if (other !is UpdateAdCounterHistogramRequest) return false
65         return this.adSelectionId == other.adSelectionId &&
66             this.adEventType == other.adEventType &&
67             this.callerAdTech == other.callerAdTech
68     }
69 
70     /** Returns the hash of the [UpdateAdCounterHistogramRequest] object's data. */
hashCodenull71     override fun hashCode(): Int {
72         var hash = adSelectionId.hashCode()
73         hash = 31 * hash + adEventType.hashCode()
74         hash = 31 * hash + callerAdTech.hashCode()
75         return hash
76     }
77 
78     /** Overrides the toString method. */
toStringnull79     override fun toString(): String {
80         val adEventTypeStr =
81             when (adEventType) {
82                 FrequencyCapFilters.AD_EVENT_TYPE_IMPRESSION -> "AD_EVENT_TYPE_IMPRESSION"
83                 FrequencyCapFilters.AD_EVENT_TYPE_VIEW -> "AD_EVENT_TYPE_VIEW"
84                 FrequencyCapFilters.AD_EVENT_TYPE_WIN -> "AD_EVENT_TYPE_WIN"
85                 FrequencyCapFilters.AD_EVENT_TYPE_CLICK -> "AD_EVENT_TYPE_CLICK"
86                 else -> "Invalid ad event type"
87             }
88         return "UpdateAdCounterHistogramRequest: adSelectionId=$adSelectionId, " +
89             "adEventType=$adEventTypeStr, callerAdTech=$callerAdTech"
90     }
91 
92     @RestrictTo(RestrictTo.Scope.LIBRARY)
93     @RequiresExtension(extension = SdkExtensions.AD_SERVICES, version = 8)
94     @RequiresExtension(extension = Build.VERSION_CODES.S, version = 9)
convertToAdServicesnull95     internal fun convertToAdServices():
96         android.adservices.adselection.UpdateAdCounterHistogramRequest {
97         return android.adservices.adselection.UpdateAdCounterHistogramRequest.Builder(
98                 adSelectionId,
99                 adEventType,
100                 callerAdTech.convertToAdServices()
101             )
102             .build()
103     }
104 }
105