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.health.connect.changelog;
18 
19 import static android.health.connect.Constants.DEFAULT_PAGE_SIZE;
20 import static android.health.connect.Constants.MAXIMUM_PAGE_SIZE;
21 
22 import android.annotation.IntRange;
23 import android.annotation.NonNull;
24 import android.health.connect.HealthConnectManager;
25 import android.os.Parcel;
26 import android.os.Parcelable;
27 
28 import java.util.Objects;
29 
30 /** Request class for {@link HealthConnectManager#getChangeLogs} */
31 public final class ChangeLogsRequest implements Parcelable {
32     private final String mToken;
33     private final int mPageSize;
34 
35     /**
36      * @see Builder
37      */
ChangeLogsRequest(@onNull String token, int pageSize)38     private ChangeLogsRequest(@NonNull String token, int pageSize) {
39         Objects.requireNonNull(token);
40 
41         mToken = token;
42         mPageSize = pageSize;
43     }
44 
ChangeLogsRequest(Parcel in)45     private ChangeLogsRequest(Parcel in) {
46         mToken = in.readString();
47         mPageSize = in.readInt();
48     }
49 
50     @NonNull
51     public static final Creator<ChangeLogsRequest> CREATOR =
52             new Creator<ChangeLogsRequest>() {
53                 @Override
54                 public ChangeLogsRequest createFromParcel(Parcel in) {
55                     return new ChangeLogsRequest(in);
56                 }
57 
58                 @Override
59                 public ChangeLogsRequest[] newArray(int size) {
60                     return new ChangeLogsRequest[size];
61                 }
62             };
63 
64     /** Returns the token for the change logs request */
65     @NonNull
getToken()66     public String getToken() {
67         return mToken;
68     }
69 
70     /**
71      * Returns the maximum number of records requested using {@link
72      * HealthConnectManager#getChangeLogs} operation
73      */
74     @IntRange(from = 1, to = 5000)
getPageSize()75     public int getPageSize() {
76         return mPageSize;
77     }
78 
79     @Override
describeContents()80     public int describeContents() {
81         return 0;
82     }
83 
84     @Override
writeToParcel(@onNull Parcel dest, int flags)85     public void writeToParcel(@NonNull Parcel dest, int flags) {
86         dest.writeString(mToken);
87         dest.writeInt(mPageSize);
88     }
89 
90     /** Builder class for {@link ChangeLogsRequest} */
91     public static final class Builder {
92         private String mToken;
93         private int mPageSize = DEFAULT_PAGE_SIZE;
94 
Builder(@onNull String token)95         public Builder(@NonNull String token) {
96             Objects.requireNonNull(token);
97 
98             mToken = token;
99         }
100 
101         /**
102          * @param pageSize number of change logs to be returned, this corresponds to the maximum
103          *     number of entries to be returned i.e. sum of the response within {@link
104          *     ChangeLogsResponse}
105          *     <p>If not set default is 1000 and maximum value is 5000.
106          * @throws IllegalArgumentException if requested pageSize > 5000 or < 1
107          */
108         @NonNull
setPageSize(@ntRangefrom = 1, to = 5000) int pageSize)109         public Builder setPageSize(@IntRange(from = 1, to = 5000) int pageSize) {
110             if (pageSize > MAXIMUM_PAGE_SIZE) {
111                 throw new IllegalArgumentException(
112                         "Maximum page size: " + MAXIMUM_PAGE_SIZE + ", requested: " + pageSize);
113             }
114             if (pageSize < 1) {
115                 throw new IllegalArgumentException("Minimum page size: 1, requested: " + pageSize);
116             }
117             mPageSize = pageSize;
118             return this;
119         }
120 
121         /** Returns Object of {@link ChangeLogsRequest} */
122         @NonNull
build()123         public ChangeLogsRequest build() {
124             return new ChangeLogsRequest(mToken, mPageSize);
125         }
126     }
127 }
128