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.aidl;
18 
19 import android.annotation.NonNull;
20 import android.health.connect.datatypes.Record;
21 import android.health.connect.internal.datatypes.utils.RecordMapper;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Objects;
29 import java.util.stream.Collectors;
30 
31 /**
32  * A parcel for activity dates request containing list of record classes to query for.
33  *
34  * @hide
35  */
36 public final class ActivityDatesRequestParcel implements Parcelable {
37     @android.annotation.NonNull
38     public static final Creator<ActivityDatesRequestParcel> CREATOR =
39             new Creator<ActivityDatesRequestParcel>() {
40                 @Override
41                 public ActivityDatesRequestParcel createFromParcel(Parcel in) {
42                     return new ActivityDatesRequestParcel(in);
43                 }
44 
45                 @Override
46                 public ActivityDatesRequestParcel[] newArray(int size) {
47                     return new ActivityDatesRequestParcel[size];
48                 }
49             };
50 
51     private final List<Integer> mRecordTypes;
52 
ActivityDatesRequestParcel(@onNull List<Class<? extends Record>> recordTypes)53     public ActivityDatesRequestParcel(@NonNull List<Class<? extends Record>> recordTypes) {
54         Objects.requireNonNull(recordTypes);
55         RecordMapper recordMapper = RecordMapper.getInstance();
56         mRecordTypes =
57                 recordTypes.stream().map(recordMapper::getRecordType).collect(Collectors.toList());
58     }
59 
ActivityDatesRequestParcel(Parcel in)60     private ActivityDatesRequestParcel(Parcel in) {
61         int size = in.readInt();
62         mRecordTypes = new ArrayList<>(size);
63         for (int i = 0; i < size; i++) {
64             mRecordTypes.add(in.readInt());
65         }
66     }
67 
68     @Override
describeContents()69     public int describeContents() {
70         return 0;
71     }
72 
73     /**
74      * Flatten this object in to a Parcel.
75      *
76      * @param dest The Parcel in which the object should be written.
77      * @param flags Additional flags about how the object should be written. May be 0 or {@link
78      *     #PARCELABLE_WRITE_RETURN_VALUE}.
79      */
80     @Override
writeToParcel(@onNull Parcel dest, int flags)81     public void writeToParcel(@NonNull Parcel dest, int flags) {
82         dest.writeInt(mRecordTypes.size());
83         for (Integer recordTypeId : mRecordTypes) {
84             dest.writeInt(recordTypeId);
85         }
86     }
87 
88     /** Returns a list of record types from this parcel. */
89     @NonNull
getRecordTypes()90     public List<Class<? extends Record>> getRecordTypes() {
91         final Map<Integer, Class<? extends Record>> mRecordIdToExternalRecordClassMap =
92                 RecordMapper.getInstance().getRecordIdToExternalRecordClassMap();
93         return mRecordTypes.stream()
94                 .map(mRecordIdToExternalRecordClassMap::get)
95                 .collect(Collectors.toList());
96     }
97 }
98