From 9694df8b596eef3fced30c841cbb610a3c39bf79 Mon Sep 17 00:00:00 2001
From: 0xRe1nk0 <0xre1nk0@gmail.com>
Date: Tue, 19 Nov 2024 11:37:49 +0200
Subject: [PATCH] delayed switch app profile
---
OsmAnd/res/values/strings.xml | 1 +
.../actions/BaseSwitchAppModeAction.java | 6 +--
.../plus/settings/backend/OsmandSettings.java | 53 ++++++++++++++++---
3 files changed, 47 insertions(+), 13 deletions(-)
diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml
index db81fde5204..b4f7f52e53b 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
-->
+ Selected profile \"%s\"
Engine
Select the parameters to be recorded in the GPX file.
Auto zoom 3D angle
diff --git a/OsmAnd/src/net/osmand/plus/quickaction/actions/BaseSwitchAppModeAction.java b/OsmAnd/src/net/osmand/plus/quickaction/actions/BaseSwitchAppModeAction.java
index 9a88dbe0440..7c994ce4187 100644
--- a/OsmAnd/src/net/osmand/plus/quickaction/actions/BaseSwitchAppModeAction.java
+++ b/OsmAnd/src/net/osmand/plus/quickaction/actions/BaseSwitchAppModeAction.java
@@ -32,11 +32,7 @@ public BaseSwitchAppModeAction(QuickAction quickAction) {
@Override
public void execute(@NonNull MapActivity mapActivity) {
OsmandSettings settings = mapActivity.getMyApplication().getSettings();
- if (shouldChangeForward()) {
- settings.switchAppModeToNext();
- } else {
- settings.switchAppModeToPrevious();
- }
+ settings.delayedSwitchAppMode(shouldChangeForward());
}
@Override
diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java
index 2740eedd197..1d2fde85ff8 100644
--- a/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java
+++ b/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java
@@ -35,7 +35,9 @@
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Environment;
+import android.os.Handler;
import android.text.TextUtils;
+import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@@ -446,22 +448,57 @@ public boolean switchAppModeToPrevious() {
public boolean switchAppMode(boolean next) {
ApplicationMode appMode = getApplicationMode();
+ 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());
+ ctx.showShortToastMessage(message);
+ return true;
+ }
+ return false;
+ }
+
+ public ApplicationMode getSwitchedAppMode(ApplicationMode selectedMode, boolean next) {
List enabledModes = ApplicationMode.values(ctx);
- int indexOfCurrent = enabledModes.indexOf(appMode);
+ 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;
}
- ApplicationMode nextAppMode = enabledModes.get(indexOfNext);
- if (appMode != nextAppMode && setApplicationMode(nextAppMode)) {
- String pattern = ctx.getString(R.string.application_profile_changed);
- String message = String.format(pattern, nextAppMode.toHumanString());
- ctx.showShortToastMessage(message);
- return true;
+ return enabledModes.get(indexOfNext);
+ }
+
+ private final Handler delayedSwitchProfileHandler = new Handler();
+ private ApplicationMode delayedSwitchProfile;
+ private Toast delayedSwitchProfileToast;
+
+ public void delayedSwitchAppMode(boolean next) {
+ ApplicationMode appMode = getApplicationMode();
+
+ if (delayedSwitchProfile == null) {
+ delayedSwitchProfile = getApplicationMode();
}
- return false;
+ delayedSwitchProfile = getSwitchedAppMode(delayedSwitchProfile, next);
+
+ if (delayedSwitchProfileToast != null) {
+ delayedSwitchProfileToast.cancel();
+ }
+ String patternDelayedSwitch = ctx.getString(R.string.selected_delayed_profile);
+ String messageDelayedSwitch = String.format(patternDelayedSwitch, delayedSwitchProfile.toHumanString());
+ delayedSwitchProfileToast = Toast.makeText(ctx, messageDelayedSwitch, Toast.LENGTH_SHORT);
+ delayedSwitchProfileToast.show();
+
+ delayedSwitchProfileHandler.removeCallbacksAndMessages(null);
+ delayedSwitchProfileHandler.postDelayed(() -> {
+ if (appMode != delayedSwitchProfile && setApplicationMode(delayedSwitchProfile)) {
+ String pattern = ctx.getString(R.string.application_profile_changed);
+ String message = String.format(pattern, delayedSwitchProfile.toHumanString());
+ ctx.showShortToastMessage(message);
+ }
+ delayedSwitchProfile = null;
+ }, 3500);
}
public boolean setApplicationMode(ApplicationMode appMode) {