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.annotation.Nullable; 21 import android.health.connect.HealthConnectManager; 22 import android.health.connect.internal.ParcelUtils; 23 import android.health.connect.internal.datatypes.RecordInternal; 24 import android.health.connect.internal.datatypes.utils.ParcelRecordConverter; 25 import android.os.Parcel; 26 import android.os.Parcelable; 27 28 import java.lang.reflect.InvocationTargetException; 29 import java.util.ArrayList; 30 import java.util.List; 31 32 /** 33 * A wrapper to carry a list of entries of type {@link RecordInternal} from and to {@link 34 * HealthConnectManager} 35 * 36 * @hide 37 */ 38 public class RecordsParcel implements Parcelable { 39 @NonNull 40 public static final Creator<RecordsParcel> CREATOR = 41 new Creator<>() { 42 @Override 43 public RecordsParcel createFromParcel(Parcel in) { 44 return new RecordsParcel(in); 45 } 46 47 @Override 48 public RecordsParcel[] newArray(int size) { 49 return new RecordsParcel[size]; 50 } 51 }; 52 53 private final List<RecordInternal<?>> mRecordInternals; 54 private long mRecordsChunkSize; 55 private List<Long> mRecordsSize; 56 57 @SuppressWarnings("NullAway.Init") // TODO(b/317029272): fix this suppression RecordsParcel(@onNull List<RecordInternal<?>> recordInternals)58 public RecordsParcel(@NonNull List<RecordInternal<?>> recordInternals) { 59 mRecordInternals = recordInternals; 60 } 61 RecordsParcel(@onNull Parcel in)62 private RecordsParcel(@NonNull Parcel in) { 63 in = ParcelUtils.getParcelForSharedMemoryIfRequired(in); 64 int size = in.readInt(); 65 mRecordInternals = new ArrayList<>(size); 66 mRecordsSize = new ArrayList<>(size); 67 long remainingParcelSize = in.dataAvail(); 68 mRecordsChunkSize = remainingParcelSize; 69 for (int i = 0; i < size; i++) { 70 int identifier = in.readInt(); 71 try { 72 mRecordInternals.add(ParcelRecordConverter.getInstance().getRecord(in, identifier)); 73 // Calculating record size based on before and after values of parcel size. 74 mRecordsSize.add(remainingParcelSize - in.dataAvail()); 75 remainingParcelSize = in.dataAvail(); 76 } catch (InstantiationException 77 | IllegalAccessException 78 | NoSuchMethodException 79 | InvocationTargetException e) { 80 throw new IllegalArgumentException(); 81 } 82 } 83 } 84 85 @Override describeContents()86 public int describeContents() { 87 return 0; 88 } 89 90 @Override writeToParcel(@onNull Parcel dest, int flags)91 public void writeToParcel(@NonNull Parcel dest, int flags) { 92 ParcelUtils.putToRequiredMemory(dest, flags, this::writeToParcelInternal); 93 } 94 95 @NonNull getRecords()96 public List<RecordInternal<?>> getRecords() { 97 return mRecordInternals; 98 } 99 100 /** 101 * @return a list containing size of the individual records. Used for memory rate limiting. 102 */ 103 @Nullable getRecordsSize()104 public List<Long> getRecordsSize() { 105 return mRecordsSize; 106 } 107 108 /** 109 * @return size of the record parcel. Used for memory rate limiting. 110 */ getRecordsChunkSize()111 public long getRecordsChunkSize() { 112 return mRecordsChunkSize; 113 } 114 writeToParcelInternal(@onNull Parcel dest)115 private void writeToParcelInternal(@NonNull Parcel dest) { 116 dest.writeInt(mRecordInternals.size()); 117 for (RecordInternal<?> recordInternal : mRecordInternals) { 118 dest.writeInt(recordInternal.getRecordType()); 119 recordInternal.writeToParcel(dest); 120 } 121 } 122 } 123