Improved wiki articles search
This commit is contained in:
parent
c6c9853396
commit
a77238a831
2 changed files with 35 additions and 37 deletions
|
@ -852,17 +852,15 @@ public class ResourceManager {
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
public AmenityIndexRepositoryBinary getWikiRepositoryByRegionName(String name) {
|
public AmenityIndexRepositoryBinary getWikiRepositoryByRegionName(List<String> regionNames) {
|
||||||
if (name == null || name.isEmpty())
|
if (regionNames == null || regionNames.isEmpty())
|
||||||
return null;
|
return null;
|
||||||
for (String filename : amenityRepositories.keySet()) {
|
for (String regionName : regionNames) {
|
||||||
if ((filename.contains("_wiki")
|
AmenityIndexRepository repository = amenityRepositories.get(Algorithms
|
||||||
|| filename.contains(IndexConstants.BINARY_WIKI_MAP_INDEX_EXT))
|
.capitalizeFirstLetterAndLowercase(regionName) +
|
||||||
&& filename.toLowerCase().contains(name)) {
|
IndexConstants.BINARY_WIKI_MAP_INDEX_EXT);
|
||||||
AmenityIndexRepository repository = amenityRepositories.get(filename);
|
if (repository != null && repository instanceof AmenityIndexRepositoryBinary) {
|
||||||
if (repository != null && repository instanceof AmenityIndexRepositoryBinary) {
|
return (AmenityIndexRepositoryBinary) repository;
|
||||||
return (AmenityIndexRepositoryBinary) repository;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|
|
@ -18,6 +18,7 @@ import android.webkit.WebViewClient;
|
||||||
|
|
||||||
import net.osmand.binary.BinaryMapDataObject;
|
import net.osmand.binary.BinaryMapDataObject;
|
||||||
import net.osmand.data.Amenity;
|
import net.osmand.data.Amenity;
|
||||||
|
import net.osmand.data.LatLon;
|
||||||
import net.osmand.data.PointDescription;
|
import net.osmand.data.PointDescription;
|
||||||
import net.osmand.map.OsmandRegions;
|
import net.osmand.map.OsmandRegions;
|
||||||
import net.osmand.plus.GPXUtilities;
|
import net.osmand.plus.GPXUtilities;
|
||||||
|
@ -45,7 +46,7 @@ import static android.support.v4.app.FragmentManager.POP_BACK_STACK_INCLUSIVE;
|
||||||
|
|
||||||
|
|
||||||
interface RegionCallback {
|
interface RegionCallback {
|
||||||
void onRegionFound(String[] s);
|
void onRegionFound(String s, List<String> names);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Custom WebView client to handle the internal links.
|
* Custom WebView client to handle the internal links.
|
||||||
|
@ -54,6 +55,7 @@ interface RegionCallback {
|
||||||
public class WikivoyageWebViewClient extends WebViewClient implements RegionCallback {
|
public class WikivoyageWebViewClient extends WebViewClient implements RegionCallback {
|
||||||
|
|
||||||
private static final String TAG = WikivoyageWebViewClient.class.getSimpleName();
|
private static final String TAG = WikivoyageWebViewClient.class.getSimpleName();
|
||||||
|
public static final int COUNTRY_LEVEL = 2;
|
||||||
|
|
||||||
private OsmandApplication app;
|
private OsmandApplication app;
|
||||||
private FragmentManager fragmentManager;
|
private FragmentManager fragmentManager;
|
||||||
|
@ -61,7 +63,7 @@ public class WikivoyageWebViewClient extends WebViewClient implements RegionCall
|
||||||
private TravelArticle article;
|
private TravelArticle article;
|
||||||
private boolean nightMode;
|
private boolean nightMode;
|
||||||
private String regionName;
|
private String regionName;
|
||||||
private String regionDownloadName;
|
private List<String> regionDownloadNames;
|
||||||
|
|
||||||
private static final String PREFIX_GEO = "geo:";
|
private static final String PREFIX_GEO = "geo:";
|
||||||
private static final String PAGE_PREFIX_HTTP = "http://";
|
private static final String PAGE_PREFIX_HTTP = "http://";
|
||||||
|
@ -186,21 +188,19 @@ public class WikivoyageWebViewClient extends WebViewClient implements RegionCall
|
||||||
|
|
||||||
private void getWikiArticle(String name, String lang, String url) {
|
private void getWikiArticle(String name, String lang, String url) {
|
||||||
articleSearchTask = new WikiArticleSearchTask(article, name, regionName, fragmentManager,
|
articleSearchTask = new WikiArticleSearchTask(article, name, regionName, fragmentManager,
|
||||||
lang, regionDownloadName, (MapActivity) context, nightMode, url);
|
lang, regionDownloadNames, (MapActivity) context, nightMode, url);
|
||||||
articleSearchTask.execute();
|
articleSearchTask.execute();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onRegionFound(String[] s) {
|
public void onRegionFound(String s, List<String> names) {
|
||||||
if (s != null) {
|
regionDownloadNames = names;
|
||||||
regionDownloadName = s[0];
|
regionName = s;
|
||||||
regionName = s[1];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getDownloadName(double lat, double lon, OsmandRegions regions) {
|
private static List<String> getDownloadNames(double lat, double lon, OsmandRegions regions) {
|
||||||
String downloadName = null;
|
List<String> regionNames = new ArrayList<>();
|
||||||
if (regions != null) {
|
if (regions != null) {
|
||||||
int x31 = MapUtils.get31TileNumberX(lon);
|
int x31 = MapUtils.get31TileNumberX(lon);
|
||||||
int y31 = MapUtils.get31TileNumberY(lat);
|
int y31 = MapUtils.get31TileNumberY(lat);
|
||||||
|
@ -213,15 +213,15 @@ public class WikivoyageWebViewClient extends WebViewClient implements RegionCall
|
||||||
if (dataObjects != null) {
|
if (dataObjects != null) {
|
||||||
for (BinaryMapDataObject b : dataObjects) {
|
for (BinaryMapDataObject b : dataObjects) {
|
||||||
if (regions.contain(b, x31, y31)) {
|
if (regions.contain(b, x31, y31)) {
|
||||||
downloadName = regions.getDownloadName(b);
|
String downloadName = regions.getDownloadName(b);
|
||||||
if (downloadName != null && !downloadName.isEmpty()) {
|
if (downloadName != null && !downloadName.isEmpty()) {
|
||||||
break;
|
regionNames.add(downloadName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return downloadName;
|
return regionNames;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stopRunningAsyncTasks() {
|
public void stopRunningAsyncTasks() {
|
||||||
|
@ -233,10 +233,11 @@ public class WikivoyageWebViewClient extends WebViewClient implements RegionCall
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class FetchWikiRegion extends AsyncTask<Void, Void, String[]> {
|
private static class FetchWikiRegion extends AsyncTask<Void, Void, String> {
|
||||||
|
|
||||||
private RegionCallback callback;
|
private RegionCallback callback;
|
||||||
private OsmandRegions osmandRegions;
|
private OsmandRegions osmandRegions;
|
||||||
|
private List<String> downloadNames;
|
||||||
private double lat;
|
private double lat;
|
||||||
private double lon;
|
private double lon;
|
||||||
|
|
||||||
|
@ -248,11 +249,10 @@ public class WikivoyageWebViewClient extends WebViewClient implements RegionCall
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String[] doInBackground(Void... voids) {
|
protected String doInBackground(Void... voids) {
|
||||||
String downloadName = getDownloadName(lat, lon, osmandRegions);
|
downloadNames = getDownloadNames(lat, lon, osmandRegions);
|
||||||
downloadName = downloadName == null ? "" : downloadName;
|
String downloadName = downloadNames.isEmpty() ? "" : downloadNames.get(0);
|
||||||
return new String[]{downloadName,
|
return osmandRegions.getLocaleName(downloadName, false);
|
||||||
osmandRegions.getLocaleName(downloadName, false)};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -261,9 +261,9 @@ public class WikivoyageWebViewClient extends WebViewClient implements RegionCall
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(String[] result) {
|
protected void onPostExecute(String result) {
|
||||||
if (callback != null) {
|
if (callback != null) {
|
||||||
callback.onRegionFound(result);
|
callback.onRegionFound(result, downloadNames);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -271,7 +271,7 @@ public class WikivoyageWebViewClient extends WebViewClient implements RegionCall
|
||||||
private static class WikiArticleSearchTask extends AsyncTask<Void, Void, List<Amenity>> {
|
private static class WikiArticleSearchTask extends AsyncTask<Void, Void, List<Amenity>> {
|
||||||
private ProgressDialog dialog;
|
private ProgressDialog dialog;
|
||||||
private String name;
|
private String name;
|
||||||
private String regionDownloadName;
|
private List<String> regionDownloadNames;
|
||||||
private String regionName;
|
private String regionName;
|
||||||
private OsmandRegions regions;
|
private OsmandRegions regions;
|
||||||
private WeakReference<MapActivity> weakContext;
|
private WeakReference<MapActivity> weakContext;
|
||||||
|
@ -283,7 +283,7 @@ public class WikivoyageWebViewClient extends WebViewClient implements RegionCall
|
||||||
private FragmentManager fragmentManager;
|
private FragmentManager fragmentManager;
|
||||||
|
|
||||||
WikiArticleSearchTask(TravelArticle article, String articleName, String regionName, FragmentManager fragmentManager,
|
WikiArticleSearchTask(TravelArticle article, String articleName, String regionName, FragmentManager fragmentManager,
|
||||||
String lang, String regionDownloadName,
|
String lang, List<String> regionDownloadNames,
|
||||||
MapActivity context, boolean nightMode, String url) {
|
MapActivity context, boolean nightMode, String url) {
|
||||||
this.fragmentManager = fragmentManager;
|
this.fragmentManager = fragmentManager;
|
||||||
this.regionName = regionName;
|
this.regionName = regionName;
|
||||||
|
@ -296,7 +296,7 @@ public class WikivoyageWebViewClient extends WebViewClient implements RegionCall
|
||||||
dialog = createProgressDialog();
|
dialog = createProgressDialog();
|
||||||
this.isNightMode = nightMode;
|
this.isNightMode = nightMode;
|
||||||
this.url = url;
|
this.url = url;
|
||||||
this.regionDownloadName = regionDownloadName;
|
this.regionDownloadNames = regionDownloadNames;
|
||||||
this.article = article;
|
this.article = article;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -309,13 +309,13 @@ public class WikivoyageWebViewClient extends WebViewClient implements RegionCall
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected List<Amenity> doInBackground(Void... voids) {
|
protected List<Amenity> doInBackground(Void... voids) {
|
||||||
if (regionDownloadName == null && article != null && !isCancelled()) {
|
if (regionDownloadNames == null && article != null && !isCancelled()) {
|
||||||
regionDownloadName = getDownloadName(article.getLat(), article.getLon(), regions);
|
regionDownloadNames = getDownloadNames(article.getLat(), article.getLon(), regions);
|
||||||
}
|
}
|
||||||
OsmandApplication application = applicationReference.get();
|
OsmandApplication application = applicationReference.get();
|
||||||
if (application != null && !isCancelled()) {
|
if (application != null && !isCancelled()) {
|
||||||
AmenityIndexRepositoryBinary index = application.getResourceManager()
|
AmenityIndexRepositoryBinary index = application.getResourceManager()
|
||||||
.getWikiRepositoryByRegionName(regionDownloadName);
|
.getWikiRepositoryByRegionName(regionDownloadNames);
|
||||||
if (index != null && !isCancelled()) {
|
if (index != null && !isCancelled()) {
|
||||||
return new ArrayList<>(index.searchAmenitiesByName(0, 0, 0, 0,
|
return new ArrayList<>(index.searchAmenitiesByName(0, 0, 0, 0,
|
||||||
Integer.MAX_VALUE, Integer.MAX_VALUE, name, null));
|
Integer.MAX_VALUE, Integer.MAX_VALUE, name, null));
|
||||||
|
|
Loading…
Reference in a new issue