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 1/3] 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) {
From 2c9009c803851bcf66aacaeaebc04019788e7776 Mon Sep 17 00:00:00 2001
From: 0xRe1nk0 <0xre1nk0@gmail.com>
Date: Mon, 25 Nov 2024 06:45:28 +0200
Subject: [PATCH 2/3] code review fix
---
.../actions/BaseSwitchAppModeAction.java | 45 ++++++++++++++++++-
.../plus/settings/backend/OsmandSettings.java | 31 -------------
2 files changed, 43 insertions(+), 33 deletions(-)
diff --git a/OsmAnd/src/net/osmand/plus/quickaction/actions/BaseSwitchAppModeAction.java b/OsmAnd/src/net/osmand/plus/quickaction/actions/BaseSwitchAppModeAction.java
index 7c994ce4187..21dd877eb3e 100644
--- a/OsmAnd/src/net/osmand/plus/quickaction/actions/BaseSwitchAppModeAction.java
+++ b/OsmAnd/src/net/osmand/plus/quickaction/actions/BaseSwitchAppModeAction.java
@@ -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;
@@ -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);
@@ -29,10 +34,46 @@ public BaseSwitchAppModeAction(QuickAction quickAction) {
@StringRes
public abstract int getQuickActionDescription();
+ private static ApplicationMode delayedSwitchProfile;
+ private static Toast delayedSwitchProfileToast;
+
+ public void delayedSwitchAppMode(@NonNull MapActivity mapActivity) {
+ OsmandSettings settings = mapActivity.getMyApplication().getSettings();
+ 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) {
- OsmandSettings settings = mapActivity.getMyApplication().getSettings();
- settings.delayedSwitchAppMode(shouldChangeForward());
+ delayedSwitchAppMode(mapActivity);
}
@Override
diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java
index 1d2fde85ff8..84404ad7104 100644
--- a/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java
+++ b/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java
@@ -470,37 +470,6 @@ public ApplicationMode getSwitchedAppMode(ApplicationMode selectedMode, boolean
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();
- }
- 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) {
return setApplicationMode(appMode, true);
}
From 3990507acaf6185987cb8751559eb16df950e918 Mon Sep 17 00:00:00 2001
From: 0xRe1nk0 <0xre1nk0@gmail.com>
Date: Mon, 25 Nov 2024 06:46:35 +0200
Subject: [PATCH 3/3] code cleanup
---
OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java | 2 --
1 file changed, 2 deletions(-)
diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java
index 84404ad7104..b0c646bfc2e 100644
--- a/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java
+++ b/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java
@@ -35,9 +35,7 @@
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;