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.MedicalResource.MEDICAL_RESOURCE_TYPE_ALLERGIES_INTOLERANCES; 20 import static android.health.connect.datatypes.MedicalResource.MEDICAL_RESOURCE_TYPE_CONDITIONS; 21 import static android.health.connect.datatypes.MedicalResource.MEDICAL_RESOURCE_TYPE_LABORATORY_RESULTS; 22 import static android.health.connect.datatypes.MedicalResource.MEDICAL_RESOURCE_TYPE_MEDICATIONS; 23 import static android.health.connect.datatypes.MedicalResource.MEDICAL_RESOURCE_TYPE_PERSONAL_DETAILS; 24 import static android.health.connect.datatypes.MedicalResource.MEDICAL_RESOURCE_TYPE_PRACTITIONER_DETAILS; 25 import static android.health.connect.datatypes.MedicalResource.MEDICAL_RESOURCE_TYPE_PREGNANCY; 26 import static android.health.connect.datatypes.MedicalResource.MEDICAL_RESOURCE_TYPE_PROCEDURES; 27 import static android.health.connect.datatypes.MedicalResource.MEDICAL_RESOURCE_TYPE_SOCIAL_HISTORY; 28 import static android.health.connect.datatypes.MedicalResource.MEDICAL_RESOURCE_TYPE_VACCINES; 29 import static android.health.connect.datatypes.MedicalResource.MEDICAL_RESOURCE_TYPE_VISITS; 30 import static android.health.connect.datatypes.MedicalResource.MEDICAL_RESOURCE_TYPE_VITAL_SIGNS; 31 32 import static com.android.healthfitness.flags.AconfigFlagHelper.isPersonalHealthRecordEnabled; 33 34 import android.annotation.NonNull; 35 import android.health.connect.HealthPermissions; 36 import android.health.connect.datatypes.MedicalResource.MedicalResourceType; 37 import android.util.ArrayMap; 38 39 import java.util.Map; 40 41 /** @hide */ 42 public final class MedicalResourceTypePermissionMapper { 43 44 private static final Map<Integer, String> sMedicalResourceTypeToReadPermissionMap = 45 new ArrayMap<>(); 46 private static final Map<String, Integer> sMedicalResourceReadPermissionToTypeMap = 47 new ArrayMap<>(); 48 MedicalResourceTypePermissionMapper()49 private MedicalResourceTypePermissionMapper() {} 50 populateMedicalResourceTypeAndReadPermissionMaps()51 private static synchronized void populateMedicalResourceTypeAndReadPermissionMaps() { 52 if (!isPersonalHealthRecordEnabled()) { 53 throw new UnsupportedOperationException( 54 "populateMedicalResourceTypeToPermissionMap is not supported"); 55 } 56 57 if (!sMedicalResourceTypeToReadPermissionMap.isEmpty()) { 58 return; 59 } 60 61 // Populate sMedicalResourceTypeToReadPermissionMap. 62 sMedicalResourceTypeToReadPermissionMap.put( 63 MEDICAL_RESOURCE_TYPE_ALLERGIES_INTOLERANCES, 64 HealthPermissions.READ_MEDICAL_DATA_ALLERGIES_INTOLERANCES); 65 sMedicalResourceTypeToReadPermissionMap.put( 66 MEDICAL_RESOURCE_TYPE_CONDITIONS, HealthPermissions.READ_MEDICAL_DATA_CONDITIONS); 67 sMedicalResourceTypeToReadPermissionMap.put( 68 MEDICAL_RESOURCE_TYPE_LABORATORY_RESULTS, 69 HealthPermissions.READ_MEDICAL_DATA_LABORATORY_RESULTS); 70 sMedicalResourceTypeToReadPermissionMap.put( 71 MEDICAL_RESOURCE_TYPE_MEDICATIONS, HealthPermissions.READ_MEDICAL_DATA_MEDICATIONS); 72 sMedicalResourceTypeToReadPermissionMap.put( 73 MEDICAL_RESOURCE_TYPE_PERSONAL_DETAILS, 74 HealthPermissions.READ_MEDICAL_DATA_PERSONAL_DETAILS); 75 sMedicalResourceTypeToReadPermissionMap.put( 76 MEDICAL_RESOURCE_TYPE_PRACTITIONER_DETAILS, 77 HealthPermissions.READ_MEDICAL_DATA_PRACTITIONER_DETAILS); 78 sMedicalResourceTypeToReadPermissionMap.put( 79 MEDICAL_RESOURCE_TYPE_PREGNANCY, HealthPermissions.READ_MEDICAL_DATA_PREGNANCY); 80 sMedicalResourceTypeToReadPermissionMap.put( 81 MEDICAL_RESOURCE_TYPE_PROCEDURES, HealthPermissions.READ_MEDICAL_DATA_PROCEDURES); 82 sMedicalResourceTypeToReadPermissionMap.put( 83 MEDICAL_RESOURCE_TYPE_SOCIAL_HISTORY, 84 HealthPermissions.READ_MEDICAL_DATA_SOCIAL_HISTORY); 85 sMedicalResourceTypeToReadPermissionMap.put( 86 MEDICAL_RESOURCE_TYPE_VACCINES, HealthPermissions.READ_MEDICAL_DATA_VACCINES); 87 sMedicalResourceTypeToReadPermissionMap.put( 88 MEDICAL_RESOURCE_TYPE_VISITS, HealthPermissions.READ_MEDICAL_DATA_VISITS); 89 sMedicalResourceTypeToReadPermissionMap.put( 90 MEDICAL_RESOURCE_TYPE_VITAL_SIGNS, HealthPermissions.READ_MEDICAL_DATA_VITAL_SIGNS); 91 92 // Populate sMedicalResourceTypeToReadPermissionMap. 93 sMedicalResourceTypeToReadPermissionMap.forEach( 94 (key, value) -> { 95 sMedicalResourceReadPermissionToTypeMap.put(value, key); 96 }); 97 } 98 99 /** 100 * Returns {@link HealthPermissions} for the input {@link MedicalResourceType}. 101 * 102 * @throws IllegalArgumentException if there is no read permission associated to the given 103 * {@link MedicalResourceType}. 104 */ getMedicalReadPermission(@edicalResourceType int resourceType)105 public static String getMedicalReadPermission(@MedicalResourceType int resourceType) { 106 populateMedicalResourceTypeAndReadPermissionMaps(); 107 108 String medicalReadPermission = sMedicalResourceTypeToReadPermissionMap.get(resourceType); 109 if (medicalReadPermission != null) { 110 return medicalReadPermission; 111 } 112 113 throw new IllegalArgumentException( 114 "Medical read permission not found for the Medical Resource Type: " + resourceType); 115 } 116 117 /** 118 * Returns {@link MedicalResourceType} for the input {@code readPermission}. 119 * 120 * @throws IllegalArgumentException if there is no {@link MedicalResourceType} associated to the 121 * given read permission. 122 */ 123 @MedicalResourceType getMedicalResourceType(@onNull String readPermission)124 public static int getMedicalResourceType(@NonNull String readPermission) { 125 populateMedicalResourceTypeAndReadPermissionMaps(); 126 127 if (sMedicalResourceReadPermissionToTypeMap.containsKey(readPermission)) { 128 return sMedicalResourceReadPermissionToTypeMap.get(readPermission); 129 } 130 131 throw new IllegalArgumentException("Medical Resource Type not found for " + readPermission); 132 } 133 } 134