summaryrefslogtreecommitdiff
path: root/adservices/framework/java/android/adservices/measurement/RegistrationRequest.java
blob: 8f71375cebc9b1268213024ccef9f0f1659c4bf2 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * 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 android.adservices.measurement;

import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.view.InputEvent;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Objects;


/**
 * Class to hold input to measurement registration calls.
 * @hide
 */
public final class RegistrationRequest implements Parcelable {
    /** @hide */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef({
       INVALID,
       REGISTER_SOURCE,
       REGISTER_TRIGGER,
    })
    public @interface RegistrationType {}
    /** Invalid registration type used as a default. */
    public static final int INVALID = 0;
    /** A request to register an Attribution Source event
     * (NOTE: adservices type not android.context.AttributionSource). */
    public static final int REGISTER_SOURCE = 1;
    /** A request to register a trigger event. */
    public static final int REGISTER_TRIGGER = 2;

    private final @RegistrationType int mRegistrationType;
    private final Uri mRegistrationUri;
    private final Uri mTopOriginUri;
    private final InputEvent mInputEvent;
    private final String mPackageName;
    private final long mRequestTime;
    private final boolean mIsAdIdPermissionGranted;

    private RegistrationRequest(@NonNull Builder builder) {
        mRegistrationType = builder.mRegistrationType;
        mRegistrationUri = builder.mRegistrationUri;
        mTopOriginUri = builder.mTopOriginUri;
        mInputEvent = builder.mInputEvent;
        mPackageName = builder.mPackageName;
        mRequestTime = builder.mRequestTime;
        mIsAdIdPermissionGranted = builder.mIsAdIdPermissionGranted;
    }

    /**
     * Unpack an RegistrationRequest from a Parcel.
     */
    private RegistrationRequest(Parcel in) {
        mRegistrationType = in.readInt();
        mRegistrationUri = Uri.CREATOR.createFromParcel(in);
        mTopOriginUri = Uri.CREATOR.createFromParcel(in);
        mPackageName = in.readString();
        boolean hasInputEvent = in.readBoolean();
        if (hasInputEvent) {
            mInputEvent = InputEvent.CREATOR.createFromParcel(in);
        } else {
            mInputEvent = null;
        }
        mRequestTime = in.readLong();
        mIsAdIdPermissionGranted = in.readBoolean();
    }

    /**
     * Creator for Paracelable (via reflection).
     */
    public static final @NonNull Parcelable.Creator<RegistrationRequest> CREATOR =
            new Parcelable.Creator<RegistrationRequest>() {
                @Override public RegistrationRequest createFromParcel(Parcel in) {
                    return new RegistrationRequest(in);
                }

                @Override public RegistrationRequest[] newArray(int size) {
                    return new RegistrationRequest[size];
                }
            };

    /**
     * For Parcelable, no special marshalled objects.
     */
    public int describeContents() {
        return 0;
    }

    /**
     * For Parcelable, write out to a Parcel in particular order.
     */
    public void writeToParcel(@NonNull Parcel out, int flags) {
        Objects.requireNonNull(out);
        out.writeInt(mRegistrationType);
        mRegistrationUri.writeToParcel(out, flags);
        mTopOriginUri.writeToParcel(out, flags);
        out.writeString(mPackageName);
        if (mInputEvent != null) {
            out.writeBoolean(true);
            mInputEvent.writeToParcel(out, flags);
        } else {
            out.writeBoolean(false);
        }
        out.writeLong(mRequestTime);
        out.writeBoolean(mIsAdIdPermissionGranted);
    }

    /**
     * Type of the registration.
     */
    public @RegistrationType int getRegistrationType() {
        return mRegistrationType;
    }

    /**
     * Top level origin of the App / Publisher.
     */
    public @NonNull Uri getTopOriginUri() {
        return mTopOriginUri;
    }

    /**
     * Source URI of the App / Publisher.
     */
    public @NonNull Uri getRegistrationUri() {
        return mRegistrationUri;
    }

    /**
     * InputEvent related to ad event.
     */
    public @Nullable InputEvent getInputEvent() {
        return mInputEvent;
    }

    /** Client's package name used for the registration. */
    public @NonNull String getPackageName() {
        return mPackageName;
    }

    /** Time the request was created, as millis since boot excluding time in deep sleep. */
    public @NonNull long getRequestTime() {
        return mRequestTime;
    }
    /** Ad ID Permission */
    public @NonNull boolean isAdIdPermissionGranted() {
        return mIsAdIdPermissionGranted;
    }

    /**
     * A builder for {@link RegistrationRequest}.
     */
    public static final class Builder {
        private @RegistrationType int mRegistrationType;
        private Uri mRegistrationUri;
        private Uri mTopOriginUri;
        private InputEvent mInputEvent;
        private String mPackageName;
        private long mRequestTime;
        private boolean mIsAdIdPermissionGranted;

        public Builder() {
            mRegistrationType = INVALID;
            mRegistrationUri = Uri.EMPTY;
        }

        /**
         * See {@link RegistrationRequest#getRegistrationType}.
         */
        public @NonNull Builder setRegistrationType(
                @RegistrationType int type) {
            if (type != REGISTER_SOURCE
                    && type != REGISTER_TRIGGER) {
                throw new IllegalArgumentException("Invalid registrationType");
            }
            mRegistrationType = type;
            return this;
        }

        /**
         * See {@link RegistrationRequest#getTopOriginUri}.
         */
        public @NonNull Builder setTopOriginUri(@NonNull Uri origin) {
            Objects.requireNonNull(origin);
            mTopOriginUri = origin;
            return this;
        }

        /**
         * See {@link RegistrationRequest#getRegistrationUri}.
         */
        public @NonNull Builder setRegistrationUri(@NonNull Uri uri) {
            Objects.requireNonNull(uri);
            mRegistrationUri = uri;
            return this;
        }

        /**
         * See {@link RegistrationRequest#getInputEvent}.
         */
        public @NonNull Builder setInputEvent(@Nullable InputEvent event) {
            mInputEvent = event;
            return this;
        }

        /** See {@link RegistrationRequest#getPackageName()}. */
        public @NonNull Builder setPackageName(@NonNull String packageName) {
            Objects.requireNonNull(packageName);
            mPackageName = packageName;
            return this;
        }

        /** See {@link RegistrationRequest#getRequestTime}. */
        public @NonNull Builder setRequestTime(long requestTime) {
            mRequestTime = requestTime;
            return this;
        }

        /** See {@link RegistrationRequest#isAdIdPermissionGranted()}. */
        public @NonNull Builder setAdIdPermissionGranted(boolean adIdPermissionGranted) {
            mIsAdIdPermissionGranted = adIdPermissionGranted;
            return this;
        }

        /**
         * Build the RegistrationRequest.
         */
        public @NonNull RegistrationRequest build() {
            // Check parameters that start in a non-null state don't
            // somehow get changed to an invalid one (this should
            // not be possible), if it happens throw IllegalStateException.
            if (mRegistrationUri == null) {
                throw new IllegalStateException("Unexpected null value");
            }
            // Ensure registrationType has been set,
            // throws IllegalArgumentException if mRegistrationType
            // isn't a valid choice.
            if (mRegistrationType != REGISTER_SOURCE
                    && mRegistrationType != REGISTER_TRIGGER) {
                throw new IllegalArgumentException("Invalid registrationType");
            }
            // Ensure the packageName has been set.
            // throws IllegalArgumentException if the packageName is null.
            if (mPackageName == null) {
                throw new IllegalArgumentException("packageName unset");
            }

            // Check if topOrigin has been set.
            // However, if it's not set, caller package is defaulted
            if (mTopOriginUri == null) {
                mTopOriginUri = Uri.parse("android-app://" + mPackageName);
            }

            return new RegistrationRequest(this);
        }
    }
}