• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.HealthConnectMappings;
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         HealthConnectMappings healthConnectMappings = HealthConnectMappings.getInstance();
56         mRecordTypes =
57                 recordTypes.stream()
58                         .map(healthConnectMappings::getRecordType)
59                         .collect(Collectors.toList());
60     }
61 
ActivityDatesRequestParcel(Parcel in)62     private ActivityDatesRequestParcel(Parcel in) {
63         int size = in.readInt();
64         mRecordTypes = new ArrayList<>(size);
65         for (int i = 0; i < size; i++) {
66             mRecordTypes.add(in.readInt());
67         }
68     }
69 
70     @Override
describeContents()71     public int describeContents() {
72         return 0;
73     }
74 
75     /**
76      * Flatten this object in to a Parcel.
77      *
78      * @param dest The Parcel in which the object should be written.
79      * @param flags Additional flags about how the object should be written. May be 0 or {@link
80      *     #PARCELABLE_WRITE_RETURN_VALUE}.
81      */
82     @Override
writeToParcel(@onNull Parcel dest, int flags)83     public void writeToParcel(@NonNull Parcel dest, int flags) {
84         dest.writeInt(mRecordTypes.size());
85         for (Integer recordTypeId : mRecordTypes) {
86             dest.writeInt(recordTypeId);
87         }
88     }
89 
90     /** Returns a list of record types from this parcel. */
91     @NonNull
getRecordTypes()92     public List<Class<? extends Record>> getRecordTypes() {
93         final Map<Integer, Class<? extends Record>> mRecordIdToExternalRecordClassMap =
94                 HealthConnectMappings.getInstance().getRecordIdToExternalRecordClassMap();
95         return mRecordTypes.stream()
96                 .map(mRecordIdToExternalRecordClassMap::get)
97                 .collect(Collectors.toList());
98     }
99 }
100