summaryrefslogtreecommitdiff
path: root/adservices/service-core/java/com/android/adservices/service/measurement/reporting/EventReportPayload.java
blob: aa48c6cc2abfc65c714bb4e2c00bdc03ae6116c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.adservices.service.measurement.reporting;

import android.annotation.NonNull;

import androidx.annotation.Nullable;

import com.android.adservices.service.measurement.util.UnsignedLong;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * EventReportPayload.
 */
public final class EventReportPayload {

    private String mAttributionDestination;
    private UnsignedLong mSourceEventId;
    @NonNull private UnsignedLong mTriggerData;
    private String mReportId;
    private String mSourceType;
    private double mRandomizedTriggerRate;
    @Nullable private UnsignedLong mSourceDebugKey;
    @Nullable private UnsignedLong mTriggerDebugKey;

    private EventReportPayload() {};

    private EventReportPayload(EventReportPayload other) {
        mAttributionDestination = other.mAttributionDestination;
        mSourceEventId = other.mSourceEventId;
        mTriggerData = other.mTriggerData;
        mReportId = other.mReportId;
        mSourceType = other.mSourceType;
        mRandomizedTriggerRate = other.mRandomizedTriggerRate;
        mSourceDebugKey = other.mSourceDebugKey;
        mTriggerDebugKey = other.mTriggerDebugKey;
    }

    /**
     * Generate the JSON serialization of the event report.
     */
    public JSONObject toJson() throws JSONException {
        JSONObject eventPayloadJson = new JSONObject();

        eventPayloadJson.put("attribution_destination", mAttributionDestination);
        eventPayloadJson.put("source_event_id", mSourceEventId.toString());
        eventPayloadJson.put("trigger_data", mTriggerData.toString());
        eventPayloadJson.put("report_id", mReportId);
        eventPayloadJson.put("source_type", mSourceType);
        eventPayloadJson.put("randomized_trigger_rate", mRandomizedTriggerRate);
        if (mSourceDebugKey != null) {
            eventPayloadJson.put("source_debug_key", mSourceDebugKey.toString());
        }
        if (mTriggerDebugKey != null) {
            eventPayloadJson.put("trigger_debug_key", mTriggerDebugKey.toString());
        }

        return eventPayloadJson;
    }

    /**
     * Builder class for EventPayloadGenerator.
     */
    public static final class Builder {
        private EventReportPayload mBuilding;

        public Builder() {
            mBuilding = new EventReportPayload();
        }

        /**
         * The attribution destination set on the source.
         */
        public @NonNull Builder setAttributionDestination(@NonNull String attributionDestination) {
            mBuilding.mAttributionDestination = attributionDestination;
            return this;
        }

        /**
         * 64-bit event id set on the attribution source.
         */
        public @NonNull Builder setSourceEventId(@NonNull UnsignedLong sourceEventId) {
            mBuilding.mSourceEventId = sourceEventId;
            return this;
        }

        /**
         * Course data set in the attribution trigger registration.
         */
        public @NonNull Builder setTriggerData(@NonNull UnsignedLong triggerData) {
            mBuilding.mTriggerData = triggerData;
            return this;
        }

        /**
         * A unique id for this report which can be used to prevent double counting.
         */
        public @NonNull Builder setReportId(@NonNull String reportId) {
            mBuilding.mReportId = reportId;
            return this;
        }

        /**
         * Either "navigation" or "event", indicates whether this source was associated with a
         * navigation.
         */
        public @NonNull Builder setSourceType(@NonNull String sourceType) {
            mBuilding.mSourceType = sourceType;
            return this;
        }

        /**
         * Decimal number between 0 and 1 indicating how often noise is applied.
         */
        public @NonNull Builder setRandomizedTriggerRate(@NonNull double randomizedTriggerRate) {
            mBuilding.mRandomizedTriggerRate = randomizedTriggerRate;
            return this;
        }

        /** Source debug key */
        public @NonNull Builder setSourceDebugKey(@Nullable UnsignedLong sourceDebugKey) {
            mBuilding.mSourceDebugKey = sourceDebugKey;
            return this;
        }

        /** Trigger debug key */
        public @NonNull Builder setTriggerDebugKey(@Nullable UnsignedLong triggerDebugKey) {
            mBuilding.mTriggerDebugKey = triggerDebugKey;
            return this;
        }

        /**
         * Build the EventReportPayload.
         */
        public @NonNull EventReportPayload build() {
            if (mBuilding.mTriggerData == null) {
                mBuilding.mTriggerData = new UnsignedLong(0L);
            }
            return new EventReportPayload(mBuilding);
        }
    }
}