diff --git a/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java b/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java index 7bca1430..56107b9b 100644 --- a/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java +++ b/android_app/app/src/main/java/com/health/openscale/core/OpenScale.java @@ -27,6 +27,8 @@ import android.widget.Toast; import com.health.openscale.R; import com.health.openscale.core.alarm.AlarmHandler; import com.health.openscale.core.bluetooth.BluetoothCommunication; +import com.health.openscale.core.bodymetric.EstimatedFatMetric; +import com.health.openscale.core.bodymetric.EstimatedWaterMetric; import com.health.openscale.core.database.ScaleDatabase; import com.health.openscale.core.database.ScaleUserDatabase; import com.health.openscale.core.datatypes.ScaleData; @@ -188,6 +190,18 @@ public class OpenScale { } } + if (prefs.getBoolean("estimateFatEnable", false)) { + EstimatedFatMetric fatMetric = EstimatedFatMetric.getEstimatedFatMetric(EstimatedFatMetric.FORMULA_FAT.valueOf(prefs.getString("estimateFatFormula", "BF_DEURENBERG_II"))); + + scaleData.setFat(fatMetric.getFat(getScaleUser(scaleData.getUserId()), scaleData)); + } + + if (prefs.getBoolean("estimateWaterEnable", false)) { + EstimatedWaterMetric waterMetric = EstimatedWaterMetric.getEstimatedWaterMetric(EstimatedWaterMetric.FORMULA_WATER.valueOf(prefs.getString("estimateWaterFormula", "TBW_BEHNKE"))); + + scaleData.setWater(waterMetric.getWater(getScaleUser(scaleData.getUserId()), scaleData)); + } + if (scaleDB.insertEntry(scaleData)) { ScaleUser scaleUser = getScaleUser(scaleData.getUserId()); diff --git a/android_app/app/src/main/java/com/health/openscale/core/bodymetric/BFBJoN.java b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/BFBJoN.java new file mode 100644 index 00000000..a7d30212 --- /dev/null +++ b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/BFBJoN.java @@ -0,0 +1,35 @@ +/* Copyright (C) 2017 olie.xdev +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see +*/ +package com.health.openscale.core.bodymetric; + +import com.health.openscale.core.datatypes.ScaleData; +import com.health.openscale.core.datatypes.ScaleUser; + +public class BFBJoN extends EstimatedFatMetric { + @Override + public String getName() { + return "British Journal of Nutrition (1991)"; + } + + @Override + public float getFat(ScaleUser user, ScaleData data) { + if (user.isMale()) { + return (data.getBMI(user.body_height) * 1.2f) + (user.getAge() * 0.23f) - 16.2f; + } + + return (data.getBMI(user.body_height) * 1.2f) + (user.getAge() * 0.23f) - 5.4f; + } +} diff --git a/android_app/app/src/main/java/com/health/openscale/core/bodymetric/BFDeurenberg.java b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/BFDeurenberg.java new file mode 100644 index 00000000..35b76bed --- /dev/null +++ b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/BFDeurenberg.java @@ -0,0 +1,35 @@ +/* Copyright (C) 2017 olie.xdev +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see +*/ +package com.health.openscale.core.bodymetric; + +import com.health.openscale.core.datatypes.ScaleData; +import com.health.openscale.core.datatypes.ScaleUser; + +public class BFDeurenberg extends EstimatedFatMetric { + @Override + public String getName() { + return "Deurenberg et. al (1998)"; + } + + @Override + public float getFat(ScaleUser user, ScaleData data) { + if (user.getAge() >= 16) { + return (1.2f * data.getBMI(user.body_height)) + (0.23f*user.getAge()) - (10.8f*(1-user.gender)) - 5.4f; + } + + return (1.294f * data.getBMI(user.body_height)) + (0.20f*user.getAge()) - (11.4f*(1-user.gender)) - 8.0f; + } +} diff --git a/android_app/app/src/main/java/com/health/openscale/core/bodymetric/BFEddy.java b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/BFEddy.java new file mode 100644 index 00000000..81e2aa45 --- /dev/null +++ b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/BFEddy.java @@ -0,0 +1,35 @@ +/* Copyright (C) 2017 olie.xdev +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see +*/ +package com.health.openscale.core.bodymetric; + +import com.health.openscale.core.datatypes.ScaleData; +import com.health.openscale.core.datatypes.ScaleUser; + +public class BFEddy extends EstimatedFatMetric { + @Override + public String getName() { + return "Eddy et. al (1976)"; + } + + @Override + public float getFat(ScaleUser user, ScaleData data) { + if (user.isMale()) { + return (1.281f* data.getBMI(user.body_height)) - 10.13f; + } + + return (1.48f* data.getBMI(user.body_height)) - 7.0f; + } +} diff --git a/android_app/app/src/main/java/com/health/openscale/core/bodymetric/BFGallagher.java b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/BFGallagher.java new file mode 100644 index 00000000..f0f31a8b --- /dev/null +++ b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/BFGallagher.java @@ -0,0 +1,37 @@ +/* Copyright (C) 2017 olie.xdev +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see +*/ +package com.health.openscale.core.bodymetric; + +import com.health.openscale.core.datatypes.ScaleData; +import com.health.openscale.core.datatypes.ScaleUser; + +public class BFGallagher extends EstimatedFatMetric { + @Override + public String getName() { + return "Gallagher et. al [non-asian] (2000)"; + } + + @Override + public float getFat(ScaleUser user, ScaleData data) { + if (user.isMale()) { + // non-asian male + return 64.5f - 848.0f * (1.0f / data.getBMI(user.body_height)) + 0.079f * user.getAge() - 16.4f + 0.05f * user.getAge() + 39.0f * (1.0f / data.getBMI(user.body_height)); + } + + // non-asian female + return 64.5f - 848.0f * (1.0f / data.getBMI(user.body_height)) + 0.079f * user.getAge(); + } +} diff --git a/android_app/app/src/main/java/com/health/openscale/core/bodymetric/BFGallagherAsian.java b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/BFGallagherAsian.java new file mode 100644 index 00000000..a6a986e2 --- /dev/null +++ b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/BFGallagherAsian.java @@ -0,0 +1,37 @@ +/* Copyright (C) 2017 olie.xdev +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see +*/ +package com.health.openscale.core.bodymetric; + +import com.health.openscale.core.datatypes.ScaleData; +import com.health.openscale.core.datatypes.ScaleUser; + +public class BFGallagherAsian extends EstimatedFatMetric { + @Override + public String getName() { + return "Gallagher et. al [asian] (2000)"; + } + + @Override + public float getFat(ScaleUser user, ScaleData data) { + if (user.isMale()) { + // asian male + return 51.9f - 740.0f * (1.0f / data.getBMI(user.body_height)) + 0.029f * user.getAge(); + } + + // asian female + return 64.8f - 752.0f * (1.0f / data.getBMI(user.body_height)) + 0.016f * user.getAge(); + } +} diff --git a/android_app/app/src/main/java/com/health/openscale/core/bodymetric/EstimatedFatMetric.java b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/EstimatedFatMetric.java new file mode 100644 index 00000000..6749a65e --- /dev/null +++ b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/EstimatedFatMetric.java @@ -0,0 +1,43 @@ +/* Copyright (C) 2017 olie.xdev +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see +*/ +package com.health.openscale.core.bodymetric; + +import com.health.openscale.core.datatypes.ScaleData; +import com.health.openscale.core.datatypes.ScaleUser; + +public abstract class EstimatedFatMetric { + public enum FORMULA_FAT { BF_DEURENBERG, BF_BJoN, BF_EDDY, BF_GALLAGHER, BF_GALLAGHER_ASIAN }; + + public static EstimatedFatMetric getEstimatedFatMetric( FORMULA_FAT fatMetric) { + switch (fatMetric) { + case BF_DEURENBERG: + return new BFDeurenberg(); + case BF_BJoN: + return new BFBJoN(); + case BF_EDDY: + return new BFEddy(); + case BF_GALLAGHER: + return new BFGallagher(); + case BF_GALLAGHER_ASIAN: + return new BFGallagherAsian(); + } + + return null; + } + + public abstract String getName(); + public abstract float getFat(ScaleUser user, ScaleData data); +} diff --git a/android_app/app/src/main/java/com/health/openscale/core/bodymetric/EstimatedWaterMetric.java b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/EstimatedWaterMetric.java new file mode 100644 index 00000000..a32e48db --- /dev/null +++ b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/EstimatedWaterMetric.java @@ -0,0 +1,41 @@ +/* Copyright (C) 2017 olie.xdev +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see +*/ +package com.health.openscale.core.bodymetric; + +import com.health.openscale.core.datatypes.ScaleData; +import com.health.openscale.core.datatypes.ScaleUser; + +public abstract class EstimatedWaterMetric { + public enum FORMULA_WATER { TBW_BEHNKE, TBW_DELWAIDECRENIER, TBW_HUMEWEYERS, TBW_LEESONGKIM }; + + public static EstimatedWaterMetric getEstimatedWaterMetric( FORMULA_WATER waterMetric) { + switch (waterMetric) { + case TBW_BEHNKE: + return new TBWBehnke(); + case TBW_DELWAIDECRENIER: + return new TBWDelwaideCrenier(); + case TBW_HUMEWEYERS: + return new TBWHumeWeyers(); + case TBW_LEESONGKIM: + return new TBWLeeSongKim(); + } + + return null; + } + + public abstract String getName(); + public abstract float getWater(ScaleUser user, ScaleData data); +} diff --git a/android_app/app/src/main/java/com/health/openscale/core/bodymetric/TBWBehnke.java b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/TBWBehnke.java new file mode 100644 index 00000000..53a6ba54 --- /dev/null +++ b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/TBWBehnke.java @@ -0,0 +1,35 @@ +/* Copyright (C) 2017 olie.xdev +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see +*/ +package com.health.openscale.core.bodymetric; + +import com.health.openscale.core.datatypes.ScaleData; +import com.health.openscale.core.datatypes.ScaleUser; + +public class TBWBehnke extends EstimatedWaterMetric { + @Override + public String getName() { + return "Behnke et. al (1963)"; + } + + @Override + public float getWater(ScaleUser user, ScaleData data) { + if (user.isMale()) { + return 0.72f * (0.204f * user.body_height * user.body_height) / 100.0f; + } + + return 0.72f * (0.18f * user.body_height * user.body_height) / 100.0f; + } +} diff --git a/android_app/app/src/main/java/com/health/openscale/core/bodymetric/TBWDelwaideCrenier.java b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/TBWDelwaideCrenier.java new file mode 100644 index 00000000..83667a04 --- /dev/null +++ b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/TBWDelwaideCrenier.java @@ -0,0 +1,31 @@ +/* Copyright (C) 2017 olie.xdev +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see +*/ +package com.health.openscale.core.bodymetric; + +import com.health.openscale.core.datatypes.ScaleData; +import com.health.openscale.core.datatypes.ScaleUser; + +public class TBWDelwaideCrenier extends EstimatedWaterMetric { + @Override + public String getName() { + return "Delwaide-Crenier et. al (1973)"; + } + + @Override + public float getWater(ScaleUser user, ScaleData data) { + return 0.72f * (-1.976f + 0.907f * data.getWeight()); + } +} diff --git a/android_app/app/src/main/java/com/health/openscale/core/bodymetric/TBWHumeWeyers.java b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/TBWHumeWeyers.java new file mode 100644 index 00000000..59d53c2d --- /dev/null +++ b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/TBWHumeWeyers.java @@ -0,0 +1,35 @@ +/* Copyright (C) 2017 olie.xdev +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see +*/ +package com.health.openscale.core.bodymetric; + +import com.health.openscale.core.datatypes.ScaleData; +import com.health.openscale.core.datatypes.ScaleUser; + +public class TBWHumeWeyers extends EstimatedWaterMetric { + @Override + public String getName() { + return "Hume & Weyers (1971)"; + } + + @Override + public float getWater(ScaleUser user, ScaleData data) { + if (user.isMale()) { + return (0.194786f * user.body_height) + (0.296785f * data.getWeight()) - 14.012934f; + } + + return (0.34454f * user.body_height) + (0.183809f * data.getWeight()) - 35.270121f; + } +} diff --git a/android_app/app/src/main/java/com/health/openscale/core/bodymetric/TBWLeeSongKim.java b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/TBWLeeSongKim.java new file mode 100644 index 00000000..0f578345 --- /dev/null +++ b/android_app/app/src/main/java/com/health/openscale/core/bodymetric/TBWLeeSongKim.java @@ -0,0 +1,35 @@ +/* Copyright (C) 2017 olie.xdev +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see +*/ +package com.health.openscale.core.bodymetric; + +import com.health.openscale.core.datatypes.ScaleData; +import com.health.openscale.core.datatypes.ScaleUser; + +public class TBWLeeSongKim extends EstimatedWaterMetric { + @Override + public String getName() { + return "Lee, Song, Kim, Lee et. al (1999)"; + } + + @Override + public float getWater(ScaleUser user, ScaleData data) { + if (user.isMale()) { + return -28.3497f + (0.243057f * user.body_height) + (0.366248f * data.getWeight()); + } + + return -26.6224f + (0.262513f * user.body_height) + (0.232948f * data.getWeight()); + } +} diff --git a/android_app/app/src/main/java/com/health/openscale/gui/preferences/MeasurementPreferences.java b/android_app/app/src/main/java/com/health/openscale/gui/preferences/MeasurementPreferences.java index 03de804f..ef6c025b 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/preferences/MeasurementPreferences.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/preferences/MeasurementPreferences.java @@ -19,18 +19,37 @@ import android.app.AlertDialog; import android.content.DialogInterface; import android.content.SharedPreferences; import android.os.Bundle; +import android.preference.CheckBoxPreference; +import android.preference.EditTextPreference; +import android.preference.ListPreference; +import android.preference.MultiSelectListPreference; import android.preference.Preference; import android.preference.PreferenceFragment; +import android.preference.PreferenceGroup; import android.preference.PreferenceManager; import android.widget.Toast; import com.health.openscale.R; import com.health.openscale.core.OpenScale; +import com.health.openscale.core.bodymetric.EstimatedFatMetric; +import com.health.openscale.core.bodymetric.EstimatedWaterMetric; -public class MeasurementPreferences extends PreferenceFragment { +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +public class MeasurementPreferences extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener { public static final String PREFERENCE_KEY_DELETE_ALL = "deleteAll"; + public static final String PREFERENCE_KEY_ESTIMATE_FAT = "estimateFatEnable"; + public static final String PREFERENCE_KEY_ESTIMATE_FAT_FORMULA = "estimateFatFormula"; + public static final String PREFERENCE_KEY_ESTIMATE_WATER = "estimateWaterEnable"; + public static final String PREFERENCE_KEY_ESTIMATE_WATER_FORMULA = "estimateWaterFormula"; private Preference deleteAll; + private CheckBoxPreference estimateFatEnable; + private ListPreference estimateFatFormula; + private CheckBoxPreference estimateWaterEnable; + private ListPreference estimateWaterFormula; @Override public void onCreate(Bundle savedInstanceState) { @@ -40,6 +59,109 @@ public class MeasurementPreferences extends PreferenceFragment { deleteAll = (Preference) findPreference(PREFERENCE_KEY_DELETE_ALL); deleteAll.setOnPreferenceClickListener(new onClickListenerDeleteAll()); + + estimateFatEnable = (CheckBoxPreference) findPreference(PREFERENCE_KEY_ESTIMATE_FAT); + estimateFatFormula = (ListPreference) findPreference(PREFERENCE_KEY_ESTIMATE_FAT_FORMULA); + estimateWaterEnable = (CheckBoxPreference) findPreference(PREFERENCE_KEY_ESTIMATE_WATER); + estimateWaterFormula = (ListPreference) findPreference(PREFERENCE_KEY_ESTIMATE_WATER_FORMULA); + + updateFatListPreferences(); + updateWaterListPreferences(); + initSummary(getPreferenceScreen()); + } + + public void updateFatListPreferences() { + ArrayList listEntries = new ArrayList(); + ArrayList listEntryValues = new ArrayList(); + + for (EstimatedFatMetric.FORMULA_FAT formulaFat : EstimatedFatMetric.FORMULA_FAT.values()) { + EstimatedFatMetric fatMetric = EstimatedFatMetric.getEstimatedFatMetric(formulaFat); + + listEntries.add(fatMetric.getName()); + listEntryValues.add(formulaFat.toString()); + } + + estimateFatFormula.setEntries(listEntries.toArray(new CharSequence[listEntries.size()])); + estimateFatFormula.setEntryValues(listEntryValues.toArray(new CharSequence[listEntryValues.size()])); + } + + public void updateWaterListPreferences() { + ArrayList listEntries = new ArrayList(); + ArrayList listEntryValues = new ArrayList(); + + for (EstimatedWaterMetric.FORMULA_WATER formulaWater : EstimatedWaterMetric.FORMULA_WATER.values()) { + EstimatedWaterMetric waterMetric = EstimatedWaterMetric.getEstimatedWaterMetric(formulaWater); + + listEntries.add(waterMetric.getName()); + listEntryValues.add(formulaWater.toString()); + } + + estimateWaterFormula.setEntries(listEntries.toArray(new CharSequence[listEntries.size()])); + estimateWaterFormula.setEntryValues(listEntryValues.toArray(new CharSequence[listEntryValues.size()])); + } + + private void initSummary(Preference p) { + if (p instanceof PreferenceGroup) { + PreferenceGroup pGrp = (PreferenceGroup) p; + for (int i = 0; i < pGrp.getPreferenceCount(); i++) { + initSummary(pGrp.getPreference(i)); + } + } else { + updatePrefSummary(p); + } + } + + @Override + public void onResume() { + super.onResume(); + getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); + } + + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + updatePrefSummary(findPreference(key)); + } + + private void updatePrefSummary(Preference p) { + if (estimateFatEnable.isChecked()) { + estimateFatFormula.setEnabled(true); + } else { + estimateFatFormula.setEnabled(false); + } + + if (estimateWaterEnable.isChecked()) { + estimateWaterFormula.setEnabled(true); + } else { + estimateWaterFormula.setEnabled(false); + } + + estimateFatFormula.setSummary(EstimatedFatMetric.getEstimatedFatMetric(EstimatedFatMetric.FORMULA_FAT.valueOf(estimateFatFormula.getValue())).getName()); + estimateWaterFormula.setSummary(EstimatedWaterMetric.getEstimatedWaterMetric(EstimatedWaterMetric.FORMULA_WATER.valueOf(estimateWaterFormula.getValue())).getName()); + + if (p instanceof EditTextPreference) { + EditTextPreference editTextPref = (EditTextPreference) p; + if (p.getTitle().toString().contains("assword")) + { + p.setSummary("******"); + } else { + p.setSummary(editTextPref.getText()); + } + } + + if (p instanceof MultiSelectListPreference) { + MultiSelectListPreference editMultiListPref = (MultiSelectListPreference) p; + + CharSequence[] entries = editMultiListPref.getEntries(); + CharSequence[] entryValues = editMultiListPref.getEntryValues(); + List currentEntries = new ArrayList<>(); + Set currentEntryValues = editMultiListPref.getValues(); + + for (int i = 0; i < entries.length; i++) + if (currentEntryValues.contains(entryValues[i])) + currentEntries.add(entries[i].toString()); + + p.setSummary(currentEntries.toString()); + } } private class onClickListenerDeleteAll implements Preference.OnPreferenceClickListener { diff --git a/android_app/app/src/main/java/com/health/openscale/gui/views/FatMeasurementView.java b/android_app/app/src/main/java/com/health/openscale/gui/views/FatMeasurementView.java index 447ba2ac..0d14a6a0 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/views/FatMeasurementView.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/views/FatMeasurementView.java @@ -26,13 +26,27 @@ import com.health.openscale.core.evaluation.EvaluationSheet; public class FatMeasurementView extends MeasurementView { + private boolean estimateFatEnable; + public FatMeasurementView(Context context) { super(context, context.getResources().getString(R.string.label_fat), ContextCompat.getDrawable(context, R.drawable.ic_fat)); } + @Override + public boolean isEditable() { + if (estimateFatEnable && getMeasurementMode() == MeasurementViewMode.ADD) { + return false; + } + return true; + } + @Override public void updateValue(ScaleData updateData) { - setValueOnView(updateData.getFat()); + if (estimateFatEnable && getMeasurementMode() == MeasurementViewMode.ADD) { + setValueOnView((getContext().getString(R.string.label_automatic))); + } else { + setValueOnView(updateData.getFat()); + } } @Override @@ -48,6 +62,7 @@ public class FatMeasurementView extends MeasurementView { @Override public void updatePreferences(SharedPreferences preferences) { setVisible(preferences.getBoolean("fatEnable", true)); + estimateFatEnable = preferences.getBoolean("estimateFatEnable", false); } @Override diff --git a/android_app/app/src/main/java/com/health/openscale/gui/views/MeasurementView.java b/android_app/app/src/main/java/com/health/openscale/gui/views/MeasurementView.java index 62a71db4..09635f65 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/views/MeasurementView.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/views/MeasurementView.java @@ -21,6 +21,7 @@ import android.content.DialogInterface; import android.content.SharedPreferences; import android.graphics.Color; import android.graphics.drawable.Drawable; +import android.os.Handler; import android.support.v4.content.ContextCompat; import android.text.Html; import android.text.InputType; @@ -28,7 +29,6 @@ import android.util.TypedValue; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; -import android.view.ViewGroup; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; @@ -38,11 +38,6 @@ import android.widget.Space; import android.widget.TableLayout; import android.widget.TableRow; import android.widget.TextView; -import android.os.Handler; -import android.view.MotionEvent; -import android.view.View; -import android.view.View.OnClickListener; -import android.view.View.OnTouchListener; import com.health.openscale.R; import com.health.openscale.core.OpenScale; @@ -213,8 +208,11 @@ public abstract class MeasurementView extends TableLayout { if (value.length() == 0) { return -1; } - - return Float.valueOf(value); + try { + return Float.valueOf(value); + } catch (NumberFormatException e) { + return -1; + } } public void incValue() { diff --git a/android_app/app/src/main/java/com/health/openscale/gui/views/WaterMeasurementView.java b/android_app/app/src/main/java/com/health/openscale/gui/views/WaterMeasurementView.java index a153aad8..54ec2de1 100644 --- a/android_app/app/src/main/java/com/health/openscale/gui/views/WaterMeasurementView.java +++ b/android_app/app/src/main/java/com/health/openscale/gui/views/WaterMeasurementView.java @@ -26,13 +26,27 @@ import com.health.openscale.core.evaluation.EvaluationSheet; public class WaterMeasurementView extends MeasurementView { + private boolean estimateWaterEnable; + public WaterMeasurementView(Context context) { super(context, context.getResources().getString(R.string.label_water), ContextCompat.getDrawable(context, R.drawable.ic_water)); } + @Override + public boolean isEditable() { + if (estimateWaterEnable && getMeasurementMode() == MeasurementViewMode.ADD) { + return false; + } + return true; + } + @Override public void updateValue(ScaleData updateData) { - setValueOnView(updateData.getWater()); + if (estimateWaterEnable && getMeasurementMode() == MeasurementViewMode.ADD) { + setValueOnView((getContext().getString(R.string.label_automatic))); + } else { + setValueOnView(updateData.getWater()); + } } @Override @@ -48,6 +62,7 @@ public class WaterMeasurementView extends MeasurementView { @Override public void updatePreferences(SharedPreferences preferences) { setVisible(preferences.getBoolean("waterEnable", true)); + estimateWaterEnable = preferences.getBoolean("estimateWaterEnable", false); } @Override diff --git a/android_app/app/src/main/res/values/strings.xml b/android_app/app/src/main/res/values/strings.xml index e649b79b..425027c0 100644 --- a/android_app/app/src/main/res/values/strings.xml +++ b/android_app/app/src/main/res/values/strings.xml @@ -120,6 +120,13 @@ Delete confirmation + Estimate body fat + Estimate body water + + Body fat formula + Body water formula + auto + Reminder Weekdays Time diff --git a/android_app/app/src/main/res/xml/measurement_preferences.xml b/android_app/app/src/main/res/xml/measurement_preferences.xml index daa046dd..6c873f85 100644 --- a/android_app/app/src/main/res/xml/measurement_preferences.xml +++ b/android_app/app/src/main/res/xml/measurement_preferences.xml @@ -6,5 +6,9 @@ + + + + \ No newline at end of file