From 9d918f6bab6af8bcb43493549f25a68d8cc56fb7 Mon Sep 17 00:00:00 2001 From: Chumva Date: Wed, 11 Dec 2019 16:04:22 +0200 Subject: [PATCH] Add ability to save and change default app modes --- .../src/net/osmand/plus/ApplicationMode.java | 105 ++++++++++++------ .../src/net/osmand/plus/OsmandSettings.java | 3 + .../src/net/osmand/plus/SettingsHelper.java | 2 +- .../AppModesBottomSheetDialogFragment.java | 4 +- .../plus/profiles/EditProfileFragment.java | 2 +- .../profiles/SettingsProfileFragment.java | 4 +- 6 files changed, 78 insertions(+), 42 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/ApplicationMode.java b/OsmAnd/src/net/osmand/plus/ApplicationMode.java index b157428939..5eaa3b107d 100644 --- a/OsmAnd/src/net/osmand/plus/ApplicationMode.java +++ b/OsmAnd/src/net/osmand/plus/ApplicationMode.java @@ -50,6 +50,7 @@ public class ApplicationMode { private static Map> widgetsAvailabilityMap = new LinkedHashMap<>(); private static List defaultValues = new ArrayList<>(); + private static List customValues = new ArrayList<>(); private static List values = new ArrayList<>(); private static List cachedFilteredValues = new ArrayList<>(); @@ -209,6 +210,7 @@ public class ApplicationMode { m.locationIconDayLost = m.parentAppMode.locationIconDayLost; m.locationIconNightLost = m.parentAppMode.locationIconNightLost; values.add(applicationMode); + customValues.add(applicationMode); return applicationMode; } @@ -354,6 +356,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) { @@ -396,6 +403,10 @@ public class ApplicationMode { return defaultValues; } + public static List getCustomValues() { + return customValues; + } + // returns modifiable ! Set to exclude non-wanted derived public static Set regWidgetVisibility(String widgetId, ApplicationMode... am) { HashSet set = new HashSet<>(); @@ -611,6 +622,7 @@ public class ApplicationMode { public static void onApplicationStart(OsmandApplication app) { // load for default profiles to initialize later custom modes + initDefaultModesParams(app); initDefaultSpeed(app); initCustomModes(app); initDefaultSpeed(app); @@ -651,6 +663,25 @@ public class ApplicationMode { return gson.toJson(mb); } + private static void initDefaultModesParams(OsmandApplication app) { + Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); + Type t = new TypeToken>() {}.getType(); + List defaultAppModeBeans = gson.fromJson(app.getSettings().DEFAULT_APP_PROFILES.get(), t); + + if (!Algorithms.isEmpty(defaultAppModeBeans)) { + for (ApplicationModeBean modeBean : defaultAppModeBeans) { + ApplicationMode applicationMode = ApplicationMode.valueOfStringKey(modeBean.stringKey, null); + if (applicationMode != null) { + applicationMode.userProfileName = modeBean.userProfileName; + applicationMode.iconResName = modeBean.iconName; + applicationMode.iconColor = modeBean.iconColor; + applicationMode.routingProfile = modeBean.routingProfile; + applicationMode.routeService = modeBean.routeService; + } + } + } + } + private static void initCustomModes(OsmandApplication app){ Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); Type t = new TypeToken>() {}.getType(); @@ -669,46 +700,51 @@ public class ApplicationMode { } - 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); - } - } + private static void saveAppModesToSettings(OsmandSettings settings, boolean saveCustomModes) { + List appModes = saveCustomModes ? customValues : defaultValues; + List modeBeans = createApplicationModeBeans(appModes); + Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); - String profiles = gson.toJson(customModes); - settings.CUSTOM_APP_PROFILES.set(profiles); + String profiles = gson.toJson(modeBeans); + if (saveCustomModes) { + settings.CUSTOM_APP_PROFILES.set(profiles); + } else { + settings.DEFAULT_APP_PROFILES.set(profiles); + } } - public static ApplicationMode saveCustomProfile(ApplicationModeBuilder builder, OsmandApplication app) { - ApplicationMode mode = null; - for(ApplicationMode m : values) { - if(m.stringKey.equals(builder.applicationMode.stringKey)) { - mode = m; - mode.iconResName = builder.applicationMode.iconResName; - mode.iconRes = builder.applicationMode.iconRes; - mode.userProfileName = builder.applicationMode.userProfileName; - mode.parentAppMode = builder.applicationMode.parentAppMode; - mode.routingProfile = builder.applicationMode.routingProfile; - mode.routeService = builder.applicationMode.routeService; - mode.iconColor = builder.applicationMode.iconColor; - break; - } + 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.getStringKey(); + mb.stringKey = mode.stringKey; + mb.routeService = mode.routeService; + mb.routingProfile = mode.routingProfile; + modeBeans.add(mb); } - if(mode == null) { + + return modeBeans; + } + + public static ApplicationMode saveProfile(ApplicationModeBuilder builder, OsmandApplication app) { + ApplicationMode mode = ApplicationMode.valueOfStringKey(builder.applicationMode.stringKey, null); + if (mode != null) { + mode.iconResName = builder.applicationMode.iconResName; + mode.iconRes = builder.applicationMode.iconRes; + mode.userProfileName = builder.applicationMode.userProfileName; + mode.parentAppMode = builder.applicationMode.parentAppMode; + mode.routingProfile = builder.applicationMode.routingProfile; + mode.routeService = builder.applicationMode.routeService; + mode.iconColor = builder.applicationMode.iconColor; + } else { mode = builder.customReg(); initRegVisibility(); } - saveCustomModeToSettings(app.getSettings()); + saveAppModesToSettings(app.getSettings(), mode.isCustomProfile()); return mode; } @@ -721,8 +757,9 @@ public class ApplicationMode { it.remove(); } } + customValues.remove(md); cachedFilteredValues.remove(md); - saveCustomModeToSettings(app.getSettings()); + saveAppModesToSettings(app.getSettings(), md.isCustomProfile()); } public static boolean changeProfileAvailability(ApplicationMode mode, boolean isSelected, OsmandApplication app) { diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index 53f33b7afe..454f11ac4a 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -3224,6 +3224,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/SettingsHelper.java b/OsmAnd/src/net/osmand/plus/SettingsHelper.java index 806231eac9..c95a8d5312 100644 --- a/OsmAnd/src/net/osmand/plus/SettingsHelper.java +++ b/OsmAnd/src/net/osmand/plus/SettingsHelper.java @@ -453,7 +453,7 @@ public class SettingsHelper { @Override public void apply() { if (appMode.isCustomProfile()) { - appMode = ApplicationMode.saveCustomProfile(builder, getSettings().getContext()); + appMode = ApplicationMode.saveProfile(builder, getSettings().getContext()); } } 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