1 /**
2  * Copyright (C) 2022 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
17 
18 import android.content.Context
19 import android.health.connect.datatypes.BloodGlucoseRecord
20 import android.health.connect.datatypes.BloodGlucoseRecord.RelationToMealType
21 import android.health.connect.datatypes.BloodGlucoseRecord.RelationToMealType.RELATION_TO_MEAL_AFTER_MEAL
22 import android.health.connect.datatypes.BloodGlucoseRecord.RelationToMealType.RELATION_TO_MEAL_BEFORE_MEAL
23 import android.health.connect.datatypes.BloodGlucoseRecord.RelationToMealType.RELATION_TO_MEAL_FASTING
24 import android.health.connect.datatypes.BloodGlucoseRecord.RelationToMealType.RELATION_TO_MEAL_GENERAL
25 import android.health.connect.datatypes.BloodGlucoseRecord.SpecimenSource
26 import android.health.connect.datatypes.BloodGlucoseRecord.SpecimenSource.SPECIMEN_SOURCE_CAPILLARY_BLOOD
27 import android.health.connect.datatypes.BloodGlucoseRecord.SpecimenSource.SPECIMEN_SOURCE_INTERSTITIAL_FLUID
28 import android.health.connect.datatypes.BloodGlucoseRecord.SpecimenSource.SPECIMEN_SOURCE_PLASMA
29 import android.health.connect.datatypes.BloodGlucoseRecord.SpecimenSource.SPECIMEN_SOURCE_SERUM
30 import android.health.connect.datatypes.BloodGlucoseRecord.SpecimenSource.SPECIMEN_SOURCE_TEARS
31 import android.health.connect.datatypes.BloodGlucoseRecord.SpecimenSource.SPECIMEN_SOURCE_WHOLE_BLOOD
32 import android.health.connect.datatypes.MealType
33 import android.icu.text.MessageFormat.format
34 import androidx.annotation.StringRes
35 import com.android.healthconnect.controller.R
36 import com.android.healthconnect.controller.dataentries.formatters.shared.EntryFormatter
37 import com.android.healthconnect.controller.dataentries.units.UnitPreferences
38 import dagger.hilt.android.qualifiers.ApplicationContext
39 import java.util.StringJoiner
40 import javax.inject.Inject
41 
42 /** Formatter for printing BloodGlucoseRecord data. */
43 class BloodGlucoseFormatter @Inject constructor(@ApplicationContext private val context: Context) :
44     EntryFormatter<BloodGlucoseRecord>(context) {
45 
formatValuenull46     override suspend fun formatValue(
47         record: BloodGlucoseRecord,
48         unitPreferences: UnitPreferences
49     ): String {
50         return format(R.string.millimoles_per_liter, record)
51     }
52 
formatA11yValuenull53     override suspend fun formatA11yValue(
54         record: BloodGlucoseRecord,
55         unitPreferences: UnitPreferences
56     ): String {
57         return format(R.string.millimoles_per_liter_long, record)
58     }
59 
formatnull60     private fun format(@StringRes res: Int, record: BloodGlucoseRecord): String {
61         val stringJoiner = StringJoiner(" ")
62         stringJoiner.add(
63             format(context.getString(res), mapOf("count" to record.level.inMillimolesPerLiter)))
64 
65         if (record.specimenSource != SpecimenSource.SPECIMEN_SOURCE_UNKNOWN) {
66             stringJoiner.add(getSpecimenSource(record.specimenSource))
67         }
68 
69         if (record.mealType != MealType.MEAL_TYPE_UNKNOWN) {
70             stringJoiner.add(MealFormatter.formatMealType(context, record.mealType))
71         }
72 
73         if (record.relationToMeal != RelationToMealType.RELATION_TO_MEAL_UNKNOWN) {
74             stringJoiner.add(getRelationToMeal(record.relationToMeal))
75         }
76 
77         return stringJoiner.toString()
78     }
79 
getRelationToMealnull80     private fun getRelationToMeal(relation: Int): String {
81         return when (relation) {
82             RELATION_TO_MEAL_AFTER_MEAL -> context.getString(R.string.blood_glucose_after_meal)
83             RELATION_TO_MEAL_FASTING -> context.getString(R.string.blood_glucose_fasting)
84             RELATION_TO_MEAL_BEFORE_MEAL -> context.getString(R.string.blood_glucose_before_meal)
85             RELATION_TO_MEAL_GENERAL -> context.getString(R.string.blood_glucose_general)
86             else -> {
87                 throw IllegalArgumentException("Unknown relation to meal: $relation")
88             }
89         }
90     }
91 
getSpecimenSourcenull92     private fun getSpecimenSource(source: Int): String {
93         return when (source) {
94             SPECIMEN_SOURCE_INTERSTITIAL_FLUID -> {
95                 context.getString(R.string.specimen_source_interstitial_fluid)
96             }
97             SPECIMEN_SOURCE_CAPILLARY_BLOOD -> {
98                 context.getString(R.string.specimen_source_capillary_blood)
99             }
100             SPECIMEN_SOURCE_PLASMA -> {
101                 context.getString(R.string.specimen_source_plasma)
102             }
103             SPECIMEN_SOURCE_SERUM -> {
104                 context.getString(R.string.specimen_source_serum)
105             }
106             SPECIMEN_SOURCE_TEARS -> {
107                 context.getString(R.string.specimen_source_tears)
108             }
109             SPECIMEN_SOURCE_WHOLE_BLOOD -> {
110                 context.getString(R.string.specimen_source_whole_blood)
111             }
112             else -> {
113                 throw IllegalArgumentException("Unknown specimen source: $source")
114             }
115         }
116     }
117 }
118