diff --git a/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java b/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java index 0fa75b9dd3..6f72392bd5 100644 --- a/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java +++ b/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java @@ -552,25 +552,14 @@ public class ContextMenuAdapter { } } - public void initDefaultOrders(@NonNull List items) { + private void initDefaultOrders(@NonNull List items) { for (int i = 0; i < items.size(); i++) { items.get(i).setOrder(i); } } - public List getItemsForRearrangeAdapter(@NonNull ScreenType screenType, @Nullable List hiddenItemsIds, @Nullable HashMap itemsOrderIds, boolean hidden) { - String idScheme = ""; - switch (screenType) { - case DRAWER: - idScheme = DRAWER_ITEM_ID_SCHEME; - break; - case CONFIGURE_MAP: - idScheme = CONFIGURE_MAP_ITEM_ID_SCHEME; - break; - case CONTEXT_MENU_ACTIONS: - idScheme = MAP_CONTEXT_MENU_ACTIONS; - break; - } + public List getItemsForRearrangeAdapter(@Nullable List hiddenItemsIds, @Nullable HashMap itemsOrderIds, boolean hidden) { + String idScheme = getIdScheme(); if (itemsOrderIds == null || itemsOrderIds.isEmpty()) { initDefaultOrders(items); } else { @@ -593,19 +582,8 @@ public class ContextMenuAdapter { return hidden ? hiddenItems : visibleItems; } - public List getDefaultItems(ScreenType screenType) { - String idScheme = ""; - switch (screenType) { - case DRAWER: - idScheme = DRAWER_ITEM_ID_SCHEME; - break; - case CONFIGURE_MAP: - idScheme = CONFIGURE_MAP_ITEM_ID_SCHEME; - break; - case CONTEXT_MENU_ACTIONS: - idScheme = MAP_CONTEXT_MENU_ACTIONS; - break; - } + public List getDefaultItems() { + String idScheme = getIdScheme(); List items = new ArrayList<>(); for (ContextMenuItem item : this.items) { String id = item.getId(); @@ -616,6 +594,26 @@ public class ContextMenuAdapter { return items; } + private String getIdScheme() { + String idScheme = ""; + for (ContextMenuItem item : items) { + String id = item.getId(); + if (id != null) { + if (id.startsWith(DRAWER_ITEM_ID_SCHEME)) { + idScheme = DRAWER_ITEM_ID_SCHEME; + break; + } else if (id.startsWith(CONFIGURE_MAP_ITEM_ID_SCHEME)) { + idScheme = CONFIGURE_MAP_ITEM_ID_SCHEME; + break; + } else if (id.startsWith(MAP_CONTEXT_MENU_ACTIONS)) { + idScheme = MAP_CONTEXT_MENU_ACTIONS; + break; + } + } + } + return idScheme; + } + public void reorderMenuItems(@NonNull List defaultItems, @NonNull HashMap itemsOrder) { for (ContextMenuItem item : defaultItems) { Integer order = itemsOrder.get(item.getId()); @@ -641,100 +639,28 @@ public class ContextMenuAdapter { return result; } - public void resetMenuItems(@NonNull OsmandApplication app, @NonNull ScreenType screenType) { - saveHiddenItemsIds(app, screenType, null); - saveItemsIdsOrder(app, screenType, null); - } - - @NonNull - public List getHiddenItemsIds(@NonNull OsmandApplication app, @NonNull ScreenType type) { - List hiddenItemsIds = null; - switch (type) { - case DRAWER: - hiddenItemsIds = app.getSettings().HIDDEN_DRAWER_ITEMS.getStringsList(); - break; - case CONFIGURE_MAP: - hiddenItemsIds = app.getSettings().HIDDEN_CONFIGURE_MAP_ITEMS.getStringsList(); - break; - case CONTEXT_MENU_ACTIONS: - hiddenItemsIds = app.getSettings().HIDDEN_CONTEXT_MENU_ACTIONS_ITEMS.getStringsList(); - break; + public void initItemsCustomOrder(@NonNull OsmandApplication app) { + OsmandSettings.MenuItemConfigPreference preference = null; + for (ContextMenuItem item : items) { + String id = item.getId(); + if (id != null) { + if (id.startsWith(DRAWER_ITEM_ID_SCHEME)) { + preference = app.getSettings().DRAWER_ITEMS; + break; + } else if (id.startsWith(CONFIGURE_MAP_ITEM_ID_SCHEME)) { + preference = app.getSettings().CONFIGURE_MAP_ITEMS; + break; + } else if (id.startsWith(MAP_CONTEXT_MENU_ACTIONS)) { + preference = app.getSettings().CONTEXT_MENU_ACTIONS_ITEMS; + break; + } + } } - return hiddenItemsIds != null ? new ArrayList<>(hiddenItemsIds) : new ArrayList(); - } - - @NonNull - public List getItemsIdsOrder(@NonNull OsmandApplication app, @NonNull ScreenType type) { - List hiddenItemsIds = null; - switch (type) { - case DRAWER: - hiddenItemsIds = app.getSettings().DRAWER_ITEMS_ORDER.getStringsList(); - break; - case CONFIGURE_MAP: - hiddenItemsIds = app.getSettings().CONFIGURE_MAP_ITEMS_ORDER.getStringsList(); - break; - case CONTEXT_MENU_ACTIONS: - hiddenItemsIds = app.getSettings().CONTEXT_MENU_ACTIONS_ITEMS_ORDER.getStringsList(); - break; + if (preference == null) { + return; } - return hiddenItemsIds != null ? new ArrayList<>(hiddenItemsIds) : new ArrayList(); - } - - public void saveHiddenItemsIds(@NonNull OsmandApplication app, @NonNull ScreenType type, @Nullable List hiddenItemsIds) { - switch (type) { - case DRAWER: - app.getSettings().HIDDEN_DRAWER_ITEMS.setStringsList(hiddenItemsIds); - break; - case CONFIGURE_MAP: - app.getSettings().HIDDEN_CONFIGURE_MAP_ITEMS.setStringsList(hiddenItemsIds); - break; - case CONTEXT_MENU_ACTIONS: - app.getSettings().HIDDEN_CONTEXT_MENU_ACTIONS_ITEMS.setStringsList(hiddenItemsIds); - break; - } - } - - public void saveItemsIdsOrder(@NonNull OsmandApplication app, @NonNull ScreenType type, @Nullable List itemsIdsOrder) { - switch (type) { - case DRAWER: - app.getSettings().DRAWER_ITEMS_ORDER.setStringsList(itemsIdsOrder); - break; - case CONFIGURE_MAP: - app.getSettings().CONFIGURE_MAP_ITEMS_ORDER.setStringsList(itemsIdsOrder); - break; - case CONTEXT_MENU_ACTIONS: - app.getSettings().CONTEXT_MENU_ACTIONS_ITEMS_ORDER.setStringsList(itemsIdsOrder); - break; - } - } - - public String getPrefIdOrder(@NonNull OsmandApplication app, @NonNull ScreenType type) { - switch (type) { - case DRAWER: - return app.getSettings().DRAWER_ITEMS_ORDER.getId(); - case CONFIGURE_MAP: - return app.getSettings().CONFIGURE_MAP_ITEMS_ORDER.getId(); - case CONTEXT_MENU_ACTIONS: - return app.getSettings().CONTEXT_MENU_ACTIONS_ITEMS_ORDER.get(); - } - return ""; - } - - public String getPrefIdHidden(@NonNull OsmandApplication app, @NonNull ScreenType type) { - switch (type) { - case DRAWER: - return app.getSettings().HIDDEN_DRAWER_ITEMS.getId(); - case CONFIGURE_MAP: - return app.getSettings().HIDDEN_CONFIGURE_MAP_ITEMS.getId(); - case CONTEXT_MENU_ACTIONS: - return app.getSettings().HIDDEN_CONTEXT_MENU_ACTIONS_ITEMS.get(); - } - return ""; - } - - public void initItemsCustomOrder(@NonNull OsmandApplication app, @NonNull ScreenType type) { - List savedOrder = getItemsIdsOrder(app, type); - List hiddenItems = getHiddenItemsIds(app, type); + List savedOrder = preference.getOrderIds(); + List hiddenItems = preference.getHiddenIds(); if (!savedOrder.isEmpty()) { reorderMenuItems(items, getMenuItemsOrder(savedOrder)); diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index ed3a6128d8..84e31e3a56 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -25,6 +25,7 @@ import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; import net.osmand.IndexConstants; +import net.osmand.PlatformUtil; import net.osmand.StateChangedListener; import net.osmand.ValueHolder; import net.osmand.aidl.OsmandAidlApi; @@ -56,6 +57,8 @@ import net.osmand.plus.voice.CommandPlayer; import net.osmand.render.RenderingRulesStorage; import net.osmand.util.Algorithms; +import org.apache.commons.logging.Log; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -79,6 +82,8 @@ import java.util.StringTokenizer; public class OsmandSettings { + private static final Log LOG = PlatformUtil.getLog(OsmandSettings.class.getName()); + public static final int VERSION = 1; public interface OsmandPreference { @@ -412,11 +417,6 @@ public class OsmandSettings { ((BooleanPreference) preference).setModeValue(mode, (Boolean) value); return true; } - } else if (preference instanceof ListStringPreference) { - if (value instanceof List) { - ((ListStringPreference) preference).setModeValues(mode, (List) value); - return true; - } } else if (preference instanceof StringPreference) { if (value instanceof String) { ((StringPreference) preference).setModeValue(mode, (String) value); @@ -1103,6 +1103,75 @@ public class OsmandSettings { } } + public class MenuItemConfigPreference extends StringPreference { + private static final String HIDDEN = "hidden"; + private static final String ORDER = "order"; + + private MenuItemConfigPreference(String id, String defaultValue) { + super(id, defaultValue); + } + + private void addIdsToJsonArray(@NonNull JSONArray jsonArray, List ids) { + if (ids != null && !ids.isEmpty()) { + for (String id : ids) { + jsonArray.put(id); + } + } + } + + private List getIdValues(String itemName) { + List ids = new ArrayList<>(); + String itemsString = get(); + if (itemsString == null) { + return ids; + } + try { + JSONObject json = new JSONObject(itemsString); + JSONObject items = json.optJSONObject(getId()); + JSONArray idsArray = items.optJSONArray(itemName); + if (idsArray != null) { + for (int i = 0; i < idsArray.length(); i++) { + String id = idsArray.optString(i); + ids.add(id); + } + } + } catch (JSONException e) { + LOG.error("Error converting to json string: " + e); + } + return ids; + } + + public String convertToJsonString(List hidden, List order) { + try { + JSONObject json = new JSONObject(); + JSONObject items = new JSONObject(); + JSONArray hiddenItems = new JSONArray(); + JSONArray orderItems = new JSONArray(); + addIdsToJsonArray(hiddenItems, hidden); + addIdsToJsonArray(orderItems, order); + items.put(HIDDEN, hiddenItems); + items.put(ORDER, orderItems); + json.put(getId(), items); + return json.toString(); + } catch (JSONException e) { + LOG.error("Error converting to json string: " + e); + } + return ""; + } + + public boolean setIdValues(List hidden, List order) { + return set(convertToJsonString(hidden, order)); + } + + public List getHiddenIds() { + return getIdValues(HIDDEN); + } + + public List getOrderIds() { + return getIdValues(ORDER); + } + } + public class EnumIntPreference> extends CommonPreference { private final E[] values; @@ -3410,30 +3479,21 @@ public class OsmandSettings { SELECTED_POI_FILTER_FOR_MAP.set(android.text.TextUtils.join(",", poiFilters)); } - public final ListStringPreference HIDDEN_CONTEXT_MENU_ACTIONS_ITEMS = (ListStringPreference) - new ListStringPreference("hidden_context_menu_actions_items", null, ",,").makeProfile().cache(); - - public final ListStringPreference CONTEXT_MENU_ACTIONS_ITEMS_ORDER = (ListStringPreference) - new ListStringPreference("context_menu_actions_items_order", null, ",,").makeProfile().cache(); - - public final ListStringPreference HIDDEN_CONFIGURE_MAP_ITEMS = (ListStringPreference) - new ListStringPreference("hidden_configure_map_items", null, ",,").makeProfile().cache(); - - public final ListStringPreference CONFIGURE_MAP_ITEMS_ORDER = (ListStringPreference) - new ListStringPreference("configure_map_items_order", null, ",,").makeProfile().cache(); - - public final ListStringPreference DRAWER_ITEMS_ORDER = (ListStringPreference) - new ListStringPreference("drawer_items_order", null, ",,").makeProfile().cache(); - - public final ListStringPreference HIDDEN_DRAWER_ITEMS = (ListStringPreference) - new ListStringPreference("hidden_drawer_items", null, ",,").makeProfile().cache(); - public final ListStringPreference POI_FILTERS_ORDER = (ListStringPreference) new ListStringPreference("poi_filters_order", null, ",,").makeProfile().cache(); public final ListStringPreference INACTIVE_POI_FILTERS = (ListStringPreference) new ListStringPreference("inactive_poi_filters", null, ",,").makeProfile().cache(); + public final MenuItemConfigPreference DRAWER_ITEMS = + (MenuItemConfigPreference) new MenuItemConfigPreference("drawer_items", null).makeProfile().cache(); + + public final MenuItemConfigPreference CONFIGURE_MAP_ITEMS = + (MenuItemConfigPreference) new MenuItemConfigPreference("configure_map_items", null).makeProfile().cache(); + + public final MenuItemConfigPreference CONTEXT_MENU_ACTIONS_ITEMS = + (MenuItemConfigPreference) new MenuItemConfigPreference("context_menu_actions_items", null).makeProfile().cache(); + public static final String VOICE_PROVIDER_NOT_USE = "VOICE_PROVIDER_NOT_USE"; public static final String[] TTS_AVAILABLE_VOICES = new String[]{ diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java index 3f081a547c..26f929038a 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java @@ -1194,7 +1194,7 @@ public class MapActivityActions implements DialogProvider { } menuItemsListView.setDivider(null); final ContextMenuAdapter contextMenuAdapter = createMainOptionsMenu(); - contextMenuAdapter.initItemsCustomOrder(getMyApplication(), ScreenType.DRAWER); + contextMenuAdapter.initItemsCustomOrder(getMyApplication()); contextMenuAdapter.setDefaultLayoutId(R.layout.simple_list_menu_item); final ArrayAdapter simpleListAdapter = contextMenuAdapter.createListAdapter(mapActivity, !nightMode); diff --git a/OsmAnd/src/net/osmand/plus/dashboard/DashboardOnMap.java b/OsmAnd/src/net/osmand/plus/dashboard/DashboardOnMap.java index 6bc0462bc9..a2fe40760a 100644 --- a/OsmAnd/src/net/osmand/plus/dashboard/DashboardOnMap.java +++ b/OsmAnd/src/net/osmand/plus/dashboard/DashboardOnMap.java @@ -698,7 +698,7 @@ public class DashboardOnMap implements ObservableScrollViewCallbacks, IRouteInfo cm = mapActivity.getMapLayers().getMapWidgetRegistry().getViewConfigureMenuAdapter(mapActivity); } else if (visibleType == DashboardType.CONFIGURE_MAP) { cm = new ConfigureMapMenu().createListAdapter(mapActivity); - cm.initItemsCustomOrder(getMyApplication(), ScreenType.CONFIGURE_MAP); + cm.initItemsCustomOrder(getMyApplication()); } else if (visibleType == DashboardType.LIST_MENU) { cm = mapActivity.getMapActions().createMainOptionsMenu(); } else if (visibleType == DashboardType.ROUTE_PREFERENCES) { diff --git a/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuItemsFragment.java b/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuItemsFragment.java index f4aca1993f..7b2ee2a734 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuItemsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuItemsFragment.java @@ -122,8 +122,8 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment hiddenMenuItems = savedInstanceState.getStringArrayList(HIDDEN_ITEMS_KEY); menuItemsOrder = (HashMap) savedInstanceState.getSerializable(ITEMS_ORDER_KEY); } else { - hiddenMenuItems = contextMenuAdapter.getHiddenItemsIds(app, screenType); - menuItemsOrder = contextMenuAdapter.getMenuItemsOrder(contextMenuAdapter.getItemsIdsOrder(app, screenType)); + hiddenMenuItems = getSettingForScreen(app, screenType).getHiddenIds(); + menuItemsOrder = contextMenuAdapter.getMenuItemsOrder(getSettingForScreen(app, screenType).getOrderIds()); } } @@ -218,34 +218,32 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment applyButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - HashMap prefsMap = new HashMap<>(); - prefsMap.put(contextMenuAdapter.getPrefIdHidden(app, screenType), (Serializable) hiddenMenuItems); - List defItems = contextMenuAdapter.getDefaultItems(screenType); - contextMenuAdapter.reorderMenuItems(defItems, menuItemsOrder); - List ids = new ArrayList<>(); - for (ContextMenuItem item : defItems) { - ids.add(item.getId()); - } - prefsMap.put(contextMenuAdapter.getPrefIdOrder(app, screenType), (Serializable) ids); - FragmentManager fm = getFragmentManager(); - if (fm != null) { - ChangeGeneralProfilesPrefBottomSheet.showInstance( - fm, - prefsMap, - getTargetFragment(), - false, - appMode, - new ChangeGeneralProfilesPrefBottomSheet.OnChangeSettingListener() { - @Override - public void onApplied() { - dismissFragment(); - } + List defItems = contextMenuAdapter.getDefaultItems(); + contextMenuAdapter.reorderMenuItems(defItems, menuItemsOrder); + List ids = new ArrayList<>(); + for (ContextMenuItem item : defItems) { + ids.add(item.getId()); + } + FragmentManager fm = getFragmentManager(); + String stringToSave = getSettingForScreen(app, screenType).convertToJsonString(hiddenMenuItems, ids); + if (fm != null) { + ChangeGeneralProfilesPrefBottomSheet.showInstance(fm, + getSettingForScreen(app, screenType).getId(), + stringToSave, + getTargetFragment(), + false, + appMode, + new ChangeGeneralProfilesPrefBottomSheet.OnChangeSettingListener() { + @Override + public void onApplied() { + dismissFragment(); + } - @Override - public void onDiscard() { + @Override + public void onDiscard() { - } - }); + } + }); } } }); @@ -259,8 +257,8 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment List items = new ArrayList<>(); items.add(new AdapterItem(DESCRIPTION, screenType)); - List visible = contextMenuAdapter.getItemsForRearrangeAdapter(screenType, hiddenMenuItems, wasReset ? null : menuItemsOrder, false); - List hiddenItems = contextMenuAdapter.getItemsForRearrangeAdapter(screenType, hiddenMenuItems, wasReset ? null : menuItemsOrder, true); + List visible = contextMenuAdapter.getItemsForRearrangeAdapter(hiddenMenuItems, wasReset ? null : menuItemsOrder, false); + List hiddenItems = contextMenuAdapter.getItemsForRearrangeAdapter(hiddenMenuItems, wasReset ? null : menuItemsOrder, true); if (screenType == ScreenType.CONTEXT_MENU_ACTIONS) { List main = new ArrayList<>(); int actionsIndex = 3; @@ -360,22 +358,22 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment public void copyAppModePrefs(ApplicationMode appMode) { if (appMode != null) { List prefs = new ArrayList<>(); - switch (screenType) { - case DRAWER: - prefs.add(app.getSettings().DRAWER_ITEMS_ORDER); - prefs.add(app.getSettings().HIDDEN_DRAWER_ITEMS); - break; - case CONFIGURE_MAP: - prefs.add(app.getSettings().CONFIGURE_MAP_ITEMS_ORDER); - prefs.add(app.getSettings().HIDDEN_CONFIGURE_MAP_ITEMS); - break; - case CONTEXT_MENU_ACTIONS: - prefs.add(app.getSettings().CONTEXT_MENU_ACTIONS_ITEMS_ORDER); - prefs.add(app.getSettings().HIDDEN_CONTEXT_MENU_ACTIONS_ITEMS); - break; - } + prefs.add(getSettingForScreen(app, screenType)); app.getSettings().copyProfilePreferences(appMode, this.appMode, prefs); dismissFragment(); } } + + public static OsmandSettings.MenuItemConfigPreference getSettingForScreen(OsmandApplication app, ScreenType screenType) { + switch (screenType) { + case DRAWER: + return app.getSettings().DRAWER_ITEMS; + case CONFIGURE_MAP: + return app.getSettings().CONFIGURE_MAP_ITEMS; + case CONTEXT_MENU_ACTIONS: + return app.getSettings().CONTEXT_MENU_ACTIONS_ITEMS; + default: + throw new IllegalArgumentException("Unsupported screen type"); + } + } } diff --git a/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuRootFragment.java b/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuRootFragment.java index c85c4ebd14..09a94eed48 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuRootFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/ConfigureMenuRootFragment.java @@ -37,6 +37,7 @@ import net.osmand.plus.activities.PluginsActivity; import net.osmand.plus.base.BaseOsmAndFragment; import net.osmand.plus.dialogs.ConfigureMapMenu; import net.osmand.plus.helpers.FontCache; +import net.osmand.plus.mapcontextmenu.MapContextMenu; import net.osmand.plus.widgets.style.CustomTypefaceSpan; @@ -222,12 +223,12 @@ public class ConfigureMenuRootFragment extends BaseOsmAndFragment { contextMenuAdapter = configureMapMenu.createListAdapter((MapActivity) activity); break; case CONTEXT_MENU_ACTIONS: - ConfigureMapMenu configureaMapMenu = new ConfigureMapMenu(); - contextMenuAdapter = configureaMapMenu.createListAdapter((MapActivity) activity); + MapContextMenu menu = ((MapActivity) activity).getContextMenu(); + contextMenuAdapter = menu.getAdapter(); break; } - int hiddenCount = contextMenuAdapter.getHiddenItemsIds(app, type).size(); - int allCount = contextMenuAdapter.getDefaultItems(type).size(); + int hiddenCount = ConfigureMenuItemsFragment.getSettingForScreen(app, type).getHiddenIds().size(); + int allCount = contextMenuAdapter.getDefaultItems().size(); String amount = getString(R.string.n_items_of_z, String.valueOf(allCount - hiddenCount), String.valueOf(allCount)); return getString(R.string.ltr_or_rtl_combine_via_colon, getString(R.string.shared_string_items), amount); } diff --git a/OsmAnd/src/net/osmand/plus/settings/RearrangeMenuItemsAdapter.java b/OsmAnd/src/net/osmand/plus/settings/RearrangeMenuItemsAdapter.java index 5f92b061f7..799705b6d8 100644 --- a/OsmAnd/src/net/osmand/plus/settings/RearrangeMenuItemsAdapter.java +++ b/OsmAnd/src/net/osmand/plus/settings/RearrangeMenuItemsAdapter.java @@ -46,6 +46,7 @@ public class RearrangeMenuItemsAdapter extends RecyclerView.Adapter items; private MenuItemsAdapterListener listener; private int activeColorRes; + private int textColorRes; public RearrangeMenuItemsAdapter(OsmandApplication app, @@ -57,6 +58,9 @@ public class RearrangeMenuItemsAdapter extends RecyclerView.Adapter prefsMap; - @Nullable private OnChangeSettingListener listener; @@ -58,12 +50,11 @@ public class ChangeGeneralProfilesPrefBottomSheet extends BasePreferenceBottomSh if (app == null || args == null) { return; } - prefId = args.getString(PREFERENCE_ID); + final String prefId = args.getString(PREFERENCE_ID); newValue = args.getSerializable(NEW_VALUE_KEY); - prefsMap = (HashMap) args.getSerializable(PREFS_MAP_KEY); -// if (newValue == null || prefId == null) { -// return; -// } + if (newValue == null || prefId == null) { + return; + } items.add(new TitleItem(getString(R.string.change_default_settings))); items.add(new LongDescriptionItem(getString(R.string.apply_preference_to_all_profiles))); @@ -75,11 +66,7 @@ public class ChangeGeneralProfilesPrefBottomSheet extends BasePreferenceBottomSh .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - if (prefsMap != null) { - savePrefs(app, prefsMap, true); - } else if (newValue != null || prefId != null) { - app.getSettings().setPreferenceForAllModes(prefId, newValue); - } + app.getSettings().setPreferenceForAllModes(prefId, newValue); updateTargetSettings(false, true); if (listener != null) { listener.onApplied(); @@ -99,11 +86,7 @@ public class ChangeGeneralProfilesPrefBottomSheet extends BasePreferenceBottomSh .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - if (prefsMap != null) { - savePrefs(app, prefsMap, false); - } else if (newValue != null || prefId != null) { - app.getSettings().setPreference(prefId, newValue, getAppMode()); - } + app.getSettings().setPreference(prefId, newValue, getAppMode()); updateTargetSettings(false, false); if (listener != null) { listener.onApplied(); @@ -143,18 +126,6 @@ public class ChangeGeneralProfilesPrefBottomSheet extends BasePreferenceBottomSh outState.putSerializable(NEW_VALUE_KEY, newValue); } - private void savePrefs(@NonNull OsmandApplication app, - @NonNull HashMap prefsMap, boolean toAllProfiles) { - List ids = new ArrayList<>(prefsMap.keySet()); - for (String id : ids) { - if (toAllProfiles) { - app.getSettings().setPreferenceForAllModes(id, prefsMap.get(id)); - } else { - app.getSettings().setPreference(id, prefsMap.get(id), getAppMode()); - } - } - } - private void updateTargetSettings(boolean discard, boolean appliedToAllProfiles) { BaseSettingsFragment target = (BaseSettingsFragment) getTargetFragment(); if (target != null) { @@ -184,22 +155,22 @@ public class ChangeGeneralProfilesPrefBottomSheet extends BasePreferenceBottomSh Fragment target, boolean usedOnMap, @Nullable ApplicationMode appMode) { - showFragmentInstance(fm, prefId, newValue, null, target, usedOnMap, appMode, null); + showFragmentInstance(fm, prefId, newValue, target, usedOnMap, appMode, null); } public static void showInstance(@NonNull FragmentManager fm, - @Nullable HashMap prefs, + @Nullable String prefId, + @Nullable Serializable newValue, Fragment target, boolean usedOnMap, @Nullable ApplicationMode appMode, @Nullable OnChangeSettingListener listener) { - showFragmentInstance(fm, null, null, prefs, target, usedOnMap, appMode, listener); + showFragmentInstance(fm, prefId, newValue, target, usedOnMap, appMode, listener); } private static void showFragmentInstance(@NonNull FragmentManager fm, @Nullable String prefId, @Nullable Serializable newValue, - @Nullable HashMap prefs, Fragment target, boolean usedOnMap, @Nullable ApplicationMode appMode, @@ -207,15 +178,8 @@ public class ChangeGeneralProfilesPrefBottomSheet extends BasePreferenceBottomSh try { if (fm.findFragmentByTag(ChangeGeneralProfilesPrefBottomSheet.TAG) == null) { Bundle args = new Bundle(); - if (prefId != null) { - args.putString(PREFERENCE_ID, prefId); - } - if (newValue != null) { - args.putSerializable(NEW_VALUE_KEY, newValue); - } - if (prefs != null) { - args.putSerializable(PREFS_MAP_KEY, prefs); - } + args.putString(PREFERENCE_ID, prefId); + args.putSerializable(NEW_VALUE_KEY, newValue); ChangeGeneralProfilesPrefBottomSheet fragment = new ChangeGeneralProfilesPrefBottomSheet(); fragment.setArguments(args);