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.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<String> getSupportedLocales(List<String> locales) {
List<String> supported = new ArrayList<>();
if (locales != null) {
for (String locale : locales) {
if (!Algorithms.isEmpty(getAdditionalInfo("content:" + locale))) {
supported.add(locale);
}
}
}
public Set<String> getSupportedLocales() {
Set<String> supported = new TreeSet<>();
supported.addAll(getNames("content", "en"));
supported.addAll(getNames("description", "en"));
return supported;
}

View file

@ -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";
}

View file

@ -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);
}
}
};

View file

@ -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<String> namesSet = new TreeSet<>();
namesSet.addAll(amenity.getNames("content", "en"));
namesSet.addAll(amenity.getNames("description", "en"));
Set<String> namesSet = amenity.getSupportedLocales();
Map<String, String> 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;

View file

@ -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<String> availableArticleLangs,
String preferredLanguage) {
Bundle wikiPoiSettings = getWikiPoiSettings(app);
if (wikiPoiSettings != null) {
boolean globalWikiPoiEnabled = wikiPoiSettings.getBoolean(GLOBAL_WIKI_POI_ENABLED_KEY);
List<String> enabledWikiPoiLocales = wikiPoiSettings.getStringArrayList(ENABLED_WIKI_POI_LANGUAGES_KEY);
if (!globalWikiPoiEnabled && enabledWikiPoiLocales != null) {
if (!enabledWikiPoiLocales.contains(preferredLocale) && amenity.getType().isWiki()) {
List<String> 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<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 preferredLocale;
return preferredLanguage;
}
}