1 /*
2  * Copyright (C) 2024 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;
18 
19 import static com.android.healthfitness.flags.Flags.FLAG_PERSONAL_HEALTH_RECORD;
20 
21 import static java.util.Objects.hash;
22 import static java.util.Objects.requireNonNull;
23 
24 import android.annotation.FlaggedApi;
25 import android.annotation.NonNull;
26 import android.health.connect.datatypes.MedicalResource;
27 import android.health.connect.internal.ParcelUtils;
28 import android.os.Parcel;
29 import android.os.Parcelable;
30 
31 import java.util.ArrayList;
32 import java.util.List;
33 
34 /** A class to represent a read response for {@link HealthConnectManager#readMedicalResources}. */
35 @FlaggedApi(FLAG_PERSONAL_HEALTH_RECORD)
36 public final class ReadMedicalResourcesResponse implements Parcelable {
37     @NonNull private final List<MedicalResource> mMedicalResources;
38 
39     /**
40      * @param medicalResources List of {@link MedicalResource}s.
41      */
ReadMedicalResourcesResponse(@onNull List<MedicalResource> medicalResources)42     public ReadMedicalResourcesResponse(@NonNull List<MedicalResource> medicalResources) {
43         requireNonNull(medicalResources);
44         mMedicalResources = medicalResources;
45     }
46 
ReadMedicalResourcesResponse(@onNull Parcel in)47     private ReadMedicalResourcesResponse(@NonNull Parcel in) {
48         requireNonNull(in);
49         in = ParcelUtils.getParcelForSharedMemoryIfRequired(in);
50         mMedicalResources = new ArrayList<>();
51         in.readParcelableList(
52                 mMedicalResources, MedicalResource.class.getClassLoader(), MedicalResource.class);
53     }
54 
55     @NonNull
56     public static final Creator<ReadMedicalResourcesResponse> CREATOR =
57             new Creator<>() {
58                 @Override
59                 public ReadMedicalResourcesResponse createFromParcel(Parcel in) {
60                     return new ReadMedicalResourcesResponse(in);
61                 }
62 
63                 @Override
64                 public ReadMedicalResourcesResponse[] newArray(int size) {
65                     return new ReadMedicalResourcesResponse[size];
66                 }
67             };
68 
69     /** Returns list of {@link MedicalResource}s. */
70     @NonNull
getMedicalResources()71     public List<MedicalResource> getMedicalResources() {
72         return mMedicalResources;
73     }
74 
75     @Override
describeContents()76     public int describeContents() {
77         return 0;
78     }
79 
80     @Override
writeToParcel(@onNull Parcel dest, int flags)81     public void writeToParcel(@NonNull Parcel dest, int flags) {
82         requireNonNull(dest);
83         ParcelUtils.putToRequiredMemory(dest, flags, this::writeToParcelInternal);
84     }
85 
writeToParcelInternal(@onNull Parcel dest)86     private void writeToParcelInternal(@NonNull Parcel dest) {
87         requireNonNull(dest);
88         dest.writeParcelableList(mMedicalResources, 0);
89     }
90 
91     /** Indicates whether some other object is "equal to" this one. */
92     @Override
equals(Object o)93     public boolean equals(Object o) {
94         if (this == o) return true;
95         if (!(o instanceof ReadMedicalResourcesResponse that)) return false;
96         return getMedicalResources().equals(that.getMedicalResources());
97     }
98 
99     /** Returns a hash code value for the object. */
100     @Override
hashCode()101     public int hashCode() {
102         return hash(getMedicalResources());
103     }
104 }
105