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.BloodPressureRecord
20 import android.health.connect.datatypes.BloodPressureRecord.BloodPressureMeasurementLocation.BLOOD_PRESSURE_MEASUREMENT_LOCATION_LEFT_UPPER_ARM
21 import android.health.connect.datatypes.BloodPressureRecord.BloodPressureMeasurementLocation.BLOOD_PRESSURE_MEASUREMENT_LOCATION_LEFT_WRIST
22 import android.health.connect.datatypes.BloodPressureRecord.BloodPressureMeasurementLocation.BLOOD_PRESSURE_MEASUREMENT_LOCATION_RIGHT_UPPER_ARM
23 import android.health.connect.datatypes.BloodPressureRecord.BloodPressureMeasurementLocation.BLOOD_PRESSURE_MEASUREMENT_LOCATION_RIGHT_WRIST
24 import android.health.connect.datatypes.BloodPressureRecord.BloodPressureMeasurementLocation.BLOOD_PRESSURE_MEASUREMENT_LOCATION_UNKNOWN
25 import android.health.connect.datatypes.BloodPressureRecord.BodyPosition
26 import androidx.annotation.StringRes
27 import com.android.healthconnect.controller.R
28 import com.android.healthconnect.controller.dataentries.formatters.shared.EntryFormatter
29 import com.android.healthconnect.controller.dataentries.units.UnitPreferences
30 import dagger.hilt.android.qualifiers.ApplicationContext
31 import java.util.Locale
32 import java.util.StringJoiner
33 import javax.inject.Inject
34 
35 /** Formatter for printing BloodPressureRecord data. */
36 class BloodPressureFormatter @Inject constructor(@ApplicationContext private val context: Context) :
37     EntryFormatter<BloodPressureRecord>(context) {
38 
formatValuenull39     override suspend fun formatValue(
40         record: BloodPressureRecord,
41         unitPreferences: UnitPreferences
42     ): String {
43         return formatBloodPressure(R.string.blood_pressure, record)
44     }
45 
formatA11yValuenull46     override suspend fun formatA11yValue(
47         record: BloodPressureRecord,
48         unitPreferences: UnitPreferences
49     ): String {
50         return formatBloodPressure(R.string.blood_pressure_long, record)
51     }
52 
formatBloodPressurenull53     private fun formatBloodPressure(@StringRes res: Int, record: BloodPressureRecord): String {
54         val systolic =
55             String.format(Locale.getDefault(), "%.0f", record.systolic.inMillimetersOfMercury)
56         val diastolic =
57             String.format(Locale.getDefault(), "%.0f", record.diastolic.inMillimetersOfMercury)
58         val bloodPressure = context.getString(res, systolic, diastolic)
59 
60         val stringJoiner = StringJoiner(" ")
61         stringJoiner.add(bloodPressure)
62 
63         if (BLOOD_PRESSURE_MEASUREMENT_LOCATION_UNKNOWN != record.measurementLocation) {
64             stringJoiner.add(getMeasurementLocation(record.measurementLocation))
65         }
66 
67         if (BodyPosition.BODY_POSITION_UNKNOWN != record.bodyPosition) {
68             stringJoiner.add(getBodyPosition(record.bodyPosition))
69         }
70 
71         return stringJoiner.toString()
72     }
73 
getBodyPositionnull74     private fun getBodyPosition(bodyPosition: Int): CharSequence? {
75         return when (bodyPosition) {
76             BodyPosition.BODY_POSITION_STANDING_UP ->
77                 context.getString(R.string.body_position_standing_up)
78             BodyPosition.BODY_POSITION_SITTING_DOWN ->
79                 context.getString(R.string.body_position_sitting_down)
80             BodyPosition.BODY_POSITION_LYING_DOWN ->
81                 context.getString(R.string.body_position_lying_down)
82             BodyPosition.BODY_POSITION_RECLINING ->
83                 context.getString(R.string.body_position_reclining)
84             else -> {
85                 throw java.lang.IllegalArgumentException(
86                     "Unrecognised blood pressure measurement position: $bodyPosition")
87             }
88         }
89     }
90 
getMeasurementLocationnull91     private fun getMeasurementLocation(location: Int): String {
92         return when (location) {
93             BLOOD_PRESSURE_MEASUREMENT_LOCATION_LEFT_WRIST ->
94                 context.getString(R.string.blood_pressure_left_wrist)
95             BLOOD_PRESSURE_MEASUREMENT_LOCATION_RIGHT_WRIST ->
96                 context.getString(R.string.blood_pressure_right_wrist)
97             BLOOD_PRESSURE_MEASUREMENT_LOCATION_LEFT_UPPER_ARM ->
98                 context.getString(R.string.blood_pressure_left_arm)
99             BLOOD_PRESSURE_MEASUREMENT_LOCATION_RIGHT_UPPER_ARM ->
100                 context.getString(R.string.blood_pressure_right_arm)
101             else -> {
102                 throw IllegalArgumentException(
103                     "Unrecognised blood pressure measurement location: $location")
104             }
105         }
106     }
107 }
108