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.measurement;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 
24 import com.android.adservices.AdServicesParcelableUtil;
25 
26 import java.util.Objects;
27 
28 /**
29  * Internal source registration request object to communicate from {@link MeasurementManager} to
30  * {@link IMeasurementService}.
31  *
32  * @hide
33  */
34 public class SourceRegistrationRequestInternal implements Parcelable {
35     /** Holds input to measurement source registration calls. */
36     @NonNull private final SourceRegistrationRequest mSourceRegistrationRequest;
37     /** Caller app package name. */
38     @NonNull private final String mAppPackageName;
39     /** Calling SDK package name. */
40     @NonNull private final String mSdkPackageName;
41     /** Time the request was created, in millis since boot excluding time in deep sleep. */
42     private final long mBootRelativeRequestTime;
43     /** Ad ID value if the permission is granted, null otherwise. */
44     @Nullable private final String mAdIdValue;
45 
SourceRegistrationRequestInternal(@onNull Builder builder)46     private SourceRegistrationRequestInternal(@NonNull Builder builder) {
47         mSourceRegistrationRequest = builder.mRegistrationRequest;
48         mAppPackageName = builder.mAppPackageName;
49         mSdkPackageName = builder.mSdkPackageName;
50         mBootRelativeRequestTime = builder.mBootRelativeRequestTime;
51         mAdIdValue = builder.mAdIdValue;
52     }
53 
SourceRegistrationRequestInternal(@onNull Parcel in)54     private SourceRegistrationRequestInternal(@NonNull Parcel in) {
55         Objects.requireNonNull(in);
56         mSourceRegistrationRequest = SourceRegistrationRequest.CREATOR.createFromParcel(in);
57         mAppPackageName = in.readString();
58         mSdkPackageName = in.readString();
59         mBootRelativeRequestTime = in.readLong();
60         mAdIdValue = AdServicesParcelableUtil.readNullableFromParcel(in, Parcel::readString);
61     }
62 
63     /** Holds input to measurement source registration calls from app context. */
64     @NonNull
getSourceRegistrationRequest()65     public SourceRegistrationRequest getSourceRegistrationRequest() {
66         return mSourceRegistrationRequest;
67     }
68 
69     /** Caller app package name. */
70     @NonNull
getAppPackageName()71     public String getAppPackageName() {
72         return mAppPackageName;
73     }
74 
75     /** Calling SDK package name. */
76     @NonNull
getSdkPackageName()77     public String getSdkPackageName() {
78         return mSdkPackageName;
79     }
80 
81     /** Time the request was created, in millis since boot excluding time in deep sleep. */
82     @NonNull
getBootRelativeRequestTime()83     public long getBootRelativeRequestTime() {
84         return mBootRelativeRequestTime;
85     }
86 
87     /** Ad ID value if the permission is granted, null otherwise. */
88     @Nullable
getAdIdValue()89     public String getAdIdValue() {
90         return mAdIdValue;
91     }
92 
93     @Override
equals(Object o)94     public boolean equals(Object o) {
95         if (this == o) return true;
96         if (!(o instanceof SourceRegistrationRequestInternal)) return false;
97         SourceRegistrationRequestInternal that = (SourceRegistrationRequestInternal) o;
98         return Objects.equals(mSourceRegistrationRequest, that.mSourceRegistrationRequest)
99                 && Objects.equals(mAppPackageName, that.mAppPackageName)
100                 && Objects.equals(mSdkPackageName, that.mSdkPackageName)
101                 && mBootRelativeRequestTime == that.mBootRelativeRequestTime
102                 && Objects.equals(mAdIdValue, that.mAdIdValue);
103     }
104 
105     @Override
hashCode()106     public int hashCode() {
107         return Objects.hash(
108                 mSourceRegistrationRequest,
109                 mAppPackageName,
110                 mSdkPackageName,
111                 mBootRelativeRequestTime,
112                 mAdIdValue);
113     }
114 
115     @Override
describeContents()116     public int describeContents() {
117         return 0;
118     }
119 
120     @Override
writeToParcel(@onNull Parcel out, int flags)121     public void writeToParcel(@NonNull Parcel out, int flags) {
122         Objects.requireNonNull(out);
123         mSourceRegistrationRequest.writeToParcel(out, flags);
124         out.writeString(mAppPackageName);
125         out.writeString(mSdkPackageName);
126         out.writeLong(mBootRelativeRequestTime);
127         AdServicesParcelableUtil.writeNullableToParcel(out, mAdIdValue, Parcel::writeString);
128     }
129 
130     /** Builder for {@link SourceRegistrationRequestInternal}. */
131     public static final class Builder {
132         /** External source registration request from client app SDK. */
133         @NonNull private final SourceRegistrationRequest mRegistrationRequest;
134         /** Package name of the app used for the registration. Used to determine the registrant. */
135         @NonNull private final String mAppPackageName;
136         /** Package name of the sdk used for the registration. */
137         @NonNull private final String mSdkPackageName;
138         /** Time the request was created, in millis since boot excluding time in deep sleep. */
139         private final long mBootRelativeRequestTime;
140         /** AD ID value if the permission was granted. */
141         @Nullable private String mAdIdValue;
142         /**
143          * Builder constructor for {@link SourceRegistrationRequestInternal}.
144          *
145          * @param registrationRequest external source registration request
146          * @param appPackageName app package name that is calling PP API
147          * @param sdkPackageName sdk package name that is calling PP API
148          */
Builder( @onNull SourceRegistrationRequest registrationRequest, @NonNull String appPackageName, @NonNull String sdkPackageName, long bootRelativeRequestTime)149         public Builder(
150                 @NonNull SourceRegistrationRequest registrationRequest,
151                 @NonNull String appPackageName,
152                 @NonNull String sdkPackageName,
153                 long bootRelativeRequestTime) {
154             Objects.requireNonNull(registrationRequest);
155             Objects.requireNonNull(appPackageName);
156             Objects.requireNonNull(sdkPackageName);
157             mRegistrationRequest = registrationRequest;
158             mAppPackageName = appPackageName;
159             mSdkPackageName = sdkPackageName;
160             mBootRelativeRequestTime = bootRelativeRequestTime;
161         }
162 
163         /** Pre-validates parameters and builds {@link SourceRegistrationRequestInternal}. */
164         @NonNull
build()165         public SourceRegistrationRequestInternal build() {
166             return new SourceRegistrationRequestInternal(this);
167         }
168 
169         /** See {@link SourceRegistrationRequestInternal#getAdIdValue()}. */
setAdIdValue(@ullable String adIdValue)170         public SourceRegistrationRequestInternal.Builder setAdIdValue(@Nullable String adIdValue) {
171             mAdIdValue = adIdValue;
172             return this;
173         }
174     }
175 
176     /** Creator for Parcelable (via reflection). */
177     public static final Creator<SourceRegistrationRequestInternal> CREATOR =
178             new Creator<>() {
179                 @Override
180                 public SourceRegistrationRequestInternal createFromParcel(Parcel in) {
181                     return new SourceRegistrationRequestInternal(in);
182                 }
183 
184                 @Override
185                 public SourceRegistrationRequestInternal[] newArray(int size) {
186                     return new SourceRegistrationRequestInternal[size];
187                 }
188             };
189 }
190