From 57fe621ba8d2e4e4d0039279dbf781849ec74ab3 Mon Sep 17 00:00:00 2001 From: vanous Date: Sat, 23 Jan 2021 21:42:12 +0100 Subject: [PATCH] honor Imperial units settings in Widget, Activity list and Workouts, fixes #2041 --- .../freeyourgadget/gadgetbridge/Widget.java | 20 ++++-- .../activities/ActivitySummaryDetail.java | 67 ++++++++++++++----- .../charts/ActivityListingAdapter.java | 24 +++++-- .../adapter/ActivitySummariesAdapter.java | 29 ++++++++ app/src/main/res/values/strings.xml | 4 ++ 5 files changed, 118 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/Widget.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/Widget.java index e5274d427..158c5ae51 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/Widget.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/Widget.java @@ -59,6 +59,7 @@ import java.util.List; import java.util.concurrent.TimeUnit; import nodomain.freeyourgadget.gadgetbridge.activities.ControlCenterv2; +import nodomain.freeyourgadget.gadgetbridge.activities.SettingsActivity; import nodomain.freeyourgadget.gadgetbridge.activities.WidgetAlarmsActivity; import nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsActivity; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; @@ -155,20 +156,31 @@ public class Widget extends AppWidgetProvider { long[] dailyTotals = getSteps(); int steps = (int) dailyTotals[0]; int sleep = (int) dailyTotals[1]; - ActivityUser activityUser = new ActivityUser(); int stepGoal = activityUser.getStepsGoal(); int sleepGoal = activityUser.getSleepDuration(); int sleepGoalMinutes = sleepGoal * 60; int distanceGoal = activityUser.getDistanceMeters() * 100; int stepLength = activityUser.getStepLengthCm(); - int distanceFormatted = (int) (dailyTotals[0] * stepLength / 100); + double distanceMeters = dailyTotals[0] * stepLength / 100; + double distanceFeet = distanceMeters * 3.28084f; + double distanceFormatted = 0; String unit = "###m"; - if (distanceFormatted > 2000) { - distanceFormatted = distanceFormatted / 1000; + distanceFormatted = distanceMeters; + if (distanceMeters > 2000) { + distanceFormatted = distanceMeters / 1000; unit = "###.#km"; } + String units = GBApplication.getPrefs().getString(SettingsActivity.PREF_MEASUREMENT_SYSTEM, GBApplication.getContext().getString(R.string.p_unit_metric)); + if (units.equals(GBApplication.getContext().getString(R.string.p_unit_imperial))) { + unit = "###ft"; + distanceFormatted = distanceFeet; + if (distanceFeet > 6000) { + distanceFormatted = distanceFeet * 0.0001893939f; + unit = "###.#mi"; + } + } DecimalFormat df = new DecimalFormat(unit); views.setTextViewText(R.id.todaywidget_steps, String.format("%1s", steps)); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ActivitySummaryDetail.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ActivitySummaryDetail.java index 19cbc9e5b..fc83d606d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ActivitySummaryDetail.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ActivitySummaryDetail.java @@ -312,6 +312,8 @@ public class ActivitySummaryDetail extends AbstractGBActivity { private void makeSummaryContent(BaseActivitySummary item) { //make view of data from summaryData of item + String units = GBApplication.getPrefs().getString(SettingsActivity.PREF_MEASUREMENT_SYSTEM, GBApplication.getContext().getString(R.string.p_unit_metric)); + String UNIT_IMPERIAL = GBApplication.getContext().getString(R.string.p_unit_imperial); TableLayout fieldLayout = findViewById(R.id.summaryDetails); fieldLayout.removeAllViews(); //remove old widgets @@ -349,27 +351,56 @@ public class ActivitySummaryDetail extends AbstractGBActivity { double value = innerData.getDouble("value"); if (!show_raw_data) { - //special casing here: + //special casing here + imperial units handling switch (unit) { - case "meters_second": - value = value * 3.6; - unit = "km_h"; - break; - case "seconds_m": - value = value * (1000 / 60D); - unit = "minutes_km"; - break; - case "seconds_km": - value = value / 60; - unit = "minutes_km"; - break; - case "meters": - if (value > 2000) { - value = value / 1000; - unit = "km"; + case "cm": + if (units.equals(UNIT_IMPERIAL)) { + value = value * 0.0328084; + unit = "ft"; + } + break; + case "meters_second": + if (units.equals(UNIT_IMPERIAL)) { + value = value * 2.236936D; + unit = "mi_h"; + } else { //metric + value = value * 3.6; + unit = "km_h"; + } + break; + case "seconds_m": + if (units.equals(UNIT_IMPERIAL)) { + value = value * (1609.344 / 60D); + unit = "minutes_mi"; + } else { //metric + value = value * (1000 / 60D); + unit = "minutes_km"; + } + break; + case "seconds_km": + if (units.equals(UNIT_IMPERIAL)) { + value = value / 60D * 1.609344; + unit = "minutes_mi"; + } else { //metric + value = value / 60D; + unit = "minutes_km"; + } + break; + case "meters": + if (units.equals(UNIT_IMPERIAL)) { + value = value * 3.28084D; + unit = "ft"; + if (value > 6000) { + value = value * 0.0001893939D; + unit = "mi"; + } + } else { //metric + if (value > 2000) { + value = value / 1000; + unit = "km"; + } } break; - } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ActivityListingAdapter.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ActivityListingAdapter.java index 9c210e856..2e7e67628 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ActivityListingAdapter.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ActivityListingAdapter.java @@ -23,7 +23,9 @@ import java.util.ArrayList; import java.util.Date; import java.util.concurrent.TimeUnit; +import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; +import nodomain.freeyourgadget.gadgetbridge.activities.SettingsActivity; import nodomain.freeyourgadget.gadgetbridge.adapter.AbstractActivityListingAdapter; import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind; import nodomain.freeyourgadget.gadgetbridge.model.ActivitySession; @@ -244,14 +246,28 @@ public class ActivityListingAdapter extends AbstractActivityListingAdapter 2000) { - distance = distance / 1000; + distanceFormatted = distanceMeters; + if (distanceMeters > 2000) { + distanceFormatted = distanceMeters / 1000; unit = "###.#km"; } + + String units = GBApplication.getPrefs().getString(SettingsActivity.PREF_MEASUREMENT_SYSTEM, GBApplication.getContext().getString(R.string.p_unit_metric)); + if (units.equals(GBApplication.getContext().getString(R.string.p_unit_imperial))) { + unit = "###ft"; + distanceFormatted = distanceFeet; + if (distanceFeet > 6000) { + distanceFormatted = distanceFeet * 0.0001893939f; + unit = "###.#mi"; + } + } DecimalFormat df = new DecimalFormat(unit); - return df.format(distance); + return df.format(distanceFormatted); } @Override diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/ActivitySummariesAdapter.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/ActivitySummariesAdapter.java index 9fb38c227..d25a6fbdf 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/ActivitySummariesAdapter.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/ActivitySummariesAdapter.java @@ -39,6 +39,7 @@ import java.util.concurrent.TimeUnit; import de.greenrobot.dao.query.QueryBuilder; import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; +import nodomain.freeyourgadget.gadgetbridge.activities.SettingsActivity; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; import nodomain.freeyourgadget.gadgetbridge.database.DBHelper; import nodomain.freeyourgadget.gadgetbridge.entities.BaseActivitySummary; @@ -221,6 +222,9 @@ public class ActivitySummariesAdapter extends AbstractActivityListingAdapter 2000) { + distanceFormatted = distanceMetric / 1000; + unit = "###.#km"; + } + + String units = GBApplication.getPrefs().getString(SettingsActivity.PREF_MEASUREMENT_SYSTEM, GBApplication.getContext().getString(R.string.p_unit_metric)); + if (units.equals(GBApplication.getContext().getString(R.string.p_unit_imperial))) { + unit = "###ft"; + distanceFormatted = distanceImperial; + if (distanceImperial > 6000) { + distanceFormatted = distanceImperial * 0.0001893939f; + unit = "###.#mi"; + } + } + DecimalFormat df = new DecimalFormat(unit); + return df.format(distanceFormatted); + } + @Override protected String getHrLabel(BaseActivitySummary item) { return null; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5eb668dd1..25f689ea7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1035,9 +1035,12 @@ m cm + ft steps m/s km/h + mi/h + min/mi str/s str sec @@ -1050,6 +1053,7 @@ min/km bpm km + mi Strokes Swimming