Fix travel helpers

This commit is contained in:
max-klaus 2020-12-18 17:23:39 +03:00
parent bbd7678425
commit d4c4ed3181
3 changed files with 73 additions and 38 deletions

View file

@ -105,7 +105,7 @@ public class TravelDbHelper implements TravelHelper {
private File selectedTravelBook = null;
private List<File> existingTravelBooks = new ArrayList<>();
private List<TravelArticle> popularArticles = new ArrayList<TravelArticle>();
private List<TravelArticle> popularArticles = new ArrayList<>();
public TravelDbHelper(OsmandApplication application) {
@ -126,6 +126,7 @@ public class TravelDbHelper implements TravelHelper {
return false;
}
@Override
public TravelLocalDataHelper getBookmarksHelper() {
return localDataHelper;
}
@ -135,6 +136,7 @@ public class TravelDbHelper implements TravelHelper {
return selectedTravelBook != null;
}
@Override
public void initializeDataOnAppStartup() {
List<File> files = getPossibleFiles();
String travelBook = application.getSettings().SELECTED_TRAVEL_BOOK.get();
@ -168,12 +170,14 @@ public class TravelDbHelper implements TravelHelper {
return null;
}
@Override
public void initializeDataToDisplay() {
localDataHelper.refreshCachedData();
loadPopularArticles();
}
@Override
public String getSelectedTravelBookName() {
if (selectedTravelBook != null) {
return selectedTravelBook.getName();
@ -210,8 +214,9 @@ public class TravelDbHelper implements TravelHelper {
}
}
@Override
@NonNull
public List<WikivoyageSearchResult> search(final String searchQuery) {
public List<WikivoyageSearchResult> search(@NonNull String searchQuery) {
List<WikivoyageSearchResult> res = new ArrayList<>();
SQLiteConnection conn = openConnection();
String[] queries = searchQuery.replace('_', ' ').replace('/', ' ').split(" ");
@ -260,6 +265,7 @@ public class TravelDbHelper implements TravelHelper {
return list;
}
@Override
@NonNull
public List<TravelArticle> getPopularArticles() {
return popularArticles;
@ -270,7 +276,7 @@ public class TravelDbHelper implements TravelHelper {
String language = application.getLanguage();
SQLiteConnection conn = openConnection();
if (conn == null) {
popularArticles = new ArrayList<TravelArticle>();
popularArticles = new ArrayList<>();
return popularArticles;
}
String LANG_WHERE = " WHERE " + ARTICLES_COL_LANG + " = '" + language + "'";
@ -444,8 +450,9 @@ public class TravelDbHelper implements TravelHelper {
}
@NonNull
@Override
public LinkedHashMap<WikivoyageSearchResult, List<WikivoyageSearchResult>> getNavigationMap(
final TravelArticle article) {
@NonNull final TravelArticle article) {
String lang = article.getLang();
String title = article.getTitle();
if (TextUtils.isEmpty(lang) || TextUtils.isEmpty(title)) {
@ -533,8 +540,9 @@ public class TravelDbHelper implements TravelHelper {
return res;
}
@Override
@Nullable
public TravelArticle getArticleById(String routeId, String lang) {
public TravelArticle getArticleById(@NonNull String routeId, @NonNull String lang) {
TravelArticle res = null;
SQLiteConnection conn = openConnection();
if (conn != null && !Algorithms.isEmpty(routeId)) {
@ -550,8 +558,9 @@ public class TravelDbHelper implements TravelHelper {
return res;
}
@Override
@Nullable
public TravelArticle getArticleByTitle(String title, String lang) {
public TravelArticle getArticleByTitle(@NonNull String title, @NonNull String lang) {
TravelArticle res = null;
SQLiteConnection conn = openConnection();
if (conn != null) {
@ -567,7 +576,9 @@ public class TravelDbHelper implements TravelHelper {
return res;
}
public String getArticleId(String title, String lang) {
@NonNull
@Override
public String getArticleId(@NonNull String title, @NonNull String lang) {
String res = "";
SQLiteConnection conn = openConnection();
if (conn != null) {
@ -585,7 +596,8 @@ public class TravelDbHelper implements TravelHelper {
}
@NonNull
public ArrayList<String> getArticleLangs(String routeId) {
@Override
public ArrayList<String> getArticleLangs(@NonNull String routeId) {
ArrayList<String> res = new ArrayList<>();
SQLiteConnection conn = openConnection();
if (conn != null) {
@ -652,12 +664,16 @@ public class TravelDbHelper implements TravelHelper {
return nm.substring(0, nm.indexOf('.')).replace('_', ' ');
}
public String getGPXName(TravelArticle article) {
@NonNull
@Override
public String getGPXName(@NonNull final TravelArticle article) {
return article.getTitle().replace('/', '_').replace('\'', '_')
.replace('\"', '_') + IndexConstants.GPX_FILE_EXT;
}
public File createGpxFile(TravelArticle article) {
@NonNull
@Override
public File createGpxFile(@NonNull final TravelArticle article) {
final GPXFile gpx = article.getGpxFile();
File file = application.getAppPath(IndexConstants.GPX_TRAVEL_DIR + getGPXName(article));
if (!file.exists()) {

View file

@ -19,26 +19,31 @@ public interface TravelHelper {
boolean isAnyTravelBookPresent();
@NonNull
List<WikivoyageSearchResult> search(final String searchQuery);
List<WikivoyageSearchResult> search(@NonNull String searchQuery);
@NonNull
List<TravelArticle> getPopularArticles();
Map<WikivoyageSearchResult, List<WikivoyageSearchResult>> getNavigationMap(
final TravelArticle article);
@NonNull
Map<WikivoyageSearchResult, List<WikivoyageSearchResult>> getNavigationMap(@NonNull final TravelArticle article);
@Nullable
TravelArticle getArticleById(String routeId, String lang);
TravelArticle getArticleById(@NonNull String routeId, @NonNull String lang);
TravelArticle getArticleByTitle(String title, String lang);
@Nullable
TravelArticle getArticleByTitle(@NonNull String title, @NonNull String lang);
String getArticleId(String title, String lang);
@NonNull
String getArticleId(@NonNull String title, @NonNull String lang);
ArrayList<String> getArticleLangs(String routeId);
@NonNull
ArrayList<String> getArticleLangs(@NonNull String routeId);
String getGPXName(TravelArticle article);
@NonNull
String getGPXName(@NonNull final TravelArticle article);
File createGpxFile(TravelArticle article);
@NonNull
File createGpxFile(@NonNull final TravelArticle article);
// TODO: this method should be deleted once TravelDBHelper is deleted
// For TravelOBFHelper it could always return "" and should be no problem

View file

@ -1,15 +1,18 @@
package net.osmand.plus.wikivoyage.data;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import net.osmand.Collator;
import net.osmand.CollatorStringMatcher;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.IndexConstants;
import net.osmand.OsmAndCollator;
import net.osmand.PlatformUtil;
import net.osmand.ResultMatcher;
import net.osmand.binary.BinaryMapIndexReader;
import net.osmand.binary.BinaryMapIndexReader.SearchPoiTypeFilter;
import net.osmand.data.Amenity;
import net.osmand.data.LatLon;
import net.osmand.osm.PoiCategory;
@ -66,7 +69,7 @@ public class TravelObfHelper implements TravelHelper {
@NonNull
public List<TravelArticle> loadPopularArticles() {
String language = app.getLanguage();
popularArticles.clear();
List<TravelArticle> popularArticles = new ArrayList<>();
List<Amenity> amenities;
for (BinaryMapIndexReader travelBookReader : getTravelBookReaders()) {
try {
@ -100,11 +103,12 @@ public class TravelObfHelper implements TravelHelper {
LOG.error(e.getMessage());
}
}
this.popularArticles = popularArticles;
return popularArticles;
}
BinaryMapIndexReader.SearchPoiTypeFilter getSearchRouteArticleFilter() {
return new BinaryMapIndexReader.SearchPoiTypeFilter() {
SearchPoiTypeFilter getSearchRouteArticleFilter() {
return new SearchPoiTypeFilter() {
@Override
public boolean accept(PoiCategory type, String subcategory) {
return subcategory.equals(ROUTE_ARTICLE);
@ -148,7 +152,7 @@ public class TravelObfHelper implements TravelHelper {
@NonNull
@Override
public List<WikivoyageSearchResult> search(String searchQuery) {
public List<WikivoyageSearchResult> search(@NonNull String searchQuery) {
return Collections.emptyList();
}
@ -158,13 +162,14 @@ public class TravelObfHelper implements TravelHelper {
return popularArticles;
}
@NonNull
@Override
public Map<WikivoyageSearchResult, List<WikivoyageSearchResult>> getNavigationMap(TravelArticle article) {
return null;
public Map<WikivoyageSearchResult, List<WikivoyageSearchResult>> getNavigationMap(@NonNull final TravelArticle article) {
return Collections.emptyMap();
}
@Override
public TravelArticle getArticleById(String routeId, String lang) {
public TravelArticle getArticleById(@NonNull String routeId, @NonNull String lang) {
TravelArticle article = cachedArticles.get(routeId);
if (article != null) {
return article;
@ -173,7 +178,7 @@ public class TravelObfHelper implements TravelHelper {
}
}
public TravelArticle getArticleByIdFromTravelBooks(final String routeId, final String lang) {
private TravelArticle getArticleByIdFromTravelBooks(final String routeId, final String lang) {
TravelArticle article = null;
final List<Amenity> amenities = new ArrayList<>();
for (BinaryMapIndexReader travelBookReader : getTravelBookReaders()) {
@ -210,8 +215,9 @@ public class TravelObfHelper implements TravelHelper {
return article;
}
@Nullable
@Override
public TravelArticle getArticleByTitle(final String title, final String lang) {
public TravelArticle getArticleByTitle(@NonNull final String title, @NonNull final String lang) {
TravelArticle article = null;
final List<Amenity> amenities = new ArrayList<>();
for (BinaryMapIndexReader travelBookReader : getTravelBookReaders()) {
@ -257,22 +263,28 @@ public class TravelObfHelper implements TravelHelper {
}
}
@NonNull
@Override
public String getArticleId(String title, String lang) {
public String getArticleId(@NonNull String title, @NonNull String lang) {
TravelArticle a = null;
for (TravelArticle article : cachedArticles.values()) {
if (article.getTitle().equals(title)) {
return article.getRouteId();
a = article;
break;
}
}
TravelArticle article = getArticleByTitle(title, lang);
if (article != null) {
return article.getRouteId();
if (a == null) {
TravelArticle article = getArticleByTitle(title, lang);
if (article != null) {
a = article;
}
}
return null;
return a != null && a.getRouteId() != null ? a.getRouteId() : "";
}
@NonNull
@Override
public ArrayList<String> getArticleLangs(String routeId) {
public ArrayList<String> getArticleLangs(@NonNull String routeId) {
ArrayList<String> res = new ArrayList<>();
res.add("en");
for (TravelArticle article : popularArticles) {
@ -283,15 +295,17 @@ public class TravelObfHelper implements TravelHelper {
return res;
}
@NonNull
@Override
public String getGPXName(TravelArticle article) {
public String getGPXName(@NonNull final TravelArticle article) {
return article.getTitle().replace('/', '_').replace('\'', '_')
.replace('\"', '_') + IndexConstants.GPX_FILE_EXT;
}
@NonNull
@Override
public File createGpxFile(TravelArticle article) {
final GPXUtilities.GPXFile gpx = article.getGpxFile();
public File createGpxFile(@NonNull final TravelArticle article) {
final GPXFile gpx = article.getGpxFile();
File file = app.getAppPath(IndexConstants.GPX_TRAVEL_DIR + getGPXName(article));
if (!file.exists()) {
GPXUtilities.writeGpxFile(file, gpx);