diff --git a/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java b/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java index 7263f119bd..3ea7eee4d7 100644 --- a/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java +++ b/OsmAnd/src/net/osmand/plus/ContextMenuAdapter.java @@ -65,6 +65,7 @@ public class ContextMenuAdapter { @LayoutRes private int DEFAULT_LAYOUT_ID = R.layout.list_menu_item_native; List items = new ArrayList<>(); + private ArrayAdapter arrayAdapter; private boolean profileDependent = false; private boolean nightMode; private ConfigureMapMenu.OnClickListener changeAppModeListener = null; @@ -100,6 +101,15 @@ public class ContextMenuAdapter { return items.get(position); } + public ContextMenuItem getItemById(@NonNull String id) { + for (ContextMenuItem item : items) { + if (id.equals(item.getId())) { + return item; + } + } + return null; + } + public List getItems() { return items; } @@ -192,8 +202,9 @@ public class ContextMenuAdapter { } } items.removeAll(itemsToRemove); - return new ContextMenuArrayAdapter(activity, layoutId, R.id.title, + this.arrayAdapter = new ContextMenuArrayAdapter(activity, layoutId, R.id.title, items.toArray(new ContextMenuItem[items.size()]), app, lightTheme, changeAppModeListener); + return this.arrayAdapter; } public class ContextMenuArrayAdapter extends ArrayAdapter { @@ -616,6 +627,12 @@ public class ContextMenuAdapter { return visible; } + public void notifyDataSetChanged() { + if (this.arrayAdapter != null) { + this.arrayAdapter.notifyDataSetChanged(); + } + } + public static OnItemDeleteAction makeDeleteAction(final OsmandPreference... prefs) { return new OnItemDeleteAction() { @Override diff --git a/OsmAnd/src/net/osmand/plus/dashboard/DashboardOnMap.java b/OsmAnd/src/net/osmand/plus/dashboard/DashboardOnMap.java index 4c9b2a2c2b..74d61c7479 100644 --- a/OsmAnd/src/net/osmand/plus/dashboard/DashboardOnMap.java +++ b/OsmAnd/src/net/osmand/plus/dashboard/DashboardOnMap.java @@ -130,6 +130,7 @@ public class DashboardOnMap implements ObservableScrollViewCallbacks, IRouteInfo private ArrayAdapter listAdapter; private OnItemClickListener listAdapterOnClickListener; + private DashboardStateListener dashboardStateListener; private boolean visible = false; private DashboardType visibleType; @@ -567,7 +568,7 @@ public class DashboardOnMap implements ObservableScrollViewCallbacks, IRouteInfo boolean appModeChanged = currentAppMode != previousAppMode; boolean refresh = this.visibleType == type && !appModeChanged; - previousAppMode = currentAppMode; + this.previousAppMode = currentAppMode; this.visibleType = type; DashboardOnMap.staticVisible = visible; DashboardOnMap.staticVisibleType = type; @@ -657,9 +658,21 @@ public class DashboardOnMap implements ObservableScrollViewCallbacks, IRouteInfo settings.MAPILLARY_FIRST_DIALOG_SHOWN.set(true); } } + notifyDashboardVisibilityStateListener(visible); mapActivity.updateStatusBarColor(); } + private void notifyDashboardVisibilityStateListener(boolean visible) { + if (dashboardStateListener != null) { + if (visible) { + dashboardStateListener.onShowDashboard(); + } else { + dashboardStateListener.onHideDashboard(); + dashboardStateListener = null; + } + } + } + public void updateDashboard() { if (visibleType == DashboardType.ROUTE_PREFERENCES) { refreshContent(false); @@ -705,7 +718,9 @@ public class DashboardOnMap implements ObservableScrollViewCallbacks, IRouteInfo if (visibleType == DashboardType.CONFIGURE_SCREEN) { cm = mapActivity.getMapLayers().getMapWidgetRegistry().getViewConfigureMenuAdapter(mapActivity); } else if (visibleType == DashboardType.CONFIGURE_MAP) { - cm = new ConfigureMapMenu().createListAdapter(mapActivity); + ConfigureMapMenu configureMapMenu = new ConfigureMapMenu(mapActivity); + dashboardStateListener = configureMapMenu; + cm = configureMapMenu.createListAdapter(mapActivity); } else if (visibleType == DashboardType.LIST_MENU) { cm = mapActivity.getMapActions().createMainOptionsMenu(); } else if (visibleType == DashboardType.ROUTE_PREFERENCES) { @@ -1315,4 +1330,12 @@ public class DashboardOnMap implements ObservableScrollViewCallbacks, IRouteInfo @Override public void routeWasFinished() { } + + public interface DashboardStateListener { + + void onShowDashboard(); + + void onHideDashboard(); + + } } diff --git a/OsmAnd/src/net/osmand/plus/dialogs/ConfigureMapMenu.java b/OsmAnd/src/net/osmand/plus/dialogs/ConfigureMapMenu.java index 647eebeb4c..900ada6a20 100644 --- a/OsmAnd/src/net/osmand/plus/dialogs/ConfigureMapMenu.java +++ b/OsmAnd/src/net/osmand/plus/dialogs/ConfigureMapMenu.java @@ -37,9 +37,11 @@ import net.osmand.plus.R; import net.osmand.plus.UiUtilities; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.SettingsActivity; +import net.osmand.plus.dashboard.DashboardOnMap; import net.osmand.plus.inapp.InAppPurchaseHelper; import net.osmand.plus.poi.PoiUIFilter; import net.osmand.plus.render.RendererRegistry; +import net.osmand.plus.render.RendererRegistry.OnChangeRenderingRuleListener; import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.settings.backend.OsmandSettings.CommonPreference; import net.osmand.plus.settings.backend.OsmandSettings.ListStringPreference; @@ -98,8 +100,9 @@ import static net.osmand.render.RenderingRuleStorageProperties.UI_CATEGORY_DETAI import static net.osmand.render.RenderingRuleStorageProperties.UI_CATEGORY_HIDE; import static net.osmand.render.RenderingRuleStorageProperties.UI_CATEGORY_ROUTES; -public class ConfigureMapMenu { +public class ConfigureMapMenu implements DashboardOnMap.DashboardStateListener { private static final Log LOG = PlatformUtil.getLog(ConfigureMapMenu.class); + public static final String TAG = ConfigureMapMenu.class.getName(); public static final String HIKING_ROUTES_OSMC_ATTR = "hikingRoutesOSMC"; public static final String CURRENT_TRACK_COLOR_ATTR = "currentTrackColor"; public static final String CURRENT_TRACK_WIDTH_ATTR = "currentTrackWidth"; @@ -110,10 +113,17 @@ public class ConfigureMapMenu { private int selectedLanguageIndex; private boolean transliterateNames; + private MapActivity mapActivity; + private ContextMenuAdapter contextMenuAdapter; + public interface OnClickListener { void onClick(); } + public ConfigureMapMenu(MapActivity mapActivity) { + this.mapActivity = mapActivity; + } + public ContextMenuAdapter createListAdapter(final MapActivity ma) { OsmandApplication app = ma.getMyApplication(); boolean nightMode = app.getDaynightHelper().isNightModeForMapControls(); @@ -136,6 +146,7 @@ public class ConfigureMapMenu { adapter.setNightMode(nightMode); createLayersItems(customRules, adapter, ma, themeRes, nightMode); createRenderingAttributeItems(customRules, adapter, ma, themeRes, nightMode); + this.contextMenuAdapter = adapter; return adapter; } @@ -273,13 +284,17 @@ public class ConfigureMapMenu { final OsmandSettings settings = app.getSettings(); final int selectedProfileColorRes = settings.APPLICATION_MODE.get().getIconColorInfo().getColor(nightMode); final int selectedProfileColor = ContextCompat.getColor(app, selectedProfileColorRes); + RendererRegistry rr = app.getRendererRegistry(); + RenderingRulesStorage storage = rr.getCurrentSelectedRenderer(); + String renderDescr = getRenderDescr(activity, storage); adapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.map_widget_map_rendering, activity) .setId(MAP_RENDERING_CATEGORY_ID) .setCategory(true).setLayout(R.layout.list_group_title_with_switch).createItem()); adapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.map_widget_renderer, activity) .setId(MAP_STYLE_ID) - .setDescription(getRenderDescr(activity)).setLayout(R.layout.list_item_single_line_descrition_narrow) + .setDescription(renderDescr) + .setLayout(R.layout.list_item_single_line_descrition_narrow) .setIcon(R.drawable.ic_map).setListener(new ContextMenuAdapter.ItemClickListener() { @Override public boolean onContextMenuClick(final ArrayAdapter ad, int itemId, @@ -942,13 +957,11 @@ public class ConfigureMapMenu { dialog.show(); } - protected String getRenderDescr(final MapActivity activity) { - RendererRegistry rr = activity.getMyApplication().getRendererRegistry(); - RenderingRulesStorage storage = rr.getCurrentSelectedRenderer(); + protected String getRenderDescr(Context ctx, RenderingRulesStorage storage) { if (storage == null) { return ""; } - String translation = RendererRegistry.getTranslatedRendererName(activity, storage.getName()); + String translation = RendererRegistry.getTranslatedRendererName(ctx, storage.getName()); return translation == null ? storage.getName() : translation; } @@ -1109,6 +1122,31 @@ public class ConfigureMapMenu { } } + @Override + public void onShowDashboard() { + getMyApplication().getRendererRegistry() + .addOnChangeRenderingRuleListener(TAG, new OnChangeRenderingRuleListener() { + @Override + public void onRenderingRuleChanged(RenderingRulesStorage currentSelectedRender) { + if (contextMenuAdapter != null) { + ContextMenuItem item = contextMenuAdapter.getItemById(MAP_STYLE_ID); + String renderDescr = getRenderDescr(mapActivity, currentSelectedRender); + item.setDescription(renderDescr); + contextMenuAdapter.notifyDataSetChanged(); + } + } + }); + } + + @Override + public void onHideDashboard() { + getMyApplication().getRendererRegistry().removeOnChangeRenderingRuleListener(TAG); + } + + private OsmandApplication getMyApplication() { + return mapActivity.getMyApplication(); + } + private static class StringSpinnerArrayAdapter extends ArrayAdapter { private boolean nightMode; diff --git a/OsmAnd/src/net/osmand/plus/render/RendererRegistry.java b/OsmAnd/src/net/osmand/plus/render/RendererRegistry.java index 17dc572bc4..e20d90c27a 100644 --- a/OsmAnd/src/net/osmand/plus/render/RendererRegistry.java +++ b/OsmAnd/src/net/osmand/plus/render/RendererRegistry.java @@ -25,6 +25,7 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; @@ -49,6 +50,7 @@ public class RendererRegistry { private RenderingRulesStorage defaultRender = null; private RenderingRulesStorage currentSelectedRender = null; + private Map onChangeRenderingRuleListeners = new HashMap<>(); private Map externalRenderers = new LinkedHashMap(); private Map internalRenderers = new LinkedHashMap(); @@ -334,8 +336,22 @@ public class RendererRegistry { return currentSelectedRender; } - public void setCurrentSelectedRender(RenderingRulesStorage currentSelectedRender) { + public void setCurrentSelectedRender(final RenderingRulesStorage currentSelectedRender) { this.currentSelectedRender = currentSelectedRender; + notifyChangeRenderRuleListeners(); + } + + public void notifyChangeRenderRuleListeners() { + app.runInUIThread(new Runnable() { + @Override + public void run() { + for (OnChangeRenderingRuleListener listener : onChangeRenderingRuleListeners.values()) { + if (listener != null) { + listener.onRenderingRuleChanged(currentSelectedRender); + } + } + } + }); } public void setRendererLoadedEventListener(IRendererLoadedEventListener listener) { @@ -361,4 +377,17 @@ public class RendererRegistry { public Map getExternalRenderers() { return externalRenderers; } + + public void addOnChangeRenderingRuleListener(@NonNull String key, + @NonNull OnChangeRenderingRuleListener listener) { + onChangeRenderingRuleListeners.put(key, listener); + } + + public void removeOnChangeRenderingRuleListener(@NonNull String key) { + onChangeRenderingRuleListeners.remove(key); + } + + public interface OnChangeRenderingRuleListener { + void onRenderingRuleChanged(RenderingRulesStorage currentSelectedRender); + } } diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/ConfigureMenuItemsFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/ConfigureMenuItemsFragment.java index 44fb4bf7f6..43cd0d59eb 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/ConfigureMenuItemsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/ConfigureMenuItemsFragment.java @@ -197,7 +197,7 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment contextMenuAdapter = ((MapActivity) activity).getMapActions().createMainOptionsMenu(); break; case CONFIGURE_MAP: - ConfigureMapMenu configureMapMenu = new ConfigureMapMenu(); + ConfigureMapMenu configureMapMenu = new ConfigureMapMenu((MapActivity)activity); contextMenuAdapter = configureMapMenu.createListAdapter((MapActivity) activity); break; case CONTEXT_MENU_ACTIONS: diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/ConfigureMenuRootFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/ConfigureMenuRootFragment.java index 330f1c0a18..b47a761e8b 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/ConfigureMenuRootFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/ConfigureMenuRootFragment.java @@ -285,7 +285,7 @@ public class ConfigureMenuRootFragment extends BaseOsmAndFragment { contextMenuAdapter = mapActivityActions.createMainOptionsMenu(); break; case CONFIGURE_MAP: - ConfigureMapMenu configureMapMenu = new ConfigureMapMenu(); + ConfigureMapMenu configureMapMenu = new ConfigureMapMenu((MapActivity) activity); contextMenuAdapter = configureMapMenu.createListAdapter((MapActivity) activity); break; case CONTEXT_MENU_ACTIONS: