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.internal.datatypes.utils; 18 19 import static android.health.connect.datatypes.validation.ValidationUtils.INTDEF_VALIDATION_ERROR_PREFIX; 20 21 import android.annotation.NonNull; 22 import android.health.connect.datatypes.Record; 23 import android.health.connect.datatypes.RecordTypeIdentifier; 24 import android.health.connect.internal.datatypes.RecordInternal; 25 26 import java.lang.reflect.InvocationTargetException; 27 import java.util.ArrayList; 28 import java.util.List; 29 import java.util.Map; 30 import java.util.Objects; 31 32 /** 33 * A helper class used to convert internal and external data types. 34 * 35 * @hide 36 */ 37 public final class InternalExternalRecordConverter { 38 @SuppressWarnings("NullAway.Init") // TODO(b/317029272): fix this suppression 39 private static volatile InternalExternalRecordConverter sInternalExternalRecordConverter; 40 41 private final Map<Integer, Class<? extends RecordInternal<?>>> 42 mRecordIdToInternalRecordClassMap; 43 InternalExternalRecordConverter()44 private InternalExternalRecordConverter() { 45 // Add any new data type here to facilitate its conversion. 46 mRecordIdToInternalRecordClassMap = 47 HealthConnectMappings.getInstance().getRecordIdToInternalRecordClassMap(); 48 } 49 50 @NonNull getInstance()51 public static synchronized InternalExternalRecordConverter getInstance() { 52 if (sInternalExternalRecordConverter == null) { 53 sInternalExternalRecordConverter = new InternalExternalRecordConverter(); 54 } 55 56 return sInternalExternalRecordConverter; 57 } 58 59 /** Returns a new instance of {@link RecordInternal} for the provided {@code type }. */ 60 @NonNull newInternalRecord(@ecordTypeIdentifier.RecordType int type)61 public RecordInternal<?> newInternalRecord(@RecordTypeIdentifier.RecordType int type) { 62 Class<? extends RecordInternal<?>> recordClass = 63 mRecordIdToInternalRecordClassMap.get(type); 64 Objects.requireNonNull(recordClass); 65 RecordInternal<?> recordInternal; 66 try { 67 recordInternal = recordClass.getConstructor().newInstance(); 68 } catch (InstantiationException 69 | IllegalAccessException 70 | InvocationTargetException 71 | NoSuchMethodException e) { 72 throw new RuntimeException(e); 73 } 74 75 return recordInternal; 76 } 77 78 /** Returns a record for {@param record} */ 79 @SuppressWarnings("NullAway") // TODO(b/317029272): fix this suppression 80 @NonNull getExternalRecords(@onNull List<RecordInternal<?>> recordInternals)81 public List<Record> getExternalRecords(@NonNull List<RecordInternal<?>> recordInternals) { 82 List<Record> externalRecordList = new ArrayList<>(recordInternals.size()); 83 84 for (RecordInternal<?> recordInternal : recordInternals) { 85 try { 86 externalRecordList.add(recordInternal.toExternalRecord()); 87 } catch (IllegalArgumentException illegalArgumentException) { 88 if (!illegalArgumentException 89 .getMessage() 90 .contains(INTDEF_VALIDATION_ERROR_PREFIX)) { 91 throw illegalArgumentException; 92 } 93 } 94 } 95 96 return externalRecordList; 97 } 98 } 99