Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delayed switch app profile #21380

Merged
merged 4 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions OsmAnd/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
- For wording and consistency, please note https://docs.osmand.net/docs/technical/contributions/translating-osmand
Thx - Hardy
-->

<string name="selected_delayed_profile">Selected profile \"%s\"</string>
<string name="shared_string_interpolation">Interpolation</string>
<string name="location_interpolation_percent">Location interpolation percentage</string>
<string name="location_interpolation_percent_desc">Set the percentage of location interpolation during route navigation. This parameter reduces the lag of your location position on the map during animation.</string>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package net.osmand.plus.quickaction.actions;

import static net.osmand.plus.OsmAndConstants.UI_HANDLER_MAP_CONTROLS;

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

import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
Expand All @@ -12,9 +15,11 @@
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.quickaction.QuickAction;
import net.osmand.plus.quickaction.QuickActionType;
import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.settings.backend.OsmandSettings;

public abstract class BaseSwitchAppModeAction extends QuickAction {
private static final long CHANGE_PROFILE_DELAY = 3500;

public BaseSwitchAppModeAction(QuickActionType type) {
super(type);
Expand All @@ -29,16 +34,48 @@ public BaseSwitchAppModeAction(QuickAction quickAction) {
@StringRes
public abstract int getQuickActionDescription();

@Override
public void execute(@NonNull MapActivity mapActivity) {
private static ApplicationMode delayedSwitchProfile;
private static Toast delayedSwitchProfileToast;

public void delayedSwitchAppMode(@NonNull MapActivity mapActivity) {
OsmandSettings settings = mapActivity.getMyApplication().getSettings();
if (shouldChangeForward()) {
settings.switchAppModeToNext();
} else {
settings.switchAppModeToPrevious();
ApplicationMode appMode = settings.getApplicationMode();
boolean next = shouldChangeForward();

if (delayedSwitchProfile == null) {
delayedSwitchProfile = appMode;
}
delayedSwitchProfile = settings.getSwitchedAppMode(delayedSwitchProfile, next);
cancelDelayedToast();

String patternDelayedSwitch = mapActivity.getString(R.string.selected_delayed_profile);
String messageDelayedSwitch = String.format(patternDelayedSwitch, delayedSwitchProfile.toHumanString());
delayedSwitchProfileToast = Toast.makeText(mapActivity, messageDelayedSwitch, Toast.LENGTH_SHORT);
delayedSwitchProfileToast.show();

mapActivity.getMyApplication().runMessageInUIThreadAndCancelPrevious(UI_HANDLER_MAP_CONTROLS + 1, () -> {
if (delayedSwitchProfile != null && appMode != delayedSwitchProfile && settings.setApplicationMode(delayedSwitchProfile)) {
cancelDelayedToast();
String pattern = mapActivity.getString(R.string.application_profile_changed);
String message = String.format(pattern, delayedSwitchProfile.toHumanString());
mapActivity.getMyApplication().showShortToastMessage(message);
}
delayedSwitchProfileToast = null;
delayedSwitchProfile = null;
}, CHANGE_PROFILE_DELAY);
}

private void cancelDelayedToast(){
if (delayedSwitchProfileToast != null) {
delayedSwitchProfileToast.cancel();
}
}

@Override
public void execute(@NonNull MapActivity mapActivity) {
delayedSwitchAppMode(mapActivity);
}

@Override
public void drawUI(@NonNull ViewGroup parent, @NonNull MapActivity mapActivity) {
View view = LayoutInflater.from(parent.getContext())
Expand Down
22 changes: 13 additions & 9 deletions OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -446,15 +446,7 @@ public boolean switchAppModeToPrevious() {

public boolean switchAppMode(boolean next) {
ApplicationMode appMode = getApplicationMode();
List<ApplicationMode> enabledModes = ApplicationMode.values(ctx);
int indexOfCurrent = enabledModes.indexOf(appMode);
int indexOfNext;
if (next) {
indexOfNext = indexOfCurrent < enabledModes.size() - 1 ? indexOfCurrent + 1 : 0;
} else {
indexOfNext = indexOfCurrent > 0 ? indexOfCurrent - 1 : enabledModes.size() - 1;
}
ApplicationMode nextAppMode = enabledModes.get(indexOfNext);
ApplicationMode nextAppMode = getSwitchedAppMode(appMode, next);
if (appMode != nextAppMode && setApplicationMode(nextAppMode)) {
String pattern = ctx.getString(R.string.application_profile_changed);
String message = String.format(pattern, nextAppMode.toHumanString());
Expand All @@ -464,6 +456,18 @@ public boolean switchAppMode(boolean next) {
return false;
}

public ApplicationMode getSwitchedAppMode(ApplicationMode selectedMode, boolean next) {
List<ApplicationMode> enabledModes = ApplicationMode.values(ctx);
int indexOfCurrent = enabledModes.indexOf(selectedMode);
int indexOfNext;
if (next) {
indexOfNext = indexOfCurrent < enabledModes.size() - 1 ? indexOfCurrent + 1 : 0;
} else {
indexOfNext = indexOfCurrent > 0 ? indexOfCurrent - 1 : enabledModes.size() - 1;
}
return enabledModes.get(indexOfNext);
}

public boolean setApplicationMode(ApplicationMode appMode) {
return setApplicationMode(appMode, true);
}
Expand Down
Loading