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.annotation.Nullable; 27 import android.health.connect.datatypes.MedicalResource; 28 import android.health.connect.internal.ParcelUtils; 29 import android.os.Parcel; 30 import android.os.Parcelable; 31 32 import java.util.ArrayList; 33 import java.util.List; 34 import java.util.Objects; 35 36 /** A read response for {@link HealthConnectManager#readMedicalResources}. */ 37 @FlaggedApi(FLAG_PERSONAL_HEALTH_RECORD) 38 public final class ReadMedicalResourcesResponse implements Parcelable { 39 @NonNull private final List<MedicalResource> mMedicalResources; 40 @Nullable private final String mNextPageToken; 41 private final int mRemainingCount; 42 43 /** 44 * Constructs a new {@link ReadMedicalResourcesResponse} instance. 45 * 46 * @param medicalResources List of {@link MedicalResource}s. 47 * @param nextPageToken The token value of the read result which can be used as input token for 48 * next read request. {@code null} if there are no more pages available. 49 * @param remainingCount the total number of medical resources remaining, excluding the ones in 50 * this response 51 */ ReadMedicalResourcesResponse( @onNull List<MedicalResource> medicalResources, @Nullable String nextPageToken, int remainingCount)52 public ReadMedicalResourcesResponse( 53 @NonNull List<MedicalResource> medicalResources, 54 @Nullable String nextPageToken, 55 int remainingCount) { 56 requireNonNull(medicalResources); 57 if (nextPageToken == null && remainingCount > 0) { 58 throw new IllegalArgumentException( 59 String.format( 60 "Remaining count must be 0 to have a null next page token, but was %d", 61 remainingCount)); 62 } 63 if (nextPageToken != null && remainingCount == 0) { 64 throw new IllegalArgumentException("Next page token provided with no remaining data"); 65 } 66 mMedicalResources = medicalResources; 67 mNextPageToken = nextPageToken; 68 mRemainingCount = remainingCount; 69 } 70 ReadMedicalResourcesResponse(@onNull Parcel in)71 private ReadMedicalResourcesResponse(@NonNull Parcel in) { 72 requireNonNull(in); 73 in = ParcelUtils.getParcelForSharedMemoryIfRequired(in); 74 mMedicalResources = new ArrayList<>(); 75 in.readParcelableList( 76 mMedicalResources, MedicalResource.class.getClassLoader(), MedicalResource.class); 77 mNextPageToken = in.readString(); 78 mRemainingCount = in.readInt(); 79 } 80 81 @NonNull 82 public static final Creator<ReadMedicalResourcesResponse> CREATOR = 83 new Creator<>() { 84 @Override 85 public ReadMedicalResourcesResponse createFromParcel(Parcel in) { 86 return new ReadMedicalResourcesResponse(in); 87 } 88 89 @Override 90 public ReadMedicalResourcesResponse[] newArray(int size) { 91 return new ReadMedicalResourcesResponse[size]; 92 } 93 }; 94 95 /** Returns list of {@link MedicalResource}s. */ 96 @NonNull getMedicalResources()97 public List<MedicalResource> getMedicalResources() { 98 return mMedicalResources; 99 } 100 101 /** 102 * Returns a page token to read the next page of the result. {@code null} if there are no more 103 * pages available. 104 */ 105 @Nullable getNextPageToken()106 public String getNextPageToken() { 107 return mNextPageToken; 108 } 109 110 /** 111 * Returns the count of medical resources still remaining which were not returned due to 112 * pagination. 113 * 114 * <p>For a response with a null next page token, this will be 0. This result is accurate at the 115 * time the request was made, and with the permissions when the request was made. However, the 116 * actual results may change if permissions change or resources are inserted or deleted. 117 */ getRemainingCount()118 public int getRemainingCount() { 119 return mRemainingCount; 120 } 121 122 @Override describeContents()123 public int describeContents() { 124 return 0; 125 } 126 127 @Override writeToParcel(@onNull Parcel dest, int flags)128 public void writeToParcel(@NonNull Parcel dest, int flags) { 129 requireNonNull(dest); 130 ParcelUtils.putToRequiredMemory(dest, flags, this::writeToParcelInternal); 131 } 132 writeToParcelInternal(@onNull Parcel dest)133 private void writeToParcelInternal(@NonNull Parcel dest) { 134 requireNonNull(dest); 135 dest.writeParcelableList(mMedicalResources, 0); 136 dest.writeString(mNextPageToken); 137 dest.writeInt(mRemainingCount); 138 } 139 140 @Override equals(Object o)141 public boolean equals(Object o) { 142 if (this == o) return true; 143 if (!(o instanceof ReadMedicalResourcesResponse that)) return false; 144 return getMedicalResources().equals(that.getMedicalResources()) 145 && Objects.equals(getNextPageToken(), that.getNextPageToken()) 146 && mRemainingCount == that.getRemainingCount(); 147 } 148 149 @Override hashCode()150 public int hashCode() { 151 return hash(getMedicalResources(), getNextPageToken(), mRemainingCount); 152 } 153 154 @Override toString()155 public String toString() { 156 StringBuilder sb = new StringBuilder(); 157 sb.append(this.getClass().getSimpleName()).append("{"); 158 sb.append("medicalResources=").append(getMedicalResources()); 159 sb.append(",nextPageToken=").append(getNextPageToken()); 160 sb.append("}"); 161 return sb.toString(); 162 } 163 } 164