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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions OsmAnd/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- 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_engine">Engine</string>
<string name="vehicle_metrics_recording_description">Select the parameters to be recorded in the GPX file.</string>
<string name="auto_zoom_3d_angle">Auto zoom 3D angle</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 45 additions & 8 deletions OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ApplicationMode> 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) {
Copy link
Member

@Chumva Chumva Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably better to store this behaviour in quick action because we need it only there. Also, can we use something like app.runMessageInUIThreadAndCancelPrevious()?

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) {
Expand Down
Loading