Skip to content

Commit

Permalink
Merge pull request #20931 from osmandapp/reset_average_speed
Browse files Browse the repository at this point in the history
Reset average speed
  • Loading branch information
Chumva committed Sep 27, 2024
2 parents 895fe35 + a5f7a15 commit ec7f086
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 5 deletions.
19 changes: 19 additions & 0 deletions OsmAnd/res/layout/setting_action_button.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:minHeight="@dimen/bottom_sheet_list_item_height">

<TextView
android:id="@+id/action_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginHorizontal="@dimen/content_padding"
android:textColor="?attr/active_color_basic"
android:textSize="@dimen/default_list_text_size"
tools:text="Action" />

</FrameLayout>
7 changes: 4 additions & 3 deletions OsmAnd/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
- For wording and consistency, please note https://docs.osmand.net/docs/technical/contributions/translating-osmand
Thx - Hardy
-->
<string name="unknown_bt_device">Unknown device</string>
<string name="obd_vin">VIN</string>
<string name="obd_vin_desc">VIN code over OBD</string>
<string name="reset_average_speed">Reset average speed</string>
<string name="unknown_bt_device">Unknown device</string>
<string name="obd_vin">VIN</string>
<string name="obd_vin_desc">VIN code over OBD</string>
<string name="unit_volt">V</string>
<string name="wikimedia">Wikimedia</string>
<string name="open_in_browser">Open in browser</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ public static int getConvertedSpeedToSkip(@NonNull SpeedConstants speedSystem) {
}
}

public void resetLocations() {
locations.clear();
}

private static class SegmentsList {

private final Segment[] segments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -77,6 +83,26 @@ private void updateAverageSpeed() {
}
}

@Nullable
@Override
protected List<PopUpMenuItem> getWidgetActions() {
List<PopUpMenuItem> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ public void showContextWidgetMenu(@NonNull View view) {
.setOnClickListener(item -> ConfigureWidgetsFragment.showInstance(mapActivity, widgetInfo.getWidgetPanel(), appMode, widgetId, true))
.create());

List<PopUpMenuItem> 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)
Expand All @@ -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());
}

Expand Down Expand Up @@ -261,6 +266,11 @@ public void showContextWidgetMenu(@NonNull View view) {
}
}

@Nullable
protected List<PopUpMenuItem> getWidgetActions() {
return null;
}

@Override
public final void updateInfo(@Nullable OsmandMapLayer.DrawSettings drawSettings) {
boolean shouldHideTopWidgets = (verticalWidget && mapActivity.getWidgetsVisibilityHelper().shouldHideVerticalWidgets());
Expand Down

0 comments on commit ec7f086

Please sign in to comment.