1 /* 2 * Copyright (C) 2022 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 android.adservices.measurement; 17 18 import android.annotation.IntDef; 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.net.Uri; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 import android.view.InputEvent; 25 26 import java.lang.annotation.Retention; 27 import java.lang.annotation.RetentionPolicy; 28 import java.util.Objects; 29 30 31 /** 32 * Class to hold input to measurement registration calls. 33 * @hide 34 */ 35 public final class RegistrationRequest implements Parcelable { 36 /** @hide */ 37 @Retention(RetentionPolicy.SOURCE) 38 @IntDef({ 39 INVALID, 40 REGISTER_SOURCE, 41 REGISTER_TRIGGER, 42 }) 43 public @interface RegistrationType {} 44 /** Invalid registration type used as a default. */ 45 public static final int INVALID = 0; 46 /** 47 * A request to register an Attribution Source event (NOTE: AdServices type not 48 * android.context.AttributionSource). 49 */ 50 public static final int REGISTER_SOURCE = 1; 51 /** A request to register a trigger event. */ 52 public static final int REGISTER_TRIGGER = 2; 53 54 @RegistrationType private final int mRegistrationType; 55 private final Uri mRegistrationUri; 56 private final InputEvent mInputEvent; 57 private final String mAppPackageName; 58 private final String mSdkPackageName; 59 private final long mRequestTime; 60 private final boolean mIsAdIdPermissionGranted; 61 private final String mAdIdValue; 62 RegistrationRequest(@onNull Builder builder)63 private RegistrationRequest(@NonNull Builder builder) { 64 mRegistrationType = builder.mRegistrationType; 65 mRegistrationUri = builder.mRegistrationUri; 66 mInputEvent = builder.mInputEvent; 67 mAppPackageName = builder.mAppPackageName; 68 mSdkPackageName = builder.mSdkPackageName; 69 mRequestTime = builder.mRequestTime; 70 mIsAdIdPermissionGranted = builder.mIsAdIdPermissionGranted; 71 mAdIdValue = builder.mAdIdValue; 72 } 73 74 /** 75 * Unpack an RegistrationRequest from a Parcel. 76 */ RegistrationRequest(Parcel in)77 private RegistrationRequest(Parcel in) { 78 mRegistrationType = in.readInt(); 79 mRegistrationUri = Uri.CREATOR.createFromParcel(in); 80 mAppPackageName = in.readString(); 81 mSdkPackageName = in.readString(); 82 boolean hasInputEvent = in.readBoolean(); 83 if (hasInputEvent) { 84 mInputEvent = InputEvent.CREATOR.createFromParcel(in); 85 } else { 86 mInputEvent = null; 87 } 88 mRequestTime = in.readLong(); 89 mIsAdIdPermissionGranted = in.readBoolean(); 90 boolean hasAdIdValue = in.readBoolean(); 91 if (hasAdIdValue) { 92 mAdIdValue = in.readString(); 93 } else { 94 mAdIdValue = null; 95 } 96 } 97 98 /** Creator for Parcelable (via reflection). */ 99 @NonNull 100 public static final Parcelable.Creator<RegistrationRequest> CREATOR = 101 new Parcelable.Creator<RegistrationRequest>() { 102 @Override 103 public RegistrationRequest createFromParcel(Parcel in) { 104 return new RegistrationRequest(in); 105 } 106 107 @Override 108 public RegistrationRequest[] newArray(int size) { 109 return new RegistrationRequest[size]; 110 } 111 }; 112 113 /** 114 * For Parcelable, no special marshalled objects. 115 */ describeContents()116 public int describeContents() { 117 return 0; 118 } 119 120 /** 121 * For Parcelable, write out to a Parcel in particular order. 122 */ writeToParcel(@onNull Parcel out, int flags)123 public void writeToParcel(@NonNull Parcel out, int flags) { 124 Objects.requireNonNull(out); 125 out.writeInt(mRegistrationType); 126 mRegistrationUri.writeToParcel(out, flags); 127 out.writeString(mAppPackageName); 128 out.writeString(mSdkPackageName); 129 if (mInputEvent != null) { 130 out.writeBoolean(true); 131 mInputEvent.writeToParcel(out, flags); 132 } else { 133 out.writeBoolean(false); 134 } 135 out.writeLong(mRequestTime); 136 out.writeBoolean(mIsAdIdPermissionGranted); 137 if (mAdIdValue != null) { 138 out.writeBoolean(true); 139 out.writeString(mAdIdValue); 140 } else { 141 out.writeBoolean(false); 142 } 143 } 144 145 /** Type of the registration. */ 146 @RegistrationType getRegistrationType()147 public int getRegistrationType() { 148 return mRegistrationType; 149 } 150 151 /** Source URI of the App / Publisher. */ 152 @NonNull getRegistrationUri()153 public Uri getRegistrationUri() { 154 return mRegistrationUri; 155 } 156 157 /** InputEvent related to an ad event. */ 158 @Nullable getInputEvent()159 public InputEvent getInputEvent() { 160 return mInputEvent; 161 } 162 163 /** Package name of the app used for the registration. */ 164 @NonNull getAppPackageName()165 public String getAppPackageName() { 166 return mAppPackageName; 167 } 168 169 /** Package name of the sdk used for the registration. */ 170 @NonNull getSdkPackageName()171 public String getSdkPackageName() { 172 return mSdkPackageName; 173 } 174 175 /** Time the request was created, as millis since boot excluding time in deep sleep. */ 176 @NonNull getRequestTime()177 public long getRequestTime() { 178 return mRequestTime; 179 } 180 181 /** Ad ID Permission */ 182 @NonNull isAdIdPermissionGranted()183 public boolean isAdIdPermissionGranted() { 184 return mIsAdIdPermissionGranted; 185 } 186 187 /** Ad ID Value */ 188 @Nullable getAdIdValue()189 public String getAdIdValue() { 190 return mAdIdValue; 191 } 192 193 /** 194 * A builder for {@link RegistrationRequest}. 195 */ 196 public static final class Builder { 197 @RegistrationType private final int mRegistrationType; 198 private final Uri mRegistrationUri; 199 private final String mAppPackageName; 200 private final String mSdkPackageName; 201 private InputEvent mInputEvent; 202 private long mRequestTime; 203 private boolean mIsAdIdPermissionGranted; 204 private String mAdIdValue; 205 206 /** 207 * Builder constructor for {@link RegistrationRequest}. 208 * 209 * @param type registration type, either source or trigger 210 * @param registrationUri registration uri endpoint for registering a source/trigger 211 * @param appPackageName app package name that is calling PP API 212 * @param sdkPackageName sdk package name that is calling PP API 213 */ Builder( @egistrationType int type, @NonNull Uri registrationUri, @NonNull String appPackageName, @NonNull String sdkPackageName)214 public Builder( 215 @RegistrationType int type, 216 @NonNull Uri registrationUri, 217 @NonNull String appPackageName, 218 @NonNull String sdkPackageName) { 219 if (type != REGISTER_SOURCE && type != REGISTER_TRIGGER) { 220 throw new IllegalArgumentException("Invalid registrationType"); 221 } 222 223 Objects.requireNonNull(registrationUri); 224 Objects.requireNonNull(appPackageName); 225 Objects.requireNonNull(sdkPackageName); 226 mRegistrationType = type; 227 mRegistrationUri = registrationUri; 228 mAppPackageName = appPackageName; 229 mSdkPackageName = sdkPackageName; 230 } 231 232 /** See {@link RegistrationRequest#getInputEvent}. */ 233 @NonNull setInputEvent(@ullable InputEvent event)234 public Builder setInputEvent(@Nullable InputEvent event) { 235 mInputEvent = event; 236 return this; 237 } 238 239 /** See {@link RegistrationRequest#getRequestTime}. */ 240 @NonNull setRequestTime(long requestTime)241 public Builder setRequestTime(long requestTime) { 242 mRequestTime = requestTime; 243 return this; 244 } 245 246 /** See {@link RegistrationRequest#isAdIdPermissionGranted()}. */ 247 @NonNull setAdIdPermissionGranted(boolean adIdPermissionGranted)248 public Builder setAdIdPermissionGranted(boolean adIdPermissionGranted) { 249 mIsAdIdPermissionGranted = adIdPermissionGranted; 250 return this; 251 } 252 253 /** See {@link RegistrationRequest#getAdIdValue()}. */ 254 @NonNull setAdIdValue(@ullable String adIdValue)255 public Builder setAdIdValue(@Nullable String adIdValue) { 256 mAdIdValue = adIdValue; 257 return this; 258 } 259 260 /** Build the RegistrationRequest. */ 261 @NonNull build()262 public RegistrationRequest build() { 263 // Ensure registrationType has been set, 264 // throws IllegalArgumentException if mRegistrationType 265 // isn't a valid choice. 266 if (mRegistrationType != REGISTER_SOURCE && mRegistrationType != REGISTER_TRIGGER) { 267 throw new IllegalArgumentException("Invalid registrationType"); 268 } 269 270 return new RegistrationRequest(this); 271 } 272 } 273 } 274