1 /** 2 * Copyright (C) 2023 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 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package com.android.healthconnect.controller.dataentries.formatters.shared 15 16 import android.content.Context 17 import android.health.connect.datatypes.InstantRecord 18 import android.health.connect.datatypes.IntervalRecord 19 import android.health.connect.datatypes.Record 20 import com.android.healthconnect.controller.R 21 import com.android.healthconnect.controller.data.entries.FormattedEntry 22 import com.android.healthconnect.controller.dataentries.units.UnitPreferences 23 import com.android.healthconnect.controller.utils.LocalDateTimeFormatter 24 import java.time.Instant 25 26 /** Abstract formatter for Record types. This formatter handles header for FormattedEntries. */ 27 abstract class BaseFormatter<T : Record>(private val context: Context) : Formatter<T> { 28 29 private val timeFormatter = LocalDateTimeFormatter(context) 30 protected val unitPreferences = UnitPreferences(context) 31 formatnull32 override suspend fun format(record: T, appName: String): FormattedEntry { 33 return formatRecord( 34 record = record, 35 header = getHeader(record, appName), 36 headerA11y = getHeaderA11y(record, appName), 37 unitPreferences = unitPreferences, 38 ) 39 } 40 formatRecordnull41 abstract suspend fun formatRecord( 42 record: T, 43 header: String, 44 headerA11y: String, 45 unitPreferences: UnitPreferences, 46 ): FormattedEntry 47 48 protected fun getStartTime(record: T): Instant { 49 return when (record) { 50 is IntervalRecord -> record.startTime 51 is InstantRecord -> record.time 52 else -> throw IllegalArgumentException("${record::class.java} Not supported!") 53 } 54 } 55 getHeadernull56 private fun getHeader(record: T, appName: String): String { 57 if (appName == "") 58 return context.getString( 59 R.string.data_entry_header_without_source_app, 60 getFormattedTime(record), 61 ) 62 return context.getString( 63 R.string.data_entry_header_with_source_app, 64 getFormattedTime(record), 65 appName, 66 ) 67 } 68 getHeaderA11ynull69 private fun getHeaderA11y(record: T, appName: String): String { 70 if (appName == "") 71 return context.getString( 72 R.string.data_entry_header_without_source_app, 73 getFormattedA11yTime(record), 74 ) 75 return context.getString( 76 R.string.data_entry_header_with_source_app, 77 getFormattedA11yTime(record), 78 appName, 79 ) 80 } 81 getFormattedTimenull82 private fun getFormattedTime(record: T): String { 83 return when (record) { 84 is IntervalRecord -> timeFormatter.formatTimeRange(record.startTime, record.endTime) 85 is InstantRecord -> timeFormatter.formatTime(record.time) 86 else -> throw IllegalArgumentException("${record::class.java} Not supported!") 87 } 88 } 89 getFormattedA11yTimenull90 private fun getFormattedA11yTime(record: T): String { 91 return when (record) { 92 is IntervalRecord -> timeFormatter.formatTimeRangeA11y(record.startTime, record.endTime) 93 is InstantRecord -> timeFormatter.formatTime(record.time) 94 else -> throw IllegalArgumentException("${record::class.java} Not supported!") 95 } 96 } 97 } 98