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 com.android.healthconnect.controller.dataentries.units.DistanceUnit.KILOMETERS 17 import com.android.healthconnect.controller.dataentries.units.DistanceUnit.MILES 18 import com.android.healthconnect.controller.dataentries.units.HeightUnit.CENTIMETERS 19 import com.android.healthconnect.controller.dataentries.units.HeightUnit.FEET 20 21 /** Length conversion utilities. */ 22 object LengthConverter { 23 private const val METERS_PER_MILE = 1609.3444978925634 24 private const val METERS_PER_KM = 1000.0 25 private const val CM_PER_METER = 100.0 26 private const val INCHES_PER_METER = 39.3701 27 28 /** 29 * Converts from meters to the provided distance units (miles or km) 30 * 31 * @param unit the units type to convert the passed in sourceCMeters (m) to 32 * @param source the m length to convert to toUnit length 33 * @return the sourceMeters in toUnit length units 34 */ convertDistanceFromMetersnull35 fun convertDistanceFromMeters(unit: DistanceUnit, source: Double): Double { 36 return when (unit) { 37 MILES -> source / METERS_PER_MILE 38 KILOMETERS -> source / METERS_PER_KM 39 } 40 } 41 42 /** 43 * Converts from meters to the provided height units (cm or in) 44 * 45 * @param unit the units type to convert the passed in sourceMeters (m) to 46 * @param sourceMeters the m length to convert to unit length 47 * @return the sourceMeters in toUnit length units 48 */ convertHeightFromMetersnull49 fun convertHeightFromMeters(unit: HeightUnit, sourceMeters: Double) : Double { 50 return when(unit) { 51 CENTIMETERS -> sourceMeters * CM_PER_METER 52 FEET -> sourceMeters * INCHES_PER_METER 53 } 54 } 55 } 56