1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * ``` 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * ``` 10 * 11 * Unless required by applicable law or agreed to in writing, software distributed under the License 12 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 13 * or implied. See the License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 package com.android.healthconnect.controller.dataentries.formatters.medical 17 18 import android.content.Context 19 import android.health.connect.datatypes.MedicalDataSource 20 import android.health.connect.datatypes.MedicalResource 21 import com.android.healthconnect.controller.R 22 import com.android.healthconnect.controller.data.entries.FormattedEntry 23 import com.android.healthconnect.controller.shared.app.AppInfoReader 24 import com.android.healthconnect.controller.shared.app.MedicalDataSourceReader 25 import dagger.hilt.android.qualifiers.ApplicationContext 26 import javax.inject.Inject 27 import javax.inject.Singleton 28 29 /** Formatter for medical entries. */ 30 @Singleton 31 class MedicalEntryFormatter 32 @Inject 33 constructor( 34 private val medicalDataSourceReader: MedicalDataSourceReader, 35 private val appInfoReader: AppInfoReader, 36 private val displayNameExtractor: DisplayNameExtractor, 37 @ApplicationContext private val context: Context, 38 ) { formatResourcenull39 suspend fun formatResource( 40 resource: MedicalResource, 41 showDataOrigin: Boolean, 42 ): FormattedEntry.FormattedMedicalDataEntry { 43 44 val dataSources = medicalDataSourceReader.fromDataSourceId(resource.dataSourceId) 45 val dataSource = dataSources.getOrNull(0) 46 val dataSourceName = dataSource?.displayName ?: "" 47 val appName: String = if (showDataOrigin) getAppName(dataSource) else "" 48 49 val header = getHeader(dataSourceName, appName) 50 return FormattedEntry.FormattedMedicalDataEntry( 51 header = header, 52 headerA11y = header, 53 title = displayNameExtractor.getDisplayName(resource.fhirResource.data), 54 titleA11y = displayNameExtractor.getDisplayName(resource.fhirResource.data), 55 medicalResourceId = resource.id, 56 ) 57 } 58 getHeadernull59 private fun getHeader(dataSourceName: String, appName: String): String { 60 if (dataSourceName == "" && appName == "") { 61 return "" 62 } 63 if (dataSourceName == "") { 64 return appName 65 } 66 if (appName == "") { 67 return dataSourceName 68 } 69 70 return context.getString( 71 R.string.data_entry_header_with_source_app, 72 appName, 73 dataSourceName, 74 ) 75 } 76 getAppNamenull77 private suspend fun getAppName(dataSource: MedicalDataSource?): String { 78 dataSource?.let { 79 return appInfoReader.getAppMetadata(dataSource.packageName).appName 80 } 81 return "" 82 } 83 } 84