• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
18 
19 package com.android.healthconnect.controller.dataentries.formatters
20 
21 import android.content.Context
22 import android.health.connect.datatypes.MenstruationPeriodRecord
23 import android.health.connect.datatypes.Record
24 import com.android.healthconnect.controller.R
25 import com.android.healthconnect.controller.data.entries.FormattedEntry.FormattedDataEntry
26 import com.android.healthconnect.controller.shared.DataType
27 import com.android.healthconnect.controller.shared.app.AppInfoReader
28 import com.android.healthconnect.controller.utils.toLocalDate
29 import dagger.hilt.android.qualifiers.ApplicationContext
30 import java.time.Instant
31 import java.time.Period
32 import java.time.temporal.ChronoUnit.DAYS
33 import javax.inject.Inject
34 
35 /** Formatter for printing MenstruationPeriodRecord data. */
36 class MenstruationPeriodFormatter
37 @Inject
38 constructor(
39     private val appInfoReader: AppInfoReader,
40     @ApplicationContext private val context: Context
41 ) {
42 
formatnull43     suspend fun format(
44         day: Instant,
45         record: MenstruationPeriodRecord,
46         showDataOrigin: Boolean = true
47     ): FormattedDataEntry {
48         val dayOfPeriod = dayOfPeriod(record, day)
49         val totalDays = totalDaysOfPeriod(record)
50         val title = context.getString(R.string.period_day, dayOfPeriod, totalDays)
51         val appName = if (showDataOrigin) getAppName(record) else ""
52 
53         return FormattedDataEntry(
54             uuid = record.metadata.id,
55             title = title,
56             titleA11y = title,
57             header = appName,
58             headerA11y = appName,
59             dataType = DataType.MENSTRUATION_PERIOD,
60             startTime = record.startTime,
61             endTime = record.endTime)
62     }
63 
dayOfPeriodnull64     private fun dayOfPeriod(record: MenstruationPeriodRecord, day: Instant): Int {
65         return (Period.between(record.startTime.toLocalDate(), day.toLocalDate()).days +
66             1) // + 1 to return a 1-indexed counter (i.e. "Period day 1", not "day 0")
67     }
68 
totalDaysOfPeriodnull69     private fun totalDaysOfPeriod(record: MenstruationPeriodRecord): Int {
70         return (DAYS.between(record.startTime.toLocalDate(), record.endTime.toLocalDate()).toInt() +
71             1)
72     }
73 
getAppNamenull74     private suspend fun getAppName(record: Record): String {
75         return appInfoReader.getAppMetadata(record.metadata.dataOrigin.packageName).appName
76     }
77 }
78