From 29fe3d793ab83c68120fcb0d3c5b9c15cbf0e35d Mon Sep 17 00:00:00 2001 From: 0xRe1nk0 <0xre1nk0@gmail.com> Date: Fri, 27 Sep 2024 09:37:26 +0300 Subject: [PATCH] Add reset average speed action --- OsmAnd/res/layout/setting_action_button.xml | 19 +++++++++++++ OsmAnd/res/values/strings.xml | 1 + .../AverageSpeedWidgetSettingFragment.java | 11 ++++++++ .../utils/AverageSpeedComputer.java | 4 +++ .../widgets/AverageSpeedWidget.java | 28 ++++++++++++++++++- .../mapwidgets/widgets/SimpleWidget.java | 12 +++++++- 6 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 OsmAnd/res/layout/setting_action_button.xml diff --git a/OsmAnd/res/layout/setting_action_button.xml b/OsmAnd/res/layout/setting_action_button.xml new file mode 100644 index 00000000000..3a640cb6c0d --- /dev/null +++ b/OsmAnd/res/layout/setting_action_button.xml @@ -0,0 +1,19 @@ + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 6a2eb4a29ff..f9fa6b57900 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -10,6 +10,7 @@ - For wording and consistency, please note https://docs.osmand.net/docs/technical/contributions/translating-osmand Thx - Hardy --> + Reset average speed V Wikimedia Open in browser diff --git a/OsmAnd/src/net/osmand/plus/views/mapwidgets/configure/settings/AverageSpeedWidgetSettingFragment.java b/OsmAnd/src/net/osmand/plus/views/mapwidgets/configure/settings/AverageSpeedWidgetSettingFragment.java index cfd0d6f6f4d..af9ad1573bb 100644 --- a/OsmAnd/src/net/osmand/plus/views/mapwidgets/configure/settings/AverageSpeedWidgetSettingFragment.java +++ b/OsmAnd/src/net/osmand/plus/views/mapwidgets/configure/settings/AverageSpeedWidgetSettingFragment.java @@ -59,6 +59,7 @@ protected void setupContent(@NonNull LayoutInflater themedInflater, @NonNull Vie setupSkipStopsSetting(); themedInflater.inflate(R.layout.divider, container); super.setupContent(themedInflater, container); + setupSettingAction(themedInflater, container); } private void setupIntervalSliderCard() { @@ -85,6 +86,16 @@ private void setupSkipStopsSetting() { skipStopsContainer.setBackground(getPressedStateDrawable()); } + private void setupSettingAction(@NonNull LayoutInflater themedInflater, @NonNull ViewGroup container) { + themedInflater.inflate(R.layout.divider, container); + View actionView = themedInflater.inflate(R.layout.setting_action_button, null); + actionView.setBackground(getPressedStateDrawable()); + actionView.setOnClickListener(v -> speedWidget.resetAverageSpeed()); + TextView title = actionView.findViewById(R.id.action_title); + title.setText(R.string.reset_average_speed); + container.addView(actionView); + } + @Override public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); diff --git a/OsmAnd/src/net/osmand/plus/views/mapwidgets/utils/AverageSpeedComputer.java b/OsmAnd/src/net/osmand/plus/views/mapwidgets/utils/AverageSpeedComputer.java index 56df0fa1c46..ec64805a880 100644 --- a/OsmAnd/src/net/osmand/plus/views/mapwidgets/utils/AverageSpeedComputer.java +++ b/OsmAnd/src/net/osmand/plus/views/mapwidgets/utils/AverageSpeedComputer.java @@ -148,6 +148,10 @@ public static int getConvertedSpeedToSkip(@NonNull SpeedConstants speedSystem) { } } + public void resetLocations() { + locations.clear(); + } + private static class SegmentsList { private final Segment[] segments; diff --git a/OsmAnd/src/net/osmand/plus/views/mapwidgets/widgets/AverageSpeedWidget.java b/OsmAnd/src/net/osmand/plus/views/mapwidgets/widgets/AverageSpeedWidget.java index 4a3ab14d991..db20fa07c5e 100644 --- a/OsmAnd/src/net/osmand/plus/views/mapwidgets/widgets/AverageSpeedWidget.java +++ b/OsmAnd/src/net/osmand/plus/views/mapwidgets/widgets/AverageSpeedWidget.java @@ -5,17 +5,23 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.settings.backend.preferences.CommonPreference; +import net.osmand.plus.utils.ColorUtilities; import net.osmand.plus.utils.OsmAndFormatter; import net.osmand.plus.utils.OsmAndFormatter.FormattedValue; +import net.osmand.plus.utils.UiUtilities; import net.osmand.plus.views.layers.base.OsmandMapLayer.DrawSettings; import net.osmand.plus.views.mapwidgets.WidgetsPanel; import net.osmand.plus.views.mapwidgets.utils.AverageSpeedComputer; -import net.osmand.plus.views.mapwidgets.WidgetType; +import net.osmand.plus.widgets.popup.PopUpMenuItem; import net.osmand.util.Algorithms; +import java.util.ArrayList; +import java.util.List; + public class AverageSpeedWidget extends SimpleWidget { private static final String MEASURED_INTERVAL_PREF_ID = "average_speed_measured_interval_millis"; @@ -77,6 +83,26 @@ private void updateAverageSpeed() { } } + @Nullable + @Override + protected List getWidgetActions() { + List actions = new ArrayList<>(); + UiUtilities uiUtilities = app.getUIUtilities(); + int iconColor = ColorUtilities.getDefaultIconColor(app, nightMode); + + actions.add(new PopUpMenuItem.Builder(app) + .setIcon(uiUtilities.getPaintedIcon(R.drawable.ic_action_reset_to_default_dark, iconColor)) + .setTitleId(R.string.reset_average_speed) + .setOnClickListener(item -> resetAverageSpeed()) + .showTopDivider(true) + .create()); + return actions; + } + + public void resetAverageSpeed() { + averageSpeedComputer.resetLocations(); + } + @Override public void copySettings(@NonNull ApplicationMode appMode, @Nullable String customId) { copySettingsFromMode(appMode, appMode, customId); diff --git a/OsmAnd/src/net/osmand/plus/views/mapwidgets/widgets/SimpleWidget.java b/OsmAnd/src/net/osmand/plus/views/mapwidgets/widgets/SimpleWidget.java index 6efa85f67e0..1d78279ad4a 100644 --- a/OsmAnd/src/net/osmand/plus/views/mapwidgets/widgets/SimpleWidget.java +++ b/OsmAnd/src/net/osmand/plus/views/mapwidgets/widgets/SimpleWidget.java @@ -217,6 +217,11 @@ public void showContextWidgetMenu(@NonNull View view) { .setOnClickListener(item -> ConfigureWidgetsFragment.showInstance(mapActivity, widgetInfo.getWidgetPanel(), appMode, widgetId, true)) .create()); + List widgetActions = getWidgetActions(); + if (!Algorithms.isEmpty(widgetActions)) { + items.addAll(widgetActions); + } + WidgetSettingsBaseFragment fragment = widgetType != null ? widgetType.getSettingsFragment(app, widgetInfo) : null; if (fragment != null) { items.add(new PopUpMenuItem.Builder(app) @@ -229,7 +234,7 @@ public void showContextWidgetMenu(@NonNull View view) { WidgetSettingsBaseFragment.showFragment(manager, args, null, fragment); }) .setIcon(uiUtilities.getPaintedIcon(R.drawable.ic_action_settings_outlined, iconColor)) - .showTopDivider(true) + .showTopDivider(Algorithms.isEmpty(widgetActions)) .create()); } @@ -261,6 +266,11 @@ public void showContextWidgetMenu(@NonNull View view) { } } + @Nullable + protected List getWidgetActions() { + return null; + } + @Override public final void updateInfo(@Nullable OsmandMapLayer.DrawSettings drawSettings) { boolean shouldHideTopWidgets = (verticalWidget && mapActivity.getWidgetsVisibilityHelper().shouldHideVerticalWidgets());