diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index 5822b8b7fe..ca2bf8a2d1 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -13,6 +13,7 @@ import android.net.NetworkInfo; import android.os.Build; import android.os.Environment; import android.support.annotation.DrawableRes; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.annotation.StringRes; import android.support.v4.util.Pair; @@ -149,7 +150,6 @@ public class OsmandSettings { /// Settings variables private final OsmandApplication ctx; - private PreferencesDataStore dataStore; private SettingsAPI settingsAPI; private Object defaultProfilePreferences; private Object globalPreferences; @@ -166,7 +166,6 @@ public class OsmandSettings { protected OsmandSettings(OsmandApplication clientContext, SettingsAPI settinsAPI) { ctx = clientContext; this.settingsAPI = settinsAPI; - dataStore = new PreferencesDataStore(); initPrefs(); } @@ -174,7 +173,6 @@ public class OsmandSettings { ctx = clientContext; this.settingsAPI = settinsAPI; CUSTOM_SHARED_PREFERENCES_NAME = CUSTOM_SHARED_PREFERENCES_PREFIX + sharedPreferencesName; - dataStore = new PreferencesDataStore(); initPrefs(); setCustomized(); } @@ -210,8 +208,8 @@ public class OsmandSettings { return settingsAPI; } - public PreferencesDataStore getDataStore() { - return dataStore; + public PreferencesDataStore getDataStore(@Nullable ApplicationMode appMode) { + return new PreferencesDataStore(appMode != null ? appMode : APPLICATION_MODE.get()); } public static String getSharedPreferencesName(ApplicationMode mode) { @@ -3507,38 +3505,44 @@ public class OsmandSettings { public class PreferencesDataStore extends PreferenceDataStore { + private ApplicationMode appMode; + + public PreferencesDataStore(@NonNull ApplicationMode appMode) { + this.appMode = appMode; + } + @Override public void putString(String key, @Nullable String value) { - setPreference(key, value); + setPreference(key, value, appMode); } @Override public void putStringSet(String key, @Nullable Set values) { - setPreference(key, values); + setPreference(key, values, appMode); } @Override public void putInt(String key, int value) { - setPreference(key, value); + setPreference(key, value, appMode); } @Override public void putLong(String key, long value) { - setPreference(key, value); + setPreference(key, value, appMode); } @Override public void putFloat(String key, float value) { - setPreference(key, value); + setPreference(key, value, appMode); } @Override public void putBoolean(String key, boolean value) { - setPreference(key, value); + setPreference(key, value, appMode); } public void putValue(String key, Object value) { - setPreference(key, value); + setPreference(key, value, appMode); } @Nullable @@ -3546,9 +3550,9 @@ public class OsmandSettings { public String getString(String key, @Nullable String defValue) { OsmandPreference preference = getPreference(key); if (preference instanceof StringPreference) { - return ((StringPreference) preference).get(); + return ((StringPreference) preference).getModeValue(appMode); } else { - Object value = preference.get(); + Object value = preference.getModeValue(appMode); if (value != null) { return value.toString(); } @@ -3566,7 +3570,7 @@ public class OsmandSettings { public int getInt(String key, int defValue) { OsmandPreference preference = getPreference(key); if (preference instanceof IntPreference) { - return ((IntPreference) preference).get(); + return ((IntPreference) preference).getModeValue(appMode); } return defValue; } @@ -3575,7 +3579,7 @@ public class OsmandSettings { public long getLong(String key, long defValue) { OsmandPreference preference = getPreference(key); if (preference instanceof LongPreference) { - return ((LongPreference) preference).get(); + return ((LongPreference) preference).getModeValue(appMode); } return defValue; } @@ -3584,7 +3588,7 @@ public class OsmandSettings { public float getFloat(String key, float defValue) { OsmandPreference preference = getPreference(key); if (preference instanceof FloatPreference) { - return ((FloatPreference) preference).get(); + return ((FloatPreference) preference).getModeValue(appMode); } return defValue; } @@ -3593,7 +3597,7 @@ public class OsmandSettings { public boolean getBoolean(String key, boolean defValue) { OsmandPreference preference = getPreference(key); if (preference instanceof BooleanPreference) { - return ((BooleanPreference) preference).get(); + return ((BooleanPreference) preference).getModeValue(appMode); } return defValue; } @@ -3602,7 +3606,7 @@ public class OsmandSettings { public Object getValue(String key, Object defValue) { OsmandPreference preference = getPreference(key); if (preference != null) { - return preference.get(); + return preference.getModeValue(appMode); } return defValue; } diff --git a/OsmAnd/src/net/osmand/plus/profiles/SelectAppModesBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/profiles/SelectAppModesBottomSheetDialogFragment.java index d145fe8167..f4b8be1cbc 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/SelectAppModesBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/profiles/SelectAppModesBottomSheetDialogFragment.java @@ -1,6 +1,8 @@ package net.osmand.plus.profiles; +import android.os.Bundle; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; @@ -18,9 +20,26 @@ public class SelectAppModesBottomSheetDialogFragment extends AppModesBottomSheet public static final String TAG = "SelectAppModesBottomSheetDialogFragment"; + private static final String APP_MODE_KEY = "app_mode_key"; + private static final String APP_MODE_CHANGEABLE_KEY = "app_mode_changeable_key"; + private static final Log LOG = PlatformUtil.getLog(SelectAppModesBottomSheetDialogFragment.class); private List activeModes = new ArrayList<>(); + private ApplicationMode appMode; + private boolean appModeChangeable; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (savedInstanceState != null) { + appMode = ApplicationMode.valueOfStringKey(savedInstanceState.getString(APP_MODE_KEY), null); + appModeChangeable = savedInstanceState.getBoolean(APP_MODE_CHANGEABLE_KEY); + } + if (appMode == null) { + appMode = requiredMyApplication().getSettings().getApplicationMode(); + } + } @Override public void onResume() { @@ -30,6 +49,22 @@ public class SelectAppModesBottomSheetDialogFragment extends AppModesBottomSheet setupHeightAndBackground(getView()); } + public ApplicationMode getAppMode() { + return appMode; + } + + public void setAppMode(ApplicationMode appMode) { + this.appMode = appMode; + } + + public boolean isAppModeChangeable() { + return appModeChangeable; + } + + public void setAppModeChangeable(boolean appModeChangeable) { + this.appModeChangeable = appModeChangeable; + } + @Override protected void getData() { activeModes.addAll(ApplicationMode.values(getMyApplication())); @@ -37,7 +72,7 @@ public class SelectAppModesBottomSheetDialogFragment extends AppModesBottomSheet @Override protected SelectProfileMenuAdapter getMenuAdapter() { - return new SelectProfileMenuAdapter(activeModes, getMyApplication(), getString(R.string.shared_string_manage), nightMode); + return new SelectProfileMenuAdapter(activeModes, requiredMyApplication(), getString(R.string.shared_string_manage), nightMode, appMode); } @Override @@ -48,9 +83,10 @@ public class SelectAppModesBottomSheetDialogFragment extends AppModesBottomSheet @Override public void onProfilePressed(ApplicationMode appMode) { OsmandSettings settings = getMyApplication().getSettings(); - if (appMode != settings.APPLICATION_MODE.get()) { - settings.APPLICATION_MODE.set(appMode); - + if (appMode != this.appMode) { + if (appModeChangeable) { + settings.APPLICATION_MODE.set(appMode); + } Fragment targetFragment = getTargetFragment(); if (targetFragment instanceof AppModeChangedListener) { AppModeChangedListener listener = (AppModeChangedListener) targetFragment; @@ -60,12 +96,24 @@ public class SelectAppModesBottomSheetDialogFragment extends AppModesBottomSheet dismiss(); } - public static void showInstance(@NonNull FragmentManager fm, Fragment target, boolean usedOnMap) { + @Override + public void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + if (appMode != null) { + outState.putString(APP_MODE_KEY, appMode.getStringKey()); + } + outState.putBoolean(APP_MODE_CHANGEABLE_KEY, appModeChangeable); + } + + public static void showInstance(@NonNull FragmentManager fm, Fragment target, boolean usedOnMap, + @Nullable ApplicationMode appMode, boolean appModeChangeable) { try { if (fm.findFragmentByTag(SelectAppModesBottomSheetDialogFragment.TAG) == null) { SelectAppModesBottomSheetDialogFragment fragment = new SelectAppModesBottomSheetDialogFragment(); fragment.setTargetFragment(target, 0); fragment.setUsedOnMap(usedOnMap); + fragment.setAppMode(appMode); + fragment.setAppModeChangeable(appModeChangeable); fragment.show(fm, SelectAppModesBottomSheetDialogFragment.TAG); } } catch (RuntimeException e) { diff --git a/OsmAnd/src/net/osmand/plus/profiles/SelectProfileMenuAdapter.java b/OsmAnd/src/net/osmand/plus/profiles/SelectProfileMenuAdapter.java index 601c940e0a..c5ed1cfc2b 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/SelectProfileMenuAdapter.java +++ b/OsmAnd/src/net/osmand/plus/profiles/SelectProfileMenuAdapter.java @@ -31,8 +31,8 @@ public class SelectProfileMenuAdapter extends AbstractProfileMenuAdapter items = new ArrayList<>(); - @Nullable private final OsmandApplication app; + private ApplicationMode appMode; @ColorRes private int selectedIconColorRes; private boolean bottomButton; @@ -41,13 +41,15 @@ public class SelectProfileMenuAdapter extends AbstractProfileMenuAdapter items, OsmandApplication app, - String bottomButtonText, boolean nightMode) { + public SelectProfileMenuAdapter(List items, @NonNull OsmandApplication app, + String bottomButtonText, boolean nightMode, + @Nullable ApplicationMode appMode) { this.items.addAll(items); if (bottomButton) { this.items.add(BUTTON_ITEM); } this.app = app; + this.appMode = appMode != null ? appMode : app.getSettings().getApplicationMode(); this.bottomButton = !Algorithms.isEmpty(bottomButtonText); this.bottomButtonText = bottomButtonText; this.nightMode = nightMode; @@ -103,7 +105,7 @@ public class SelectProfileMenuAdapter extends AbstractProfileMenuAdapter