Skip to content

Commit

Permalink
Merge pull request #20919 from osmandapp/QuickActionsForTripRecording
Browse files Browse the repository at this point in the history
Implement quick actions for Trip Recording (issue #20800)
  • Loading branch information
Chumva authored Sep 30, 2024
2 parents 6235ce6 + 98bab66 commit 414380b
Show file tree
Hide file tree
Showing 11 changed files with 373 additions and 53 deletions.
10 changes: 10 additions & 0 deletions OsmAnd/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
- For wording and consistency, please note https://docs.osmand.net/docs/technical/contributions/translating-osmand
Thx - Hardy
-->
<string name="track_does_not_contain_data_to_save">Track doesn\'t contain data to save.</string>
<string name="quick_action_finish_trip_recording_summary">A button to save the GPX track and finish trip recording.</string>
<string name="start_trip_recording_first_m">Please start trip recording first.</string>
<string name="new_segment_started_m">Started recording a new track segment.</string>
<string name="quick_action_save_recorded_trip_and_continue_summary">A button to save the current recorded trip as a GPX file and continue recording a new track without interruption.</string>
<string name="quick_action_save_recorded_trip_and_continue">Recorded Trip &amp; Continue</string>
<string name="quick_action_new_trip_segment">A button to start a new segment in the ongoing GPX track recording.</string>
<string name="new_trip_segment">New trip segment</string>
<string name="quick_action_start_pause_recording">A button to start, pause or resume recording of the GPX track.</string>
<string name="quick_action_verb_start_pause">Start / Pause</string>
<string name="help_article_personal_color_palette_schemes_name">Color Schemes</string>
<string name="movies_list">Movies list</string>
<string name="third_links">Third Links</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
import net.osmand.Location;
import net.osmand.PlatformUtil;
import net.osmand.data.ValueHolder;
import net.osmand.plus.plugins.monitoring.actions.FinishTripRecordingAction;
import net.osmand.plus.plugins.monitoring.actions.SaveRecordedTripAndContinueAction;
import net.osmand.plus.plugins.monitoring.actions.StartNewTripSegmentAction;
import net.osmand.plus.plugins.monitoring.actions.TripRecordingAction;
import net.osmand.plus.quickaction.QuickActionType;
import net.osmand.shared.gpx.GpxFile;
import net.osmand.plus.NavigationService;
import net.osmand.plus.OsmAndTaskManager.OsmAndTaskRunnable;
Expand Down Expand Up @@ -229,17 +234,9 @@ public SelectedGpxFile getCurrentTrack() {
return app.getSavingTrackHelper().getCurrentTrack();
}

public boolean wasTrackMonitored() {
return settings.SAVE_GLOBAL_TRACK_TO_GPX.get();
}

public boolean hasDataToSave() {
return app.getSavingTrackHelper().hasDataToSave();
}

public void askShowTripRecordingDialog(@NonNull FragmentActivity activity) {
FragmentManager fragmentManager = activity.getSupportFragmentManager();
if (hasDataToSave() || wasTrackMonitored()) {
if (hasDataToSave() || isRecordingTrack()) {
TripRecordingBottomSheet.showInstance(fragmentManager);
} else {
askStartRecording(activity);
Expand All @@ -263,14 +260,23 @@ private void askStartRecordingStep2(@NonNull FragmentActivity activity) {

public void startRecording(@Nullable FragmentActivity activity) {
app.getSavingTrackHelper().startNewSegment();
app.getSettings().SAVE_GLOBAL_TRACK_TO_GPX.set(true);
setRecordingTrack(true);
app.startNavigationService(NavigationService.USED_BY_GPX);

if (activity != null) {
AndroidUtils.requestNotificationPermissionIfNeeded(activity);
}
}

public boolean finishRecording() {
if (mapActivity != null && hasDataToSave()) {
saveCurrentTrack(null, mapActivity);
app.getNotificationHelper().refreshNotifications();
return true;
}
return false;
}

public void saveCurrentTrack() {
saveCurrentTrack(null, null, true, false);
}
Expand Down Expand Up @@ -374,12 +380,28 @@ public void updateWidgets() {
}
}

public void pauseOrResumeRecording() {
if (isRecordingTrack()) {
setRecordingTrack(false);
NavigationService navigationService = app.getNavigationService();
if (navigationService != null) {
navigationService.stopIfNeeded(app, NavigationService.USED_BY_GPX);
}
} else {
setRecordingTrack(true);
app.startNavigationService(NavigationService.USED_BY_GPX);
if (mapActivity != null) {
AndroidUtils.requestNotificationPermissionIfNeeded(mapActivity);
}
}
}

public void stopRecording() {
stopRecording(false);
}

public void stopRecording(boolean clearData) {
settings.SAVE_GLOBAL_TRACK_TO_GPX.set(false);
setRecordingTrack(false);
app.getSavingTrackHelper().onStopRecording(clearData);
if (app.getNavigationService() != null) {
app.getNavigationService().stopIfNeeded(app, NavigationService.USED_BY_GPX);
Expand All @@ -390,6 +412,18 @@ public boolean isSaving() {
return isSaving;
}

public void setRecordingTrack(boolean recording) {
settings.SAVE_GLOBAL_TRACK_TO_GPX.set(recording);
}

public boolean isRecordingTrack() {
return settings.SAVE_GLOBAL_TRACK_TO_GPX.get();
}

public boolean hasDataToSave() {
return app.getSavingTrackHelper().hasDataToSave();
}

public boolean isLiveMonitoringEnabled() {
return liveMonitoringHelper.isLiveMonitoringEnabled();
}
Expand All @@ -404,8 +438,8 @@ public void startGPXMonitoring(@Nullable FragmentActivity activity) {
if (choice.value || activity == null) {
Runnable runnable = () -> {
app.getSavingTrackHelper().startNewSegment();
setRecordingTrack(true);
settings.SAVE_GLOBAL_TRACK_INTERVAL.set(vs.value);
settings.SAVE_GLOBAL_TRACK_TO_GPX.set(true);
settings.SAVE_GLOBAL_TRACK_REMEMBER.set(choice.value);

if (activity != null) {
Expand All @@ -423,4 +457,14 @@ public void startGPXMonitoring(@Nullable FragmentActivity activity) {
public DashFragmentData getCardFragment() {
return DashTrackFragment.FRAGMENT_DATA;
}

@Override
protected List<QuickActionType> getQuickActionTypes() {
List<QuickActionType> quickActionTypes = new ArrayList<>();
quickActionTypes.add(TripRecordingAction.TYPE);
quickActionTypes.add(StartNewTripSegmentAction.TYPE);
quickActionTypes.add(SaveRecordedTripAndContinueAction.TYPE);
quickActionTypes.add(FinishTripRecordingAction.TYPE);
return quickActionTypes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,20 +217,8 @@ private void setupResumePauseButton(View container) {
blockStatisticsBuilder.runUpdatingStatBlocksIfNeeded();
handler.startChartUpdatesIfNotRunning();
}
if (isRecordingTrack) {
settings.SAVE_GLOBAL_TRACK_TO_GPX.set(false);
NavigationService navigationService = app.getNavigationService();
if (navigationService != null) {
navigationService.stopIfNeeded(app, NavigationService.USED_BY_GPX);
}
} else {
app.getSettings().SAVE_GLOBAL_TRACK_TO_GPX.set(true);
app.startNavigationService(NavigationService.USED_BY_GPX);

FragmentActivity activity = getMapActivity();
if (activity != null) {
AndroidUtils.requestNotificationPermissionIfNeeded(activity);
}
if (plugin != null) {
plugin.pauseOrResumeRecording();
}
updateStatus();
createItem(resumePauseButton, !isRecordingTrack ? ItemType.PAUSE : ItemType.RESUME);
Expand All @@ -241,10 +229,7 @@ private void setupFinishButton(View container) {
CardView finishButton = container.findViewById(R.id.button_center_right);
createItem(finishButton, ItemType.FINISH);
finishButton.setOnClickListener(v -> {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null && plugin != null && hasDataToSave()) {
plugin.saveCurrentTrack(null, mapActivity);
app.getNotificationHelper().refreshNotifications();
if (plugin != null && plugin.finishRecording()) {
dismiss();
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,9 @@ public void createMenuItems(Bundle savedInstanceState) {

items.add(new BaseBottomSheetItem.Builder()
.setCustomView(buttonClear)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (fragmentManager != null && hasDataToSave()) {
TripRecordingClearDataBottomSheet.showInstance(fragmentManager, TripRecordingOptionsBottomSheet.this);
}
.setOnClickListener(v -> {
if (fragmentManager != null && hasDataToSave()) {
TripRecordingClearDataBottomSheet.showInstance(fragmentManager, TripRecordingOptionsBottomSheet.this);
}
})
.create());
Expand All @@ -117,12 +114,9 @@ public void onClick(View v) {

items.add(new BaseBottomSheetItem.Builder()
.setCustomView(buttonDiscard)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (fragmentManager != null) {
TripRecordingDiscardBottomSheet.showInstance(fragmentManager, TripRecordingOptionsBottomSheet.this);
}
.setOnClickListener(v -> {
if (fragmentManager != null) {
TripRecordingDiscardBottomSheet.showInstance(fragmentManager, TripRecordingOptionsBottomSheet.this);
}
})
.create());
Expand All @@ -131,13 +125,10 @@ public void onClick(View v) {

items.add(new BaseBottomSheetItem.Builder()
.setCustomView(buttonOnline)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean wasOnlineMonitored = !settings.LIVE_MONITORING.get();
settings.LIVE_MONITORING.set(wasOnlineMonitored);
createItem(buttonOnline, wasOnlineMonitored ? ItemType.STOP_ONLINE : ItemType.START_ONLINE);
}
.setOnClickListener(v -> {
boolean wasOnlineMonitored = !settings.LIVE_MONITORING.get();
settings.LIVE_MONITORING.set(wasOnlineMonitored);
createItem(buttonOnline, wasOnlineMonitored ? ItemType.STOP_ONLINE : ItemType.START_ONLINE);
})
.create());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package net.osmand.plus.plugins.monitoring.actions;

import androidx.annotation.Nullable;

import net.osmand.plus.plugins.PluginsHelper;
import net.osmand.plus.plugins.monitoring.OsmandMonitoringPlugin;
import net.osmand.plus.quickaction.QuickAction;
import net.osmand.plus.quickaction.QuickActionType;

abstract class BaseMonitoringAction extends QuickAction {

public BaseMonitoringAction(QuickActionType actionType) {
super(actionType);
}

public BaseMonitoringAction(QuickAction quickAction) {
super(quickAction);
}

protected boolean isRecordingTrack() {
OsmandMonitoringPlugin plugin = getPlugin();
return plugin != null && plugin.isRecordingTrack();
}

protected boolean hasDataToSave() {
OsmandMonitoringPlugin plugin = getPlugin();
return plugin != null && plugin.hasDataToSave();
}

@Nullable
protected OsmandMonitoringPlugin getPlugin() {
return PluginsHelper.getPlugin(OsmandMonitoringPlugin.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package net.osmand.plus.plugins.monitoring.actions;

import static net.osmand.plus.quickaction.QuickActionIds.FINISH_TRIP_RECORDING_ACTION;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.plugins.monitoring.OsmandMonitoringPlugin;
import net.osmand.plus.quickaction.QuickAction;
import net.osmand.plus.quickaction.QuickActionType;

public class FinishTripRecordingAction extends BaseMonitoringAction {

public static final QuickActionType TYPE = new QuickActionType(FINISH_TRIP_RECORDING_ACTION,
"finish.trip.recording", FinishTripRecordingAction.class)
.nameRes(R.string.shared_string_trip_recording)
.iconRes(R.drawable.ic_action_trip_rec_finish)
.nonEditable()
.category(QuickActionType.MY_PLACES)
.nameActionRes(R.string.shared_string_finish);

public FinishTripRecordingAction() {
super(TYPE);
}

public FinishTripRecordingAction(QuickAction quickAction) {
super(quickAction);
}

@Override
public void execute(@NonNull MapActivity mapActivity) {
OsmandMonitoringPlugin plugin = getPlugin();
if (plugin != null) {
OsmandApplication app = mapActivity.getMyApplication();
if (!isRecordingTrack()) {
app.showToastMessage(R.string.start_trip_recording_first_m);
} else if (!hasDataToSave()) {
app.showToastMessage(R.string.track_does_not_contain_data_to_save);
} else {
plugin.finishRecording();
}
}
}

@Override
public void drawUI(@NonNull ViewGroup parent, @NonNull MapActivity mapActivity) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.quick_action_with_text, parent, false);
((TextView) view.findViewById(R.id.text)).setText(R.string.quick_action_finish_trip_recording_summary);
parent.addView(view);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package net.osmand.plus.plugins.monitoring.actions;

import static net.osmand.plus.quickaction.QuickActionIds.SAVE_RECORDED_TRIP_AND_CONTINUE_ACTION;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.plugins.monitoring.OsmandMonitoringPlugin;
import net.osmand.plus.quickaction.QuickAction;
import net.osmand.plus.quickaction.QuickActionType;
import net.osmand.plus.track.helpers.save.SaveGpxHelper;
import net.osmand.shared.gpx.GpxFile;

public class SaveRecordedTripAndContinueAction extends BaseMonitoringAction {

public static final QuickActionType TYPE = new QuickActionType(SAVE_RECORDED_TRIP_AND_CONTINUE_ACTION,
"save.trip.and.continue", SaveRecordedTripAndContinueAction.class)
.nameRes(R.string.quick_action_save_recorded_trip_and_continue)
.iconRes(R.drawable.ic_action_trip_rec_save)
.nonEditable()
.category(QuickActionType.MY_PLACES)
.nameActionRes(R.string.shared_string_save);

public SaveRecordedTripAndContinueAction() {
super(TYPE);
}

public SaveRecordedTripAndContinueAction(QuickAction quickAction) {
super(quickAction);
}

@Override
public void execute(@NonNull MapActivity mapActivity) {
OsmandMonitoringPlugin plugin = getPlugin();
if (plugin != null) {
OsmandApplication app = mapActivity.getMyApplication();
if (!isRecordingTrack()) {
app.showToastMessage(R.string.start_trip_recording_first_m);
} else if (!hasDataToSave()) {
app.showToastMessage(R.string.track_does_not_contain_data_to_save);
} else {
GpxFile gpxFile = app.getSavingTrackHelper().getCurrentTrack().getGpxFile();
SaveGpxHelper.saveCurrentTrack(app, gpxFile, errorMessage -> {
plugin.saveCurrentTrack(null, mapActivity, false, true);
});
}
}
}

@Override
public void drawUI(@NonNull ViewGroup parent, @NonNull MapActivity mapActivity) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.quick_action_with_text, parent, false);
((TextView) view.findViewById(R.id.text)).setText(R.string.quick_action_save_recorded_trip_and_continue_summary);
parent.addView(view);
}
}
Loading

0 comments on commit 414380b

Please sign in to comment.