From 447682d319bcc7b82da9f06aa5fbbd28ac143620 Mon Sep 17 00:00:00 2001 From: Nazar-Kutz Date: Thu, 16 Apr 2020 11:12:40 +0300 Subject: [PATCH] Wikipedia fixes (After review) p.2 --- .../main/java/net/osmand/data/Amenity.java | 15 ++++---- .../builders/AmenityMenuBuilder.java | 8 ++--- .../controllers/AmenityMenuController.java | 7 +--- .../wikipedia/WikipediaDialogFragment.java | 10 +++--- .../plus/wikipedia/WikipediaPoiMenu.java | 35 ++++++++++++------- 5 files changed, 37 insertions(+), 38 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java b/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java index ab5491b371..69643cefa1 100644 --- a/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java +++ b/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java @@ -19,6 +19,8 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Set; +import java.util.TreeSet; import java.util.zip.GZIPInputStream; import gnu.trove.list.array.TIntArrayList; @@ -218,15 +220,10 @@ public class Amenity extends MapObject { return lng; } - public List getSupportedLocales(List locales) { - List supported = new ArrayList<>(); - if (locales != null) { - for (String locale : locales) { - if (!Algorithms.isEmpty(getAdditionalInfo("content:" + locale))) { - supported.add(locale); - } - } - } + public Set getSupportedLocales() { + Set supported = new TreeSet<>(); + supported.addAll(getNames("content", "en")); + supported.addAll(getNames("description", "en")); return supported; } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/AmenityMenuBuilder.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/AmenityMenuBuilder.java index d53106bf18..0c79374a30 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/AmenityMenuBuilder.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/AmenityMenuBuilder.java @@ -27,7 +27,6 @@ import net.osmand.osm.MapPoiTypes; import net.osmand.osm.PoiCategory; import net.osmand.osm.PoiType; import net.osmand.plus.OsmAndFormatter; -import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings.MetricsConstants; @@ -267,9 +266,7 @@ public class AmenityMenuBuilder extends MenuBuilder { button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - String preferredLocale = app.getSettings().MAP_PREFERRED_LOCALE.get(); - WikipediaDialogFragment.showInstance(mapActivity, amenity, - WikipediaPoiMenu.getWikiArticleLocale(app, preferredLocale, amenity)); + WikipediaDialogFragment.showInstance(mapActivity, amenity, null); } }); button.setAllCaps(true); @@ -408,7 +405,8 @@ public class AmenityMenuBuilder extends MenuBuilder { if (amenity.getType().isWiki()) { if (!hasWiki) { - String lng = amenity.getContentLanguage("content", preferredLang, "en"); + String articleLang = WikipediaPoiMenu.getWikiArticleLanguage(app, amenity.getSupportedLocales(), preferredLang); + String lng = amenity.getContentLanguage("content", articleLang, "en"); if (Algorithms.isEmpty(lng)) { lng = "en"; } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AmenityMenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AmenityMenuController.java index 5e7d45263d..d49f7e93f4 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AmenityMenuController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AmenityMenuController.java @@ -13,7 +13,6 @@ import net.osmand.osm.PoiCategory; import net.osmand.osm.PoiFilter; import net.osmand.osm.PoiType; import net.osmand.plus.MapMarkersHelper.MapMarker; -import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.mapcontextmenu.MenuBuilder; @@ -22,7 +21,6 @@ import net.osmand.plus.mapcontextmenu.builders.AmenityMenuBuilder; import net.osmand.plus.render.RenderingIcons; import net.osmand.plus.transport.TransportStopRoute; import net.osmand.plus.wikipedia.WikipediaDialogFragment; -import net.osmand.plus.wikipedia.WikipediaPoiMenu; import net.osmand.util.Algorithms; import net.osmand.util.OpeningHoursParser; @@ -74,10 +72,7 @@ public class AmenityMenuController extends MenuController { public void buttonPressed() { MapActivity activity = getMapActivity(); if (activity != null) { - OsmandApplication app = activity.getMyApplication(); - String preferredLocale = app.getSettings().MAP_PREFERRED_LOCALE.get(); - WikipediaDialogFragment.showInstance(activity, amenity, - WikipediaPoiMenu.getWikiArticleLocale(app, preferredLocale, amenity)); + WikipediaDialogFragment.showInstance(activity, amenity, null); } } }; diff --git a/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaDialogFragment.java b/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaDialogFragment.java index f2f162a76e..987e49057b 100644 --- a/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaDialogFragment.java @@ -44,7 +44,6 @@ import java.io.File; import java.util.HashMap; import java.util.Map; import java.util.Set; -import java.util.TreeSet; public class WikipediaDialogFragment extends WikiArticleBaseDialogFragment { @@ -231,9 +230,7 @@ public class WikipediaDialogFragment extends WikiArticleBaseDialogFragment { Context context = getContext(); if (context != null) { final PopupMenu optionsMenu = new PopupMenu(context, view, Gravity.RIGHT); - Set namesSet = new TreeSet<>(); - namesSet.addAll(amenity.getNames("content", "en")); - namesSet.addAll(amenity.getNames("description", "en")); + Set namesSet = amenity.getSupportedLocales(); Map names = new HashMap<>(); for (String n : namesSet) { @@ -285,8 +282,9 @@ public class WikipediaDialogFragment extends WikiArticleBaseDialogFragment { WikipediaDialogFragment wikipediaDialogFragment = new WikipediaDialogFragment(); wikipediaDialogFragment.setAmenity(amenity); - wikipediaDialogFragment.setLanguage(lang == null ? - app.getSettings().MAP_PREFERRED_LOCALE.get() : lang); + lang = lang != null ? lang : WikipediaPoiMenu.getWikiArticleLanguage(app, + amenity.getSupportedLocales(), app.getSettings().MAP_PREFERRED_LOCALE.get()); + wikipediaDialogFragment.setLanguage(lang); wikipediaDialogFragment.setRetainInstance(true); wikipediaDialogFragment.show(activity.getSupportFragmentManager(), TAG); return true; diff --git a/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaPoiMenu.java b/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaPoiMenu.java index a89b3fadc9..eb56a9c397 100644 --- a/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaPoiMenu.java +++ b/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaPoiMenu.java @@ -1,12 +1,12 @@ package net.osmand.plus.wikipedia; import android.os.Bundle; +import android.text.TextUtils; import android.widget.ArrayAdapter; import androidx.annotation.NonNull; import net.osmand.CallbackWithObject; -import net.osmand.data.Amenity; import net.osmand.plus.ApplicationMode; import net.osmand.plus.ContextMenuAdapter; import net.osmand.plus.ContextMenuItem; @@ -27,6 +27,7 @@ import java.io.IOException; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; +import java.util.Set; import static net.osmand.osm.MapPoiTypes.WIKI_LANG; import static net.osmand.plus.poi.PoiFiltersHelper.PoiTemplateList; @@ -381,20 +382,30 @@ public class WikipediaPoiMenu { return new WikipediaPoiMenu(mapActivity).createLayersItems(); } - public static String getWikiArticleLocale(OsmandApplication app, String preferredLocale, Amenity amenity) { + public static String getWikiArticleLanguage(@NonNull OsmandApplication app, + @NonNull Set availableArticleLangs, + String preferredLanguage) { Bundle wikiPoiSettings = getWikiPoiSettings(app); - if (wikiPoiSettings != null) { - boolean globalWikiPoiEnabled = wikiPoiSettings.getBoolean(GLOBAL_WIKI_POI_ENABLED_KEY); - List enabledWikiPoiLocales = wikiPoiSettings.getStringArrayList(ENABLED_WIKI_POI_LANGUAGES_KEY); - if (!globalWikiPoiEnabled && enabledWikiPoiLocales != null) { - if (!enabledWikiPoiLocales.contains(preferredLocale) && amenity.getType().isWiki()) { - List supportedLanguages = amenity.getSupportedLocales(enabledWikiPoiLocales); - if (supportedLanguages != null && supportedLanguages.size() > 0) { - preferredLocale = supportedLanguages.get(0); - } + if (!app.getSettings().SHOW_WIKIPEDIA_POI.get() || wikiPoiSettings == null) { + // Wikipedia POI setting disabled + return preferredLanguage; + } + if (wikiPoiSettings.getBoolean(GLOBAL_WIKI_POI_ENABLED_KEY)) { + // global Wikipedia POI filter enabled + return preferredLanguage; + } + if (TextUtils.isEmpty(preferredLanguage)) { + preferredLanguage = app.getLanguage(); + } + List wikiLangs = wikiPoiSettings.getStringArrayList(ENABLED_WIKI_POI_LANGUAGES_KEY); + if (wikiLangs != null && !wikiLangs.contains(preferredLanguage)) { + // return first matched language from enabled Wikipedia languages + for (String language : wikiLangs) { + if (availableArticleLangs.contains(language)) { + return language; } } } - return preferredLocale; + return preferredLanguage; } }