Merge pull request #5295 from osmandapp/PaulsBranch

Open wiki article in the same language as wikivoyage
This commit is contained in:
vshcherb 2018-04-28 13:12:14 +03:00 committed by GitHub
commit 4ecdfad6a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 9 deletions

View file

@ -271,17 +271,17 @@ public class WikipediaDialogFragment extends DialogFragment {
return (OsmandApplication) getActivity().getApplication(); return (OsmandApplication) getActivity().getApplication();
} }
public static boolean showInstance(AppCompatActivity activity, Amenity amenity) { public static boolean showInstance(AppCompatActivity activity, Amenity amenity, String lang) {
try { try {
if (!amenity.getType().isWiki()) { if (!amenity.getType().isWiki()) {
return false; return false;
} }
OsmandApplication app = (OsmandApplication) activity.getApplication(); OsmandApplication app = (OsmandApplication) activity.getApplication();
String lang = app.getSettings().MAP_PREFERRED_LOCALE.get();
WikipediaDialogFragment wikipediaDialogFragment = new WikipediaDialogFragment(); WikipediaDialogFragment wikipediaDialogFragment = new WikipediaDialogFragment();
wikipediaDialogFragment.setAmenity(amenity); wikipediaDialogFragment.setAmenity(amenity);
wikipediaDialogFragment.setLanguage(lang); wikipediaDialogFragment.setLanguage(lang == null ?
app.getSettings().MAP_PREFERRED_LOCALE.get() : lang);
wikipediaDialogFragment.setRetainInstance(true); wikipediaDialogFragment.setRetainInstance(true);
wikipediaDialogFragment.show(activity.getSupportFragmentManager(), TAG); wikipediaDialogFragment.show(activity.getSupportFragmentManager(), TAG);
return true; return true;
@ -289,4 +289,8 @@ public class WikipediaDialogFragment extends DialogFragment {
return false; return false;
} }
} }
public static boolean showInstance(AppCompatActivity activity, Amenity amenity) {
return showInstance(activity, amenity, null);
}
} }

View file

@ -91,8 +91,9 @@ public class WikivoyageWebViewClient extends WebViewClient implements RegionCall
} }
return true; return true;
} else if (url.contains(WIKI_DOMAIN)) { } else if (url.contains(WIKI_DOMAIN)) {
String articleName = getArticleNameFromUrl(url, getLang(url)); String lang = getLang(url);
getWikiArticle(articleName, url); String articleName = getArticleNameFromUrl(url, lang);
getWikiArticle(articleName, lang, url);
} else if (url.startsWith(PAGE_PREFIX_HTTP) || url.startsWith(PAGE_PREFIX_HTTPS)) { } else if (url.startsWith(PAGE_PREFIX_HTTP) || url.startsWith(PAGE_PREFIX_HTTPS)) {
warnAboutExternalLoad(url, context, nightMode); warnAboutExternalLoad(url, context, nightMode);
} else if (url.startsWith(PREFIX_GEO)) { } else if (url.startsWith(PREFIX_GEO)) {
@ -181,14 +182,14 @@ public class WikivoyageWebViewClient extends WebViewClient implements RegionCall
} }
} }
private void getWikiArticle(String name, String url) { private void getWikiArticle(String name, String lang, String url) {
List<AmenityIndexRepositoryBinary> indexes = app.getResourceManager() List<AmenityIndexRepositoryBinary> indexes = app.getResourceManager()
.getWikiAmenityRepository(article.getLat(), article.getLon()); .getWikiAmenityRepository(article.getLat(), article.getLon());
if (indexes.isEmpty()) { if (indexes.isEmpty()) {
WikivoyageArticleWikiLinkFragment.showInstance(fragmentManager, regionName == null ? WikivoyageArticleWikiLinkFragment.showInstance(fragmentManager, regionName == null ?
"" : regionName, url); "" : regionName, url);
} else { } else {
articleSearchTask = new WikiArticleSearchTask(name, indexes, (MapActivity) context, nightMode, url); articleSearchTask = new WikiArticleSearchTask(name, lang, indexes, (MapActivity) context, nightMode, url);
articleSearchTask.execute(); articleSearchTask.execute();
} }
@ -274,10 +275,12 @@ public class WikivoyageWebViewClient extends WebViewClient implements RegionCall
private WeakReference<MapActivity> weakContext; private WeakReference<MapActivity> weakContext;
private boolean isNightMode; private boolean isNightMode;
private String url; private String url;
private String lang;
WikiArticleSearchTask(String articleName, List<AmenityIndexRepositoryBinary> indexes, WikiArticleSearchTask(String articleName, String lang, List<AmenityIndexRepositoryBinary> indexes,
MapActivity context, boolean isNightMode, String url) { MapActivity context, boolean isNightMode, String url) {
name = articleName; name = articleName;
this.lang = lang;
this.indexes = indexes; this.indexes = indexes;
weakContext = new WeakReference<>(context); weakContext = new WeakReference<>(context);
dialog = createProgressDialog(); dialog = createProgressDialog();
@ -317,7 +320,7 @@ public class WikivoyageWebViewClient extends WebViewClient implements RegionCall
if (activity != null && !activity.isActivityDestroyed() && dialog != null) { if (activity != null && !activity.isActivityDestroyed() && dialog != null) {
dialog.dismiss(); dialog.dismiss();
if (!found.isEmpty()) { if (!found.isEmpty()) {
WikipediaDialogFragment.showInstance(activity, found.get(0)); WikipediaDialogFragment.showInstance(activity, found.get(0), lang);
} else { } else {
warnAboutExternalLoad(url, weakContext.get(), isNightMode); warnAboutExternalLoad(url, weakContext.get(), isNightMode);
} }