From 221d6c8aacdf7b52b014f5b04a3d59d9573f9416 Mon Sep 17 00:00:00 2001 From: Chumva Date: Mon, 2 Sep 2019 16:02:45 +0300 Subject: [PATCH] Fix ChangeGeneralProfilesPrefBottomSheet --- OsmAnd/res/values/strings.xml | 1 + OsmAnd/src/net/osmand/AndroidUtils.java | 3 +- .../src/net/osmand/plus/OsmandSettings.java | 23 +++-- ...ChangeGeneralProfilesPrefBottomSheet.java} | 97 ++++++++++++------- .../GeneralProfileSettingsFragment.java | 2 +- .../plus/settings/GlobalSettingsFragment.java | 2 +- 6 files changed, 80 insertions(+), 48 deletions(-) rename OsmAnd/src/net/osmand/plus/settings/{ChangeProfilesPreferenceBottomSheet.java => ChangeGeneralProfilesPrefBottomSheet.java} (52%) diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index d38dc5991e..bc80ae3aa6 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -11,6 +11,7 @@ Thx - Hardy --> + This setting is selected by default for profiles: %s. Change default settings Discard changes Apply to current %1$s profile diff --git a/OsmAnd/src/net/osmand/AndroidUtils.java b/OsmAnd/src/net/osmand/AndroidUtils.java index a7e923fde5..68c6c88ce0 100644 --- a/OsmAnd/src/net/osmand/AndroidUtils.java +++ b/OsmAnd/src/net/osmand/AndroidUtils.java @@ -32,7 +32,6 @@ import android.support.v4.content.ContextCompat; import android.support.v4.content.FileProvider; import android.support.v4.text.TextUtilsCompat; import android.support.v4.view.ViewCompat; -import android.text.ParcelableSpan; import android.text.Spannable; import android.text.SpannableString; import android.text.SpannableStringBuilder; @@ -594,7 +593,7 @@ public class AndroidUtils { } if(replaceStyle != null) { ssb.setSpan(replaceStyle, indexOfPlaceholder, - stringToInsertAndStyle.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + indexOfPlaceholder + stringToInsertAndStyle.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } return ssb; } else { diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index 7891ba98a3..de948c9da8 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -218,8 +218,12 @@ public class OsmandSettings { return registeredPreferences.get(key); } - @SuppressWarnings("unchecked") public boolean setPreference(String key, Object value) { + return setPreference(key, value, APPLICATION_MODE.get()); + } + + @SuppressWarnings("unchecked") + public boolean setPreference(String key, Object value, ApplicationMode mode) { OsmandPreference preference = registeredPreferences.get(key); if (preference != null) { if (preference == APPLICATION_MODE) { @@ -251,7 +255,7 @@ public class OsmandSettings { } catch (IllegalArgumentException e) { return false; } - METRIC_SYSTEM.set(metricSystem); + METRIC_SYSTEM.setModeValue(mode, metricSystem); return true; } else if (preference == SPEED_SYSTEM && value instanceof String) { String speedSystemName = (String) value; @@ -261,31 +265,31 @@ public class OsmandSettings { } catch (IllegalArgumentException e) { return false; } - SPEED_SYSTEM.set(speedSystem); + SPEED_SYSTEM.setModeValue(mode, speedSystem); return true; } else if (preference instanceof BooleanPreference) { if (value instanceof Boolean) { - ((BooleanPreference) preference).set((Boolean) value); + ((BooleanPreference) preference).setModeValue(mode, (Boolean) value); return true; } } else if (preference instanceof StringPreference) { if (value instanceof String) { - ((StringPreference) preference).set((String) value); + ((StringPreference) preference).setModeValue(mode, (String) value); return true; } } else if (preference instanceof FloatPreference) { if (value instanceof Float) { - ((FloatPreference) preference).set((Float) value); + ((FloatPreference) preference).setModeValue(mode, (Float) value); return true; } } else if (preference instanceof IntPreference) { if (value instanceof Integer) { - ((IntPreference) preference).set((Integer) value); + ((IntPreference) preference).setModeValue(mode, (Integer) value); return true; } } else if (preference instanceof LongPreference) { if (value instanceof Long) { - ((LongPreference) preference).set((Long) value); + ((LongPreference) preference).setModeValue(mode, (Long) value); return true; } } else if (preference instanceof EnumIntPreference) { @@ -294,7 +298,7 @@ public class OsmandSettings { int newVal = (Integer) value; if (enumPref.values.length > newVal) { Enum enumValue = enumPref.values[newVal]; - return enumPref.set(enumValue); + return enumPref.setModeValue(mode, enumValue); } return false; } @@ -3202,6 +3206,7 @@ public class OsmandSettings { return defValue; } + @Nullable public Object getValue(String key, Object defValue) { OsmandPreference preference = getPreference(key); if (preference != null) { diff --git a/OsmAnd/src/net/osmand/plus/settings/ChangeProfilesPreferenceBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/ChangeGeneralProfilesPrefBottomSheet.java similarity index 52% rename from OsmAnd/src/net/osmand/plus/settings/ChangeProfilesPreferenceBottomSheet.java rename to OsmAnd/src/net/osmand/plus/settings/ChangeGeneralProfilesPrefBottomSheet.java index dd197604b8..0bf158162e 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ChangeProfilesPreferenceBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/ChangeGeneralProfilesPrefBottomSheet.java @@ -1,49 +1,80 @@ package net.osmand.plus.settings; -import android.content.Context; +import android.graphics.Typeface; import android.os.Bundle; import android.support.annotation.NonNull; +import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.view.View; +import net.osmand.AndroidUtils; import net.osmand.PlatformUtil; import net.osmand.plus.ApplicationMode; import net.osmand.plus.OsmandApplication; -import net.osmand.plus.OsmandSettings; +import net.osmand.plus.OsmandSettings.CommonPreference; +import net.osmand.plus.OsmandSettings.OsmandPreference; import net.osmand.plus.R; import net.osmand.plus.base.MenuBottomSheetDialogFragment; import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem; +import net.osmand.plus.base.bottomsheetmenu.simpleitems.LongDescriptionItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem; import org.apache.commons.logging.Log; +import java.util.ArrayList; import java.util.List; -public class ChangeProfilesPreferenceBottomSheet extends MenuBottomSheetDialogFragment { +public class ChangeGeneralProfilesPrefBottomSheet extends MenuBottomSheetDialogFragment { - public static final String TAG = "ChangeProfilesPreferenceBottomSheet"; + public static final String TAG = "ChangeGeneralProfilesPrefBottomSheet"; private static final String PREFERENCE_ID = "preference_id"; - private static final Log LOG = PlatformUtil.getLog(ChangeProfilesPreferenceBottomSheet.class); + private static final Log LOG = PlatformUtil.getLog(ChangeGeneralProfilesPrefBottomSheet.class); private Object newValue; @Override public void createMenuItems(Bundle savedInstanceState) { - Context context = getContext(); - if (context == null) { - return; - } + final OsmandApplication app = getMyApplication(); Bundle args = getArguments(); - if (args == null || !args.containsKey(PREFERENCE_ID)) { + if (app == null || args == null || !args.containsKey(PREFERENCE_ID)) { return; } final String prefId = args.getString(PREFERENCE_ID); + CommonPreference pref = getPreference(prefId); + if (pref == null) { + return; + } items.add(new TitleItem(getString(R.string.change_default_settings))); + StringBuilder builder = new StringBuilder(); + final List values = ApplicationMode.values(app); + List appModesSameValue = new ArrayList<>(); + + for (int i = 0; i < values.size(); i++) { + ApplicationMode mode = values.get(i); + Object modeValue = pref.getModeValue(mode); + if (modeValue.equals(newValue)) { + appModesSameValue.add(mode); + } + } + + for (int i = 0; i < appModesSameValue.size(); i++) { + ApplicationMode mode = appModesSameValue.get(i); + builder.append(mode.toHumanString(app)); + if (i < appModesSameValue.size() - 1) { + builder.append(", "); + } + } + + if (builder.length() > 0) { + CharSequence description = AndroidUtils.getStyledString(app.getString(R.string.pref_selected_by_default_for_profiles), builder.toString(), Typeface.BOLD); + items.add(new LongDescriptionItem(description)); + } + BaseBottomSheetItem applyToAllProfiles = new SimpleBottomSheetItem.Builder() .setTitle(getString(R.string.apply_to_all_profiles)) .setIcon(getActiveIcon(R.drawable.ic_action_copy)) @@ -51,17 +82,8 @@ public class ChangeProfilesPreferenceBottomSheet extends MenuBottomSheetDialogFr .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - OsmandApplication app = getMyApplication(); - if (app != null) { - OsmandSettings settings = app.getSettings(); - OsmandSettings.OsmandPreference pref = settings.getPreference(prefId); - if (pref instanceof OsmandSettings.CommonPreference) { - OsmandSettings.CommonPreference commonPref = (OsmandSettings.CommonPreference) pref; - final List values = ApplicationMode.values(app); - for (ApplicationMode mode : values) { - commonPref.setModeValue(mode, newValue); - } - } + for (ApplicationMode mode : values) { + app.getSettings().setPreference(prefId, newValue, mode); } dismiss(); } @@ -72,21 +94,13 @@ public class ChangeProfilesPreferenceBottomSheet extends MenuBottomSheetDialogFr ApplicationMode selectedAppMode = getMyApplication().getSettings().APPLICATION_MODE.get(); BaseBottomSheetItem applyToCurrentProfile = new SimpleBottomSheetItem.Builder() - .setTitle(getString(R.string.apply_to_current_profile, selectedAppMode.toHumanString(context))) + .setTitle(getString(R.string.apply_to_current_profile, selectedAppMode.toHumanString(app))) .setIcon(getActiveIcon(selectedAppMode.getIconRes())) .setLayoutId(R.layout.bottom_sheet_item_simple) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - OsmandApplication app = getMyApplication(); - if (app != null) { - OsmandSettings settings = app.getSettings(); - OsmandSettings.OsmandPreference pref = settings.getPreference(prefId); - if (pref instanceof OsmandSettings.CommonPreference) { - OsmandSettings.CommonPreference commonPref = (OsmandSettings.CommonPreference) pref; - commonPref.setModeValue(settings.APPLICATION_MODE.get(), newValue); - } - } + app.getSettings().setPreference(prefId, newValue); dismiss(); } }) @@ -112,16 +126,29 @@ public class ChangeProfilesPreferenceBottomSheet extends MenuBottomSheetDialogFr return true; } - public static void showInstance(@NonNull FragmentManager fm, String prefId, Object newValue) { + private CommonPreference getPreference(String prefId) { + OsmandApplication app = getMyApplication(); + if (app != null) { + OsmandPreference pref = app.getSettings().getPreference(prefId); + if (pref instanceof CommonPreference) { + return (CommonPreference) pref; + } + } + + return null; + } + + public static void showInstance(@NonNull FragmentManager fm, String prefId, Object newValue, Fragment target) { try { - if (fm.findFragmentByTag(ChangeProfilesPreferenceBottomSheet.TAG) == null) { + if (fm.findFragmentByTag(ChangeGeneralProfilesPrefBottomSheet.TAG) == null) { Bundle args = new Bundle(); args.putString(PREFERENCE_ID, prefId); - ChangeProfilesPreferenceBottomSheet fragment = new ChangeProfilesPreferenceBottomSheet(); + ChangeGeneralProfilesPrefBottomSheet fragment = new ChangeGeneralProfilesPrefBottomSheet(); fragment.setArguments(args); + fragment.setTargetFragment(target, 0); fragment.newValue = newValue; - fragment.show(fm, ChangeProfilesPreferenceBottomSheet.TAG); + fragment.show(fm, ChangeGeneralProfilesPrefBottomSheet.TAG); } } catch (RuntimeException e) { LOG.error("showInstance", e); diff --git a/OsmAnd/src/net/osmand/plus/settings/GeneralProfileSettingsFragment.java b/OsmAnd/src/net/osmand/plus/settings/GeneralProfileSettingsFragment.java index da5ce58be2..f6b898fa86 100644 --- a/OsmAnd/src/net/osmand/plus/settings/GeneralProfileSettingsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/GeneralProfileSettingsFragment.java @@ -331,7 +331,7 @@ public class GeneralProfileSettingsFragment extends BaseSettingsFragment { public boolean onPreferenceChange(Preference preference, Object newValue) { FragmentManager fragmentManager = getFragmentManager(); if (fragmentManager != null) { - ChangeProfilesPreferenceBottomSheet.showInstance(fragmentManager, preference.getKey(), newValue); + ChangeGeneralProfilesPrefBottomSheet.showInstance(fragmentManager, preference.getKey(), newValue, this); } return false; } diff --git a/OsmAnd/src/net/osmand/plus/settings/GlobalSettingsFragment.java b/OsmAnd/src/net/osmand/plus/settings/GlobalSettingsFragment.java index 2ec54642ef..32d7008581 100644 --- a/OsmAnd/src/net/osmand/plus/settings/GlobalSettingsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/GlobalSettingsFragment.java @@ -85,7 +85,7 @@ public class GlobalSettingsFragment extends BaseSettingsFragment { } private void setupDefaultAppModePref() { - ApplicationMode selectedMode = getSelectedAppMode(); + ApplicationMode selectedMode = settings.DEFAULT_APPLICATION_MODE.get(); int iconRes = selectedMode.getIconRes(); String title = selectedMode.toHumanString(getContext());