From cf1b4ee368594eee5a64f68f75dac8c5b1148705 Mon Sep 17 00:00:00 2001 From: "S. Kozyr" Date: Fri, 23 Aug 2024 09:36:25 +0300 Subject: [PATCH] [Android] Introduced SPEED_LIMIT_THRESHOLD for speedometer warning Signed-off-by: S. Kozyr --- .../src/main/cpp/app/organicmaps/util/StringUtils.cpp | 6 ++++++ .../main/java/app/organicmaps/util/StringUtils.java | 1 + .../main/java/app/organicmaps/widget/menu/NavMenu.java | 8 +++++++- platform/measurement_utils.cpp | 10 ++++++++++ platform/measurement_utils.hpp | 2 ++ 5 files changed, 26 insertions(+), 1 deletion(-) diff --git a/android/app/src/main/cpp/app/organicmaps/util/StringUtils.cpp b/android/app/src/main/cpp/app/organicmaps/util/StringUtils.cpp index e99a34b0ed..0e458299c0 100644 --- a/android/app/src/main/cpp/app/organicmaps/util/StringUtils.cpp +++ b/android/app/src/main/cpp/app/organicmaps/util/StringUtils.cpp @@ -82,4 +82,10 @@ Java_app_organicmaps_util_StringUtils_nativeGetLocalizedSpeedUnits(JNIEnv * env, { return jni::ToJavaString(env, platform::GetLocalizedSpeedUnits()); } + +JNIEXPORT jdouble JNICALL +Java_app_organicmaps_util_StringUtils_nativeLocalizedSpeedToMps(JNIEnv * env, jclass, jdouble speed) +{ + return measurement_utils::UnitsToMps(speed, measurement_utils::GetMeasurementUnits()); +} } // extern "C" diff --git a/android/app/src/main/java/app/organicmaps/util/StringUtils.java b/android/app/src/main/java/app/organicmaps/util/StringUtils.java index d82b80a092..a4b0134385 100644 --- a/android/app/src/main/java/app/organicmaps/util/StringUtils.java +++ b/android/app/src/main/java/app/organicmaps/util/StringUtils.java @@ -31,6 +31,7 @@ public class StringUtils public static native Pair nativeFormatSpeedAndUnits(double metersPerSecond); public static native Distance nativeFormatDistance(double meters); + public static native double nativeLocalizedSpeedToMps(double speed); @NonNull public static native Pair nativeGetLocalizedDistanceUnits(); @NonNull diff --git a/android/app/src/main/java/app/organicmaps/widget/menu/NavMenu.java b/android/app/src/main/java/app/organicmaps/widget/menu/NavMenu.java index 763a3c86d1..d006d77054 100644 --- a/android/app/src/main/java/app/organicmaps/widget/menu/NavMenu.java +++ b/android/app/src/main/java/app/organicmaps/widget/menu/NavMenu.java @@ -28,6 +28,11 @@ import java.util.concurrent.TimeUnit; public class NavMenu { + // If the current speed exceeds the allowed speed by 0.5 we do not show any warnings. + // If the speed exceeds by more than 1 we inform the user about speed violation. + // This constant should be treated as kmph or mph depending on locale. + public static final double SPEED_LIMIT_THRESHOLD = 1.0; + private final BottomSheetBehavior mNavBottomSheetBehavior; private final View mBottomSheetBackground; private final View mHeaderFrame; @@ -222,7 +227,8 @@ public class NavMenu else mSpeedValue.setText(speedAndUnits.first); - if (info.speedLimitMps > 0.0 && last.getSpeed() > info.speedLimitMps) + final double speedLimitThresholdMps = StringUtils.nativeLocalizedSpeedToMps(SPEED_LIMIT_THRESHOLD); + if (info.speedLimitMps > 0.0 && last.getSpeed() > info.speedLimitMps + speedLimitThresholdMps) { if (info.isSpeedCamLimitExceeded()) mSpeedValue.setTextColor(ContextCompat.getColor(mActivity, R.color.white_primary)); diff --git a/platform/measurement_utils.cpp b/platform/measurement_utils.cpp index 579077f2dc..81094b1cb7 100644 --- a/platform/measurement_utils.cpp +++ b/platform/measurement_utils.cpp @@ -184,6 +184,16 @@ double MpsToUnits(double metersPerSecond, Units units) UNREACHABLE(); } +double UnitsToMps(double speed, Units units) +{ + switch (units) + { + case Units::Imperial: return MphToMps(speed); break; + case Units::Metric: return KmphToMps(speed); break; + } + UNREACHABLE(); +} + std::string FormatSpeedNumeric(double metersPerSecond, Units units) { double const unitsPerHour = MpsToUnits(metersPerSecond, units); diff --git a/platform/measurement_utils.hpp b/platform/measurement_utils.hpp index 906d4960e5..480ff594eb 100644 --- a/platform/measurement_utils.hpp +++ b/platform/measurement_utils.hpp @@ -29,9 +29,11 @@ inline double FeetToMiles(double ft) { return ft * 0.00018939; } inline double InchesToMeters(double in) { return in / 39.370; } inline double NauticalMilesToMeters(double nmi) { return nmi * 1852; } inline double constexpr KmphToMps(double kmph) { return kmph * 1000 / 3600; } +inline double constexpr MphToMps(double mph) { return MiphToKmph(mph) * 1000 / 3600; } double ToSpeedKmPH(double speed, Units units); double MpsToUnits(double mps, Units units); +double UnitsToMps(double speed, Units units); /// @return Speed value string (without suffix) in km/h for Metric and in mph for Imperial. std::string FormatSpeedNumeric(double metersPerSecond, Units units); -- 2.45.3