From ba5bb6dc9c0d88b34a2263826b430444447e1d24 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Thu, 2 Jul 2020 17:09:07 +0300 Subject: [PATCH] Move custom poi filters to plugin --- OsmAnd/src/net/osmand/plus/OsmandPlugin.java | 28 +++++++++++------ .../net/osmand/plus/poi/PoiFiltersHelper.java | 13 ++------ .../SelectWikiLanguagesBottomSheet.java | 10 ++---- .../plus/wikipedia/WikipediaPlugin.java | 31 ++++++++++++------- 4 files changed, 44 insertions(+), 38 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/OsmandPlugin.java b/OsmAnd/src/net/osmand/plus/OsmandPlugin.java index 4cbfe665e2..bf9eccf4b4 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandPlugin.java +++ b/OsmAnd/src/net/osmand/plus/OsmandPlugin.java @@ -207,6 +207,10 @@ public abstract class OsmandPlugin { return Collections.emptyList(); } + protected List getCustomPoiFilters() { + return Collections.emptyList(); + } + /** * Plugin was installed */ @@ -550,7 +554,7 @@ public abstract class OsmandPlugin { protected void newDownloadIndexes(Fragment fragment) { } - protected void prepareExtraTopPoiFilters(PoiUIFilter ... filters) { + protected void prepareExtraTopPoiFilters(Set poiUIFilter) { } protected String getMapObjectsLocale(Amenity amenity, String preferredLocale) { @@ -820,26 +824,26 @@ public abstract class OsmandPlugin { public static boolean onSearchFinished(QuickSearchDialogFragment searchFragment, SearchPhrase phrase, boolean isResultEmpty) { boolean processed = false; - for (OsmandPlugin plugin : getAvailablePlugins()) { + for (OsmandPlugin plugin : getEnabledPlugins()) { processed = plugin.searchFinished(searchFragment, phrase, isResultEmpty) || processed; } return processed; } public static void onNewDownloadIndexes(Fragment fragment) { - for (OsmandPlugin plugin : getAvailablePlugins()) { + for (OsmandPlugin plugin : getEnabledPlugins()) { plugin.newDownloadIndexes(fragment); } } - public static void onPrepareExtraTopPoiFilters(PoiUIFilter ... filters) { - for (OsmandPlugin plugin : getAvailablePlugins()) { - plugin.prepareExtraTopPoiFilters(filters); + public static void onPrepareExtraTopPoiFilters(Set poiUIFilters) { + for (OsmandPlugin plugin : getEnabledPlugins()) { + plugin.prepareExtraTopPoiFilters(poiUIFilters); } } public static String onGetMapObjectPreferredLang(MapObject object, String preferredMapLang, String preferredMapAppLang) { - for (OsmandPlugin plugin : getAvailablePlugins()) { + for (OsmandPlugin plugin : getEnabledPlugins()) { String locale = plugin.getMapObjectPreferredLang(object, preferredMapLang); if (locale != null) { return locale; @@ -849,7 +853,7 @@ public abstract class OsmandPlugin { } public static String onGetMapObjectsLocale(Amenity amenity, String preferredLocale) { - for (OsmandPlugin plugin : getAvailablePlugins()) { + for (OsmandPlugin plugin : getEnabledPlugins()) { String locale = plugin.getMapObjectsLocale(amenity, preferredLocale); if (locale != null) { return locale; @@ -858,6 +862,12 @@ public abstract class OsmandPlugin { return preferredLocale; } + public static void registerCustomPoiFilters(List poiUIFilters) { + for (OsmandPlugin p : getEnabledPlugins()) { + poiUIFilters.addAll(p.getCustomPoiFilters()); + } + } + public static Collection getPluginsCardsList() { HashSet collection = new HashSet<>(); for (OsmandPlugin plugin : getEnabledPlugins()) { @@ -879,8 +889,6 @@ public abstract class OsmandPlugin { return installed; } - - public static boolean onMapActivityKeyUp(MapActivity mapActivity, int keyCode) { for (OsmandPlugin p : getEnabledPlugins()) { if (p.mapActivityKeyUp(mapActivity, keyCode)) diff --git a/OsmAnd/src/net/osmand/plus/poi/PoiFiltersHelper.java b/OsmAnd/src/net/osmand/plus/poi/PoiFiltersHelper.java index ea6a7c1e6e..a67d5554a6 100644 --- a/OsmAnd/src/net/osmand/plus/poi/PoiFiltersHelper.java +++ b/OsmAnd/src/net/osmand/plus/poi/PoiFiltersHelper.java @@ -111,10 +111,6 @@ public class PoiFiltersHelper { return customPOIFilter; } - public void prepareExtraTopPoiFilters(PoiUIFilter ... filters) { - OsmandPlugin.onPrepareExtraTopPoiFilters(filters); - } - public PoiUIFilter getTopWikiPoiFilter() { if (topWikiPoiFilter == null) { String wikiFilterId = PoiUIFilter.STD_PREFIX + "osmwiki"; @@ -244,6 +240,7 @@ public class PoiFiltersHelper { PoiUIFilter f = new PoiUIFilter(t, application, ""); top.add(f); } + OsmandPlugin.registerCustomPoiFilters(top); this.cacheTopStandardFilters = top; } List result = new ArrayList<>(); @@ -459,11 +456,9 @@ public class PoiFiltersHelper { } public void addSelectedPoiFilter(PoiUIFilter filter) { - if (filter.isTopWikiFilter()) { - prepareExtraTopPoiFilters(filter); - } Set selectedPoiFilters = new TreeSet<>(this.selectedPoiFilters); selectedPoiFilters.add(filter); + OsmandPlugin.onPrepareExtraTopPoiFilters(selectedPoiFilters); saveSelectedPoiFilters(selectedPoiFilters); this.selectedPoiFilters = selectedPoiFilters; } @@ -553,12 +548,10 @@ public class PoiFiltersHelper { for (String f : application.getSettings().getSelectedPoiFilters()) { PoiUIFilter filter = getFilterById(f); if (filter != null) { - if (filter.isTopWikiFilter()) { - prepareExtraTopPoiFilters(filter); - } selectedPoiFilters.add(filter); } } + OsmandPlugin.onPrepareExtraTopPoiFilters(selectedPoiFilters); this.selectedPoiFilters = selectedPoiFilters; } diff --git a/OsmAnd/src/net/osmand/plus/wikipedia/SelectWikiLanguagesBottomSheet.java b/OsmAnd/src/net/osmand/plus/wikipedia/SelectWikiLanguagesBottomSheet.java index 86d2c74fc8..873feba53c 100644 --- a/OsmAnd/src/net/osmand/plus/wikipedia/SelectWikiLanguagesBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/wikipedia/SelectWikiLanguagesBottomSheet.java @@ -13,10 +13,8 @@ import androidx.core.os.ConfigurationCompat; import androidx.core.os.LocaleListCompat; import net.osmand.AndroidUtils; -import net.osmand.plus.OsmandPlugin; -import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.OsmandApplication; -import net.osmand.plus.settings.backend.OsmandSettings; +import net.osmand.plus.OsmandPlugin; import net.osmand.plus.R; import net.osmand.plus.UiUtilities; import net.osmand.plus.activities.MapActivity; @@ -26,6 +24,7 @@ import net.osmand.plus.base.bottomsheetmenu.simpleitems.DividerItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.DividerSpaceItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.LongDescriptionItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem; +import net.osmand.plus.settings.backend.ApplicationMode; import java.util.ArrayList; import java.util.Collections; @@ -40,7 +39,6 @@ public class SelectWikiLanguagesBottomSheet extends MenuBottomSheetDialogFragmen private OsmandApplication app; private ApplicationMode appMode; - private OsmandSettings settings; private WikipediaPlugin wikiPlugin; private List languageItems; @@ -52,8 +50,7 @@ public class SelectWikiLanguagesBottomSheet extends MenuBottomSheetDialogFragmen public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); app = requiredMyApplication(); - settings = app.getSettings(); - appMode = settings.getApplicationMode(); + appMode = app.getSettings().getApplicationMode(); wikiPlugin = OsmandPlugin.getPlugin(WikipediaPlugin.class); initLanguagesData(); } @@ -185,7 +182,6 @@ public class SelectWikiLanguagesBottomSheet extends MenuBottomSheetDialogFragmen @Override protected void onRightBottomButtonClick() { - super.onRightBottomButtonClick(); List localesForSaving = new ArrayList<>(); for (WikiLanguageItem language : languages) { if (language.isChecked()) { diff --git a/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaPlugin.java b/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaPlugin.java index 5b67d37e2e..6726036dde 100644 --- a/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaPlugin.java +++ b/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaPlugin.java @@ -48,10 +48,12 @@ import static net.osmand.osm.MapPoiTypes.WIKI_LANG; public class WikipediaPlugin extends OsmandPlugin { + public static final String ID = "osmand.wikipedia"; + private MapActivity mapActivity; private OsmandSettings settings; - public static final String ID = "osmand.wikipedia"; + private PoiUIFilter topWikiPoiFilter; public WikipediaPlugin(OsmandApplication app) { super(app); @@ -144,6 +146,18 @@ public class WikipediaPlugin extends OsmandPlugin { .setListener(listener).createItem()); } + @Override + protected List getCustomPoiFilters() { + List poiFilters = new ArrayList<>(); + if (topWikiPoiFilter == null) { + AbstractPoiType poiType = app.getPoiTypes().getOsmwiki(); + topWikiPoiFilter = new PoiUIFilter(poiType, app, ""); + } + poiFilters.add(topWikiPoiFilter); + + return poiFilters; + } + public void updateWikipediaState() { if (isShowAllLanguages() || hasLanguagesFilter()) { refreshWikiOnMap(); @@ -359,8 +373,8 @@ public class WikipediaPlugin extends OsmandPlugin { } @Override - protected void prepareExtraTopPoiFilters(PoiUIFilter... filters) { - for (PoiUIFilter filter : filters) { + protected void prepareExtraTopPoiFilters(Set poiUIFilters) { + for (PoiUIFilter filter : poiUIFilters) { if (filter.isTopWikiFilter()) { boolean prepareByDefault = true; if (hasCustomSettings()) { @@ -388,17 +402,12 @@ public class WikipediaPlugin extends OsmandPlugin { Object obj = phrase.getLastSelectedWord().getResult().object; if (obj instanceof PoiUIFilter) { PoiUIFilter pf = (PoiUIFilter) obj; - if (pf.isWikiFilter()) { - return true; - } + return pf.isWikiFilter(); } else if (obj instanceof AbstractPoiType) { AbstractPoiType pt = (AbstractPoiType) obj; - if (pt.getKeyName().startsWith(WIKI_LANG)) { - return true; - } + return pt.getKeyName().startsWith(WIKI_LANG); } } return false; } - -} +} \ No newline at end of file