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  *      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.units
15 
16 import android.content.Context
17 import android.content.SharedPreferences
18 import androidx.preference.PreferenceManager
19 import com.android.healthconnect.controller.dataentries.units.DistanceUnit.KILOMETERS
20 import com.android.healthconnect.controller.dataentries.units.EnergyUnit.CALORIE
21 import com.android.healthconnect.controller.dataentries.units.EnergyUnit.valueOf
22 import com.android.healthconnect.controller.dataentries.units.HeightUnit.CENTIMETERS
23 import com.android.healthconnect.controller.dataentries.units.TemperatureUnit.FAHRENHEIT
24 import com.android.healthconnect.controller.dataentries.units.WeightUnit.POUND
25 import com.google.common.annotations.VisibleForTesting
26 import dagger.hilt.android.qualifiers.ApplicationContext
27 import javax.inject.Inject
28 import javax.inject.Singleton
29 
30 /** Preferences wrapper for health data units. */
31 @Singleton
32 class UnitPreferences @Inject constructor(@ApplicationContext private val context: Context) {
33 
34     companion object {
35         const val DISTANCE_UNIT_PREF_KEY = "DISTANCE_UNIT_KEY"
36         const val HEIGHT_UNIT_PREF_KEY = "HEIGHT_UNIT_KEY"
37         const val WEIGHT_UNIT_PREF_KEY = "WEIGHT_UNIT_KEY"
38         const val ENERGY_UNIT_PREF_KEY = "ENERGY_UNIT_KEY"
39         const val TEMPERATURE_UNIT_PREF_KEY = "TEMPERATURE_UNIT_KEY"
40 
41         @VisibleForTesting val DEFAULT_DISTANCE_UNIT = KILOMETERS
42         @VisibleForTesting val DEFAULT_HEIGHT_UNIT = CENTIMETERS
43         @VisibleForTesting val DEFAULT_WEIGHT_UNIT = POUND
44         @VisibleForTesting val DEFAULT_ENERGY_UNIT = CALORIE
45         @VisibleForTesting val DEFAULT_TEMPERATURE_UNIT = FAHRENHEIT
46     }
47 
<lambda>null48     private val unitSharedPreference: SharedPreferences by lazy {
49         PreferenceManager.getDefaultSharedPreferences(context)
50     }
51 
getDistanceUnitnull52     fun getDistanceUnit(): DistanceUnit {
53         if (!unitSharedPreference.contains(DISTANCE_UNIT_PREF_KEY)) {
54             setDistanceUnit(DEFAULT_DISTANCE_UNIT)
55             return DEFAULT_DISTANCE_UNIT
56         }
57         val unitString =
58             unitSharedPreference.getString(
59                 DISTANCE_UNIT_PREF_KEY, DEFAULT_DISTANCE_UNIT.toString())!!
60         return DistanceUnit.valueOf(unitString)
61     }
62 
setDistanceUnitnull63     fun setDistanceUnit(distanceUnit: DistanceUnit) {
64         with(unitSharedPreference.edit()) {
65             putString(DISTANCE_UNIT_PREF_KEY, distanceUnit.toString())
66             apply()
67         }
68     }
69 
getHeightUnitnull70     fun getHeightUnit(): HeightUnit {
71         if (!unitSharedPreference.contains(HEIGHT_UNIT_PREF_KEY)) {
72             setHeightUnit(DEFAULT_HEIGHT_UNIT)
73             return DEFAULT_HEIGHT_UNIT
74         }
75         val unitString =
76             unitSharedPreference.getString(HEIGHT_UNIT_PREF_KEY, DEFAULT_HEIGHT_UNIT.toString())!!
77         return HeightUnit.valueOf(unitString)
78     }
79 
setHeightUnitnull80     fun setHeightUnit(heightUnit: HeightUnit) {
81         with(unitSharedPreference.edit()) {
82             putString(HEIGHT_UNIT_PREF_KEY, heightUnit.toString())
83             apply()
84         }
85     }
86 
getWeightUnitnull87     fun getWeightUnit(): WeightUnit {
88         if (!unitSharedPreference.contains(WEIGHT_UNIT_PREF_KEY)) {
89             setWeightUnit(DEFAULT_WEIGHT_UNIT)
90             return DEFAULT_WEIGHT_UNIT
91         }
92         val unitString =
93             unitSharedPreference.getString(WEIGHT_UNIT_PREF_KEY, DEFAULT_WEIGHT_UNIT.toString())!!
94         return WeightUnit.valueOf(unitString)
95     }
96 
setWeightUnitnull97     fun setWeightUnit(weightUnit: WeightUnit) {
98         with(unitSharedPreference.edit()) {
99             putString(WEIGHT_UNIT_PREF_KEY, weightUnit.toString())
100             apply()
101         }
102     }
103 
getEnergyUnitnull104     fun getEnergyUnit(): EnergyUnit {
105         if (!unitSharedPreference.contains(ENERGY_UNIT_PREF_KEY)) {
106             setEnergyUnit(DEFAULT_ENERGY_UNIT)
107             return DEFAULT_ENERGY_UNIT
108         }
109         val unitString =
110             unitSharedPreference.getString(ENERGY_UNIT_PREF_KEY, DEFAULT_ENERGY_UNIT.toString())!!
111         return valueOf(unitString)
112     }
113 
setEnergyUnitnull114     fun setEnergyUnit(energyUnit: EnergyUnit) {
115         with(unitSharedPreference.edit()) {
116             putString(ENERGY_UNIT_PREF_KEY, energyUnit.toString())
117             apply()
118         }
119     }
120 
getTemperatureUnitnull121     fun getTemperatureUnit(): TemperatureUnit {
122         if (!unitSharedPreference.contains(TEMPERATURE_UNIT_PREF_KEY)) {
123             setTemperatureUnit(DEFAULT_TEMPERATURE_UNIT)
124             return DEFAULT_TEMPERATURE_UNIT
125         }
126         val unitString =
127             unitSharedPreference.getString(
128                 TEMPERATURE_UNIT_PREF_KEY, DEFAULT_TEMPERATURE_UNIT.toString())!!
129         return TemperatureUnit.valueOf(unitString)
130     }
131 
setTemperatureUnitnull132     fun setTemperatureUnit(temperatureUnit: TemperatureUnit) {
133         with(unitSharedPreference.edit()) {
134             putString(TEMPERATURE_UNIT_PREF_KEY, temperatureUnit.toString())
135             apply()
136         }
137     }
138 }
139 
140 interface UnitPreference
141 
142 enum class DistanceUnit : UnitPreference {
143     KILOMETERS,
144     MILES
145 }
146 
147 enum class HeightUnit : UnitPreference {
148     CENTIMETERS,
149     FEET
150 }
151 
152 enum class WeightUnit : UnitPreference {
153     POUND,
154     KILOGRAM,
155     STONE
156 }
157 
158 enum class EnergyUnit : UnitPreference {
159     CALORIE,
160     KILOJOULE
161 }
162 
163 enum class TemperatureUnit : UnitPreference {
164     CELSIUS,
165     FAHRENHEIT,
166     KELVIN
167 }
168