From c470f0113dd73181ce5175b4be85423bf15c6d64 Mon Sep 17 00:00:00 2001 From: Chumva Date: Tue, 1 Oct 2019 17:33:34 +0300 Subject: [PATCH] Add ability to change routing profile in default app modes --- .../src/net/osmand/plus/ApplicationMode.java | 81 +++++++++++++++---- .../src/net/osmand/plus/OsmandSettings.java | 3 + .../AppModesBottomSheetDialogFragment.java | 4 +- .../plus/profiles/EditProfileFragment.java | 76 +++++++++-------- .../profiles/SettingsProfileFragment.java | 4 +- 5 files changed, 113 insertions(+), 55 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/ApplicationMode.java b/OsmAnd/src/net/osmand/plus/ApplicationMode.java index 94a4ec5de3..f33121141e 100644 --- a/OsmAnd/src/net/osmand/plus/ApplicationMode.java +++ b/OsmAnd/src/net/osmand/plus/ApplicationMode.java @@ -350,6 +350,11 @@ public class ApplicationMode { return create(parent,-1, stringKey).userProfileTitle(userProfileTitle); } + public static ApplicationModeBuilder changeBaseMode(ApplicationMode applicationMode) { + ApplicationModeBuilder builder = new ApplicationModeBuilder(); + builder.applicationMode = applicationMode; + return builder; + } public static List values(OsmandApplication app) { if (customizationListener == null) { @@ -392,6 +397,16 @@ public class ApplicationMode { return defaultValues; } + public static List getCustomValues() { + List customModes = new ArrayList<>(); + for (ApplicationMode mode : values) { + if (mode.isCustomProfile()) { + customModes.add(mode); + } + } + return customModes; + } + // returns modifiable ! Set to exclude non-wanted derived public static Set regWidgetVisibility(String widgetId, ApplicationMode... am) { HashSet set = new HashSet<>(); @@ -607,6 +622,7 @@ public class ApplicationMode { public static void onApplicationStart(OsmandApplication app) { // load for default profiles to initialize later custom modes + initDefaultModesUpdates(app); initDefaultSpeed(app); initCustomModes(app); initDefaultSpeed(app); @@ -640,25 +656,60 @@ public class ApplicationMode { } + private static void initDefaultModesUpdates(OsmandApplication app) { + Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); + Type t = new TypeToken>() {}.getType(); + List defaultAppModeBeans = gson.fromJson(app.getSettings().DEFAULT_APP_PROFILES.get(), t); - private static void saveCustomModeToSettings(OsmandSettings settings){ - List customModes = new ArrayList<>(); - for (ApplicationMode mode : values) { - if (mode.parentAppMode != null) { - ApplicationModeBean mb = new ApplicationModeBean(); - mb.userProfileName = mode.userProfileName; - mb.iconColor = mode.iconColor; - mb.iconName = mode.iconResName; - mb.parent = mode.parentAppMode.getStringKey(); - mb.stringKey = mode.stringKey; - mb.routeService = mode.routeService; - mb.routingProfile = mode.routingProfile; - customModes.add(mb); + if (!Algorithms.isEmpty(defaultAppModeBeans)) { + for (ApplicationModeBean modeBean : defaultAppModeBeans) { + ApplicationMode applicationMode = ApplicationMode.valueOfStringKey(modeBean.stringKey, null); + if (applicationMode != null) { + if (!applicationMode.routingProfile.equals(modeBean.routingProfile)) { + applicationMode.routingProfile = modeBean.routingProfile; + } + if (!applicationMode.routeService.equals(modeBean.routeService)) { + applicationMode.routeService = modeBean.routeService; + } + } } } + } + + private static void saveCustomModeToSettings(OsmandSettings settings) { + List customModes = createApplicationModeBeans(getCustomValues()); + saveModesToSettings(settings, customModes, true); + } + + public static void saveDefaultModeToSettings(OsmandSettings settings) { + List defaultModes = createApplicationModeBeans(defaultValues); + saveModesToSettings(settings, defaultModes, false); + } + + private static List createApplicationModeBeans(List applicationModes) { + List modeBeans = new ArrayList<>(); + for (ApplicationMode mode : applicationModes) { + ApplicationModeBean mb = new ApplicationModeBean(); + mb.userProfileName = mode.userProfileName; + mb.iconColor = mode.iconColor; + mb.iconName = mode.iconResName; + mb.parent = mode.parentAppMode != null ? mode.parentAppMode.getStringKey() : null; + mb.stringKey = mode.stringKey; + mb.routeService = mode.routeService; + mb.routingProfile = mode.routingProfile; + modeBeans.add(mb); + } + return modeBeans; + } + + private static void saveModesToSettings(OsmandSettings settings, List modeBeans, boolean customModes) { Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); - String profiles = gson.toJson(customModes); - settings.CUSTOM_APP_PROFILES.set(profiles); + String profiles = gson.toJson(modeBeans); + if (customModes) { + settings.CUSTOM_APP_PROFILES.set(profiles); + } else { + settings.DEFAULT_APP_PROFILES.set(profiles); + } } public static ApplicationMode saveCustomProfile(ApplicationModeBuilder builder, OsmandApplication app) { diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index 870a77280d..89a1e709ba 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -2938,6 +2938,9 @@ public class OsmandSettings { RateUsBottomSheetDialog.RateUsState.INITIAL_STATE, RateUsBottomSheetDialog.RateUsState.values()) .makeGlobal(); + public final CommonPreference DEFAULT_APP_PROFILES = + new StringPreference("default_app_profiles", "").makeGlobal().cache(); + public final CommonPreference CUSTOM_APP_PROFILES = new StringPreference("custom_app_profiles", "").makeGlobal().cache(); diff --git a/OsmAnd/src/net/osmand/plus/profiles/AppModesBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/profiles/AppModesBottomSheetDialogFragment.java index 2254cd909f..3952016a87 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/AppModesBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/profiles/AppModesBottomSheetDialogFragment.java @@ -81,9 +81,7 @@ public abstract class AppModesBottomSheetDialogFragment