Wikipedia fixes (After review) p.2

This commit is contained in:
Nazar-Kutz 2020-04-16 11:12:40 +03:00
parent b389513d7d
commit 447682d319
5 changed files with 37 additions and 38 deletions

View file

@ -19,6 +19,8 @@ import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import gnu.trove.list.array.TIntArrayList; import gnu.trove.list.array.TIntArrayList;
@ -218,15 +220,10 @@ public class Amenity extends MapObject {
return lng; return lng;
} }
public List<String> getSupportedLocales(List<String> locales) { public Set<String> getSupportedLocales() {
List<String> supported = new ArrayList<>(); Set<String> supported = new TreeSet<>();
if (locales != null) { supported.addAll(getNames("content", "en"));
for (String locale : locales) { supported.addAll(getNames("description", "en"));
if (!Algorithms.isEmpty(getAdditionalInfo("content:" + locale))) {
supported.add(locale);
}
}
}
return supported; return supported;
} }

View file

@ -27,7 +27,6 @@ import net.osmand.osm.MapPoiTypes;
import net.osmand.osm.PoiCategory; import net.osmand.osm.PoiCategory;
import net.osmand.osm.PoiType; import net.osmand.osm.PoiType;
import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin; import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings;
import net.osmand.plus.OsmandSettings.MetricsConstants; import net.osmand.plus.OsmandSettings.MetricsConstants;
@ -267,9 +266,7 @@ public class AmenityMenuBuilder extends MenuBuilder {
button.setOnClickListener(new View.OnClickListener() { button.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
String preferredLocale = app.getSettings().MAP_PREFERRED_LOCALE.get(); WikipediaDialogFragment.showInstance(mapActivity, amenity, null);
WikipediaDialogFragment.showInstance(mapActivity, amenity,
WikipediaPoiMenu.getWikiArticleLocale(app, preferredLocale, amenity));
} }
}); });
button.setAllCaps(true); button.setAllCaps(true);
@ -408,7 +405,8 @@ public class AmenityMenuBuilder extends MenuBuilder {
if (amenity.getType().isWiki()) { if (amenity.getType().isWiki()) {
if (!hasWiki) { 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)) { if (Algorithms.isEmpty(lng)) {
lng = "en"; lng = "en";
} }

View file

@ -13,7 +13,6 @@ import net.osmand.osm.PoiCategory;
import net.osmand.osm.PoiFilter; import net.osmand.osm.PoiFilter;
import net.osmand.osm.PoiType; import net.osmand.osm.PoiType;
import net.osmand.plus.MapMarkersHelper.MapMarker; import net.osmand.plus.MapMarkersHelper.MapMarker;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.mapcontextmenu.MenuBuilder; 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.render.RenderingIcons;
import net.osmand.plus.transport.TransportStopRoute; import net.osmand.plus.transport.TransportStopRoute;
import net.osmand.plus.wikipedia.WikipediaDialogFragment; import net.osmand.plus.wikipedia.WikipediaDialogFragment;
import net.osmand.plus.wikipedia.WikipediaPoiMenu;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import net.osmand.util.OpeningHoursParser; import net.osmand.util.OpeningHoursParser;
@ -74,10 +72,7 @@ public class AmenityMenuController extends MenuController {
public void buttonPressed() { public void buttonPressed() {
MapActivity activity = getMapActivity(); MapActivity activity = getMapActivity();
if (activity != null) { if (activity != null) {
OsmandApplication app = activity.getMyApplication(); WikipediaDialogFragment.showInstance(activity, amenity, null);
String preferredLocale = app.getSettings().MAP_PREFERRED_LOCALE.get();
WikipediaDialogFragment.showInstance(activity, amenity,
WikipediaPoiMenu.getWikiArticleLocale(app, preferredLocale, amenity));
} }
} }
}; };

View file

@ -44,7 +44,6 @@ import java.io.File;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TreeSet;
public class WikipediaDialogFragment extends WikiArticleBaseDialogFragment { public class WikipediaDialogFragment extends WikiArticleBaseDialogFragment {
@ -231,9 +230,7 @@ public class WikipediaDialogFragment extends WikiArticleBaseDialogFragment {
Context context = getContext(); Context context = getContext();
if (context != null) { if (context != null) {
final PopupMenu optionsMenu = new PopupMenu(context, view, Gravity.RIGHT); final PopupMenu optionsMenu = new PopupMenu(context, view, Gravity.RIGHT);
Set<String> namesSet = new TreeSet<>(); Set<String> namesSet = amenity.getSupportedLocales();
namesSet.addAll(amenity.getNames("content", "en"));
namesSet.addAll(amenity.getNames("description", "en"));
Map<String, String> names = new HashMap<>(); Map<String, String> names = new HashMap<>();
for (String n : namesSet) { for (String n : namesSet) {
@ -285,8 +282,9 @@ public class WikipediaDialogFragment extends WikiArticleBaseDialogFragment {
WikipediaDialogFragment wikipediaDialogFragment = new WikipediaDialogFragment(); WikipediaDialogFragment wikipediaDialogFragment = new WikipediaDialogFragment();
wikipediaDialogFragment.setAmenity(amenity); wikipediaDialogFragment.setAmenity(amenity);
wikipediaDialogFragment.setLanguage(lang == null ? lang = lang != null ? lang : WikipediaPoiMenu.getWikiArticleLanguage(app,
app.getSettings().MAP_PREFERRED_LOCALE.get() : lang); amenity.getSupportedLocales(), app.getSettings().MAP_PREFERRED_LOCALE.get());
wikipediaDialogFragment.setLanguage(lang);
wikipediaDialogFragment.setRetainInstance(true); wikipediaDialogFragment.setRetainInstance(true);
wikipediaDialogFragment.show(activity.getSupportFragmentManager(), TAG); wikipediaDialogFragment.show(activity.getSupportFragmentManager(), TAG);
return true; return true;

View file

@ -1,12 +1,12 @@
package net.osmand.plus.wikipedia; package net.osmand.plus.wikipedia;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import net.osmand.CallbackWithObject; import net.osmand.CallbackWithObject;
import net.osmand.data.Amenity;
import net.osmand.plus.ApplicationMode; import net.osmand.plus.ApplicationMode;
import net.osmand.plus.ContextMenuAdapter; import net.osmand.plus.ContextMenuAdapter;
import net.osmand.plus.ContextMenuItem; import net.osmand.plus.ContextMenuItem;
@ -27,6 +27,7 @@ import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set;
import static net.osmand.osm.MapPoiTypes.WIKI_LANG; import static net.osmand.osm.MapPoiTypes.WIKI_LANG;
import static net.osmand.plus.poi.PoiFiltersHelper.PoiTemplateList; import static net.osmand.plus.poi.PoiFiltersHelper.PoiTemplateList;
@ -381,20 +382,30 @@ public class WikipediaPoiMenu {
return new WikipediaPoiMenu(mapActivity).createLayersItems(); return new WikipediaPoiMenu(mapActivity).createLayersItems();
} }
public static String getWikiArticleLocale(OsmandApplication app, String preferredLocale, Amenity amenity) { public static String getWikiArticleLanguage(@NonNull OsmandApplication app,
@NonNull Set<String> availableArticleLangs,
String preferredLanguage) {
Bundle wikiPoiSettings = getWikiPoiSettings(app); Bundle wikiPoiSettings = getWikiPoiSettings(app);
if (wikiPoiSettings != null) { if (!app.getSettings().SHOW_WIKIPEDIA_POI.get() || wikiPoiSettings == null) {
boolean globalWikiPoiEnabled = wikiPoiSettings.getBoolean(GLOBAL_WIKI_POI_ENABLED_KEY); // Wikipedia POI setting disabled
List<String> enabledWikiPoiLocales = wikiPoiSettings.getStringArrayList(ENABLED_WIKI_POI_LANGUAGES_KEY); return preferredLanguage;
if (!globalWikiPoiEnabled && enabledWikiPoiLocales != null) { }
if (!enabledWikiPoiLocales.contains(preferredLocale) && amenity.getType().isWiki()) { if (wikiPoiSettings.getBoolean(GLOBAL_WIKI_POI_ENABLED_KEY)) {
List<String> supportedLanguages = amenity.getSupportedLocales(enabledWikiPoiLocales); // global Wikipedia POI filter enabled
if (supportedLanguages != null && supportedLanguages.size() > 0) { return preferredLanguage;
preferredLocale = supportedLanguages.get(0); }
if (TextUtils.isEmpty(preferredLanguage)) {
preferredLanguage = app.getLanguage();
}
List<String> 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 preferredLanguage;
return preferredLocale;
} }
} }